| 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 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 31 | // Log debug messages about channel creation | 
 | 32 | #define DEBUG_CHANNEL_CREATION 0 | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 40 | // Log debug messages about touch occlusion | 
 | 41 | // STOPSHIP(b/169067926): Set to false | 
 | 42 | static constexpr bool DEBUG_TOUCH_OCCLUSION = true; | 
 | 43 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | // Log debug messages about the app switch latency optimization. | 
 | 45 | #define DEBUG_APP_SWITCH 0 | 
 | 46 |  | 
 | 47 | // Log debug messages about hover events. | 
 | 48 | #define DEBUG_HOVER 0 | 
 | 49 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 50 | #include <android-base/chrono_utils.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 51 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 52 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 53 | #include <binder/Binder.h> | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 54 | #include <binder/IServiceManager.h> | 
 | 55 | #include <com/android/internal/compat/IPlatformCompatNative.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 56 | #include <input/InputDevice.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 57 | #include <input/InputWindow.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 58 | #include <log/log.h> | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 59 | #include <log/log_event_list.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 60 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 61 | #include <statslog.h> | 
 | 62 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 63 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 65 | #include <cerrno> | 
 | 66 | #include <cinttypes> | 
 | 67 | #include <climits> | 
 | 68 | #include <cstddef> | 
 | 69 | #include <ctime> | 
 | 70 | #include <queue> | 
 | 71 | #include <sstream> | 
 | 72 |  | 
 | 73 | #include "Connection.h" | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 74 | #include "InputDispatcher.h" | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 75 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | #define INDENT "  " | 
 | 77 | #define INDENT2 "    " | 
 | 78 | #define INDENT3 "      " | 
 | 79 | #define INDENT4 "        " | 
 | 80 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 81 | using android::base::StringPrintf; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 82 | using android::os::BlockUntrustedTouchesMode; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 83 | using android::os::IInputConstants; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 84 | using android::os::InputEventInjectionResult; | 
 | 85 | using android::os::InputEventInjectionSync; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 86 | using com::android::internal::compat::IPlatformCompatNative; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 87 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 88 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 |  | 
 | 90 | // Default input dispatching timeout if there is no focused application or paused window | 
 | 91 | // from which to determine an appropriate dispatching timeout. | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 92 | constexpr std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = | 
 | 93 |         std::chrono::milliseconds(android::os::IInputConstants::DEFAULT_DISPATCHING_TIMEOUT_MILLIS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 |  | 
 | 95 | // Amount of time to allow for all pending events to be processed when an app switch | 
 | 96 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
 | 97 | // 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] | 98 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 |  | 
 | 100 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) | 
 | 101 | // before considering it stale and dropping it. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 102 | constexpr nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 104 | // 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] | 105 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
 | 106 |  | 
 | 107 | // Log a warning when an interception call takes longer than this to process. | 
 | 108 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 109 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 110 | // Additional key latency in case a connection is still processing some motion events. | 
 | 111 | // This will help with the case when a user touched a button that opens a new window, | 
 | 112 | // and gives us the chance to dispatch the key to this new window. | 
 | 113 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
 | 114 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 116 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
 | 117 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 118 | // Event log tags. See EventLogTags.logtags for reference | 
 | 119 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
 | 120 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
 | 121 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 122 | static inline nsecs_t now() { | 
 | 123 |     return systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 124 | } | 
 | 125 |  | 
 | 126 | static inline const char* toString(bool value) { | 
 | 127 |     return value ? "true" : "false"; | 
 | 128 | } | 
 | 129 |  | 
 | 130 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 131 |     return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
 | 132 |             AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | } | 
 | 134 |  | 
 | 135 | static bool isValidKeyAction(int32_t action) { | 
 | 136 |     switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 137 |         case AKEY_EVENT_ACTION_DOWN: | 
 | 138 |         case AKEY_EVENT_ACTION_UP: | 
 | 139 |             return true; | 
 | 140 |         default: | 
 | 141 |             return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 142 |     } | 
 | 143 | } | 
 | 144 |  | 
 | 145 | static bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 146 |     if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 147 |         ALOGE("Key event has invalid action code 0x%x", action); | 
 | 148 |         return false; | 
 | 149 |     } | 
 | 150 |     return true; | 
 | 151 | } | 
 | 152 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 153 | static bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 154 |     switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 155 |         case AMOTION_EVENT_ACTION_DOWN: | 
 | 156 |         case AMOTION_EVENT_ACTION_UP: | 
 | 157 |         case AMOTION_EVENT_ACTION_CANCEL: | 
 | 158 |         case AMOTION_EVENT_ACTION_MOVE: | 
 | 159 |         case AMOTION_EVENT_ACTION_OUTSIDE: | 
 | 160 |         case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
 | 161 |         case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
 | 162 |         case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
 | 163 |         case AMOTION_EVENT_ACTION_SCROLL: | 
 | 164 |             return true; | 
 | 165 |         case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
 | 166 |         case AMOTION_EVENT_ACTION_POINTER_UP: { | 
 | 167 |             int32_t index = getMotionEventActionPointerIndex(action); | 
 | 168 |             return index >= 0 && index < pointerCount; | 
 | 169 |         } | 
 | 170 |         case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
 | 171 |         case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
 | 172 |             return actionButton != 0; | 
 | 173 |         default: | 
 | 174 |             return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 |     } | 
 | 176 | } | 
 | 177 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 178 | static int64_t millis(std::chrono::nanoseconds t) { | 
 | 179 |     return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
 | 180 | } | 
 | 181 |  | 
| Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 182 | static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 183 |                                 const PointerProperties* pointerProperties) { | 
 | 184 |     if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 |         ALOGE("Motion event has invalid action code 0x%x", action); | 
 | 186 |         return false; | 
 | 187 |     } | 
 | 188 |     if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Narayan Kamath | 37764c7 | 2014-03-27 14:21:09 +0000 | [diff] [blame] | 189 |         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] | 190 |               pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 |         return false; | 
 | 192 |     } | 
 | 193 |     BitSet32 pointerIdBits; | 
 | 194 |     for (size_t i = 0; i < pointerCount; i++) { | 
 | 195 |         int32_t id = pointerProperties[i].id; | 
 | 196 |         if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 197 |             ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
 | 198 |                   MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 |             return false; | 
 | 200 |         } | 
 | 201 |         if (pointerIdBits.hasBit(id)) { | 
 | 202 |             ALOGE("Motion event has duplicate pointer id %d", id); | 
 | 203 |             return false; | 
 | 204 |         } | 
 | 205 |         pointerIdBits.markBit(id); | 
 | 206 |     } | 
 | 207 |     return true; | 
 | 208 | } | 
 | 209 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 210 | static std::string dumpRegion(const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 |     if (region.isEmpty()) { | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 212 |         return "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 |     } | 
 | 214 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 215 |     std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 |     bool first = true; | 
 | 217 |     Region::const_iterator cur = region.begin(); | 
 | 218 |     Region::const_iterator const tail = region.end(); | 
 | 219 |     while (cur != tail) { | 
 | 220 |         if (first) { | 
 | 221 |             first = false; | 
 | 222 |         } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 223 |             dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 |         } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 225 |         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] | 226 |         cur++; | 
 | 227 |     } | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 228 |     return dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 229 | } | 
 | 230 |  | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 231 | static std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { | 
 | 232 |     constexpr size_t maxEntries = 50; // max events to print | 
 | 233 |     constexpr size_t skipBegin = maxEntries / 2; | 
 | 234 |     const size_t skipEnd = queue.size() - maxEntries / 2; | 
 | 235 |     // skip from maxEntries / 2 ... size() - maxEntries/2 | 
 | 236 |     // only print from 0 .. skipBegin and then from skipEnd .. size() | 
 | 237 |  | 
 | 238 |     std::string dump; | 
 | 239 |     for (size_t i = 0; i < queue.size(); i++) { | 
 | 240 |         const DispatchEntry& entry = *queue[i]; | 
 | 241 |         if (i >= skipBegin && i < skipEnd) { | 
 | 242 |             dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); | 
 | 243 |             i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' | 
 | 244 |             continue; | 
 | 245 |         } | 
 | 246 |         dump.append(INDENT4); | 
 | 247 |         dump += entry.eventEntry->getDescription(); | 
 | 248 |         dump += StringPrintf(", seq=%" PRIu32 | 
 | 249 |                              ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", | 
 | 250 |                              entry.seq, entry.targetFlags, entry.resolvedAction, | 
 | 251 |                              ns2ms(currentTime - entry.eventEntry->eventTime)); | 
 | 252 |         if (entry.deliveryTime != 0) { | 
 | 253 |             // This entry was delivered, so add information on how long we've been waiting | 
 | 254 |             dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); | 
 | 255 |         } | 
 | 256 |         dump.append("\n"); | 
 | 257 |     } | 
 | 258 |     return dump; | 
 | 259 | } | 
 | 260 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 261 | /** | 
 | 262 |  * Find the entry in std::unordered_map by key, and return it. | 
 | 263 |  * If the entry is not found, return a default constructed entry. | 
 | 264 |  * | 
 | 265 |  * Useful when the entries are vectors, since an empty vector will be returned | 
 | 266 |  * if the entry is not found. | 
 | 267 |  * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
 | 268 |  */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 269 | template <typename K, typename V> | 
 | 270 | static V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 271 |     auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 272 |     return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 273 | } | 
 | 274 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 275 | /** | 
 | 276 |  * Find the entry in std::unordered_map by value, and remove it. | 
 | 277 |  * If more than one entry has the same value, then all matching | 
 | 278 |  * key-value pairs will be removed. | 
 | 279 |  * | 
 | 280 |  * Return true if at least one value has been removed. | 
 | 281 |  */ | 
 | 282 | template <typename K, typename V> | 
 | 283 | static bool removeByValue(std::unordered_map<K, V>& map, const V& value) { | 
 | 284 |     bool removed = false; | 
 | 285 |     for (auto it = map.begin(); it != map.end();) { | 
 | 286 |         if (it->second == value) { | 
 | 287 |             it = map.erase(it); | 
 | 288 |             removed = true; | 
 | 289 |         } else { | 
 | 290 |             it++; | 
 | 291 |         } | 
 | 292 |     } | 
 | 293 |     return removed; | 
 | 294 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 295 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 296 | /** | 
 | 297 |  * Find the entry in std::unordered_map by key and return the value as an optional. | 
 | 298 |  */ | 
 | 299 | template <typename K, typename V> | 
 | 300 | static std::optional<V> getOptionalValueByKey(const std::unordered_map<K, V>& map, K key) { | 
 | 301 |     auto it = map.find(key); | 
 | 302 |     return it != map.end() ? std::optional<V>{it->second} : std::nullopt; | 
 | 303 | } | 
 | 304 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 305 | static bool haveSameToken(const sp<InputWindowHandle>& first, const sp<InputWindowHandle>& second) { | 
 | 306 |     if (first == second) { | 
 | 307 |         return true; | 
 | 308 |     } | 
 | 309 |  | 
 | 310 |     if (first == nullptr || second == nullptr) { | 
 | 311 |         return false; | 
 | 312 |     } | 
 | 313 |  | 
 | 314 |     return first->getToken() == second->getToken(); | 
 | 315 | } | 
 | 316 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 317 | static bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
 | 318 |     return currentTime - entry.eventTime >= STALE_EVENT_TIMEOUT; | 
 | 319 | } | 
 | 320 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 321 | static std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 322 |                                                           std::shared_ptr<EventEntry> eventEntry, | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 323 |                                                           int32_t inputTargetFlags) { | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 324 |     if (eventEntry->type == EventEntry::Type::MOTION) { | 
 | 325 |         const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
 | 326 |         if (motionEntry.source & AINPUT_SOURCE_CLASS_JOYSTICK) { | 
 | 327 |             const ui::Transform identityTransform; | 
 | 328 |             // Use identity transform for joystick events events because they don't depend on | 
 | 329 |             // the window info | 
 | 330 |             return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, identityTransform, | 
 | 331 |                                                    1.0f /*globalScaleFactor*/); | 
 | 332 |         } | 
 | 333 |     } | 
 | 334 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 335 |     if (inputTarget.useDefaultPointerTransform()) { | 
 | 336 |         const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 337 |         return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 338 |                                                inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 339 |     } | 
 | 340 |  | 
 | 341 |     ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
 | 342 |     const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
 | 343 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 344 |     std::vector<PointerCoords> pointerCoords; | 
 | 345 |     pointerCoords.resize(motionEntry.pointerCount); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 346 |  | 
 | 347 |     // Use the first pointer information to normalize all other pointers. This could be any pointer | 
 | 348 |     // as long as all other pointers are normalized to the same value and the final DispatchEntry | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 349 |     // uses the transform for the normalized pointer. | 
 | 350 |     const ui::Transform& firstPointerTransform = | 
 | 351 |             inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
 | 352 |     ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 353 |  | 
 | 354 |     // Iterate through all pointers in the event to normalize against the first. | 
 | 355 |     for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
 | 356 |         const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
 | 357 |         uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 358 |         const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 359 |  | 
 | 360 |         pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 361 |         // First, apply the current pointer's transform to update the coordinates into | 
 | 362 |         // window space. | 
 | 363 |         pointerCoords[pointerIndex].transform(currTransform); | 
 | 364 |         // Next, apply the inverse transform of the normalized coordinates so the | 
 | 365 |         // current coordinates are transformed into the normalized coordinate space. | 
 | 366 |         pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 367 |     } | 
 | 368 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 369 |     std::unique_ptr<MotionEntry> combinedMotionEntry = | 
 | 370 |             std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, | 
 | 371 |                                           motionEntry.deviceId, motionEntry.source, | 
 | 372 |                                           motionEntry.displayId, motionEntry.policyFlags, | 
 | 373 |                                           motionEntry.action, motionEntry.actionButton, | 
 | 374 |                                           motionEntry.flags, motionEntry.metaState, | 
 | 375 |                                           motionEntry.buttonState, motionEntry.classification, | 
 | 376 |                                           motionEntry.edgeFlags, motionEntry.xPrecision, | 
 | 377 |                                           motionEntry.yPrecision, motionEntry.xCursorPosition, | 
 | 378 |                                           motionEntry.yCursorPosition, motionEntry.downTime, | 
 | 379 |                                           motionEntry.pointerCount, motionEntry.pointerProperties, | 
 | 380 |                                           pointerCoords.data(), 0 /* xOffset */, 0 /* yOffset */); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 381 |  | 
 | 382 |     if (motionEntry.injectionState) { | 
 | 383 |         combinedMotionEntry->injectionState = motionEntry.injectionState; | 
 | 384 |         combinedMotionEntry->injectionState->refCount += 1; | 
 | 385 |     } | 
 | 386 |  | 
 | 387 |     std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 388 |             std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, | 
 | 389 |                                             firstPointerTransform, inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 390 |     return dispatchEntry; | 
 | 391 | } | 
 | 392 |  | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 393 | static void addGestureMonitors(const std::vector<Monitor>& monitors, | 
 | 394 |                                std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, | 
 | 395 |                                float yOffset = 0) { | 
 | 396 |     if (monitors.empty()) { | 
 | 397 |         return; | 
 | 398 |     } | 
 | 399 |     outTouchedMonitors.reserve(monitors.size() + outTouchedMonitors.size()); | 
 | 400 |     for (const Monitor& monitor : monitors) { | 
 | 401 |         outTouchedMonitors.emplace_back(monitor, xOffset, yOffset); | 
 | 402 |     } | 
 | 403 | } | 
 | 404 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 405 | static status_t openInputChannelPair(const std::string& name, | 
 | 406 |                                      std::shared_ptr<InputChannel>& serverChannel, | 
 | 407 |                                      std::unique_ptr<InputChannel>& clientChannel) { | 
 | 408 |     std::unique_ptr<InputChannel> uniqueServerChannel; | 
 | 409 |     status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); | 
 | 410 |  | 
 | 411 |     serverChannel = std::move(uniqueServerChannel); | 
 | 412 |     return result; | 
 | 413 | } | 
 | 414 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 415 | const char* InputDispatcher::typeToString(InputDispatcher::FocusResult result) { | 
 | 416 |     switch (result) { | 
 | 417 |         case InputDispatcher::FocusResult::OK: | 
 | 418 |             return "Ok"; | 
 | 419 |         case InputDispatcher::FocusResult::NO_WINDOW: | 
 | 420 |             return "Window not found"; | 
 | 421 |         case InputDispatcher::FocusResult::NOT_FOCUSABLE: | 
 | 422 |             return "Window not focusable"; | 
 | 423 |         case InputDispatcher::FocusResult::NOT_VISIBLE: | 
 | 424 |             return "Window not visible"; | 
 | 425 |     } | 
 | 426 | } | 
 | 427 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 428 | template <typename T> | 
 | 429 | static bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { | 
 | 430 |     if (lhs == nullptr && rhs == nullptr) { | 
 | 431 |         return true; | 
 | 432 |     } | 
 | 433 |     if (lhs == nullptr || rhs == nullptr) { | 
 | 434 |         return false; | 
 | 435 |     } | 
 | 436 |     return *lhs == *rhs; | 
 | 437 | } | 
 | 438 |  | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 439 | static sp<IPlatformCompatNative> getCompatService() { | 
 | 440 |     sp<IBinder> service(defaultServiceManager()->getService(String16("platform_compat_native"))); | 
 | 441 |     if (service == nullptr) { | 
 | 442 |         ALOGE("Failed to link to compat service"); | 
 | 443 |         return nullptr; | 
 | 444 |     } | 
 | 445 |     return interface_cast<IPlatformCompatNative>(service); | 
 | 446 | } | 
 | 447 |  | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 448 | static KeyEvent createKeyEvent(const KeyEntry& entry) { | 
 | 449 |     KeyEvent event; | 
 | 450 |     event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
 | 451 |                      entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
 | 452 |                      entry.repeatCount, entry.downTime, entry.eventTime); | 
 | 453 |     return event; | 
 | 454 | } | 
 | 455 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 456 | static std::optional<int32_t> findMonitorPidByToken( | 
 | 457 |         const std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay, | 
 | 458 |         const sp<IBinder>& token) { | 
 | 459 |     for (const auto& it : monitorsByDisplay) { | 
 | 460 |         const std::vector<Monitor>& monitors = it.second; | 
 | 461 |         for (const Monitor& monitor : monitors) { | 
 | 462 |             if (monitor.inputChannel->getConnectionToken() == token) { | 
 | 463 |                 return monitor.pid; | 
 | 464 |             } | 
 | 465 |         } | 
 | 466 |     } | 
 | 467 |     return std::nullopt; | 
 | 468 | } | 
 | 469 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 470 | // --- InputDispatcher --- | 
 | 471 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 472 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
 | 473 |       : mPolicy(policy), | 
 | 474 |         mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 475 |         mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 476 |         mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 477 |         mAppSwitchSawKeyDown(false), | 
 | 478 |         mAppSwitchDueTime(LONG_LONG_MAX), | 
 | 479 |         mNextUnblockedEvent(nullptr), | 
 | 480 |         mDispatchEnabled(false), | 
 | 481 |         mDispatchFrozen(false), | 
 | 482 |         mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 483 |         // mInTouchMode will be initialized by the WindowManager to the default device config. | 
 | 484 |         // To avoid leaking stack in case that call never comes, and for tests, | 
 | 485 |         // initialize it here anyways. | 
 | 486 |         mInTouchMode(true), | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 487 |         mMaximumObscuringOpacityForTouch(1.0f), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 488 |         mFocusedDisplayId(ADISPLAY_ID_DEFAULT), | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 489 |         mFocusedWindowRequestedPointerCapture(false), | 
 | 490 |         mWindowTokenWithPointerCapture(nullptr), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 491 |         mCompatService(getCompatService()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 |     mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 493 |     mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 494 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 495 |     mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 496 |  | 
 | 497 |     policy->getDispatcherConfiguration(&mConfig); | 
 | 498 | } | 
 | 499 |  | 
 | 500 | InputDispatcher::~InputDispatcher() { | 
 | 501 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 502 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 503 |  | 
 | 504 |         resetKeyRepeatLocked(); | 
 | 505 |         releasePendingEventLocked(); | 
 | 506 |         drainInboundQueueLocked(); | 
 | 507 |     } | 
 | 508 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 509 |     while (!mConnectionsByFd.empty()) { | 
 | 510 |         sp<Connection> connection = mConnectionsByFd.begin()->second; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 511 |         removeInputChannel(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 |     } | 
 | 513 | } | 
 | 514 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 515 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 516 |     if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 517 |         return ALREADY_EXISTS; | 
 | 518 |     } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 519 |     mThread = std::make_unique<InputThread>( | 
 | 520 |             "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
 | 521 |     return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 522 | } | 
 | 523 |  | 
 | 524 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 525 |     if (mThread && mThread->isCallingThread()) { | 
 | 526 |         ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 527 |         return INVALID_OPERATION; | 
 | 528 |     } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 529 |     mThread.reset(); | 
 | 530 |     return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 531 | } | 
 | 532 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 533 | void InputDispatcher::dispatchOnce() { | 
 | 534 |     nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
 | 535 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 536 |         std::scoped_lock _l(mLock); | 
 | 537 |         mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 |  | 
 | 539 |         // Run a dispatch loop if there are no pending commands. | 
 | 540 |         // The dispatch loop might enqueue commands to run afterwards. | 
 | 541 |         if (!haveCommandsLocked()) { | 
 | 542 |             dispatchOnceInnerLocked(&nextWakeupTime); | 
 | 543 |         } | 
 | 544 |  | 
 | 545 |         // Run all pending commands if there are any. | 
 | 546 |         // If any commands were run then force the next poll to wake up immediately. | 
 | 547 |         if (runCommandsLockedInterruptible()) { | 
 | 548 |             nextWakeupTime = LONG_LONG_MIN; | 
 | 549 |         } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 550 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 551 |         // If we are still waiting for ack on some events, | 
 | 552 |         // we might have to wake up earlier to check if an app is anr'ing. | 
 | 553 |         const nsecs_t nextAnrCheck = processAnrsLocked(); | 
 | 554 |         nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
 | 555 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 556 |         // We are about to enter an infinitely long sleep, because we have no commands or | 
 | 557 |         // pending or queued events | 
 | 558 |         if (nextWakeupTime == LONG_LONG_MAX) { | 
 | 559 |             mDispatcherEnteredIdle.notify_all(); | 
 | 560 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 561 |     } // release lock | 
 | 562 |  | 
 | 563 |     // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
 | 564 |     nsecs_t currentTime = now(); | 
 | 565 |     int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
 | 566 |     mLooper->pollOnce(timeoutMillis); | 
 | 567 | } | 
 | 568 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 569 | /** | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 570 |  * Raise ANR if there is no focused window. | 
 | 571 |  * Before the ANR is raised, do a final state check: | 
 | 572 |  * 1. The currently focused application must be the same one we are waiting for. | 
 | 573 |  * 2. Ensure we still don't have a focused window. | 
 | 574 |  */ | 
 | 575 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
 | 576 |     // Check if the application that we are waiting for is still focused. | 
 | 577 |     std::shared_ptr<InputApplicationHandle> focusedApplication = | 
 | 578 |             getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
 | 579 |     if (focusedApplication == nullptr || | 
 | 580 |         focusedApplication->getApplicationToken() != | 
 | 581 |                 mAwaitedFocusedApplication->getApplicationToken()) { | 
 | 582 |         // Unexpected because we should have reset the ANR timer when focused application changed | 
 | 583 |         ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
 | 584 |               focusedApplication->getName().c_str()); | 
 | 585 |         return; // The focused application has changed. | 
 | 586 |     } | 
 | 587 |  | 
 | 588 |     const sp<InputWindowHandle>& focusedWindowHandle = | 
 | 589 |             getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
 | 590 |     if (focusedWindowHandle != nullptr) { | 
 | 591 |         return; // We now have a focused window. No need for ANR. | 
 | 592 |     } | 
 | 593 |     onAnrLocked(mAwaitedFocusedApplication); | 
 | 594 | } | 
 | 595 |  | 
 | 596 | /** | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 597 |  * Check if any of the connections' wait queues have events that are too old. | 
 | 598 |  * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
 | 599 |  * Return the time at which we should wake up next. | 
 | 600 |  */ | 
 | 601 | nsecs_t InputDispatcher::processAnrsLocked() { | 
 | 602 |     const nsecs_t currentTime = now(); | 
 | 603 |     nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
 | 604 |     // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
 | 605 |     if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
 | 606 |         if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 607 |             processNoFocusedWindowAnrLocked(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 608 |             mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 609 |             mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 610 |             return LONG_LONG_MIN; | 
 | 611 |         } else { | 
| Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 612 |             // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 613 |             nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
 | 614 |         } | 
 | 615 |     } | 
 | 616 |  | 
 | 617 |     // Check if any connection ANRs are due | 
 | 618 |     nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
 | 619 |     if (currentTime < nextAnrCheck) { // most likely scenario | 
 | 620 |         return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
 | 621 |     } | 
 | 622 |  | 
 | 623 |     // If we reached here, we have an unresponsive connection. | 
 | 624 |     sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
 | 625 |     if (connection == nullptr) { | 
 | 626 |         ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
 | 627 |         return nextAnrCheck; | 
 | 628 |     } | 
 | 629 |     connection->responsive = false; | 
 | 630 |     // Stop waking up for this unresponsive connection | 
 | 631 |     mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 632 |     onAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 633 |     return LONG_LONG_MIN; | 
 | 634 | } | 
 | 635 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 636 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(const sp<IBinder>& token) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 637 |     sp<InputWindowHandle> window = getWindowHandleLocked(token); | 
 | 638 |     if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 639 |         return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 640 |     } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 641 |     return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 642 | } | 
 | 643 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 644 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
 | 645 |     nsecs_t currentTime = now(); | 
 | 646 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 647 |     // Reset the key repeat timer whenever normal dispatch is suspended while the | 
 | 648 |     // device is in a non-interactive state.  This is to ensure that we abort a key | 
 | 649 |     // repeat if the device is just coming out of sleep. | 
 | 650 |     if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 |         resetKeyRepeatLocked(); | 
 | 652 |     } | 
 | 653 |  | 
 | 654 |     // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
 | 655 |     if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 656 |         if (DEBUG_FOCUS) { | 
 | 657 |             ALOGD("Dispatch frozen.  Waiting some more."); | 
 | 658 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 |         return; | 
 | 660 |     } | 
 | 661 |  | 
 | 662 |     // Optimize latency of app switches. | 
 | 663 |     // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
 | 664 |     // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
 | 665 |     bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
 | 666 |     if (mAppSwitchDueTime < *nextWakeupTime) { | 
 | 667 |         *nextWakeupTime = mAppSwitchDueTime; | 
 | 668 |     } | 
 | 669 |  | 
 | 670 |     // Ready to start a new event. | 
 | 671 |     // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 672 |     if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 673 |         if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 |             if (isAppSwitchDue) { | 
 | 675 |                 // The inbound queue is empty so the app switch key we were waiting | 
 | 676 |                 // for will never arrive.  Stop waiting for it. | 
 | 677 |                 resetPendingAppSwitchLocked(false); | 
 | 678 |                 isAppSwitchDue = false; | 
 | 679 |             } | 
 | 680 |  | 
 | 681 |             // Synthesize a key repeat if appropriate. | 
 | 682 |             if (mKeyRepeatState.lastKeyEntry) { | 
 | 683 |                 if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
 | 684 |                     mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
 | 685 |                 } else { | 
 | 686 |                     if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
 | 687 |                         *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
 | 688 |                     } | 
 | 689 |                 } | 
 | 690 |             } | 
 | 691 |  | 
 | 692 |             // Nothing to do if there is no pending event. | 
 | 693 |             if (!mPendingEvent) { | 
 | 694 |                 return; | 
 | 695 |             } | 
 | 696 |         } else { | 
 | 697 |             // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 698 |             mPendingEvent = mInboundQueue.front(); | 
 | 699 |             mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 700 |             traceInboundQueueLengthLocked(); | 
 | 701 |         } | 
 | 702 |  | 
 | 703 |         // Poke user activity for this event. | 
 | 704 |         if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 705 |             pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 707 |     } | 
 | 708 |  | 
 | 709 |     // Now we have an event to dispatch. | 
 | 710 |     // 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] | 711 |     ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 712 |     bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 713 |     DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 |     if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 715 |         dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 |     } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 717 |         dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 |     } | 
 | 719 |  | 
 | 720 |     if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 721 |         mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 |     } | 
 | 723 |  | 
 | 724 |     switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 725 |         case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 726 |             const ConfigurationChangedEntry& typedEntry = | 
 | 727 |                     static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 728 |             done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 729 |             dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 730 |             break; | 
 | 731 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 732 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 733 |         case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 734 |             const DeviceResetEntry& typedEntry = | 
 | 735 |                     static_cast<const DeviceResetEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 736 |             done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 737 |             dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 738 |             break; | 
 | 739 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 741 |         case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 742 |             std::shared_ptr<FocusEntry> typedEntry = | 
 | 743 |                     std::static_pointer_cast<FocusEntry>(mPendingEvent); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 744 |             dispatchFocusLocked(currentTime, typedEntry); | 
 | 745 |             done = true; | 
 | 746 |             dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
 | 747 |             break; | 
 | 748 |         } | 
 | 749 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 750 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
 | 751 |             const auto typedEntry = | 
 | 752 |                     std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); | 
 | 753 |             dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); | 
 | 754 |             done = true; | 
 | 755 |             break; | 
 | 756 |         } | 
 | 757 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 758 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 759 |             std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 760 |             if (isAppSwitchDue) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 761 |                 if (isAppSwitchKeyEvent(*keyEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 762 |                     resetPendingAppSwitchLocked(true); | 
 | 763 |                     isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 764 |                 } else if (dropReason == DropReason::NOT_DROPPED) { | 
 | 765 |                     dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 766 |                 } | 
 | 767 |             } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 768 |             if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 769 |                 dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 770 |             } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 771 |             if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
 | 772 |                 dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 773 |             } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 774 |             done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 775 |             break; | 
 | 776 |         } | 
 | 777 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 778 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 779 |             std::shared_ptr<MotionEntry> motionEntry = | 
 | 780 |                     std::static_pointer_cast<MotionEntry>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 781 |             if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
 | 782 |                 dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 |             } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 784 |             if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 785 |                 dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 786 |             } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 787 |             if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
 | 788 |                 dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 789 |             } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 790 |             done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 791 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 |         } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 793 |  | 
 | 794 |         case EventEntry::Type::SENSOR: { | 
 | 795 |             std::shared_ptr<SensorEntry> sensorEntry = | 
 | 796 |                     std::static_pointer_cast<SensorEntry>(mPendingEvent); | 
 | 797 |             if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
 | 798 |                 dropReason = DropReason::APP_SWITCH; | 
 | 799 |             } | 
 | 800 |             //  Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use | 
 | 801 |             // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. | 
 | 802 |             nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); | 
 | 803 |             if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { | 
 | 804 |                 dropReason = DropReason::STALE; | 
 | 805 |             } | 
 | 806 |             dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); | 
 | 807 |             done = true; | 
 | 808 |             break; | 
 | 809 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 810 |     } | 
 | 811 |  | 
 | 812 |     if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 813 |         if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 814 |             dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 |         } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 816 |         mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 817 |  | 
 | 818 |         releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 819 |         *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 |     } | 
 | 821 | } | 
 | 822 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 823 | /** | 
 | 824 |  * Return true if the events preceding this incoming motion event should be dropped | 
 | 825 |  * Return false otherwise (the default behaviour) | 
 | 826 |  */ | 
 | 827 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 828 |     const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 829 |             (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 830 |  | 
 | 831 |     // Optimize case where the current application is unresponsive and the user | 
 | 832 |     // decides to touch a window in a different application. | 
 | 833 |     // If the application takes too long to catch up then we drop all events preceding | 
 | 834 |     // the touch into the other window. | 
 | 835 |     if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 836 |         int32_t displayId = motionEntry.displayId; | 
 | 837 |         int32_t x = static_cast<int32_t>( | 
 | 838 |                 motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 839 |         int32_t y = static_cast<int32_t>( | 
 | 840 |                 motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
 | 841 |         sp<InputWindowHandle> touchedWindowHandle = | 
 | 842 |                 findTouchedWindowAtLocked(displayId, x, y, nullptr); | 
 | 843 |         if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 844 |             touchedWindowHandle->getApplicationToken() != | 
 | 845 |                     mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 846 |             // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 847 |             ALOGI("Pruning input queue because user touched a different application while waiting " | 
 | 848 |                   "for %s", | 
 | 849 |                   mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 850 |             return true; | 
 | 851 |         } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 852 |  | 
 | 853 |         // Alternatively, maybe there's a gesture monitor that could handle this event | 
 | 854 |         std::vector<TouchedMonitor> gestureMonitors = | 
 | 855 |                 findTouchedGestureMonitorsLocked(displayId, {}); | 
 | 856 |         for (TouchedMonitor& gestureMonitor : gestureMonitors) { | 
 | 857 |             sp<Connection> connection = | 
 | 858 |                     getConnectionLocked(gestureMonitor.monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 859 |             if (connection != nullptr && connection->responsive) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 860 |                 // This monitor could take more input. Drop all events preceding this | 
 | 861 |                 // event, so that gesture monitor could get a chance to receive the stream | 
 | 862 |                 ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
 | 863 |                       "responsive gesture monitor that may handle the event", | 
 | 864 |                       mAwaitedFocusedApplication->getName().c_str()); | 
 | 865 |                 return true; | 
 | 866 |             } | 
 | 867 |         } | 
 | 868 |     } | 
 | 869 |  | 
 | 870 |     // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
 | 871 |     // yet been processed by some connections, the dispatcher will wait for these motion | 
 | 872 |     // events to be processed before dispatching the key event. This is because these motion events | 
 | 873 |     // may cause a new window to be launched, which the user might expect to receive focus. | 
 | 874 |     // To prevent waiting forever for such events, just send the key to the currently focused window | 
 | 875 |     if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
 | 876 |         ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
 | 877 |               "just send the pending key event to the focused window."); | 
 | 878 |         mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 879 |     } | 
 | 880 |     return false; | 
 | 881 | } | 
 | 882 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 883 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 884 |     bool needWake = mInboundQueue.empty(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 885 |     mInboundQueue.push_back(std::move(newEntry)); | 
 | 886 |     EventEntry& entry = *(mInboundQueue.back()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 887 |     traceInboundQueueLengthLocked(); | 
 | 888 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 889 |     switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 890 |         case EventEntry::Type::KEY: { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 891 |             // Optimize app switch latency. | 
 | 892 |             // If the application takes too long to catch up then we drop all events preceding | 
 | 893 |             // the app switch key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 894 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 895 |             if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 896 |                 if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 897 |                     mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 898 |                 } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 899 |                     if (mAppSwitchSawKeyDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | #if DEBUG_APP_SWITCH | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 901 |                         ALOGD("App switch is pending!"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 902 | #endif | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 903 |                         mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 904 |                         mAppSwitchSawKeyDown = false; | 
 | 905 |                         needWake = true; | 
 | 906 |                     } | 
 | 907 |                 } | 
 | 908 |             } | 
 | 909 |             break; | 
 | 910 |         } | 
 | 911 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 912 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 913 |             if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { | 
 | 914 |                 mNextUnblockedEvent = mInboundQueue.back(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 915 |                 needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 916 |             } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 917 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 918 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 919 |         case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 920 |             LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
 | 921 |             break; | 
 | 922 |         } | 
 | 923 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 924 |         case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 925 |         case EventEntry::Type::SENSOR: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 926 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 927 |             // nothing to do | 
 | 928 |             break; | 
 | 929 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 930 |     } | 
 | 931 |  | 
 | 932 |     return needWake; | 
 | 933 | } | 
 | 934 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 935 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 936 |     // Do not store sensor event in recent queue to avoid flooding the queue. | 
 | 937 |     if (entry->type != EventEntry::Type::SENSOR) { | 
 | 938 |         mRecentQueue.push_back(entry); | 
 | 939 |     } | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 940 |     if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 941 |         mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 942 |     } | 
 | 943 | } | 
 | 944 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 945 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 946 |                                                                  int32_t y, TouchState* touchState, | 
 | 947 |                                                                  bool addOutsideTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 948 |                                                                  bool addPortalWindows) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 949 |     if ((addPortalWindows || addOutsideTargets) && touchState == nullptr) { | 
 | 950 |         LOG_ALWAYS_FATAL( | 
 | 951 |                 "Must provide a valid touch state if adding portal windows or outside targets"); | 
 | 952 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 953 |     // Traverse windows from front to back to find touched window. | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 954 |     const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 955 |     for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 956 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
 | 957 |         if (windowInfo->displayId == displayId) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 958 |             auto flags = windowInfo->flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 959 |  | 
 | 960 |             if (windowInfo->visible) { | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 961 |                 if (!flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
 | 962 |                     bool isTouchModal = !flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE) && | 
 | 963 |                             !flags.test(InputWindowInfo::Flag::NOT_TOUCH_MODAL); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 964 |                     if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 965 |                         int32_t portalToDisplayId = windowInfo->portalToDisplayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 966 |                         if (portalToDisplayId != ADISPLAY_ID_NONE && | 
 | 967 |                             portalToDisplayId != displayId) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 968 |                             if (addPortalWindows) { | 
 | 969 |                                 // For the monitoring channels of the display. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 970 |                                 touchState->addPortalWindow(windowHandle); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 971 |                             } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 972 |                             return findTouchedWindowAtLocked(portalToDisplayId, x, y, touchState, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 973 |                                                              addOutsideTargets, addPortalWindows); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 974 |                         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 975 |                         // Found window. | 
 | 976 |                         return windowHandle; | 
 | 977 |                     } | 
 | 978 |                 } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 979 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 980 |                 if (addOutsideTargets && flags.test(InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH)) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 981 |                     touchState->addOrUpdateWindow(windowHandle, | 
 | 982 |                                                   InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
 | 983 |                                                   BitSet32(0)); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 984 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 985 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 986 |         } | 
 | 987 |     } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 988 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 989 | } | 
 | 990 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 991 | std::vector<TouchedMonitor> InputDispatcher::findTouchedGestureMonitorsLocked( | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 992 |         int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 993 |     std::vector<TouchedMonitor> touchedMonitors; | 
 | 994 |  | 
 | 995 |     std::vector<Monitor> monitors = getValueByKey(mGestureMonitorsByDisplay, displayId); | 
 | 996 |     addGestureMonitors(monitors, touchedMonitors); | 
 | 997 |     for (const sp<InputWindowHandle>& portalWindow : portalWindows) { | 
 | 998 |         const InputWindowInfo* windowInfo = portalWindow->getInfo(); | 
 | 999 |         monitors = getValueByKey(mGestureMonitorsByDisplay, windowInfo->portalToDisplayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1000 |         addGestureMonitors(monitors, touchedMonitors, -windowInfo->frameLeft, | 
 | 1001 |                            -windowInfo->frameTop); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1002 |     } | 
 | 1003 |     return touchedMonitors; | 
 | 1004 | } | 
 | 1005 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1006 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 |     const char* reason; | 
 | 1008 |     switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1009 |         case DropReason::POLICY: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1010 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1011 |             ALOGD("Dropped event because policy consumed it."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1012 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1013 |             reason = "inbound event was dropped because the policy consumed it"; | 
 | 1014 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1015 |         case DropReason::DISABLED: | 
 | 1016 |             if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1017 |                 ALOGI("Dropped event because input dispatch is disabled."); | 
 | 1018 |             } | 
 | 1019 |             reason = "inbound event was dropped because input dispatch is disabled"; | 
 | 1020 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1021 |         case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1022 |             ALOGI("Dropped event because of pending overdue app switch."); | 
 | 1023 |             reason = "inbound event was dropped because of pending overdue app switch"; | 
 | 1024 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1025 |         case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1026 |             ALOGI("Dropped event because the current application is not responding and the user " | 
 | 1027 |                   "has started interacting with a different application."); | 
 | 1028 |             reason = "inbound event was dropped because the current application is not responding " | 
 | 1029 |                      "and the user has started interacting with a different application"; | 
 | 1030 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1031 |         case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1032 |             ALOGI("Dropped event because it is stale."); | 
 | 1033 |             reason = "inbound event was dropped because it is stale"; | 
 | 1034 |             break; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1035 |         case DropReason::NO_POINTER_CAPTURE: | 
 | 1036 |             ALOGI("Dropped event because there is no window with Pointer Capture."); | 
 | 1037 |             reason = "inbound event was dropped because there is no window with Pointer Capture"; | 
 | 1038 |             break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1039 |         case DropReason::NOT_DROPPED: { | 
 | 1040 |             LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1041 |             return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1042 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1043 |     } | 
 | 1044 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1045 |     switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1046 |         case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1047 |             CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
 | 1048 |             synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1049 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1051 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1052 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
 | 1053 |             if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1054 |                 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
 | 1055 |                 synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1056 |             } else { | 
 | 1057 |                 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
 | 1058 |                 synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1059 |             } | 
 | 1060 |             break; | 
 | 1061 |         } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1062 |         case EventEntry::Type::SENSOR: { | 
 | 1063 |             break; | 
 | 1064 |         } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1065 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
 | 1066 |             break; | 
 | 1067 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1068 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1069 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 1070 |         case EventEntry::Type::DEVICE_RESET: { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1071 |             LOG_ALWAYS_FATAL("Should not drop %s events", NamedEnum::string(entry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1072 |             break; | 
 | 1073 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1074 |     } | 
 | 1075 | } | 
 | 1076 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1077 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1078 |     return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
 | 1079 |             keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | } | 
 | 1081 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1082 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
 | 1083 |     return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
 | 1084 |             (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
 | 1085 |             (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1086 | } | 
 | 1087 |  | 
 | 1088 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
 | 1089 |     return mAppSwitchDueTime != LONG_LONG_MAX; | 
 | 1090 | } | 
 | 1091 |  | 
 | 1092 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
 | 1093 |     mAppSwitchDueTime = LONG_LONG_MAX; | 
 | 1094 |  | 
 | 1095 | #if DEBUG_APP_SWITCH | 
 | 1096 |     if (handled) { | 
 | 1097 |         ALOGD("App switch has arrived."); | 
 | 1098 |     } else { | 
 | 1099 |         ALOGD("App switch was abandoned."); | 
 | 1100 |     } | 
 | 1101 | #endif | 
 | 1102 | } | 
 | 1103 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1104 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1105 |     return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1106 | } | 
 | 1107 |  | 
 | 1108 | bool InputDispatcher::runCommandsLockedInterruptible() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1109 |     if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1110 |         return false; | 
 | 1111 |     } | 
 | 1112 |  | 
 | 1113 |     do { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1114 |         std::unique_ptr<CommandEntry> commandEntry = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1115 |         mCommandQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1116 |         Command command = commandEntry->command; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1117 |         command(*this, commandEntry.get()); // commands are implicitly 'LockedInterruptible' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1118 |  | 
 | 1119 |         commandEntry->connection.clear(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1120 |     } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1121 |     return true; | 
 | 1122 | } | 
 | 1123 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1124 | void InputDispatcher::postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) { | 
 | 1125 |     mCommandQueue.push_back(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | } | 
 | 1127 |  | 
 | 1128 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1129 |     while (!mInboundQueue.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1130 |         std::shared_ptr<EventEntry> entry = mInboundQueue.front(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1131 |         mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1132 |         releaseInboundEventLocked(entry); | 
 | 1133 |     } | 
 | 1134 |     traceInboundQueueLengthLocked(); | 
 | 1135 | } | 
 | 1136 |  | 
 | 1137 | void InputDispatcher::releasePendingEventLocked() { | 
 | 1138 |     if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 |         releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1140 |         mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1141 |     } | 
 | 1142 | } | 
 | 1143 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1144 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1145 |     InjectionState* injectionState = entry->injectionState; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1146 |     if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | #if DEBUG_DISPATCH_CYCLE | 
 | 1148 |         ALOGD("Injected inbound event was dropped."); | 
 | 1149 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1150 |         setInjectionResult(*entry, InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1151 |     } | 
 | 1152 |     if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1153 |         mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 |     } | 
 | 1155 |     addRecentEventLocked(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1156 | } | 
 | 1157 |  | 
 | 1158 | void InputDispatcher::resetKeyRepeatLocked() { | 
 | 1159 |     if (mKeyRepeatState.lastKeyEntry) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1160 |         mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1161 |     } | 
 | 1162 | } | 
 | 1163 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1164 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
 | 1165 |     std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1166 |  | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1167 |     uint32_t policyFlags = entry->policyFlags & | 
 | 1168 |             (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1169 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1170 |     std::shared_ptr<KeyEntry> newEntry = | 
 | 1171 |             std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, | 
 | 1172 |                                        entry->source, entry->displayId, policyFlags, entry->action, | 
 | 1173 |                                        entry->flags, entry->keyCode, entry->scanCode, | 
 | 1174 |                                        entry->metaState, entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1175 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1176 |     newEntry->syntheticRepeat = true; | 
 | 1177 |     mKeyRepeatState.lastKeyEntry = newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1178 |     mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1179 |     return newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | } | 
 | 1181 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1182 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1183 |                                                          const ConfigurationChangedEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1184 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1185 |     ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | #endif | 
 | 1187 |  | 
 | 1188 |     // Reset key repeating in case a keyboard device was added or removed or something. | 
 | 1189 |     resetKeyRepeatLocked(); | 
 | 1190 |  | 
 | 1191 |     // 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] | 1192 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 1193 |             &InputDispatcher::doNotifyConfigurationChangedLockedInterruptible); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1194 |     commandEntry->eventTime = entry.eventTime; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1195 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 |     return true; | 
 | 1197 | } | 
 | 1198 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1199 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, | 
 | 1200 |                                                 const DeviceResetEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1201 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1202 |     ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, | 
 | 1203 |           entry.deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | #endif | 
 | 1205 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1206 |     CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1207 |     options.deviceId = entry.deviceId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 |     synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1209 |     return true; | 
 | 1210 | } | 
 | 1211 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1212 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1213 |                                               std::string_view reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1214 |     if (mPendingEvent != nullptr) { | 
 | 1215 |         // Move the pending event to the front of the queue. This will give the chance | 
 | 1216 |         // for the pending event to get dispatched to the newly focused window | 
 | 1217 |         mInboundQueue.push_front(mPendingEvent); | 
 | 1218 |         mPendingEvent = nullptr; | 
 | 1219 |     } | 
 | 1220 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1221 |     std::unique_ptr<FocusEntry> focusEntry = | 
 | 1222 |             std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, | 
 | 1223 |                                          reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1224 |  | 
 | 1225 |     // This event should go to the front of the queue, but behind all other focus events | 
 | 1226 |     // Find the last focus event, and insert right after it | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1227 |     std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1228 |             std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1229 |                          [](const std::shared_ptr<EventEntry>& event) { | 
 | 1230 |                              return event->type == EventEntry::Type::FOCUS; | 
 | 1231 |                          }); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1232 |  | 
 | 1233 |     // Maintain the order of focus events. Insert the entry after all other focus events. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1234 |     mInboundQueue.insert(it.base(), std::move(focusEntry)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1235 | } | 
 | 1236 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1237 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1238 |     std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1239 |     if (channel == nullptr) { | 
 | 1240 |         return; // Window has gone away | 
 | 1241 |     } | 
 | 1242 |     InputTarget target; | 
 | 1243 |     target.inputChannel = channel; | 
 | 1244 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 1245 |     entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1246 |     std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
 | 1247 |             channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1248 |     std::string reason = std::string("reason=").append(entry->reason); | 
 | 1249 |     android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1250 |     dispatchEventLocked(currentTime, entry, {target}); | 
 | 1251 | } | 
 | 1252 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1253 | void InputDispatcher::dispatchPointerCaptureChangedLocked( | 
 | 1254 |         nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, | 
 | 1255 |         DropReason& dropReason) { | 
 | 1256 |     const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; | 
 | 1257 |     if (entry->pointerCaptureEnabled == haveWindowWithPointerCapture) { | 
 | 1258 |         LOG_ALWAYS_FATAL_IF(mFocusedWindowRequestedPointerCapture, | 
 | 1259 |                             "The Pointer Capture state has already been dispatched to the window."); | 
 | 1260 |         // Pointer capture was already forcefully disabled because of focus change. | 
 | 1261 |         dropReason = DropReason::NOT_DROPPED; | 
 | 1262 |         return; | 
 | 1263 |     } | 
 | 1264 |  | 
 | 1265 |     // Set drop reason for early returns | 
 | 1266 |     dropReason = DropReason::NO_POINTER_CAPTURE; | 
 | 1267 |  | 
 | 1268 |     sp<IBinder> token; | 
 | 1269 |     if (entry->pointerCaptureEnabled) { | 
 | 1270 |         // Enable Pointer Capture | 
 | 1271 |         if (!mFocusedWindowRequestedPointerCapture) { | 
 | 1272 |             // This can happen if a window requests capture and immediately releases capture. | 
 | 1273 |             ALOGW("No window requested Pointer Capture."); | 
 | 1274 |             return; | 
 | 1275 |         } | 
 | 1276 |         token = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
 | 1277 |         LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); | 
 | 1278 |         mWindowTokenWithPointerCapture = token; | 
 | 1279 |     } else { | 
 | 1280 |         // Disable Pointer Capture | 
 | 1281 |         token = mWindowTokenWithPointerCapture; | 
 | 1282 |         mWindowTokenWithPointerCapture = nullptr; | 
 | 1283 |         mFocusedWindowRequestedPointerCapture = false; | 
 | 1284 |     } | 
 | 1285 |  | 
 | 1286 |     auto channel = getInputChannelLocked(token); | 
 | 1287 |     if (channel == nullptr) { | 
 | 1288 |         // Window has gone away, clean up Pointer Capture state. | 
 | 1289 |         mWindowTokenWithPointerCapture = nullptr; | 
 | 1290 |         mFocusedWindowRequestedPointerCapture = false; | 
 | 1291 |         return; | 
 | 1292 |     } | 
 | 1293 |     InputTarget target; | 
 | 1294 |     target.inputChannel = channel; | 
 | 1295 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 1296 |     entry->dispatchInProgress = true; | 
 | 1297 |     dispatchEventLocked(currentTime, entry, {target}); | 
 | 1298 |  | 
 | 1299 |     dropReason = DropReason::NOT_DROPPED; | 
 | 1300 | } | 
 | 1301 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1302 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1303 |                                         DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1304 |     // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1305 |     if (!entry->dispatchInProgress) { | 
 | 1306 |         if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
 | 1307 |             (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
 | 1308 |             (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
 | 1309 |             if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1310 |                 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1311 |                 // We have seen two identical key downs in a row which indicates that the device | 
 | 1312 |                 // driver is automatically generating key repeats itself.  We take note of the | 
 | 1313 |                 // repeat here, but we disable our own next key repeat timer since it is clear that | 
 | 1314 |                 // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1315 |                 mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
 | 1316 |                 // Make sure we don't get key down from a different device. If a different | 
 | 1317 |                 // device Id has same key pressed down, the new device Id will replace the | 
 | 1318 |                 // current one to hold the key repeat with repeat count reset. | 
 | 1319 |                 // In the future when got a KEY_UP on the device id, drop it and do not | 
 | 1320 |                 // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1321 |                 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
 | 1322 |                 resetKeyRepeatLocked(); | 
 | 1323 |                 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
 | 1324 |             } else { | 
 | 1325 |                 // Not a repeat.  Save key down state in case we do see a repeat later. | 
 | 1326 |                 resetKeyRepeatLocked(); | 
 | 1327 |                 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
 | 1328 |             } | 
 | 1329 |             mKeyRepeatState.lastKeyEntry = entry; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1330 |         } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
 | 1331 |                    mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1332 |             // The key on device 'deviceId' is still down, do not stop key repeat | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1333 | #if DEBUG_INBOUND_EVENT_DETAILS | 
 | 1334 |             ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
 | 1335 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1336 |         } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1337 |             resetKeyRepeatLocked(); | 
 | 1338 |         } | 
 | 1339 |  | 
 | 1340 |         if (entry->repeatCount == 1) { | 
 | 1341 |             entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
 | 1342 |         } else { | 
 | 1343 |             entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
 | 1344 |         } | 
 | 1345 |  | 
 | 1346 |         entry->dispatchInProgress = true; | 
 | 1347 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1348 |         logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 |     } | 
 | 1350 |  | 
 | 1351 |     // Handle case where the policy asked us to try again later last time. | 
 | 1352 |     if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
 | 1353 |         if (currentTime < entry->interceptKeyWakeupTime) { | 
 | 1354 |             if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
 | 1355 |                 *nextWakeupTime = entry->interceptKeyWakeupTime; | 
 | 1356 |             } | 
 | 1357 |             return false; // wait until next wakeup | 
 | 1358 |         } | 
 | 1359 |         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
 | 1360 |         entry->interceptKeyWakeupTime = 0; | 
 | 1361 |     } | 
 | 1362 |  | 
 | 1363 |     // Give the policy a chance to intercept the key. | 
 | 1364 |     if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
 | 1365 |         if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1366 |             std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
| Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 1367 |                     &InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1368 |             sp<IBinder> focusedWindowToken = | 
 | 1369 |                     getValueByKey(mFocusedWindowTokenByDisplay, getTargetDisplayId(*entry)); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 1370 |             commandEntry->connectionToken = focusedWindowToken; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1371 |             commandEntry->keyEntry = entry; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 1372 |             postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1373 |             return false; // wait for the command to run | 
 | 1374 |         } else { | 
 | 1375 |             entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
 | 1376 |         } | 
 | 1377 |     } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1378 |         if (*dropReason == DropReason::NOT_DROPPED) { | 
 | 1379 |             *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1380 |         } | 
 | 1381 |     } | 
 | 1382 |  | 
 | 1383 |     // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1384 |     if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1385 |         setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1386 |                            *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
 | 1387 |                                                              : InputEventInjectionResult::FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1388 |         mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1389 |         return true; | 
 | 1390 |     } | 
 | 1391 |  | 
 | 1392 |     // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1393 |     std::vector<InputTarget> inputTargets; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1394 |     InputEventInjectionResult injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1395 |             findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1396 |     if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1397 |         return false; | 
 | 1398 |     } | 
 | 1399 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1400 |     setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1401 |     if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1402 |         return true; | 
 | 1403 |     } | 
 | 1404 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1405 |     // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1406 |     addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1407 |  | 
 | 1408 |     // Dispatch the key. | 
 | 1409 |     dispatchEventLocked(currentTime, entry, inputTargets); | 
 | 1410 |     return true; | 
 | 1411 | } | 
 | 1412 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1413 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1414 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 1415 |     ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1416 |           "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
 | 1417 |           "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1418 |           prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
 | 1419 |           entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
 | 1420 |           entry.repeatCount, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | #endif | 
 | 1422 | } | 
 | 1423 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1424 | void InputDispatcher::doNotifySensorLockedInterruptible(CommandEntry* commandEntry) { | 
 | 1425 |     mLock.unlock(); | 
 | 1426 |  | 
 | 1427 |     const std::shared_ptr<SensorEntry>& entry = commandEntry->sensorEntry; | 
 | 1428 |     if (entry->accuracyChanged) { | 
 | 1429 |         mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); | 
 | 1430 |     } | 
 | 1431 |     mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, | 
 | 1432 |                                entry->hwTimestamp, entry->values); | 
 | 1433 |     mLock.lock(); | 
 | 1434 | } | 
 | 1435 |  | 
 | 1436 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, | 
 | 1437 |                                            DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
 | 1438 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 1439 |     ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " | 
 | 1440 |           "source=0x%x, sensorType=%s", | 
 | 1441 |           entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, | 
 | 1442 |           NamedEnum::string(sensorType).c_str()); | 
 | 1443 | #endif | 
 | 1444 |     std::unique_ptr<CommandEntry> commandEntry = | 
 | 1445 |             std::make_unique<CommandEntry>(&InputDispatcher::doNotifySensorLockedInterruptible); | 
 | 1446 |     commandEntry->sensorEntry = entry; | 
 | 1447 |     postCommandLocked(std::move(commandEntry)); | 
 | 1448 | } | 
 | 1449 |  | 
 | 1450 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { | 
 | 1451 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 1452 |     ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, | 
 | 1453 |           NamedEnum::string(sensorType).c_str()); | 
 | 1454 | #endif | 
 | 1455 |     { // acquire lock | 
 | 1456 |         std::scoped_lock _l(mLock); | 
 | 1457 |  | 
 | 1458 |         for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { | 
 | 1459 |             std::shared_ptr<EventEntry> entry = *it; | 
 | 1460 |             if (entry->type == EventEntry::Type::SENSOR) { | 
 | 1461 |                 it = mInboundQueue.erase(it); | 
 | 1462 |                 releaseInboundEventLocked(entry); | 
 | 1463 |             } | 
 | 1464 |         } | 
 | 1465 |     } | 
 | 1466 |     return true; | 
 | 1467 | } | 
 | 1468 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1469 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1470 |                                            DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1471 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1472 |     // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1473 |     if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 |         entry->dispatchInProgress = true; | 
 | 1475 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1476 |         logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1477 |     } | 
 | 1478 |  | 
 | 1479 |     // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1480 |     if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1481 |         setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1482 |                            *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
 | 1483 |                                                              : InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1484 |         return true; | 
 | 1485 |     } | 
 | 1486 |  | 
 | 1487 |     bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; | 
 | 1488 |  | 
 | 1489 |     // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1490 |     std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 |  | 
 | 1492 |     bool conflictingPointerActions = false; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1493 |     InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 |     if (isPointerEvent) { | 
 | 1495 |         // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1496 |         injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1497 |                 findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1498 |                                                &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1499 |     } else { | 
 | 1500 |         // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1501 |         injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1502 |                 findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1503 |     } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1504 |     if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1505 |         return false; | 
 | 1506 |     } | 
 | 1507 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1508 |     setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1509 |     if (injectionResult == InputEventInjectionResult::PERMISSION_DENIED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1510 |         ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); | 
 | 1511 |         return true; | 
 | 1512 |     } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1513 |     if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1514 |         CancelationOptions::Mode mode(isPointerEvent | 
 | 1515 |                                               ? CancelationOptions::CANCEL_POINTER_EVENTS | 
 | 1516 |                                               : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
 | 1517 |         CancelationOptions options(mode, "input event injection failed"); | 
 | 1518 |         synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 |         return true; | 
 | 1520 |     } | 
 | 1521 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1522 |     // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1523 |     addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1524 |  | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1525 |     if (isPointerEvent) { | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1526 |         std::unordered_map<int32_t, TouchState>::iterator it = | 
 | 1527 |                 mTouchStatesByDisplay.find(entry->displayId); | 
 | 1528 |         if (it != mTouchStatesByDisplay.end()) { | 
 | 1529 |             const TouchState& state = it->second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1530 |             if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1531 |                 // The event has gone through these portal windows, so we add monitoring targets of | 
 | 1532 |                 // the corresponding displays as well. | 
 | 1533 |                 for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1534 |                     const InputWindowInfo* windowInfo = state.portalWindows[i]->getInfo(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1535 |                     addGlobalMonitoringTargetsLocked(inputTargets, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1536 |                                                      -windowInfo->frameLeft, -windowInfo->frameTop); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1537 |                 } | 
 | 1538 |             } | 
 | 1539 |         } | 
 | 1540 |     } | 
 | 1541 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1542 |     // Dispatch the motion. | 
 | 1543 |     if (conflictingPointerActions) { | 
 | 1544 |         CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1545 |                                    "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 |         synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 1547 |     } | 
 | 1548 |     dispatchEventLocked(currentTime, entry, inputTargets); | 
 | 1549 |     return true; | 
 | 1550 | } | 
 | 1551 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1552 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1553 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 1554 |     ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1555 |           ", policyFlags=0x%x, " | 
 | 1556 |           "action=0x%x, actionButton=0x%x, flags=0x%x, " | 
 | 1557 |           "metaState=0x%x, buttonState=0x%x," | 
 | 1558 |           "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1559 |           prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, entry.policyFlags, | 
 | 1560 |           entry.action, entry.actionButton, entry.flags, entry.metaState, entry.buttonState, | 
 | 1561 |           entry.edgeFlags, entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1562 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1563 |     for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1564 |         ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1565 |               "x=%f, y=%f, pressure=%f, size=%f, " | 
 | 1566 |               "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
 | 1567 |               "orientation=%f", | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1568 |               i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
 | 1569 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
 | 1570 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
 | 1571 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
 | 1572 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
 | 1573 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
 | 1574 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
 | 1575 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
 | 1576 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
 | 1577 |               entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 |     } | 
 | 1579 | #endif | 
 | 1580 | } | 
 | 1581 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1582 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, | 
 | 1583 |                                           std::shared_ptr<EventEntry> eventEntry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1584 |                                           const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1585 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1586 | #if DEBUG_DISPATCH_CYCLE | 
 | 1587 |     ALOGD("dispatchEventToCurrentInputTargets"); | 
 | 1588 | #endif | 
 | 1589 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1590 |     updateInteractionTokensLocked(*eventEntry, inputTargets); | 
 | 1591 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 |     ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
 | 1593 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1594 |     pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1596 |     for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1597 |         sp<Connection> connection = | 
 | 1598 |                 getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1599 |         if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1600 |             prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1601 |         } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1602 |             if (DEBUG_FOCUS) { | 
 | 1603 |                 ALOGD("Dropping event delivery to target with channel '%s' because it " | 
 | 1604 |                       "is no longer registered with the input dispatcher.", | 
 | 1605 |                       inputTarget.inputChannel->getName().c_str()); | 
 | 1606 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1607 |         } | 
 | 1608 |     } | 
 | 1609 | } | 
 | 1610 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1611 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
 | 1612 |     // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
 | 1613 |     // If the policy decides to close the app, we will get a channel removal event via | 
 | 1614 |     // unregisterInputChannel, and will clean up the connection that way. We are already not | 
 | 1615 |     // sending new pointers to the connection when it blocked, but focused events will continue to | 
 | 1616 |     // pile up. | 
 | 1617 |     ALOGW("Canceling events for %s because it is unresponsive", | 
 | 1618 |           connection->inputChannel->getName().c_str()); | 
 | 1619 |     if (connection->status == Connection::STATUS_NORMAL) { | 
 | 1620 |         CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
 | 1621 |                                    "application not responding"); | 
 | 1622 |         synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1623 |     } | 
 | 1624 | } | 
 | 1625 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1626 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1627 |     if (DEBUG_FOCUS) { | 
 | 1628 |         ALOGD("Resetting ANR timeouts."); | 
 | 1629 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1630 |  | 
 | 1631 |     // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1632 |     mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1633 |     mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1634 | } | 
 | 1635 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1636 | /** | 
 | 1637 |  * Get the display id that the given event should go to. If this event specifies a valid display id, | 
 | 1638 |  * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
 | 1639 |  * Focused display is the display that the user most recently interacted with. | 
 | 1640 |  */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1641 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1642 |     int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1643 |     switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1644 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1645 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
 | 1646 |             displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1647 |             break; | 
 | 1648 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1649 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1650 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
 | 1651 |             displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1652 |             break; | 
 | 1653 |         } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1654 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1655 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1656 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1657 |         case EventEntry::Type::DEVICE_RESET: | 
 | 1658 |         case EventEntry::Type::SENSOR: { | 
 | 1659 |             ALOGE("%s events do not have a target display", NamedEnum::string(entry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1660 |             return ADISPLAY_ID_NONE; | 
 | 1661 |         } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1662 |     } | 
 | 1663 |     return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
 | 1664 | } | 
 | 1665 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1666 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
 | 1667 |                                                 const char* focusedWindowName) { | 
 | 1668 |     if (mAnrTracker.empty()) { | 
 | 1669 |         // already processed all events that we waited for | 
 | 1670 |         mKeyIsWaitingForEventsTimeout = std::nullopt; | 
 | 1671 |         return false; | 
 | 1672 |     } | 
 | 1673 |  | 
 | 1674 |     if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
 | 1675 |         // Start the timer | 
 | 1676 |         ALOGD("Waiting to send key to %s because there are unprocessed events that may cause " | 
 | 1677 |               "focus to change", | 
 | 1678 |               focusedWindowName); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1679 |         mKeyIsWaitingForEventsTimeout = currentTime + | 
 | 1680 |                 std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
 | 1681 |                         .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1682 |         return true; | 
 | 1683 |     } | 
 | 1684 |  | 
 | 1685 |     // We still have pending events, and already started the timer | 
 | 1686 |     if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
 | 1687 |         return true; // Still waiting | 
 | 1688 |     } | 
 | 1689 |  | 
 | 1690 |     // Waited too long, and some connection still hasn't processed all motions | 
 | 1691 |     // Just send the key to the focused window | 
 | 1692 |     ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
 | 1693 |           focusedWindowName); | 
 | 1694 |     mKeyIsWaitingForEventsTimeout = std::nullopt; | 
 | 1695 |     return false; | 
 | 1696 | } | 
 | 1697 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1698 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( | 
 | 1699 |         nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, | 
 | 1700 |         nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1701 |     std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1702 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1703 |     int32_t displayId = getTargetDisplayId(entry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1704 |     sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1705 |     std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1706 |             getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
 | 1707 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1708 |     // If there is no currently focused window and no focused application | 
 | 1709 |     // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1710 |     if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
 | 1711 |         ALOGI("Dropping %s event because there is no focused window or focused application in " | 
 | 1712 |               "display %" PRId32 ".", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1713 |               NamedEnum::string(entry.type).c_str(), displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1714 |         return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1715 |     } | 
 | 1716 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1717 |     // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
 | 1718 |     // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
 | 1719 |     // start interacting with another application via touch (app switch). This code can be removed | 
 | 1720 |     // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
 | 1721 |     // an app is expected to have a focused window. | 
 | 1722 |     if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
 | 1723 |         if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
 | 1724 |             // We just discovered that there's no focused window. Start the ANR timer | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1725 |             std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
 | 1726 |                     DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
 | 1727 |             mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1728 |             mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1729 |             mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1730 |             ALOGW("Waiting because no window has focus but %s may eventually add a " | 
 | 1731 |                   "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1732 |                   mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1733 |             *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1734 |             return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1735 |         } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
 | 1736 |             // Already raised ANR. Drop the event | 
 | 1737 |             ALOGE("Dropping %s event because there is no focused window", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1738 |                   NamedEnum::string(entry.type).c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1739 |             return InputEventInjectionResult::FAILED; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1740 |         } else { | 
 | 1741 |             // Still waiting for the focused window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1742 |             return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1743 |         } | 
 | 1744 |     } | 
 | 1745 |  | 
 | 1746 |     // we have a valid, non-null focused window | 
 | 1747 |     resetNoFocusedWindowTimeoutLocked(); | 
 | 1748 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1749 |     // Check permissions. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1750 |     if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1751 |         return InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1752 |     } | 
 | 1753 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1754 |     if (focusedWindowHandle->getInfo()->paused) { | 
 | 1755 |         ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1756 |         return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1757 |     } | 
 | 1758 |  | 
 | 1759 |     // If the event is a key event, then we must wait for all previous events to | 
 | 1760 |     // complete before delivering it because previous events may have the | 
 | 1761 |     // side-effect of transferring focus to a different window and we want to | 
 | 1762 |     // ensure that the following keys are sent to the new window. | 
 | 1763 |     // | 
 | 1764 |     // Suppose the user touches a button in a window then immediately presses "A". | 
 | 1765 |     // If the button causes a pop-up window to appear then we want to ensure that | 
 | 1766 |     // the "A" key is delivered to the new pop-up window.  This is because users | 
 | 1767 |     // often anticipate pending UI changes when typing on a keyboard. | 
 | 1768 |     // To obtain this behavior, we must serialize key events with respect to all | 
 | 1769 |     // prior input events. | 
 | 1770 |     if (entry.type == EventEntry::Type::KEY) { | 
 | 1771 |         if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
 | 1772 |             *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1773 |             return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1774 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1775 |     } | 
 | 1776 |  | 
 | 1777 |     // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1778 |     addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1779 |                           InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
 | 1780 |                           BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1781 |  | 
 | 1782 |     // Done. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1783 |     return InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1784 | } | 
 | 1785 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1786 | /** | 
 | 1787 |  * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
 | 1788 |  * that are currently unresponsive. | 
 | 1789 |  */ | 
 | 1790 | std::vector<TouchedMonitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
 | 1791 |         const std::vector<TouchedMonitor>& monitors) const { | 
 | 1792 |     std::vector<TouchedMonitor> responsiveMonitors; | 
 | 1793 |     std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
 | 1794 |                  [this](const TouchedMonitor& monitor) REQUIRES(mLock) { | 
 | 1795 |                      sp<Connection> connection = getConnectionLocked( | 
 | 1796 |                              monitor.monitor.inputChannel->getConnectionToken()); | 
 | 1797 |                      if (connection == nullptr) { | 
 | 1798 |                          ALOGE("Could not find connection for monitor %s", | 
 | 1799 |                                monitor.monitor.inputChannel->getName().c_str()); | 
 | 1800 |                          return false; | 
 | 1801 |                      } | 
 | 1802 |                      if (!connection->responsive) { | 
 | 1803 |                          ALOGW("Unresponsive monitor %s will not get the new gesture", | 
 | 1804 |                                connection->inputChannel->getName().c_str()); | 
 | 1805 |                          return false; | 
 | 1806 |                      } | 
 | 1807 |                      return true; | 
 | 1808 |                  }); | 
 | 1809 |     return responsiveMonitors; | 
 | 1810 | } | 
 | 1811 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1812 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( | 
 | 1813 |         nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, | 
 | 1814 |         nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1815 |     ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1816 |     enum InjectionPermission { | 
 | 1817 |         INJECTION_PERMISSION_UNKNOWN, | 
 | 1818 |         INJECTION_PERMISSION_GRANTED, | 
 | 1819 |         INJECTION_PERMISSION_DENIED | 
 | 1820 |     }; | 
 | 1821 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1822 |     // For security reasons, we defer updating the touch state until we are sure that | 
 | 1823 |     // event injection will be allowed. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1824 |     int32_t displayId = entry.displayId; | 
 | 1825 |     int32_t action = entry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1826 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
 | 1827 |  | 
 | 1828 |     // Update the touch state as needed based on the properties of the touch event. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1829 |     InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1830 |     InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1831 |     sp<InputWindowHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
 | 1832 |     sp<InputWindowHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1833 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1834 |     // Copy current touch state into tempTouchState. | 
 | 1835 |     // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
 | 1836 |     // 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] | 1837 |     const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1838 |     TouchState tempTouchState; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 1839 |     std::unordered_map<int32_t, TouchState>::iterator oldStateIt = | 
 | 1840 |             mTouchStatesByDisplay.find(displayId); | 
 | 1841 |     if (oldStateIt != mTouchStatesByDisplay.end()) { | 
 | 1842 |         oldState = &(oldStateIt->second); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1843 |         tempTouchState.copyFrom(*oldState); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 1844 |     } | 
 | 1845 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1846 |     bool isSplit = tempTouchState.split; | 
 | 1847 |     bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
 | 1848 |             (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
 | 1849 |              tempTouchState.displayId != displayId); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1850 |     bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
 | 1851 |                           maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
 | 1852 |                           maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
 | 1853 |     bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
 | 1854 |                        maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1855 |     const bool isFromMouse = entry.source == AINPUT_SOURCE_MOUSE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1856 |     bool wrongDevice = false; | 
 | 1857 |     if (newGesture) { | 
 | 1858 |         bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1859 |         if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1860 |             ALOGI("Dropping event because a pointer for a different device is already down " | 
 | 1861 |                   "in display %" PRId32, | 
 | 1862 |                   displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1863 |             // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1864 |             injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1865 |             switchedDevice = false; | 
 | 1866 |             wrongDevice = true; | 
 | 1867 |             goto Failed; | 
 | 1868 |         } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1869 |         tempTouchState.reset(); | 
 | 1870 |         tempTouchState.down = down; | 
 | 1871 |         tempTouchState.deviceId = entry.deviceId; | 
 | 1872 |         tempTouchState.source = entry.source; | 
 | 1873 |         tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1874 |         isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1875 |     } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 1876 |         ALOGI("Dropping move event because a pointer for a different device is already active " | 
 | 1877 |               "in display %" PRId32, | 
 | 1878 |               displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1879 |         // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1880 |         injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 1881 |         switchedDevice = false; | 
 | 1882 |         wrongDevice = true; | 
 | 1883 |         goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1884 |     } | 
 | 1885 |  | 
 | 1886 |     if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
 | 1887 |         /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
 | 1888 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1889 |         int32_t x; | 
 | 1890 |         int32_t y; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1891 |         int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1892 |         // Always dispatch mouse events to cursor position. | 
 | 1893 |         if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1894 |             x = int32_t(entry.xCursorPosition); | 
 | 1895 |             y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1896 |         } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1897 |             x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 1898 |             y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1899 |         } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1900 |         bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1901 |         newTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1902 |                 findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
 | 1903 |                                           isDown /*addOutsideTargets*/, true /*addPortalWindows*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1904 |  | 
 | 1905 |         std::vector<TouchedMonitor> newGestureMonitors = isDown | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1906 |                 ? findTouchedGestureMonitorsLocked(displayId, tempTouchState.portalWindows) | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1907 |                 : std::vector<TouchedMonitor>{}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1908 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1909 |         // Figure out whether splitting will be allowed for this window. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1910 |         if (newTouchedWindowHandle != nullptr && | 
 | 1911 |             newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Garfield Tan | addb02b | 2019-06-25 16:36:13 -0700 | [diff] [blame] | 1912 |             // New window supports splitting, but we should never split mouse events. | 
 | 1913 |             isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 |         } else if (isSplit) { | 
 | 1915 |             // New window does not support splitting but we have already split events. | 
 | 1916 |             // Ignore the new window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1917 |             newTouchedWindowHandle = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1918 |         } | 
 | 1919 |  | 
 | 1920 |         // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1921 |         if (newTouchedWindowHandle == nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1922 |             // Try to assign the pointer to the first foreground window we find, if there is one. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1923 |             newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1924 |         } | 
 | 1925 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1926 |         if (newTouchedWindowHandle != nullptr && newTouchedWindowHandle->getInfo()->paused) { | 
 | 1927 |             ALOGI("Not sending touch event to %s because it is paused", | 
 | 1928 |                   newTouchedWindowHandle->getName().c_str()); | 
 | 1929 |             newTouchedWindowHandle = nullptr; | 
 | 1930 |         } | 
 | 1931 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1932 |         // Ensure the window has a connection and the connection is responsive | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1933 |         if (newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1934 |             const bool isResponsive = hasResponsiveConnectionLocked(*newTouchedWindowHandle); | 
 | 1935 |             if (!isResponsive) { | 
 | 1936 |                 ALOGW("%s will not receive the new gesture at %" PRIu64, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1937 |                       newTouchedWindowHandle->getName().c_str(), entry.eventTime); | 
 | 1938 |                 newTouchedWindowHandle = nullptr; | 
 | 1939 |             } | 
 | 1940 |         } | 
 | 1941 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1942 |         // Drop events that can't be trusted due to occlusion | 
 | 1943 |         if (newTouchedWindowHandle != nullptr && | 
 | 1944 |             mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { | 
 | 1945 |             TouchOcclusionInfo occlusionInfo = | 
 | 1946 |                     computeTouchOcclusionInfoLocked(newTouchedWindowHandle, x, y); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1947 |             if (!isTouchTrustedLocked(occlusionInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 1948 |                 if (DEBUG_TOUCH_OCCLUSION) { | 
 | 1949 |                     ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); | 
 | 1950 |                     for (const auto& log : occlusionInfo.debugInfo) { | 
 | 1951 |                         ALOGD("%s", log.c_str()); | 
 | 1952 |                     } | 
 | 1953 |                 } | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 1954 |                 onUntrustedTouchLocked(occlusionInfo.obscuringPackage); | 
 | 1955 |                 if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { | 
 | 1956 |                     ALOGW("Dropping untrusted touch event due to %s/%d", | 
 | 1957 |                           occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); | 
 | 1958 |                     newTouchedWindowHandle = nullptr; | 
 | 1959 |                 } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 1960 |             } | 
 | 1961 |         } | 
 | 1962 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1963 |         // Also don't send the new touch event to unresponsive gesture monitors | 
 | 1964 |         newGestureMonitors = selectResponsiveMonitorsLocked(newGestureMonitors); | 
 | 1965 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1966 |         if (newTouchedWindowHandle == nullptr && newGestureMonitors.empty()) { | 
 | 1967 |             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] | 1968 |                   "(%d, %d) in display %" PRId32 ".", | 
 | 1969 |                   x, y, displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1970 |             injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1971 |             goto Failed; | 
 | 1972 |         } | 
 | 1973 |  | 
 | 1974 |         if (newTouchedWindowHandle != nullptr) { | 
 | 1975 |             // Set target flags. | 
 | 1976 |             int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 1977 |             if (isSplit) { | 
 | 1978 |                 targetFlags |= InputTarget::FLAG_SPLIT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1979 |             } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1980 |             if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
 | 1981 |                 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
 | 1982 |             } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
 | 1983 |                 targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
 | 1984 |             } | 
 | 1985 |  | 
 | 1986 |             // Update hover state. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1987 |             if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
 | 1988 |                 newHoverWindowHandle = nullptr; | 
 | 1989 |             } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1990 |                 newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1991 |             } | 
 | 1992 |  | 
 | 1993 |             // Update the temporary touch state. | 
 | 1994 |             BitSet32 pointerIds; | 
 | 1995 |             if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1996 |                 uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1997 |                 pointerIds.markBit(pointerId); | 
 | 1998 |             } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1999 |             tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 |         } | 
 | 2001 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2002 |         tempTouchState.addGestureMonitors(newGestureMonitors); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2003 |     } else { | 
 | 2004 |         /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
 | 2005 |  | 
 | 2006 |         // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2007 |         if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2008 |             if (DEBUG_FOCUS) { | 
 | 2009 |                 ALOGD("Dropping event because the pointer is not down or we previously " | 
 | 2010 |                       "dropped the pointer down event in display %" PRId32, | 
 | 2011 |                       displayId); | 
 | 2012 |             } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2013 |             injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2014 |             goto Failed; | 
 | 2015 |         } | 
 | 2016 |  | 
 | 2017 |         // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2018 |         if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2019 |             tempTouchState.isSlippery()) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2020 |             int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
 | 2021 |             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] | 2022 |  | 
 | 2023 |             sp<InputWindowHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2024 |                     tempTouchState.getFirstForegroundWindowHandle(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2025 |             newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2026 |             if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
 | 2027 |                 oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2028 |                 if (DEBUG_FOCUS) { | 
 | 2029 |                     ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
 | 2030 |                           oldTouchedWindowHandle->getName().c_str(), | 
 | 2031 |                           newTouchedWindowHandle->getName().c_str(), displayId); | 
 | 2032 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2033 |                 // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2034 |                 tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
 | 2035 |                                                  InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
 | 2036 |                                                  BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2037 |  | 
 | 2038 |                 // Make a slippery entrance into the new window. | 
 | 2039 |                 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
 | 2040 |                     isSplit = true; | 
 | 2041 |                 } | 
 | 2042 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2043 |                 int32_t targetFlags = | 
 | 2044 |                         InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2045 |                 if (isSplit) { | 
 | 2046 |                     targetFlags |= InputTarget::FLAG_SPLIT; | 
 | 2047 |                 } | 
 | 2048 |                 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
 | 2049 |                     targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2050 |                 } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
 | 2051 |                     targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2052 |                 } | 
 | 2053 |  | 
 | 2054 |                 BitSet32 pointerIds; | 
 | 2055 |                 if (isSplit) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2056 |                     pointerIds.markBit(entry.pointerProperties[0].id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2057 |                 } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2058 |                 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2059 |             } | 
 | 2060 |         } | 
 | 2061 |     } | 
 | 2062 |  | 
 | 2063 |     if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2064 |         // Let the previous window know that the hover sequence is over, unless we already did it | 
 | 2065 |         // when dispatching it as is to newTouchedWindowHandle. | 
 | 2066 |         if (mLastHoverWindowHandle != nullptr && | 
 | 2067 |             (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
 | 2068 |              mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2069 | #if DEBUG_HOVER | 
 | 2070 |             ALOGD("Sending hover exit event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2071 |                   mLastHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2072 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2073 |             tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
 | 2074 |                                              InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2075 |         } | 
 | 2076 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2077 |         // Let the new window know that the hover sequence is starting, unless we already did it | 
 | 2078 |         // when dispatching it as is to newTouchedWindowHandle. | 
 | 2079 |         if (newHoverWindowHandle != nullptr && | 
 | 2080 |             (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
 | 2081 |              newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2082 | #if DEBUG_HOVER | 
 | 2083 |             ALOGD("Sending hover enter event to window %s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2084 |                   newHoverWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2085 | #endif | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2086 |             tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
 | 2087 |                                              InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
 | 2088 |                                              BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2089 |         } | 
 | 2090 |     } | 
 | 2091 |  | 
 | 2092 |     // Check permission to inject into all touched foreground windows and ensure there | 
 | 2093 |     // is at least one touched foreground window. | 
 | 2094 |     { | 
 | 2095 |         bool haveForegroundWindow = false; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2096 |         for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2097 |             if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
 | 2098 |                 haveForegroundWindow = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2099 |                 if (!checkInjectionPermission(touchedWindow.windowHandle, entry.injectionState)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2100 |                     injectionResult = InputEventInjectionResult::PERMISSION_DENIED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2101 |                     injectionPermission = INJECTION_PERMISSION_DENIED; | 
 | 2102 |                     goto Failed; | 
 | 2103 |                 } | 
 | 2104 |             } | 
 | 2105 |         } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2106 |         bool hasGestureMonitor = !tempTouchState.gestureMonitors.empty(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2107 |         if (!haveForegroundWindow && !hasGestureMonitor) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2108 |             ALOGI("Dropping event because there is no touched foreground window in display " | 
 | 2109 |                   "%" PRId32 " or gesture monitor to receive it.", | 
 | 2110 |                   displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2111 |             injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2112 |             goto Failed; | 
 | 2113 |         } | 
 | 2114 |  | 
 | 2115 |         // Permission granted to injection into all touched foreground windows. | 
 | 2116 |         injectionPermission = INJECTION_PERMISSION_GRANTED; | 
 | 2117 |     } | 
 | 2118 |  | 
 | 2119 |     // Check whether windows listening for outside touches are owned by the same UID. If it is | 
 | 2120 |     // set the policy flag that we will not reveal coordinate information to this window. | 
 | 2121 |     if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 2122 |         sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2123 |                 tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2124 |         if (foregroundWindowHandle) { | 
 | 2125 |             const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2126 |             for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2127 |                 if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
 | 2128 |                     sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; | 
 | 2129 |                     if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2130 |                         tempTouchState.addOrUpdateWindow(inputWindowHandle, | 
 | 2131 |                                                          InputTarget::FLAG_ZERO_COORDS, | 
 | 2132 |                                                          BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2133 |                     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2134 |                 } | 
 | 2135 |             } | 
 | 2136 |         } | 
 | 2137 |     } | 
 | 2138 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2139 |     // If this is the first pointer going down and the touched window has a wallpaper | 
 | 2140 |     // then also add the touched wallpaper windows so they are locked in for the duration | 
 | 2141 |     // of the touch gesture. | 
 | 2142 |     // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
 | 2143 |     // engine only supports touch events.  We would need to add a mechanism similar | 
 | 2144 |     // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
 | 2145 |     if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 2146 |         sp<InputWindowHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2147 |                 tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2148 |         if (foregroundWindowHandle && foregroundWindowHandle->getInfo()->hasWallpaper) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2149 |             const std::vector<sp<InputWindowHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2150 |                     getWindowHandlesLocked(displayId); | 
 | 2151 |             for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2152 |                 const InputWindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2153 |                 if (info->displayId == displayId && | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2154 |                     windowHandle->getInfo()->type == InputWindowInfo::Type::WALLPAPER) { | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2155 |                     tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2156 |                             .addOrUpdateWindow(windowHandle, | 
 | 2157 |                                                InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
 | 2158 |                                                        InputTarget:: | 
 | 2159 |                                                                FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
 | 2160 |                                                        InputTarget::FLAG_DISPATCH_AS_IS, | 
 | 2161 |                                                BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2162 |                 } | 
 | 2163 |             } | 
 | 2164 |         } | 
 | 2165 |     } | 
 | 2166 |  | 
 | 2167 |     // Success!  Output targets. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2168 |     injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2169 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2170 |     for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2171 |         addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2172 |                               touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2173 |     } | 
 | 2174 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2175 |     for (const TouchedMonitor& touchedMonitor : tempTouchState.gestureMonitors) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2176 |         addMonitoringTargetLocked(touchedMonitor.monitor, touchedMonitor.xOffset, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2177 |                                   touchedMonitor.yOffset, inputTargets); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2178 |     } | 
 | 2179 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2180 |     // Drop the outside or hover touch windows since we will not care about them | 
 | 2181 |     // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2182 |     tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2183 |  | 
 | 2184 | Failed: | 
 | 2185 |     // Check injection permission once and for all. | 
 | 2186 |     if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2187 |         if (checkInjectionPermission(nullptr, entry.injectionState)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2188 |             injectionPermission = INJECTION_PERMISSION_GRANTED; | 
 | 2189 |         } else { | 
 | 2190 |             injectionPermission = INJECTION_PERMISSION_DENIED; | 
 | 2191 |         } | 
 | 2192 |     } | 
 | 2193 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2194 |     if (injectionPermission != INJECTION_PERMISSION_GRANTED) { | 
 | 2195 |         return injectionResult; | 
 | 2196 |     } | 
 | 2197 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2198 |     // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2199 |     if (!wrongDevice) { | 
 | 2200 |         if (switchedDevice) { | 
 | 2201 |             if (DEBUG_FOCUS) { | 
 | 2202 |                 ALOGD("Conflicting pointer actions: Switched to a different device."); | 
 | 2203 |             } | 
 | 2204 |             *outConflictingPointerActions = true; | 
 | 2205 |         } | 
 | 2206 |  | 
 | 2207 |         if (isHoverAction) { | 
 | 2208 |             // Started hovering, therefore no longer down. | 
 | 2209 |             if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2210 |                 if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2211 |                     ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
 | 2212 |                           "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2213 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2214 |                 *outConflictingPointerActions = true; | 
 | 2215 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2216 |             tempTouchState.reset(); | 
 | 2217 |             if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
 | 2218 |                 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
 | 2219 |                 tempTouchState.deviceId = entry.deviceId; | 
 | 2220 |                 tempTouchState.source = entry.source; | 
 | 2221 |                 tempTouchState.displayId = displayId; | 
 | 2222 |             } | 
 | 2223 |         } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
 | 2224 |                    maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
 | 2225 |             // All pointers up or canceled. | 
 | 2226 |             tempTouchState.reset(); | 
 | 2227 |         } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
 | 2228 |             // First pointer went down. | 
 | 2229 |             if (oldState && oldState->down) { | 
 | 2230 |                 if (DEBUG_FOCUS) { | 
 | 2231 |                     ALOGD("Conflicting pointer actions: Down received while already down."); | 
 | 2232 |                 } | 
 | 2233 |                 *outConflictingPointerActions = true; | 
 | 2234 |             } | 
 | 2235 |         } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
 | 2236 |             // One pointer went up. | 
 | 2237 |             if (isSplit) { | 
 | 2238 |                 int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
 | 2239 |                 uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2240 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2241 |                 for (size_t i = 0; i < tempTouchState.windows.size();) { | 
 | 2242 |                     TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
 | 2243 |                     if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { | 
 | 2244 |                         touchedWindow.pointerIds.clearBit(pointerId); | 
 | 2245 |                         if (touchedWindow.pointerIds.isEmpty()) { | 
 | 2246 |                             tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
 | 2247 |                             continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2248 |                         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2249 |                     } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2250 |                     i += 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 |                 } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2252 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2253 |         } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2254 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2255 |         // Save changes unless the action was scroll in which case the temporary touch | 
 | 2256 |         // state was only valid for this one action. | 
 | 2257 |         if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
 | 2258 |             if (tempTouchState.displayId >= 0) { | 
 | 2259 |                 mTouchStatesByDisplay[displayId] = tempTouchState; | 
 | 2260 |             } else { | 
 | 2261 |                 mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2262 |             } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2263 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2264 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2265 |         // Update hover state. | 
 | 2266 |         mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2267 |     } | 
 | 2268 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2269 |     return injectionResult; | 
 | 2270 | } | 
 | 2271 |  | 
 | 2272 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2273 |                                             int32_t targetFlags, BitSet32 pointerIds, | 
 | 2274 |                                             std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2275 |     std::vector<InputTarget>::iterator it = | 
 | 2276 |             std::find_if(inputTargets.begin(), inputTargets.end(), | 
 | 2277 |                          [&windowHandle](const InputTarget& inputTarget) { | 
 | 2278 |                              return inputTarget.inputChannel->getConnectionToken() == | 
 | 2279 |                                      windowHandle->getToken(); | 
 | 2280 |                          }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2281 |  | 
| Chavi Weingarten | 114b77f | 2020-01-15 22:35:10 +0000 | [diff] [blame] | 2282 |     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2283 |  | 
 | 2284 |     if (it == inputTargets.end()) { | 
 | 2285 |         InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2286 |         std::shared_ptr<InputChannel> inputChannel = | 
 | 2287 |                 getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2288 |         if (inputChannel == nullptr) { | 
 | 2289 |             ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
 | 2290 |             return; | 
 | 2291 |         } | 
 | 2292 |         inputTarget.inputChannel = inputChannel; | 
 | 2293 |         inputTarget.flags = targetFlags; | 
 | 2294 |         inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 2295 |         inputTargets.push_back(inputTarget); | 
 | 2296 |         it = inputTargets.end() - 1; | 
 | 2297 |     } | 
 | 2298 |  | 
 | 2299 |     ALOG_ASSERT(it->flags == targetFlags); | 
 | 2300 |     ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
 | 2301 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2302 |     it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2303 | } | 
 | 2304 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2305 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2306 |                                                        int32_t displayId, float xOffset, | 
 | 2307 |                                                        float yOffset) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2308 |     std::unordered_map<int32_t, std::vector<Monitor>>::const_iterator it = | 
 | 2309 |             mGlobalMonitorsByDisplay.find(displayId); | 
 | 2310 |  | 
 | 2311 |     if (it != mGlobalMonitorsByDisplay.end()) { | 
 | 2312 |         const std::vector<Monitor>& monitors = it->second; | 
 | 2313 |         for (const Monitor& monitor : monitors) { | 
 | 2314 |             addMonitoringTargetLocked(monitor, xOffset, yOffset, inputTargets); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2315 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2316 |     } | 
 | 2317 | } | 
 | 2318 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2319 | void InputDispatcher::addMonitoringTargetLocked(const Monitor& monitor, float xOffset, | 
 | 2320 |                                                 float yOffset, | 
 | 2321 |                                                 std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2322 |     InputTarget target; | 
 | 2323 |     target.inputChannel = monitor.inputChannel; | 
 | 2324 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2325 |     ui::Transform t; | 
 | 2326 |     t.set(xOffset, yOffset); | 
 | 2327 |     target.setDefaultPointerTransform(t); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2328 |     inputTargets.push_back(target); | 
 | 2329 | } | 
 | 2330 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2331 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2332 |                                                const InjectionState* injectionState) { | 
 | 2333 |     if (injectionState && | 
 | 2334 |         (windowHandle == nullptr || | 
 | 2335 |          windowHandle->getInfo()->ownerUid != injectionState->injectorUid) && | 
 | 2336 |         !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2337 |         if (windowHandle != nullptr) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2338 |             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] | 2339 |                   "owned by uid %d", | 
 | 2340 |                   injectionState->injectorPid, injectionState->injectorUid, | 
 | 2341 |                   windowHandle->getName().c_str(), windowHandle->getInfo()->ownerUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2342 |         } else { | 
 | 2343 |             ALOGW("Permission denied: injecting event from pid %d uid %d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2344 |                   injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2345 |         } | 
 | 2346 |         return false; | 
 | 2347 |     } | 
 | 2348 |     return true; | 
 | 2349 | } | 
 | 2350 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2351 | /** | 
 | 2352 |  * Indicate whether one window handle should be considered as obscuring | 
 | 2353 |  * another window handle. We only check a few preconditions. Actually | 
 | 2354 |  * checking the bounds is left to the caller. | 
 | 2355 |  */ | 
 | 2356 | static bool canBeObscuredBy(const sp<InputWindowHandle>& windowHandle, | 
 | 2357 |                             const sp<InputWindowHandle>& otherHandle) { | 
 | 2358 |     // Compare by token so cloned layers aren't counted | 
 | 2359 |     if (haveSameToken(windowHandle, otherHandle)) { | 
 | 2360 |         return false; | 
 | 2361 |     } | 
 | 2362 |     auto info = windowHandle->getInfo(); | 
 | 2363 |     auto otherInfo = otherHandle->getInfo(); | 
 | 2364 |     if (!otherInfo->visible) { | 
 | 2365 |         return false; | 
| Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2366 |     } else if (otherInfo->alpha == 0 && | 
 | 2367 |                otherInfo->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE)) { | 
 | 2368 |         // Those act as if they were invisible, so we don't need to flag them. | 
 | 2369 |         // We do want to potentially flag touchable windows even if they have 0 | 
 | 2370 |         // opacity, since they can consume touches and alter the effects of the | 
 | 2371 |         // user interaction (eg. apps that rely on | 
 | 2372 |         // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those | 
 | 2373 |         // windows), hence we also check for FLAG_NOT_TOUCHABLE. | 
 | 2374 |         return false; | 
| Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2375 |     } else if (info->ownerUid == otherInfo->ownerUid) { | 
 | 2376 |         // If ownerUid is the same we don't generate occlusion events as there | 
 | 2377 |         // is no security boundary within an uid. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2378 |         return false; | 
| Chris Ye | fcdff3e | 2020-05-10 15:16:04 -0700 | [diff] [blame] | 2379 |     } else if (otherInfo->trustedOverlay) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2380 |         return false; | 
 | 2381 |     } else if (otherInfo->displayId != info->displayId) { | 
 | 2382 |         return false; | 
 | 2383 |     } | 
 | 2384 |     return true; | 
 | 2385 | } | 
 | 2386 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2387 | /** | 
 | 2388 |  * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is | 
 | 2389 |  * untrusted, one should check: | 
 | 2390 |  * | 
 | 2391 |  * 1. If result.hasBlockingOcclusion is true. | 
 | 2392 |  *    If it's, it means the touch should be blocked due to a window with occlusion mode of | 
 | 2393 |  *    BLOCK_UNTRUSTED. | 
 | 2394 |  * | 
 | 2395 |  * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. | 
 | 2396 |  *    If it is (and 1 is false), then the touch should be blocked because a stack of windows | 
 | 2397 |  *    (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed | 
 | 2398 |  *    obscuring opacity above the threshold. Note that if there was no window of occlusion mode | 
 | 2399 |  *    USE_OPACITY, result.obscuringOpacity would've been 0 and since | 
 | 2400 |  *    mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. | 
 | 2401 |  * | 
 | 2402 |  * If neither of those is true, then it means the touch can be allowed. | 
 | 2403 |  */ | 
 | 2404 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( | 
 | 2405 |         const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2406 |     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
 | 2407 |     int32_t displayId = windowInfo->displayId; | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2408 |     const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
 | 2409 |     TouchOcclusionInfo info; | 
 | 2410 |     info.hasBlockingOcclusion = false; | 
 | 2411 |     info.obscuringOpacity = 0; | 
 | 2412 |     info.obscuringUid = -1; | 
 | 2413 |     std::map<int32_t, float> opacityByUid; | 
 | 2414 |     for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
 | 2415 |         if (windowHandle == otherHandle) { | 
 | 2416 |             break; // All future windows are below us. Exit early. | 
 | 2417 |         } | 
 | 2418 |         const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
 | 2419 |         if (canBeObscuredBy(windowHandle, otherHandle) && | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2420 |             windowInfo->ownerUid != otherInfo->ownerUid && otherInfo->frameContainsPoint(x, y)) { | 
 | 2421 |             if (DEBUG_TOUCH_OCCLUSION) { | 
 | 2422 |                 info.debugInfo.push_back( | 
 | 2423 |                         dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); | 
 | 2424 |             } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2425 |             // canBeObscuredBy() has returned true above, which means this window is untrusted, so | 
 | 2426 |             // we perform the checks below to see if the touch can be propagated or not based on the | 
 | 2427 |             // window's touch occlusion mode | 
 | 2428 |             if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { | 
 | 2429 |                 info.hasBlockingOcclusion = true; | 
 | 2430 |                 info.obscuringUid = otherInfo->ownerUid; | 
 | 2431 |                 info.obscuringPackage = otherInfo->packageName; | 
 | 2432 |                 break; | 
 | 2433 |             } | 
 | 2434 |             if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { | 
 | 2435 |                 uint32_t uid = otherInfo->ownerUid; | 
 | 2436 |                 float opacity = | 
 | 2437 |                         (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; | 
 | 2438 |                 // Given windows A and B: | 
 | 2439 |                 // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] | 
 | 2440 |                 opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); | 
 | 2441 |                 opacityByUid[uid] = opacity; | 
 | 2442 |                 if (opacity > info.obscuringOpacity) { | 
 | 2443 |                     info.obscuringOpacity = opacity; | 
 | 2444 |                     info.obscuringUid = uid; | 
 | 2445 |                     info.obscuringPackage = otherInfo->packageName; | 
 | 2446 |                 } | 
 | 2447 |             } | 
 | 2448 |         } | 
 | 2449 |     } | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2450 |     if (DEBUG_TOUCH_OCCLUSION) { | 
 | 2451 |         info.debugInfo.push_back( | 
 | 2452 |                 dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); | 
 | 2453 |     } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2454 |     return info; | 
 | 2455 | } | 
 | 2456 |  | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2457 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const InputWindowInfo* info, | 
 | 2458 |                                                          bool isTouchedWindow) const { | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2459 |     return StringPrintf(INDENT2 "* %stype=%s, package=%s/%" PRId32 ", id=%" PRId32 | 
 | 2460 |                                 ", mode=%s, alpha=%.2f, " | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 2461 |                                 "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 | 
 | 2462 |                                 "], touchableRegion=%s, window={%s}, applicationInfo=%s, " | 
 | 2463 |                                 "flags={%s}, inputFeatures={%s}, hasToken=%s\n", | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2464 |                         (isTouchedWindow) ? "[TOUCHED] " : "", | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2465 |                         NamedEnum::string(info->type, "%" PRId32).c_str(), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 2466 |                         info->packageName.c_str(), info->ownerUid, info->id, | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 2467 |                         toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, | 
 | 2468 |                         info->frameTop, info->frameRight, info->frameBottom, | 
 | 2469 |                         dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 2470 |                         info->applicationInfo.name.c_str(), info->flags.string().c_str(), | 
 | 2471 |                         info->inputFeatures.string().c_str(), toString(info->token != nullptr)); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2472 | } | 
 | 2473 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2474 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { | 
 | 2475 |     if (occlusionInfo.hasBlockingOcclusion) { | 
 | 2476 |         ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), | 
 | 2477 |               occlusionInfo.obscuringUid); | 
 | 2478 |         return false; | 
 | 2479 |     } | 
 | 2480 |     if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { | 
 | 2481 |         ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " | 
 | 2482 |               "%.2f, maximum allowed = %.2f)", | 
 | 2483 |               occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, | 
 | 2484 |               occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); | 
 | 2485 |         return false; | 
 | 2486 |     } | 
 | 2487 |     return true; | 
 | 2488 | } | 
 | 2489 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2490 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, | 
 | 2491 |                                                     int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2492 |     int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2493 |     const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2494 |     for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2495 |         if (windowHandle == otherHandle) { | 
 | 2496 |             break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2497 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2498 |         const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2499 |         if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2500 |             otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2501 |             return true; | 
 | 2502 |         } | 
 | 2503 |     } | 
 | 2504 |     return false; | 
 | 2505 | } | 
 | 2506 |  | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2507 | bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const { | 
 | 2508 |     int32_t displayId = windowHandle->getInfo()->displayId; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2509 |     const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2510 |     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2511 |     for (const sp<InputWindowHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2512 |         if (windowHandle == otherHandle) { | 
 | 2513 |             break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2514 |         } | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2515 |         const InputWindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2516 |         if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2517 |             otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2518 |             return true; | 
 | 2519 |         } | 
 | 2520 |     } | 
 | 2521 |     return false; | 
 | 2522 | } | 
 | 2523 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2524 | std::string InputDispatcher::getApplicationWindowLabel( | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 2525 |         const InputApplicationHandle* applicationHandle, | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2526 |         const sp<InputWindowHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2527 |     if (applicationHandle != nullptr) { | 
 | 2528 |         if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2529 |             return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2530 |         } else { | 
 | 2531 |             return applicationHandle->getName(); | 
 | 2532 |         } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2533 |     } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2534 |         return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2535 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2536 |         return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2537 |     } | 
 | 2538 | } | 
 | 2539 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2540 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2541 |     if (eventEntry.type == EventEntry::Type::FOCUS || | 
 | 2542 |         eventEntry.type == EventEntry::Type::POINTER_CAPTURE_CHANGED) { | 
 | 2543 |         // Focus or pointer capture changed events are passed to apps, but do not represent user | 
 | 2544 |         // activity. | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2545 |         return; | 
 | 2546 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2547 |     int32_t displayId = getTargetDisplayId(eventEntry); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2548 |     sp<InputWindowHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2549 |     if (focusedWindowHandle != nullptr) { | 
 | 2550 |         const InputWindowInfo* info = focusedWindowHandle->getInfo(); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2551 |         if (info->inputFeatures.test(InputWindowInfo::Feature::DISABLE_USER_ACTIVITY)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2552 | #if DEBUG_DISPATCH_CYCLE | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2553 |             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] | 2554 | #endif | 
 | 2555 |             return; | 
 | 2556 |         } | 
 | 2557 |     } | 
 | 2558 |  | 
 | 2559 |     int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2560 |     switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2561 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2562 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
 | 2563 |             if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2564 |                 return; | 
 | 2565 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2566 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2567 |             if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2568 |                 eventType = USER_ACTIVITY_EVENT_TOUCH; | 
 | 2569 |             } | 
 | 2570 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2571 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2572 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2573 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
 | 2574 |             if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2575 |                 return; | 
 | 2576 |             } | 
 | 2577 |             eventType = USER_ACTIVITY_EVENT_BUTTON; | 
 | 2578 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2579 |         } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2580 |         case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2581 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2582 |         case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2583 |         case EventEntry::Type::SENSOR: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2584 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2585 |             LOG_ALWAYS_FATAL("%s events are not user activity", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2586 |                              NamedEnum::string(eventEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2587 |             break; | 
 | 2588 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2589 |     } | 
 | 2590 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2591 |     std::unique_ptr<CommandEntry> commandEntry = | 
 | 2592 |             std::make_unique<CommandEntry>(&InputDispatcher::doPokeUserActivityLockedInterruptible); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2593 |     commandEntry->eventTime = eventEntry.eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2594 |     commandEntry->userActivityEventType = eventType; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2595 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2596 | } | 
 | 2597 |  | 
 | 2598 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2599 |                                                  const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2600 |                                                  std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2601 |                                                  const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2602 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2603 |         std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2604 |                 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2605 |                              connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2606 |         ATRACE_NAME(message.c_str()); | 
 | 2607 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2608 | #if DEBUG_DISPATCH_CYCLE | 
 | 2609 |     ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2610 |           "globalScaleFactor=%f, pointerIds=0x%x %s", | 
 | 2611 |           connection->getInputChannelName().c_str(), inputTarget.flags, | 
 | 2612 |           inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
 | 2613 |           inputTarget.getPointerInfoString().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2614 | #endif | 
 | 2615 |  | 
 | 2616 |     // Skip this event if the connection status is not normal. | 
 | 2617 |     // We don't want to enqueue additional outbound events if the connection is broken. | 
 | 2618 |     if (connection->status != Connection::STATUS_NORMAL) { | 
 | 2619 | #if DEBUG_DISPATCH_CYCLE | 
 | 2620 |         ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2621 |               connection->getInputChannelName().c_str(), connection->getStatusLabel()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2622 | #endif | 
 | 2623 |         return; | 
 | 2624 |     } | 
 | 2625 |  | 
 | 2626 |     // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2627 |     if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
 | 2628 |         LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
 | 2629 |                             "Entry type %s should not have FLAG_SPLIT", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2630 |                             NamedEnum::string(eventEntry->type).c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2631 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2632 |         const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2633 |         if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2634 |             std::unique_ptr<MotionEntry> splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2635 |                     splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2636 |             if (!splitMotionEntry) { | 
 | 2637 |                 return; // split event was dropped | 
 | 2638 |             } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2639 |             if (DEBUG_FOCUS) { | 
 | 2640 |                 ALOGD("channel '%s' ~ Split motion event.", | 
 | 2641 |                       connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2642 |                 logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2643 |             } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2644 |             enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), | 
 | 2645 |                                          inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2646 |             return; | 
 | 2647 |         } | 
 | 2648 |     } | 
 | 2649 |  | 
 | 2650 |     // Not splitting.  Enqueue dispatch entries for the event as is. | 
 | 2651 |     enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
 | 2652 | } | 
 | 2653 |  | 
 | 2654 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2655 |                                                    const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2656 |                                                    std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2657 |                                                    const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2658 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2659 |         std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2660 |                 StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2661 |                              connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2662 |         ATRACE_NAME(message.c_str()); | 
 | 2663 |     } | 
 | 2664 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2665 |     bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2666 |  | 
 | 2667 |     // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2668 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2669 |                                InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2670 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2671 |                                InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2672 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2673 |                                InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2674 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2675 |                                InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2676 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2677 |                                InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2678 |     enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2679 |                                InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2680 |  | 
 | 2681 |     // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2682 |     if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2683 |         startDispatchCycleLocked(currentTime, connection); | 
 | 2684 |     } | 
 | 2685 | } | 
 | 2686 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2687 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2688 |                                                  std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2689 |                                                  const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2690 |                                                  int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2691 |     if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2692 |         std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
 | 2693 |                                            connection->getInputChannelName().c_str(), | 
 | 2694 |                                            dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2695 |         ATRACE_NAME(message.c_str()); | 
 | 2696 |     } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2697 |     int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2698 |     if (!(inputTargetFlags & dispatchMode)) { | 
 | 2699 |         return; | 
 | 2700 |     } | 
 | 2701 |     inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
 | 2702 |  | 
 | 2703 |     // This is a new event. | 
 | 2704 |     // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2705 |     std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2706 |             createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2707 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2708 |     // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
 | 2709 |     // different EventEntry than what was passed in. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2710 |     EventEntry& newEntry = *(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2711 |     // Apply target flags and update the connection's input state. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2712 |     switch (newEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2713 |         case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2714 |             const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2715 |             dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2716 |             dispatchEntry->resolvedAction = keyEntry.action; | 
 | 2717 |             dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2718 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2719 |             if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
 | 2720 |                                                  dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2721 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2722 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", | 
 | 2723 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2724 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2725 |                 return; // skip the inconsistent event | 
 | 2726 |             } | 
 | 2727 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2728 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2729 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2730 |         case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2731 |             const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2732 |             // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
 | 2733 |             // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
 | 2734 |             constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
 | 2735 |                     static_cast<int32_t>(IdGenerator::Source::OTHER); | 
 | 2736 |             dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2737 |             if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
 | 2738 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
 | 2739 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
 | 2740 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
 | 2741 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
 | 2742 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
 | 2743 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
 | 2744 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
 | 2745 |             } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
 | 2746 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
 | 2747 |             } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2748 |                 dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2749 |                 dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2750 |             } | 
 | 2751 |             if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2752 |                 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
 | 2753 |                                                    motionEntry.displayId)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2754 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2755 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter " | 
 | 2756 |                       "event", | 
 | 2757 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2758 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2759 |                 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
 | 2760 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2761 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2762 |             dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2763 |             if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
 | 2764 |                 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
 | 2765 |             } | 
 | 2766 |             if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
 | 2767 |                 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
 | 2768 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2769 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2770 |             if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
 | 2771 |                                                     dispatchEntry->resolvedFlags)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2772 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2773 |                 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
 | 2774 |                       "event", | 
 | 2775 |                       connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2776 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2777 |                 return; // skip the inconsistent event | 
 | 2778 |             } | 
 | 2779 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2780 |             dispatchEntry->resolvedEventId = | 
 | 2781 |                     dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
 | 2782 |                     ? mIdGenerator.nextId() | 
 | 2783 |                     : motionEntry.id; | 
 | 2784 |             if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
 | 2785 |                 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
 | 2786 |                                                    ") to MotionEvent(id=0x%" PRIx32 ").", | 
 | 2787 |                                                    motionEntry.id, dispatchEntry->resolvedEventId); | 
 | 2788 |                 ATRACE_NAME(message.c_str()); | 
 | 2789 |             } | 
 | 2790 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2791 |             dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2792 |                                             inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 |  | 
 | 2794 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2795 |         } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 2796 |         case EventEntry::Type::FOCUS: | 
 | 2797 |         case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2798 |             break; | 
 | 2799 |         } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2800 |         case EventEntry::Type::SENSOR: { | 
 | 2801 |             LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); | 
 | 2802 |             break; | 
 | 2803 |         } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2804 |         case EventEntry::Type::CONFIGURATION_CHANGED: | 
 | 2805 |         case EventEntry::Type::DEVICE_RESET: { | 
 | 2806 |             LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 2807 |                              NamedEnum::string(newEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2808 |             break; | 
 | 2809 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2810 |     } | 
 | 2811 |  | 
 | 2812 |     // Remember that we are waiting for this dispatch to complete. | 
 | 2813 |     if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2814 |         incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2815 |     } | 
 | 2816 |  | 
 | 2817 |     // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2818 |     connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2819 |     traceOutboundQueueLength(connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2820 | } | 
 | 2821 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 2822 | /** | 
 | 2823 |  * This function is purely for debugging. It helps us understand where the user interaction | 
 | 2824 |  * was taking place. For example, if user is touching launcher, we will see a log that user | 
 | 2825 |  * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
 | 2826 |  * We will see both launcher and wallpaper in that list. | 
 | 2827 |  * Once the interaction with a particular set of connections starts, no new logs will be printed | 
 | 2828 |  * until the set of interacted connections changes. | 
 | 2829 |  * | 
 | 2830 |  * The following items are skipped, to reduce the logspam: | 
 | 2831 |  * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
 | 2832 |  * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
 | 2833 |  * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
 | 2834 |  * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
 | 2835 |  * Both of those ACTION_UP events would not be logged | 
 | 2836 |  * Monitors (both gesture and global): any gesture monitors or global monitors receiving events | 
 | 2837 |  * will not be logged. This is omitted to reduce the amount of data printed. | 
 | 2838 |  * If you see <none>, it's likely that one of the gesture monitors pilfered the event, and therefore | 
 | 2839 |  * gesture monitor is the only connection receiving the remainder of the gesture. | 
 | 2840 |  */ | 
 | 2841 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
 | 2842 |                                                     const std::vector<InputTarget>& targets) { | 
 | 2843 |     // Skip ACTION_UP events, and all events other than keys and motions | 
 | 2844 |     if (entry.type == EventEntry::Type::KEY) { | 
 | 2845 |         const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
 | 2846 |         if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
 | 2847 |             return; | 
 | 2848 |         } | 
 | 2849 |     } else if (entry.type == EventEntry::Type::MOTION) { | 
 | 2850 |         const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
 | 2851 |         if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
 | 2852 |             motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
 | 2853 |             return; | 
 | 2854 |         } | 
 | 2855 |     } else { | 
 | 2856 |         return; // Not a key or a motion | 
 | 2857 |     } | 
 | 2858 |  | 
 | 2859 |     std::unordered_set<sp<IBinder>, IBinderHash> newConnectionTokens; | 
 | 2860 |     std::vector<sp<Connection>> newConnections; | 
 | 2861 |     for (const InputTarget& target : targets) { | 
 | 2862 |         if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
 | 2863 |             InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
 | 2864 |             continue; // Skip windows that receive ACTION_OUTSIDE | 
 | 2865 |         } | 
 | 2866 |  | 
 | 2867 |         sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
 | 2868 |         sp<Connection> connection = getConnectionLocked(token); | 
 | 2869 |         if (connection == nullptr || connection->monitor) { | 
 | 2870 |             continue; // We only need to keep track of the non-monitor connections. | 
 | 2871 |         } | 
 | 2872 |         newConnectionTokens.insert(std::move(token)); | 
 | 2873 |         newConnections.emplace_back(connection); | 
 | 2874 |     } | 
 | 2875 |     if (newConnectionTokens == mInteractionConnectionTokens) { | 
 | 2876 |         return; // no change | 
 | 2877 |     } | 
 | 2878 |     mInteractionConnectionTokens = newConnectionTokens; | 
 | 2879 |  | 
 | 2880 |     std::string windowList; | 
 | 2881 |     for (const sp<Connection>& connection : newConnections) { | 
 | 2882 |         windowList += connection->getWindowName() + ", "; | 
 | 2883 |     } | 
 | 2884 |     std::string message = "Interaction with windows: " + windowList; | 
 | 2885 |     if (windowList.empty()) { | 
 | 2886 |         message += "<none>"; | 
 | 2887 |     } | 
 | 2888 |     android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
 | 2889 | } | 
 | 2890 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2891 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2892 |                                                       const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2893 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2894 |     uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
 | 2895 |     if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2896 |         return; | 
 | 2897 |     } | 
 | 2898 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2899 |     sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
 | 2900 |     if (focusedToken == token) { | 
 | 2901 |         // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2902 |         return; | 
 | 2903 |     } | 
 | 2904 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2905 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 2906 |             &InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 2907 |     commandEntry->newToken = token; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 2908 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2909 | } | 
 | 2910 |  | 
 | 2911 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2912 |                                                const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2913 |     if (ATRACE_ENABLED()) { | 
 | 2914 |         std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2915 |                                            connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2916 |         ATRACE_NAME(message.c_str()); | 
 | 2917 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2918 | #if DEBUG_DISPATCH_CYCLE | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2919 |     ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | #endif | 
 | 2921 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2922 |     while (connection->status == Connection::STATUS_NORMAL && !connection->outboundQueue.empty()) { | 
 | 2923 |         DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2924 |         dispatchEntry->deliveryTime = currentTime; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2925 |         const std::chrono::nanoseconds timeout = | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2926 |                 getDispatchingTimeoutLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 2927 |         dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 |  | 
 | 2929 |         // Publish the event. | 
 | 2930 |         status_t status; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2931 |         const EventEntry& eventEntry = *(dispatchEntry->eventEntry); | 
 | 2932 |         switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2933 |             case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2934 |                 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
 | 2935 |                 std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2936 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2937 |                 // Publish the key event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2938 |                 status = connection->inputPublisher | 
 | 2939 |                                  .publishKeyEvent(dispatchEntry->seq, | 
 | 2940 |                                                   dispatchEntry->resolvedEventId, keyEntry.deviceId, | 
 | 2941 |                                                   keyEntry.source, keyEntry.displayId, | 
 | 2942 |                                                   std::move(hmac), dispatchEntry->resolvedAction, | 
 | 2943 |                                                   dispatchEntry->resolvedFlags, keyEntry.keyCode, | 
 | 2944 |                                                   keyEntry.scanCode, keyEntry.metaState, | 
 | 2945 |                                                   keyEntry.repeatCount, keyEntry.downTime, | 
 | 2946 |                                                   keyEntry.eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2947 |                 break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 |             } | 
 | 2949 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2950 |             case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2951 |                 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2952 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2953 |                 PointerCoords scaledCoords[MAX_POINTERS]; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2954 |                 const PointerCoords* usingCoords = motionEntry.pointerCoords; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2955 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2956 |                 // Set the X and Y offset and X and Y scale depending on the input source. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2957 |                 if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2958 |                     !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
 | 2959 |                     float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2960 |                     if (globalScaleFactor != 1.0f) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2961 |                         for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
 | 2962 |                             scaledCoords[i] = motionEntry.pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 2963 |                             // Don't apply window scale here since we don't want scale to affect raw | 
 | 2964 |                             // coordinates. The scale will be sent back to the client and applied | 
 | 2965 |                             // later when requesting relative coordinates. | 
 | 2966 |                             scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
 | 2967 |                                                   1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2968 |                         } | 
 | 2969 |                         usingCoords = scaledCoords; | 
 | 2970 |                     } | 
 | 2971 |                 } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2972 |                     // We don't want the dispatch target to know. | 
 | 2973 |                     if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2974 |                         for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2975 |                             scaledCoords[i].clear(); | 
 | 2976 |                         } | 
 | 2977 |                         usingCoords = scaledCoords; | 
 | 2978 |                     } | 
 | 2979 |                 } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 2980 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2981 |                 std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2982 |  | 
 | 2983 |                 // Publish the motion event. | 
 | 2984 |                 status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2985 |                                  .publishMotionEvent(dispatchEntry->seq, | 
 | 2986 |                                                      dispatchEntry->resolvedEventId, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2987 |                                                      motionEntry.deviceId, motionEntry.source, | 
 | 2988 |                                                      motionEntry.displayId, std::move(hmac), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2989 |                                                      dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2990 |                                                      motionEntry.actionButton, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2991 |                                                      dispatchEntry->resolvedFlags, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2992 |                                                      motionEntry.edgeFlags, motionEntry.metaState, | 
 | 2993 |                                                      motionEntry.buttonState, | 
 | 2994 |                                                      motionEntry.classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2995 |                                                      dispatchEntry->transform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2996 |                                                      motionEntry.xPrecision, motionEntry.yPrecision, | 
 | 2997 |                                                      motionEntry.xCursorPosition, | 
 | 2998 |                                                      motionEntry.yCursorPosition, | 
 | 2999 |                                                      motionEntry.downTime, motionEntry.eventTime, | 
 | 3000 |                                                      motionEntry.pointerCount, | 
 | 3001 |                                                      motionEntry.pointerProperties, usingCoords); | 
 | 3002 |                 reportTouchEventForStatistics(motionEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3003 |                 break; | 
 | 3004 |             } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3005 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3006 |             case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3007 |                 const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3008 |                 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3009 |                                                                       focusEntry.id, | 
 | 3010 |                                                                       focusEntry.hasFocus, | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3011 |                                                                       mInTouchMode); | 
 | 3012 |                 break; | 
 | 3013 |             } | 
 | 3014 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3015 |             case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
 | 3016 |                 const auto& captureEntry = | 
 | 3017 |                         static_cast<const PointerCaptureChangedEntry&>(eventEntry); | 
 | 3018 |                 status = connection->inputPublisher | 
 | 3019 |                                  .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, | 
 | 3020 |                                                       captureEntry.pointerCaptureEnabled); | 
 | 3021 |                 break; | 
 | 3022 |             } | 
 | 3023 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3024 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3025 |             case EventEntry::Type::DEVICE_RESET: | 
 | 3026 |             case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3027 |                 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3028 |                                  NamedEnum::string(eventEntry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3029 |                 return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3030 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 |         } | 
 | 3032 |  | 
 | 3033 |         // Check the result. | 
 | 3034 |         if (status) { | 
 | 3035 |             if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3036 |                 if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3037 |                     ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3038 |                           "This is unexpected because the wait queue is empty, so the pipe " | 
 | 3039 |                           "should be empty and we shouldn't have any problems writing an " | 
 | 3040 |                           "event to it, status=%d", | 
 | 3041 |                           connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3042 |                     abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
 | 3043 |                 } else { | 
 | 3044 |                     // Pipe is full and we are waiting for the app to finish process some events | 
 | 3045 |                     // before sending more events to it. | 
 | 3046 | #if DEBUG_DISPATCH_CYCLE | 
 | 3047 |                     ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3048 |                           "waiting for the application to catch up", | 
 | 3049 |                           connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3050 | #endif | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3051 |                 } | 
 | 3052 |             } else { | 
 | 3053 |                 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3054 |                       "status=%d", | 
 | 3055 |                       connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3056 |                 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
 | 3057 |             } | 
 | 3058 |             return; | 
 | 3059 |         } | 
 | 3060 |  | 
 | 3061 |         // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3062 |         connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
 | 3063 |                                                     connection->outboundQueue.end(), | 
 | 3064 |                                                     dispatchEntry)); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3065 |         traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3066 |         connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3067 |         if (connection->responsive) { | 
 | 3068 |             mAnrTracker.insert(dispatchEntry->timeoutTime, | 
 | 3069 |                                connection->inputChannel->getConnectionToken()); | 
 | 3070 |         } | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3071 |         traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3072 |     } | 
 | 3073 | } | 
 | 3074 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3075 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
 | 3076 |     size_t size; | 
 | 3077 |     switch (event.type) { | 
 | 3078 |         case VerifiedInputEvent::Type::KEY: { | 
 | 3079 |             size = sizeof(VerifiedKeyEvent); | 
 | 3080 |             break; | 
 | 3081 |         } | 
 | 3082 |         case VerifiedInputEvent::Type::MOTION: { | 
 | 3083 |             size = sizeof(VerifiedMotionEvent); | 
 | 3084 |             break; | 
 | 3085 |         } | 
 | 3086 |     } | 
 | 3087 |     const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
 | 3088 |     return mHmacKeyManager.sign(start, size); | 
 | 3089 | } | 
 | 3090 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3091 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
 | 3092 |         const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
 | 3093 |     int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
 | 3094 |     if ((actionMasked == AMOTION_EVENT_ACTION_UP) || (actionMasked == AMOTION_EVENT_ACTION_DOWN)) { | 
 | 3095 |         // Only sign events up and down events as the purely move events | 
 | 3096 |         // are tied to their up/down counterparts so signing would be redundant. | 
 | 3097 |         VerifiedMotionEvent verifiedEvent = verifiedMotionEventFromMotionEntry(motionEntry); | 
 | 3098 |         verifiedEvent.actionMasked = actionMasked; | 
 | 3099 |         verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3100 |         return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3101 |     } | 
 | 3102 |     return INVALID_HMAC; | 
 | 3103 | } | 
 | 3104 |  | 
 | 3105 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
 | 3106 |         const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
 | 3107 |     VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
 | 3108 |     verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
 | 3109 |     verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3110 |     return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3111 | } | 
 | 3112 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3113 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3114 |                                                 const sp<Connection>& connection, uint32_t seq, | 
 | 3115 |                                                 bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3116 | #if DEBUG_DISPATCH_CYCLE | 
 | 3117 |     ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3118 |           connection->getInputChannelName().c_str(), seq, toString(handled)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3119 | #endif | 
 | 3120 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3121 |     if (connection->status == Connection::STATUS_BROKEN || | 
 | 3122 |         connection->status == Connection::STATUS_ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3123 |         return; | 
 | 3124 |     } | 
 | 3125 |  | 
 | 3126 |     // Notify other system components and prepare to start the next dispatch cycle. | 
 | 3127 |     onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); | 
 | 3128 | } | 
 | 3129 |  | 
 | 3130 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3131 |                                                      const sp<Connection>& connection, | 
 | 3132 |                                                      bool notify) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3133 | #if DEBUG_DISPATCH_CYCLE | 
 | 3134 |     ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3135 |           connection->getInputChannelName().c_str(), toString(notify)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3136 | #endif | 
 | 3137 |  | 
 | 3138 |     // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3139 |     drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3140 |     traceOutboundQueueLength(connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3141 |     drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 3142 |     traceWaitQueueLength(connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3143 |  | 
 | 3144 |     // The connection appears to be unrecoverably broken. | 
 | 3145 |     // Ignore already broken or zombie connections. | 
 | 3146 |     if (connection->status == Connection::STATUS_NORMAL) { | 
 | 3147 |         connection->status = Connection::STATUS_BROKEN; | 
 | 3148 |  | 
 | 3149 |         if (notify) { | 
 | 3150 |             // Notify other system components. | 
 | 3151 |             onDispatchCycleBrokenLocked(currentTime, connection); | 
 | 3152 |         } | 
 | 3153 |     } | 
 | 3154 | } | 
 | 3155 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3156 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
 | 3157 |     while (!queue.empty()) { | 
 | 3158 |         DispatchEntry* dispatchEntry = queue.front(); | 
 | 3159 |         queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3160 |         releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3161 |     } | 
 | 3162 | } | 
 | 3163 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3164 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3165 |     if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3166 |         decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3167 |     } | 
 | 3168 |     delete dispatchEntry; | 
 | 3169 | } | 
 | 3170 |  | 
 | 3171 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { | 
 | 3172 |     InputDispatcher* d = static_cast<InputDispatcher*>(data); | 
 | 3173 |  | 
 | 3174 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3175 |         std::scoped_lock _l(d->mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3176 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3177 |         if (d->mConnectionsByFd.find(fd) == d->mConnectionsByFd.end()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3178 |             ALOGE("Received spurious receive callback for unknown input channel.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3179 |                   "fd=%d, events=0x%x", | 
 | 3180 |                   fd, events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3181 |             return 0; // remove the callback | 
 | 3182 |         } | 
 | 3183 |  | 
 | 3184 |         bool notify; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3185 |         sp<Connection> connection = d->mConnectionsByFd[fd]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3186 |         if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
 | 3187 |             if (!(events & ALOOPER_EVENT_INPUT)) { | 
 | 3188 |                 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3189 |                       "events=0x%x", | 
 | 3190 |                       connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3191 |                 return 1; | 
 | 3192 |             } | 
 | 3193 |  | 
 | 3194 |             nsecs_t currentTime = now(); | 
 | 3195 |             bool gotOne = false; | 
 | 3196 |             status_t status; | 
 | 3197 |             for (;;) { | 
 | 3198 |                 uint32_t seq; | 
 | 3199 |                 bool handled; | 
 | 3200 |                 status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); | 
 | 3201 |                 if (status) { | 
 | 3202 |                     break; | 
 | 3203 |                 } | 
 | 3204 |                 d->finishDispatchCycleLocked(currentTime, connection, seq, handled); | 
 | 3205 |                 gotOne = true; | 
 | 3206 |             } | 
 | 3207 |             if (gotOne) { | 
 | 3208 |                 d->runCommandsLockedInterruptible(); | 
 | 3209 |                 if (status == WOULD_BLOCK) { | 
 | 3210 |                     return 1; | 
 | 3211 |                 } | 
 | 3212 |             } | 
 | 3213 |  | 
 | 3214 |             notify = status != DEAD_OBJECT || !connection->monitor; | 
 | 3215 |             if (notify) { | 
 | 3216 |                 ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3217 |                       connection->getInputChannelName().c_str(), status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3218 |             } | 
 | 3219 |         } else { | 
 | 3220 |             // Monitor channels are never explicitly unregistered. | 
 | 3221 |             // We do it automatically when the remote endpoint is closed so don't warn | 
 | 3222 |             // about them. | 
| arthurhung | d352cb3 | 2020-04-28 17:09:28 +0800 | [diff] [blame] | 3223 |             const bool stillHaveWindowHandle = | 
 | 3224 |                     d->getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != | 
 | 3225 |                     nullptr; | 
 | 3226 |             notify = !connection->monitor && stillHaveWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3227 |             if (notify) { | 
 | 3228 |                 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3229 |                       "events=0x%x", | 
 | 3230 |                       connection->getInputChannelName().c_str(), events); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 |             } | 
 | 3232 |         } | 
 | 3233 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3234 |         // Remove the channel. | 
 | 3235 |         d->removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3236 |         return 0; // remove the callback | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3237 |     }             // release lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3238 | } | 
 | 3239 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3240 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3241 |         const CancelationOptions& options) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3242 |     for (const auto& [fd, connection] : mConnectionsByFd) { | 
 | 3243 |         synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3244 |     } | 
 | 3245 | } | 
 | 3246 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3247 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3248 |         const CancelationOptions& options) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3249 |     synthesizeCancelationEventsForMonitorsLocked(options, mGlobalMonitorsByDisplay); | 
 | 3250 |     synthesizeCancelationEventsForMonitorsLocked(options, mGestureMonitorsByDisplay); | 
 | 3251 | } | 
 | 3252 |  | 
 | 3253 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
 | 3254 |         const CancelationOptions& options, | 
 | 3255 |         std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
 | 3256 |     for (const auto& it : monitorsByDisplay) { | 
 | 3257 |         const std::vector<Monitor>& monitors = it.second; | 
 | 3258 |         for (const Monitor& monitor : monitors) { | 
 | 3259 |             synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3260 |         } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3261 |     } | 
 | 3262 | } | 
 | 3263 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3264 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3265 |         const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3266 |     sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3267 |     if (connection == nullptr) { | 
 | 3268 |         return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3269 |     } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3270 |  | 
 | 3271 |     synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3272 | } | 
 | 3273 |  | 
 | 3274 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
 | 3275 |         const sp<Connection>& connection, const CancelationOptions& options) { | 
 | 3276 |     if (connection->status == Connection::STATUS_BROKEN) { | 
 | 3277 |         return; | 
 | 3278 |     } | 
 | 3279 |  | 
 | 3280 |     nsecs_t currentTime = now(); | 
 | 3281 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3282 |     std::vector<std::unique_ptr<EventEntry>> cancelationEvents = | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3283 |             connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3284 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3285 |     if (cancelationEvents.empty()) { | 
 | 3286 |         return; | 
 | 3287 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3288 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3289 |     ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
 | 3290 |           "with reality: %s, mode=%d.", | 
 | 3291 |           connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
 | 3292 |           options.mode); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3293 | #endif | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3294 |  | 
 | 3295 |     InputTarget target; | 
 | 3296 |     sp<InputWindowHandle> windowHandle = | 
 | 3297 |             getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
 | 3298 |     if (windowHandle != nullptr) { | 
 | 3299 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3300 |         target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3301 |         target.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 3302 |     } | 
 | 3303 |     target.inputChannel = connection->inputChannel; | 
 | 3304 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 3305 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3306 |     for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3307 |         std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3308 |         switch (cancelationEventEntry->type) { | 
 | 3309 |             case EventEntry::Type::KEY: { | 
 | 3310 |                 logOutboundKeyDetails("cancel - ", | 
 | 3311 |                                       static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
 | 3312 |                 break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3313 |             } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3314 |             case EventEntry::Type::MOTION: { | 
 | 3315 |                 logOutboundMotionDetails("cancel - ", | 
 | 3316 |                                          static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
 | 3317 |                 break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3318 |             } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3319 |             case EventEntry::Type::FOCUS: | 
 | 3320 |             case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
 | 3321 |                 LOG_ALWAYS_FATAL("Canceling %s events is not supported", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3322 |                                  NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3323 |                 break; | 
 | 3324 |             } | 
 | 3325 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3326 |             case EventEntry::Type::DEVICE_RESET: | 
 | 3327 |             case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3328 |                 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3329 |                                  NamedEnum::string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3330 |                 break; | 
 | 3331 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3332 |         } | 
 | 3333 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3334 |         enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, | 
 | 3335 |                                    InputTarget::FLAG_DISPATCH_AS_IS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3336 |     } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3337 |  | 
 | 3338 |     startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3339 | } | 
 | 3340 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3341 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
 | 3342 |         const sp<Connection>& connection) { | 
 | 3343 |     if (connection->status == Connection::STATUS_BROKEN) { | 
 | 3344 |         return; | 
 | 3345 |     } | 
 | 3346 |  | 
 | 3347 |     nsecs_t currentTime = now(); | 
 | 3348 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3349 |     std::vector<std::unique_ptr<EventEntry>> downEvents = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3350 |             connection->inputState.synthesizePointerDownEvents(currentTime); | 
 | 3351 |  | 
 | 3352 |     if (downEvents.empty()) { | 
 | 3353 |         return; | 
 | 3354 |     } | 
 | 3355 |  | 
 | 3356 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 3357 |         ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
 | 3358 |               connection->getInputChannelName().c_str(), downEvents.size()); | 
 | 3359 | #endif | 
 | 3360 |  | 
 | 3361 |     InputTarget target; | 
 | 3362 |     sp<InputWindowHandle> windowHandle = | 
 | 3363 |             getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
 | 3364 |     if (windowHandle != nullptr) { | 
 | 3365 |         const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3366 |         target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3367 |         target.globalScaleFactor = windowInfo->globalScaleFactor; | 
 | 3368 |     } | 
 | 3369 |     target.inputChannel = connection->inputChannel; | 
 | 3370 |     target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 3371 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3372 |     for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3373 |         switch (downEventEntry->type) { | 
 | 3374 |             case EventEntry::Type::MOTION: { | 
 | 3375 |                 logOutboundMotionDetails("down - ", | 
 | 3376 |                         static_cast<const MotionEntry&>(*downEventEntry)); | 
 | 3377 |                 break; | 
 | 3378 |             } | 
 | 3379 |  | 
 | 3380 |             case EventEntry::Type::KEY: | 
 | 3381 |             case EventEntry::Type::FOCUS: | 
 | 3382 |             case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3383 |             case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3384 |             case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
 | 3385 |             case EventEntry::Type::SENSOR: { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3386 |                 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3387 |                                  NamedEnum::string(downEventEntry->type).c_str()); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3388 |                 break; | 
 | 3389 |             } | 
 | 3390 |         } | 
 | 3391 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3392 |         enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, | 
 | 3393 |                                    InputTarget::FLAG_DISPATCH_AS_IS); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3394 |     } | 
 | 3395 |  | 
 | 3396 |     startDispatchCycleLocked(currentTime, connection); | 
 | 3397 | } | 
 | 3398 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3399 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( | 
 | 3400 |         const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3401 |     ALOG_ASSERT(pointerIds.value != 0); | 
 | 3402 |  | 
 | 3403 |     uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
 | 3404 |     PointerProperties splitPointerProperties[MAX_POINTERS]; | 
 | 3405 |     PointerCoords splitPointerCoords[MAX_POINTERS]; | 
 | 3406 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3407 |     uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3408 |     uint32_t splitPointerCount = 0; | 
 | 3409 |  | 
 | 3410 |     for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3411 |          originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3412 |         const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3413 |                 originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3414 |         uint32_t pointerId = uint32_t(pointerProperties.id); | 
 | 3415 |         if (pointerIds.hasBit(pointerId)) { | 
 | 3416 |             splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
 | 3417 |             splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
 | 3418 |             splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3419 |                     originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3420 |             splitPointerCount += 1; | 
 | 3421 |         } | 
 | 3422 |     } | 
 | 3423 |  | 
 | 3424 |     if (splitPointerCount != pointerIds.count()) { | 
 | 3425 |         // This is bad.  We are missing some of the pointers that we expected to deliver. | 
 | 3426 |         // Most likely this indicates that we received an ACTION_MOVE events that has | 
 | 3427 |         // different pointer ids than we expected based on the previous ACTION_DOWN | 
 | 3428 |         // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
 | 3429 |         // in this way. | 
 | 3430 |         ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3431 |               "we expected there to be %d pointers.  This probably means we received " | 
 | 3432 |               "a broken sequence of pointer ids from the input device.", | 
 | 3433 |               splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3434 |         return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3435 |     } | 
 | 3436 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3437 |     int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3438 |     int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3439 |     if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
 | 3440 |         maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3441 |         int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
 | 3442 |         const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3443 |                 originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 |         uint32_t pointerId = uint32_t(pointerProperties.id); | 
 | 3445 |         if (pointerIds.hasBit(pointerId)) { | 
 | 3446 |             if (pointerIds.count() == 1) { | 
 | 3447 |                 // The first/last pointer went down/up. | 
 | 3448 |                 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3449 |                         ? AMOTION_EVENT_ACTION_DOWN | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3450 |                         : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 | 
 | 3451 |                                 ? AMOTION_EVENT_ACTION_CANCEL | 
 | 3452 |                                 : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3453 |             } else { | 
 | 3454 |                 // A secondary pointer went down/up. | 
 | 3455 |                 uint32_t splitPointerIndex = 0; | 
 | 3456 |                 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
 | 3457 |                     splitPointerIndex += 1; | 
 | 3458 |                 } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3459 |                 action = maskedAction | | 
 | 3460 |                         (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3461 |             } | 
 | 3462 |         } else { | 
 | 3463 |             // An unrelated pointer changed. | 
 | 3464 |             action = AMOTION_EVENT_ACTION_MOVE; | 
 | 3465 |         } | 
 | 3466 |     } | 
 | 3467 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3468 |     int32_t newId = mIdGenerator.nextId(); | 
 | 3469 |     if (ATRACE_ENABLED()) { | 
 | 3470 |         std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
 | 3471 |                                            ") to MotionEvent(id=0x%" PRIx32 ").", | 
 | 3472 |                                            originalMotionEntry.id, newId); | 
 | 3473 |         ATRACE_NAME(message.c_str()); | 
 | 3474 |     } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3475 |     std::unique_ptr<MotionEntry> splitMotionEntry = | 
 | 3476 |             std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, | 
 | 3477 |                                           originalMotionEntry.deviceId, originalMotionEntry.source, | 
 | 3478 |                                           originalMotionEntry.displayId, | 
 | 3479 |                                           originalMotionEntry.policyFlags, action, | 
 | 3480 |                                           originalMotionEntry.actionButton, | 
 | 3481 |                                           originalMotionEntry.flags, originalMotionEntry.metaState, | 
 | 3482 |                                           originalMotionEntry.buttonState, | 
 | 3483 |                                           originalMotionEntry.classification, | 
 | 3484 |                                           originalMotionEntry.edgeFlags, | 
 | 3485 |                                           originalMotionEntry.xPrecision, | 
 | 3486 |                                           originalMotionEntry.yPrecision, | 
 | 3487 |                                           originalMotionEntry.xCursorPosition, | 
 | 3488 |                                           originalMotionEntry.yCursorPosition, | 
 | 3489 |                                           originalMotionEntry.downTime, splitPointerCount, | 
 | 3490 |                                           splitPointerProperties, splitPointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3491 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3492 |     if (originalMotionEntry.injectionState) { | 
 | 3493 |         splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3494 |         splitMotionEntry->injectionState->refCount += 1; | 
 | 3495 |     } | 
 | 3496 |  | 
 | 3497 |     return splitMotionEntry; | 
 | 3498 | } | 
 | 3499 |  | 
 | 3500 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
 | 3501 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3502 |     ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3503 | #endif | 
 | 3504 |  | 
 | 3505 |     bool needWake; | 
 | 3506 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3507 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3509 |         std::unique_ptr<ConfigurationChangedEntry> newEntry = | 
 | 3510 |                 std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); | 
 | 3511 |         needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3512 |     } // release lock | 
 | 3513 |  | 
 | 3514 |     if (needWake) { | 
 | 3515 |         mLooper->wake(); | 
 | 3516 |     } | 
 | 3517 | } | 
 | 3518 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3519 | /** | 
 | 3520 |  * If one of the meta shortcuts is detected, process them here: | 
 | 3521 |  *     Meta + Backspace -> generate BACK | 
 | 3522 |  *     Meta + Enter -> generate HOME | 
 | 3523 |  * This will potentially overwrite keyCode and metaState. | 
 | 3524 |  */ | 
 | 3525 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3526 |                                               int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3527 |     if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
 | 3528 |         int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
 | 3529 |         if (keyCode == AKEYCODE_DEL) { | 
 | 3530 |             newKeyCode = AKEYCODE_BACK; | 
 | 3531 |         } else if (keyCode == AKEYCODE_ENTER) { | 
 | 3532 |             newKeyCode = AKEYCODE_HOME; | 
 | 3533 |         } | 
 | 3534 |         if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3535 |             std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3536 |             struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3537 |             mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3538 |             keyCode = newKeyCode; | 
 | 3539 |             metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
 | 3540 |         } | 
 | 3541 |     } else if (action == AKEY_EVENT_ACTION_UP) { | 
 | 3542 |         // In order to maintain a consistent stream of up and down events, check to see if the key | 
 | 3543 |         // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
 | 3544 |         // 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] | 3545 |         std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3546 |         struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3547 |         auto replacementIt = mReplacedKeys.find(replacement); | 
 | 3548 |         if (replacementIt != mReplacedKeys.end()) { | 
 | 3549 |             keyCode = replacementIt->second; | 
 | 3550 |             mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3551 |             metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
 | 3552 |         } | 
 | 3553 |     } | 
 | 3554 | } | 
 | 3555 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3556 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
 | 3557 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3558 |     ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
 | 3559 |           "policyFlags=0x%x, action=0x%x, " | 
 | 3560 |           "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
 | 3561 |           args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
 | 3562 |           args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
 | 3563 |           args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3564 | #endif | 
 | 3565 |     if (!validateKeyEvent(args->action)) { | 
 | 3566 |         return; | 
 | 3567 |     } | 
 | 3568 |  | 
 | 3569 |     uint32_t policyFlags = args->policyFlags; | 
 | 3570 |     int32_t flags = args->flags; | 
 | 3571 |     int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3572 |     // InputDispatcher tracks and generates key repeats on behalf of | 
 | 3573 |     // whatever notifies it, so repeatCount should always be set to 0 | 
 | 3574 |     constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3575 |     if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
 | 3576 |         policyFlags |= POLICY_FLAG_VIRTUAL; | 
 | 3577 |         flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
 | 3578 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3579 |     if (policyFlags & POLICY_FLAG_FUNCTION) { | 
 | 3580 |         metaState |= AMETA_FUNCTION_ON; | 
 | 3581 |     } | 
 | 3582 |  | 
 | 3583 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
 | 3584 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3585 |     int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3586 |     accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3587 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3588 |     KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3589 |     event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3590 |                      args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
 | 3591 |                      args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3593 |     android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3594 |     mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3595 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3596 |         ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3597 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3598 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3599 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3600 |     bool needWake; | 
 | 3601 |     { // acquire lock | 
 | 3602 |         mLock.lock(); | 
 | 3603 |  | 
 | 3604 |         if (shouldSendKeyToInputFilterLocked(args)) { | 
 | 3605 |             mLock.unlock(); | 
 | 3606 |  | 
 | 3607 |             policyFlags |= POLICY_FLAG_FILTERED; | 
 | 3608 |             if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
 | 3609 |                 return; // event was consumed by the filter | 
 | 3610 |             } | 
 | 3611 |  | 
 | 3612 |             mLock.lock(); | 
 | 3613 |         } | 
 | 3614 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3615 |         std::unique_ptr<KeyEntry> newEntry = | 
 | 3616 |                 std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, | 
 | 3617 |                                            args->displayId, policyFlags, args->action, flags, | 
 | 3618 |                                            keyCode, args->scanCode, metaState, repeatCount, | 
 | 3619 |                                            args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3620 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3621 |         needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3622 |         mLock.unlock(); | 
 | 3623 |     } // release lock | 
 | 3624 |  | 
 | 3625 |     if (needWake) { | 
 | 3626 |         mLooper->wake(); | 
 | 3627 |     } | 
 | 3628 | } | 
 | 3629 |  | 
 | 3630 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
 | 3631 |     return mInputFilterEnabled; | 
 | 3632 | } | 
 | 3633 |  | 
 | 3634 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
 | 3635 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3636 |     ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
 | 3637 |           "displayId=%" PRId32 ", policyFlags=0x%x, " | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3638 |           "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
 | 3639 |           "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
| Garfield Tan | ab0ab9c | 2019-07-10 18:58:28 -0700 | [diff] [blame] | 3640 |           "yCursorPosition=%f, downTime=%" PRId64, | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3641 |           args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
 | 3642 |           args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
 | 3643 |           args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
 | 3644 |           args->xCursorPosition, args->yCursorPosition, args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3645 |     for (uint32_t i = 0; i < args->pointerCount; i++) { | 
 | 3646 |         ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3647 |               "x=%f, y=%f, pressure=%f, size=%f, " | 
 | 3648 |               "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
 | 3649 |               "orientation=%f", | 
 | 3650 |               i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
 | 3651 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
 | 3652 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
 | 3653 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
 | 3654 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
 | 3655 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
 | 3656 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
 | 3657 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
 | 3658 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
 | 3659 |               args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3660 |     } | 
 | 3661 | #endif | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3662 |     if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
 | 3663 |                              args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3664 |         return; | 
 | 3665 |     } | 
 | 3666 |  | 
 | 3667 |     uint32_t policyFlags = args->policyFlags; | 
 | 3668 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3669 |  | 
 | 3670 |     android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 3671 |     mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3672 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3673 |         ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3674 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3675 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3676 |  | 
 | 3677 |     bool needWake; | 
 | 3678 |     { // acquire lock | 
 | 3679 |         mLock.lock(); | 
 | 3680 |  | 
 | 3681 |         if (shouldSendMotionToInputFilterLocked(args)) { | 
 | 3682 |             mLock.unlock(); | 
 | 3683 |  | 
 | 3684 |             MotionEvent event; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3685 |             ui::Transform transform; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3686 |             event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
 | 3687 |                              args->action, args->actionButton, args->flags, args->edgeFlags, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3688 |                              args->metaState, args->buttonState, args->classification, transform, | 
 | 3689 |                              args->xPrecision, args->yPrecision, args->xCursorPosition, | 
 | 3690 |                              args->yCursorPosition, args->downTime, args->eventTime, | 
 | 3691 |                              args->pointerCount, args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3692 |  | 
 | 3693 |             policyFlags |= POLICY_FLAG_FILTERED; | 
 | 3694 |             if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
 | 3695 |                 return; // event was consumed by the filter | 
 | 3696 |             } | 
 | 3697 |  | 
 | 3698 |             mLock.lock(); | 
 | 3699 |         } | 
 | 3700 |  | 
 | 3701 |         // Just enqueue a new motion event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3702 |         std::unique_ptr<MotionEntry> newEntry = | 
 | 3703 |                 std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, | 
 | 3704 |                                               args->source, args->displayId, policyFlags, | 
 | 3705 |                                               args->action, args->actionButton, args->flags, | 
 | 3706 |                                               args->metaState, args->buttonState, | 
 | 3707 |                                               args->classification, args->edgeFlags, | 
 | 3708 |                                               args->xPrecision, args->yPrecision, | 
 | 3709 |                                               args->xCursorPosition, args->yCursorPosition, | 
 | 3710 |                                               args->downTime, args->pointerCount, | 
 | 3711 |                                               args->pointerProperties, args->pointerCoords, 0, 0); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3712 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3713 |         needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3714 |         mLock.unlock(); | 
 | 3715 |     } // release lock | 
 | 3716 |  | 
 | 3717 |     if (needWake) { | 
 | 3718 |         mLooper->wake(); | 
 | 3719 |     } | 
 | 3720 | } | 
 | 3721 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3722 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { | 
 | 3723 | #if DEBUG_INBOUND_EVENT_DETAILS | 
 | 3724 |     ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
 | 3725 |           " sensorType=%s", | 
 | 3726 |           args->id, args->eventTime, args->deviceId, args->source, | 
 | 3727 |           NamedEnum::string(args->sensorType).c_str()); | 
 | 3728 | #endif | 
 | 3729 |  | 
 | 3730 |     bool needWake; | 
 | 3731 |     { // acquire lock | 
 | 3732 |         mLock.lock(); | 
 | 3733 |  | 
 | 3734 |         // Just enqueue a new sensor event. | 
 | 3735 |         std::unique_ptr<SensorEntry> newEntry = | 
 | 3736 |                 std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, | 
 | 3737 |                                               args->source, 0 /* policyFlags*/, args->hwTimestamp, | 
 | 3738 |                                               args->sensorType, args->accuracy, | 
 | 3739 |                                               args->accuracyChanged, args->values); | 
 | 3740 |  | 
 | 3741 |         needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
 | 3742 |         mLock.unlock(); | 
 | 3743 |     } // release lock | 
 | 3744 |  | 
 | 3745 |     if (needWake) { | 
 | 3746 |         mLooper->wake(); | 
 | 3747 |     } | 
 | 3748 | } | 
 | 3749 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3750 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 3751 |     return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3752 | } | 
 | 3753 |  | 
 | 3754 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
 | 3755 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Siarhei Vishniakou | 5d83f60 | 2017-09-12 12:40:29 -0700 | [diff] [blame] | 3756 |     ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3757 |           "switchMask=0x%08x", | 
 | 3758 |           args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3759 | #endif | 
 | 3760 |  | 
 | 3761 |     uint32_t policyFlags = args->policyFlags; | 
 | 3762 |     policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3763 |     mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3764 | } | 
 | 3765 |  | 
 | 3766 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
 | 3767 | #if DEBUG_INBOUND_EVENT_DETAILS | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3768 |     ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
 | 3769 |           args->deviceId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3770 | #endif | 
 | 3771 |  | 
 | 3772 |     bool needWake; | 
 | 3773 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3774 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3775 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3776 |         std::unique_ptr<DeviceResetEntry> newEntry = | 
 | 3777 |                 std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); | 
 | 3778 |         needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3779 |     } // release lock | 
 | 3780 |  | 
 | 3781 |     if (needWake) { | 
 | 3782 |         mLooper->wake(); | 
 | 3783 |     } | 
 | 3784 | } | 
 | 3785 |  | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3786 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { | 
 | 3787 | #if DEBUG_INBOUND_EVENT_DETAILS | 
 | 3788 |     ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, | 
 | 3789 |           args->enabled ? "true" : "false"); | 
 | 3790 | #endif | 
 | 3791 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3792 |     bool needWake; | 
 | 3793 |     { // acquire lock | 
 | 3794 |         std::scoped_lock _l(mLock); | 
 | 3795 |         auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, | 
 | 3796 |                                                                   args->enabled); | 
 | 3797 |         needWake = enqueueInboundEventLocked(std::move(entry)); | 
 | 3798 |     } // release lock | 
 | 3799 |  | 
 | 3800 |     if (needWake) { | 
 | 3801 |         mLooper->wake(); | 
 | 3802 |     } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 3803 | } | 
 | 3804 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3805 | InputEventInjectionResult InputDispatcher::injectInputEvent( | 
 | 3806 |         const InputEvent* event, int32_t injectorPid, int32_t injectorUid, | 
 | 3807 |         InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3808 | #if DEBUG_INBOUND_EVENT_DETAILS | 
 | 3809 |     ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3810 |           "syncMode=%d, timeout=%lld, policyFlags=0x%08x", | 
 | 3811 |           event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3812 | #endif | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 3813 |     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] | 3814 |  | 
 | 3815 |     policyFlags |= POLICY_FLAG_INJECTED; | 
 | 3816 |     if (hasInjectionPermission(injectorPid, injectorUid)) { | 
 | 3817 |         policyFlags |= POLICY_FLAG_TRUSTED; | 
 | 3818 |     } | 
 | 3819 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3820 |     std::queue<std::unique_ptr<EventEntry>> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3821 |     switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3822 |         case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3823 |             const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
 | 3824 |             int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3825 |             if (!validateKeyEvent(action)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3826 |                 return InputEventInjectionResult::FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3827 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3828 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3829 |             int32_t flags = incomingKey.getFlags(); | 
 | 3830 |             int32_t keyCode = incomingKey.getKeyCode(); | 
 | 3831 |             int32_t metaState = incomingKey.getMetaState(); | 
 | 3832 |             accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3833 |                                     /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3834 |             KeyEvent keyEvent; | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3835 |             keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 3836 |                                 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
 | 3837 |                                 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
 | 3838 |                                 incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3839 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3840 |             if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
 | 3841 |                 policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3842 |             } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3843 |  | 
 | 3844 |             if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
 | 3845 |                 android::base::Timer t; | 
 | 3846 |                 mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
 | 3847 |                 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3848 |                     ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
 | 3849 |                           std::to_string(t.duration().count()).c_str()); | 
 | 3850 |                 } | 
 | 3851 |             } | 
 | 3852 |  | 
 | 3853 |             mLock.lock(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3854 |             std::unique_ptr<KeyEntry> injectedEntry = | 
 | 3855 |                     std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), | 
 | 3856 |                                                VIRTUAL_KEYBOARD_ID, incomingKey.getSource(), | 
 | 3857 |                                                incomingKey.getDisplayId(), policyFlags, action, | 
 | 3858 |                                                flags, keyCode, incomingKey.getScanCode(), metaState, | 
 | 3859 |                                                incomingKey.getRepeatCount(), | 
 | 3860 |                                                incomingKey.getDownTime()); | 
 | 3861 |             injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3862 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3863 |         } | 
 | 3864 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3865 |         case AINPUT_EVENT_TYPE_MOTION: { | 
 | 3866 |             const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); | 
 | 3867 |             int32_t action = motionEvent->getAction(); | 
 | 3868 |             size_t pointerCount = motionEvent->getPointerCount(); | 
 | 3869 |             const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); | 
 | 3870 |             int32_t actionButton = motionEvent->getActionButton(); | 
 | 3871 |             int32_t displayId = motionEvent->getDisplayId(); | 
 | 3872 |             if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3873 |                 return InputEventInjectionResult::FAILED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3874 |             } | 
 | 3875 |  | 
 | 3876 |             if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
 | 3877 |                 nsecs_t eventTime = motionEvent->getEventTime(); | 
 | 3878 |                 android::base::Timer t; | 
 | 3879 |                 mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
 | 3880 |                 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 3881 |                     ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
 | 3882 |                           std::to_string(t.duration().count()).c_str()); | 
 | 3883 |                 } | 
 | 3884 |             } | 
 | 3885 |  | 
 | 3886 |             mLock.lock(); | 
 | 3887 |             const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); | 
 | 3888 |             const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3889 |             std::unique_ptr<MotionEntry> injectedEntry = | 
 | 3890 |                     std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
 | 3891 |                                                   VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
 | 3892 |                                                   motionEvent->getDisplayId(), policyFlags, action, | 
 | 3893 |                                                   actionButton, motionEvent->getFlags(), | 
 | 3894 |                                                   motionEvent->getMetaState(), | 
 | 3895 |                                                   motionEvent->getButtonState(), | 
 | 3896 |                                                   motionEvent->getClassification(), | 
 | 3897 |                                                   motionEvent->getEdgeFlags(), | 
 | 3898 |                                                   motionEvent->getXPrecision(), | 
 | 3899 |                                                   motionEvent->getYPrecision(), | 
 | 3900 |                                                   motionEvent->getRawXCursorPosition(), | 
 | 3901 |                                                   motionEvent->getRawYCursorPosition(), | 
 | 3902 |                                                   motionEvent->getDownTime(), | 
 | 3903 |                                                   uint32_t(pointerCount), pointerProperties, | 
 | 3904 |                                                   samplePointerCoords, motionEvent->getXOffset(), | 
 | 3905 |                                                   motionEvent->getYOffset()); | 
 | 3906 |             injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3907 |             for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { | 
 | 3908 |                 sampleEventTimes += 1; | 
 | 3909 |                 samplePointerCoords += pointerCount; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3910 |                 std::unique_ptr<MotionEntry> nextInjectedEntry = | 
 | 3911 |                         std::make_unique<MotionEntry>(motionEvent->getId(), *sampleEventTimes, | 
 | 3912 |                                                       VIRTUAL_KEYBOARD_ID, motionEvent->getSource(), | 
 | 3913 |                                                       motionEvent->getDisplayId(), policyFlags, | 
 | 3914 |                                                       action, actionButton, motionEvent->getFlags(), | 
 | 3915 |                                                       motionEvent->getMetaState(), | 
 | 3916 |                                                       motionEvent->getButtonState(), | 
 | 3917 |                                                       motionEvent->getClassification(), | 
 | 3918 |                                                       motionEvent->getEdgeFlags(), | 
 | 3919 |                                                       motionEvent->getXPrecision(), | 
 | 3920 |                                                       motionEvent->getYPrecision(), | 
 | 3921 |                                                       motionEvent->getRawXCursorPosition(), | 
 | 3922 |                                                       motionEvent->getRawYCursorPosition(), | 
 | 3923 |                                                       motionEvent->getDownTime(), | 
 | 3924 |                                                       uint32_t(pointerCount), pointerProperties, | 
 | 3925 |                                                       samplePointerCoords, | 
 | 3926 |                                                       motionEvent->getXOffset(), | 
 | 3927 |                                                       motionEvent->getYOffset()); | 
 | 3928 |                 injectedEntries.push(std::move(nextInjectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3929 |             } | 
 | 3930 |             break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3931 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3932 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3933 |         default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 3934 |             ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3935 |             return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 |     } | 
 | 3937 |  | 
 | 3938 |     InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3939 |     if (syncMode == InputEventInjectionSync::NONE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3940 |         injectionState->injectionIsAsync = true; | 
 | 3941 |     } | 
 | 3942 |  | 
 | 3943 |     injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3944 |     injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3945 |  | 
 | 3946 |     bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3947 |     while (!injectedEntries.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3948 |         needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 3949 |         injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3950 |     } | 
 | 3951 |  | 
 | 3952 |     mLock.unlock(); | 
 | 3953 |  | 
 | 3954 |     if (needWake) { | 
 | 3955 |         mLooper->wake(); | 
 | 3956 |     } | 
 | 3957 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3958 |     InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3959 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3960 |         std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3961 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3962 |         if (syncMode == InputEventInjectionSync::NONE) { | 
 | 3963 |             injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3964 |         } else { | 
 | 3965 |             for (;;) { | 
 | 3966 |                 injectionResult = injectionState->injectionResult; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3967 |                 if (injectionResult != InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 |                     break; | 
 | 3969 |                 } | 
 | 3970 |  | 
 | 3971 |                 nsecs_t remainingTimeout = endTime - now(); | 
 | 3972 |                 if (remainingTimeout <= 0) { | 
 | 3973 | #if DEBUG_INJECTION | 
 | 3974 |                     ALOGD("injectInputEvent - Timed out waiting for injection result " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3975 |                           "to become available."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3976 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3977 |                     injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3978 |                     break; | 
 | 3979 |                 } | 
 | 3980 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3981 |                 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3982 |             } | 
 | 3983 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3984 |             if (injectionResult == InputEventInjectionResult::SUCCEEDED && | 
 | 3985 |                 syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3986 |                 while (injectionState->pendingForegroundDispatches != 0) { | 
 | 3987 | #if DEBUG_INJECTION | 
 | 3988 |                     ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3989 |                           injectionState->pendingForegroundDispatches); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3990 | #endif | 
 | 3991 |                     nsecs_t remainingTimeout = endTime - now(); | 
 | 3992 |                     if (remainingTimeout <= 0) { | 
 | 3993 | #if DEBUG_INJECTION | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3994 |                         ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
 | 3995 |                               "dispatches to finish."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3996 | #endif | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3997 |                         injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3998 |                         break; | 
 | 3999 |                     } | 
 | 4000 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4001 |                     mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4002 |                 } | 
 | 4003 |             } | 
 | 4004 |         } | 
 | 4005 |  | 
 | 4006 |         injectionState->release(); | 
 | 4007 |     } // release lock | 
 | 4008 |  | 
 | 4009 | #if DEBUG_INJECTION | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4010 |     ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4011 |           injectionResult, injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | #endif | 
 | 4013 |  | 
 | 4014 |     return injectionResult; | 
 | 4015 | } | 
 | 4016 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4017 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4018 |     std::array<uint8_t, 32> calculatedHmac; | 
 | 4019 |     std::unique_ptr<VerifiedInputEvent> result; | 
 | 4020 |     switch (event.getType()) { | 
 | 4021 |         case AINPUT_EVENT_TYPE_KEY: { | 
 | 4022 |             const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
 | 4023 |             VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
 | 4024 |             result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4025 |             calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4026 |             break; | 
 | 4027 |         } | 
 | 4028 |         case AINPUT_EVENT_TYPE_MOTION: { | 
 | 4029 |             const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
 | 4030 |             VerifiedMotionEvent verifiedMotionEvent = | 
 | 4031 |                     verifiedMotionEventFromMotionEvent(motionEvent); | 
 | 4032 |             result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4033 |             calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4034 |             break; | 
 | 4035 |         } | 
 | 4036 |         default: { | 
 | 4037 |             ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
 | 4038 |             return nullptr; | 
 | 4039 |         } | 
 | 4040 |     } | 
 | 4041 |     if (calculatedHmac == INVALID_HMAC) { | 
 | 4042 |         return nullptr; | 
 | 4043 |     } | 
 | 4044 |     if (calculatedHmac != event.getHmac()) { | 
 | 4045 |         return nullptr; | 
 | 4046 |     } | 
 | 4047 |     return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4048 | } | 
 | 4049 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4050 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4051 |     return injectorUid == 0 || | 
 | 4052 |             mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4053 | } | 
 | 4054 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4055 | void InputDispatcher::setInjectionResult(EventEntry& entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4056 |                                          InputEventInjectionResult injectionResult) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4057 |     InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4058 |     if (injectionState) { | 
 | 4059 | #if DEBUG_INJECTION | 
 | 4060 |         ALOGD("Setting input event injection result to %d.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4061 |               "injectorPid=%d, injectorUid=%d", | 
 | 4062 |               injectionResult, injectionState->injectorPid, injectionState->injectorUid); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4063 | #endif | 
 | 4064 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4065 |         if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4066 |             // Log the outcome since the injector did not wait for the injection result. | 
 | 4067 |             switch (injectionResult) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4068 |                 case InputEventInjectionResult::SUCCEEDED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4069 |                     ALOGV("Asynchronous input event injection succeeded."); | 
 | 4070 |                     break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4071 |                 case InputEventInjectionResult::FAILED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4072 |                     ALOGW("Asynchronous input event injection failed."); | 
 | 4073 |                     break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4074 |                 case InputEventInjectionResult::PERMISSION_DENIED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4075 |                     ALOGW("Asynchronous input event injection permission denied."); | 
 | 4076 |                     break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4077 |                 case InputEventInjectionResult::TIMED_OUT: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4078 |                     ALOGW("Asynchronous input event injection timed out."); | 
 | 4079 |                     break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4080 |                 case InputEventInjectionResult::PENDING: | 
 | 4081 |                     ALOGE("Setting result to 'PENDING' for asynchronous injection"); | 
 | 4082 |                     break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4083 |             } | 
 | 4084 |         } | 
 | 4085 |  | 
 | 4086 |         injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4087 |         mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4088 |     } | 
 | 4089 | } | 
 | 4090 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4091 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { | 
 | 4092 |     InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4093 |     if (injectionState) { | 
 | 4094 |         injectionState->pendingForegroundDispatches += 1; | 
 | 4095 |     } | 
 | 4096 | } | 
 | 4097 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4098 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { | 
 | 4099 |     InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4100 |     if (injectionState) { | 
 | 4101 |         injectionState->pendingForegroundDispatches -= 1; | 
 | 4102 |  | 
 | 4103 |         if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4104 |             mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4105 |         } | 
 | 4106 |     } | 
 | 4107 | } | 
 | 4108 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4109 | const std::vector<sp<InputWindowHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4110 |         int32_t displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4111 |     static const std::vector<sp<InputWindowHandle>> EMPTY_WINDOW_HANDLES; | 
 | 4112 |     auto it = mWindowHandlesByDisplay.find(displayId); | 
 | 4113 |     return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4114 | } | 
 | 4115 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4116 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4117 |         const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4118 |     if (windowHandleToken == nullptr) { | 
 | 4119 |         return nullptr; | 
 | 4120 |     } | 
 | 4121 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4122 |     for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4123 |         const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4124 |         for (const sp<InputWindowHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4125 |             if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4126 |                 return windowHandle; | 
 | 4127 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 |         } | 
 | 4129 |     } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4130 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4131 | } | 
 | 4132 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4133 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
 | 4134 |                                                              int displayId) const { | 
 | 4135 |     if (windowHandleToken == nullptr) { | 
 | 4136 |         return nullptr; | 
 | 4137 |     } | 
 | 4138 |  | 
 | 4139 |     for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
 | 4140 |         if (windowHandle->getToken() == windowHandleToken) { | 
 | 4141 |             return windowHandle; | 
 | 4142 |         } | 
 | 4143 |     } | 
 | 4144 |     return nullptr; | 
 | 4145 | } | 
 | 4146 |  | 
 | 4147 | sp<InputWindowHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
 | 4148 |     sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
 | 4149 |     return getWindowHandleLocked(focusedToken, displayId); | 
 | 4150 | } | 
 | 4151 |  | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4152 | bool InputDispatcher::hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const { | 
 | 4153 |     for (auto& it : mWindowHandlesByDisplay) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4154 |         const std::vector<sp<InputWindowHandle>>& windowHandles = it.second; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4155 |         for (const sp<InputWindowHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4156 |             if (handle->getId() == windowHandle->getId() && | 
 | 4157 |                 handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4158 |                 if (windowHandle->getInfo()->displayId != it.first) { | 
 | 4159 |                     ALOGE("Found window %s in display %" PRId32 | 
 | 4160 |                           ", but it should belong to display %" PRId32, | 
 | 4161 |                           windowHandle->getName().c_str(), it.first, | 
 | 4162 |                           windowHandle->getInfo()->displayId); | 
 | 4163 |                 } | 
 | 4164 |                 return true; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4165 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4166 |         } | 
 | 4167 |     } | 
 | 4168 |     return false; | 
 | 4169 | } | 
 | 4170 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4171 | bool InputDispatcher::hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const { | 
 | 4172 |     sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
 | 4173 |     const bool noInputChannel = | 
 | 4174 |             windowHandle.getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
 | 4175 |     if (connection != nullptr && noInputChannel) { | 
 | 4176 |         ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
 | 4177 |               windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
 | 4178 |         return false; | 
 | 4179 |     } | 
 | 4180 |  | 
 | 4181 |     if (connection == nullptr) { | 
 | 4182 |         if (!noInputChannel) { | 
 | 4183 |             ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
 | 4184 |         } | 
 | 4185 |         return false; | 
 | 4186 |     } | 
 | 4187 |     if (!connection->responsive) { | 
 | 4188 |         ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
 | 4189 |         return false; | 
 | 4190 |     } | 
 | 4191 |     return true; | 
 | 4192 | } | 
 | 4193 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4194 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
 | 4195 |         const sp<IBinder>& token) const { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4196 |     size_t count = mInputChannelsByToken.count(token); | 
 | 4197 |     if (count == 0) { | 
 | 4198 |         return nullptr; | 
 | 4199 |     } | 
 | 4200 |     return mInputChannelsByToken.at(token); | 
 | 4201 | } | 
 | 4202 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4203 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
 | 4204 |         const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
 | 4205 |     if (inputWindowHandles.empty()) { | 
 | 4206 |         // Remove all handles on a display if there are no windows left. | 
 | 4207 |         mWindowHandlesByDisplay.erase(displayId); | 
 | 4208 |         return; | 
 | 4209 |     } | 
 | 4210 |  | 
 | 4211 |     // Since we compare the pointer of input window handles across window updates, we need | 
 | 4212 |     // to make sure the handle object for the same window stays unchanged across updates. | 
 | 4213 |     const std::vector<sp<InputWindowHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4214 |     std::unordered_map<int32_t /*id*/, sp<InputWindowHandle>> oldHandlesById; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4215 |     for (const sp<InputWindowHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4216 |         oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4217 |     } | 
 | 4218 |  | 
 | 4219 |     std::vector<sp<InputWindowHandle>> newHandles; | 
 | 4220 |     for (const sp<InputWindowHandle>& handle : inputWindowHandles) { | 
 | 4221 |         if (!handle->updateInfo()) { | 
 | 4222 |             // handle no longer valid | 
 | 4223 |             continue; | 
 | 4224 |         } | 
 | 4225 |  | 
 | 4226 |         const InputWindowInfo* info = handle->getInfo(); | 
 | 4227 |         if ((getInputChannelLocked(handle->getToken()) == nullptr && | 
 | 4228 |              info->portalToDisplayId == ADISPLAY_ID_NONE)) { | 
 | 4229 |             const bool noInputChannel = | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4230 |                     info->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
 | 4231 |             const bool canReceiveInput = !info->flags.test(InputWindowInfo::Flag::NOT_TOUCHABLE) || | 
 | 4232 |                     !info->flags.test(InputWindowInfo::Flag::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4233 |             if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4234 |                 ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4235 |                       handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4236 |                 continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4237 |             } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4238 |         } | 
 | 4239 |  | 
 | 4240 |         if (info->displayId != displayId) { | 
 | 4241 |             ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
 | 4242 |                   handle->getName().c_str(), displayId, info->displayId); | 
 | 4243 |             continue; | 
 | 4244 |         } | 
 | 4245 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4246 |         if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
 | 4247 |                 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4248 |             const sp<InputWindowHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4249 |             oldHandle->updateFrom(handle); | 
 | 4250 |             newHandles.push_back(oldHandle); | 
 | 4251 |         } else { | 
 | 4252 |             newHandles.push_back(handle); | 
 | 4253 |         } | 
 | 4254 |     } | 
 | 4255 |  | 
 | 4256 |     // Insert or replace | 
 | 4257 |     mWindowHandlesByDisplay[displayId] = newHandles; | 
 | 4258 | } | 
 | 4259 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4260 | void InputDispatcher::setInputWindows( | 
 | 4261 |         const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>& handlesPerDisplay) { | 
 | 4262 |     { // acquire lock | 
 | 4263 |         std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4264 |         for (const auto& [displayId, handles] : handlesPerDisplay) { | 
 | 4265 |             setInputWindowsLocked(handles, displayId); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4266 |         } | 
 | 4267 |     } | 
 | 4268 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4269 |     mLooper->wake(); | 
 | 4270 | } | 
 | 4271 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4272 | /** | 
 | 4273 |  * Called from InputManagerService, update window handle list by displayId that can receive input. | 
 | 4274 |  * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
 | 4275 |  * If set an empty list, remove all handles from the specific display. | 
 | 4276 |  * For focused handle, check if need to change and send a cancel event to previous one. | 
 | 4277 |  * For removed handle, check if need to send a cancel event if already in touch. | 
 | 4278 |  */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4279 | void InputDispatcher::setInputWindowsLocked( | 
 | 4280 |         const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4281 |     if (DEBUG_FOCUS) { | 
 | 4282 |         std::string windowList; | 
 | 4283 |         for (const sp<InputWindowHandle>& iwh : inputWindowHandles) { | 
 | 4284 |             windowList += iwh->getName() + " "; | 
 | 4285 |         } | 
 | 4286 |         ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
 | 4287 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4288 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4289 |     // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
 | 4290 |     for (const sp<InputWindowHandle>& window : inputWindowHandles) { | 
 | 4291 |         const bool noInputWindow = | 
 | 4292 |                 window->getInfo()->inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL); | 
 | 4293 |         if (noInputWindow && window->getToken() != nullptr) { | 
 | 4294 |             ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
 | 4295 |                   window->getName().c_str()); | 
 | 4296 |             window->releaseChannel(); | 
 | 4297 |         } | 
 | 4298 |     } | 
 | 4299 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4300 |     // Copy old handles for release if they are no longer present. | 
 | 4301 |     const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4302 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4303 |     updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4304 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4305 |     const std::vector<sp<InputWindowHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
 | 4306 |     if (mLastHoverWindowHandle && | 
 | 4307 |         std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
 | 4308 |                 windowHandles.end()) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4309 |         mLastHoverWindowHandle = nullptr; | 
 | 4310 |     } | 
 | 4311 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4312 |     sp<IBinder> focusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
 | 4313 |     if (focusedToken) { | 
 | 4314 |         FocusResult result = checkTokenFocusableLocked(focusedToken, displayId); | 
 | 4315 |         if (result != FocusResult::OK) { | 
 | 4316 |             onFocusChangedLocked(focusedToken, nullptr, displayId, typeToString(result)); | 
 | 4317 |         } | 
 | 4318 |     } | 
 | 4319 |  | 
 | 4320 |     std::optional<FocusRequest> focusRequest = | 
 | 4321 |             getOptionalValueByKey(mPendingFocusRequests, displayId); | 
 | 4322 |     if (focusRequest) { | 
 | 4323 |         // If the window from the pending request is now visible, provide it focus. | 
 | 4324 |         FocusResult result = handleFocusRequestLocked(*focusRequest); | 
 | 4325 |         if (result != FocusResult::NOT_VISIBLE) { | 
 | 4326 |             // Drop the request if we were able to change the focus or we cannot change | 
 | 4327 |             // it for another reason. | 
 | 4328 |             mPendingFocusRequests.erase(displayId); | 
 | 4329 |         } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4330 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4331 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4332 |     std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
 | 4333 |             mTouchStatesByDisplay.find(displayId); | 
 | 4334 |     if (stateIt != mTouchStatesByDisplay.end()) { | 
 | 4335 |         TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4336 |         for (size_t i = 0; i < state.windows.size();) { | 
 | 4337 |             TouchedWindow& touchedWindow = state.windows[i]; | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4338 |             if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4339 |                 if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4340 |                     ALOGD("Touched window was removed: %s in display %" PRId32, | 
 | 4341 |                           touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4342 |                 } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4343 |                 std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4344 |                         getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
 | 4345 |                 if (touchedInputChannel != nullptr) { | 
 | 4346 |                     CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
 | 4347 |                                                "touched window was removed"); | 
 | 4348 |                     synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4349 |                 } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4350 |                 state.windows.erase(state.windows.begin() + i); | 
 | 4351 |             } else { | 
 | 4352 |                 ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4353 |             } | 
 | 4354 |         } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4355 |     } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4356 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4357 |     // Release information for windows that are no longer present. | 
 | 4358 |     // This ensures that unused input channels are released promptly. | 
 | 4359 |     // Otherwise, they might stick around until the window handle is destroyed | 
 | 4360 |     // which might not happen until the next GC. | 
 | 4361 |     for (const sp<InputWindowHandle>& oldWindowHandle : oldWindowHandles) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4362 |         if (!hasWindowHandleLocked(oldWindowHandle)) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4363 |             if (DEBUG_FOCUS) { | 
 | 4364 |                 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4365 |             } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4366 |             oldWindowHandle->releaseChannel(); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4367 |             // To avoid making too many calls into the compat framework, only | 
 | 4368 |             // check for window flags when windows are going away. | 
 | 4369 |             // TODO(b/157929241) : delete this. This is only needed temporarily | 
 | 4370 |             // in order to gather some data about the flag usage | 
 | 4371 |             if (oldWindowHandle->getInfo()->flags.test(InputWindowInfo::Flag::SLIPPERY)) { | 
 | 4372 |                 ALOGW("%s has FLAG_SLIPPERY. Please report this in b/157929241", | 
 | 4373 |                       oldWindowHandle->getName().c_str()); | 
 | 4374 |                 if (mCompatService != nullptr) { | 
 | 4375 |                     mCompatService->reportChangeByUid(IInputConstants::BLOCK_FLAG_SLIPPERY, | 
 | 4376 |                                                       oldWindowHandle->getInfo()->ownerUid); | 
 | 4377 |                 } | 
 | 4378 |             } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4379 |         } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4380 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4381 | } | 
 | 4382 |  | 
 | 4383 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4384 |         int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4385 |     if (DEBUG_FOCUS) { | 
 | 4386 |         ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
 | 4387 |               inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
 | 4388 |     } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4389 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4390 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4392 |         std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4393 |                 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4394 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4395 |         if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
 | 4396 |             return; // This application is already focused. No need to wake up or change anything. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4397 |         } | 
 | 4398 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4399 |         // Set the new application handle. | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4400 |         if (inputApplicationHandle != nullptr) { | 
 | 4401 |             mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
 | 4402 |         } else { | 
 | 4403 |             mFocusedApplicationHandlesByDisplay.erase(displayId); | 
 | 4404 |         } | 
 | 4405 |  | 
 | 4406 |         // No matter what the old focused application was, stop waiting on it because it is | 
 | 4407 |         // no longer focused. | 
 | 4408 |         resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4409 |     } // release lock | 
 | 4410 |  | 
 | 4411 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4412 |     mLooper->wake(); | 
 | 4413 | } | 
 | 4414 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4415 | /** | 
 | 4416 |  * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
 | 4417 |  * the display not specified. | 
 | 4418 |  * | 
 | 4419 |  * We track any unreleased events for each window. If a window loses the ability to receive the | 
 | 4420 |  * released event, we will send a cancel event to it. So when the focused display is changed, we | 
 | 4421 |  * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
 | 4422 |  * display. The display-specified events won't be affected. | 
 | 4423 |  */ | 
 | 4424 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4425 |     if (DEBUG_FOCUS) { | 
 | 4426 |         ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
 | 4427 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4428 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4429 |         std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4430 |  | 
 | 4431 |         if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4432 |             sp<IBinder> oldFocusedWindowToken = | 
 | 4433 |                     getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
 | 4434 |             if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4435 |                 std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4436 |                         getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4437 |                 if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4438 |                     CancelationOptions | 
 | 4439 |                             options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
 | 4440 |                                     "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4441 |                     options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4442 |                     synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
 | 4443 |                 } | 
 | 4444 |             } | 
 | 4445 |             mFocusedDisplayId = displayId; | 
 | 4446 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4447 |             // Find new focused window and validate | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4448 |             sp<IBinder> newFocusedWindowToken = | 
 | 4449 |                     getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
 | 4450 |             notifyFocusChangedLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4451 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4452 |             if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4453 |                 ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4454 |                 if (!mFocusedWindowTokenByDisplay.empty()) { | 
 | 4455 |                     ALOGE("But another display has a focused window\n%s", | 
 | 4456 |                           dumpFocusedWindowsLocked().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4457 |                 } | 
 | 4458 |             } | 
 | 4459 |         } | 
 | 4460 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4461 |         if (DEBUG_FOCUS) { | 
 | 4462 |             logDispatchStateLocked(); | 
 | 4463 |         } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4464 |     } // release lock | 
 | 4465 |  | 
 | 4466 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4467 |     mLooper->wake(); | 
 | 4468 | } | 
 | 4469 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4470 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4471 |     if (DEBUG_FOCUS) { | 
 | 4472 |         ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
 | 4473 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4474 |  | 
 | 4475 |     bool changed; | 
 | 4476 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4477 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4478 |  | 
 | 4479 |         if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
 | 4480 |             if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4481 |                 resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4482 |             } | 
 | 4483 |  | 
 | 4484 |             if (mDispatchEnabled && !enabled) { | 
 | 4485 |                 resetAndDropEverythingLocked("dispatcher is being disabled"); | 
 | 4486 |             } | 
 | 4487 |  | 
 | 4488 |             mDispatchEnabled = enabled; | 
 | 4489 |             mDispatchFrozen = frozen; | 
 | 4490 |             changed = true; | 
 | 4491 |         } else { | 
 | 4492 |             changed = false; | 
 | 4493 |         } | 
 | 4494 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4495 |         if (DEBUG_FOCUS) { | 
 | 4496 |             logDispatchStateLocked(); | 
 | 4497 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4498 |     } // release lock | 
 | 4499 |  | 
 | 4500 |     if (changed) { | 
 | 4501 |         // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4502 |         mLooper->wake(); | 
 | 4503 |     } | 
 | 4504 | } | 
 | 4505 |  | 
 | 4506 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4507 |     if (DEBUG_FOCUS) { | 
 | 4508 |         ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
 | 4509 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4510 |  | 
 | 4511 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4512 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4513 |  | 
 | 4514 |         if (mInputFilterEnabled == enabled) { | 
 | 4515 |             return; | 
 | 4516 |         } | 
 | 4517 |  | 
 | 4518 |         mInputFilterEnabled = enabled; | 
 | 4519 |         resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
 | 4520 |     } // release lock | 
 | 4521 |  | 
 | 4522 |     // Wake up poll loop since there might be work to do to drop everything. | 
 | 4523 |     mLooper->wake(); | 
 | 4524 | } | 
 | 4525 |  | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 4526 | void InputDispatcher::setInTouchMode(bool inTouchMode) { | 
 | 4527 |     std::scoped_lock lock(mLock); | 
 | 4528 |     mInTouchMode = inTouchMode; | 
 | 4529 | } | 
 | 4530 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 4531 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { | 
 | 4532 |     if (opacity < 0 || opacity > 1) { | 
 | 4533 |         LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); | 
 | 4534 |         return; | 
 | 4535 |     } | 
 | 4536 |  | 
 | 4537 |     std::scoped_lock lock(mLock); | 
 | 4538 |     mMaximumObscuringOpacityForTouch = opacity; | 
 | 4539 | } | 
 | 4540 |  | 
 | 4541 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { | 
 | 4542 |     std::scoped_lock lock(mLock); | 
 | 4543 |     mBlockUntrustedTouchesMode = mode; | 
 | 4544 | } | 
 | 4545 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4546 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) { | 
 | 4547 |     if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4548 |         if (DEBUG_FOCUS) { | 
 | 4549 |             ALOGD("Trivial transfer to same window."); | 
 | 4550 |         } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4551 |         return true; | 
 | 4552 |     } | 
 | 4553 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4554 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4555 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4556 |  | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4557 |         sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken); | 
 | 4558 |         sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4559 |         if (fromWindowHandle == nullptr || toWindowHandle == nullptr) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4560 |             ALOGW("Cannot transfer focus because from or to window not found."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4561 |             return false; | 
 | 4562 |         } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4563 |         if (DEBUG_FOCUS) { | 
 | 4564 |             ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
 | 4565 |                   fromWindowHandle->getName().c_str(), toWindowHandle->getName().c_str()); | 
 | 4566 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4567 |         if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4568 |             if (DEBUG_FOCUS) { | 
 | 4569 |                 ALOGD("Cannot transfer focus because windows are on different displays."); | 
 | 4570 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4571 |             return false; | 
 | 4572 |         } | 
 | 4573 |  | 
 | 4574 |         bool found = false; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4575 |         for (std::pair<const int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
 | 4576 |             TouchState& state = pair.second; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4577 |             for (size_t i = 0; i < state.windows.size(); i++) { | 
 | 4578 |                 const TouchedWindow& touchedWindow = state.windows[i]; | 
 | 4579 |                 if (touchedWindow.windowHandle == fromWindowHandle) { | 
 | 4580 |                     int32_t oldTargetFlags = touchedWindow.targetFlags; | 
 | 4581 |                     BitSet32 pointerIds = touchedWindow.pointerIds; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4582 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4583 |                     state.windows.erase(state.windows.begin() + i); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4584 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4585 |                     int32_t newTargetFlags = oldTargetFlags & | 
 | 4586 |                             (InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_SPLIT | | 
 | 4587 |                              InputTarget::FLAG_DISPATCH_AS_IS); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4588 |                     state.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4589 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4590 |                     found = true; | 
 | 4591 |                     goto Found; | 
 | 4592 |                 } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4593 |             } | 
 | 4594 |         } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4595 |     Found: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4596 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4597 |         if (!found) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4598 |             if (DEBUG_FOCUS) { | 
 | 4599 |                 ALOGD("Focus transfer failed because from window did not have focus."); | 
 | 4600 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4601 |             return false; | 
 | 4602 |         } | 
 | 4603 |  | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 4604 |         sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
 | 4605 |         sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4606 |         if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4607 |             fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4608 |             CancelationOptions | 
 | 4609 |                     options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
 | 4610 |                             "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4611 |             synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4612 |             synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4613 |         } | 
 | 4614 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4615 |         if (DEBUG_FOCUS) { | 
 | 4616 |             logDispatchStateLocked(); | 
 | 4617 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4618 |     } // release lock | 
 | 4619 |  | 
 | 4620 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 4621 |     mLooper->wake(); | 
 | 4622 |     return true; | 
 | 4623 | } | 
 | 4624 |  | 
 | 4625 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4626 |     if (DEBUG_FOCUS) { | 
 | 4627 |         ALOGD("Resetting and dropping all events (%s).", reason); | 
 | 4628 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4629 |  | 
 | 4630 |     CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
 | 4631 |     synthesizeCancelationEventsForAllConnectionsLocked(options); | 
 | 4632 |  | 
 | 4633 |     resetKeyRepeatLocked(); | 
 | 4634 |     releasePendingEventLocked(); | 
 | 4635 |     drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4636 |     resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4637 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4638 |     mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4639 |     mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4640 |     mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4641 |     mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4642 | } | 
 | 4643 |  | 
 | 4644 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4645 |     std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4646 |     dumpDispatchStateLocked(dump); | 
 | 4647 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4648 |     std::istringstream stream(dump); | 
 | 4649 |     std::string line; | 
 | 4650 |  | 
 | 4651 |     while (std::getline(stream, line, '\n')) { | 
 | 4652 |         ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4653 |     } | 
 | 4654 | } | 
 | 4655 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4656 | std::string InputDispatcher::dumpFocusedWindowsLocked() { | 
 | 4657 |     if (mFocusedWindowTokenByDisplay.empty()) { | 
 | 4658 |         return INDENT "FocusedWindows: <none>\n"; | 
 | 4659 |     } | 
 | 4660 |  | 
 | 4661 |     std::string dump; | 
 | 4662 |     dump += INDENT "FocusedWindows:\n"; | 
 | 4663 |     for (auto& it : mFocusedWindowTokenByDisplay) { | 
 | 4664 |         const int32_t displayId = it.first; | 
 | 4665 |         const sp<InputWindowHandle> windowHandle = getFocusedWindowHandleLocked(displayId); | 
 | 4666 |         if (windowHandle) { | 
 | 4667 |             dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", name='%s'\n", displayId, | 
 | 4668 |                                  windowHandle->getName().c_str()); | 
 | 4669 |         } else { | 
 | 4670 |             dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
 | 4671 |                                          " has focused token without a window'\n", | 
 | 4672 |                                  displayId); | 
 | 4673 |         } | 
 | 4674 |     } | 
 | 4675 |     return dump; | 
 | 4676 | } | 
 | 4677 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 4678 | std::string InputDispatcher::dumpPendingFocusRequestsLocked() { | 
 | 4679 |     if (mPendingFocusRequests.empty()) { | 
 | 4680 |         return INDENT "mPendingFocusRequests: <none>\n"; | 
 | 4681 |     } | 
 | 4682 |  | 
 | 4683 |     std::string dump; | 
 | 4684 |     dump += INDENT "mPendingFocusRequests:\n"; | 
 | 4685 |     for (const auto& [displayId, focusRequest] : mPendingFocusRequests) { | 
 | 4686 |         // Rather than printing raw values for focusRequest.token and focusRequest.focusedToken, | 
 | 4687 |         // try to resolve them to actual windows. | 
 | 4688 |         std::string windowName = getConnectionNameLocked(focusRequest.token); | 
 | 4689 |         std::string focusedWindowName = getConnectionNameLocked(focusRequest.focusedToken); | 
 | 4690 |         dump += StringPrintf(INDENT2 "displayId=%" PRId32 ", token->%s, focusedToken->%s\n", | 
 | 4691 |                              displayId, windowName.c_str(), focusedWindowName.c_str()); | 
 | 4692 |     } | 
 | 4693 |     return dump; | 
 | 4694 | } | 
 | 4695 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4696 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { | 
 | 4697 |     std::string dump; | 
 | 4698 |  | 
 | 4699 |     dump += StringPrintf(INDENT "FocusedWindowRequestedPointerCapture: %s\n", | 
 | 4700 |                          toString(mFocusedWindowRequestedPointerCapture)); | 
 | 4701 |  | 
 | 4702 |     std::string windowName = "None"; | 
 | 4703 |     if (mWindowTokenWithPointerCapture) { | 
 | 4704 |         const sp<InputWindowHandle> captureWindowHandle = | 
 | 4705 |                 getWindowHandleLocked(mWindowTokenWithPointerCapture); | 
 | 4706 |         windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() | 
 | 4707 |                                          : "token has capture without window"; | 
 | 4708 |     } | 
 | 4709 |     dump += StringPrintf(INDENT "CurrentWindowWithPointerCapture: %s\n", windowName.c_str()); | 
 | 4710 |  | 
 | 4711 |     return dump; | 
 | 4712 | } | 
 | 4713 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4714 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 4715 |     dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
 | 4716 |     dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
 | 4717 |     dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4718 |     dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4720 |     if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
 | 4721 |         dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
 | 4722 |         for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
 | 4723 |             const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4724 |             const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4725 |             const std::chrono::duration timeout = | 
 | 4726 |                     applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4727 |             dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4728 |                                          ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 4729 |                                  displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4730 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4731 |     } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4732 |         dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4733 |     } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4734 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4735 |     dump += dumpFocusedWindowsLocked(); | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 4736 |     dump += dumpPendingFocusRequestsLocked(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4737 |     dump += dumpPointerCaptureStateLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4738 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4739 |     if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4740 |         dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4741 |         for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
 | 4742 |             const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4743 |             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] | 4744 |                                  state.displayId, toString(state.down), toString(state.split), | 
 | 4745 |                                  state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4746 |             if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4747 |                 dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4748 |                 for (size_t i = 0; i < state.windows.size(); i++) { | 
 | 4749 |                     const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4750 |                     dump += StringPrintf(INDENT4 | 
 | 4751 |                                          "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
 | 4752 |                                          i, touchedWindow.windowHandle->getName().c_str(), | 
 | 4753 |                                          touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4754 |                 } | 
 | 4755 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4756 |                 dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 4757 |             } | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4758 |             if (!state.portalWindows.empty()) { | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4759 |                 dump += INDENT3 "Portal windows:\n"; | 
 | 4760 |                 for (size_t i = 0; i < state.portalWindows.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4761 |                     const sp<InputWindowHandle> portalWindowHandle = state.portalWindows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4762 |                     dump += StringPrintf(INDENT4 "%zu: name='%s'\n", i, | 
 | 4763 |                                          portalWindowHandle->getName().c_str()); | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 4764 |                 } | 
 | 4765 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4766 |         } | 
 | 4767 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4768 |         dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4769 |     } | 
 | 4770 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4771 |     if (!mWindowHandlesByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4772 |         for (auto& it : mWindowHandlesByDisplay) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4773 |             const std::vector<sp<InputWindowHandle>> windowHandles = it.second; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 4774 |             dump += StringPrintf(INDENT "Display: %" PRId32 "\n", it.first); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4775 |             if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4776 |                 dump += INDENT2 "Windows:\n"; | 
 | 4777 |                 for (size_t i = 0; i < windowHandles.size(); i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4778 |                     const sp<InputWindowHandle>& windowHandle = windowHandles[i]; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4779 |                     const InputWindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4780 |  | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4781 |                     dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4782 |                                                  "portalToDisplayId=%d, paused=%s, focusable=%s, " | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4783 |                                                  "hasWallpaper=%s, visible=%s, alpha=%.2f, " | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4784 |                                                  "flags=%s, type=%s, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4785 |                                                  "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4786 |                                                  "applicationInfo=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4787 |                                                  "touchableRegion=", | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4788 |                                          i, windowInfo->name.c_str(), windowInfo->id, | 
 | 4789 |                                          windowInfo->displayId, windowInfo->portalToDisplayId, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4790 |                                          toString(windowInfo->paused), | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4791 |                                          toString(windowInfo->focusable), | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4792 |                                          toString(windowInfo->hasWallpaper), | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 4793 |                                          toString(windowInfo->visible), windowInfo->alpha, | 
| Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 4794 |                                          windowInfo->flags.string().c_str(), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4795 |                                          NamedEnum::string(windowInfo->type).c_str(), | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4796 |                                          windowInfo->frameLeft, windowInfo->frameTop, | 
 | 4797 |                                          windowInfo->frameRight, windowInfo->frameBottom, | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4798 |                                          windowInfo->globalScaleFactor, | 
 | 4799 |                                          windowInfo->applicationInfo.name.c_str()); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 4800 |                     dump += dumpRegion(windowInfo->touchableRegion); | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 4801 |                     dump += StringPrintf(", inputFeatures=%s", | 
 | 4802 |                                          windowInfo->inputFeatures.string().c_str()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4803 |                     dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4804 |                                          "ms, trustedOverlay=%s, hasToken=%s, " | 
 | 4805 |                                          "touchOcclusionMode=%s\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4806 |                                          windowInfo->ownerPid, windowInfo->ownerUid, | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 4807 |                                          millis(windowInfo->dispatchingTimeout), | 
 | 4808 |                                          toString(windowInfo->trustedOverlay), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 4809 |                                          toString(windowInfo->token != nullptr), | 
 | 4810 |                                          toString(windowInfo->touchOcclusionMode).c_str()); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 4811 |                     windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4812 |                 } | 
 | 4813 |             } else { | 
 | 4814 |                 dump += INDENT2 "Windows: <none>\n"; | 
 | 4815 |             } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4816 |         } | 
 | 4817 |     } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4818 |         dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4819 |     } | 
 | 4820 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4821 |     if (!mGlobalMonitorsByDisplay.empty() || !mGestureMonitorsByDisplay.empty()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4822 |         for (auto& it : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4823 |             const std::vector<Monitor>& monitors = it.second; | 
 | 4824 |             dump += StringPrintf(INDENT "Global monitors in display %" PRId32 ":\n", it.first); | 
 | 4825 |             dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4826 |         } | 
 | 4827 |         for (auto& it : mGestureMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4828 |             const std::vector<Monitor>& monitors = it.second; | 
 | 4829 |             dump += StringPrintf(INDENT "Gesture monitors in display %" PRId32 ":\n", it.first); | 
 | 4830 |             dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4831 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4832 |     } else { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4833 |         dump += INDENT "Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4834 |     } | 
 | 4835 |  | 
 | 4836 |     nsecs_t currentTime = now(); | 
 | 4837 |  | 
 | 4838 |     // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4839 |     if (!mRecentQueue.empty()) { | 
 | 4840 |         dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4841 |         for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4842 |             dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4843 |             dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4844 |             dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4845 |         } | 
 | 4846 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4847 |         dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4848 |     } | 
 | 4849 |  | 
 | 4850 |     // Dump event currently being dispatched. | 
 | 4851 |     if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4852 |         dump += INDENT "PendingEvent:\n"; | 
 | 4853 |         dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4854 |         dump += mPendingEvent->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4855 |         dump += StringPrintf(", age=%" PRId64 "ms\n", | 
 | 4856 |                              ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4857 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4858 |         dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4859 |     } | 
 | 4860 |  | 
 | 4861 |     // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4862 |     if (!mInboundQueue.empty()) { | 
 | 4863 |         dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4864 |         for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4865 |             dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4866 |             dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4867 |             dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4868 |         } | 
 | 4869 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4870 |         dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4871 |     } | 
 | 4872 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4873 |     if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4874 |         dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4875 |         for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
 | 4876 |             const KeyReplacement& replacement = pair.first; | 
 | 4877 |             int32_t newKeyCode = pair.second; | 
 | 4878 |             dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4879 |                                  replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4880 |         } | 
 | 4881 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4882 |         dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 4883 |     } | 
 | 4884 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4885 |     if (!mConnectionsByFd.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4886 |         dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4887 |         for (const auto& pair : mConnectionsByFd) { | 
 | 4888 |             const sp<Connection>& connection = pair.second; | 
 | 4889 |             dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4890 |                                          "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4891 |                                  pair.first, connection->getInputChannelName().c_str(), | 
 | 4892 |                                  connection->getWindowName().c_str(), connection->getStatusLabel(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4893 |                                  toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4894 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4895 |             if (!connection->outboundQueue.empty()) { | 
 | 4896 |                 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
 | 4897 |                                      connection->outboundQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4898 |                 dump += dumpQueue(connection->outboundQueue, currentTime); | 
 | 4899 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4900 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4901 |                 dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4902 |             } | 
 | 4903 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 4904 |             if (!connection->waitQueue.empty()) { | 
 | 4905 |                 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
 | 4906 |                                      connection->waitQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 4907 |                 dump += dumpQueue(connection->waitQueue, currentTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4908 |             } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4909 |                 dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4910 |             } | 
 | 4911 |         } | 
 | 4912 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4913 |         dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4914 |     } | 
 | 4915 |  | 
 | 4916 |     if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4917 |         dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
 | 4918 |                              ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4919 |     } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4920 |         dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4921 |     } | 
 | 4922 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 4923 |     dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4924 |     dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
 | 4925 |     dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
 | 4926 |                          ns2ms(mConfig.keyRepeatTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4927 | } | 
 | 4928 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4929 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
 | 4930 |     const size_t numMonitors = monitors.size(); | 
 | 4931 |     for (size_t i = 0; i < numMonitors; i++) { | 
 | 4932 |         const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4933 |         const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4934 |         dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
 | 4935 |         dump += "\n"; | 
 | 4936 |     } | 
 | 4937 | } | 
 | 4938 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4939 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel( | 
 | 4940 |         const std::string& name) { | 
 | 4941 | #if DEBUG_CHANNEL_CREATION | 
 | 4942 |     ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4943 | #endif | 
 | 4944 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4945 |     std::shared_ptr<InputChannel> serverChannel; | 
 | 4946 |     std::unique_ptr<InputChannel> clientChannel; | 
 | 4947 |     status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
 | 4948 |  | 
 | 4949 |     if (result) { | 
 | 4950 |         return base::Error(result) << "Failed to open input channel pair with name " << name; | 
 | 4951 |     } | 
 | 4952 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4953 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4954 |         std::scoped_lock _l(mLock); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4955 |         sp<Connection> connection = new Connection(serverChannel, false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4956 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4957 |         int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4958 |         mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4959 |         mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4960 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4961 |         mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
 | 4962 |     } // release lock | 
 | 4963 |  | 
 | 4964 |     // Wake the looper because some connections have changed. | 
 | 4965 |     mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4966 |     return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4967 | } | 
 | 4968 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4969 | base::Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor( | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 4970 |         int32_t displayId, bool isGestureMonitor, const std::string& name, int32_t pid) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4971 |     std::shared_ptr<InputChannel> serverChannel; | 
 | 4972 |     std::unique_ptr<InputChannel> clientChannel; | 
 | 4973 |     status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
 | 4974 |     if (result) { | 
 | 4975 |         return base::Error(result) << "Failed to open input channel pair with name " << name; | 
 | 4976 |     } | 
 | 4977 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4978 |     { // acquire lock | 
 | 4979 |         std::scoped_lock _l(mLock); | 
 | 4980 |  | 
 | 4981 |         if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4982 |             return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
 | 4983 |                                           << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4984 |         } | 
 | 4985 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4986 |         sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4987 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4988 |         const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 4989 |         mConnectionsByFd[fd] = connection; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4990 |         mInputChannelsByToken[serverChannel->getConnectionToken()] = serverChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4991 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4992 |         auto& monitorsByDisplay = | 
 | 4993 |                 isGestureMonitor ? mGestureMonitorsByDisplay : mGlobalMonitorsByDisplay; | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 4994 |         monitorsByDisplay[displayId].emplace_back(serverChannel, pid); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4995 |  | 
 | 4996 |         mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4997 |     } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4998 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4999 |     // Wake the looper because some connections have changed. | 
 | 5000 |     mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5001 |     return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5002 | } | 
 | 5003 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5004 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5005 |     { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5006 |         std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5007 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5008 |         status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5009 |         if (status) { | 
 | 5010 |             return status; | 
 | 5011 |         } | 
 | 5012 |     } // release lock | 
 | 5013 |  | 
 | 5014 |     // Wake the poll loop because removing the connection may have changed the current | 
 | 5015 |     // synchronization state. | 
 | 5016 |     mLooper->wake(); | 
 | 5017 |     return OK; | 
 | 5018 | } | 
 | 5019 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5020 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
 | 5021 |                                                    bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5022 |     sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5023 |     if (connection == nullptr) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5024 |         ALOGW("Attempted to unregister already unregistered input channel"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5025 |         return BAD_VALUE; | 
 | 5026 |     } | 
 | 5027 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5028 |     removeConnectionLocked(connection); | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5029 |     mInputChannelsByToken.erase(connectionToken); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5030 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5031 |     if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5032 |         removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5033 |     } | 
 | 5034 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5035 |     mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5036 |  | 
 | 5037 |     nsecs_t currentTime = now(); | 
 | 5038 |     abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
 | 5039 |  | 
 | 5040 |     connection->status = Connection::STATUS_ZOMBIE; | 
 | 5041 |     return OK; | 
 | 5042 | } | 
 | 5043 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5044 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
 | 5045 |     removeMonitorChannelLocked(connectionToken, mGlobalMonitorsByDisplay); | 
 | 5046 |     removeMonitorChannelLocked(connectionToken, mGestureMonitorsByDisplay); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5047 | } | 
 | 5048 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5049 | void InputDispatcher::removeMonitorChannelLocked( | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5050 |         const sp<IBinder>& connectionToken, | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5051 |         std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5052 |     for (auto it = monitorsByDisplay.begin(); it != monitorsByDisplay.end();) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5053 |         std::vector<Monitor>& monitors = it->second; | 
 | 5054 |         const size_t numMonitors = monitors.size(); | 
 | 5055 |         for (size_t i = 0; i < numMonitors; i++) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5056 |             if (monitors[i].inputChannel->getConnectionToken() == connectionToken) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5057 |                 monitors.erase(monitors.begin() + i); | 
 | 5058 |                 break; | 
 | 5059 |             } | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5060 |         } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5061 |         if (monitors.empty()) { | 
 | 5062 |             it = monitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5063 |         } else { | 
 | 5064 |             ++it; | 
 | 5065 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5066 |     } | 
 | 5067 | } | 
 | 5068 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5069 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
 | 5070 |     { // acquire lock | 
 | 5071 |         std::scoped_lock _l(mLock); | 
 | 5072 |         std::optional<int32_t> foundDisplayId = findGestureMonitorDisplayByTokenLocked(token); | 
 | 5073 |  | 
 | 5074 |         if (!foundDisplayId) { | 
 | 5075 |             ALOGW("Attempted to pilfer pointers from an un-registered monitor or invalid token"); | 
 | 5076 |             return BAD_VALUE; | 
 | 5077 |         } | 
 | 5078 |         int32_t displayId = foundDisplayId.value(); | 
 | 5079 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5080 |         std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
 | 5081 |                 mTouchStatesByDisplay.find(displayId); | 
 | 5082 |         if (stateIt == mTouchStatesByDisplay.end()) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5083 |             ALOGW("Failed to pilfer pointers: no pointers on display %" PRId32 ".", displayId); | 
 | 5084 |             return BAD_VALUE; | 
 | 5085 |         } | 
 | 5086 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5087 |         TouchState& state = stateIt->second; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5088 |         std::optional<int32_t> foundDeviceId; | 
 | 5089 |         for (const TouchedMonitor& touchedMonitor : state.gestureMonitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5090 |             if (touchedMonitor.monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5091 |                 foundDeviceId = state.deviceId; | 
 | 5092 |             } | 
 | 5093 |         } | 
 | 5094 |         if (!foundDeviceId || !state.down) { | 
 | 5095 |             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] | 5096 |                   " Ignoring."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5097 |             return BAD_VALUE; | 
 | 5098 |         } | 
 | 5099 |         int32_t deviceId = foundDeviceId.value(); | 
 | 5100 |  | 
 | 5101 |         // Send cancel events to all the input channels we're stealing from. | 
 | 5102 |         CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5103 |                                    "gesture monitor stole pointer stream"); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5104 |         options.deviceId = deviceId; | 
 | 5105 |         options.displayId = displayId; | 
 | 5106 |         for (const TouchedWindow& window : state.windows) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5107 |             std::shared_ptr<InputChannel> channel = | 
 | 5108 |                     getInputChannelLocked(window.windowHandle->getToken()); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5109 |             if (channel != nullptr) { | 
 | 5110 |                 synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
 | 5111 |             } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5112 |         } | 
 | 5113 |         // Then clear the current touch state so we stop dispatching to them as well. | 
 | 5114 |         state.filterNonMonitors(); | 
 | 5115 |     } | 
 | 5116 |     return OK; | 
 | 5117 | } | 
 | 5118 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5119 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { | 
 | 5120 |     { // acquire lock | 
 | 5121 |         std::scoped_lock _l(mLock); | 
 | 5122 |         if (DEBUG_FOCUS) { | 
 | 5123 |             const sp<InputWindowHandle> windowHandle = getWindowHandleLocked(windowToken); | 
 | 5124 |             ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", | 
 | 5125 |                   windowHandle != nullptr ? windowHandle->getName().c_str() | 
 | 5126 |                                           : "token without window"); | 
 | 5127 |         } | 
 | 5128 |  | 
 | 5129 |         const sp<IBinder> focusedToken = | 
 | 5130 |                 getValueByKey(mFocusedWindowTokenByDisplay, mFocusedDisplayId); | 
 | 5131 |         if (focusedToken != windowToken) { | 
 | 5132 |             ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", | 
 | 5133 |                   enabled ? "enable" : "disable"); | 
 | 5134 |             return; | 
 | 5135 |         } | 
 | 5136 |  | 
 | 5137 |         if (enabled == mFocusedWindowRequestedPointerCapture) { | 
 | 5138 |             ALOGW("Ignoring request to %s Pointer Capture: " | 
 | 5139 |                   "window has %s requested pointer capture.", | 
 | 5140 |                   enabled ? "enable" : "disable", enabled ? "already" : "not"); | 
 | 5141 |             return; | 
 | 5142 |         } | 
 | 5143 |  | 
 | 5144 |         mFocusedWindowRequestedPointerCapture = enabled; | 
 | 5145 |         setPointerCaptureLocked(enabled); | 
 | 5146 |     } // release lock | 
 | 5147 |  | 
 | 5148 |     // Wake the thread to process command entries. | 
 | 5149 |     mLooper->wake(); | 
 | 5150 | } | 
 | 5151 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5152 | std::optional<int32_t> InputDispatcher::findGestureMonitorDisplayByTokenLocked( | 
 | 5153 |         const sp<IBinder>& token) { | 
 | 5154 |     for (const auto& it : mGestureMonitorsByDisplay) { | 
 | 5155 |         const std::vector<Monitor>& monitors = it.second; | 
 | 5156 |         for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5157 |             if (monitor.inputChannel->getConnectionToken() == token) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5158 |                 return it.first; | 
 | 5159 |             } | 
 | 5160 |         } | 
 | 5161 |     } | 
 | 5162 |     return std::nullopt; | 
 | 5163 | } | 
 | 5164 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5165 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { | 
 | 5166 |     std::optional<int32_t> gesturePid = findMonitorPidByToken(mGestureMonitorsByDisplay, token); | 
 | 5167 |     if (gesturePid.has_value()) { | 
 | 5168 |         return gesturePid; | 
 | 5169 |     } | 
 | 5170 |     return findMonitorPidByToken(mGlobalMonitorsByDisplay, token); | 
 | 5171 | } | 
 | 5172 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5173 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5174 |     if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5175 |         return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5176 |     } | 
 | 5177 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5178 |     for (const auto& pair : mConnectionsByFd) { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5179 |         const sp<Connection>& connection = pair.second; | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5180 |         if (connection->inputChannel->getConnectionToken() == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5181 |             return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5182 |         } | 
 | 5183 |     } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5184 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5185 |     return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5186 | } | 
 | 5187 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5188 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { | 
 | 5189 |     sp<Connection> connection = getConnectionLocked(connectionToken); | 
 | 5190 |     if (connection == nullptr) { | 
 | 5191 |         return "<nullptr>"; | 
 | 5192 |     } | 
 | 5193 |     return connection->getInputChannelName(); | 
 | 5194 | } | 
 | 5195 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5196 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5197 |     mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5198 |     removeByValue(mConnectionsByFd, connection); | 
 | 5199 | } | 
 | 5200 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5201 | void InputDispatcher::onDispatchCycleFinishedLocked(nsecs_t currentTime, | 
 | 5202 |                                                     const sp<Connection>& connection, uint32_t seq, | 
 | 5203 |                                                     bool handled) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5204 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 5205 |             &InputDispatcher::doDispatchCycleFinishedLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 |     commandEntry->connection = connection; | 
 | 5207 |     commandEntry->eventTime = currentTime; | 
 | 5208 |     commandEntry->seq = seq; | 
 | 5209 |     commandEntry->handled = handled; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5210 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5211 | } | 
 | 5212 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5213 | void InputDispatcher::onDispatchCycleBrokenLocked(nsecs_t currentTime, | 
 | 5214 |                                                   const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5215 |     ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5216 |           connection->getInputChannelName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5217 |  | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5218 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 5219 |             &InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5220 |     commandEntry->connection = connection; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5221 |     postCommandLocked(std::move(commandEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5222 | } | 
 | 5223 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5224 | void InputDispatcher::notifyFocusChangedLocked(const sp<IBinder>& oldToken, | 
 | 5225 |                                                const sp<IBinder>& newToken) { | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5226 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 5227 |             &InputDispatcher::doNotifyFocusChangedLockedInterruptible); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5228 |     commandEntry->oldToken = oldToken; | 
 | 5229 |     commandEntry->newToken = newToken; | 
| Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 5230 |     postCommandLocked(std::move(commandEntry)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5231 | } | 
 | 5232 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5233 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { | 
 | 5234 |     if (connection == nullptr) { | 
 | 5235 |         LOG_ALWAYS_FATAL("Caller must check for nullness"); | 
 | 5236 |     } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5237 |     // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
 | 5238 |     // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5239 |     if (connection->waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5240 |         ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5241 |               connection->inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5242 |         return; | 
 | 5243 |     } | 
 | 5244 |     /** | 
 | 5245 |      * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
 | 5246 |      * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
 | 5247 |      * has changed. This could cause newer entries to time out before the already dispatched | 
 | 5248 |      * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
 | 5249 |      * processes the events linearly. So providing information about the oldest entry seems to be | 
 | 5250 |      * most useful. | 
 | 5251 |      */ | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5252 |     DispatchEntry* oldestEntry = *connection->waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5253 |     const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
 | 5254 |     std::string reason = | 
 | 5255 |             android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5256 |                                         connection->inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5257 |                                         ns2ms(currentWait), | 
 | 5258 |                                         oldestEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5259 |     sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5260 |     updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5261 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5262 |     processConnectionUnresponsiveLocked(*connection, std::move(reason)); | 
 | 5263 |  | 
 | 5264 |     // Stop waking up for events on this connection, it is already unresponsive | 
 | 5265 |     cancelEventsForAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5266 | } | 
 | 5267 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5268 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { | 
 | 5269 |     std::string reason = | 
 | 5270 |             StringPrintf("%s does not have a focused window", application->getName().c_str()); | 
 | 5271 |     updateLastAnrStateLocked(*application, reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5272 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5273 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 5274 |             &InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible); | 
 | 5275 |     commandEntry->inputApplicationHandle = std::move(application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5276 |     postCommandLocked(std::move(commandEntry)); | 
 | 5277 | } | 
 | 5278 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5279 | void InputDispatcher::onUntrustedTouchLocked(const std::string& obscuringPackage) { | 
 | 5280 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 5281 |             &InputDispatcher::doNotifyUntrustedTouchLockedInterruptible); | 
 | 5282 |     commandEntry->obscuringPackage = obscuringPackage; | 
 | 5283 |     postCommandLocked(std::move(commandEntry)); | 
 | 5284 | } | 
 | 5285 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5286 | void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& window, | 
 | 5287 |                                                const std::string& reason) { | 
 | 5288 |     const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
 | 5289 |     updateLastAnrStateLocked(windowLabel, reason); | 
 | 5290 | } | 
 | 5291 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5292 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, | 
 | 5293 |                                                const std::string& reason) { | 
 | 5294 |     const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5295 |     updateLastAnrStateLocked(windowLabel, reason); | 
 | 5296 | } | 
 | 5297 |  | 
 | 5298 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
 | 5299 |                                                const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5300 |     // 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] | 5301 |     time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5302 |     struct tm tm; | 
 | 5303 |     localtime_r(&t, &tm); | 
 | 5304 |     char timestr[64]; | 
 | 5305 |     strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5306 |     mLastAnrState.clear(); | 
 | 5307 |     mLastAnrState += INDENT "ANR:\n"; | 
 | 5308 |     mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5309 |     mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
 | 5310 |     mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5311 |     dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5312 | } | 
 | 5313 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5314 | void InputDispatcher::doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5315 |     mLock.unlock(); | 
 | 5316 |  | 
 | 5317 |     mPolicy->notifyConfigurationChanged(commandEntry->eventTime); | 
 | 5318 |  | 
 | 5319 |     mLock.lock(); | 
 | 5320 | } | 
 | 5321 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5322 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5323 |     sp<Connection> connection = commandEntry->connection; | 
 | 5324 |  | 
 | 5325 |     if (connection->status != Connection::STATUS_ZOMBIE) { | 
 | 5326 |         mLock.unlock(); | 
 | 5327 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5328 |         mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5329 |  | 
 | 5330 |         mLock.lock(); | 
 | 5331 |     } | 
 | 5332 | } | 
 | 5333 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5334 | void InputDispatcher::doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) { | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5335 |     sp<IBinder> oldToken = commandEntry->oldToken; | 
 | 5336 |     sp<IBinder> newToken = commandEntry->newToken; | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5337 |     mLock.unlock(); | 
| chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 5338 |     mPolicy->notifyFocusChanged(oldToken, newToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5339 |     mLock.lock(); | 
 | 5340 | } | 
 | 5341 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5342 | void InputDispatcher::doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5343 |     mLock.unlock(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5344 |  | 
 | 5345 |     mPolicy->notifyNoFocusedWindowAnr(commandEntry->inputApplicationHandle); | 
 | 5346 |  | 
 | 5347 |     mLock.lock(); | 
 | 5348 | } | 
 | 5349 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5350 | void InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5351 |     mLock.unlock(); | 
 | 5352 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5353 |     mPolicy->notifyWindowUnresponsive(commandEntry->connectionToken, commandEntry->reason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5354 |  | 
 | 5355 |     mLock.lock(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5356 | } | 
 | 5357 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5358 | void InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5359 |     mLock.unlock(); | 
 | 5360 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5361 |     mPolicy->notifyMonitorUnresponsive(commandEntry->pid, commandEntry->reason); | 
 | 5362 |  | 
 | 5363 |     mLock.lock(); | 
 | 5364 | } | 
 | 5365 |  | 
 | 5366 | void InputDispatcher::doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
 | 5367 |     mLock.unlock(); | 
 | 5368 |  | 
 | 5369 |     mPolicy->notifyWindowResponsive(commandEntry->connectionToken); | 
 | 5370 |  | 
 | 5371 |     mLock.lock(); | 
 | 5372 | } | 
 | 5373 |  | 
 | 5374 | void InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) { | 
 | 5375 |     mLock.unlock(); | 
 | 5376 |  | 
 | 5377 |     mPolicy->notifyMonitorResponsive(commandEntry->pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5378 |  | 
 | 5379 |     mLock.lock(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5380 | } | 
 | 5381 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5382 | void InputDispatcher::doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) { | 
 | 5383 |     mLock.unlock(); | 
 | 5384 |  | 
 | 5385 |     mPolicy->notifyUntrustedTouch(commandEntry->obscuringPackage); | 
 | 5386 |  | 
 | 5387 |     mLock.lock(); | 
 | 5388 | } | 
 | 5389 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5390 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( | 
 | 5391 |         CommandEntry* commandEntry) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5392 |     KeyEntry& entry = *(commandEntry->keyEntry); | 
 | 5393 |     KeyEvent event = createKeyEvent(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5394 |  | 
 | 5395 |     mLock.unlock(); | 
 | 5396 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5397 |     android::base::Timer t; | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5398 |     const sp<IBinder>& token = commandEntry->connectionToken; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5399 |     nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(token, &event, entry.policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5400 |     if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
 | 5401 |         ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5402 |               std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 5403 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5404 |  | 
 | 5405 |     mLock.lock(); | 
 | 5406 |  | 
 | 5407 |     if (delay < 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5408 |         entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5409 |     } else if (!delay) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5410 |         entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 |     } else { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5412 |         entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
 | 5413 |         entry.interceptKeyWakeupTime = now() + delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5414 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5415 | } | 
 | 5416 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5417 | void InputDispatcher::doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) { | 
 | 5418 |     mLock.unlock(); | 
 | 5419 |     mPolicy->onPointerDownOutsideFocus(commandEntry->newToken); | 
 | 5420 |     mLock.lock(); | 
 | 5421 | } | 
 | 5422 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5423 | /** | 
 | 5424 |  * Connection is responsive if it has no events in the waitQueue that are older than the | 
 | 5425 |  * current time. | 
 | 5426 |  */ | 
 | 5427 | static bool isConnectionResponsive(const Connection& connection) { | 
 | 5428 |     const nsecs_t currentTime = now(); | 
 | 5429 |     for (const DispatchEntry* entry : connection.waitQueue) { | 
 | 5430 |         if (entry->timeoutTime < currentTime) { | 
 | 5431 |             return false; | 
 | 5432 |         } | 
 | 5433 |     } | 
 | 5434 |     return true; | 
 | 5435 | } | 
 | 5436 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5437 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5438 |     sp<Connection> connection = commandEntry->connection; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5439 |     const nsecs_t finishTime = commandEntry->eventTime; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5440 |     uint32_t seq = commandEntry->seq; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5441 |     const bool handled = commandEntry->handled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5442 |  | 
 | 5443 |     // Handle post-event policy actions. | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 5444 |     std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5445 |     if (dispatchEntryIt == connection->waitQueue.end()) { | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5446 |         return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5447 |     } | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5448 |     DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5449 |     const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5450 |     if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5451 |         ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
 | 5452 |               ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5453 |     } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5454 |     reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5455 |  | 
 | 5456 |     bool restartEvent; | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5457 |     if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5458 |         KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5459 |         restartEvent = | 
 | 5460 |                 afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 5461 |     } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5462 |         MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5463 |         restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, | 
 | 5464 |                                                            handled); | 
 | 5465 |     } else { | 
 | 5466 |         restartEvent = false; | 
 | 5467 |     } | 
 | 5468 |  | 
 | 5469 |     // Dequeue the event and start the next cycle. | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 5470 |     // Because the lock might have been released, it is possible that the | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5471 |     // contents of the wait queue to have been drained, so we need to double-check | 
 | 5472 |     // a few things. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5473 |     dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
 | 5474 |     if (dispatchEntryIt != connection->waitQueue.end()) { | 
 | 5475 |         dispatchEntry = *dispatchEntryIt; | 
 | 5476 |         connection->waitQueue.erase(dispatchEntryIt); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5477 |         const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); | 
 | 5478 |         mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5479 |         if (!connection->responsive) { | 
 | 5480 |             connection->responsive = isConnectionResponsive(*connection); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5481 |             if (connection->responsive) { | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5482 |                 // The connection was unresponsive, and now it's responsive. | 
 | 5483 |                 processConnectionResponsiveLocked(*connection); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5484 |             } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5485 |         } | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5486 |         traceWaitQueueLength(connection); | 
 | 5487 |         if (restartEvent && connection->status == Connection::STATUS_NORMAL) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5488 |             connection->outboundQueue.push_front(dispatchEntry); | 
| Siarhei Vishniakou | 4e68fbf | 2019-07-31 14:00:52 -0700 | [diff] [blame] | 5489 |             traceOutboundQueueLength(connection); | 
 | 5490 |         } else { | 
 | 5491 |             releaseDispatchEntry(dispatchEntry); | 
 | 5492 |         } | 
 | 5493 |     } | 
 | 5494 |  | 
 | 5495 |     // Start the next dispatch cycle for this connection. | 
 | 5496 |     startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5497 | } | 
 | 5498 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame^] | 5499 | void InputDispatcher::sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) { | 
 | 5500 |     std::unique_ptr<CommandEntry> monitorUnresponsiveCommand = std::make_unique<CommandEntry>( | 
 | 5501 |             &InputDispatcher::doNotifyMonitorUnresponsiveLockedInterruptible); | 
 | 5502 |     monitorUnresponsiveCommand->pid = pid; | 
 | 5503 |     monitorUnresponsiveCommand->reason = std::move(reason); | 
 | 5504 |     postCommandLocked(std::move(monitorUnresponsiveCommand)); | 
 | 5505 | } | 
 | 5506 |  | 
 | 5507 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, | 
 | 5508 |                                                           std::string reason) { | 
 | 5509 |     std::unique_ptr<CommandEntry> windowUnresponsiveCommand = std::make_unique<CommandEntry>( | 
 | 5510 |             &InputDispatcher::doNotifyWindowUnresponsiveLockedInterruptible); | 
 | 5511 |     windowUnresponsiveCommand->connectionToken = std::move(connectionToken); | 
 | 5512 |     windowUnresponsiveCommand->reason = std::move(reason); | 
 | 5513 |     postCommandLocked(std::move(windowUnresponsiveCommand)); | 
 | 5514 | } | 
 | 5515 |  | 
 | 5516 | void InputDispatcher::sendMonitorResponsiveCommandLocked(int32_t pid) { | 
 | 5517 |     std::unique_ptr<CommandEntry> monitorResponsiveCommand = std::make_unique<CommandEntry>( | 
 | 5518 |             &InputDispatcher::doNotifyMonitorResponsiveLockedInterruptible); | 
 | 5519 |     monitorResponsiveCommand->pid = pid; | 
 | 5520 |     postCommandLocked(std::move(monitorResponsiveCommand)); | 
 | 5521 | } | 
 | 5522 |  | 
 | 5523 | void InputDispatcher::sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) { | 
 | 5524 |     std::unique_ptr<CommandEntry> windowResponsiveCommand = std::make_unique<CommandEntry>( | 
 | 5525 |             &InputDispatcher::doNotifyWindowResponsiveLockedInterruptible); | 
 | 5526 |     windowResponsiveCommand->connectionToken = std::move(connectionToken); | 
 | 5527 |     postCommandLocked(std::move(windowResponsiveCommand)); | 
 | 5528 | } | 
 | 5529 |  | 
 | 5530 | /** | 
 | 5531 |  * Tell the policy that a connection has become unresponsive so that it can start ANR. | 
 | 5532 |  * Check whether the connection of interest is a monitor or a window, and add the corresponding | 
 | 5533 |  * command entry to the command queue. | 
 | 5534 |  */ | 
 | 5535 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, | 
 | 5536 |                                                           std::string reason) { | 
 | 5537 |     const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
 | 5538 |     if (connection.monitor) { | 
 | 5539 |         ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
 | 5540 |               reason.c_str()); | 
 | 5541 |         std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
 | 5542 |         if (!pid.has_value()) { | 
 | 5543 |             ALOGE("Could not find unresponsive monitor for connection %s", | 
 | 5544 |                   connection.inputChannel->getName().c_str()); | 
 | 5545 |             return; | 
 | 5546 |         } | 
 | 5547 |         sendMonitorUnresponsiveCommandLocked(pid.value(), std::move(reason)); | 
 | 5548 |         return; | 
 | 5549 |     } | 
 | 5550 |     // If not a monitor, must be a window | 
 | 5551 |     ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
 | 5552 |           reason.c_str()); | 
 | 5553 |     sendWindowUnresponsiveCommandLocked(connectionToken, std::move(reason)); | 
 | 5554 | } | 
 | 5555 |  | 
 | 5556 | /** | 
 | 5557 |  * Tell the policy that a connection has become responsive so that it can stop ANR. | 
 | 5558 |  */ | 
 | 5559 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { | 
 | 5560 |     const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
 | 5561 |     if (connection.monitor) { | 
 | 5562 |         std::optional<int32_t> pid = findMonitorPidByTokenLocked(connectionToken); | 
 | 5563 |         if (!pid.has_value()) { | 
 | 5564 |             ALOGE("Could not find responsive monitor for connection %s", | 
 | 5565 |                   connection.inputChannel->getName().c_str()); | 
 | 5566 |             return; | 
 | 5567 |         } | 
 | 5568 |         sendMonitorResponsiveCommandLocked(pid.value()); | 
 | 5569 |         return; | 
 | 5570 |     } | 
 | 5571 |     // If not a monitor, must be a window | 
 | 5572 |     sendWindowResponsiveCommandLocked(connectionToken); | 
 | 5573 | } | 
 | 5574 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5575 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5576 |                                                        DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5577 |                                                        KeyEntry& keyEntry, bool handled) { | 
 | 5578 |     if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5579 |         if (!handled) { | 
 | 5580 |             // Report the key as unhandled, since the fallback was not handled. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5581 |             mReporter->reportUnhandledKey(keyEntry.id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5582 |         } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5583 |         return false; | 
 | 5584 |     } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5585 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5586 |     // Get the fallback key state. | 
 | 5587 |     // Clear it out after dispatching the UP. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5588 |     int32_t originalKeyCode = keyEntry.keyCode; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5589 |     int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5590 |     if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5591 |         connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 5592 |     } | 
 | 5593 |  | 
 | 5594 |     if (handled || !dispatchEntry->hasForegroundTarget()) { | 
 | 5595 |         // If the application handles the original key for which we previously | 
 | 5596 |         // generated a fallback or if the window is not a foreground window, | 
 | 5597 |         // then cancel the associated fallback key, if any. | 
 | 5598 |         if (fallbackKeyCode != -1) { | 
 | 5599 |             // Dispatch the unhandled key to the policy with the cancel flag. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5600 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5601 |             ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5602 |                   "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5603 |                   keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5604 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5605 |             KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5606 |             event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5607 |  | 
 | 5608 |             mLock.unlock(); | 
 | 5609 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5610 |             mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5611 |                                           keyEntry.policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5612 |  | 
 | 5613 |             mLock.lock(); | 
 | 5614 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5615 |             // Cancel the fallback key. | 
 | 5616 |             if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5617 |                 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5618 |                                            "application handled the original non-fallback key " | 
 | 5619 |                                            "or is no longer a foreground target, " | 
 | 5620 |                                            "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5621 |                 options.keyCode = fallbackKeyCode; | 
 | 5622 |                 synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5623 |             } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5624 |             connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 5625 |         } | 
 | 5626 |     } else { | 
 | 5627 |         // If the application did not handle a non-fallback key, first check | 
 | 5628 |         // that we are in a good state to perform unhandled key event processing | 
 | 5629 |         // Then ask the policy what to do with it. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5630 |         bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5631 |         if (fallbackKeyCode == -1 && !initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5632 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5633 |             ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5634 |                   "since this is not an initial down.  " | 
 | 5635 |                   "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5636 |                   originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5637 | #endif | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5638 |             return false; | 
 | 5639 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5640 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5641 |         // Dispatch the unhandled key to the policy. | 
 | 5642 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 5643 |         ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5644 |               "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5645 |               keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5646 | #endif | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5647 |         KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5648 |  | 
 | 5649 |         mLock.unlock(); | 
 | 5650 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5651 |         bool fallback = | 
 | 5652 |                 mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5653 |                                               &event, keyEntry.policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5654 |  | 
 | 5655 |         mLock.lock(); | 
 | 5656 |  | 
 | 5657 |         if (connection->status != Connection::STATUS_NORMAL) { | 
 | 5658 |             connection->inputState.removeFallbackKey(originalKeyCode); | 
 | 5659 |             return false; | 
 | 5660 |         } | 
 | 5661 |  | 
 | 5662 |         // Latch the fallback keycode for this key on an initial down. | 
 | 5663 |         // The fallback keycode cannot change at any other point in the lifecycle. | 
 | 5664 |         if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5665 |             if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5666 |                 fallbackKeyCode = event.getKeyCode(); | 
 | 5667 |             } else { | 
 | 5668 |                 fallbackKeyCode = AKEYCODE_UNKNOWN; | 
 | 5669 |             } | 
 | 5670 |             connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
 | 5671 |         } | 
 | 5672 |  | 
 | 5673 |         ALOG_ASSERT(fallbackKeyCode != -1); | 
 | 5674 |  | 
 | 5675 |         // Cancel the fallback key if the policy decides not to send it anymore. | 
 | 5676 |         // We will continue to dispatch the key to the policy but we will no | 
 | 5677 |         // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5678 |         if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
 | 5679 |             (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5680 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 5681 |             if (fallback) { | 
 | 5682 |                 ALOGD("Unhandled key event: Policy requested to send key %d" | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5683 |                       "as a fallback for %d, but on the DOWN it had requested " | 
 | 5684 |                       "to send %d instead.  Fallback canceled.", | 
 | 5685 |                       event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5686 |             } else { | 
 | 5687 |                 ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5688 |                       "but on the DOWN it had requested to send %d.  " | 
 | 5689 |                       "Fallback canceled.", | 
 | 5690 |                       originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5691 |             } | 
 | 5692 | #endif | 
 | 5693 |  | 
 | 5694 |             CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
 | 5695 |                                        "canceling fallback, policy no longer desires it"); | 
 | 5696 |             options.keyCode = fallbackKeyCode; | 
 | 5697 |             synthesizeCancelationEventsForConnectionLocked(connection, options); | 
 | 5698 |  | 
 | 5699 |             fallback = false; | 
 | 5700 |             fallbackKeyCode = AKEYCODE_UNKNOWN; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5701 |             if (keyEntry.action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5702 |                 connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5703 |             } | 
 | 5704 |         } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5705 |  | 
 | 5706 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5707 |         { | 
 | 5708 |             std::string msg; | 
 | 5709 |             const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
 | 5710 |                     connection->inputState.getFallbackKeys(); | 
 | 5711 |             for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5712 |                 msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5713 |             } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5714 |             ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5715 |                   fallbackKeys.size(), msg.c_str()); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5716 |         } | 
 | 5717 | #endif | 
 | 5718 |  | 
 | 5719 |         if (fallback) { | 
 | 5720 |             // Restart the dispatch cycle using the fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5721 |             keyEntry.eventTime = event.getEventTime(); | 
 | 5722 |             keyEntry.deviceId = event.getDeviceId(); | 
 | 5723 |             keyEntry.source = event.getSource(); | 
 | 5724 |             keyEntry.displayId = event.getDisplayId(); | 
 | 5725 |             keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
 | 5726 |             keyEntry.keyCode = fallbackKeyCode; | 
 | 5727 |             keyEntry.scanCode = event.getScanCode(); | 
 | 5728 |             keyEntry.metaState = event.getMetaState(); | 
 | 5729 |             keyEntry.repeatCount = event.getRepeatCount(); | 
 | 5730 |             keyEntry.downTime = event.getDownTime(); | 
 | 5731 |             keyEntry.syntheticRepeat = false; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5732 |  | 
 | 5733 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 5734 |             ALOGD("Unhandled key event: Dispatching fallback key.  " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5735 |                   "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5736 |                   originalKeyCode, fallbackKeyCode, keyEntry.metaState); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5737 | #endif | 
 | 5738 |             return true; // restart the event | 
 | 5739 |         } else { | 
 | 5740 | #if DEBUG_OUTBOUND_EVENT_DETAILS | 
 | 5741 |             ALOGD("Unhandled key event: No fallback key."); | 
 | 5742 | #endif | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5743 |  | 
 | 5744 |             // Report the key as unhandled, since there is no fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5745 |             mReporter->reportUnhandledKey(keyEntry.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5746 |         } | 
 | 5747 |     } | 
 | 5748 |     return false; | 
 | 5749 | } | 
 | 5750 |  | 
 | 5751 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5752 |                                                           DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5753 |                                                           MotionEntry& motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5754 |     return false; | 
 | 5755 | } | 
 | 5756 |  | 
 | 5757 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { | 
 | 5758 |     mLock.unlock(); | 
 | 5759 |  | 
 | 5760 |     mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); | 
 | 5761 |  | 
 | 5762 |     mLock.lock(); | 
 | 5763 | } | 
 | 5764 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 5765 | void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, | 
 | 5766 |                                                const Connection& connection, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5767 |     // TODO Write some statistics about how long we spend waiting. | 
 | 5768 | } | 
 | 5769 |  | 
| Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 5770 | /** | 
 | 5771 |  * Report the touch event latency to the statsd server. | 
 | 5772 |  * Input events are reported for statistics if: | 
 | 5773 |  * - This is a touchscreen event | 
 | 5774 |  * - InputFilter is not enabled | 
 | 5775 |  * - Event is not injected or synthesized | 
 | 5776 |  * | 
 | 5777 |  * Statistics should be reported before calling addValue, to prevent a fresh new sample | 
 | 5778 |  * from getting aggregated with the "old" data. | 
 | 5779 |  */ | 
 | 5780 | void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry) | 
 | 5781 |         REQUIRES(mLock) { | 
 | 5782 |     const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) && | 
 | 5783 |             !(motionEntry.isSynthesized()) && !mInputFilterEnabled; | 
 | 5784 |     if (!reportForStatistics) { | 
 | 5785 |         return; | 
 | 5786 |     } | 
 | 5787 |  | 
 | 5788 |     if (mTouchStatistics.shouldReport()) { | 
 | 5789 |         android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(), | 
 | 5790 |                                    mTouchStatistics.getMax(), mTouchStatistics.getMean(), | 
 | 5791 |                                    mTouchStatistics.getStDev(), mTouchStatistics.getCount()); | 
 | 5792 |         mTouchStatistics.reset(); | 
 | 5793 |     } | 
 | 5794 |     const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime); | 
 | 5795 |     mTouchStatistics.addValue(latencyMicros); | 
 | 5796 | } | 
 | 5797 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5798 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
 | 5799 |     if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5800 |         ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5801 |     } | 
 | 5802 | } | 
 | 5803 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5804 | void InputDispatcher::traceOutboundQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5805 |     if (ATRACE_ENABLED()) { | 
 | 5806 |         char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5807 |         snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5808 |         ATRACE_INT(counterName, connection->outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5809 |     } | 
 | 5810 | } | 
 | 5811 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 5812 | void InputDispatcher::traceWaitQueueLength(const sp<Connection>& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5813 |     if (ATRACE_ENABLED()) { | 
 | 5814 |         char counterName[40]; | 
| Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 5815 |         snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName().c_str()); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5816 |         ATRACE_INT(counterName, connection->waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5817 |     } | 
 | 5818 | } | 
 | 5819 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5820 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5821 |     std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5822 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5823 |     dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5824 |     dumpDispatchStateLocked(dump); | 
 | 5825 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5826 |     if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5827 |         dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5828 |         dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5829 |     } | 
 | 5830 | } | 
 | 5831 |  | 
 | 5832 | void InputDispatcher::monitor() { | 
 | 5833 |     // 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] | 5834 |     std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5835 |     mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5836 |     mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5837 | } | 
 | 5838 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5839 | /** | 
 | 5840 |  * Wake up the dispatcher and wait until it processes all events and commands. | 
 | 5841 |  * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
 | 5842 |  * this method can be safely called from any thread, as long as you've ensured that | 
 | 5843 |  * the work you are interested in completing has already been queued. | 
 | 5844 |  */ | 
 | 5845 | bool InputDispatcher::waitForIdle() { | 
 | 5846 |     /** | 
 | 5847 |      * Timeout should represent the longest possible time that a device might spend processing | 
 | 5848 |      * events and commands. | 
 | 5849 |      */ | 
 | 5850 |     constexpr std::chrono::duration TIMEOUT = 100ms; | 
 | 5851 |     std::unique_lock lock(mLock); | 
 | 5852 |     mLooper->wake(); | 
 | 5853 |     std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
 | 5854 |     return result == std::cv_status::no_timeout; | 
 | 5855 | } | 
 | 5856 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 5857 | /** | 
 | 5858 |  * Sets focus to the window identified by the token. This must be called | 
 | 5859 |  * after updating any input window handles. | 
 | 5860 |  * | 
 | 5861 |  * Params: | 
 | 5862 |  *  request.token - input channel token used to identify the window that should gain focus. | 
 | 5863 |  *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
 | 5864 |  *  specified token does not match the currently focused window, this request will be dropped. | 
 | 5865 |  *  If the specified focused token matches the currently focused window, the call will succeed. | 
 | 5866 |  *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
 | 5867 |  *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
 | 5868 |  *  when requesting the focus change. This determines which request gets | 
 | 5869 |  *  precedence if there is a focus change request from another source such as pointer down. | 
 | 5870 |  */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5871 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
 | 5872 |     { // acquire lock | 
 | 5873 |         std::scoped_lock _l(mLock); | 
 | 5874 |  | 
 | 5875 |         const int32_t displayId = request.displayId; | 
 | 5876 |         const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
 | 5877 |         if (request.focusedToken && oldFocusedToken != request.focusedToken) { | 
 | 5878 |             ALOGD_IF(DEBUG_FOCUS, | 
 | 5879 |                      "setFocusedWindow on display %" PRId32 | 
 | 5880 |                      " ignored, reason: focusedToken is not focused", | 
 | 5881 |                      displayId); | 
 | 5882 |             return; | 
 | 5883 |         } | 
 | 5884 |  | 
 | 5885 |         mPendingFocusRequests.erase(displayId); | 
 | 5886 |         FocusResult result = handleFocusRequestLocked(request); | 
 | 5887 |         if (result == FocusResult::NOT_VISIBLE) { | 
 | 5888 |             // The requested window is not currently visible. Wait for the window to become visible | 
 | 5889 |             // and then provide it focus. This is to handle situations where a user action triggers | 
 | 5890 |             // a new window to appear. We want to be able to queue any key events after the user | 
 | 5891 |             // action and deliver it to the newly focused window. In order for this to happen, we | 
 | 5892 |             // take focus from the currently focused window so key events can be queued. | 
 | 5893 |             ALOGD_IF(DEBUG_FOCUS, | 
 | 5894 |                      "setFocusedWindow on display %" PRId32 | 
 | 5895 |                      " pending, reason: window is not visible", | 
 | 5896 |                      displayId); | 
 | 5897 |             mPendingFocusRequests[displayId] = request; | 
 | 5898 |             onFocusChangedLocked(oldFocusedToken, nullptr, displayId, | 
 | 5899 |                                  "setFocusedWindow_AwaitingWindowVisibility"); | 
 | 5900 |         } else if (result != FocusResult::OK) { | 
 | 5901 |             ALOGW("setFocusedWindow on display %" PRId32 " ignored, reason:%s", displayId, | 
 | 5902 |                   typeToString(result)); | 
 | 5903 |         } | 
 | 5904 |     } // release lock | 
 | 5905 |     // Wake up poll loop since it may need to make new input dispatching choices. | 
 | 5906 |     mLooper->wake(); | 
 | 5907 | } | 
 | 5908 |  | 
 | 5909 | InputDispatcher::FocusResult InputDispatcher::handleFocusRequestLocked( | 
 | 5910 |         const FocusRequest& request) { | 
 | 5911 |     const int32_t displayId = request.displayId; | 
 | 5912 |     const sp<IBinder> newFocusedToken = request.token; | 
 | 5913 |     const sp<IBinder> oldFocusedToken = getValueByKey(mFocusedWindowTokenByDisplay, displayId); | 
 | 5914 |  | 
 | 5915 |     if (oldFocusedToken == request.token) { | 
 | 5916 |         ALOGD_IF(DEBUG_FOCUS, | 
 | 5917 |                  "setFocusedWindow on display %" PRId32 " ignored, reason: already focused", | 
 | 5918 |                  displayId); | 
 | 5919 |         return FocusResult::OK; | 
 | 5920 |     } | 
 | 5921 |  | 
 | 5922 |     FocusResult result = checkTokenFocusableLocked(newFocusedToken, displayId); | 
 | 5923 |     if (result != FocusResult::OK) { | 
 | 5924 |         return result; | 
 | 5925 |     } | 
 | 5926 |  | 
 | 5927 |     std::string_view reason = | 
 | 5928 |             (request.focusedToken) ? "setFocusedWindow_FocusCheck" : "setFocusedWindow"; | 
 | 5929 |     onFocusChangedLocked(oldFocusedToken, newFocusedToken, displayId, reason); | 
 | 5930 |     return FocusResult::OK; | 
 | 5931 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5932 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5933 | void InputDispatcher::onFocusChangedLocked(const sp<IBinder>& oldFocusedToken, | 
 | 5934 |                                            const sp<IBinder>& newFocusedToken, int32_t displayId, | 
 | 5935 |                                            std::string_view reason) { | 
 | 5936 |     if (oldFocusedToken) { | 
 | 5937 |         std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(oldFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5938 |         if (focusedInputChannel) { | 
 | 5939 |             CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
 | 5940 |                                        "focus left window"); | 
 | 5941 |             synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5942 |             enqueueFocusEventLocked(oldFocusedToken, false /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5943 |         } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5944 |         mFocusedWindowTokenByDisplay.erase(displayId); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5945 |     } | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5946 |     if (newFocusedToken) { | 
 | 5947 |         mFocusedWindowTokenByDisplay[displayId] = newFocusedToken; | 
 | 5948 |         enqueueFocusEventLocked(newFocusedToken, true /*hasFocus*/, reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5949 |     } | 
 | 5950 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5951 |     // If a window has pointer capture, then it must have focus. We need to ensure that this | 
 | 5952 |     // contract is upheld when pointer capture is being disabled due to a loss of window focus. | 
 | 5953 |     // If the window loses focus before it loses pointer capture, then the window can be in a state | 
 | 5954 |     // where it has pointer capture but not focus, violating the contract. Therefore we must | 
 | 5955 |     // dispatch the pointer capture event before the focus event. Since focus events are added to | 
 | 5956 |     // the front of the queue (above), we add the pointer capture event to the front of the queue | 
 | 5957 |     // after the focus events are added. This ensures the pointer capture event ends up at the | 
 | 5958 |     // front. | 
 | 5959 |     disablePointerCaptureForcedLocked(); | 
 | 5960 |  | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5961 |     if (mFocusedDisplayId == displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 5962 |         notifyFocusChangedLocked(oldFocusedToken, newFocusedToken); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 5963 |     } | 
 | 5964 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5965 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5966 | void InputDispatcher::disablePointerCaptureForcedLocked() { | 
 | 5967 |     if (!mFocusedWindowRequestedPointerCapture && !mWindowTokenWithPointerCapture) { | 
 | 5968 |         return; | 
 | 5969 |     } | 
 | 5970 |  | 
 | 5971 |     ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); | 
 | 5972 |  | 
 | 5973 |     if (mFocusedWindowRequestedPointerCapture) { | 
 | 5974 |         mFocusedWindowRequestedPointerCapture = false; | 
 | 5975 |         setPointerCaptureLocked(false); | 
 | 5976 |     } | 
 | 5977 |  | 
 | 5978 |     if (!mWindowTokenWithPointerCapture) { | 
 | 5979 |         // No need to send capture changes because no window has capture. | 
 | 5980 |         return; | 
 | 5981 |     } | 
 | 5982 |  | 
 | 5983 |     if (mPendingEvent != nullptr) { | 
 | 5984 |         // Move the pending event to the front of the queue. This will give the chance | 
 | 5985 |         // for the pending event to be dropped if it is a captured event. | 
 | 5986 |         mInboundQueue.push_front(mPendingEvent); | 
 | 5987 |         mPendingEvent = nullptr; | 
 | 5988 |     } | 
 | 5989 |  | 
 | 5990 |     auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), | 
 | 5991 |                                                               false /* hasCapture */); | 
 | 5992 |     mInboundQueue.push_front(std::move(entry)); | 
 | 5993 | } | 
 | 5994 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5995 | /** | 
 | 5996 |  * Checks if the window token can be focused on a display. The token can be focused if there is | 
 | 5997 |  * at least one window handle that is visible with the same token and all window handles with the | 
 | 5998 |  * same token are focusable. | 
 | 5999 |  * | 
 | 6000 |  * In the case of mirroring, two windows may share the same window token and their visibility | 
 | 6001 |  * might be different. Example, the mirrored window can cover the window its mirroring. However, | 
 | 6002 |  * we expect the focusability of the windows to match since its hard to reason why one window can | 
 | 6003 |  * receive focus events and the other cannot when both are backed by the same input channel. | 
 | 6004 |  */ | 
 | 6005 | InputDispatcher::FocusResult InputDispatcher::checkTokenFocusableLocked(const sp<IBinder>& token, | 
 | 6006 |                                                                         int32_t displayId) const { | 
 | 6007 |     bool allWindowsAreFocusable = true; | 
 | 6008 |     bool visibleWindowFound = false; | 
 | 6009 |     bool windowFound = false; | 
 | 6010 |     for (const sp<InputWindowHandle>& window : getWindowHandlesLocked(displayId)) { | 
 | 6011 |         if (window->getToken() != token) { | 
 | 6012 |             continue; | 
 | 6013 |         } | 
 | 6014 |         windowFound = true; | 
 | 6015 |         if (window->getInfo()->visible) { | 
 | 6016 |             // Check if at least a single window is visible. | 
 | 6017 |             visibleWindowFound = true; | 
 | 6018 |         } | 
 | 6019 |         if (!window->getInfo()->focusable) { | 
 | 6020 |             // Check if all windows with the window token are focusable. | 
 | 6021 |             allWindowsAreFocusable = false; | 
 | 6022 |             break; | 
 | 6023 |         } | 
 | 6024 |     } | 
 | 6025 |  | 
 | 6026 |     if (!windowFound) { | 
 | 6027 |         return FocusResult::NO_WINDOW; | 
 | 6028 |     } | 
 | 6029 |     if (!allWindowsAreFocusable) { | 
 | 6030 |         return FocusResult::NOT_FOCUSABLE; | 
 | 6031 |     } | 
 | 6032 |     if (!visibleWindowFound) { | 
 | 6033 |         return FocusResult::NOT_VISIBLE; | 
 | 6034 |     } | 
 | 6035 |  | 
 | 6036 |     return FocusResult::OK; | 
 | 6037 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6038 |  | 
 | 6039 | void InputDispatcher::setPointerCaptureLocked(bool enabled) { | 
 | 6040 |     std::unique_ptr<CommandEntry> commandEntry = std::make_unique<CommandEntry>( | 
 | 6041 |             &InputDispatcher::doSetPointerCaptureLockedInterruptible); | 
 | 6042 |     commandEntry->enabled = enabled; | 
 | 6043 |     postCommandLocked(std::move(commandEntry)); | 
 | 6044 | } | 
 | 6045 |  | 
 | 6046 | void InputDispatcher::doSetPointerCaptureLockedInterruptible( | 
 | 6047 |         android::inputdispatcher::CommandEntry* commandEntry) { | 
 | 6048 |     mLock.unlock(); | 
 | 6049 |  | 
 | 6050 |     mPolicy->setPointerCapture(commandEntry->enabled); | 
 | 6051 |  | 
 | 6052 |     mLock.lock(); | 
 | 6053 | } | 
 | 6054 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6055 | } // namespace android::inputdispatcher |