| 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 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 22 | #include <android-base/chrono_utils.h> | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 23 | #include <android-base/properties.h> | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 25 | #include <android/os/IInputConstants.h> | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 26 | #include <binder/Binder.h> | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 27 | #include <ftl/enum.h> | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 28 | #include <gui/SurfaceComposerClient.h> | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 29 | #include <input/InputDevice.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 30 | #include <powermanager/PowerManager.h> | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 31 | #include <unistd.h> | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 |  | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 34 | #include <cerrno> | 
|  | 35 | #include <cinttypes> | 
|  | 36 | #include <climits> | 
|  | 37 | #include <cstddef> | 
|  | 38 | #include <ctime> | 
|  | 39 | #include <queue> | 
|  | 40 | #include <sstream> | 
|  | 41 |  | 
|  | 42 | #include "Connection.h" | 
| Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 43 | #include "DebugConfig.h" | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 44 | #include "InputDispatcher.h" | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 45 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | #define INDENT "  " | 
|  | 47 | #define INDENT2 "    " | 
|  | 48 | #define INDENT3 "      " | 
|  | 49 | #define INDENT4 "        " | 
|  | 50 |  | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 51 | using android::base::HwTimeoutMultiplier; | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 52 | using android::base::Result; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 53 | using android::base::StringPrintf; | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 54 | using android::gui::DisplayInfo; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 55 | using android::gui::FocusRequest; | 
|  | 56 | using android::gui::TouchOcclusionMode; | 
|  | 57 | using android::gui::WindowInfo; | 
|  | 58 | using android::gui::WindowInfoHandle; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 59 | using android::os::BlockUntrustedTouchesMode; | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 60 | using android::os::IInputConstants; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 61 | using android::os::InputEventInjectionResult; | 
|  | 62 | using android::os::InputEventInjectionSync; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 63 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 64 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 65 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 66 | namespace { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 67 | // Temporarily releases a held mutex for the lifetime of the instance. | 
|  | 68 | // Named to match std::scoped_lock | 
|  | 69 | class scoped_unlock { | 
|  | 70 | public: | 
|  | 71 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } | 
|  | 72 | ~scoped_unlock() { mMutex.lock(); } | 
|  | 73 |  | 
|  | 74 | private: | 
|  | 75 | std::mutex& mMutex; | 
|  | 76 | }; | 
|  | 77 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 78 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 79 | // from which to determine an appropriate dispatching timeout. | 
| Peter Collingbourne | b04b9b8 | 2021-02-08 12:09:47 -0800 | [diff] [blame] | 80 | const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds( | 
|  | 81 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * | 
|  | 82 | HwTimeoutMultiplier()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | // Amount of time to allow for all pending events to be processed when an app switch | 
|  | 85 | // key is on the way.  This is used to preempt input dispatch and drop input events | 
|  | 86 | // when an application takes too long to respond and the user has pressed an app switch key. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 87 | constexpr nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 89 | const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 90 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | // 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] | 92 | constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec | 
|  | 93 |  | 
|  | 94 | // Log a warning when an interception call takes longer than this to process. | 
|  | 95 | constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 97 | // Additional key latency in case a connection is still processing some motion events. | 
|  | 98 | // This will help with the case when a user touched a button that opens a new window, | 
|  | 99 | // and gives us the chance to dispatch the key to this new window. | 
|  | 100 | constexpr std::chrono::nanoseconds KEY_WAITING_FOR_EVENTS_TIMEOUT = 500ms; | 
|  | 101 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 102 | // Number of recent events to keep for debugging purposes. | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 103 | constexpr size_t RECENT_QUEUE_MAX_SIZE = 10; | 
|  | 104 |  | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 105 | // Event log tags. See EventLogTags.logtags for reference. | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 106 | constexpr int LOGTAG_INPUT_INTERACTION = 62000; | 
|  | 107 | constexpr int LOGTAG_INPUT_FOCUS = 62001; | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 108 | constexpr int LOGTAG_INPUT_CANCEL = 62003; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 109 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 110 | inline nsecs_t now() { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 111 | return systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 112 | } | 
|  | 113 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 114 | inline const char* toString(bool value) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | return value ? "true" : "false"; | 
|  | 116 | } | 
|  | 117 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 118 | inline const std::string toString(const sp<IBinder>& binder) { | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 119 | if (binder == nullptr) { | 
|  | 120 | return "<null>"; | 
|  | 121 | } | 
|  | 122 | return StringPrintf("%p", binder.get()); | 
|  | 123 | } | 
|  | 124 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 125 | inline int32_t getMotionEventActionPointerIndex(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 126 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> | 
|  | 127 | AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 130 | bool isValidKeyAction(int32_t action) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | switch (action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 132 | case AKEY_EVENT_ACTION_DOWN: | 
|  | 133 | case AKEY_EVENT_ACTION_UP: | 
|  | 134 | return true; | 
|  | 135 | default: | 
|  | 136 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 140 | bool validateKeyEvent(int32_t action) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 141 | if (!isValidKeyAction(action)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 142 | ALOGE("Key event has invalid action code 0x%x", action); | 
|  | 143 | return false; | 
|  | 144 | } | 
|  | 145 | return true; | 
|  | 146 | } | 
|  | 147 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 148 | bool isValidMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 149 | switch (action & AMOTION_EVENT_ACTION_MASK) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 150 | case AMOTION_EVENT_ACTION_DOWN: | 
|  | 151 | case AMOTION_EVENT_ACTION_UP: | 
|  | 152 | case AMOTION_EVENT_ACTION_CANCEL: | 
|  | 153 | case AMOTION_EVENT_ACTION_MOVE: | 
|  | 154 | case AMOTION_EVENT_ACTION_OUTSIDE: | 
|  | 155 | case AMOTION_EVENT_ACTION_HOVER_ENTER: | 
|  | 156 | case AMOTION_EVENT_ACTION_HOVER_MOVE: | 
|  | 157 | case AMOTION_EVENT_ACTION_HOVER_EXIT: | 
|  | 158 | case AMOTION_EVENT_ACTION_SCROLL: | 
|  | 159 | return true; | 
|  | 160 | case AMOTION_EVENT_ACTION_POINTER_DOWN: | 
|  | 161 | case AMOTION_EVENT_ACTION_POINTER_UP: { | 
|  | 162 | int32_t index = getMotionEventActionPointerIndex(action); | 
|  | 163 | return index >= 0 && index < pointerCount; | 
|  | 164 | } | 
|  | 165 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: | 
|  | 166 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: | 
|  | 167 | return actionButton != 0; | 
|  | 168 | default: | 
|  | 169 | return false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 173 | int64_t millis(std::chrono::nanoseconds t) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 174 | return std::chrono::duration_cast<std::chrono::milliseconds>(t).count(); | 
|  | 175 | } | 
|  | 176 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 177 | bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount, | 
|  | 178 | const PointerProperties* pointerProperties) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 179 | if (!isValidMotionAction(action, actionButton, pointerCount)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | ALOGE("Motion event has invalid action code 0x%x", action); | 
|  | 181 | return false; | 
|  | 182 | } | 
|  | 183 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { | 
| Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 184 | ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %zu.", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 185 | pointerCount, MAX_POINTERS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | return false; | 
|  | 187 | } | 
|  | 188 | BitSet32 pointerIdBits; | 
|  | 189 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 190 | int32_t id = pointerProperties[i].id; | 
|  | 191 | if (id < 0 || id > MAX_POINTER_ID) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 192 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", id, | 
|  | 193 | MAX_POINTER_ID); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 194 | return false; | 
|  | 195 | } | 
|  | 196 | if (pointerIdBits.hasBit(id)) { | 
|  | 197 | ALOGE("Motion event has duplicate pointer id %d", id); | 
|  | 198 | return false; | 
|  | 199 | } | 
|  | 200 | pointerIdBits.markBit(id); | 
|  | 201 | } | 
|  | 202 | return true; | 
|  | 203 | } | 
|  | 204 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 205 | std::string dumpRegion(const Region& region) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | if (region.isEmpty()) { | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 207 | return "<empty>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 210 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | bool first = true; | 
|  | 212 | Region::const_iterator cur = region.begin(); | 
|  | 213 | Region::const_iterator const tail = region.end(); | 
|  | 214 | while (cur != tail) { | 
|  | 215 | if (first) { | 
|  | 216 | first = false; | 
|  | 217 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 218 | dump += "|"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | } | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 220 | 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] | 221 | cur++; | 
|  | 222 | } | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 223 | return dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 226 | std::string dumpQueue(const std::deque<DispatchEntry*>& queue, nsecs_t currentTime) { | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 227 | constexpr size_t maxEntries = 50; // max events to print | 
|  | 228 | constexpr size_t skipBegin = maxEntries / 2; | 
|  | 229 | const size_t skipEnd = queue.size() - maxEntries / 2; | 
|  | 230 | // skip from maxEntries / 2 ... size() - maxEntries/2 | 
|  | 231 | // only print from 0 .. skipBegin and then from skipEnd .. size() | 
|  | 232 |  | 
|  | 233 | std::string dump; | 
|  | 234 | for (size_t i = 0; i < queue.size(); i++) { | 
|  | 235 | const DispatchEntry& entry = *queue[i]; | 
|  | 236 | if (i >= skipBegin && i < skipEnd) { | 
|  | 237 | dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin); | 
|  | 238 | i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue' | 
|  | 239 | continue; | 
|  | 240 | } | 
|  | 241 | dump.append(INDENT4); | 
|  | 242 | dump += entry.eventEntry->getDescription(); | 
|  | 243 | dump += StringPrintf(", seq=%" PRIu32 | 
|  | 244 | ", targetFlags=0x%08x, resolvedAction=%d, age=%" PRId64 "ms", | 
|  | 245 | entry.seq, entry.targetFlags, entry.resolvedAction, | 
|  | 246 | ns2ms(currentTime - entry.eventEntry->eventTime)); | 
|  | 247 | if (entry.deliveryTime != 0) { | 
|  | 248 | // This entry was delivered, so add information on how long we've been waiting | 
|  | 249 | dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime)); | 
|  | 250 | } | 
|  | 251 | dump.append("\n"); | 
|  | 252 | } | 
|  | 253 | return dump; | 
|  | 254 | } | 
|  | 255 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 256 | /** | 
|  | 257 | * Find the entry in std::unordered_map by key, and return it. | 
|  | 258 | * If the entry is not found, return a default constructed entry. | 
|  | 259 | * | 
|  | 260 | * Useful when the entries are vectors, since an empty vector will be returned | 
|  | 261 | * if the entry is not found. | 
|  | 262 | * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned. | 
|  | 263 | */ | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 264 | template <typename K, typename V> | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 265 | V getValueByKey(const std::unordered_map<K, V>& map, K key) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 266 | auto it = map.find(key); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 267 | return it != map.end() ? it->second : V{}; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 270 | bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 271 | if (first == second) { | 
|  | 272 | return true; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | if (first == nullptr || second == nullptr) { | 
|  | 276 | return false; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | return first->getToken() == second->getToken(); | 
|  | 280 | } | 
|  | 281 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 282 | bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) { | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 283 | if (first == nullptr || second == nullptr) { | 
|  | 284 | return false; | 
|  | 285 | } | 
|  | 286 | return first->applicationInfo.token != nullptr && | 
|  | 287 | first->applicationInfo.token == second->applicationInfo.token; | 
|  | 288 | } | 
|  | 289 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 290 | std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget, | 
|  | 291 | std::shared_ptr<EventEntry> eventEntry, | 
|  | 292 | int32_t inputTargetFlags) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 293 | if (inputTarget.useDefaultPointerTransform()) { | 
|  | 294 | const ui::Transform& transform = inputTarget.getDefaultPointerTransform(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 295 | return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 296 | inputTarget.displayTransform, | 
|  | 297 | inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
|  | 300 | ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); | 
|  | 301 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
|  | 302 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 303 | std::vector<PointerCoords> pointerCoords; | 
|  | 304 | pointerCoords.resize(motionEntry.pointerCount); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 305 |  | 
|  | 306 | // Use the first pointer information to normalize all other pointers. This could be any pointer | 
|  | 307 | // 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] | 308 | // uses the transform for the normalized pointer. | 
|  | 309 | const ui::Transform& firstPointerTransform = | 
|  | 310 | inputTarget.pointerTransforms[inputTarget.pointerIds.firstMarkedBit()]; | 
|  | 311 | ui::Transform inverseFirstTransform = firstPointerTransform.inverse(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 312 |  | 
|  | 313 | // Iterate through all pointers in the event to normalize against the first. | 
|  | 314 | for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.pointerCount; pointerIndex++) { | 
|  | 315 | const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex]; | 
|  | 316 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 317 | const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId]; | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 318 |  | 
|  | 319 | pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 320 | // First, apply the current pointer's transform to update the coordinates into | 
|  | 321 | // window space. | 
|  | 322 | pointerCoords[pointerIndex].transform(currTransform); | 
|  | 323 | // Next, apply the inverse transform of the normalized coordinates so the | 
|  | 324 | // current coordinates are transformed into the normalized coordinate space. | 
|  | 325 | pointerCoords[pointerIndex].transform(inverseFirstTransform); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 326 | } | 
|  | 327 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 328 | std::unique_ptr<MotionEntry> combinedMotionEntry = | 
|  | 329 | std::make_unique<MotionEntry>(motionEntry.id, motionEntry.eventTime, | 
|  | 330 | motionEntry.deviceId, motionEntry.source, | 
|  | 331 | motionEntry.displayId, motionEntry.policyFlags, | 
|  | 332 | motionEntry.action, motionEntry.actionButton, | 
|  | 333 | motionEntry.flags, motionEntry.metaState, | 
|  | 334 | motionEntry.buttonState, motionEntry.classification, | 
|  | 335 | motionEntry.edgeFlags, motionEntry.xPrecision, | 
|  | 336 | motionEntry.yPrecision, motionEntry.xCursorPosition, | 
|  | 337 | motionEntry.yCursorPosition, motionEntry.downTime, | 
|  | 338 | motionEntry.pointerCount, motionEntry.pointerProperties, | 
| Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 339 | pointerCoords.data()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 340 |  | 
|  | 341 | if (motionEntry.injectionState) { | 
|  | 342 | combinedMotionEntry->injectionState = motionEntry.injectionState; | 
|  | 343 | combinedMotionEntry->injectionState->refCount += 1; | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 347 | std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 348 | firstPointerTransform, inputTarget.displayTransform, | 
|  | 349 | inputTarget.globalScaleFactor); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 350 | return dispatchEntry; | 
|  | 351 | } | 
|  | 352 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 353 | status_t openInputChannelPair(const std::string& name, std::shared_ptr<InputChannel>& serverChannel, | 
|  | 354 | std::unique_ptr<InputChannel>& clientChannel) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 355 | std::unique_ptr<InputChannel> uniqueServerChannel; | 
|  | 356 | status_t result = InputChannel::openInputChannelPair(name, uniqueServerChannel, clientChannel); | 
|  | 357 |  | 
|  | 358 | serverChannel = std::move(uniqueServerChannel); | 
|  | 359 | return result; | 
|  | 360 | } | 
|  | 361 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 362 | template <typename T> | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 363 | bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) { | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 364 | if (lhs == nullptr && rhs == nullptr) { | 
|  | 365 | return true; | 
|  | 366 | } | 
|  | 367 | if (lhs == nullptr || rhs == nullptr) { | 
|  | 368 | return false; | 
|  | 369 | } | 
|  | 370 | return *lhs == *rhs; | 
|  | 371 | } | 
|  | 372 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 373 | KeyEvent createKeyEvent(const KeyEntry& entry) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 374 | KeyEvent event; | 
|  | 375 | event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC, | 
|  | 376 | entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState, | 
|  | 377 | entry.repeatCount, entry.downTime, entry.eventTime); | 
|  | 378 | return event; | 
|  | 379 | } | 
|  | 380 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 381 | bool shouldReportMetricsForConnection(const Connection& connection) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 382 | // Do not keep track of gesture monitors. They receive every event and would disproportionately | 
|  | 383 | // affect the statistics. | 
|  | 384 | if (connection.monitor) { | 
|  | 385 | return false; | 
|  | 386 | } | 
|  | 387 | // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics | 
|  | 388 | if (!connection.responsive) { | 
|  | 389 | return false; | 
|  | 390 | } | 
|  | 391 | return true; | 
|  | 392 | } | 
|  | 393 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 394 | bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 395 | const EventEntry& eventEntry = *dispatchEntry.eventEntry; | 
|  | 396 | const int32_t& inputEventId = eventEntry.id; | 
|  | 397 | if (inputEventId != dispatchEntry.resolvedEventId) { | 
|  | 398 | // Event was transmuted | 
|  | 399 | return false; | 
|  | 400 | } | 
|  | 401 | if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) { | 
|  | 402 | return false; | 
|  | 403 | } | 
|  | 404 | // Only track latency for events that originated from hardware | 
|  | 405 | if (eventEntry.isSynthesized()) { | 
|  | 406 | return false; | 
|  | 407 | } | 
|  | 408 | const EventEntry::Type& inputEventEntryType = eventEntry.type; | 
|  | 409 | if (inputEventEntryType == EventEntry::Type::KEY) { | 
|  | 410 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 411 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
|  | 412 | return false; | 
|  | 413 | } | 
|  | 414 | } else if (inputEventEntryType == EventEntry::Type::MOTION) { | 
|  | 415 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 416 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL || | 
|  | 417 | motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 418 | return false; | 
|  | 419 | } | 
|  | 420 | } else { | 
|  | 421 | // Not a key or a motion | 
|  | 422 | return false; | 
|  | 423 | } | 
|  | 424 | if (!shouldReportMetricsForConnection(connection)) { | 
|  | 425 | return false; | 
|  | 426 | } | 
|  | 427 | return true; | 
|  | 428 | } | 
|  | 429 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 430 | /** | 
|  | 431 | * Connection is responsive if it has no events in the waitQueue that are older than the | 
|  | 432 | * current time. | 
|  | 433 | */ | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 434 | bool isConnectionResponsive(const Connection& connection) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 435 | const nsecs_t currentTime = now(); | 
|  | 436 | for (const DispatchEntry* entry : connection.waitQueue) { | 
|  | 437 | if (entry->timeoutTime < currentTime) { | 
|  | 438 | return false; | 
|  | 439 | } | 
|  | 440 | } | 
|  | 441 | return true; | 
|  | 442 | } | 
|  | 443 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 444 | // Returns true if the event type passed as argument represents a user activity. | 
|  | 445 | bool isUserActivityEvent(const EventEntry& eventEntry) { | 
|  | 446 | switch (eventEntry.type) { | 
|  | 447 | case EventEntry::Type::FOCUS: | 
|  | 448 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 449 | case EventEntry::Type::DRAG: | 
|  | 450 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
|  | 451 | case EventEntry::Type::SENSOR: | 
|  | 452 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 453 | return false; | 
|  | 454 | case EventEntry::Type::DEVICE_RESET: | 
|  | 455 | case EventEntry::Type::KEY: | 
|  | 456 | case EventEntry::Type::MOTION: | 
|  | 457 | return true; | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 461 | // Returns true if the given window can accept pointer events at the given display location. | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 462 | bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, int32_t x, int32_t y, | 
|  | 463 | bool isStylus) { | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 464 | const auto inputConfig = windowInfo.inputConfig; | 
|  | 465 | if (windowInfo.displayId != displayId || | 
|  | 466 | inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 467 | return false; | 
|  | 468 | } | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 469 | const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 470 | if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 471 | return false; | 
|  | 472 | } | 
| Prabir Pradhan | 0634904 | 2022-02-04 09:19:17 -0800 | [diff] [blame] | 473 | if (!windowInfo.touchableRegionContainsPoint(x, y)) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 474 | return false; | 
|  | 475 | } | 
|  | 476 | return true; | 
|  | 477 | } | 
|  | 478 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 479 | bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) { | 
|  | 480 | return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) && | 
|  | 481 | (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || | 
|  | 482 | entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER); | 
|  | 483 | } | 
|  | 484 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 485 | // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND. | 
|  | 486 | // Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to | 
|  | 487 | // such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can | 
|  | 488 | // be sent to such a window, but it is not a foreground event and doesn't use | 
|  | 489 | // InputTarget::FLAG_FOREGROUND. | 
|  | 490 | bool canReceiveForegroundTouches(const WindowInfo& info) { | 
|  | 491 | // A non-touchable window can still receive touch events (e.g. in the case of | 
|  | 492 | // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches. | 
|  | 493 | return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); | 
|  | 494 | } | 
|  | 495 |  | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 496 | bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { | 
|  | 497 | if (windowHandle == nullptr) { | 
|  | 498 | return false; | 
|  | 499 | } | 
|  | 500 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 501 | if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) { | 
|  | 502 | return true; | 
|  | 503 | } | 
|  | 504 | return false; | 
|  | 505 | } | 
|  | 506 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 507 | // Checks targeted injection using the window's owner's uid. | 
|  | 508 | // Returns an empty string if an entry can be sent to the given window, or an error message if the | 
|  | 509 | // entry is a targeted injection whose uid target doesn't match the window owner. | 
|  | 510 | std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window, | 
|  | 511 | const EventEntry& entry) { | 
|  | 512 | if (entry.injectionState == nullptr || !entry.injectionState->targetUid) { | 
|  | 513 | // The event was not injected, or the injected event does not target a window. | 
|  | 514 | return {}; | 
|  | 515 | } | 
|  | 516 | const int32_t uid = *entry.injectionState->targetUid; | 
|  | 517 | if (window == nullptr) { | 
|  | 518 | return StringPrintf("No valid window target for injection into uid %d.", uid); | 
|  | 519 | } | 
|  | 520 | if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { | 
|  | 521 | return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " | 
|  | 522 | "owned by uid %d.", | 
|  | 523 | uid, window->getName().c_str(), window->getInfo()->ownerUid); | 
|  | 524 | } | 
|  | 525 | return {}; | 
|  | 526 | } | 
|  | 527 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 528 | } // namespace | 
|  | 529 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 530 | // --- InputDispatcher --- | 
|  | 531 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 532 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 533 | : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {} | 
|  | 534 |  | 
|  | 535 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy, | 
|  | 536 | std::chrono::nanoseconds staleEventTimeout) | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 537 | : mPolicy(policy), | 
|  | 538 | mPendingEvent(nullptr), | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 539 | mLastDropReason(DropReason::NOT_DROPPED), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 540 | mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 541 | mAppSwitchSawKeyDown(false), | 
|  | 542 | mAppSwitchDueTime(LONG_LONG_MAX), | 
|  | 543 | mNextUnblockedEvent(nullptr), | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 544 | mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT), | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 545 | mDispatchEnabled(false), | 
|  | 546 | mDispatchFrozen(false), | 
|  | 547 | mInputFilterEnabled(false), | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 548 | // mInTouchMode will be initialized by the WindowManager to the default device config. | 
|  | 549 | // To avoid leaking stack in case that call never comes, and for tests, | 
|  | 550 | // initialize it here anyways. | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 551 | mInTouchMode(kDefaultInTouchMode), | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 552 | mMaximumObscuringOpacityForTouch(1.0f), | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 553 | mFocusedDisplayId(ADISPLAY_ID_DEFAULT), | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 554 | mWindowTokenWithPointerCapture(nullptr), | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 555 | mStaleEventTimeout(staleEventTimeout), | 
| Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 556 | mLatencyAggregator(), | 
| Siarhei Vishniakou | bd25272 | 2022-01-06 03:49:35 -0800 | [diff] [blame] | 557 | mLatencyTracker(&mLatencyAggregator) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | mLooper = new Looper(false); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 559 | mReporter = createInputReporter(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 561 | mWindowInfoListener = new DispatcherWindowListener(*this); | 
|  | 562 | SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener); | 
|  | 563 |  | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 564 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 |  | 
|  | 566 | policy->getDispatcherConfiguration(&mConfig); | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | InputDispatcher::~InputDispatcher() { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 570 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 572 | resetKeyRepeatLocked(); | 
|  | 573 | releasePendingEventLocked(); | 
|  | 574 | drainInboundQueueLocked(); | 
|  | 575 | mCommandQueue.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 577 | while (!mConnectionsByToken.empty()) { | 
|  | 578 | sp<Connection> connection = mConnectionsByToken.begin()->second; | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 579 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), | 
|  | 580 | false /* notify */); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | } | 
|  | 582 | } | 
|  | 583 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 584 | status_t InputDispatcher::start() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 585 | if (mThread) { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 586 | return ALREADY_EXISTS; | 
|  | 587 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 588 | mThread = std::make_unique<InputThread>( | 
|  | 589 | "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); }); | 
|  | 590 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
|  | 593 | status_t InputDispatcher::stop() { | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 594 | if (mThread && mThread->isCallingThread()) { | 
|  | 595 | ALOGE("InputDispatcher cannot be stopped from its own thread!"); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | return INVALID_OPERATION; | 
|  | 597 | } | 
| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 598 | mThread.reset(); | 
|  | 599 | return OK; | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 600 | } | 
|  | 601 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | void InputDispatcher::dispatchOnce() { | 
|  | 603 | nsecs_t nextWakeupTime = LONG_LONG_MAX; | 
|  | 604 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 605 | std::scoped_lock _l(mLock); | 
|  | 606 | mDispatcherIsAlive.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 |  | 
|  | 608 | // Run a dispatch loop if there are no pending commands. | 
|  | 609 | // The dispatch loop might enqueue commands to run afterwards. | 
|  | 610 | if (!haveCommandsLocked()) { | 
|  | 611 | dispatchOnceInnerLocked(&nextWakeupTime); | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | // Run all pending commands if there are any. | 
|  | 615 | // If any commands were run then force the next poll to wake up immediately. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 616 | if (runCommandsLockedInterruptable()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | nextWakeupTime = LONG_LONG_MIN; | 
|  | 618 | } | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 619 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 620 | // If we are still waiting for ack on some events, | 
|  | 621 | // we might have to wake up earlier to check if an app is anr'ing. | 
|  | 622 | const nsecs_t nextAnrCheck = processAnrsLocked(); | 
|  | 623 | nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck); | 
|  | 624 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 625 | // We are about to enter an infinitely long sleep, because we have no commands or | 
|  | 626 | // pending or queued events | 
|  | 627 | if (nextWakeupTime == LONG_LONG_MAX) { | 
|  | 628 | mDispatcherEnteredIdle.notify_all(); | 
|  | 629 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | } // release lock | 
|  | 631 |  | 
|  | 632 | // Wait for callback or timeout or wake.  (make sure we round up, not down) | 
|  | 633 | nsecs_t currentTime = now(); | 
|  | 634 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); | 
|  | 635 | mLooper->pollOnce(timeoutMillis); | 
|  | 636 | } | 
|  | 637 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 638 | /** | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 639 | * Raise ANR if there is no focused window. | 
|  | 640 | * Before the ANR is raised, do a final state check: | 
|  | 641 | * 1. The currently focused application must be the same one we are waiting for. | 
|  | 642 | * 2. Ensure we still don't have a focused window. | 
|  | 643 | */ | 
|  | 644 | void InputDispatcher::processNoFocusedWindowAnrLocked() { | 
|  | 645 | // Check if the application that we are waiting for is still focused. | 
|  | 646 | std::shared_ptr<InputApplicationHandle> focusedApplication = | 
|  | 647 | getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId); | 
|  | 648 | if (focusedApplication == nullptr || | 
|  | 649 | focusedApplication->getApplicationToken() != | 
|  | 650 | mAwaitedFocusedApplication->getApplicationToken()) { | 
|  | 651 | // Unexpected because we should have reset the ANR timer when focused application changed | 
|  | 652 | ALOGE("Waited for a focused window, but focused application has already changed to %s", | 
|  | 653 | focusedApplication->getName().c_str()); | 
|  | 654 | return; // The focused application has changed. | 
|  | 655 | } | 
|  | 656 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 657 | const sp<WindowInfoHandle>& focusedWindowHandle = | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 658 | getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId); | 
|  | 659 | if (focusedWindowHandle != nullptr) { | 
|  | 660 | return; // We now have a focused window. No need for ANR. | 
|  | 661 | } | 
|  | 662 | onAnrLocked(mAwaitedFocusedApplication); | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | /** | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 666 | * Check if any of the connections' wait queues have events that are too old. | 
|  | 667 | * If we waited for events to be ack'ed for more than the window timeout, raise an ANR. | 
|  | 668 | * Return the time at which we should wake up next. | 
|  | 669 | */ | 
|  | 670 | nsecs_t InputDispatcher::processAnrsLocked() { | 
|  | 671 | const nsecs_t currentTime = now(); | 
|  | 672 | nsecs_t nextAnrCheck = LONG_LONG_MAX; | 
|  | 673 | // Check if we are waiting for a focused window to appear. Raise ANR if waited too long | 
|  | 674 | if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) { | 
|  | 675 | if (currentTime >= *mNoFocusedWindowTimeoutTime) { | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 676 | processNoFocusedWindowAnrLocked(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 677 | mAwaitedFocusedApplication.reset(); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 678 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 679 | return LONG_LONG_MIN; | 
|  | 680 | } else { | 
| Siarhei Vishniakou | 38a6d27 | 2020-10-20 20:29:33 -0500 | [diff] [blame] | 681 | // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 682 | nextAnrCheck = *mNoFocusedWindowTimeoutTime; | 
|  | 683 | } | 
|  | 684 | } | 
|  | 685 |  | 
|  | 686 | // Check if any connection ANRs are due | 
|  | 687 | nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout()); | 
|  | 688 | if (currentTime < nextAnrCheck) { // most likely scenario | 
|  | 689 | return nextAnrCheck;          // everything is normal. Let's check again at nextAnrCheck | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | // If we reached here, we have an unresponsive connection. | 
|  | 693 | sp<Connection> connection = getConnectionLocked(mAnrTracker.firstToken()); | 
|  | 694 | if (connection == nullptr) { | 
|  | 695 | ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout()); | 
|  | 696 | return nextAnrCheck; | 
|  | 697 | } | 
|  | 698 | connection->responsive = false; | 
|  | 699 | // Stop waking up for this unresponsive connection | 
|  | 700 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 701 | onAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 702 | return LONG_LONG_MIN; | 
|  | 703 | } | 
|  | 704 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 705 | std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked( | 
|  | 706 | const sp<Connection>& connection) { | 
|  | 707 | if (connection->monitor) { | 
|  | 708 | return mMonitorDispatchingTimeout; | 
|  | 709 | } | 
|  | 710 | const sp<WindowInfoHandle> window = | 
|  | 711 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | if (window != nullptr) { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 713 | return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 714 | } | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 715 | return DEFAULT_INPUT_DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 716 | } | 
|  | 717 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { | 
|  | 719 | nsecs_t currentTime = now(); | 
|  | 720 |  | 
| Jeff Brown | dc5992e | 2014-04-11 01:27:26 -0700 | [diff] [blame] | 721 | // Reset the key repeat timer whenever normal dispatch is suspended while the | 
|  | 722 | // device is in a non-interactive state.  This is to ensure that we abort a key | 
|  | 723 | // repeat if the device is just coming out of sleep. | 
|  | 724 | if (!mDispatchEnabled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | resetKeyRepeatLocked(); | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. | 
|  | 729 | if (mDispatchFrozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 730 | if (DEBUG_FOCUS) { | 
|  | 731 | ALOGD("Dispatch frozen.  Waiting some more."); | 
|  | 732 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 733 | return; | 
|  | 734 | } | 
|  | 735 |  | 
|  | 736 | // Optimize latency of app switches. | 
|  | 737 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has | 
|  | 738 | // been pressed.  When it expires, we preempt dispatch and drop all other pending events. | 
|  | 739 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; | 
|  | 740 | if (mAppSwitchDueTime < *nextWakeupTime) { | 
|  | 741 | *nextWakeupTime = mAppSwitchDueTime; | 
|  | 742 | } | 
|  | 743 |  | 
|  | 744 | // Ready to start a new event. | 
|  | 745 | // If we don't already have a pending event, go grab one. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 746 | if (!mPendingEvent) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 747 | if (mInboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 748 | if (isAppSwitchDue) { | 
|  | 749 | // The inbound queue is empty so the app switch key we were waiting | 
|  | 750 | // for will never arrive.  Stop waiting for it. | 
|  | 751 | resetPendingAppSwitchLocked(false); | 
|  | 752 | isAppSwitchDue = false; | 
|  | 753 | } | 
|  | 754 |  | 
|  | 755 | // Synthesize a key repeat if appropriate. | 
|  | 756 | if (mKeyRepeatState.lastKeyEntry) { | 
|  | 757 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { | 
|  | 758 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); | 
|  | 759 | } else { | 
|  | 760 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { | 
|  | 761 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; | 
|  | 762 | } | 
|  | 763 | } | 
|  | 764 | } | 
|  | 765 |  | 
|  | 766 | // Nothing to do if there is no pending event. | 
|  | 767 | if (!mPendingEvent) { | 
|  | 768 | return; | 
|  | 769 | } | 
|  | 770 | } else { | 
|  | 771 | // Inbound queue has at least one entry. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 772 | mPendingEvent = mInboundQueue.front(); | 
|  | 773 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | traceInboundQueueLengthLocked(); | 
|  | 775 | } | 
|  | 776 |  | 
|  | 777 | // Poke user activity for this event. | 
|  | 778 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 779 | pokeUserActivityLocked(*mPendingEvent); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | } | 
|  | 782 |  | 
|  | 783 | // Now we have an event to dispatch. | 
|  | 784 | // 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] | 785 | ALOG_ASSERT(mPendingEvent != nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | bool done = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 787 | DropReason dropReason = DropReason::NOT_DROPPED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 789 | dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 790 | } else if (!mDispatchEnabled) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 791 | dropReason = DropReason::DISABLED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 792 | } | 
|  | 793 |  | 
|  | 794 | if (mNextUnblockedEvent == mPendingEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 795 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 796 | } | 
|  | 797 |  | 
|  | 798 | switch (mPendingEvent->type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 799 | case EventEntry::Type::CONFIGURATION_CHANGED: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 800 | const ConfigurationChangedEntry& typedEntry = | 
|  | 801 | static_cast<const ConfigurationChangedEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 802 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 803 | dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 804 | break; | 
|  | 805 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 807 | case EventEntry::Type::DEVICE_RESET: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 808 | const DeviceResetEntry& typedEntry = | 
|  | 809 | static_cast<const DeviceResetEntry&>(*mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 810 | done = dispatchDeviceResetLocked(currentTime, typedEntry); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 811 | dropReason = DropReason::NOT_DROPPED; // device resets are never dropped | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 812 | break; | 
|  | 813 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 815 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 816 | std::shared_ptr<FocusEntry> typedEntry = | 
|  | 817 | std::static_pointer_cast<FocusEntry>(mPendingEvent); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 818 | dispatchFocusLocked(currentTime, typedEntry); | 
|  | 819 | done = true; | 
|  | 820 | dropReason = DropReason::NOT_DROPPED; // focus events are never dropped | 
|  | 821 | break; | 
|  | 822 | } | 
|  | 823 |  | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 824 | case EventEntry::Type::TOUCH_MODE_CHANGED: { | 
|  | 825 | const auto typedEntry = std::static_pointer_cast<TouchModeEntry>(mPendingEvent); | 
|  | 826 | dispatchTouchModeChangeLocked(currentTime, typedEntry); | 
|  | 827 | done = true; | 
|  | 828 | dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped | 
|  | 829 | break; | 
|  | 830 | } | 
|  | 831 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 832 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 833 | const auto typedEntry = | 
|  | 834 | std::static_pointer_cast<PointerCaptureChangedEntry>(mPendingEvent); | 
|  | 835 | dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason); | 
|  | 836 | done = true; | 
|  | 837 | break; | 
|  | 838 | } | 
|  | 839 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 840 | case EventEntry::Type::DRAG: { | 
|  | 841 | std::shared_ptr<DragEntry> typedEntry = | 
|  | 842 | std::static_pointer_cast<DragEntry>(mPendingEvent); | 
|  | 843 | dispatchDragLocked(currentTime, typedEntry); | 
|  | 844 | done = true; | 
|  | 845 | break; | 
|  | 846 | } | 
|  | 847 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 848 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 849 | std::shared_ptr<KeyEntry> keyEntry = std::static_pointer_cast<KeyEntry>(mPendingEvent); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 850 | if (isAppSwitchDue) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 851 | if (isAppSwitchKeyEvent(*keyEntry)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 852 | resetPendingAppSwitchLocked(true); | 
|  | 853 | isAppSwitchDue = false; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 854 | } else if (dropReason == DropReason::NOT_DROPPED) { | 
|  | 855 | dropReason = DropReason::APP_SWITCH; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 856 | } | 
|  | 857 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 858 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 859 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 860 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 861 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 862 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 863 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 864 | done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 865 | break; | 
|  | 866 | } | 
|  | 867 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 868 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 869 | std::shared_ptr<MotionEntry> motionEntry = | 
|  | 870 | std::static_pointer_cast<MotionEntry>(mPendingEvent); | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 871 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 872 | dropReason = DropReason::APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 873 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 874 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 875 | dropReason = DropReason::STALE; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 876 | } | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 877 | if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) { | 
|  | 878 | dropReason = DropReason::BLOCKED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 879 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 880 | done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 881 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 882 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 883 |  | 
|  | 884 | case EventEntry::Type::SENSOR: { | 
|  | 885 | std::shared_ptr<SensorEntry> sensorEntry = | 
|  | 886 | std::static_pointer_cast<SensorEntry>(mPendingEvent); | 
|  | 887 | if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { | 
|  | 888 | dropReason = DropReason::APP_SWITCH; | 
|  | 889 | } | 
|  | 890 | //  Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use | 
|  | 891 | // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead. | 
|  | 892 | nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME); | 
|  | 893 | if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) { | 
|  | 894 | dropReason = DropReason::STALE; | 
|  | 895 | } | 
|  | 896 | dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime); | 
|  | 897 | done = true; | 
|  | 898 | break; | 
|  | 899 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | } | 
|  | 901 |  | 
|  | 902 | if (done) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 903 | if (dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 904 | dropInboundEventLocked(*mPendingEvent, dropReason); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | } | 
| Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 906 | mLastDropReason = dropReason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 907 |  | 
|  | 908 | releasePendingEventLocked(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 909 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 910 | } | 
|  | 911 | } | 
|  | 912 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 913 | bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) { | 
|  | 914 | return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout; | 
|  | 915 | } | 
|  | 916 |  | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 917 | /** | 
|  | 918 | * Return true if the events preceding this incoming motion event should be dropped | 
|  | 919 | * Return false otherwise (the default behaviour) | 
|  | 920 | */ | 
|  | 921 | bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 922 | const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN && | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 923 | isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 924 |  | 
|  | 925 | // Optimize case where the current application is unresponsive and the user | 
|  | 926 | // decides to touch a window in a different application. | 
|  | 927 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 928 | // the touch into the other window. | 
|  | 929 | if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 930 | int32_t displayId = motionEntry.displayId; | 
|  | 931 | int32_t x = static_cast<int32_t>( | 
|  | 932 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 933 | int32_t y = static_cast<int32_t>( | 
|  | 934 | motionEntry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 935 |  | 
|  | 936 | const bool isStylus = isPointerFromStylus(motionEntry, 0 /*pointerIndex*/); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 937 | sp<WindowInfoHandle> touchedWindowHandle = | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 938 | findTouchedWindowAtLocked(displayId, x, y, nullptr, isStylus); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 939 | if (touchedWindowHandle != nullptr && | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 940 | touchedWindowHandle->getApplicationToken() != | 
|  | 941 | mAwaitedFocusedApplication->getApplicationToken()) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 942 | // User touched a different application than the one we are waiting on. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 943 | ALOGI("Pruning input queue because user touched a different application while waiting " | 
|  | 944 | "for %s", | 
|  | 945 | mAwaitedFocusedApplication->getName().c_str()); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 946 | return true; | 
|  | 947 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 948 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 949 | // Alternatively, maybe there's a spy window that could handle this event. | 
|  | 950 | const std::vector<sp<WindowInfoHandle>> touchedSpies = | 
|  | 951 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); | 
|  | 952 | for (const auto& windowHandle : touchedSpies) { | 
|  | 953 | const sp<Connection> connection = getConnectionLocked(windowHandle->getToken()); | 
| Siarhei Vishniakou | 34ed4d4 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 954 | if (connection != nullptr && connection->responsive) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 955 | // This spy window could take more input. Drop all events preceding this | 
|  | 956 | // event, so that the spy window can get a chance to receive the stream. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 957 | ALOGW("Pruning the input queue because %s is unresponsive, but we have a " | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 958 | "responsive spy window that may handle the event.", | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 959 | mAwaitedFocusedApplication->getName().c_str()); | 
|  | 960 | return true; | 
|  | 961 | } | 
|  | 962 | } | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | // Prevent getting stuck: if we have a pending key event, and some motion events that have not | 
|  | 966 | // yet been processed by some connections, the dispatcher will wait for these motion | 
|  | 967 | // events to be processed before dispatching the key event. This is because these motion events | 
|  | 968 | // may cause a new window to be launched, which the user might expect to receive focus. | 
|  | 969 | // To prevent waiting forever for such events, just send the key to the currently focused window | 
|  | 970 | if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) { | 
|  | 971 | ALOGD("Received a new pointer down event, stop waiting for events to process and " | 
|  | 972 | "just send the pending key event to the focused window."); | 
|  | 973 | mKeyIsWaitingForEventsTimeout = now(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 974 | } | 
|  | 975 | return false; | 
|  | 976 | } | 
|  | 977 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 978 | bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 979 | bool needWake = mInboundQueue.empty(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 980 | mInboundQueue.push_back(std::move(newEntry)); | 
|  | 981 | EventEntry& entry = *(mInboundQueue.back()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 982 | traceInboundQueueLengthLocked(); | 
|  | 983 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 984 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 985 | case EventEntry::Type::KEY: { | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 986 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, | 
|  | 987 | "Unexpected untrusted event."); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 988 | // Optimize app switch latency. | 
|  | 989 | // If the application takes too long to catch up then we drop all events preceding | 
|  | 990 | // the app switch key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 991 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 992 | if (isAppSwitchKeyEvent(keyEntry)) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 993 | if (keyEntry.action == AKEY_EVENT_ACTION_DOWN) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 994 | mAppSwitchSawKeyDown = true; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 995 | } else if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 996 | if (mAppSwitchSawKeyDown) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 997 | if (DEBUG_APP_SWITCH) { | 
|  | 998 | ALOGD("App switch is pending!"); | 
|  | 999 | } | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1000 | mAppSwitchDueTime = keyEntry.eventTime + APP_SWITCH_TIMEOUT; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1001 | mAppSwitchSawKeyDown = false; | 
|  | 1002 | needWake = true; | 
|  | 1003 | } | 
|  | 1004 | } | 
|  | 1005 | } | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 1006 |  | 
|  | 1007 | // If a new up event comes in, and the pending event with same key code has been asked | 
|  | 1008 | // to try again later because of the policy. We have to reset the intercept key wake up | 
|  | 1009 | // time for it may have been handled in the policy and could be dropped. | 
|  | 1010 | if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent && | 
|  | 1011 | mPendingEvent->type == EventEntry::Type::KEY) { | 
|  | 1012 | KeyEntry& pendingKey = static_cast<KeyEntry&>(*mPendingEvent); | 
|  | 1013 | if (pendingKey.keyCode == keyEntry.keyCode && | 
|  | 1014 | pendingKey.interceptKeyResult == | 
|  | 1015 | KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1016 | pendingKey.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1017 | pendingKey.interceptKeyWakeupTime = 0; | 
|  | 1018 | needWake = true; | 
|  | 1019 | } | 
|  | 1020 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1021 | break; | 
|  | 1022 | } | 
|  | 1023 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1024 | case EventEntry::Type::MOTION: { | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1025 | LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0, | 
|  | 1026 | "Unexpected untrusted event."); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1027 | if (shouldPruneInboundQueueLocked(static_cast<MotionEntry&>(entry))) { | 
|  | 1028 | mNextUnblockedEvent = mInboundQueue.back(); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1029 | needWake = true; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1031 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1032 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1033 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1034 | LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked"); | 
|  | 1035 | break; | 
|  | 1036 | } | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1037 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1038 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1039 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1040 | case EventEntry::Type::SENSOR: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1041 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 1042 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1043 | // nothing to do | 
|  | 1044 | break; | 
|  | 1045 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1046 | } | 
|  | 1047 |  | 
|  | 1048 | return needWake; | 
|  | 1049 | } | 
|  | 1050 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1051 | void InputDispatcher::addRecentEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1052 | // Do not store sensor event in recent queue to avoid flooding the queue. | 
|  | 1053 | if (entry->type != EventEntry::Type::SENSOR) { | 
|  | 1054 | mRecentQueue.push_back(entry); | 
|  | 1055 | } | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1056 | if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1057 | mRecentQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | } | 
|  | 1059 | } | 
|  | 1060 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1061 | sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, int32_t x, | 
|  | 1062 | int32_t y, TouchState* touchState, | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1063 | bool isStylus, | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1064 | bool addOutsideTargets, | 
|  | 1065 | bool ignoreDragWindow) { | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 1066 | if (addOutsideTargets && touchState == nullptr) { | 
|  | 1067 | LOG_ALWAYS_FATAL("Must provide a valid touch state if adding outside targets"); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 1068 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1069 | // Traverse windows from front to back to find touched window. | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1070 | const auto& windowHandles = getWindowHandlesLocked(displayId); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1071 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1072 | if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1073 | continue; | 
|  | 1074 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1075 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1076 | const WindowInfo& info = *windowHandle->getInfo(); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1077 | if (!info.isSpy() && windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1078 | return windowHandle; | 
|  | 1079 | } | 
| Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1080 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1081 | if (addOutsideTargets && | 
|  | 1082 | info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1083 | touchState->addOrUpdateWindow(windowHandle, InputTarget::FLAG_DISPATCH_AS_OUTSIDE, | 
|  | 1084 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1085 | } | 
|  | 1086 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1087 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1088 | } | 
|  | 1089 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1090 | std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked( | 
|  | 1091 | int32_t displayId, int32_t x, int32_t y, bool isStylus) const { | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1092 | // Traverse windows from front to back and gather the touched spy windows. | 
|  | 1093 | std::vector<sp<WindowInfoHandle>> spyWindows; | 
|  | 1094 | const auto& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 1095 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
|  | 1096 | const WindowInfo& info = *windowHandle->getInfo(); | 
|  | 1097 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 1098 | if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus)) { | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1099 | continue; | 
|  | 1100 | } | 
|  | 1101 | if (!info.isSpy()) { | 
|  | 1102 | // The first touched non-spy window was found, so return the spy windows touched so far. | 
|  | 1103 | return spyWindows; | 
|  | 1104 | } | 
|  | 1105 | spyWindows.push_back(windowHandle); | 
|  | 1106 | } | 
|  | 1107 | return spyWindows; | 
|  | 1108 | } | 
|  | 1109 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1110 | void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1111 | const char* reason; | 
|  | 1112 | switch (dropReason) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1113 | case DropReason::POLICY: | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1114 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 1115 | ALOGD("Dropped event because policy consumed it."); | 
|  | 1116 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1117 | reason = "inbound event was dropped because the policy consumed it"; | 
|  | 1118 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1119 | case DropReason::DISABLED: | 
|  | 1120 | if (mLastDropReason != DropReason::DISABLED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1121 | ALOGI("Dropped event because input dispatch is disabled."); | 
|  | 1122 | } | 
|  | 1123 | reason = "inbound event was dropped because input dispatch is disabled"; | 
|  | 1124 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1125 | case DropReason::APP_SWITCH: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1126 | ALOGI("Dropped event because of pending overdue app switch."); | 
|  | 1127 | reason = "inbound event was dropped because of pending overdue app switch"; | 
|  | 1128 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1129 | case DropReason::BLOCKED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1130 | ALOGI("Dropped event because the current application is not responding and the user " | 
|  | 1131 | "has started interacting with a different application."); | 
|  | 1132 | reason = "inbound event was dropped because the current application is not responding " | 
|  | 1133 | "and the user has started interacting with a different application"; | 
|  | 1134 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1135 | case DropReason::STALE: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1136 | ALOGI("Dropped event because it is stale."); | 
|  | 1137 | reason = "inbound event was dropped because it is stale"; | 
|  | 1138 | break; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1139 | case DropReason::NO_POINTER_CAPTURE: | 
|  | 1140 | ALOGI("Dropped event because there is no window with Pointer Capture."); | 
|  | 1141 | reason = "inbound event was dropped because there is no window with Pointer Capture"; | 
|  | 1142 | break; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1143 | case DropReason::NOT_DROPPED: { | 
|  | 1144 | LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event"); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1145 | return; | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1146 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | } | 
|  | 1148 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1149 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1150 | case EventEntry::Type::KEY: { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1151 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1152 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1153 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1154 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1155 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1156 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1157 | if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1158 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); | 
|  | 1159 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1160 | } else { | 
|  | 1161 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); | 
|  | 1162 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1163 | } | 
|  | 1164 | break; | 
|  | 1165 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1166 | case EventEntry::Type::SENSOR: { | 
|  | 1167 | break; | 
|  | 1168 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1169 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 1170 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1171 | break; | 
|  | 1172 | } | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1173 | case EventEntry::Type::FOCUS: | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1174 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1175 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 1176 | case EventEntry::Type::DEVICE_RESET: { | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1177 | LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1178 | break; | 
|  | 1179 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1180 | } | 
|  | 1181 | } | 
|  | 1182 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1183 | static bool isAppSwitchKeyCode(int32_t keyCode) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1184 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL || | 
|  | 1185 | keyCode == AKEYCODE_APP_SWITCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1186 | } | 
|  | 1187 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1188 | bool InputDispatcher::isAppSwitchKeyEvent(const KeyEntry& keyEntry) { | 
|  | 1189 | return !(keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) && isAppSwitchKeyCode(keyEntry.keyCode) && | 
|  | 1190 | (keyEntry.policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1191 | (keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1192 | } | 
|  | 1193 |  | 
|  | 1194 | bool InputDispatcher::isAppSwitchPendingLocked() { | 
|  | 1195 | return mAppSwitchDueTime != LONG_LONG_MAX; | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { | 
|  | 1199 | mAppSwitchDueTime = LONG_LONG_MAX; | 
|  | 1200 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1201 | if (DEBUG_APP_SWITCH) { | 
|  | 1202 | if (handled) { | 
|  | 1203 | ALOGD("App switch has arrived."); | 
|  | 1204 | } else { | 
|  | 1205 | ALOGD("App switch was abandoned."); | 
|  | 1206 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1207 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | } | 
|  | 1209 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1210 | bool InputDispatcher::haveCommandsLocked() const { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1211 | return !mCommandQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1212 | } | 
|  | 1213 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1214 | bool InputDispatcher::runCommandsLockedInterruptable() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1215 | if (mCommandQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1216 | return false; | 
|  | 1217 | } | 
|  | 1218 |  | 
|  | 1219 | do { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1220 | auto command = std::move(mCommandQueue.front()); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1221 | mCommandQueue.pop_front(); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1222 | // Commands are run with the lock held, but may release and re-acquire the lock from within. | 
|  | 1223 | command(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1224 | } while (!mCommandQueue.empty()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1225 | return true; | 
|  | 1226 | } | 
|  | 1227 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1228 | void InputDispatcher::postCommandLocked(Command&& command) { | 
|  | 1229 | mCommandQueue.push_back(command); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | void InputDispatcher::drainInboundQueueLocked() { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1233 | while (!mInboundQueue.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1234 | std::shared_ptr<EventEntry> entry = mInboundQueue.front(); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 1235 | mInboundQueue.pop_front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1236 | releaseInboundEventLocked(entry); | 
|  | 1237 | } | 
|  | 1238 | traceInboundQueueLengthLocked(); | 
|  | 1239 | } | 
|  | 1240 |  | 
|  | 1241 | void InputDispatcher::releasePendingEventLocked() { | 
|  | 1242 | if (mPendingEvent) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1243 | releaseInboundEventLocked(mPendingEvent); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1244 | mPendingEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | } | 
|  | 1246 | } | 
|  | 1247 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1248 | void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1249 | InjectionState* injectionState = entry->injectionState; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1250 | if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1251 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 1252 | ALOGD("Injected inbound event was dropped."); | 
|  | 1253 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1254 | setInjectionResult(*entry, InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 | } | 
|  | 1256 | if (entry == mNextUnblockedEvent) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1257 | mNextUnblockedEvent = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1258 | } | 
|  | 1259 | addRecentEventLocked(entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1260 | } | 
|  | 1261 |  | 
|  | 1262 | void InputDispatcher::resetKeyRepeatLocked() { | 
|  | 1263 | if (mKeyRepeatState.lastKeyEntry) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1264 | mKeyRepeatState.lastKeyEntry = nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1268 | std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { | 
|  | 1269 | std::shared_ptr<KeyEntry> entry = mKeyRepeatState.lastKeyEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1270 |  | 
| Michael Wright | 2e73295 | 2014-09-24 13:26:59 -0700 | [diff] [blame] | 1271 | uint32_t policyFlags = entry->policyFlags & | 
|  | 1272 | (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1273 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1274 | std::shared_ptr<KeyEntry> newEntry = | 
|  | 1275 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, entry->deviceId, | 
|  | 1276 | entry->source, entry->displayId, policyFlags, entry->action, | 
|  | 1277 | entry->flags, entry->keyCode, entry->scanCode, | 
|  | 1278 | entry->metaState, entry->repeatCount + 1, entry->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1279 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1280 | newEntry->syntheticRepeat = true; | 
|  | 1281 | mKeyRepeatState.lastKeyEntry = newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1282 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1283 | return newEntry; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1284 | } | 
|  | 1285 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1286 | bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1287 | const ConfigurationChangedEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1288 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1289 | ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime); | 
|  | 1290 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1291 |  | 
|  | 1292 | // Reset key repeating in case a keyboard device was added or removed or something. | 
|  | 1293 | resetKeyRepeatLocked(); | 
|  | 1294 |  | 
|  | 1295 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1296 | auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) { | 
|  | 1297 | scoped_unlock unlock(mLock); | 
|  | 1298 | mPolicy->notifyConfigurationChanged(eventTime); | 
|  | 1299 | }; | 
|  | 1300 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1301 | return true; | 
|  | 1302 | } | 
|  | 1303 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1304 | bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime, | 
|  | 1305 | const DeviceResetEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1306 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1307 | ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime, | 
|  | 1308 | entry.deviceId); | 
|  | 1309 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1310 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 1311 | // Reset key repeating in case a keyboard device was disabled or enabled. | 
|  | 1312 | if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) { | 
|  | 1313 | resetKeyRepeatLocked(); | 
|  | 1314 | } | 
|  | 1315 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1316 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, "device was reset"); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1317 | options.deviceId = entry.deviceId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1318 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1319 | return true; | 
|  | 1320 | } | 
|  | 1321 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1322 | void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1323 | const std::string& reason) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1324 | if (mPendingEvent != nullptr) { | 
|  | 1325 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 1326 | // for the pending event to get dispatched to the newly focused window | 
|  | 1327 | mInboundQueue.push_front(mPendingEvent); | 
|  | 1328 | mPendingEvent = nullptr; | 
|  | 1329 | } | 
|  | 1330 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1331 | std::unique_ptr<FocusEntry> focusEntry = | 
|  | 1332 | std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus, | 
|  | 1333 | reason); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1334 |  | 
|  | 1335 | // This event should go to the front of the queue, but behind all other focus events | 
|  | 1336 | // Find the last focus event, and insert right after it | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1337 | std::deque<std::shared_ptr<EventEntry>>::reverse_iterator it = | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1338 | std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1339 | [](const std::shared_ptr<EventEntry>& event) { | 
|  | 1340 | return event->type == EventEntry::Type::FOCUS; | 
|  | 1341 | }); | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 1342 |  | 
|  | 1343 | // 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] | 1344 | mInboundQueue.insert(it.base(), std::move(focusEntry)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1345 | } | 
|  | 1346 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1347 | void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 1348 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1349 | if (channel == nullptr) { | 
|  | 1350 | return; // Window has gone away | 
|  | 1351 | } | 
|  | 1352 | InputTarget target; | 
|  | 1353 | target.inputChannel = channel; | 
|  | 1354 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1355 | entry->dispatchInProgress = true; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1356 | std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") + | 
|  | 1357 | channel->getName(); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 1358 | std::string reason = std::string("reason=").append(entry->reason); | 
|  | 1359 | android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1360 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1361 | } | 
|  | 1362 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1363 | void InputDispatcher::dispatchPointerCaptureChangedLocked( | 
|  | 1364 | nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, | 
|  | 1365 | DropReason& dropReason) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1366 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1367 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1368 | const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1369 | sp<IBinder> token; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1370 |  | 
|  | 1371 | if (entry->pointerCaptureRequest.enable) { | 
|  | 1372 | // Enable Pointer Capture. | 
|  | 1373 | if (haveWindowWithPointerCapture && | 
|  | 1374 | (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) { | 
| Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 1375 | // This can happen if pointer capture is disabled and re-enabled before we notify the | 
|  | 1376 | // app of the state change, so there is no need to notify the app. | 
|  | 1377 | ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change."); | 
|  | 1378 | return; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1379 | } | 
|  | 1380 | if (!mCurrentPointerCaptureRequest.enable) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1381 | // This can happen if a window requests capture and immediately releases capture. | 
|  | 1382 | ALOGW("No window requested Pointer Capture."); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1383 | dropReason = DropReason::NO_POINTER_CAPTURE; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1384 | return; | 
|  | 1385 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1386 | if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) { | 
|  | 1387 | ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch."); | 
|  | 1388 | return; | 
|  | 1389 | } | 
|  | 1390 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1391 | token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1392 | LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture."); | 
|  | 1393 | mWindowTokenWithPointerCapture = token; | 
|  | 1394 | } else { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1395 | // Disable Pointer Capture. | 
|  | 1396 | // We do not check if the sequence number matches for requests to disable Pointer Capture | 
|  | 1397 | // for two reasons: | 
|  | 1398 | //  1. Pointer Capture can be disabled by a focus change, which means we can get two entries | 
|  | 1399 | //     to disable capture with the same sequence number: one generated by | 
|  | 1400 | //     disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer | 
|  | 1401 | //     Capture being disabled in InputReader. | 
|  | 1402 | //  2. We respect any request to disable Pointer Capture generated by InputReader, since the | 
|  | 1403 | //     actual Pointer Capture state that affects events being generated by input devices is | 
|  | 1404 | //     in InputReader. | 
|  | 1405 | if (!haveWindowWithPointerCapture) { | 
|  | 1406 | // Pointer capture was already forcefully disabled because of focus change. | 
|  | 1407 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1408 | return; | 
|  | 1409 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1410 | token = mWindowTokenWithPointerCapture; | 
|  | 1411 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1412 | if (mCurrentPointerCaptureRequest.enable) { | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1413 | setPointerCaptureLocked(false); | 
|  | 1414 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | auto channel = getInputChannelLocked(token); | 
|  | 1418 | if (channel == nullptr) { | 
|  | 1419 | // Window has gone away, clean up Pointer Capture state. | 
|  | 1420 | mWindowTokenWithPointerCapture = nullptr; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1421 | if (mCurrentPointerCaptureRequest.enable) { | 
| Prabir Pradhan | 7d03038 | 2020-12-21 07:58:35 -0800 | [diff] [blame] | 1422 | setPointerCaptureLocked(false); | 
|  | 1423 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1424 | return; | 
|  | 1425 | } | 
|  | 1426 | InputTarget target; | 
|  | 1427 | target.inputChannel = channel; | 
|  | 1428 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1429 | entry->dispatchInProgress = true; | 
|  | 1430 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1431 |  | 
|  | 1432 | dropReason = DropReason::NOT_DROPPED; | 
|  | 1433 | } | 
|  | 1434 |  | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 1435 | void InputDispatcher::dispatchTouchModeChangeLocked(nsecs_t currentTime, | 
|  | 1436 | const std::shared_ptr<TouchModeEntry>& entry) { | 
|  | 1437 | const std::vector<sp<WindowInfoHandle>>& windowHandles = | 
|  | 1438 | getWindowHandlesLocked(mFocusedDisplayId); | 
|  | 1439 | if (windowHandles.empty()) { | 
|  | 1440 | return; | 
|  | 1441 | } | 
|  | 1442 | const std::vector<InputTarget> inputTargets = | 
|  | 1443 | getInputTargetsFromWindowHandlesLocked(windowHandles); | 
|  | 1444 | if (inputTargets.empty()) { | 
|  | 1445 | return; | 
|  | 1446 | } | 
|  | 1447 | entry->dispatchInProgress = true; | 
|  | 1448 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1449 | } | 
|  | 1450 |  | 
|  | 1451 | std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked( | 
|  | 1452 | const std::vector<sp<WindowInfoHandle>>& windowHandles) const { | 
|  | 1453 | std::vector<InputTarget> inputTargets; | 
|  | 1454 | for (const sp<WindowInfoHandle>& handle : windowHandles) { | 
|  | 1455 | // TODO(b/193718270): Due to performance concerns, consider notifying visible windows only. | 
|  | 1456 | const sp<IBinder>& token = handle->getToken(); | 
|  | 1457 | if (token == nullptr) { | 
|  | 1458 | continue; | 
|  | 1459 | } | 
|  | 1460 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(token); | 
|  | 1461 | if (channel == nullptr) { | 
|  | 1462 | continue; // Window has gone away | 
|  | 1463 | } | 
|  | 1464 | InputTarget target; | 
|  | 1465 | target.inputChannel = channel; | 
|  | 1466 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1467 | inputTargets.push_back(target); | 
|  | 1468 | } | 
|  | 1469 | return inputTargets; | 
|  | 1470 | } | 
|  | 1471 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1472 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1473 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1474 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1475 | if (!entry->dispatchInProgress) { | 
|  | 1476 | if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN && | 
|  | 1477 | (entry->policyFlags & POLICY_FLAG_TRUSTED) && | 
|  | 1478 | (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { | 
|  | 1479 | if (mKeyRepeatState.lastKeyEntry && | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1480 | mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode && | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | // We have seen two identical key downs in a row which indicates that the device | 
|  | 1482 | // driver is automatically generating key repeats itself.  We take note of the | 
|  | 1483 | // repeat here, but we disable our own next key repeat timer since it is clear that | 
|  | 1484 | // we will not need to synthesize key repeats ourselves. | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1485 | mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) { | 
|  | 1486 | // Make sure we don't get key down from a different device. If a different | 
|  | 1487 | // device Id has same key pressed down, the new device Id will replace the | 
|  | 1488 | // current one to hold the key repeat with repeat count reset. | 
|  | 1489 | // In the future when got a KEY_UP on the device id, drop it and do not | 
|  | 1490 | // stop the key repeat on current device. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1491 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; | 
|  | 1492 | resetKeyRepeatLocked(); | 
|  | 1493 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves | 
|  | 1494 | } else { | 
|  | 1495 | // Not a repeat.  Save key down state in case we do see a repeat later. | 
|  | 1496 | resetKeyRepeatLocked(); | 
|  | 1497 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; | 
|  | 1498 | } | 
|  | 1499 | mKeyRepeatState.lastKeyEntry = entry; | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 1500 | } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry && | 
|  | 1501 | mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1502 | // The key on device 'deviceId' is still down, do not stop key repeat | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1503 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 1504 | ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId); | 
|  | 1505 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1506 | } else if (!entry->syntheticRepeat) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1507 | resetKeyRepeatLocked(); | 
|  | 1508 | } | 
|  | 1509 |  | 
|  | 1510 | if (entry->repeatCount == 1) { | 
|  | 1511 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1512 | } else { | 
|  | 1513 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | entry->dispatchInProgress = true; | 
|  | 1517 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1518 | logOutboundKeyDetails("dispatchKey - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | } | 
|  | 1520 |  | 
|  | 1521 | // Handle case where the policy asked us to try again later last time. | 
|  | 1522 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { | 
|  | 1523 | if (currentTime < entry->interceptKeyWakeupTime) { | 
|  | 1524 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { | 
|  | 1525 | *nextWakeupTime = entry->interceptKeyWakeupTime; | 
|  | 1526 | } | 
|  | 1527 | return false; // wait until next wakeup | 
|  | 1528 | } | 
|  | 1529 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; | 
|  | 1530 | entry->interceptKeyWakeupTime = 0; | 
|  | 1531 | } | 
|  | 1532 |  | 
|  | 1533 | // Give the policy a chance to intercept the key. | 
|  | 1534 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { | 
|  | 1535 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 1536 | sp<IBinder> focusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 1537 | mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry)); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1538 |  | 
|  | 1539 | auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) { | 
|  | 1540 | doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry); | 
|  | 1541 | }; | 
|  | 1542 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1543 | return false; // wait for the command to run | 
|  | 1544 | } else { | 
|  | 1545 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
|  | 1546 | } | 
|  | 1547 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1548 | if (*dropReason == DropReason::NOT_DROPPED) { | 
|  | 1549 | *dropReason = DropReason::POLICY; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | } | 
|  | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1554 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1555 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1556 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1557 | : InputEventInjectionResult::FAILED); | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1558 | mReporter->reportDroppedKey(entry->id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1559 | return true; | 
|  | 1560 | } | 
|  | 1561 |  | 
|  | 1562 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1563 | std::vector<InputTarget> inputTargets; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1564 | InputEventInjectionResult injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1565 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1566 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1567 | return false; | 
|  | 1568 | } | 
|  | 1569 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1570 | setInjectionResult(*entry, injectionResult); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1571 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1572 | return true; | 
|  | 1573 | } | 
|  | 1574 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1575 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1576 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1577 |  | 
|  | 1578 | // Dispatch the key. | 
|  | 1579 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1580 | return true; | 
|  | 1581 | } | 
|  | 1582 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1583 | void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1584 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1585 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", " | 
|  | 1586 | "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, " | 
|  | 1587 | "metaState=0x%x, repeatCount=%d, downTime=%" PRId64, | 
|  | 1588 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, | 
|  | 1589 | entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode, | 
|  | 1590 | entry.metaState, entry.repeatCount, entry.downTime); | 
|  | 1591 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | } | 
|  | 1593 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1594 | void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime, | 
|  | 1595 | const std::shared_ptr<SensorEntry>& entry, | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1596 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1597 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1598 | ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, " | 
|  | 1599 | "source=0x%x, sensorType=%s", | 
|  | 1600 | entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source, | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1601 | ftl::enum_string(entry->sensorType).c_str()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1602 | } | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 1603 | auto command = [this, entry]() REQUIRES(mLock) { | 
|  | 1604 | scoped_unlock unlock(mLock); | 
|  | 1605 |  | 
|  | 1606 | if (entry->accuracyChanged) { | 
|  | 1607 | mPolicy->notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy); | 
|  | 1608 | } | 
|  | 1609 | mPolicy->notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy, | 
|  | 1610 | entry->hwTimestamp, entry->values); | 
|  | 1611 | }; | 
|  | 1612 | postCommandLocked(std::move(command)); | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1616 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1617 | ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId, | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1618 | ftl::enum_string(sensorType).c_str()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1619 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1620 | { // acquire lock | 
|  | 1621 | std::scoped_lock _l(mLock); | 
|  | 1622 |  | 
|  | 1623 | for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) { | 
|  | 1624 | std::shared_ptr<EventEntry> entry = *it; | 
|  | 1625 | if (entry->type == EventEntry::Type::SENSOR) { | 
|  | 1626 | it = mInboundQueue.erase(it); | 
|  | 1627 | releaseInboundEventLocked(entry); | 
|  | 1628 | } | 
|  | 1629 | } | 
|  | 1630 | } | 
|  | 1631 | return true; | 
|  | 1632 | } | 
|  | 1633 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1634 | bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1635 | DropReason* dropReason, nsecs_t* nextWakeupTime) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1636 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1637 | // Preprocessing. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1638 | if (!entry->dispatchInProgress) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1639 | entry->dispatchInProgress = true; | 
|  | 1640 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1641 | logOutboundMotionDetails("dispatchMotion - ", *entry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1642 | } | 
|  | 1643 |  | 
|  | 1644 | // Clean up if dropping the event. | 
| Siarhei Vishniakou | 0fb1a0e | 2019-10-22 11:23:36 -0700 | [diff] [blame] | 1645 | if (*dropReason != DropReason::NOT_DROPPED) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1646 | setInjectionResult(*entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1647 | *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED | 
|  | 1648 | : InputEventInjectionResult::FAILED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1649 | return true; | 
|  | 1650 | } | 
|  | 1651 |  | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 1652 | const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1653 |  | 
|  | 1654 | // Identify targets. | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1655 | std::vector<InputTarget> inputTargets; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1656 |  | 
|  | 1657 | bool conflictingPointerActions = false; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1658 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1659 | if (isPointerEvent) { | 
|  | 1660 | // Pointer event.  (eg. touchscreen) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1661 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1662 | findTouchedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1663 | &conflictingPointerActions); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1664 | } else { | 
|  | 1665 | // Non touch event.  (eg. trackball) | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1666 | injectionResult = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1667 | findFocusedWindowTargetsLocked(currentTime, *entry, inputTargets, nextWakeupTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1668 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1669 | if (injectionResult == InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1670 | return false; | 
|  | 1671 | } | 
|  | 1672 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1673 | setInjectionResult(*entry, injectionResult); | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1674 | if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1675 | return true; | 
|  | 1676 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1677 | if (injectionResult != InputEventInjectionResult::SUCCEEDED) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 1678 | CancelationOptions::Mode mode(isPointerEvent | 
|  | 1679 | ? CancelationOptions::CANCEL_POINTER_EVENTS | 
|  | 1680 | : CancelationOptions::CANCEL_NON_POINTER_EVENTS); | 
|  | 1681 | CancelationOptions options(mode, "input event injection failed"); | 
|  | 1682 | synthesizeCancelationEventsForMonitorsLocked(options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1683 | return true; | 
|  | 1684 | } | 
|  | 1685 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1686 | // Add monitor channels from event's or focused display. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1687 | addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1688 |  | 
|  | 1689 | // Dispatch the motion. | 
|  | 1690 | if (conflictingPointerActions) { | 
|  | 1691 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1692 | "conflicting pointer actions"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1693 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 1694 | } | 
|  | 1695 | dispatchEventLocked(currentTime, entry, inputTargets); | 
|  | 1696 | return true; | 
|  | 1697 | } | 
|  | 1698 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1699 | void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle, | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1700 | bool isExiting, const int32_t rawX, | 
|  | 1701 | const int32_t rawY) { | 
|  | 1702 | const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY)); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1703 | std::unique_ptr<DragEntry> dragEntry = | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 1704 | std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(), | 
|  | 1705 | isExiting, xy.x, xy.y); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1706 |  | 
|  | 1707 | enqueueInboundEventLocked(std::move(dragEntry)); | 
|  | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | void InputDispatcher::dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) { | 
|  | 1711 | std::shared_ptr<InputChannel> channel = getInputChannelLocked(entry->connectionToken); | 
|  | 1712 | if (channel == nullptr) { | 
|  | 1713 | return; // Window has gone away | 
|  | 1714 | } | 
|  | 1715 | InputTarget target; | 
|  | 1716 | target.inputChannel = channel; | 
|  | 1717 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 1718 | entry->dispatchInProgress = true; | 
|  | 1719 | dispatchEventLocked(currentTime, entry, {target}); | 
|  | 1720 | } | 
|  | 1721 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1722 | void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1723 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 1724 | ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 1725 | ", policyFlags=0x%x, " | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1726 | "action=%s, actionButton=0x%x, flags=0x%x, " | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1727 | "metaState=0x%x, buttonState=0x%x," | 
|  | 1728 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64, | 
|  | 1729 | prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId, | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1730 | entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(), | 
|  | 1731 | entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags, | 
|  | 1732 | entry.xPrecision, entry.yPrecision, entry.downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1734 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
|  | 1735 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
|  | 1736 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 1737 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 1738 | "orientation=%f", | 
|  | 1739 | i, entry.pointerProperties[i].id, entry.pointerProperties[i].toolType, | 
|  | 1740 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 1741 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 1742 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 1743 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 1744 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 1745 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 1746 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 1747 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 1748 | entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
|  | 1749 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1750 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1751 | } | 
|  | 1752 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 1753 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, | 
|  | 1754 | std::shared_ptr<EventEntry> eventEntry, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1755 | const std::vector<InputTarget>& inputTargets) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1756 | ATRACE_CALL(); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 1757 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 1758 | ALOGD("dispatchEventToCurrentInputTargets"); | 
|  | 1759 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1760 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 1761 | updateInteractionTokensLocked(*eventEntry, inputTargets); | 
|  | 1762 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1763 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true | 
|  | 1764 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1765 | pokeUserActivityLocked(*eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1766 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 1767 | for (const InputTarget& inputTarget : inputTargets) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 1768 | sp<Connection> connection = | 
|  | 1769 | getConnectionLocked(inputTarget.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 1770 | if (connection != nullptr) { | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 1771 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1772 | } else { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1773 | if (DEBUG_FOCUS) { | 
|  | 1774 | ALOGD("Dropping event delivery to target with channel '%s' because it " | 
|  | 1775 | "is no longer registered with the input dispatcher.", | 
|  | 1776 | inputTarget.inputChannel->getName().c_str()); | 
|  | 1777 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | } | 
|  | 1779 | } | 
|  | 1780 | } | 
|  | 1781 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1782 | void InputDispatcher::cancelEventsForAnrLocked(const sp<Connection>& connection) { | 
|  | 1783 | // We will not be breaking any connections here, even if the policy wants us to abort dispatch. | 
|  | 1784 | // If the policy decides to close the app, we will get a channel removal event via | 
|  | 1785 | // unregisterInputChannel, and will clean up the connection that way. We are already not | 
|  | 1786 | // sending new pointers to the connection when it blocked, but focused events will continue to | 
|  | 1787 | // pile up. | 
|  | 1788 | ALOGW("Canceling events for %s because it is unresponsive", | 
|  | 1789 | connection->inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 1790 | if (connection->status == Connection::Status::NORMAL) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1791 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, | 
|  | 1792 | "application not responding"); | 
|  | 1793 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1794 | } | 
|  | 1795 | } | 
|  | 1796 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1797 | void InputDispatcher::resetNoFocusedWindowTimeoutLocked() { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 1798 | if (DEBUG_FOCUS) { | 
|  | 1799 | ALOGD("Resetting ANR timeouts."); | 
|  | 1800 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 |  | 
|  | 1802 | // Reset input target wait timeout. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1803 | mNoFocusedWindowTimeoutTime = std::nullopt; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1804 | mAwaitedFocusedApplication.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 | } | 
|  | 1806 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1807 | /** | 
|  | 1808 | * Get the display id that the given event should go to. If this event specifies a valid display id, | 
|  | 1809 | * then it should be dispatched to that display. Otherwise, the event goes to the focused display. | 
|  | 1810 | * Focused display is the display that the user most recently interacted with. | 
|  | 1811 | */ | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1812 | int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1813 | int32_t displayId; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1814 | switch (entry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1815 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1816 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 1817 | displayId = keyEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1818 | break; | 
|  | 1819 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1820 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 1821 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 1822 | displayId = motionEntry.displayId; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1823 | break; | 
|  | 1824 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1825 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1826 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1827 | case EventEntry::Type::FOCUS: | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 1828 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1829 | case EventEntry::Type::DEVICE_RESET: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1830 | case EventEntry::Type::SENSOR: | 
|  | 1831 | case EventEntry::Type::DRAG: { | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1832 | ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1833 | return ADISPLAY_ID_NONE; | 
|  | 1834 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1835 | } | 
|  | 1836 | return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId; | 
|  | 1837 | } | 
|  | 1838 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1839 | bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime, | 
|  | 1840 | const char* focusedWindowName) { | 
|  | 1841 | if (mAnrTracker.empty()) { | 
|  | 1842 | // already processed all events that we waited for | 
|  | 1843 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1844 | return false; | 
|  | 1845 | } | 
|  | 1846 |  | 
|  | 1847 | if (!mKeyIsWaitingForEventsTimeout.has_value()) { | 
|  | 1848 | // Start the timer | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 1849 | // Wait to send key because there are unprocessed events that may cause focus to change | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 1850 | mKeyIsWaitingForEventsTimeout = currentTime + | 
|  | 1851 | std::chrono::duration_cast<std::chrono::nanoseconds>(KEY_WAITING_FOR_EVENTS_TIMEOUT) | 
|  | 1852 | .count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1853 | return true; | 
|  | 1854 | } | 
|  | 1855 |  | 
|  | 1856 | // We still have pending events, and already started the timer | 
|  | 1857 | if (currentTime < *mKeyIsWaitingForEventsTimeout) { | 
|  | 1858 | return true; // Still waiting | 
|  | 1859 | } | 
|  | 1860 |  | 
|  | 1861 | // Waited too long, and some connection still hasn't processed all motions | 
|  | 1862 | // Just send the key to the focused window | 
|  | 1863 | ALOGW("Dispatching key to %s even though there are other unprocessed events", | 
|  | 1864 | focusedWindowName); | 
|  | 1865 | mKeyIsWaitingForEventsTimeout = std::nullopt; | 
|  | 1866 | return false; | 
|  | 1867 | } | 
|  | 1868 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1869 | InputEventInjectionResult InputDispatcher::findFocusedWindowTargetsLocked( | 
|  | 1870 | nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1871 | nsecs_t* nextWakeupTime) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1872 | std::string reason; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1873 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1874 | int32_t displayId = getTargetDisplayId(entry); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1875 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1876 | std::shared_ptr<InputApplicationHandle> focusedApplicationHandle = | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1877 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 1878 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1879 | // If there is no currently focused window and no focused application | 
|  | 1880 | // then drop the event. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1881 | if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) { | 
|  | 1882 | ALOGI("Dropping %s event because there is no focused window or focused application in " | 
|  | 1883 | "display %" PRId32 ".", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1884 | ftl::enum_string(entry.type).c_str(), displayId); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1885 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1886 | } | 
|  | 1887 |  | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 1888 | // Drop key events if requested by input feature | 
|  | 1889 | if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) { | 
|  | 1890 | return InputEventInjectionResult::FAILED; | 
|  | 1891 | } | 
|  | 1892 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1893 | // Compatibility behavior: raise ANR if there is a focused application, but no focused window. | 
|  | 1894 | // Only start counting when we have a focused event to dispatch. The ANR is canceled if we | 
|  | 1895 | // start interacting with another application via touch (app switch). This code can be removed | 
|  | 1896 | // if the "no focused window ANR" is moved to the policy. Input doesn't know whether | 
|  | 1897 | // an app is expected to have a focused window. | 
|  | 1898 | if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) { | 
|  | 1899 | if (!mNoFocusedWindowTimeoutTime.has_value()) { | 
|  | 1900 | // 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] | 1901 | std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout( | 
|  | 1902 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
|  | 1903 | mNoFocusedWindowTimeoutTime = currentTime + timeout.count(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1904 | mAwaitedFocusedApplication = focusedApplicationHandle; | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 1905 | mAwaitedApplicationDisplayId = displayId; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1906 | ALOGW("Waiting because no window has focus but %s may eventually add a " | 
|  | 1907 | "window when it finishes starting up. Will wait for %" PRId64 "ms", | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1908 | mAwaitedFocusedApplication->getName().c_str(), millis(timeout)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1909 | *nextWakeupTime = *mNoFocusedWindowTimeoutTime; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1910 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1911 | } else if (currentTime > *mNoFocusedWindowTimeoutTime) { | 
|  | 1912 | // Already raised ANR. Drop the event | 
|  | 1913 | ALOGE("Dropping %s event because there is no focused window", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 1914 | ftl::enum_string(entry.type).c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1915 | return InputEventInjectionResult::FAILED; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1916 | } else { | 
|  | 1917 | // Still waiting for the focused window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1918 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1919 | } | 
|  | 1920 | } | 
|  | 1921 |  | 
|  | 1922 | // we have a valid, non-null focused window | 
|  | 1923 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 1924 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1925 | // Verify targeted injection. | 
|  | 1926 | if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) { | 
|  | 1927 | ALOGW("Dropping injected event: %s", (*err).c_str()); | 
|  | 1928 | return InputEventInjectionResult::TARGET_MISMATCH; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | } | 
|  | 1930 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1931 | if (focusedWindowHandle->getInfo()->inputConfig.test( | 
|  | 1932 | WindowInfo::InputConfig::PAUSE_DISPATCHING)) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1933 | ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str()); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1934 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1935 | } | 
|  | 1936 |  | 
|  | 1937 | // If the event is a key event, then we must wait for all previous events to | 
|  | 1938 | // complete before delivering it because previous events may have the | 
|  | 1939 | // side-effect of transferring focus to a different window and we want to | 
|  | 1940 | // ensure that the following keys are sent to the new window. | 
|  | 1941 | // | 
|  | 1942 | // Suppose the user touches a button in a window then immediately presses "A". | 
|  | 1943 | // If the button causes a pop-up window to appear then we want to ensure that | 
|  | 1944 | // the "A" key is delivered to the new pop-up window.  This is because users | 
|  | 1945 | // often anticipate pending UI changes when typing on a keyboard. | 
|  | 1946 | // To obtain this behavior, we must serialize key events with respect to all | 
|  | 1947 | // prior input events. | 
|  | 1948 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 1949 | if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) { | 
|  | 1950 | *nextWakeupTime = *mKeyIsWaitingForEventsTimeout; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1951 | return InputEventInjectionResult::PENDING; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1952 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1953 | } | 
|  | 1954 |  | 
|  | 1955 | // Success!  Output targets. | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1956 | addWindowTargetLocked(focusedWindowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 1957 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, | 
|  | 1958 | BitSet32(0), inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1959 |  | 
|  | 1960 | // Done. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1961 | return InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1962 | } | 
|  | 1963 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1964 | /** | 
|  | 1965 | * Given a list of monitors, remove the ones we cannot find a connection for, and the ones | 
|  | 1966 | * that are currently unresponsive. | 
|  | 1967 | */ | 
| Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1968 | std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked( | 
|  | 1969 | const std::vector<Monitor>& monitors) const { | 
|  | 1970 | std::vector<Monitor> responsiveMonitors; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1971 | std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors), | 
| Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1972 | [this](const Monitor& monitor) REQUIRES(mLock) { | 
|  | 1973 | sp<Connection> connection = | 
|  | 1974 | getConnectionLocked(monitor.inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1975 | if (connection == nullptr) { | 
|  | 1976 | ALOGE("Could not find connection for monitor %s", | 
| Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 1977 | monitor.inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1978 | return false; | 
|  | 1979 | } | 
|  | 1980 | if (!connection->responsive) { | 
|  | 1981 | ALOGW("Unresponsive monitor %s will not get the new gesture", | 
|  | 1982 | connection->inputChannel->getName().c_str()); | 
|  | 1983 | return false; | 
|  | 1984 | } | 
|  | 1985 | return true; | 
|  | 1986 | }); | 
|  | 1987 | return responsiveMonitors; | 
|  | 1988 | } | 
|  | 1989 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1990 | InputEventInjectionResult InputDispatcher::findTouchedWindowTargetsLocked( | 
|  | 1991 | nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, | 
|  | 1992 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1993 | ATRACE_CALL(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1994 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1995 | // For security reasons, we defer updating the touch state until we are sure that | 
|  | 1996 | // event injection will be allowed. | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1997 | const int32_t displayId = entry.displayId; | 
|  | 1998 | const int32_t action = entry.action; | 
|  | 1999 | const int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2000 |  | 
|  | 2001 | // 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] | 2002 | InputEventInjectionResult injectionResult = InputEventInjectionResult::PENDING; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2003 | sp<WindowInfoHandle> newHoverWindowHandle(mLastHoverWindowHandle); | 
|  | 2004 | sp<WindowInfoHandle> newTouchedWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2005 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2006 | // Copy current touch state into tempTouchState. | 
|  | 2007 | // This state will be used to update mTouchStatesByDisplay at the end of this function. | 
|  | 2008 | // 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] | 2009 | const TouchState* oldState = nullptr; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2010 | TouchState tempTouchState; | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2011 | if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) { | 
|  | 2012 | oldState = &(it->second); | 
| Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 2013 | tempTouchState = *oldState; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2014 | } | 
|  | 2015 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2016 | bool isSplit = tempTouchState.split; | 
|  | 2017 | bool switchedDevice = tempTouchState.deviceId >= 0 && tempTouchState.displayId >= 0 && | 
|  | 2018 | (tempTouchState.deviceId != entry.deviceId || tempTouchState.source != entry.source || | 
|  | 2019 | tempTouchState.displayId != displayId); | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2020 |  | 
|  | 2021 | const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE || | 
|  | 2022 | maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2023 | maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); | 
|  | 2024 | const bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN || | 
|  | 2025 | maskedAction == AMOTION_EVENT_ACTION_SCROLL || isHoverAction); | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 2026 | const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2027 | bool wrongDevice = false; | 
|  | 2028 | if (newGesture) { | 
|  | 2029 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2030 | if (switchedDevice && tempTouchState.down && !down && !isHoverAction) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2031 | ALOGI("Dropping event because a pointer for a different device is already down " | 
|  | 2032 | "in display %" PRId32, | 
|  | 2033 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2034 | // TODO: test multiple simultaneous input streams. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2035 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2036 | switchedDevice = false; | 
|  | 2037 | wrongDevice = true; | 
|  | 2038 | goto Failed; | 
|  | 2039 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2040 | tempTouchState.reset(); | 
|  | 2041 | tempTouchState.down = down; | 
|  | 2042 | tempTouchState.deviceId = entry.deviceId; | 
|  | 2043 | tempTouchState.source = entry.source; | 
|  | 2044 | tempTouchState.displayId = displayId; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2045 | isSplit = false; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2046 | } else if (switchedDevice && maskedAction == AMOTION_EVENT_ACTION_MOVE) { | 
| Siarhei Vishniakou | f0007dd | 2020-04-13 11:40:37 -0700 | [diff] [blame] | 2047 | ALOGI("Dropping move event because a pointer for a different device is already active " | 
|  | 2048 | "in display %" PRId32, | 
|  | 2049 | displayId); | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2050 | // TODO: test multiple simultaneous input streams. | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2051 | injectionResult = InputEventInjectionResult::FAILED; | 
| Kevin Schoedel | 1eb587b | 2017-05-03 13:58:56 -0400 | [diff] [blame] | 2052 | switchedDevice = false; | 
|  | 2053 | wrongDevice = true; | 
|  | 2054 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2055 | } | 
|  | 2056 |  | 
|  | 2057 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { | 
|  | 2058 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ | 
|  | 2059 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2060 | int32_t x; | 
|  | 2061 | int32_t y; | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2062 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2063 | // Always dispatch mouse events to cursor position. | 
|  | 2064 | if (isFromMouse) { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2065 | x = int32_t(entry.xCursorPosition); | 
|  | 2066 | y = int32_t(entry.yCursorPosition); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2067 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2068 | x = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2069 | y = int32_t(entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2070 | } | 
| Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2071 | const bool isDown = maskedAction == AMOTION_EVENT_ACTION_DOWN; | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2072 | const bool isStylus = isPointerFromStylus(entry, pointerIndex); | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2073 | newTouchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2074 | isStylus, isDown /*addOutsideTargets*/); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2075 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2076 | // Handle the case where we did not find a window. | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2077 | if (newTouchedWindowHandle == nullptr) { | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2078 | ALOGD("No new touched window at (%" PRId32 ", %" PRId32 ") in display %" PRId32, x, y, | 
|  | 2079 | displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2080 | // 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] | 2081 | newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2082 | } | 
|  | 2083 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2084 | // Verify targeted injection. | 
|  | 2085 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { | 
|  | 2086 | ALOGW("Dropping injected touch event: %s", (*err).c_str()); | 
|  | 2087 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; | 
|  | 2088 | newTouchedWindowHandle = nullptr; | 
|  | 2089 | goto Failed; | 
|  | 2090 | } | 
|  | 2091 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2092 | // Figure out whether splitting will be allowed for this window. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2093 | if (newTouchedWindowHandle != nullptr) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2094 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
|  | 2095 | // New window supports splitting, but we should never split mouse events. | 
|  | 2096 | isSplit = !isFromMouse; | 
|  | 2097 | } else if (isSplit) { | 
|  | 2098 | // New window does not support splitting but we have already split events. | 
|  | 2099 | // Ignore the new window. | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2100 | newTouchedWindowHandle = nullptr; | 
|  | 2101 | } | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2102 | } else { | 
|  | 2103 | // No window is touched, so set split to true. This will allow the next pointer down to | 
| Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2104 | // be delivered to a new window which supports split touch. Pointers from a mouse device | 
|  | 2105 | // should never be split. | 
|  | 2106 | tempTouchState.split = isSplit = !isFromMouse; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2107 | } | 
|  | 2108 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2109 | // Update hover state. | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2110 | if (newTouchedWindowHandle != nullptr) { | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2111 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) { | 
|  | 2112 | newHoverWindowHandle = nullptr; | 
|  | 2113 | } else if (isHoverAction) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2114 | newHoverWindowHandle = newTouchedWindowHandle; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2115 | } | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2116 | } | 
|  | 2117 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2118 | std::vector<sp<WindowInfoHandle>> newTouchedWindows = | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2119 | findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus); | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2120 | if (newTouchedWindowHandle != nullptr) { | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2121 | // Process the foreground window first so that it is the first to receive the event. | 
|  | 2122 | newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle); | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2123 | } | 
|  | 2124 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2125 | if (newTouchedWindows.empty()) { | 
|  | 2126 | ALOGI("Dropping event because there is no touchable window at (%d, %d) on display %d.", | 
|  | 2127 | x, y, displayId); | 
|  | 2128 | injectionResult = InputEventInjectionResult::FAILED; | 
|  | 2129 | goto Failed; | 
|  | 2130 | } | 
|  | 2131 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2132 | for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) { | 
|  | 2133 | const WindowInfo& info = *windowHandle->getInfo(); | 
|  | 2134 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2135 | // Skip spy window targets that are not valid for targeted injection. | 
|  | 2136 | if (const auto err = verifyTargetedInjection(windowHandle, entry); err) { | 
|  | 2137 | continue; | 
|  | 2138 | } | 
|  | 2139 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2140 | if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2141 | ALOGI("Not sending touch event to %s because it is paused", | 
|  | 2142 | windowHandle->getName().c_str()); | 
|  | 2143 | continue; | 
|  | 2144 | } | 
|  | 2145 |  | 
|  | 2146 | // Ensure the window has a connection and the connection is responsive | 
|  | 2147 | const bool isResponsive = hasResponsiveConnectionLocked(*windowHandle); | 
|  | 2148 | if (!isResponsive) { | 
|  | 2149 | ALOGW("Not sending touch gesture to %s because it is not responsive", | 
|  | 2150 | windowHandle->getName().c_str()); | 
|  | 2151 | continue; | 
|  | 2152 | } | 
|  | 2153 |  | 
|  | 2154 | // Drop events that can't be trusted due to occlusion | 
|  | 2155 | if (mBlockUntrustedTouchesMode != BlockUntrustedTouchesMode::DISABLED) { | 
|  | 2156 | TouchOcclusionInfo occlusionInfo = | 
|  | 2157 | computeTouchOcclusionInfoLocked(windowHandle, x, y); | 
|  | 2158 | if (!isTouchTrustedLocked(occlusionInfo)) { | 
|  | 2159 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2160 | ALOGD("Stack of obscuring windows during untrusted touch (%d, %d):", x, y); | 
|  | 2161 | for (const auto& log : occlusionInfo.debugInfo) { | 
|  | 2162 | ALOGD("%s", log.c_str()); | 
|  | 2163 | } | 
|  | 2164 | } | 
|  | 2165 | sendUntrustedTouchCommandLocked(occlusionInfo.obscuringPackage); | 
|  | 2166 | if (mBlockUntrustedTouchesMode == BlockUntrustedTouchesMode::BLOCK) { | 
|  | 2167 | ALOGW("Dropping untrusted touch event due to %s/%d", | 
|  | 2168 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid); | 
|  | 2169 | continue; | 
|  | 2170 | } | 
|  | 2171 | } | 
|  | 2172 | } | 
|  | 2173 |  | 
|  | 2174 | // Drop touch events if requested by input feature | 
|  | 2175 | if (shouldDropInput(entry, windowHandle)) { | 
|  | 2176 | continue; | 
|  | 2177 | } | 
|  | 2178 |  | 
|  | 2179 | // Set target flags. | 
|  | 2180 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2181 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2182 | if (canReceiveForegroundTouches(*windowHandle->getInfo())) { | 
|  | 2183 | // There should only be one touched window that can be "foreground" for the pointer. | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2184 | targetFlags |= InputTarget::FLAG_FOREGROUND; | 
|  | 2185 | } | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2186 |  | 
|  | 2187 | if (isSplit) { | 
|  | 2188 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 2189 | } | 
|  | 2190 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { | 
|  | 2191 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
|  | 2192 | } else if (isWindowObscuredLocked(windowHandle)) { | 
|  | 2193 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 2194 | } | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2195 |  | 
|  | 2196 | // Update the temporary touch state. | 
|  | 2197 | BitSet32 pointerIds; | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2198 | pointerIds.markBit(entry.pointerProperties[pointerIndex].id); | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2199 |  | 
|  | 2200 | tempTouchState.addOrUpdateWindow(windowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2201 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2202 | } else { | 
|  | 2203 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ | 
|  | 2204 |  | 
|  | 2205 | // If the pointer is not currently down, then ignore the event. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2206 | if (!tempTouchState.down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2207 | if (DEBUG_FOCUS) { | 
|  | 2208 | ALOGD("Dropping event because the pointer is not down or we previously " | 
|  | 2209 | "dropped the pointer down event in display %" PRId32, | 
|  | 2210 | displayId); | 
|  | 2211 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2212 | injectionResult = InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2213 | goto Failed; | 
|  | 2214 | } | 
|  | 2215 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2216 | addDragEventLocked(entry); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2217 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2218 | // Check whether touches should slip outside of the current foreground window. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2219 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.pointerCount == 1 && | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2220 | tempTouchState.isSlippery()) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2221 | const int32_t x = int32_t(entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); | 
|  | 2222 | const 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] | 2223 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2224 | const bool isStylus = isPointerFromStylus(entry, 0 /*pointerIndex*/); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2225 | sp<WindowInfoHandle> oldTouchedWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2226 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2227 | newTouchedWindowHandle = | 
|  | 2228 | findTouchedWindowAtLocked(displayId, x, y, &tempTouchState, isStylus); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2229 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2230 | // Verify targeted injection. | 
|  | 2231 | if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) { | 
|  | 2232 | ALOGW("Dropping injected event: %s", (*err).c_str()); | 
|  | 2233 | injectionResult = os::InputEventInjectionResult::TARGET_MISMATCH; | 
|  | 2234 | newTouchedWindowHandle = nullptr; | 
|  | 2235 | goto Failed; | 
|  | 2236 | } | 
|  | 2237 |  | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 2238 | // Drop touch events if requested by input feature | 
|  | 2239 | if (newTouchedWindowHandle != nullptr && | 
|  | 2240 | shouldDropInput(entry, newTouchedWindowHandle)) { | 
|  | 2241 | newTouchedWindowHandle = nullptr; | 
|  | 2242 | } | 
|  | 2243 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2244 | if (oldTouchedWindowHandle != newTouchedWindowHandle && | 
|  | 2245 | oldTouchedWindowHandle != nullptr && newTouchedWindowHandle != nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2246 | if (DEBUG_FOCUS) { | 
|  | 2247 | ALOGD("Touch is slipping out of window %s into window %s in display %" PRId32, | 
|  | 2248 | oldTouchedWindowHandle->getName().c_str(), | 
|  | 2249 | newTouchedWindowHandle->getName().c_str(), displayId); | 
|  | 2250 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2251 | // Make a slippery exit from the old window. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2252 | tempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, | 
|  | 2253 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, | 
|  | 2254 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2255 |  | 
|  | 2256 | // Make a slippery entrance into the new window. | 
|  | 2257 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { | 
| Prabir Pradhan | 713bb3e | 2021-12-20 02:07:40 -0800 | [diff] [blame] | 2258 | isSplit = !isFromMouse; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2259 | } | 
|  | 2260 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2261 | int32_t targetFlags = InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; | 
|  | 2262 | if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) { | 
|  | 2263 | targetFlags |= InputTarget::FLAG_FOREGROUND; | 
|  | 2264 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2265 | if (isSplit) { | 
|  | 2266 | targetFlags |= InputTarget::FLAG_SPLIT; | 
|  | 2267 | } | 
|  | 2268 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { | 
|  | 2269 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2270 | } else if (isWindowObscuredLocked(newTouchedWindowHandle)) { | 
|  | 2271 | targetFlags |= InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2272 | } | 
|  | 2273 |  | 
|  | 2274 | BitSet32 pointerIds; | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2275 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2276 | tempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2277 | } | 
|  | 2278 | } | 
| Arthur Hung | 69d7557 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2279 |  | 
|  | 2280 | // Update the pointerIds for non-splittable when it received pointer down. | 
|  | 2281 | if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) { | 
|  | 2282 | // If no split, we suppose all touched windows should receive pointer down. | 
|  | 2283 | const int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2284 | for (size_t i = 0; i < tempTouchState.windows.size(); i++) { | 
|  | 2285 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 2286 | // Ignore drag window for it should just track one pointer. | 
|  | 2287 | if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) { | 
|  | 2288 | continue; | 
|  | 2289 | } | 
|  | 2290 | touchedWindow.pointerIds.markBit(entry.pointerProperties[pointerIndex].id); | 
|  | 2291 | } | 
|  | 2292 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2293 | } | 
|  | 2294 |  | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2295 | // Update dispatching for hover enter and exit. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2296 | if (newHoverWindowHandle != mLastHoverWindowHandle) { | 
| Prabir Pradhan | 3f90d31 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 2297 | // Let the previous window know that the hover sequence is over, unless we already did | 
|  | 2298 | // it when dispatching it as is to newTouchedWindowHandle. | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2299 | if (mLastHoverWindowHandle != nullptr && | 
|  | 2300 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT || | 
|  | 2301 | mLastHoverWindowHandle != newTouchedWindowHandle)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2302 | if (DEBUG_HOVER) { | 
|  | 2303 | ALOGD("Sending hover exit event to window %s.", | 
|  | 2304 | mLastHoverWindowHandle->getName().c_str()); | 
|  | 2305 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2306 | tempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, | 
|  | 2307 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2308 | } | 
|  | 2309 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2310 | // Let the new window know that the hover sequence is starting, unless we already did it | 
|  | 2311 | // when dispatching it as is to newTouchedWindowHandle. | 
|  | 2312 | if (newHoverWindowHandle != nullptr && | 
|  | 2313 | (maskedAction != AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2314 | newHoverWindowHandle != newTouchedWindowHandle)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2315 | if (DEBUG_HOVER) { | 
|  | 2316 | ALOGD("Sending hover enter event to window %s.", | 
|  | 2317 | newHoverWindowHandle->getName().c_str()); | 
|  | 2318 | } | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2319 | tempTouchState.addOrUpdateWindow(newHoverWindowHandle, | 
|  | 2320 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, | 
|  | 2321 | BitSet32(0)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2322 | } | 
|  | 2323 | } | 
|  | 2324 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2325 | // Ensure that we have at least one foreground window or at least one window that cannot be a | 
|  | 2326 | // foreground target. If we only have windows that are not receiving foreground touches (e.g. we | 
|  | 2327 | // only have windows getting ACTION_OUTSIDE), then drop the event, because there is no window | 
|  | 2328 | // that is actually receiving the entire gesture. | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2329 | if (std::none_of(tempTouchState.windows.begin(), tempTouchState.windows.end(), | 
|  | 2330 | [](const TouchedWindow& touchedWindow) { | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 2331 | return !canReceiveForegroundTouches( | 
|  | 2332 | *touchedWindow.windowHandle->getInfo()) || | 
|  | 2333 | (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) != 0; | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2334 | })) { | 
| Siarhei Vishniakou | 1fb1891 | 2022-03-08 10:31:39 -0800 | [diff] [blame] | 2335 | ALOGI("Dropping event because there is no touched window on display %d to receive it: %s", | 
|  | 2336 | displayId, entry.getDescription().c_str()); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2337 | injectionResult = InputEventInjectionResult::FAILED; | 
|  | 2338 | goto Failed; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2339 | } | 
|  | 2340 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 2341 | // Ensure that all touched windows are valid for injection. | 
|  | 2342 | if (entry.injectionState != nullptr) { | 
|  | 2343 | std::string errs; | 
|  | 2344 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
|  | 2345 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 2346 | // Allow ACTION_OUTSIDE events generated by targeted injection to be | 
|  | 2347 | // dispatched to any uid, since the coords will be zeroed out later. | 
|  | 2348 | continue; | 
|  | 2349 | } | 
|  | 2350 | const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry); | 
|  | 2351 | if (err) errs += "\n  - " + *err; | 
|  | 2352 | } | 
|  | 2353 | if (!errs.empty()) { | 
|  | 2354 | ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " | 
|  | 2355 | "%d:%s", | 
|  | 2356 | *entry.injectionState->targetUid, errs.c_str()); | 
|  | 2357 | injectionResult = InputEventInjectionResult::TARGET_MISMATCH; | 
|  | 2358 | goto Failed; | 
|  | 2359 | } | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2360 | } | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2361 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2362 | // Check whether windows listening for outside touches are owned by the same UID. If it is | 
|  | 2363 | // set the policy flag that we will not reveal coordinate information to this window. | 
|  | 2364 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2365 | sp<WindowInfoHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2366 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2367 | if (foregroundWindowHandle) { | 
|  | 2368 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2369 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2370 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2371 | sp<WindowInfoHandle> windowInfoHandle = touchedWindow.windowHandle; | 
|  | 2372 | if (windowInfoHandle->getInfo()->ownerUid != foregroundWindowUid) { | 
|  | 2373 | tempTouchState.addOrUpdateWindow(windowInfoHandle, | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2374 | InputTarget::FLAG_ZERO_COORDS, | 
|  | 2375 | BitSet32(0)); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2376 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2377 | } | 
|  | 2378 | } | 
|  | 2379 | } | 
|  | 2380 | } | 
|  | 2381 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2382 | // If this is the first pointer going down and the touched window has a wallpaper | 
|  | 2383 | // then also add the touched wallpaper windows so they are locked in for the duration | 
|  | 2384 | // of the touch gesture. | 
|  | 2385 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper | 
|  | 2386 | // engine only supports touch events.  We would need to add a mechanism similar | 
|  | 2387 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. | 
|  | 2388 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2389 | sp<WindowInfoHandle> foregroundWindowHandle = | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2390 | tempTouchState.getFirstForegroundWindowHandle(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2391 | if (foregroundWindowHandle && | 
|  | 2392 | foregroundWindowHandle->getInfo()->inputConfig.test( | 
|  | 2393 | WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2394 | const std::vector<sp<WindowInfoHandle>>& windowHandles = | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 2395 | getWindowHandlesLocked(displayId); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2396 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
|  | 2397 | const WindowInfo* info = windowHandle->getInfo(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2398 | if (info->displayId == displayId && | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2399 | windowHandle->getInfo()->inputConfig.test( | 
|  | 2400 | WindowInfo::InputConfig::IS_WALLPAPER)) { | 
| Arthur Hung | 69d7557 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2401 | BitSet32 pointerIds; | 
|  | 2402 | pointerIds.markBit(entry.pointerProperties[0].id); | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2403 | tempTouchState | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2404 | .addOrUpdateWindow(windowHandle, | 
|  | 2405 | InputTarget::FLAG_WINDOW_IS_OBSCURED | | 
|  | 2406 | InputTarget:: | 
|  | 2407 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED | | 
|  | 2408 | InputTarget::FLAG_DISPATCH_AS_IS, | 
| Arthur Hung | 69d7557 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2409 | pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2410 | } | 
|  | 2411 | } | 
|  | 2412 | } | 
|  | 2413 | } | 
|  | 2414 |  | 
|  | 2415 | // Success!  Output targets. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2416 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2417 |  | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2418 | for (const TouchedWindow& touchedWindow : tempTouchState.windows) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2419 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2420 | touchedWindow.pointerIds, inputTargets); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2421 | } | 
|  | 2422 |  | 
|  | 2423 | // Drop the outside or hover touch windows since we will not care about them | 
|  | 2424 | // in the next iteration. | 
| Siarhei Vishniakou | 33eceeb | 2020-03-24 19:50:03 -0700 | [diff] [blame] | 2425 | tempTouchState.filterNonAsIsTouchWindows(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2426 |  | 
|  | 2427 | Failed: | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2428 | // Update final pieces of touch state if the injector had permission. | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2429 | if (!wrongDevice) { | 
|  | 2430 | if (switchedDevice) { | 
|  | 2431 | if (DEBUG_FOCUS) { | 
|  | 2432 | ALOGD("Conflicting pointer actions: Switched to a different device."); | 
|  | 2433 | } | 
|  | 2434 | *outConflictingPointerActions = true; | 
|  | 2435 | } | 
|  | 2436 |  | 
|  | 2437 | if (isHoverAction) { | 
|  | 2438 | // Started hovering, therefore no longer down. | 
|  | 2439 | if (oldState && oldState->down) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2440 | if (DEBUG_FOCUS) { | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2441 | ALOGD("Conflicting pointer actions: Hover received while pointer was " | 
|  | 2442 | "down."); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2443 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2444 | *outConflictingPointerActions = true; | 
|  | 2445 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2446 | tempTouchState.reset(); | 
|  | 2447 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || | 
|  | 2448 | maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { | 
|  | 2449 | tempTouchState.deviceId = entry.deviceId; | 
|  | 2450 | tempTouchState.source = entry.source; | 
|  | 2451 | tempTouchState.displayId = displayId; | 
|  | 2452 | } | 
|  | 2453 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP || | 
|  | 2454 | maskedAction == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2455 | // All pointers up or canceled. | 
|  | 2456 | tempTouchState.reset(); | 
|  | 2457 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 2458 | // First pointer went down. | 
|  | 2459 | if (oldState && oldState->down) { | 
|  | 2460 | if (DEBUG_FOCUS) { | 
|  | 2461 | ALOGD("Conflicting pointer actions: Down received while already down."); | 
|  | 2462 | } | 
|  | 2463 | *outConflictingPointerActions = true; | 
|  | 2464 | } | 
|  | 2465 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
|  | 2466 | // One pointer went up. | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2467 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 2468 | uint32_t pointerId = entry.pointerProperties[pointerIndex].id; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2469 |  | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2470 | for (size_t i = 0; i < tempTouchState.windows.size();) { | 
|  | 2471 | TouchedWindow& touchedWindow = tempTouchState.windows[i]; | 
|  | 2472 | touchedWindow.pointerIds.clearBit(pointerId); | 
|  | 2473 | if (touchedWindow.pointerIds.isEmpty()) { | 
|  | 2474 | tempTouchState.windows.erase(tempTouchState.windows.begin() + i); | 
|  | 2475 | continue; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2476 | } | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 2477 | i += 1; | 
|  | 2478 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2479 | } | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 2480 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2481 | // Save changes unless the action was scroll in which case the temporary touch | 
|  | 2482 | // state was only valid for this one action. | 
|  | 2483 | if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { | 
|  | 2484 | if (tempTouchState.displayId >= 0) { | 
|  | 2485 | mTouchStatesByDisplay[displayId] = tempTouchState; | 
|  | 2486 | } else { | 
|  | 2487 | mTouchStatesByDisplay.erase(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2488 | } | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2489 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2490 |  | 
| Siarhei Vishniakou | 767917f | 2020-03-24 20:49:09 -0700 | [diff] [blame] | 2491 | // Update hover state. | 
|  | 2492 | mLastHoverWindowHandle = newHoverWindowHandle; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2493 | } | 
|  | 2494 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2495 | return injectionResult; | 
|  | 2496 | } | 
|  | 2497 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2498 | void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) { | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2499 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we | 
|  | 2500 | // have an explicit reason to support it. | 
|  | 2501 | constexpr bool isStylus = false; | 
|  | 2502 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2503 | const sp<WindowInfoHandle> dropWindow = | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 2504 | findTouchedWindowAtLocked(displayId, x, y, nullptr /*touchState*/, isStylus, | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 2505 | false /*addOutsideTargets*/, true /*ignoreDragWindow*/); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2506 | if (dropWindow) { | 
|  | 2507 | vec2 local = dropWindow->getInfo()->transform.transform(x, y); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2508 | sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 2509 | } else { | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2510 | ALOGW("No window found when drop."); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2511 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2512 | } | 
|  | 2513 | mDragState.reset(); | 
|  | 2514 | } | 
|  | 2515 |  | 
|  | 2516 | void InputDispatcher::addDragEventLocked(const MotionEntry& entry) { | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 2517 | if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2518 | return; | 
|  | 2519 | } | 
|  | 2520 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2521 | if (!mDragState->isStartDrag) { | 
|  | 2522 | mDragState->isStartDrag = true; | 
|  | 2523 | mDragState->isStylusButtonDownAtStart = | 
|  | 2524 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2525 | } | 
|  | 2526 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2527 | // Find the pointer index by id. | 
|  | 2528 | int32_t pointerIndex = 0; | 
|  | 2529 | for (; static_cast<uint32_t>(pointerIndex) < entry.pointerCount; pointerIndex++) { | 
|  | 2530 | const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex]; | 
|  | 2531 | if (pointerProperties.id == mDragState->pointerId) { | 
|  | 2532 | break; | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2533 | } | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2534 | } | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2535 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2536 | if (uint32_t(pointerIndex) == entry.pointerCount) { | 
|  | 2537 | LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2538 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 2539 | mDragState.reset(); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 2540 | return; | 
|  | 2541 | } | 
|  | 2542 |  | 
|  | 2543 | const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK; | 
|  | 2544 | const int32_t x = entry.pointerCoords[pointerIndex].getX(); | 
|  | 2545 | const int32_t y = entry.pointerCoords[pointerIndex].getY(); | 
|  | 2546 |  | 
|  | 2547 | switch (maskedAction) { | 
|  | 2548 | case AMOTION_EVENT_ACTION_MOVE: { | 
|  | 2549 | // Handle the special case : stylus button no longer pressed. | 
|  | 2550 | bool isStylusButtonDown = | 
|  | 2551 | (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0; | 
|  | 2552 | if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) { | 
|  | 2553 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2554 | return; | 
|  | 2555 | } | 
|  | 2556 |  | 
|  | 2557 | // Prevent stylus interceptor windows from affecting drag and drop behavior for now, | 
|  | 2558 | // until we have an explicit reason to support it. | 
|  | 2559 | constexpr bool isStylus = false; | 
|  | 2560 |  | 
|  | 2561 | const sp<WindowInfoHandle> hoverWindowHandle = | 
|  | 2562 | findTouchedWindowAtLocked(entry.displayId, x, y, nullptr /*touchState*/, | 
|  | 2563 | isStylus, false /*addOutsideTargets*/, | 
|  | 2564 | true /*ignoreDragWindow*/); | 
|  | 2565 | // enqueue drag exit if needed. | 
|  | 2566 | if (hoverWindowHandle != mDragState->dragHoverWindowHandle && | 
|  | 2567 | !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) { | 
|  | 2568 | if (mDragState->dragHoverWindowHandle != nullptr) { | 
|  | 2569 | enqueueDragEventLocked(mDragState->dragHoverWindowHandle, true /*isExiting*/, x, | 
|  | 2570 | y); | 
|  | 2571 | } | 
|  | 2572 | mDragState->dragHoverWindowHandle = hoverWindowHandle; | 
|  | 2573 | } | 
|  | 2574 | // enqueue drag location if needed. | 
|  | 2575 | if (hoverWindowHandle != nullptr) { | 
|  | 2576 | enqueueDragEventLocked(hoverWindowHandle, false /*isExiting*/, x, y); | 
|  | 2577 | } | 
|  | 2578 | break; | 
|  | 2579 | } | 
|  | 2580 |  | 
|  | 2581 | case AMOTION_EVENT_ACTION_POINTER_UP: | 
|  | 2582 | if (getMotionEventActionPointerIndex(entry.action) != pointerIndex) { | 
|  | 2583 | break; | 
|  | 2584 | } | 
|  | 2585 | // The drag pointer is up. | 
|  | 2586 | [[fallthrough]]; | 
|  | 2587 | case AMOTION_EVENT_ACTION_UP: | 
|  | 2588 | finishDragAndDrop(entry.displayId, x, y); | 
|  | 2589 | break; | 
|  | 2590 | case AMOTION_EVENT_ACTION_CANCEL: { | 
|  | 2591 | ALOGD("Receiving cancel when drag and drop."); | 
|  | 2592 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
|  | 2593 | mDragState.reset(); | 
|  | 2594 | break; | 
|  | 2595 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 2596 | } | 
|  | 2597 | } | 
|  | 2598 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2599 | void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2600 | int32_t targetFlags, BitSet32 pointerIds, | 
|  | 2601 | std::vector<InputTarget>& inputTargets) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2602 | std::vector<InputTarget>::iterator it = | 
|  | 2603 | std::find_if(inputTargets.begin(), inputTargets.end(), | 
|  | 2604 | [&windowHandle](const InputTarget& inputTarget) { | 
|  | 2605 | return inputTarget.inputChannel->getConnectionToken() == | 
|  | 2606 | windowHandle->getToken(); | 
|  | 2607 | }); | 
| Chavi Weingarten | 97b8eec | 2020-01-09 18:09:08 +0000 | [diff] [blame] | 2608 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2609 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2610 |  | 
|  | 2611 | if (it == inputTargets.end()) { | 
|  | 2612 | InputTarget inputTarget; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 2613 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 2614 | getInputChannelLocked(windowHandle->getToken()); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2615 | if (inputChannel == nullptr) { | 
|  | 2616 | ALOGW("Window %s already unregistered input channel", windowHandle->getName().c_str()); | 
|  | 2617 | return; | 
|  | 2618 | } | 
|  | 2619 | inputTarget.inputChannel = inputChannel; | 
|  | 2620 | inputTarget.flags = targetFlags; | 
|  | 2621 | inputTarget.globalScaleFactor = windowInfo->globalScaleFactor; | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2622 | const auto& displayInfoIt = mDisplayInfos.find(windowInfo->displayId); | 
|  | 2623 | if (displayInfoIt != mDisplayInfos.end()) { | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 2624 | inputTarget.displayTransform = displayInfoIt->second.transform; | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2625 | } else { | 
| Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 2626 | ALOGE("DisplayInfo not found for window on display: %d", windowInfo->displayId); | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 2627 | } | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2628 | inputTargets.push_back(inputTarget); | 
|  | 2629 | it = inputTargets.end() - 1; | 
|  | 2630 | } | 
|  | 2631 |  | 
|  | 2632 | ALOG_ASSERT(it->flags == targetFlags); | 
|  | 2633 | ALOG_ASSERT(it->globalScaleFactor == windowInfo->globalScaleFactor); | 
|  | 2634 |  | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2635 | it->addPointers(pointerIds, windowInfo->transform); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2636 | } | 
|  | 2637 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2638 | void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, | 
| Prabir Pradhan | 0a99c92 | 2021-09-03 08:27:53 -0700 | [diff] [blame] | 2639 | int32_t displayId) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2640 | auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId); | 
|  | 2641 | if (monitorsIt == mGlobalMonitorsByDisplay.end()) return; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2642 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2643 | for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) { | 
|  | 2644 | InputTarget target; | 
|  | 2645 | target.inputChannel = monitor.inputChannel; | 
|  | 2646 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 2647 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { | 
|  | 2648 | target.displayTransform = it->second.transform; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2649 | } | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 2650 | target.setDefaultPointerTransform(target.displayTransform); | 
|  | 2651 | inputTargets.push_back(target); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2652 | } | 
|  | 2653 | } | 
|  | 2654 |  | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2655 | /** | 
|  | 2656 | * Indicate whether one window handle should be considered as obscuring | 
|  | 2657 | * another window handle. We only check a few preconditions. Actually | 
|  | 2658 | * checking the bounds is left to the caller. | 
|  | 2659 | */ | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2660 | static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle, | 
|  | 2661 | const sp<WindowInfoHandle>& otherHandle) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2662 | // Compare by token so cloned layers aren't counted | 
|  | 2663 | if (haveSameToken(windowHandle, otherHandle)) { | 
|  | 2664 | return false; | 
|  | 2665 | } | 
|  | 2666 | auto info = windowHandle->getInfo(); | 
|  | 2667 | auto otherInfo = otherHandle->getInfo(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2668 | if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2669 | return false; | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2670 | } else if (otherInfo->alpha == 0 && | 
|  | 2671 | otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) { | 
| Bernardo Rufino | 653d2e0 | 2020-10-20 17:32:40 +0000 | [diff] [blame] | 2672 | // Those act as if they were invisible, so we don't need to flag them. | 
|  | 2673 | // We do want to potentially flag touchable windows even if they have 0 | 
|  | 2674 | // opacity, since they can consume touches and alter the effects of the | 
|  | 2675 | // user interaction (eg. apps that rely on | 
|  | 2676 | // FLAG_WINDOW_IS_PARTIALLY_OBSCURED should still be told about those | 
|  | 2677 | // windows), hence we also check for FLAG_NOT_TOUCHABLE. | 
|  | 2678 | return false; | 
| Bernardo Rufino | 8007daf | 2020-09-22 09:40:01 +0000 | [diff] [blame] | 2679 | } else if (info->ownerUid == otherInfo->ownerUid) { | 
|  | 2680 | // If ownerUid is the same we don't generate occlusion events as there | 
|  | 2681 | // is no security boundary within an uid. | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2682 | return false; | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2683 | } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2684 | return false; | 
|  | 2685 | } else if (otherInfo->displayId != info->displayId) { | 
|  | 2686 | return false; | 
|  | 2687 | } | 
|  | 2688 | return true; | 
|  | 2689 | } | 
|  | 2690 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2691 | /** | 
|  | 2692 | * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is | 
|  | 2693 | * untrusted, one should check: | 
|  | 2694 | * | 
|  | 2695 | * 1. If result.hasBlockingOcclusion is true. | 
|  | 2696 | *    If it's, it means the touch should be blocked due to a window with occlusion mode of | 
|  | 2697 | *    BLOCK_UNTRUSTED. | 
|  | 2698 | * | 
|  | 2699 | * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch. | 
|  | 2700 | *    If it is (and 1 is false), then the touch should be blocked because a stack of windows | 
|  | 2701 | *    (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed | 
|  | 2702 | *    obscuring opacity above the threshold. Note that if there was no window of occlusion mode | 
|  | 2703 | *    USE_OPACITY, result.obscuringOpacity would've been 0 and since | 
|  | 2704 | *    mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true. | 
|  | 2705 | * | 
|  | 2706 | * If neither of those is true, then it means the touch can be allowed. | 
|  | 2707 | */ | 
|  | 2708 | InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2709 | const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const { | 
|  | 2710 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2711 | int32_t displayId = windowInfo->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2712 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2713 | TouchOcclusionInfo info; | 
|  | 2714 | info.hasBlockingOcclusion = false; | 
|  | 2715 | info.obscuringOpacity = 0; | 
|  | 2716 | info.obscuringUid = -1; | 
|  | 2717 | std::map<int32_t, float> opacityByUid; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2718 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2719 | if (windowHandle == otherHandle) { | 
|  | 2720 | break; // All future windows are below us. Exit early. | 
|  | 2721 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2722 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Bernardo Rufino | 1ff9d59 | 2021-01-18 16:58:57 +0000 | [diff] [blame] | 2723 | if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) && | 
|  | 2724 | !haveSameApplicationToken(windowInfo, otherInfo)) { | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2725 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2726 | info.debugInfo.push_back( | 
|  | 2727 | dumpWindowForTouchOcclusion(otherInfo, /* isTouchedWindow */ false)); | 
|  | 2728 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2729 | // canBeObscuredBy() has returned true above, which means this window is untrusted, so | 
|  | 2730 | // we perform the checks below to see if the touch can be propagated or not based on the | 
|  | 2731 | // window's touch occlusion mode | 
|  | 2732 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) { | 
|  | 2733 | info.hasBlockingOcclusion = true; | 
|  | 2734 | info.obscuringUid = otherInfo->ownerUid; | 
|  | 2735 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2736 | break; | 
|  | 2737 | } | 
|  | 2738 | if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { | 
|  | 2739 | uint32_t uid = otherInfo->ownerUid; | 
|  | 2740 | float opacity = | 
|  | 2741 | (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; | 
|  | 2742 | // Given windows A and B: | 
|  | 2743 | // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)] | 
|  | 2744 | opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha); | 
|  | 2745 | opacityByUid[uid] = opacity; | 
|  | 2746 | if (opacity > info.obscuringOpacity) { | 
|  | 2747 | info.obscuringOpacity = opacity; | 
|  | 2748 | info.obscuringUid = uid; | 
|  | 2749 | info.obscuringPackage = otherInfo->packageName; | 
|  | 2750 | } | 
|  | 2751 | } | 
|  | 2752 | } | 
|  | 2753 | } | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2754 | if (DEBUG_TOUCH_OCCLUSION) { | 
|  | 2755 | info.debugInfo.push_back( | 
|  | 2756 | dumpWindowForTouchOcclusion(windowInfo, /* isTouchedWindow */ true)); | 
|  | 2757 | } | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2758 | return info; | 
|  | 2759 | } | 
|  | 2760 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2761 | std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2762 | bool isTouchedWindow) const { | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2763 | return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " | 
|  | 2764 | "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 | 
|  | 2765 | "], touchableRegion=%s, window={%s}, inputConfig={%s}, " | 
|  | 2766 | "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2767 | isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), | 
|  | 2768 | info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), | 
|  | 2769 | info->alpha, info->frameLeft, info->frameTop, info->frameRight, | 
|  | 2770 | info->frameBottom, dumpRegion(info->touchableRegion).c_str(), | 
|  | 2771 | info->name.c_str(), info->inputConfig.string().c_str(), | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2772 | toString(info->token != nullptr), info->applicationInfo.name.c_str(), | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 2773 | toString(info->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 4bae0ac | 2020-10-14 18:33:46 +0000 | [diff] [blame] | 2774 | } | 
|  | 2775 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 2776 | bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { | 
|  | 2777 | if (occlusionInfo.hasBlockingOcclusion) { | 
|  | 2778 | ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), | 
|  | 2779 | occlusionInfo.obscuringUid); | 
|  | 2780 | return false; | 
|  | 2781 | } | 
|  | 2782 | if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { | 
|  | 2783 | ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " | 
|  | 2784 | "%.2f, maximum allowed = %.2f)", | 
|  | 2785 | occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, | 
|  | 2786 | occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); | 
|  | 2787 | return false; | 
|  | 2788 | } | 
|  | 2789 | return true; | 
|  | 2790 | } | 
|  | 2791 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2792 | bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2793 | int32_t x, int32_t y) const { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2794 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2795 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2796 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2797 | if (windowHandle == otherHandle) { | 
|  | 2798 | break; // All future windows are below us. Exit early. | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2799 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2800 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2801 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2802 | otherInfo->frameContainsPoint(x, y)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2803 | return true; | 
|  | 2804 | } | 
|  | 2805 | } | 
|  | 2806 | return false; | 
|  | 2807 | } | 
|  | 2808 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2809 | bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2810 | int32_t displayId = windowHandle->getInfo()->displayId; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2811 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
|  | 2812 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
|  | 2813 | for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2814 | if (windowHandle == otherHandle) { | 
|  | 2815 | break; // All future windows are below us. Exit early. | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2816 | } | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2817 | const WindowInfo* otherInfo = otherHandle->getInfo(); | 
| Robert Carr | c9bf1d3 | 2020-04-13 17:21:08 -0700 | [diff] [blame] | 2818 | if (canBeObscuredBy(windowHandle, otherHandle) && | 
| mincheli | f28cc4e | 2020-03-19 11:18:11 +0800 | [diff] [blame] | 2819 | otherInfo->overlaps(windowInfo)) { | 
| Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 2820 | return true; | 
|  | 2821 | } | 
|  | 2822 | } | 
|  | 2823 | return false; | 
|  | 2824 | } | 
|  | 2825 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 2826 | std::string InputDispatcher::getApplicationWindowLabel( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2827 | const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) { | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2828 | if (applicationHandle != nullptr) { | 
|  | 2829 | if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2830 | return applicationHandle->getName() + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2831 | } else { | 
|  | 2832 | return applicationHandle->getName(); | 
|  | 2833 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 2834 | } else if (windowHandle != nullptr) { | 
| Siarhei Vishniakou | 850ce12 | 2020-05-26 22:39:43 -0700 | [diff] [blame] | 2835 | return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2836 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 2837 | return "<unknown application or window>"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2838 | } | 
|  | 2839 | } | 
|  | 2840 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2841 | void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2842 | if (!isUserActivityEvent(eventEntry)) { | 
|  | 2843 | // Not poking user activity if the event type does not represent a user activity | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2844 | return; | 
|  | 2845 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2846 | int32_t displayId = getTargetDisplayId(eventEntry); | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2847 | sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2848 | if (focusedWindowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 2849 | const WindowInfo* info = focusedWindowHandle->getInfo(); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 2850 | if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2851 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2852 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.c_str()); | 
|  | 2853 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2854 | return; | 
|  | 2855 | } | 
|  | 2856 | } | 
|  | 2857 |  | 
|  | 2858 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2859 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2860 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2861 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
|  | 2862 | if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2863 | return; | 
|  | 2864 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2865 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2866 | if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2867 | eventType = USER_ACTIVITY_EVENT_TOUCH; | 
|  | 2868 | } | 
|  | 2869 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2870 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2871 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2872 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 2873 | if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2874 | return; | 
|  | 2875 | } | 
|  | 2876 | eventType = USER_ACTIVITY_EVENT_BUTTON; | 
|  | 2877 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2878 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 2879 | default: { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2880 | LOG_ALWAYS_FATAL("%s events are not user activity", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2881 | ftl::enum_string(eventEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 2882 | break; | 
|  | 2883 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2884 | } | 
|  | 2885 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 2886 | auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]() | 
|  | 2887 | REQUIRES(mLock) { | 
|  | 2888 | scoped_unlock unlock(mLock); | 
|  | 2889 | mPolicy->pokeUserActivity(eventTime, eventType, displayId); | 
|  | 2890 | }; | 
|  | 2891 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2892 | } | 
|  | 2893 |  | 
|  | 2894 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2895 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2896 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2897 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2898 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2899 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2900 | StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2901 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2902 | ATRACE_NAME(message.c_str()); | 
|  | 2903 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2904 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2905 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " | 
|  | 2906 | "globalScaleFactor=%f, pointerIds=0x%x %s", | 
|  | 2907 | connection->getInputChannelName().c_str(), inputTarget.flags, | 
|  | 2908 | inputTarget.globalScaleFactor, inputTarget.pointerIds.value, | 
|  | 2909 | inputTarget.getPointerInfoString().c_str()); | 
|  | 2910 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2911 |  | 
|  | 2912 | // Skip this event if the connection status is not normal. | 
|  | 2913 | // We don't want to enqueue additional outbound events if the connection is broken. | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2914 | if (connection->status != Connection::Status::NORMAL) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2915 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 2916 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 2917 | connection->getInputChannelName().c_str(), | 
|  | 2918 | ftl::enum_string(connection->status).c_str()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 2919 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2920 | return; | 
|  | 2921 | } | 
|  | 2922 |  | 
|  | 2923 | // Split a motion event if needed. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2924 | if (inputTarget.flags & InputTarget::FLAG_SPLIT) { | 
|  | 2925 | LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION, | 
|  | 2926 | "Entry type %s should not have FLAG_SPLIT", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 2927 | ftl::enum_string(eventEntry->type).c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2928 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2929 | const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2930 | if (inputTarget.pointerIds.count() != originalMotionEntry.pointerCount) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2931 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2932 | splitMotionEvent(originalMotionEntry, inputTarget.pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2933 | if (!splitMotionEntry) { | 
|  | 2934 | return; // split event was dropped | 
|  | 2935 | } | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 2936 | if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 2937 | std::string reason = std::string("reason=pointer cancel on split window"); | 
|  | 2938 | android_log_event_list(LOGTAG_INPUT_CANCEL) | 
|  | 2939 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; | 
|  | 2940 | } | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2941 | if (DEBUG_FOCUS) { | 
|  | 2942 | ALOGD("channel '%s' ~ Split motion event.", | 
|  | 2943 | connection->getInputChannelName().c_str()); | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 2944 | logOutboundMotionDetails("  ", *splitMotionEntry); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 2945 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2946 | enqueueDispatchEntriesLocked(currentTime, connection, std::move(splitMotionEntry), | 
|  | 2947 | inputTarget); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2948 | return; | 
|  | 2949 | } | 
|  | 2950 | } | 
|  | 2951 |  | 
|  | 2952 | // Not splitting.  Enqueue dispatch entries for the event as is. | 
|  | 2953 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); | 
|  | 2954 | } | 
|  | 2955 |  | 
|  | 2956 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2957 | const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2958 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2959 | const InputTarget& inputTarget) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2960 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2961 | std::string message = | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2962 | StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")", | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2963 | connection->getInputChannelName().c_str(), eventEntry->id); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2964 | ATRACE_NAME(message.c_str()); | 
|  | 2965 | } | 
|  | 2966 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2967 | bool wasEmpty = connection->outboundQueue.empty(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2968 |  | 
|  | 2969 | // Enqueue dispatch entries for the requested modes. | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2970 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2971 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2972 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2973 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2974 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2975 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2976 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2977 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2978 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2979 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 2980 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2981 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2982 |  | 
|  | 2983 | // If the outbound queue was previously empty, start the dispatch cycle going. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 2984 | if (wasEmpty && !connection->outboundQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 2985 | startDispatchCycleLocked(currentTime, connection); | 
|  | 2986 | } | 
|  | 2987 | } | 
|  | 2988 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2989 | void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connection, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 2990 | std::shared_ptr<EventEntry> eventEntry, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2991 | const InputTarget& inputTarget, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2992 | int32_t dispatchMode) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2993 | if (ATRACE_ENABLED()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 2994 | std::string message = StringPrintf("enqueueDispatchEntry(inputChannel=%s, dispatchMode=%s)", | 
|  | 2995 | connection->getInputChannelName().c_str(), | 
|  | 2996 | dispatchModeToString(dispatchMode).c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 2997 | ATRACE_NAME(message.c_str()); | 
|  | 2998 | } | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 2999 | int32_t inputTargetFlags = inputTarget.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3000 | if (!(inputTargetFlags & dispatchMode)) { | 
|  | 3001 | return; | 
|  | 3002 | } | 
|  | 3003 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; | 
|  | 3004 |  | 
|  | 3005 | // This is a new event. | 
|  | 3006 | // Enqueue a new dispatch entry onto the outbound queue for this connection. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3007 | std::unique_ptr<DispatchEntry> dispatchEntry = | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3008 | createDispatchEntry(inputTarget, eventEntry, inputTargetFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3009 |  | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3010 | // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a | 
|  | 3011 | // different EventEntry than what was passed in. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3012 | EventEntry& newEntry = *(dispatchEntry->eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3013 | // Apply target flags and update the connection's input state. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3014 | switch (newEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3015 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3016 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3017 | dispatchEntry->resolvedEventId = keyEntry.id; | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3018 | dispatchEntry->resolvedAction = keyEntry.action; | 
|  | 3019 | dispatchEntry->resolvedFlags = keyEntry.flags; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3020 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3021 | if (!connection->inputState.trackKey(keyEntry, dispatchEntry->resolvedAction, | 
|  | 3022 | dispatchEntry->resolvedFlags)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3023 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3024 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key " | 
|  | 3025 | "event", | 
|  | 3026 | connection->getInputChannelName().c_str()); | 
|  | 3027 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3028 | return; // skip the inconsistent event | 
|  | 3029 | } | 
|  | 3030 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3031 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3032 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3033 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3034 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(newEntry); | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3035 | // Assign a default value to dispatchEntry that will never be generated by InputReader, | 
|  | 3036 | // and assign a InputDispatcher value if it doesn't change in the if-else chain below. | 
|  | 3037 | constexpr int32_t DEFAULT_RESOLVED_EVENT_ID = | 
|  | 3038 | static_cast<int32_t>(IdGenerator::Source::OTHER); | 
|  | 3039 | dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3040 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 3041 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; | 
|  | 3042 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { | 
|  | 3043 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
|  | 3044 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { | 
|  | 3045 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 3046 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
|  | 3047 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; | 
|  | 3048 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { | 
|  | 3049 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; | 
|  | 3050 | } else { | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3051 | dispatchEntry->resolvedAction = motionEntry.action; | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3052 | dispatchEntry->resolvedEventId = motionEntry.id; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3053 | } | 
|  | 3054 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE && | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3055 | !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source, | 
|  | 3056 | motionEntry.displayId)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3057 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3058 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover " | 
|  | 3059 | "enter event", | 
|  | 3060 | connection->getInputChannelName().c_str()); | 
|  | 3061 | } | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3062 | // We keep the 'resolvedEventId' here equal to the original 'motionEntry.id' because | 
|  | 3063 | // this is a one-to-one event conversion. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3064 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
|  | 3065 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3066 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3067 | dispatchEntry->resolvedFlags = motionEntry.flags; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3068 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { | 
|  | 3069 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; | 
|  | 3070 | } | 
|  | 3071 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_PARTIALLY_OBSCURED) { | 
|  | 3072 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 3073 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3074 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3075 | if (!connection->inputState.trackMotion(motionEntry, dispatchEntry->resolvedAction, | 
|  | 3076 | dispatchEntry->resolvedFlags)) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3077 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3078 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion " | 
|  | 3079 | "event", | 
|  | 3080 | connection->getInputChannelName().c_str()); | 
|  | 3081 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3082 | return; // skip the inconsistent event | 
|  | 3083 | } | 
|  | 3084 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3085 | dispatchEntry->resolvedEventId = | 
|  | 3086 | dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID | 
|  | 3087 | ? mIdGenerator.nextId() | 
|  | 3088 | : motionEntry.id; | 
|  | 3089 | if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) { | 
|  | 3090 | std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32 | 
|  | 3091 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3092 | motionEntry.id, dispatchEntry->resolvedEventId); | 
|  | 3093 | ATRACE_NAME(message.c_str()); | 
|  | 3094 | } | 
|  | 3095 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 3096 | if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) && | 
|  | 3097 | (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) { | 
|  | 3098 | // Skip reporting pointer down outside focus to the policy. | 
|  | 3099 | break; | 
|  | 3100 | } | 
|  | 3101 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3102 | dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3103 | inputTarget.inputChannel->getConnectionToken()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3104 |  | 
|  | 3105 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3106 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3107 | case EventEntry::Type::FOCUS: | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3108 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3109 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 3110 | case EventEntry::Type::DRAG: { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3111 | break; | 
|  | 3112 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3113 | case EventEntry::Type::SENSOR: { | 
|  | 3114 | LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel"); | 
|  | 3115 | break; | 
|  | 3116 | } | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3117 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
|  | 3118 | case EventEntry::Type::DEVICE_RESET: { | 
|  | 3119 | LOG_ALWAYS_FATAL("%s events should not go to apps", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3120 | ftl::enum_string(newEntry.type).c_str()); | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3121 | break; | 
|  | 3122 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3123 | } | 
|  | 3124 |  | 
|  | 3125 | // Remember that we are waiting for this dispatch to complete. | 
|  | 3126 | if (dispatchEntry->hasForegroundTarget()) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 3127 | incrementPendingForegroundDispatches(newEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3128 | } | 
|  | 3129 |  | 
|  | 3130 | // Enqueue the dispatch entry. | 
| Siarhei Vishniakou | 5d6b661 | 2020-01-08 16:03:04 -0800 | [diff] [blame] | 3131 | connection->outboundQueue.push_back(dispatchEntry.release()); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3132 | traceOutboundQueueLength(*connection); | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3133 | } | 
|  | 3134 |  | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3135 | /** | 
|  | 3136 | * This function is purely for debugging. It helps us understand where the user interaction | 
|  | 3137 | * was taking place. For example, if user is touching launcher, we will see a log that user | 
|  | 3138 | * started interacting with launcher. In that example, the event would go to the wallpaper as well. | 
|  | 3139 | * We will see both launcher and wallpaper in that list. | 
|  | 3140 | * Once the interaction with a particular set of connections starts, no new logs will be printed | 
|  | 3141 | * until the set of interacted connections changes. | 
|  | 3142 | * | 
|  | 3143 | * The following items are skipped, to reduce the logspam: | 
|  | 3144 | * ACTION_OUTSIDE: any windows that are receiving ACTION_OUTSIDE are not logged | 
|  | 3145 | * ACTION_UP: any windows that receive ACTION_UP are not logged (for both keys and motions). | 
|  | 3146 | * This includes situations like the soft BACK button key. When the user releases (lifts up the | 
|  | 3147 | * finger) the back button, then navigation bar will inject KEYCODE_BACK with ACTION_UP. | 
|  | 3148 | * Both of those ACTION_UP events would not be logged | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3149 | */ | 
|  | 3150 | void InputDispatcher::updateInteractionTokensLocked(const EventEntry& entry, | 
|  | 3151 | const std::vector<InputTarget>& targets) { | 
|  | 3152 | // Skip ACTION_UP events, and all events other than keys and motions | 
|  | 3153 | if (entry.type == EventEntry::Type::KEY) { | 
|  | 3154 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); | 
|  | 3155 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
|  | 3156 | return; | 
|  | 3157 | } | 
|  | 3158 | } else if (entry.type == EventEntry::Type::MOTION) { | 
|  | 3159 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); | 
|  | 3160 | if (motionEntry.action == AMOTION_EVENT_ACTION_UP || | 
|  | 3161 | motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 3162 | return; | 
|  | 3163 | } | 
|  | 3164 | } else { | 
|  | 3165 | return; // Not a key or a motion | 
|  | 3166 | } | 
|  | 3167 |  | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 3168 | std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3169 | std::vector<sp<Connection>> newConnections; | 
|  | 3170 | for (const InputTarget& target : targets) { | 
|  | 3171 | if ((target.flags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) == | 
|  | 3172 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { | 
|  | 3173 | continue; // Skip windows that receive ACTION_OUTSIDE | 
|  | 3174 | } | 
|  | 3175 |  | 
|  | 3176 | sp<IBinder> token = target.inputChannel->getConnectionToken(); | 
|  | 3177 | sp<Connection> connection = getConnectionLocked(token); | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3178 | if (connection == nullptr) { | 
|  | 3179 | continue; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3180 | } | 
|  | 3181 | newConnectionTokens.insert(std::move(token)); | 
|  | 3182 | newConnections.emplace_back(connection); | 
|  | 3183 | } | 
|  | 3184 | if (newConnectionTokens == mInteractionConnectionTokens) { | 
|  | 3185 | return; // no change | 
|  | 3186 | } | 
|  | 3187 | mInteractionConnectionTokens = newConnectionTokens; | 
|  | 3188 |  | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3189 | std::string targetList; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3190 | for (const sp<Connection>& connection : newConnections) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3191 | targetList += connection->getWindowName() + ", "; | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3192 | } | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 3193 | std::string message = "Interaction with: " + targetList; | 
|  | 3194 | if (targetList.empty()) { | 
| Siarhei Vishniakou | 887b7d9 | 2020-06-18 00:43:02 +0000 | [diff] [blame] | 3195 | message += "<none>"; | 
|  | 3196 | } | 
|  | 3197 | android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS; | 
|  | 3198 | } | 
|  | 3199 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3200 | void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3201 | const sp<IBinder>& token) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3202 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 3203 | uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK; | 
|  | 3204 | if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) { | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3205 | return; | 
|  | 3206 | } | 
|  | 3207 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 3208 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 3209 | if (focusedToken == token) { | 
|  | 3210 | // ignore since token is focused | 
| chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 3211 | return; | 
|  | 3212 | } | 
|  | 3213 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3214 | auto command = [this, token]() REQUIRES(mLock) { | 
|  | 3215 | scoped_unlock unlock(mLock); | 
|  | 3216 | mPolicy->onPointerDownOutsideFocus(token); | 
|  | 3217 | }; | 
|  | 3218 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3219 | } | 
|  | 3220 |  | 
|  | 3221 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3222 | const sp<Connection>& connection) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3223 | if (ATRACE_ENABLED()) { | 
|  | 3224 | std::string message = StringPrintf("startDispatchCycleLocked(inputChannel=%s)", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3225 | connection->getInputChannelName().c_str()); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3226 | ATRACE_NAME(message.c_str()); | 
|  | 3227 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3228 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3229 | ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str()); | 
|  | 3230 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3231 |  | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3232 | while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3233 | DispatchEntry* dispatchEntry = connection->outboundQueue.front(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3234 | dispatchEntry->deliveryTime = currentTime; | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3235 | const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 3236 | dispatchEntry->timeoutTime = currentTime + timeout.count(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3237 |  | 
|  | 3238 | // Publish the event. | 
|  | 3239 | status_t status; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3240 | const EventEntry& eventEntry = *(dispatchEntry->eventEntry); | 
|  | 3241 | switch (eventEntry.type) { | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3242 | case EventEntry::Type::KEY: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3243 | const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); | 
|  | 3244 | std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3245 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3246 | // Publish the key event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3247 | status = connection->inputPublisher | 
|  | 3248 | .publishKeyEvent(dispatchEntry->seq, | 
|  | 3249 | dispatchEntry->resolvedEventId, keyEntry.deviceId, | 
|  | 3250 | keyEntry.source, keyEntry.displayId, | 
|  | 3251 | std::move(hmac), dispatchEntry->resolvedAction, | 
|  | 3252 | dispatchEntry->resolvedFlags, keyEntry.keyCode, | 
|  | 3253 | keyEntry.scanCode, keyEntry.metaState, | 
|  | 3254 | keyEntry.repeatCount, keyEntry.downTime, | 
|  | 3255 | keyEntry.eventTime); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3256 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3257 | } | 
|  | 3258 |  | 
| Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 3259 | case EventEntry::Type::MOTION: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3260 | const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3261 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3262 | PointerCoords scaledCoords[MAX_POINTERS]; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3263 | const PointerCoords* usingCoords = motionEntry.pointerCoords; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3264 |  | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3265 | // 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] | 3266 | if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) && | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3267 | !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { | 
|  | 3268 | float globalScaleFactor = dispatchEntry->globalScaleFactor; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3269 | if (globalScaleFactor != 1.0f) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3270 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
|  | 3271 | scaledCoords[i] = motionEntry.pointerCoords[i]; | 
| chaviw | 8235709 | 2020-01-28 13:13:06 -0800 | [diff] [blame] | 3272 | // Don't apply window scale here since we don't want scale to affect raw | 
|  | 3273 | // coordinates. The scale will be sent back to the client and applied | 
|  | 3274 | // later when requesting relative coordinates. | 
|  | 3275 | scaledCoords[i].scale(globalScaleFactor, 1 /* windowXScale */, | 
|  | 3276 | 1 /* windowYScale */); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3277 | } | 
|  | 3278 | usingCoords = scaledCoords; | 
|  | 3279 | } | 
|  | 3280 | } else { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3281 | // We don't want the dispatch target to know. | 
|  | 3282 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3283 | for (uint32_t i = 0; i < motionEntry.pointerCount; i++) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3284 | scaledCoords[i].clear(); | 
|  | 3285 | } | 
|  | 3286 | usingCoords = scaledCoords; | 
|  | 3287 | } | 
|  | 3288 | } | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3289 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3290 | std::array<uint8_t, 32> hmac = getSignature(motionEntry, *dispatchEntry); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3291 |  | 
|  | 3292 | // Publish the motion event. | 
|  | 3293 | status = connection->inputPublisher | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3294 | .publishMotionEvent(dispatchEntry->seq, | 
|  | 3295 | dispatchEntry->resolvedEventId, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3296 | motionEntry.deviceId, motionEntry.source, | 
|  | 3297 | motionEntry.displayId, std::move(hmac), | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3298 | dispatchEntry->resolvedAction, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3299 | motionEntry.actionButton, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3300 | dispatchEntry->resolvedFlags, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3301 | motionEntry.edgeFlags, motionEntry.metaState, | 
|  | 3302 | motionEntry.buttonState, | 
|  | 3303 | motionEntry.classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 3304 | dispatchEntry->transform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3305 | motionEntry.xPrecision, motionEntry.yPrecision, | 
|  | 3306 | motionEntry.xCursorPosition, | 
|  | 3307 | motionEntry.yCursorPosition, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 3308 | dispatchEntry->rawTransform, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3309 | motionEntry.downTime, motionEntry.eventTime, | 
|  | 3310 | motionEntry.pointerCount, | 
|  | 3311 | motionEntry.pointerProperties, usingCoords); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3312 | break; | 
|  | 3313 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3314 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3315 | case EventEntry::Type::FOCUS: { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3316 | const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3317 | status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3318 | focusEntry.id, | 
| Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 3319 | focusEntry.hasFocus); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3320 | break; | 
|  | 3321 | } | 
|  | 3322 |  | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3323 | case EventEntry::Type::TOUCH_MODE_CHANGED: { | 
|  | 3324 | const TouchModeEntry& touchModeEntry = | 
|  | 3325 | static_cast<const TouchModeEntry&>(eventEntry); | 
|  | 3326 | status = connection->inputPublisher | 
|  | 3327 | .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id, | 
|  | 3328 | touchModeEntry.inTouchMode); | 
|  | 3329 |  | 
|  | 3330 | break; | 
|  | 3331 | } | 
|  | 3332 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3333 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: { | 
|  | 3334 | const auto& captureEntry = | 
|  | 3335 | static_cast<const PointerCaptureChangedEntry&>(eventEntry); | 
|  | 3336 | status = connection->inputPublisher | 
|  | 3337 | .publishCaptureEvent(dispatchEntry->seq, captureEntry.id, | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 3338 | captureEntry.pointerCaptureRequest.enable); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3339 | break; | 
|  | 3340 | } | 
|  | 3341 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3342 | case EventEntry::Type::DRAG: { | 
|  | 3343 | const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry); | 
|  | 3344 | status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq, | 
|  | 3345 | dragEntry.id, dragEntry.x, | 
|  | 3346 | dragEntry.y, | 
|  | 3347 | dragEntry.isExiting); | 
|  | 3348 | break; | 
|  | 3349 | } | 
|  | 3350 |  | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3351 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3352 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3353 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3354 | LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3355 | ftl::enum_string(eventEntry.type).c_str()); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3356 | return; | 
| Siarhei Vishniakou | 3b37f9a | 2019-11-23 13:42:41 -0800 | [diff] [blame] | 3357 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3358 | } | 
|  | 3359 |  | 
|  | 3360 | // Check the result. | 
|  | 3361 | if (status) { | 
|  | 3362 | if (status == WOULD_BLOCK) { | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3363 | if (connection->waitQueue.empty()) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3364 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3365 | "This is unexpected because the wait queue is empty, so the pipe " | 
|  | 3366 | "should be empty and we shouldn't have any problems writing an " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3367 | "event to it, status=%s(%d)", | 
|  | 3368 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3369 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3370 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3371 | } else { | 
|  | 3372 | // Pipe is full and we are waiting for the app to finish process some events | 
|  | 3373 | // before sending more events to it. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3374 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3375 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " | 
|  | 3376 | "waiting for the application to catch up", | 
|  | 3377 | connection->getInputChannelName().c_str()); | 
|  | 3378 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3379 | } | 
|  | 3380 | } else { | 
|  | 3381 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " | 
| Siarhei Vishniakou | 09b02ac | 2021-04-14 22:24:04 +0000 | [diff] [blame] | 3382 | "status=%s(%d)", | 
|  | 3383 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3384 | status); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3385 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); | 
|  | 3386 | } | 
|  | 3387 | return; | 
|  | 3388 | } | 
|  | 3389 |  | 
|  | 3390 | // Re-enqueue the event on the wait queue. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3391 | connection->outboundQueue.erase(std::remove(connection->outboundQueue.begin(), | 
|  | 3392 | connection->outboundQueue.end(), | 
|  | 3393 | dispatchEntry)); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3394 | traceOutboundQueueLength(*connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3395 | connection->waitQueue.push_back(dispatchEntry); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3396 | if (connection->responsive) { | 
|  | 3397 | mAnrTracker.insert(dispatchEntry->timeoutTime, | 
|  | 3398 | connection->inputChannel->getConnectionToken()); | 
|  | 3399 | } | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3400 | traceWaitQueueLength(*connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3401 | } | 
|  | 3402 | } | 
|  | 3403 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3404 | std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const { | 
|  | 3405 | size_t size; | 
|  | 3406 | switch (event.type) { | 
|  | 3407 | case VerifiedInputEvent::Type::KEY: { | 
|  | 3408 | size = sizeof(VerifiedKeyEvent); | 
|  | 3409 | break; | 
|  | 3410 | } | 
|  | 3411 | case VerifiedInputEvent::Type::MOTION: { | 
|  | 3412 | size = sizeof(VerifiedMotionEvent); | 
|  | 3413 | break; | 
|  | 3414 | } | 
|  | 3415 | } | 
|  | 3416 | const uint8_t* start = reinterpret_cast<const uint8_t*>(&event); | 
|  | 3417 | return mHmacKeyManager.sign(start, size); | 
|  | 3418 | } | 
|  | 3419 |  | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3420 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3421 | const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const { | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3422 | const int32_t actionMasked = dispatchEntry.resolvedAction & AMOTION_EVENT_ACTION_MASK; | 
|  | 3423 | if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) { | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3424 | // Only sign events up and down events as the purely move events | 
|  | 3425 | // are tied to their up/down counterparts so signing would be redundant. | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3426 | return INVALID_HMAC; | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3427 | } | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3428 |  | 
|  | 3429 | VerifiedMotionEvent verifiedEvent = | 
|  | 3430 | verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform); | 
|  | 3431 | verifiedEvent.actionMasked = actionMasked; | 
|  | 3432 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS; | 
|  | 3433 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3434 | } | 
|  | 3435 |  | 
|  | 3436 | const std::array<uint8_t, 32> InputDispatcher::getSignature( | 
|  | 3437 | const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const { | 
|  | 3438 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry); | 
|  | 3439 | verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS; | 
|  | 3440 | verifiedEvent.action = dispatchEntry.resolvedAction; | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3441 | return sign(verifiedEvent); | 
| Edgar Arriaga | c6ae4bb | 2020-04-16 18:46:48 -0700 | [diff] [blame] | 3442 | } | 
|  | 3443 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3444 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3445 | const sp<Connection>& connection, uint32_t seq, | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 3446 | bool handled, nsecs_t consumeTime) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3447 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3448 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", | 
|  | 3449 | connection->getInputChannelName().c_str(), seq, toString(handled)); | 
|  | 3450 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3451 |  | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3452 | if (connection->status == Connection::Status::BROKEN || | 
|  | 3453 | connection->status == Connection::Status::ZOMBIE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3454 | return; | 
|  | 3455 | } | 
|  | 3456 |  | 
|  | 3457 | // Notify other system components and prepare to start the next dispatch cycle. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3458 | auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) { | 
|  | 3459 | doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime); | 
|  | 3460 | }; | 
|  | 3461 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3462 | } | 
|  | 3463 |  | 
|  | 3464 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3465 | const sp<Connection>& connection, | 
|  | 3466 | bool notify) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3467 | if (DEBUG_DISPATCH_CYCLE) { | 
|  | 3468 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", | 
|  | 3469 | connection->getInputChannelName().c_str(), toString(notify)); | 
|  | 3470 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3471 |  | 
|  | 3472 | // Clear the dispatch queues. | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3473 | drainDispatchQueue(connection->outboundQueue); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3474 | traceOutboundQueueLength(*connection); | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3475 | drainDispatchQueue(connection->waitQueue); | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 3476 | traceWaitQueueLength(*connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3477 |  | 
|  | 3478 | // The connection appears to be unrecoverably broken. | 
|  | 3479 | // Ignore already broken or zombie connections. | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3480 | if (connection->status == Connection::Status::NORMAL) { | 
|  | 3481 | connection->status = Connection::Status::BROKEN; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3482 |  | 
|  | 3483 | if (notify) { | 
|  | 3484 | // Notify other system components. | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3485 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", | 
|  | 3486 | connection->getInputChannelName().c_str()); | 
|  | 3487 |  | 
|  | 3488 | auto command = [this, connection]() REQUIRES(mLock) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3489 | scoped_unlock unlock(mLock); | 
|  | 3490 | mPolicy->notifyInputChannelBroken(connection->inputChannel->getConnectionToken()); | 
|  | 3491 | }; | 
|  | 3492 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3493 | } | 
|  | 3494 | } | 
|  | 3495 | } | 
|  | 3496 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 3497 | void InputDispatcher::drainDispatchQueue(std::deque<DispatchEntry*>& queue) { | 
|  | 3498 | while (!queue.empty()) { | 
|  | 3499 | DispatchEntry* dispatchEntry = queue.front(); | 
|  | 3500 | queue.pop_front(); | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3501 | releaseDispatchEntry(dispatchEntry); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3502 | } | 
|  | 3503 | } | 
|  | 3504 |  | 
| Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 3505 | void InputDispatcher::releaseDispatchEntry(DispatchEntry* dispatchEntry) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3506 | if (dispatchEntry->hasForegroundTarget()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3507 | decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3508 | } | 
|  | 3509 | delete dispatchEntry; | 
|  | 3510 | } | 
|  | 3511 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3512 | int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) { | 
|  | 3513 | std::scoped_lock _l(mLock); | 
|  | 3514 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 3515 | if (connection == nullptr) { | 
|  | 3516 | ALOGW("Received looper callback for unknown input channel token %p.  events=0x%x", | 
|  | 3517 | connectionToken.get(), events); | 
|  | 3518 | return 0; // remove the callback | 
|  | 3519 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3520 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3521 | bool notify; | 
|  | 3522 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { | 
|  | 3523 | if (!(events & ALOOPER_EVENT_INPUT)) { | 
|  | 3524 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event.  " | 
|  | 3525 | "events=0x%x", | 
|  | 3526 | connection->getInputChannelName().c_str(), events); | 
|  | 3527 | return 1; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3528 | } | 
|  | 3529 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3530 | nsecs_t currentTime = now(); | 
|  | 3531 | bool gotOne = false; | 
|  | 3532 | status_t status = OK; | 
|  | 3533 | for (;;) { | 
|  | 3534 | Result<InputPublisher::ConsumerResponse> result = | 
|  | 3535 | connection->inputPublisher.receiveConsumerResponse(); | 
|  | 3536 | if (!result.ok()) { | 
|  | 3537 | status = result.error().code(); | 
|  | 3538 | break; | 
|  | 3539 | } | 
|  | 3540 |  | 
|  | 3541 | if (std::holds_alternative<InputPublisher::Finished>(*result)) { | 
|  | 3542 | const InputPublisher::Finished& finish = | 
|  | 3543 | std::get<InputPublisher::Finished>(*result); | 
|  | 3544 | finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled, | 
|  | 3545 | finish.consumeTime); | 
|  | 3546 | } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) { | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 3547 | if (shouldReportMetricsForConnection(*connection)) { | 
|  | 3548 | const InputPublisher::Timeline& timeline = | 
|  | 3549 | std::get<InputPublisher::Timeline>(*result); | 
|  | 3550 | mLatencyTracker | 
|  | 3551 | .trackGraphicsLatency(timeline.inputEventId, | 
|  | 3552 | connection->inputChannel->getConnectionToken(), | 
|  | 3553 | std::move(timeline.graphicsTimeline)); | 
|  | 3554 | } | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3555 | } | 
|  | 3556 | gotOne = true; | 
|  | 3557 | } | 
|  | 3558 | if (gotOne) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 3559 | runCommandsLockedInterruptable(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3560 | if (status == WOULD_BLOCK) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3561 | return 1; | 
|  | 3562 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3563 | } | 
|  | 3564 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3565 | notify = status != DEAD_OBJECT || !connection->monitor; | 
|  | 3566 | if (notify) { | 
|  | 3567 | ALOGE("channel '%s' ~ Failed to receive finished signal.  status=%s(%d)", | 
|  | 3568 | connection->getInputChannelName().c_str(), statusToString(status).c_str(), | 
|  | 3569 | status); | 
|  | 3570 | } | 
|  | 3571 | } else { | 
|  | 3572 | // Monitor channels are never explicitly unregistered. | 
|  | 3573 | // We do it automatically when the remote endpoint is closed so don't warn about them. | 
|  | 3574 | const bool stillHaveWindowHandle = | 
|  | 3575 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()) != nullptr; | 
|  | 3576 | notify = !connection->monitor && stillHaveWindowHandle; | 
|  | 3577 | if (notify) { | 
|  | 3578 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred.  events=0x%x", | 
|  | 3579 | connection->getInputChannelName().c_str(), events); | 
|  | 3580 | } | 
|  | 3581 | } | 
|  | 3582 |  | 
|  | 3583 | // Remove the channel. | 
|  | 3584 | removeInputChannelLocked(connection->inputChannel->getConnectionToken(), notify); | 
|  | 3585 | return 0; // remove the callback | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3586 | } | 
|  | 3587 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3588 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3589 | const CancelationOptions& options) { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 3590 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 2e2ea99 | 2020-12-15 02:57:19 +0000 | [diff] [blame] | 3591 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3592 | } | 
|  | 3593 | } | 
|  | 3594 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3595 | void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked( | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3596 | const CancelationOptions& options) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3597 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 3598 | for (const Monitor& monitor : monitors) { | 
|  | 3599 | synthesizeCancelationEventsForInputChannelLocked(monitor.inputChannel, options); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 3600 | } | 
| Michael Wright | fa13dcf | 2015-06-12 13:25:11 +0100 | [diff] [blame] | 3601 | } | 
|  | 3602 | } | 
|  | 3603 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3604 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 3605 | const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 3606 | sp<Connection> connection = getConnectionLocked(channel->getConnectionToken()); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3607 | if (connection == nullptr) { | 
|  | 3608 | return; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3609 | } | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 3610 |  | 
|  | 3611 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3612 | } | 
|  | 3613 |  | 
|  | 3614 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( | 
|  | 3615 | const sp<Connection>& connection, const CancelationOptions& options) { | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3616 | if (connection->status == Connection::Status::BROKEN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3617 | return; | 
|  | 3618 | } | 
|  | 3619 |  | 
|  | 3620 | nsecs_t currentTime = now(); | 
|  | 3621 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3622 | std::vector<std::unique_ptr<EventEntry>> cancelationEvents = | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 3623 | connection->inputState.synthesizeCancelationEvents(currentTime, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3624 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3625 | if (cancelationEvents.empty()) { | 
|  | 3626 | return; | 
|  | 3627 | } | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3628 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 3629 | ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync " | 
|  | 3630 | "with reality: %s, mode=%d.", | 
|  | 3631 | connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason, | 
|  | 3632 | options.mode); | 
|  | 3633 | } | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3634 |  | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3635 | std::string reason = std::string("reason=").append(options.reason); | 
|  | 3636 | android_log_event_list(LOGTAG_INPUT_CANCEL) | 
|  | 3637 | << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS; | 
|  | 3638 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3639 | InputTarget target; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3640 | sp<WindowInfoHandle> windowHandle = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3641 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3642 | if (windowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3643 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3644 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3645 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3646 | } | 
|  | 3647 | target.inputChannel = connection->inputChannel; | 
|  | 3648 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3649 |  | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3650 | for (size_t i = 0; i < cancelationEvents.size(); i++) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3651 | std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3652 | switch (cancelationEventEntry->type) { | 
|  | 3653 | case EventEntry::Type::KEY: { | 
|  | 3654 | logOutboundKeyDetails("cancel - ", | 
|  | 3655 | static_cast<const KeyEntry&>(*cancelationEventEntry)); | 
|  | 3656 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3657 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3658 | case EventEntry::Type::MOTION: { | 
|  | 3659 | logOutboundMotionDetails("cancel - ", | 
|  | 3660 | static_cast<const MotionEntry&>(*cancelationEventEntry)); | 
|  | 3661 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3662 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3663 | case EventEntry::Type::FOCUS: | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3664 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3665 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
|  | 3666 | case EventEntry::Type::DRAG: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3667 | LOG_ALWAYS_FATAL("Canceling %s events is not supported", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3668 | ftl::enum_string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3669 | break; | 
|  | 3670 | } | 
|  | 3671 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3672 | case EventEntry::Type::DEVICE_RESET: | 
|  | 3673 | case EventEntry::Type::SENSOR: { | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3674 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3675 | ftl::enum_string(cancelationEventEntry->type).c_str()); | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3676 | break; | 
|  | 3677 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3678 | } | 
|  | 3679 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3680 | enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), target, | 
|  | 3681 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3682 | } | 
| Siarhei Vishniakou | bd11889 | 2020-01-10 14:08:28 -0800 | [diff] [blame] | 3683 |  | 
|  | 3684 | startDispatchCycleLocked(currentTime, connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3685 | } | 
|  | 3686 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3687 | void InputDispatcher::synthesizePointerDownEventsForConnectionLocked( | 
|  | 3688 | const sp<Connection>& connection) { | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 3689 | if (connection->status == Connection::Status::BROKEN) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3690 | return; | 
|  | 3691 | } | 
|  | 3692 |  | 
|  | 3693 | nsecs_t currentTime = now(); | 
|  | 3694 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3695 | std::vector<std::unique_ptr<EventEntry>> downEvents = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3696 | connection->inputState.synthesizePointerDownEvents(currentTime); | 
|  | 3697 |  | 
|  | 3698 | if (downEvents.empty()) { | 
|  | 3699 | return; | 
|  | 3700 | } | 
|  | 3701 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3702 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3703 | ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.", | 
|  | 3704 | connection->getInputChannelName().c_str(), downEvents.size()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3705 | } | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3706 |  | 
|  | 3707 | InputTarget target; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3708 | sp<WindowInfoHandle> windowHandle = | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3709 | getWindowHandleLocked(connection->inputChannel->getConnectionToken()); | 
|  | 3710 | if (windowHandle != nullptr) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 3711 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 3712 | target.setDefaultPointerTransform(windowInfo->transform); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3713 | target.globalScaleFactor = windowInfo->globalScaleFactor; | 
|  | 3714 | } | 
|  | 3715 | target.inputChannel = connection->inputChannel; | 
|  | 3716 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; | 
|  | 3717 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3718 | for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3719 | switch (downEventEntry->type) { | 
|  | 3720 | case EventEntry::Type::MOTION: { | 
|  | 3721 | logOutboundMotionDetails("down - ", | 
|  | 3722 | static_cast<const MotionEntry&>(*downEventEntry)); | 
|  | 3723 | break; | 
|  | 3724 | } | 
|  | 3725 |  | 
|  | 3726 | case EventEntry::Type::KEY: | 
|  | 3727 | case EventEntry::Type::FOCUS: | 
| Antonio Kantek | 7242d8b | 2021-08-05 16:07:20 -0700 | [diff] [blame] | 3728 | case EventEntry::Type::TOUCH_MODE_CHANGED: | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3729 | case EventEntry::Type::CONFIGURATION_CHANGED: | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 3730 | case EventEntry::Type::DEVICE_RESET: | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 3731 | case EventEntry::Type::POINTER_CAPTURE_CHANGED: | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 3732 | case EventEntry::Type::SENSOR: | 
|  | 3733 | case EventEntry::Type::DRAG: { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3734 | LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 3735 | ftl::enum_string(downEventEntry->type).c_str()); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3736 | break; | 
|  | 3737 | } | 
|  | 3738 | } | 
|  | 3739 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3740 | enqueueDispatchEntryLocked(connection, std::move(downEventEntry), target, | 
|  | 3741 | InputTarget::FLAG_DISPATCH_AS_IS); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3742 | } | 
|  | 3743 |  | 
|  | 3744 | startDispatchCycleLocked(currentTime, connection); | 
|  | 3745 | } | 
|  | 3746 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3747 | std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent( | 
|  | 3748 | const MotionEntry& originalMotionEntry, BitSet32 pointerIds) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3749 | ALOG_ASSERT(pointerIds.value != 0); | 
|  | 3750 |  | 
|  | 3751 | uint32_t splitPointerIndexMap[MAX_POINTERS]; | 
|  | 3752 | PointerProperties splitPointerProperties[MAX_POINTERS]; | 
|  | 3753 | PointerCoords splitPointerCoords[MAX_POINTERS]; | 
|  | 3754 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3755 | uint32_t originalPointerCount = originalMotionEntry.pointerCount; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3756 | uint32_t splitPointerCount = 0; | 
|  | 3757 |  | 
|  | 3758 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3759 | originalPointerIndex++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3760 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3761 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3762 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3763 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3764 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; | 
|  | 3765 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); | 
|  | 3766 | splitPointerCoords[splitPointerCount].copyFrom( | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3767 | originalMotionEntry.pointerCoords[originalPointerIndex]); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3768 | splitPointerCount += 1; | 
|  | 3769 | } | 
|  | 3770 | } | 
|  | 3771 |  | 
|  | 3772 | if (splitPointerCount != pointerIds.count()) { | 
|  | 3773 | // This is bad.  We are missing some of the pointers that we expected to deliver. | 
|  | 3774 | // Most likely this indicates that we received an ACTION_MOVE events that has | 
|  | 3775 | // different pointer ids than we expected based on the previous ACTION_DOWN | 
|  | 3776 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers | 
|  | 3777 | // in this way. | 
|  | 3778 | ALOGW("Dropping split motion event because the pointer count is %d but " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3779 | "we expected there to be %d pointers.  This probably means we received " | 
|  | 3780 | "a broken sequence of pointer ids from the input device.", | 
|  | 3781 | splitPointerCount, pointerIds.count()); | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 3782 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3783 | } | 
|  | 3784 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3785 | int32_t action = originalMotionEntry.action; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3786 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3787 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || | 
|  | 3788 | maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3789 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); | 
|  | 3790 | const PointerProperties& pointerProperties = | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3791 | originalMotionEntry.pointerProperties[originalPointerIndex]; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3792 | uint32_t pointerId = uint32_t(pointerProperties.id); | 
|  | 3793 | if (pointerIds.hasBit(pointerId)) { | 
|  | 3794 | if (pointerIds.count() == 1) { | 
|  | 3795 | // The first/last pointer went down/up. | 
|  | 3796 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3797 | ? AMOTION_EVENT_ACTION_DOWN | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3798 | : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0 | 
|  | 3799 | ? AMOTION_EVENT_ACTION_CANCEL | 
|  | 3800 | : AMOTION_EVENT_ACTION_UP; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3801 | } else { | 
|  | 3802 | // A secondary pointer went down/up. | 
|  | 3803 | uint32_t splitPointerIndex = 0; | 
|  | 3804 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { | 
|  | 3805 | splitPointerIndex += 1; | 
|  | 3806 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3807 | action = maskedAction | | 
|  | 3808 | (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3809 | } | 
|  | 3810 | } else { | 
|  | 3811 | // An unrelated pointer changed. | 
|  | 3812 | action = AMOTION_EVENT_ACTION_MOVE; | 
|  | 3813 | } | 
|  | 3814 | } | 
|  | 3815 |  | 
| Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3816 | int32_t newId = mIdGenerator.nextId(); | 
|  | 3817 | if (ATRACE_ENABLED()) { | 
|  | 3818 | std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32 | 
|  | 3819 | ") to MotionEvent(id=0x%" PRIx32 ").", | 
|  | 3820 | originalMotionEntry.id, newId); | 
|  | 3821 | ATRACE_NAME(message.c_str()); | 
|  | 3822 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3823 | std::unique_ptr<MotionEntry> splitMotionEntry = | 
|  | 3824 | std::make_unique<MotionEntry>(newId, originalMotionEntry.eventTime, | 
|  | 3825 | originalMotionEntry.deviceId, originalMotionEntry.source, | 
|  | 3826 | originalMotionEntry.displayId, | 
|  | 3827 | originalMotionEntry.policyFlags, action, | 
|  | 3828 | originalMotionEntry.actionButton, | 
|  | 3829 | originalMotionEntry.flags, originalMotionEntry.metaState, | 
|  | 3830 | originalMotionEntry.buttonState, | 
|  | 3831 | originalMotionEntry.classification, | 
|  | 3832 | originalMotionEntry.edgeFlags, | 
|  | 3833 | originalMotionEntry.xPrecision, | 
|  | 3834 | originalMotionEntry.yPrecision, | 
|  | 3835 | originalMotionEntry.xCursorPosition, | 
|  | 3836 | originalMotionEntry.yCursorPosition, | 
|  | 3837 | originalMotionEntry.downTime, splitPointerCount, | 
| Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3838 | splitPointerProperties, splitPointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3839 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 3840 | if (originalMotionEntry.injectionState) { | 
|  | 3841 | splitMotionEntry->injectionState = originalMotionEntry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3842 | splitMotionEntry->injectionState->refCount += 1; | 
|  | 3843 | } | 
|  | 3844 |  | 
|  | 3845 | return splitMotionEntry; | 
|  | 3846 | } | 
|  | 3847 |  | 
|  | 3848 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3849 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3850 | ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args->eventTime); | 
|  | 3851 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3852 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3853 | bool needWake = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3854 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3855 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3856 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3857 | std::unique_ptr<ConfigurationChangedEntry> newEntry = | 
|  | 3858 | std::make_unique<ConfigurationChangedEntry>(args->id, args->eventTime); | 
|  | 3859 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3860 | } // release lock | 
|  | 3861 |  | 
|  | 3862 | if (needWake) { | 
|  | 3863 | mLooper->wake(); | 
|  | 3864 | } | 
|  | 3865 | } | 
|  | 3866 |  | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3867 | /** | 
|  | 3868 | * If one of the meta shortcuts is detected, process them here: | 
|  | 3869 | *     Meta + Backspace -> generate BACK | 
|  | 3870 | *     Meta + Enter -> generate HOME | 
|  | 3871 | * This will potentially overwrite keyCode and metaState. | 
|  | 3872 | */ | 
|  | 3873 | void InputDispatcher::accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3874 | int32_t& keyCode, int32_t& metaState) { | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3875 | if (metaState & AMETA_META_ON && action == AKEY_EVENT_ACTION_DOWN) { | 
|  | 3876 | int32_t newKeyCode = AKEYCODE_UNKNOWN; | 
|  | 3877 | if (keyCode == AKEYCODE_DEL) { | 
|  | 3878 | newKeyCode = AKEYCODE_BACK; | 
|  | 3879 | } else if (keyCode == AKEYCODE_ENTER) { | 
|  | 3880 | newKeyCode = AKEYCODE_HOME; | 
|  | 3881 | } | 
|  | 3882 | if (newKeyCode != AKEYCODE_UNKNOWN) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 3883 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3884 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3885 | mReplacedKeys[replacement] = newKeyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3886 | keyCode = newKeyCode; | 
|  | 3887 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3888 | } | 
|  | 3889 | } else if (action == AKEY_EVENT_ACTION_UP) { | 
|  | 3890 | // In order to maintain a consistent stream of up and down events, check to see if the key | 
|  | 3891 | // going up is one we've replaced in a down event and haven't yet replaced in an up event, | 
|  | 3892 | // 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] | 3893 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3894 | struct KeyReplacement replacement = {keyCode, deviceId}; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 3895 | auto replacementIt = mReplacedKeys.find(replacement); | 
|  | 3896 | if (replacementIt != mReplacedKeys.end()) { | 
|  | 3897 | keyCode = replacementIt->second; | 
|  | 3898 | mReplacedKeys.erase(replacementIt); | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3899 | metaState &= ~(AMETA_META_ON | AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON); | 
|  | 3900 | } | 
|  | 3901 | } | 
|  | 3902 | } | 
|  | 3903 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3904 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3905 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3906 | ALOGD("notifyKey - eventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 | 
|  | 3907 | "policyFlags=0x%x, action=0x%x, " | 
|  | 3908 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%" PRId64, | 
|  | 3909 | args->eventTime, args->deviceId, args->source, args->displayId, args->policyFlags, | 
|  | 3910 | args->action, args->flags, args->keyCode, args->scanCode, args->metaState, | 
|  | 3911 | args->downTime); | 
|  | 3912 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3913 | if (!validateKeyEvent(args->action)) { | 
|  | 3914 | return; | 
|  | 3915 | } | 
|  | 3916 |  | 
|  | 3917 | uint32_t policyFlags = args->policyFlags; | 
|  | 3918 | int32_t flags = args->flags; | 
|  | 3919 | int32_t metaState = args->metaState; | 
| Siarhei Vishniakou | 622bd32 | 2018-10-29 18:02:27 -0700 | [diff] [blame] | 3920 | // InputDispatcher tracks and generates key repeats on behalf of | 
|  | 3921 | // whatever notifies it, so repeatCount should always be set to 0 | 
|  | 3922 | constexpr int32_t repeatCount = 0; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3923 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { | 
|  | 3924 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
|  | 3925 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; | 
|  | 3926 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3927 | if (policyFlags & POLICY_FLAG_FUNCTION) { | 
|  | 3928 | metaState |= AMETA_FUNCTION_ON; | 
|  | 3929 | } | 
|  | 3930 |  | 
|  | 3931 | policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 3932 |  | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3933 | int32_t keyCode = args->keyCode; | 
| Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 3934 | accelerateMetaShortcuts(args->deviceId, args->action, keyCode, metaState); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 3935 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3936 | KeyEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3937 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
| Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 3938 | args->action, flags, keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3939 | args->downTime, args->eventTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3940 |  | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3941 | android::base::Timer t; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3942 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3943 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 3944 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 3945 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 3946 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3947 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3948 | bool needWake = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3949 | { // acquire lock | 
|  | 3950 | mLock.lock(); | 
|  | 3951 |  | 
|  | 3952 | if (shouldSendKeyToInputFilterLocked(args)) { | 
|  | 3953 | mLock.unlock(); | 
|  | 3954 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 3955 | policyFlags |= POLICY_FLAG_FILTERED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3956 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 3957 | return; // event was consumed by the filter | 
|  | 3958 | } | 
|  | 3959 |  | 
|  | 3960 | mLock.lock(); | 
|  | 3961 | } | 
|  | 3962 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3963 | std::unique_ptr<KeyEntry> newEntry = | 
|  | 3964 | std::make_unique<KeyEntry>(args->id, args->eventTime, args->deviceId, args->source, | 
|  | 3965 | args->displayId, policyFlags, args->action, flags, | 
|  | 3966 | keyCode, args->scanCode, metaState, repeatCount, | 
|  | 3967 | args->downTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3968 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 3969 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 3970 | mLock.unlock(); | 
|  | 3971 | } // release lock | 
|  | 3972 |  | 
|  | 3973 | if (needWake) { | 
|  | 3974 | mLooper->wake(); | 
|  | 3975 | } | 
|  | 3976 | } | 
|  | 3977 |  | 
|  | 3978 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { | 
|  | 3979 | return mInputFilterEnabled; | 
|  | 3980 | } | 
|  | 3981 |  | 
|  | 3982 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 3983 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 3984 | ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 3985 | "displayId=%" PRId32 ", policyFlags=0x%x, " | 
|  | 3986 | "action=0x%x, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, " | 
|  | 3987 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, " | 
|  | 3988 | "yCursorPosition=%f, downTime=%" PRId64, | 
|  | 3989 | args->id, args->eventTime, args->deviceId, args->source, args->displayId, | 
|  | 3990 | args->policyFlags, args->action, args->actionButton, args->flags, args->metaState, | 
|  | 3991 | args->buttonState, args->edgeFlags, args->xPrecision, args->yPrecision, | 
|  | 3992 | args->xCursorPosition, args->yCursorPosition, args->downTime); | 
|  | 3993 | for (uint32_t i = 0; i < args->pointerCount; i++) { | 
|  | 3994 | ALOGD("  Pointer %d: id=%d, toolType=%d, " | 
|  | 3995 | "x=%f, y=%f, pressure=%f, size=%f, " | 
|  | 3996 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " | 
|  | 3997 | "orientation=%f", | 
|  | 3998 | i, args->pointerProperties[i].id, args->pointerProperties[i].toolType, | 
|  | 3999 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), | 
|  | 4000 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), | 
|  | 4001 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), | 
|  | 4002 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), | 
|  | 4003 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), | 
|  | 4004 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), | 
|  | 4005 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), | 
|  | 4006 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), | 
|  | 4007 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); | 
|  | 4008 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4009 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4010 | if (!validateMotionEvent(args->action, args->actionButton, args->pointerCount, | 
|  | 4011 | args->pointerProperties)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4012 | return; | 
|  | 4013 | } | 
|  | 4014 |  | 
|  | 4015 | uint32_t policyFlags = args->policyFlags; | 
|  | 4016 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4017 |  | 
|  | 4018 | android::base::Timer t; | 
| Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 4019 | mPolicy->interceptMotionBeforeQueueing(args->displayId, args->eventTime, /*byref*/ policyFlags); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4020 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4021 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4022 | std::to_string(t.duration().count()).c_str()); | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4023 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4024 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4025 | bool needWake = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4026 | { // acquire lock | 
|  | 4027 | mLock.lock(); | 
|  | 4028 |  | 
|  | 4029 | if (shouldSendMotionToInputFilterLocked(args)) { | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4030 | ui::Transform displayTransform; | 
|  | 4031 | if (const auto it = mDisplayInfos.find(args->displayId); it != mDisplayInfos.end()) { | 
|  | 4032 | displayTransform = it->second.transform; | 
|  | 4033 | } | 
|  | 4034 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4035 | mLock.unlock(); | 
|  | 4036 |  | 
|  | 4037 | MotionEvent event; | 
| Garfield Tan | 6a5a14e | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4038 | event.initialize(args->id, args->deviceId, args->source, args->displayId, INVALID_HMAC, | 
|  | 4039 | args->action, args->actionButton, args->flags, args->edgeFlags, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4040 | args->metaState, args->buttonState, args->classification, | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4041 | displayTransform, args->xPrecision, args->yPrecision, | 
|  | 4042 | args->xCursorPosition, args->yCursorPosition, displayTransform, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4043 | args->downTime, args->eventTime, args->pointerCount, | 
|  | 4044 | args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4045 |  | 
|  | 4046 | policyFlags |= POLICY_FLAG_FILTERED; | 
|  | 4047 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { | 
|  | 4048 | return; // event was consumed by the filter | 
|  | 4049 | } | 
|  | 4050 |  | 
|  | 4051 | mLock.lock(); | 
|  | 4052 | } | 
|  | 4053 |  | 
|  | 4054 | // Just enqueue a new motion event. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4055 | std::unique_ptr<MotionEntry> newEntry = | 
|  | 4056 | std::make_unique<MotionEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 4057 | args->source, args->displayId, policyFlags, | 
|  | 4058 | args->action, args->actionButton, args->flags, | 
|  | 4059 | args->metaState, args->buttonState, | 
|  | 4060 | args->classification, args->edgeFlags, | 
|  | 4061 | args->xPrecision, args->yPrecision, | 
|  | 4062 | args->xCursorPosition, args->yCursorPosition, | 
|  | 4063 | args->downTime, args->pointerCount, | 
| Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4064 | args->pointerProperties, args->pointerCoords); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4065 |  | 
| Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 4066 | if (args->id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID && | 
|  | 4067 | IdGenerator::getSource(args->id) == IdGenerator::Source::INPUT_READER && | 
|  | 4068 | !mInputFilterEnabled) { | 
|  | 4069 | const bool isDown = args->action == AMOTION_EVENT_ACTION_DOWN; | 
|  | 4070 | mLatencyTracker.trackListener(args->id, isDown, args->eventTime, args->readTime); | 
|  | 4071 | } | 
|  | 4072 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4073 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4074 | mLock.unlock(); | 
|  | 4075 | } // release lock | 
|  | 4076 |  | 
|  | 4077 | if (needWake) { | 
|  | 4078 | mLooper->wake(); | 
|  | 4079 | } | 
|  | 4080 | } | 
|  | 4081 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4082 | void InputDispatcher::notifySensor(const NotifySensorArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4083 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4084 | ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, " | 
|  | 4085 | " sensorType=%s", | 
|  | 4086 | args->id, args->eventTime, args->deviceId, args->source, | 
| Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 4087 | ftl::enum_string(args->sensorType).c_str()); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4088 | } | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4089 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4090 | bool needWake = false; | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 4091 | { // acquire lock | 
|  | 4092 | mLock.lock(); | 
|  | 4093 |  | 
|  | 4094 | // Just enqueue a new sensor event. | 
|  | 4095 | std::unique_ptr<SensorEntry> newEntry = | 
|  | 4096 | std::make_unique<SensorEntry>(args->id, args->eventTime, args->deviceId, | 
|  | 4097 | args->source, 0 /* policyFlags*/, args->hwTimestamp, | 
|  | 4098 | args->sensorType, args->accuracy, | 
|  | 4099 | args->accuracyChanged, args->values); | 
|  | 4100 |  | 
|  | 4101 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
|  | 4102 | mLock.unlock(); | 
|  | 4103 | } // release lock | 
|  | 4104 |  | 
|  | 4105 | if (needWake) { | 
|  | 4106 | mLooper->wake(); | 
|  | 4107 | } | 
|  | 4108 | } | 
|  | 4109 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4110 | void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4111 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4112 | ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d,  isOn=%d", args->eventTime, | 
|  | 4113 | args->deviceId, args->isOn); | 
|  | 4114 | } | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 4115 | mPolicy->notifyVibratorState(args->deviceId, args->isOn); | 
|  | 4116 | } | 
|  | 4117 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4118 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4119 | return mInputFilterEnabled; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4120 | } | 
|  | 4121 |  | 
|  | 4122 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4123 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4124 | ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, " | 
|  | 4125 | "switchMask=0x%08x", | 
|  | 4126 | args->eventTime, args->policyFlags, args->switchValues, args->switchMask); | 
|  | 4127 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4128 |  | 
|  | 4129 | uint32_t policyFlags = args->policyFlags; | 
|  | 4130 | policyFlags |= POLICY_FLAG_TRUSTED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4131 | mPolicy->notifySwitch(args->eventTime, args->switchValues, args->switchMask, policyFlags); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4132 | } | 
|  | 4133 |  | 
|  | 4134 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4135 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4136 | ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args->eventTime, | 
|  | 4137 | args->deviceId); | 
|  | 4138 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4139 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4140 | bool needWake = false; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4141 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4142 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4143 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4144 | std::unique_ptr<DeviceResetEntry> newEntry = | 
|  | 4145 | std::make_unique<DeviceResetEntry>(args->id, args->eventTime, args->deviceId); | 
|  | 4146 | needWake = enqueueInboundEventLocked(std::move(newEntry)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4147 | } // release lock | 
|  | 4148 |  | 
|  | 4149 | if (needWake) { | 
|  | 4150 | mLooper->wake(); | 
|  | 4151 | } | 
|  | 4152 | } | 
|  | 4153 |  | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4154 | void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4155 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
|  | 4156 | ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args->eventTime, | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4157 | args->request.enable ? "true" : "false"); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4158 | } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4159 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4160 | bool needWake = false; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4161 | { // acquire lock | 
|  | 4162 | std::scoped_lock _l(mLock); | 
|  | 4163 | auto entry = std::make_unique<PointerCaptureChangedEntry>(args->id, args->eventTime, | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 4164 | args->request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 4165 | needWake = enqueueInboundEventLocked(std::move(entry)); | 
|  | 4166 | } // release lock | 
|  | 4167 |  | 
|  | 4168 | if (needWake) { | 
|  | 4169 | mLooper->wake(); | 
|  | 4170 | } | 
| Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 4171 | } | 
|  | 4172 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4173 | InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, | 
|  | 4174 | std::optional<int32_t> targetUid, | 
|  | 4175 | InputEventInjectionSync syncMode, | 
|  | 4176 | std::chrono::milliseconds timeout, | 
|  | 4177 | uint32_t policyFlags) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4178 | if (DEBUG_INBOUND_EVENT_DETAILS) { | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4179 | ALOGD("injectInputEvent - eventType=%d, targetUid=%s, syncMode=%d, timeout=%lld, " | 
|  | 4180 | "policyFlags=0x%08x", | 
|  | 4181 | event->getType(), targetUid ? std::to_string(*targetUid).c_str() : "none", syncMode, | 
|  | 4182 | timeout.count(), policyFlags); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4183 | } | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 4184 | 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] | 4185 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4186 | policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4187 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4188 | // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4189 | // that have gone through the InputFilter. If the event passed through the InputFilter, assign | 
|  | 4190 | // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes | 
|  | 4191 | // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY. | 
|  | 4192 | // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them | 
|  | 4193 | // from events that originate from actual hardware. | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4194 | int32_t resolvedDeviceId = VIRTUAL_KEYBOARD_ID; | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4195 | if (policyFlags & POLICY_FLAG_FILTERED) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4196 | resolvedDeviceId = event->getDeviceId(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4197 | } | 
|  | 4198 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4199 | std::queue<std::unique_ptr<EventEntry>> injectedEntries; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4200 | switch (event->getType()) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4201 | case AINPUT_EVENT_TYPE_KEY: { | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4202 | const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event); | 
|  | 4203 | int32_t action = incomingKey.getAction(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4204 | if (!validateKeyEvent(action)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4206 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4207 |  | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4208 | int32_t flags = incomingKey.getFlags(); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4209 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { | 
|  | 4210 | flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; | 
|  | 4211 | } | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4212 | int32_t keyCode = incomingKey.getKeyCode(); | 
|  | 4213 | int32_t metaState = incomingKey.getMetaState(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4214 | accelerateMetaShortcuts(resolvedDeviceId, action, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4215 | /*byref*/ keyCode, /*byref*/ metaState); | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4216 | KeyEvent keyEvent; | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4217 | keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(), | 
| Siarhei Vishniakou | 0d8ed6e | 2020-01-17 15:48:59 -0800 | [diff] [blame] | 4218 | incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode, | 
|  | 4219 | incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(), | 
|  | 4220 | incomingKey.getDownTime(), incomingKey.getEventTime()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4221 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4222 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { | 
|  | 4223 | policyFlags |= POLICY_FLAG_VIRTUAL; | 
| Michael Wright | 2b3c330 | 2018-03-02 17:19:13 +0000 | [diff] [blame] | 4224 | } | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4225 |  | 
|  | 4226 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
|  | 4227 | android::base::Timer t; | 
|  | 4228 | mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags); | 
|  | 4229 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4230 | ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms", | 
|  | 4231 | std::to_string(t.duration().count()).c_str()); | 
|  | 4232 | } | 
|  | 4233 | } | 
|  | 4234 |  | 
|  | 4235 | mLock.lock(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4236 | std::unique_ptr<KeyEntry> injectedEntry = | 
|  | 4237 | std::make_unique<KeyEntry>(incomingKey.getId(), incomingKey.getEventTime(), | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4238 | resolvedDeviceId, incomingKey.getSource(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4239 | incomingKey.getDisplayId(), policyFlags, action, | 
|  | 4240 | flags, keyCode, incomingKey.getScanCode(), metaState, | 
|  | 4241 | incomingKey.getRepeatCount(), | 
|  | 4242 | incomingKey.getDownTime()); | 
|  | 4243 | injectedEntries.push(std::move(injectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4244 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4245 | } | 
|  | 4246 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4247 | case AINPUT_EVENT_TYPE_MOTION: { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4248 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4249 | const int32_t action = motionEvent.getAction(); | 
|  | 4250 | const bool isPointerEvent = | 
|  | 4251 | isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER); | 
|  | 4252 | // If a pointer event has no displayId specified, inject it to the default display. | 
|  | 4253 | const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE) | 
|  | 4254 | ? ADISPLAY_ID_DEFAULT | 
|  | 4255 | : event->getDisplayId(); | 
|  | 4256 | const size_t pointerCount = motionEvent.getPointerCount(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4257 | const PointerProperties* pointerProperties = motionEvent.getPointerProperties(); | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4258 | const int32_t actionButton = motionEvent.getActionButton(); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4259 | int32_t flags = motionEvent.getFlags(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4260 | if (!validateMotionEvent(action, actionButton, pointerCount, pointerProperties)) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4261 | return InputEventInjectionResult::FAILED; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4262 | } | 
|  | 4263 |  | 
|  | 4264 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4265 | nsecs_t eventTime = motionEvent.getEventTime(); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4266 | android::base::Timer t; | 
|  | 4267 | mPolicy->interceptMotionBeforeQueueing(displayId, eventTime, /*byref*/ policyFlags); | 
|  | 4268 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 4269 | ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms", | 
|  | 4270 | std::to_string(t.duration().count()).c_str()); | 
|  | 4271 | } | 
|  | 4272 | } | 
|  | 4273 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4274 | if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) { | 
|  | 4275 | flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT; | 
|  | 4276 | } | 
|  | 4277 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4278 | mLock.lock(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4279 | const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes(); | 
|  | 4280 | const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords(); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4281 | std::unique_ptr<MotionEntry> injectedEntry = | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4282 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, | 
|  | 4283 | resolvedDeviceId, motionEvent.getSource(), | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4284 | displayId, policyFlags, action, actionButton, | 
|  | 4285 | flags, motionEvent.getMetaState(), | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4286 | motionEvent.getButtonState(), | 
|  | 4287 | motionEvent.getClassification(), | 
|  | 4288 | motionEvent.getEdgeFlags(), | 
|  | 4289 | motionEvent.getXPrecision(), | 
|  | 4290 | motionEvent.getYPrecision(), | 
|  | 4291 | motionEvent.getRawXCursorPosition(), | 
|  | 4292 | motionEvent.getRawYCursorPosition(), | 
|  | 4293 | motionEvent.getDownTime(), uint32_t(pointerCount), | 
| Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4294 | pointerProperties, samplePointerCoords); | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4295 | transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4296 | injectedEntries.push(std::move(injectedEntry)); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4297 | for (size_t i = motionEvent.getHistorySize(); i > 0; i--) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4298 | sampleEventTimes += 1; | 
|  | 4299 | samplePointerCoords += pointerCount; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4300 | std::unique_ptr<MotionEntry> nextInjectedEntry = | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4301 | std::make_unique<MotionEntry>(motionEvent.getId(), *sampleEventTimes, | 
|  | 4302 | resolvedDeviceId, motionEvent.getSource(), | 
| Prabir Pradhan | aa561d1 | 2021-09-24 06:57:33 -0700 | [diff] [blame] | 4303 | displayId, policyFlags, action, actionButton, | 
|  | 4304 | flags, motionEvent.getMetaState(), | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4305 | motionEvent.getButtonState(), | 
|  | 4306 | motionEvent.getClassification(), | 
|  | 4307 | motionEvent.getEdgeFlags(), | 
|  | 4308 | motionEvent.getXPrecision(), | 
|  | 4309 | motionEvent.getYPrecision(), | 
|  | 4310 | motionEvent.getRawXCursorPosition(), | 
|  | 4311 | motionEvent.getRawYCursorPosition(), | 
|  | 4312 | motionEvent.getDownTime(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4313 | uint32_t(pointerCount), pointerProperties, | 
| Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4314 | samplePointerCoords); | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4315 | transformMotionEntryForInjectionLocked(*nextInjectedEntry, | 
|  | 4316 | motionEvent.getTransform()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4317 | injectedEntries.push(std::move(nextInjectedEntry)); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4318 | } | 
|  | 4319 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4320 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4321 |  | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4322 | default: | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 4323 | ALOGW("Cannot inject %s events", inputEventTypeToString(event->getType())); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4324 | return InputEventInjectionResult::FAILED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4325 | } | 
|  | 4326 |  | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4327 | InjectionState* injectionState = new InjectionState(targetUid); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4328 | if (syncMode == InputEventInjectionSync::NONE) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4329 | injectionState->injectionIsAsync = true; | 
|  | 4330 | } | 
|  | 4331 |  | 
|  | 4332 | injectionState->refCount += 1; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4333 | injectedEntries.back()->injectionState = injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4334 |  | 
|  | 4335 | bool needWake = false; | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4336 | while (!injectedEntries.empty()) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4337 | needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front())); | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 4338 | injectedEntries.pop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4339 | } | 
|  | 4340 |  | 
|  | 4341 | mLock.unlock(); | 
|  | 4342 |  | 
|  | 4343 | if (needWake) { | 
|  | 4344 | mLooper->wake(); | 
|  | 4345 | } | 
|  | 4346 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4347 | InputEventInjectionResult injectionResult; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4348 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4349 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4350 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4351 | if (syncMode == InputEventInjectionSync::NONE) { | 
|  | 4352 | injectionResult = InputEventInjectionResult::SUCCEEDED; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4353 | } else { | 
|  | 4354 | for (;;) { | 
|  | 4355 | injectionResult = injectionState->injectionResult; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4356 | if (injectionResult != InputEventInjectionResult::PENDING) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4357 | break; | 
|  | 4358 | } | 
|  | 4359 |  | 
|  | 4360 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4361 | if (remainingTimeout <= 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4362 | if (DEBUG_INJECTION) { | 
|  | 4363 | ALOGD("injectInputEvent - Timed out waiting for injection result " | 
|  | 4364 | "to become available."); | 
|  | 4365 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4366 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4367 | break; | 
|  | 4368 | } | 
|  | 4369 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4370 | mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4371 | } | 
|  | 4372 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4373 | if (injectionResult == InputEventInjectionResult::SUCCEEDED && | 
|  | 4374 | syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4375 | while (injectionState->pendingForegroundDispatches != 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4376 | if (DEBUG_INJECTION) { | 
|  | 4377 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", | 
|  | 4378 | injectionState->pendingForegroundDispatches); | 
|  | 4379 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4380 | nsecs_t remainingTimeout = endTime - now(); | 
|  | 4381 | if (remainingTimeout <= 0) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4382 | if (DEBUG_INJECTION) { | 
|  | 4383 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " | 
|  | 4384 | "dispatches to finish."); | 
|  | 4385 | } | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4386 | injectionResult = InputEventInjectionResult::TIMED_OUT; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4387 | break; | 
|  | 4388 | } | 
|  | 4389 |  | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4390 | mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4391 | } | 
|  | 4392 | } | 
|  | 4393 | } | 
|  | 4394 |  | 
|  | 4395 | injectionState->release(); | 
|  | 4396 | } // release lock | 
|  | 4397 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4398 | if (DEBUG_INJECTION) { | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4399 | ALOGD("injectInputEvent - Finished with result %d.", injectionResult); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4400 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4401 |  | 
|  | 4402 | return injectionResult; | 
|  | 4403 | } | 
|  | 4404 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4405 | std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) { | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4406 | std::array<uint8_t, 32> calculatedHmac; | 
|  | 4407 | std::unique_ptr<VerifiedInputEvent> result; | 
|  | 4408 | switch (event.getType()) { | 
|  | 4409 | case AINPUT_EVENT_TYPE_KEY: { | 
|  | 4410 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 4411 | VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent); | 
|  | 4412 | result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4413 | calculatedHmac = sign(verifiedKeyEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4414 | break; | 
|  | 4415 | } | 
|  | 4416 | case AINPUT_EVENT_TYPE_MOTION: { | 
|  | 4417 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 4418 | VerifiedMotionEvent verifiedMotionEvent = | 
|  | 4419 | verifiedMotionEventFromMotionEvent(motionEvent); | 
|  | 4420 | result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent); | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4421 | calculatedHmac = sign(verifiedMotionEvent); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4422 | break; | 
|  | 4423 | } | 
|  | 4424 | default: { | 
|  | 4425 | ALOGE("Cannot verify events of type %" PRId32, event.getType()); | 
|  | 4426 | return nullptr; | 
|  | 4427 | } | 
|  | 4428 | } | 
|  | 4429 | if (calculatedHmac == INVALID_HMAC) { | 
|  | 4430 | return nullptr; | 
|  | 4431 | } | 
|  | 4432 | if (calculatedHmac != event.getHmac()) { | 
|  | 4433 | return nullptr; | 
|  | 4434 | } | 
|  | 4435 | return result; | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 4436 | } | 
|  | 4437 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4438 | void InputDispatcher::setInjectionResult(EventEntry& entry, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4439 | InputEventInjectionResult injectionResult) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4440 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4441 | if (injectionState) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4442 | if (DEBUG_INJECTION) { | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4443 | ALOGD("Setting input event injection result to %d.", injectionResult); | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 4444 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4445 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4446 | if (injectionState->injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4447 | // Log the outcome since the injector did not wait for the injection result. | 
|  | 4448 | switch (injectionResult) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4449 | case InputEventInjectionResult::SUCCEEDED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4450 | ALOGV("Asynchronous input event injection succeeded."); | 
|  | 4451 | break; | 
| Prabir Pradhan | 8a2b1a4 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4452 | case InputEventInjectionResult::TARGET_MISMATCH: | 
|  | 4453 | ALOGV("Asynchronous input event injection target mismatch."); | 
|  | 4454 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4455 | case InputEventInjectionResult::FAILED: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4456 | ALOGW("Asynchronous input event injection failed."); | 
|  | 4457 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4458 | case InputEventInjectionResult::TIMED_OUT: | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4459 | ALOGW("Asynchronous input event injection timed out."); | 
|  | 4460 | break; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4461 | case InputEventInjectionResult::PENDING: | 
|  | 4462 | ALOGE("Setting result to 'PENDING' for asynchronous injection"); | 
|  | 4463 | break; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4464 | } | 
|  | 4465 | } | 
|  | 4466 |  | 
|  | 4467 | injectionState->injectionResult = injectionResult; | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4468 | mInjectionResultAvailable.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4469 | } | 
|  | 4470 | } | 
|  | 4471 |  | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4472 | void InputDispatcher::transformMotionEntryForInjectionLocked( | 
|  | 4473 | MotionEntry& entry, const ui::Transform& injectedTransform) const { | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4474 | // Input injection works in the logical display coordinate space, but the input pipeline works | 
|  | 4475 | // display space, so we need to transform the injected events accordingly. | 
|  | 4476 | const auto it = mDisplayInfos.find(entry.displayId); | 
|  | 4477 | if (it == mDisplayInfos.end()) return; | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4478 | const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform; | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4479 |  | 
| Prabir Pradhan | d9a2ebe | 2022-07-20 19:25:13 +0000 | [diff] [blame] | 4480 | if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && | 
|  | 4481 | entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { | 
|  | 4482 | const vec2 cursor = | 
|  | 4483 | MotionEvent::calculateTransformedXY(entry.source, transformToDisplay, | 
|  | 4484 | {entry.xCursorPosition, entry.yCursorPosition}); | 
|  | 4485 | entry.xCursorPosition = cursor.x; | 
|  | 4486 | entry.yCursorPosition = cursor.y; | 
|  | 4487 | } | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4488 | for (uint32_t i = 0; i < entry.pointerCount; i++) { | 
| Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 4489 | entry.pointerCoords[i] = | 
|  | 4490 | MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay, | 
|  | 4491 | entry.pointerCoords[i]); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4492 | } | 
|  | 4493 | } | 
|  | 4494 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4495 | void InputDispatcher::incrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4496 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4497 | if (injectionState) { | 
|  | 4498 | injectionState->pendingForegroundDispatches += 1; | 
|  | 4499 | } | 
|  | 4500 | } | 
|  | 4501 |  | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 4502 | void InputDispatcher::decrementPendingForegroundDispatches(EventEntry& entry) { | 
|  | 4503 | InjectionState* injectionState = entry.injectionState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4504 | if (injectionState) { | 
|  | 4505 | injectionState->pendingForegroundDispatches -= 1; | 
|  | 4506 |  | 
|  | 4507 | if (injectionState->pendingForegroundDispatches == 0) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4508 | mInjectionSyncFinished.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4509 | } | 
|  | 4510 | } | 
|  | 4511 | } | 
|  | 4512 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4513 | const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked( | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 4514 | int32_t displayId) const { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4515 | static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES; | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4516 | auto it = mWindowHandlesByDisplay.find(displayId); | 
|  | 4517 | return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4518 | } | 
|  | 4519 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4520 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4521 | const sp<IBinder>& windowHandleToken) const { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4522 | if (windowHandleToken == nullptr) { | 
|  | 4523 | return nullptr; | 
|  | 4524 | } | 
|  | 4525 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4526 | for (auto& it : mWindowHandlesByDisplay) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4527 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; | 
|  | 4528 | for (const sp<WindowInfoHandle>& windowHandle : windowHandles) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 4529 | if (windowHandle->getToken() == windowHandleToken) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4530 | return windowHandle; | 
|  | 4531 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4532 | } | 
|  | 4533 | } | 
| Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 4534 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4535 | } | 
|  | 4536 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4537 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(const sp<IBinder>& windowHandleToken, | 
|  | 4538 | int displayId) const { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4539 | if (windowHandleToken == nullptr) { | 
|  | 4540 | return nullptr; | 
|  | 4541 | } | 
|  | 4542 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4543 | for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(displayId)) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4544 | if (windowHandle->getToken() == windowHandleToken) { | 
|  | 4545 | return windowHandle; | 
|  | 4546 | } | 
|  | 4547 | } | 
|  | 4548 | return nullptr; | 
|  | 4549 | } | 
|  | 4550 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4551 | sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked( | 
|  | 4552 | const sp<WindowInfoHandle>& windowHandle) const { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4553 | for (auto& it : mWindowHandlesByDisplay) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4554 | const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second; | 
|  | 4555 | for (const sp<WindowInfoHandle>& handle : windowHandles) { | 
| arthurhung | be73767 | 2020-06-24 12:29:21 +0800 | [diff] [blame] | 4556 | if (handle->getId() == windowHandle->getId() && | 
|  | 4557 | handle->getToken() == windowHandle->getToken()) { | 
| Mady Mellor | 017bcd1 | 2020-06-23 19:12:00 +0000 | [diff] [blame] | 4558 | if (windowHandle->getInfo()->displayId != it.first) { | 
|  | 4559 | ALOGE("Found window %s in display %" PRId32 | 
|  | 4560 | ", but it should belong to display %" PRId32, | 
|  | 4561 | windowHandle->getName().c_str(), it.first, | 
|  | 4562 | windowHandle->getInfo()->displayId); | 
|  | 4563 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4564 | return handle; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4565 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4566 | } | 
|  | 4567 | } | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4568 | return nullptr; | 
|  | 4569 | } | 
|  | 4570 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4571 | sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const { | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4572 | sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId); | 
|  | 4573 | return getWindowHandleLocked(focusedToken, displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4574 | } | 
|  | 4575 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4576 | bool InputDispatcher::hasResponsiveConnectionLocked(WindowInfoHandle& windowHandle) const { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4577 | sp<Connection> connection = getConnectionLocked(windowHandle.getToken()); | 
|  | 4578 | const bool noInputChannel = | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4579 | windowHandle.getInfo()->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4580 | if (connection != nullptr && noInputChannel) { | 
|  | 4581 | ALOGW("%s has feature NO_INPUT_CHANNEL, but it matched to connection %s", | 
|  | 4582 | windowHandle.getName().c_str(), connection->inputChannel->getName().c_str()); | 
|  | 4583 | return false; | 
|  | 4584 | } | 
|  | 4585 |  | 
|  | 4586 | if (connection == nullptr) { | 
|  | 4587 | if (!noInputChannel) { | 
|  | 4588 | ALOGI("Could not find connection for %s", windowHandle.getName().c_str()); | 
|  | 4589 | } | 
|  | 4590 | return false; | 
|  | 4591 | } | 
|  | 4592 | if (!connection->responsive) { | 
|  | 4593 | ALOGW("Window %s is not responsive", windowHandle.getName().c_str()); | 
|  | 4594 | return false; | 
|  | 4595 | } | 
|  | 4596 | return true; | 
|  | 4597 | } | 
|  | 4598 |  | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4599 | std::shared_ptr<InputChannel> InputDispatcher::getInputChannelLocked( | 
|  | 4600 | const sp<IBinder>& token) const { | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4601 | auto connectionIt = mConnectionsByToken.find(token); | 
|  | 4602 | if (connectionIt == mConnectionsByToken.end()) { | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4603 | return nullptr; | 
|  | 4604 | } | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 4605 | return connectionIt->second->inputChannel; | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 4606 | } | 
|  | 4607 |  | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4608 | void InputDispatcher::updateWindowHandlesForDisplayLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4609 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { | 
|  | 4610 | if (windowInfoHandles.empty()) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4611 | // Remove all handles on a display if there are no windows left. | 
|  | 4612 | mWindowHandlesByDisplay.erase(displayId); | 
|  | 4613 | return; | 
|  | 4614 | } | 
|  | 4615 |  | 
|  | 4616 | // Since we compare the pointer of input window handles across window updates, we need | 
|  | 4617 | // to make sure the handle object for the same window stays unchanged across updates. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4618 | const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId); | 
|  | 4619 | std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById; | 
|  | 4620 | for (const sp<WindowInfoHandle>& handle : oldHandles) { | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4621 | oldHandlesById[handle->getId()] = handle; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4622 | } | 
|  | 4623 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4624 | std::vector<sp<WindowInfoHandle>> newHandles; | 
|  | 4625 | for (const sp<WindowInfoHandle>& handle : windowInfoHandles) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4626 | const WindowInfo* info = handle->getInfo(); | 
| Siarhei Vishniakou | 6445293 | 2020-11-06 17:51:32 -0600 | [diff] [blame] | 4627 | if (getInputChannelLocked(handle->getToken()) == nullptr) { | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4628 | const bool noInputChannel = | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4629 | info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4630 | const bool canReceiveInput = | 
|  | 4631 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) || | 
|  | 4632 | !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4633 | if (canReceiveInput && !noInputChannel) { | 
| John Reck | e071058 | 2019-09-26 13:46:12 -0700 | [diff] [blame] | 4634 | ALOGV("Window handle %s has no registered input channel", | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4635 | handle->getName().c_str()); | 
| Robert Carr | 2984b7a | 2020-04-13 17:06:45 -0700 | [diff] [blame] | 4636 | continue; | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4637 | } | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4638 | } | 
|  | 4639 |  | 
|  | 4640 | if (info->displayId != displayId) { | 
|  | 4641 | ALOGE("Window %s updated by wrong display %d, should belong to display %d", | 
|  | 4642 | handle->getName().c_str(), displayId, info->displayId); | 
|  | 4643 | continue; | 
|  | 4644 | } | 
|  | 4645 |  | 
| Robert Carr | edd1360 | 2020-04-13 17:24:34 -0700 | [diff] [blame] | 4646 | if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) && | 
|  | 4647 | (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4648 | const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId()); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4649 | oldHandle->updateFrom(handle); | 
|  | 4650 | newHandles.push_back(oldHandle); | 
|  | 4651 | } else { | 
|  | 4652 | newHandles.push_back(handle); | 
|  | 4653 | } | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | // Insert or replace | 
|  | 4657 | mWindowHandlesByDisplay[displayId] = newHandles; | 
|  | 4658 | } | 
|  | 4659 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4660 | void InputDispatcher::setInputWindows( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4661 | const std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>>& handlesPerDisplay) { | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 4662 | // TODO(b/198444055): Remove setInputWindows from InputDispatcher. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4663 | { // acquire lock | 
|  | 4664 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | 2508b87 | 2020-12-03 16:33:53 -1000 | [diff] [blame] | 4665 | for (const auto& [displayId, handles] : handlesPerDisplay) { | 
|  | 4666 | setInputWindowsLocked(handles, displayId); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4667 | } | 
|  | 4668 | } | 
|  | 4669 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4670 | mLooper->wake(); | 
|  | 4671 | } | 
|  | 4672 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4673 | /** | 
|  | 4674 | * Called from InputManagerService, update window handle list by displayId that can receive input. | 
|  | 4675 | * A window handle contains information about InputChannel, Touch Region, Types, Focused,... | 
|  | 4676 | * If set an empty list, remove all handles from the specific display. | 
|  | 4677 | * For focused handle, check if need to change and send a cancel event to previous one. | 
|  | 4678 | * For removed handle, check if need to send a cancel event if already in touch. | 
|  | 4679 | */ | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4680 | void InputDispatcher::setInputWindowsLocked( | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4681 | const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4682 | if (DEBUG_FOCUS) { | 
|  | 4683 | std::string windowList; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4684 | for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4685 | windowList += iwh->getName() + " "; | 
|  | 4686 | } | 
|  | 4687 | ALOGD("setInputWindows displayId=%" PRId32 " %s", displayId, windowList.c_str()); | 
|  | 4688 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4689 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4690 | // Check preconditions for new input windows | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4691 | for (const sp<WindowInfoHandle>& window : windowInfoHandles) { | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4692 | const WindowInfo& info = *window->getInfo(); | 
|  | 4693 |  | 
|  | 4694 | // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 4695 | const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4696 | if (noInputWindow && window->getToken() != nullptr) { | 
|  | 4697 | ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing", | 
|  | 4698 | window->getName().c_str()); | 
|  | 4699 | window->releaseChannel(); | 
|  | 4700 | } | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4701 |  | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4702 | // Ensure all spy windows are trusted overlays | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4703 | LOG_ALWAYS_FATAL_IF(info.isSpy() && | 
|  | 4704 | !info.inputConfig.test( | 
|  | 4705 | WindowInfo::InputConfig::TRUSTED_OVERLAY), | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 4706 | "%s has feature SPY, but is not a trusted overlay.", | 
|  | 4707 | window->getName().c_str()); | 
|  | 4708 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4709 | // Ensure all stylus interceptors are trusted overlays | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4710 | LOG_ALWAYS_FATAL_IF(info.interceptsStylus() && | 
|  | 4711 | !info.inputConfig.test( | 
|  | 4712 | WindowInfo::InputConfig::TRUSTED_OVERLAY), | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 4713 | "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.", | 
|  | 4714 | window->getName().c_str()); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 4715 | } | 
|  | 4716 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4717 | // Copy old handles for release if they are no longer present. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4718 | const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4719 |  | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4720 | // Save the old windows' orientation by ID before it gets updated. | 
|  | 4721 | std::unordered_map<int32_t, uint32_t> oldWindowOrientations; | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4722 | for (const sp<WindowInfoHandle>& handle : oldWindowHandles) { | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4723 | oldWindowOrientations.emplace(handle->getId(), | 
|  | 4724 | handle->getInfo()->transform.getOrientation()); | 
|  | 4725 | } | 
|  | 4726 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4727 | updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId); | 
| Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 4728 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4729 | const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId); | 
| Tommy Nordgren | ff0c66e | 2022-10-13 11:25:57 +0200 | [diff] [blame^] | 4730 | if (mLastHoverWindowHandle) { | 
|  | 4731 | const WindowInfo* lastHoverWindowInfo = mLastHoverWindowHandle->getInfo(); | 
|  | 4732 | if (lastHoverWindowInfo->displayId == displayId && | 
|  | 4733 | std::find(windowHandles.begin(), windowHandles.end(), mLastHoverWindowHandle) == | 
|  | 4734 | windowHandles.end()) { | 
|  | 4735 | mLastHoverWindowHandle = nullptr; | 
|  | 4736 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4737 | } | 
|  | 4738 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4739 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 4740 | mFocusResolver.setInputWindows(displayId, windowHandles); | 
|  | 4741 | if (changes) { | 
|  | 4742 | onFocusChangedLocked(*changes); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4743 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4744 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 4745 | std::unordered_map<int32_t, TouchState>::iterator stateIt = | 
|  | 4746 | mTouchStatesByDisplay.find(displayId); | 
|  | 4747 | if (stateIt != mTouchStatesByDisplay.end()) { | 
|  | 4748 | TouchState& state = stateIt->second; | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4749 | for (size_t i = 0; i < state.windows.size();) { | 
|  | 4750 | TouchedWindow& touchedWindow = state.windows[i]; | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4751 | if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4752 | if (DEBUG_FOCUS) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4753 | ALOGD("Touched window was removed: %s in display %" PRId32, | 
|  | 4754 | touchedWindow.windowHandle->getName().c_str(), displayId); | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4755 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4756 | std::shared_ptr<InputChannel> touchedInputChannel = | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4757 | getInputChannelLocked(touchedWindow.windowHandle->getToken()); | 
|  | 4758 | if (touchedInputChannel != nullptr) { | 
|  | 4759 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4760 | "touched window was removed"); | 
|  | 4761 | synthesizeCancelationEventsForInputChannelLocked(touchedInputChannel, options); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4762 | // Since we are about to drop the touch, cancel the events for the wallpaper as | 
|  | 4763 | // well. | 
|  | 4764 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND && | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4765 | touchedWindow.windowHandle->getInfo()->inputConfig.test( | 
|  | 4766 | gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) { | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4767 | sp<WindowInfoHandle> wallpaper = state.getWallpaperWindow(); | 
|  | 4768 | if (wallpaper != nullptr) { | 
|  | 4769 | sp<Connection> wallpaperConnection = | 
|  | 4770 | getConnectionLocked(wallpaper->getToken()); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 4771 | if (wallpaperConnection != nullptr) { | 
|  | 4772 | synthesizeCancelationEventsForConnectionLocked(wallpaperConnection, | 
|  | 4773 | options); | 
|  | 4774 | } | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4775 | } | 
|  | 4776 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4777 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4778 | state.windows.erase(state.windows.begin() + i); | 
|  | 4779 | } else { | 
|  | 4780 | ++i; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4781 | } | 
|  | 4782 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4783 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4784 | // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4785 | // could just clear the state here. | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4786 | if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId && | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4787 | std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) == | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4788 | windowHandles.end()) { | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 4789 | ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str()); | 
|  | 4790 | sendDropWindowCommandLocked(nullptr, 0, 0); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 4791 | mDragState.reset(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 4792 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4793 | } | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4794 |  | 
| Prabir Pradhan | 8b89c2f | 2021-07-29 16:30:14 +0000 | [diff] [blame] | 4795 | // Determine if the orientation of any of the input windows have changed, and cancel all | 
|  | 4796 | // pointer events if necessary. | 
|  | 4797 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { | 
|  | 4798 | const sp<WindowInfoHandle> newWindowHandle = getWindowHandleLocked(oldWindowHandle); | 
|  | 4799 | if (newWindowHandle != nullptr && | 
|  | 4800 | newWindowHandle->getInfo()->transform.getOrientation() != | 
|  | 4801 | oldWindowOrientations[oldWindowHandle->getId()]) { | 
|  | 4802 | std::shared_ptr<InputChannel> inputChannel = | 
|  | 4803 | getInputChannelLocked(newWindowHandle->getToken()); | 
|  | 4804 | if (inputChannel != nullptr) { | 
|  | 4805 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 4806 | "touched window's orientation changed"); | 
|  | 4807 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
| Prabir Pradhan | 93a0f91 | 2021-04-21 13:47:42 -0700 | [diff] [blame] | 4808 | } | 
|  | 4809 | } | 
|  | 4810 | } | 
|  | 4811 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4812 | // Release information for windows that are no longer present. | 
|  | 4813 | // This ensures that unused input channels are released promptly. | 
|  | 4814 | // Otherwise, they might stick around until the window handle is destroyed | 
|  | 4815 | // which might not happen until the next GC. | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 4816 | for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) { | 
| Prabir Pradhan | 6a9a831 | 2021-04-23 11:59:31 -0700 | [diff] [blame] | 4817 | if (getWindowHandleLocked(oldWindowHandle) == nullptr) { | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4818 | if (DEBUG_FOCUS) { | 
|  | 4819 | ALOGD("Window went away: %s", oldWindowHandle->getName().c_str()); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4820 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4821 | oldWindowHandle->releaseChannel(); | 
| Arthur Hung | 25e2af1 | 2020-03-26 12:58:37 +0000 | [diff] [blame] | 4822 | } | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 4823 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4824 | } | 
|  | 4825 |  | 
|  | 4826 | void InputDispatcher::setFocusedApplication( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4827 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4828 | if (DEBUG_FOCUS) { | 
|  | 4829 | ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId, | 
|  | 4830 | inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>"); | 
|  | 4831 | } | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 4832 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4833 | std::scoped_lock _l(mLock); | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4834 | setFocusedApplicationLocked(displayId, inputApplicationHandle); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4835 | } // release lock | 
|  | 4836 |  | 
|  | 4837 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4838 | mLooper->wake(); | 
|  | 4839 | } | 
|  | 4840 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4841 | void InputDispatcher::setFocusedApplicationLocked( | 
|  | 4842 | int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) { | 
|  | 4843 | std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle = | 
|  | 4844 | getValueByKey(mFocusedApplicationHandlesByDisplay, displayId); | 
|  | 4845 |  | 
|  | 4846 | if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) { | 
|  | 4847 | return; // This application is already focused. No need to wake up or change anything. | 
|  | 4848 | } | 
|  | 4849 |  | 
|  | 4850 | // Set the new application handle. | 
|  | 4851 | if (inputApplicationHandle != nullptr) { | 
|  | 4852 | mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle; | 
|  | 4853 | } else { | 
|  | 4854 | mFocusedApplicationHandlesByDisplay.erase(displayId); | 
|  | 4855 | } | 
|  | 4856 |  | 
|  | 4857 | // No matter what the old focused application was, stop waiting on it because it is | 
|  | 4858 | // no longer focused. | 
|  | 4859 | resetNoFocusedWindowTimeoutLocked(); | 
|  | 4860 | } | 
|  | 4861 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4862 | /** | 
|  | 4863 | * Sets the focused display, which is responsible for receiving focus-dispatched input events where | 
|  | 4864 | * the display not specified. | 
|  | 4865 | * | 
|  | 4866 | * We track any unreleased events for each window. If a window loses the ability to receive the | 
|  | 4867 | * released event, we will send a cancel event to it. So when the focused display is changed, we | 
|  | 4868 | * cancel all the unreleased display-unspecified events for the focused window on the old focused | 
|  | 4869 | * display. The display-specified events won't be affected. | 
|  | 4870 | */ | 
|  | 4871 | void InputDispatcher::setFocusedDisplay(int32_t displayId) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4872 | if (DEBUG_FOCUS) { | 
|  | 4873 | ALOGD("setFocusedDisplay displayId=%" PRId32, displayId); | 
|  | 4874 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4875 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4876 | std::scoped_lock _l(mLock); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4877 |  | 
|  | 4878 | if (mFocusedDisplayId != displayId) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4879 | sp<IBinder> oldFocusedWindowToken = | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4880 | mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4881 | if (oldFocusedWindowToken != nullptr) { | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 4882 | std::shared_ptr<InputChannel> inputChannel = | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4883 | getInputChannelLocked(oldFocusedWindowToken); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4884 | if (inputChannel != nullptr) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 4885 | CancelationOptions | 
|  | 4886 | options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 4887 | "The display which contains this window no longer has focus."); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 4888 | options.displayId = ADISPLAY_ID_NONE; | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4889 | synthesizeCancelationEventsForInputChannelLocked(inputChannel, options); | 
|  | 4890 | } | 
|  | 4891 | } | 
|  | 4892 | mFocusedDisplayId = displayId; | 
|  | 4893 |  | 
| Chris Ye | 3c2d6f5 | 2020-08-09 10:39:48 -0700 | [diff] [blame] | 4894 | // Find new focused window and validate | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4895 | sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 4896 | sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 4897 |  | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4898 | if (newFocusedWindowToken == nullptr) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4899 | ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4900 | if (mFocusResolver.hasFocusedWindowTokens()) { | 
| Vishnu Nair | ad321cd | 2020-08-20 16:40:21 -0700 | [diff] [blame] | 4901 | ALOGE("But another display has a focused window\n%s", | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 4902 | mFocusResolver.dumpFocusedWindows().c_str()); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4903 | } | 
|  | 4904 | } | 
|  | 4905 | } | 
|  | 4906 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4907 | if (DEBUG_FOCUS) { | 
|  | 4908 | logDispatchStateLocked(); | 
|  | 4909 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4910 | } // release lock | 
|  | 4911 |  | 
|  | 4912 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4913 | mLooper->wake(); | 
|  | 4914 | } | 
|  | 4915 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4916 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4917 | if (DEBUG_FOCUS) { | 
|  | 4918 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); | 
|  | 4919 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4920 |  | 
|  | 4921 | bool changed; | 
|  | 4922 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4923 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4924 |  | 
|  | 4925 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { | 
|  | 4926 | if (mDispatchFrozen && !frozen) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4927 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4928 | } | 
|  | 4929 |  | 
|  | 4930 | if (mDispatchEnabled && !enabled) { | 
|  | 4931 | resetAndDropEverythingLocked("dispatcher is being disabled"); | 
|  | 4932 | } | 
|  | 4933 |  | 
|  | 4934 | mDispatchEnabled = enabled; | 
|  | 4935 | mDispatchFrozen = frozen; | 
|  | 4936 | changed = true; | 
|  | 4937 | } else { | 
|  | 4938 | changed = false; | 
|  | 4939 | } | 
|  | 4940 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4941 | if (DEBUG_FOCUS) { | 
|  | 4942 | logDispatchStateLocked(); | 
|  | 4943 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4944 | } // release lock | 
|  | 4945 |  | 
|  | 4946 | if (changed) { | 
|  | 4947 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 4948 | mLooper->wake(); | 
|  | 4949 | } | 
|  | 4950 | } | 
|  | 4951 |  | 
|  | 4952 | void InputDispatcher::setInputFilterEnabled(bool enabled) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 4953 | if (DEBUG_FOCUS) { | 
|  | 4954 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); | 
|  | 4955 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4956 |  | 
|  | 4957 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 4958 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 4959 |  | 
|  | 4960 | if (mInputFilterEnabled == enabled) { | 
|  | 4961 | return; | 
|  | 4962 | } | 
|  | 4963 |  | 
|  | 4964 | mInputFilterEnabled = enabled; | 
|  | 4965 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); | 
|  | 4966 | } // release lock | 
|  | 4967 |  | 
|  | 4968 | // Wake up poll loop since there might be work to do to drop everything. | 
|  | 4969 | mLooper->wake(); | 
|  | 4970 | } | 
|  | 4971 |  | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4972 | bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, | 
|  | 4973 | bool hasPermission) { | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4974 | bool needWake = false; | 
|  | 4975 | { | 
|  | 4976 | std::scoped_lock lock(mLock); | 
|  | 4977 | if (mInTouchMode == inTouchMode) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4978 | return false; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4979 | } | 
|  | 4980 | if (DEBUG_TOUCH_MODE) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4981 | ALOGD("Request to change touch mode from %s to %s (calling pid=%d, uid=%d, " | 
|  | 4982 | "hasPermission=%s)", | 
|  | 4983 | toString(mInTouchMode), toString(inTouchMode), pid, uid, toString(hasPermission)); | 
|  | 4984 | } | 
|  | 4985 | if (!hasPermission) { | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 4986 | if (!focusedWindowIsOwnedByLocked(pid, uid) && | 
|  | 4987 | !recentWindowsAreOwnedByLocked(pid, uid)) { | 
|  | 4988 | ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " | 
|  | 4989 | "window nor none of the previously interacted window", | 
|  | 4990 | pid, uid); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4991 | return false; | 
|  | 4992 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4993 | } | 
|  | 4994 |  | 
|  | 4995 | // TODO(b/198499018): Store touch mode per display. | 
|  | 4996 | mInTouchMode = inTouchMode; | 
|  | 4997 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4998 | auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode); | 
|  | 4999 | needWake = enqueueInboundEventLocked(std::move(entry)); | 
|  | 5000 | } // release lock | 
|  | 5001 |  | 
|  | 5002 | if (needWake) { | 
|  | 5003 | mLooper->wake(); | 
|  | 5004 | } | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5005 | return true; | 
| Siarhei Vishniakou | f3bc1aa | 2019-11-25 13:48:53 -0800 | [diff] [blame] | 5006 | } | 
|  | 5007 |  | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 5008 | bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { | 
|  | 5009 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
|  | 5010 | if (focusedToken == nullptr) { | 
|  | 5011 | return false; | 
|  | 5012 | } | 
|  | 5013 | sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken); | 
|  | 5014 | return isWindowOwnedBy(windowHandle, pid, uid); | 
|  | 5015 | } | 
|  | 5016 |  | 
|  | 5017 | bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { | 
|  | 5018 | return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), | 
|  | 5019 | [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { | 
|  | 5020 | const sp<WindowInfoHandle> windowHandle = | 
|  | 5021 | getWindowHandleLocked(connectionToken); | 
|  | 5022 | return isWindowOwnedBy(windowHandle, pid, uid); | 
|  | 5023 | }) != mInteractionConnectionTokens.end(); | 
|  | 5024 | } | 
|  | 5025 |  | 
| Bernardo Rufino | ea97d18 | 2020-08-19 14:43:14 +0100 | [diff] [blame] | 5026 | void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) { | 
|  | 5027 | if (opacity < 0 || opacity > 1) { | 
|  | 5028 | LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1"); | 
|  | 5029 | return; | 
|  | 5030 | } | 
|  | 5031 |  | 
|  | 5032 | std::scoped_lock lock(mLock); | 
|  | 5033 | mMaximumObscuringOpacityForTouch = opacity; | 
|  | 5034 | } | 
|  | 5035 |  | 
|  | 5036 | void InputDispatcher::setBlockUntrustedTouchesMode(BlockUntrustedTouchesMode mode) { | 
|  | 5037 | std::scoped_lock lock(mLock); | 
|  | 5038 | mBlockUntrustedTouchesMode = mode; | 
|  | 5039 | } | 
|  | 5040 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5041 | std::pair<TouchState*, TouchedWindow*> InputDispatcher::findTouchStateAndWindowLocked( | 
|  | 5042 | const sp<IBinder>& token) { | 
|  | 5043 | for (auto& [displayId, state] : mTouchStatesByDisplay) { | 
|  | 5044 | for (TouchedWindow& w : state.windows) { | 
|  | 5045 | if (w.windowHandle->getToken() == token) { | 
|  | 5046 | return std::make_pair(&state, &w); | 
|  | 5047 | } | 
|  | 5048 | } | 
|  | 5049 | } | 
|  | 5050 | return std::make_pair(nullptr, nullptr); | 
|  | 5051 | } | 
|  | 5052 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 5053 | bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, | 
|  | 5054 | bool isDragDrop) { | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5055 | if (fromToken == toToken) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5056 | if (DEBUG_FOCUS) { | 
|  | 5057 | ALOGD("Trivial transfer to same window."); | 
|  | 5058 | } | 
| chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 5059 | return true; | 
|  | 5060 | } | 
|  | 5061 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5062 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5063 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5064 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5065 | // Find the target touch state and touched window by fromToken. | 
|  | 5066 | auto [state, touchedWindow] = findTouchStateAndWindowLocked(fromToken); | 
|  | 5067 | if (state == nullptr || touchedWindow == nullptr) { | 
|  | 5068 | ALOGD("Focus transfer failed because from window is not being touched."); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5069 | return false; | 
|  | 5070 | } | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5071 |  | 
|  | 5072 | const int32_t displayId = state->displayId; | 
|  | 5073 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId); | 
|  | 5074 | if (toWindowHandle == nullptr) { | 
|  | 5075 | ALOGW("Cannot transfer focus because to window not found."); | 
|  | 5076 | return false; | 
|  | 5077 | } | 
|  | 5078 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5079 | if (DEBUG_FOCUS) { | 
|  | 5080 | ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s", | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5081 | touchedWindow->windowHandle->getName().c_str(), | 
|  | 5082 | toWindowHandle->getName().c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5083 | } | 
|  | 5084 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5085 | // Erase old window. | 
|  | 5086 | int32_t oldTargetFlags = touchedWindow->targetFlags; | 
|  | 5087 | BitSet32 pointerIds = touchedWindow->pointerIds; | 
|  | 5088 | state->removeWindowByToken(fromToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5089 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5090 | // Add new window. | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 5091 | int32_t newTargetFlags = | 
|  | 5092 | oldTargetFlags & (InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); | 
|  | 5093 | if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) { | 
|  | 5094 | newTargetFlags |= InputTarget::FLAG_FOREGROUND; | 
|  | 5095 | } | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5096 | state->addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5097 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5098 | // Store the dragging window. | 
|  | 5099 | if (isDragDrop) { | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5100 | if (pointerIds.count() != 1) { | 
|  | 5101 | ALOGW("The drag and drop cannot be started when there is no pointer or more than 1" | 
|  | 5102 | " pointer on the window."); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5103 | return false; | 
|  | 5104 | } | 
| Arthur Hung | 0270160 | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 5105 | // Track the pointer id for drag window and generate the drag state. | 
|  | 5106 | const int32_t id = pointerIds.firstMarkedBit(); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 5107 | mDragState = std::make_unique<DragState>(toWindowHandle, id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5108 | } | 
|  | 5109 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5110 | // Synthesize cancel for old window and down for new window. | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5111 | sp<Connection> fromConnection = getConnectionLocked(fromToken); | 
|  | 5112 | sp<Connection> toConnection = getConnectionLocked(toToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5113 | if (fromConnection != nullptr && toConnection != nullptr) { | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5114 | fromConnection->inputState.mergePointerStateTo(toConnection->inputState); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5115 | CancelationOptions | 
|  | 5116 | options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 5117 | "transferring touch focus from this window to another window"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5118 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5119 | synthesizePointerDownEventsForConnectionLocked(toConnection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5120 | } | 
|  | 5121 |  | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5122 | if (DEBUG_FOCUS) { | 
|  | 5123 | logDispatchStateLocked(); | 
|  | 5124 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5125 | } // release lock | 
|  | 5126 |  | 
|  | 5127 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 5128 | mLooper->wake(); | 
|  | 5129 | return true; | 
|  | 5130 | } | 
|  | 5131 |  | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5132 | /** | 
|  | 5133 | * Get the touched foreground window on the given display. | 
|  | 5134 | * Return null if there are no windows touched on that display, or if more than one foreground | 
|  | 5135 | * window is being touched. | 
|  | 5136 | */ | 
|  | 5137 | sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const { | 
|  | 5138 | auto stateIt = mTouchStatesByDisplay.find(displayId); | 
|  | 5139 | if (stateIt == mTouchStatesByDisplay.end()) { | 
|  | 5140 | ALOGI("No touch state on display %" PRId32, displayId); | 
|  | 5141 | return nullptr; | 
|  | 5142 | } | 
|  | 5143 |  | 
|  | 5144 | const TouchState& state = stateIt->second; | 
|  | 5145 | sp<WindowInfoHandle> touchedForegroundWindow; | 
|  | 5146 | // If multiple foreground windows are touched, return nullptr | 
|  | 5147 | for (const TouchedWindow& window : state.windows) { | 
|  | 5148 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
|  | 5149 | if (touchedForegroundWindow != nullptr) { | 
|  | 5150 | ALOGI("Two or more foreground windows: %s and %s", | 
|  | 5151 | touchedForegroundWindow->getName().c_str(), | 
|  | 5152 | window.windowHandle->getName().c_str()); | 
|  | 5153 | return nullptr; | 
|  | 5154 | } | 
|  | 5155 | touchedForegroundWindow = window.windowHandle; | 
|  | 5156 | } | 
|  | 5157 | } | 
|  | 5158 | return touchedForegroundWindow; | 
|  | 5159 | } | 
|  | 5160 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5161 | // Binder call | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5162 | bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) { | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5163 | sp<IBinder> fromToken; | 
|  | 5164 | { // acquire lock | 
|  | 5165 | std::scoped_lock _l(mLock); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5166 | sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5167 | if (toWindowHandle == nullptr) { | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5168 | ALOGW("Could not find window associated with token=%p on display %" PRId32, | 
|  | 5169 | destChannelToken.get(), displayId); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5170 | return false; | 
|  | 5171 | } | 
|  | 5172 |  | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5173 | sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId); | 
|  | 5174 | if (from == nullptr) { | 
|  | 5175 | ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get()); | 
|  | 5176 | return false; | 
|  | 5177 | } | 
|  | 5178 |  | 
|  | 5179 | fromToken = from->getToken(); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5180 | } // release lock | 
|  | 5181 |  | 
|  | 5182 | return transferTouchFocus(fromToken, destChannelToken); | 
|  | 5183 | } | 
|  | 5184 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5185 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { | 
| Siarhei Vishniakou | 8658728 | 2019-09-09 18:20:15 +0100 | [diff] [blame] | 5186 | if (DEBUG_FOCUS) { | 
|  | 5187 | ALOGD("Resetting and dropping all events (%s).", reason); | 
|  | 5188 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5189 |  | 
|  | 5190 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); | 
|  | 5191 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 5192 |  | 
|  | 5193 | resetKeyRepeatLocked(); | 
|  | 5194 | releasePendingEventLocked(); | 
|  | 5195 | drainInboundQueueLocked(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5196 | resetNoFocusedWindowTimeoutLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5197 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5198 | mAnrTracker.clear(); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5199 | mTouchStatesByDisplay.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5200 | mLastHoverWindowHandle.clear(); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5201 | mReplacedKeys.clear(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5202 | } | 
|  | 5203 |  | 
|  | 5204 | void InputDispatcher::logDispatchStateLocked() { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5205 | std::string dump; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5206 | dumpDispatchStateLocked(dump); | 
|  | 5207 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5208 | std::istringstream stream(dump); | 
|  | 5209 | std::string line; | 
|  | 5210 |  | 
|  | 5211 | while (std::getline(stream, line, '\n')) { | 
|  | 5212 | ALOGD("%s", line.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5213 | } | 
|  | 5214 | } | 
|  | 5215 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5216 | std::string InputDispatcher::dumpPointerCaptureStateLocked() { | 
|  | 5217 | std::string dump; | 
|  | 5218 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5219 | dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n", | 
|  | 5220 | toString(mCurrentPointerCaptureRequest.enable)); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5221 |  | 
|  | 5222 | std::string windowName = "None"; | 
|  | 5223 | if (mWindowTokenWithPointerCapture) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5224 | const sp<WindowInfoHandle> captureWindowHandle = | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5225 | getWindowHandleLocked(mWindowTokenWithPointerCapture); | 
|  | 5226 | windowName = captureWindowHandle ? captureWindowHandle->getName().c_str() | 
|  | 5227 | : "token has capture without window"; | 
|  | 5228 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5229 | dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str()); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5230 |  | 
|  | 5231 | return dump; | 
|  | 5232 | } | 
|  | 5233 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5234 | void InputDispatcher::dumpDispatchStateLocked(std::string& dump) { | 
| Siarhei Vishniakou | 043a3ec | 2019-05-01 11:30:46 -0700 | [diff] [blame] | 5235 | dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled)); | 
|  | 5236 | dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen)); | 
|  | 5237 | dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5238 | dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5239 |  | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5240 | if (!mFocusedApplicationHandlesByDisplay.empty()) { | 
|  | 5241 | dump += StringPrintf(INDENT "FocusedApplications:\n"); | 
|  | 5242 | for (auto& it : mFocusedApplicationHandlesByDisplay) { | 
|  | 5243 | const int32_t displayId = it.first; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5244 | const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second; | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5245 | const std::chrono::duration timeout = | 
|  | 5246 | applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5247 | dump += StringPrintf(INDENT2 "displayId=%" PRId32 | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5248 | ", name='%s', dispatchingTimeout=%" PRId64 "ms\n", | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 5249 | displayId, applicationHandle->getName().c_str(), millis(timeout)); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5250 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5251 | } else { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5252 | dump += StringPrintf(INDENT "FocusedApplications: <none>\n"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5253 | } | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5254 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5255 | dump += mFocusResolver.dump(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5256 | dump += dumpPointerCaptureStateLocked(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5257 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5258 | if (!mTouchStatesByDisplay.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5259 | dump += StringPrintf(INDENT "TouchStatesByDisplay:\n"); | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5260 | for (const std::pair<int32_t, TouchState>& pair : mTouchStatesByDisplay) { | 
|  | 5261 | const TouchState& state = pair.second; | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5262 | 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] | 5263 | state.displayId, toString(state.down), toString(state.split), | 
|  | 5264 | state.deviceId, state.source); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5265 | if (!state.windows.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5266 | dump += INDENT3 "Windows:\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5267 | for (size_t i = 0; i < state.windows.size(); i++) { | 
|  | 5268 | const TouchedWindow& touchedWindow = state.windows[i]; | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5269 | dump += StringPrintf(INDENT4 | 
|  | 5270 | "%zu: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", | 
|  | 5271 | i, touchedWindow.windowHandle->getName().c_str(), | 
|  | 5272 | touchedWindow.pointerIds.value, touchedWindow.targetFlags); | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5273 | } | 
|  | 5274 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5275 | dump += INDENT3 "Windows: <none>\n"; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 5276 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5277 | } | 
|  | 5278 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5279 | dump += INDENT "TouchStates: <no displays touched>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5280 | } | 
|  | 5281 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 5282 | if (mDragState) { | 
|  | 5283 | dump += StringPrintf(INDENT "DragState:\n"); | 
|  | 5284 | mDragState->dump(dump, INDENT2); | 
|  | 5285 | } | 
|  | 5286 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5287 | if (!mWindowHandlesByDisplay.empty()) { | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5288 | for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) { | 
|  | 5289 | dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId); | 
|  | 5290 | if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) { | 
|  | 5291 | const auto& displayInfo = it->second; | 
|  | 5292 | dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth, | 
|  | 5293 | displayInfo.logicalHeight); | 
|  | 5294 | displayInfo.transform.dump(dump, "transform", INDENT4); | 
|  | 5295 | } else { | 
|  | 5296 | dump += INDENT2 "No DisplayInfo found!\n"; | 
|  | 5297 | } | 
|  | 5298 |  | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 5299 | if (!windowHandles.empty()) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5300 | dump += INDENT2 "Windows:\n"; | 
|  | 5301 | for (size_t i = 0; i < windowHandles.size(); i++) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5302 | const sp<WindowInfoHandle>& windowHandle = windowHandles[i]; | 
|  | 5303 | const WindowInfo* windowInfo = windowHandle->getInfo(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5304 |  | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5305 | dump += StringPrintf(INDENT3 "%zu: name='%s', id=%" PRId32 ", displayId=%d, " | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5306 | "inputConfig=%s, alpha=%.2f, " | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5307 | "frame=[%d,%d][%d,%d], globalScale=%f, " | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5308 | "applicationInfo.name=%s, " | 
|  | 5309 | "applicationInfo.token=%s, " | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5310 | "touchableRegion=", | 
| Bernardo Rufino | 0f6a36e | 2020-11-11 10:10:59 +0000 | [diff] [blame] | 5311 | i, windowInfo->name.c_str(), windowInfo->id, | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5312 | windowInfo->displayId, | 
|  | 5313 | windowInfo->inputConfig.string().c_str(), | 
|  | 5314 | windowInfo->alpha, windowInfo->frameLeft, | 
|  | 5315 | windowInfo->frameTop, windowInfo->frameRight, | 
|  | 5316 | windowInfo->frameBottom, windowInfo->globalScaleFactor, | 
| Bernardo Rufino | 49d99e4 | 2021-01-18 15:16:59 +0000 | [diff] [blame] | 5317 | windowInfo->applicationInfo.name.c_str(), | 
|  | 5318 | toString(windowInfo->applicationInfo.token).c_str()); | 
| Bernardo Rufino | 53fc31e | 2020-11-03 11:01:07 +0000 | [diff] [blame] | 5319 | dump += dumpRegion(windowInfo->touchableRegion); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5320 | dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5321 | "ms, hasToken=%s, " | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5322 | "touchOcclusionMode=%s\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5323 | windowInfo->ownerPid, windowInfo->ownerUid, | 
| Bernardo Rufino | c2f1fad | 2020-11-04 17:30:57 +0000 | [diff] [blame] | 5324 | millis(windowInfo->dispatchingTimeout), | 
| Bernardo Rufino | 5fd822d | 2020-11-13 16:11:39 +0000 | [diff] [blame] | 5325 | toString(windowInfo->token != nullptr), | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 5326 | toString(windowInfo->touchOcclusionMode).c_str()); | 
| chaviw | 85b4420 | 2020-07-24 11:46:21 -0700 | [diff] [blame] | 5327 | windowInfo->transform.dump(dump, "transform", INDENT4); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5328 | } | 
|  | 5329 | } else { | 
|  | 5330 | dump += INDENT2 "Windows: <none>\n"; | 
|  | 5331 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5332 | } | 
|  | 5333 | } else { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5334 | dump += INDENT "Displays: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5335 | } | 
|  | 5336 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5337 | if (!mGlobalMonitorsByDisplay.empty()) { | 
|  | 5338 | for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) { | 
|  | 5339 | dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5340 | dumpMonitors(dump, monitors); | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5341 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5342 | } else { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5343 | dump += INDENT "Global Monitors: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5344 | } | 
|  | 5345 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5346 | const nsecs_t currentTime = now(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5347 |  | 
|  | 5348 | // Dump recently dispatched or dropped events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5349 | if (!mRecentQueue.empty()) { | 
|  | 5350 | dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5351 | for (std::shared_ptr<EventEntry>& entry : mRecentQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5352 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5353 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5354 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5355 | } | 
|  | 5356 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5357 | dump += INDENT "RecentQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5358 | } | 
|  | 5359 |  | 
|  | 5360 | // Dump event currently being dispatched. | 
|  | 5361 | if (mPendingEvent) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5362 | dump += INDENT "PendingEvent:\n"; | 
|  | 5363 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5364 | dump += mPendingEvent->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5365 | dump += StringPrintf(", age=%" PRId64 "ms\n", | 
|  | 5366 | ns2ms(currentTime - mPendingEvent->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5367 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5368 | dump += INDENT "PendingEvent: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5369 | } | 
|  | 5370 |  | 
|  | 5371 | // Dump inbound events from oldest to newest. | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 5372 | if (!mInboundQueue.empty()) { | 
|  | 5373 | dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size()); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5374 | for (std::shared_ptr<EventEntry>& entry : mInboundQueue) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5375 | dump += INDENT2; | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5376 | dump += entry->getDescription(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5377 | dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5378 | } | 
|  | 5379 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5380 | dump += INDENT "InboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5381 | } | 
|  | 5382 |  | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5383 | if (!mReplacedKeys.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5384 | dump += INDENT "ReplacedKeys:\n"; | 
| Siarhei Vishniakou | 4700f82 | 2020-03-24 19:05:54 -0700 | [diff] [blame] | 5385 | for (const std::pair<KeyReplacement, int32_t>& pair : mReplacedKeys) { | 
|  | 5386 | const KeyReplacement& replacement = pair.first; | 
|  | 5387 | int32_t newKeyCode = pair.second; | 
|  | 5388 | dump += StringPrintf(INDENT2 "originalKeyCode=%d, deviceId=%d -> newKeyCode=%d\n", | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5389 | replacement.keyCode, replacement.deviceId, newKeyCode); | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5390 | } | 
|  | 5391 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5392 | dump += INDENT "ReplacedKeys: <empty>\n"; | 
| Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 5393 | } | 
|  | 5394 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5395 | if (!mCommandQueue.empty()) { | 
|  | 5396 | dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size()); | 
|  | 5397 | } else { | 
|  | 5398 | dump += INDENT "CommandQueue: <empty>\n"; | 
|  | 5399 | } | 
|  | 5400 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5401 | if (!mConnectionsByToken.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5402 | dump += INDENT "Connections:\n"; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5403 | for (const auto& [token, connection] : mConnectionsByToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5404 | dump += StringPrintf(INDENT2 "%i: channelName='%s', windowName='%s', " | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5405 | "status=%s, monitor=%s, responsive=%s\n", | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5406 | connection->inputChannel->getFd().get(), | 
|  | 5407 | connection->getInputChannelName().c_str(), | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5408 | connection->getWindowName().c_str(), | 
|  | 5409 | ftl::enum_string(connection->status).c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5410 | toString(connection->monitor), toString(connection->responsive)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5411 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5412 | if (!connection->outboundQueue.empty()) { | 
|  | 5413 | dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n", | 
|  | 5414 | connection->outboundQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5415 | dump += dumpQueue(connection->outboundQueue, currentTime); | 
|  | 5416 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5417 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5418 | dump += INDENT3 "OutboundQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5419 | } | 
|  | 5420 |  | 
| Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 5421 | if (!connection->waitQueue.empty()) { | 
|  | 5422 | dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n", | 
|  | 5423 | connection->waitQueue.size()); | 
| Siarhei Vishniakou | 14411c9 | 2020-09-18 21:15:05 -0500 | [diff] [blame] | 5424 | dump += dumpQueue(connection->waitQueue, currentTime); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5425 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5426 | dump += INDENT3 "WaitQueue: <empty>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5427 | } | 
|  | 5428 | } | 
|  | 5429 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5430 | dump += INDENT "Connections: <none>\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5431 | } | 
|  | 5432 |  | 
|  | 5433 | if (isAppSwitchPendingLocked()) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5434 | dump += StringPrintf(INDENT "AppSwitch: pending, due in %" PRId64 "ms\n", | 
|  | 5435 | ns2ms(mAppSwitchDueTime - now())); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5436 | } else { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5437 | dump += INDENT "AppSwitch: not pending\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5438 | } | 
|  | 5439 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 5440 | dump += INDENT "Configuration:\n"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5441 | dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay)); | 
|  | 5442 | dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n", | 
|  | 5443 | ns2ms(mConfig.keyRepeatTimeout)); | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 5444 | dump += mLatencyTracker.dump(INDENT2); | 
| Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 5445 | dump += mLatencyAggregator.dump(INDENT2); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5446 | } | 
|  | 5447 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5448 | void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) { | 
|  | 5449 | const size_t numMonitors = monitors.size(); | 
|  | 5450 | for (size_t i = 0; i < numMonitors; i++) { | 
|  | 5451 | const Monitor& monitor = monitors[i]; | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 5452 | const std::shared_ptr<InputChannel>& channel = monitor.inputChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5453 | dump += StringPrintf(INDENT2 "%zu: '%s', ", i, channel->getName().c_str()); | 
|  | 5454 | dump += "\n"; | 
|  | 5455 | } | 
|  | 5456 | } | 
|  | 5457 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5458 | class LooperEventCallback : public LooperCallback { | 
|  | 5459 | public: | 
|  | 5460 | LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {} | 
|  | 5461 | int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); } | 
|  | 5462 |  | 
|  | 5463 | private: | 
|  | 5464 | std::function<int(int events)> mCallback; | 
|  | 5465 | }; | 
|  | 5466 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5467 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 5468 | if (DEBUG_CHANNEL_CREATION) { | 
|  | 5469 | ALOGD("channel '%s' ~ createInputChannel", name.c_str()); | 
|  | 5470 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5471 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5472 | std::unique_ptr<InputChannel> serverChannel; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5473 | std::unique_ptr<InputChannel> clientChannel; | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5474 | status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5475 |  | 
|  | 5476 | if (result) { | 
|  | 5477 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5478 | } | 
|  | 5479 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5480 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5481 | std::scoped_lock _l(mLock); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5482 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5483 | int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5484 | sp<Connection> connection = | 
|  | 5485 | new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5486 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5487 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5488 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5489 | } | 
|  | 5490 | mConnectionsByToken.emplace(token, connection); | 
|  | 5491 |  | 
|  | 5492 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5493 | this, std::placeholders::_1, token); | 
|  | 5494 |  | 
|  | 5495 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5496 | } // release lock | 
|  | 5497 |  | 
|  | 5498 | // Wake the looper because some connections have changed. | 
|  | 5499 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5500 | return clientChannel; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5501 | } | 
|  | 5502 |  | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5503 | Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId, | 
| Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 5504 | const std::string& name, | 
|  | 5505 | int32_t pid) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5506 | std::shared_ptr<InputChannel> serverChannel; | 
|  | 5507 | std::unique_ptr<InputChannel> clientChannel; | 
|  | 5508 | status_t result = openInputChannelPair(name, serverChannel, clientChannel); | 
|  | 5509 | if (result) { | 
|  | 5510 | return base::Error(result) << "Failed to open input channel pair with name " << name; | 
|  | 5511 | } | 
|  | 5512 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5513 | { // acquire lock | 
|  | 5514 | std::scoped_lock _l(mLock); | 
|  | 5515 |  | 
|  | 5516 | if (displayId < 0) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5517 | return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name | 
|  | 5518 | << " without a specified display."; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5519 | } | 
|  | 5520 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5521 | sp<Connection> connection = new Connection(serverChannel, true /*monitor*/, mIdGenerator); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5522 | const sp<IBinder>& token = serverChannel->getConnectionToken(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5523 | const int fd = serverChannel->getFd(); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5524 |  | 
|  | 5525 | if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) { | 
|  | 5526 | ALOGE("Created a new connection, but the token %p is already known", token.get()); | 
|  | 5527 | } | 
|  | 5528 | mConnectionsByToken.emplace(token, connection); | 
|  | 5529 | std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback, | 
|  | 5530 | this, std::placeholders::_1, token); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5531 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5532 | mGlobalMonitorsByDisplay[displayId].emplace_back(serverChannel, pid); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5533 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5534 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5535 | } | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5536 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5537 | // Wake the looper because some connections have changed. | 
|  | 5538 | mLooper->wake(); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5539 | return clientChannel; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5540 | } | 
|  | 5541 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5542 | status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5543 | { // acquire lock | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 5544 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5545 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5546 | status_t status = removeInputChannelLocked(connectionToken, false /*notify*/); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5547 | if (status) { | 
|  | 5548 | return status; | 
|  | 5549 | } | 
|  | 5550 | } // release lock | 
|  | 5551 |  | 
|  | 5552 | // Wake the poll loop because removing the connection may have changed the current | 
|  | 5553 | // synchronization state. | 
|  | 5554 | mLooper->wake(); | 
|  | 5555 | return OK; | 
|  | 5556 | } | 
|  | 5557 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5558 | status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken, | 
|  | 5559 | bool notify) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5560 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5561 | if (connection == nullptr) { | 
| Siarhei Vishniakou | d706a28 | 2021-02-13 00:08:48 +0000 | [diff] [blame] | 5562 | // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel' | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5563 | return BAD_VALUE; | 
|  | 5564 | } | 
|  | 5565 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5566 | removeConnectionLocked(connection); | 
| Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 5567 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5568 | if (connection->monitor) { | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5569 | removeMonitorChannelLocked(connectionToken); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5570 | } | 
|  | 5571 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5572 | mLooper->removeFd(connection->inputChannel->getFd()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5573 |  | 
|  | 5574 | nsecs_t currentTime = now(); | 
|  | 5575 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); | 
|  | 5576 |  | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5577 | connection->status = Connection::Status::ZOMBIE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5578 | return OK; | 
|  | 5579 | } | 
|  | 5580 |  | 
| Siarhei Vishniakou | adefc3e | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 5581 | void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5582 | for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) { | 
|  | 5583 | auto& [displayId, monitors] = *it; | 
|  | 5584 | std::erase_if(monitors, [connectionToken](const Monitor& monitor) { | 
|  | 5585 | return monitor.inputChannel->getConnectionToken() == connectionToken; | 
|  | 5586 | }); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5587 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5588 | if (monitors.empty()) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5589 | it = mGlobalMonitorsByDisplay.erase(it); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5590 | } else { | 
|  | 5591 | ++it; | 
|  | 5592 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5593 | } | 
|  | 5594 | } | 
|  | 5595 |  | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5596 | status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5597 | std::scoped_lock _l(mLock); | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5598 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5599 | const std::shared_ptr<InputChannel> requestingChannel = getInputChannelLocked(token); | 
|  | 5600 | if (!requestingChannel) { | 
|  | 5601 | ALOGW("Attempted to pilfer pointers from an un-registered channel or invalid token"); | 
|  | 5602 | return BAD_VALUE; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5603 | } | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5604 |  | 
|  | 5605 | auto [statePtr, windowPtr] = findTouchStateAndWindowLocked(token); | 
|  | 5606 | if (statePtr == nullptr || windowPtr == nullptr || !statePtr->down) { | 
|  | 5607 | ALOGW("Attempted to pilfer points from a channel without any on-going pointer streams." | 
|  | 5608 | " Ignoring."); | 
|  | 5609 | return BAD_VALUE; | 
|  | 5610 | } | 
|  | 5611 |  | 
|  | 5612 | TouchState& state = *statePtr; | 
|  | 5613 |  | 
|  | 5614 | // Send cancel events to all the input channels we're stealing from. | 
|  | 5615 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 5616 | "input channel stole pointer stream"); | 
|  | 5617 | options.deviceId = state.deviceId; | 
|  | 5618 | options.displayId = state.displayId; | 
|  | 5619 | std::string canceledWindows; | 
|  | 5620 | for (const TouchedWindow& window : state.windows) { | 
|  | 5621 | const std::shared_ptr<InputChannel> channel = | 
|  | 5622 | getInputChannelLocked(window.windowHandle->getToken()); | 
|  | 5623 | if (channel != nullptr && channel->getConnectionToken() != token) { | 
|  | 5624 | synthesizeCancelationEventsForInputChannelLocked(channel, options); | 
|  | 5625 | canceledWindows += canceledWindows.empty() ? "[" : ", "; | 
|  | 5626 | canceledWindows += channel->getName(); | 
|  | 5627 | } | 
|  | 5628 | } | 
|  | 5629 | canceledWindows += canceledWindows.empty() ? "[]" : "]"; | 
|  | 5630 | ALOGI("Channel %s is stealing touch from %s", requestingChannel->getName().c_str(), | 
|  | 5631 | canceledWindows.c_str()); | 
|  | 5632 |  | 
| Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5633 | // Prevent the gesture from being sent to any other windows. | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5634 | state.filterWindowsExcept(token); | 
| Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 5635 | state.preventNewTargets = true; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5636 | return OK; | 
|  | 5637 | } | 
|  | 5638 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5639 | void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) { | 
|  | 5640 | { // acquire lock | 
|  | 5641 | std::scoped_lock _l(mLock); | 
|  | 5642 | if (DEBUG_FOCUS) { | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5643 | const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5644 | ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable", | 
|  | 5645 | windowHandle != nullptr ? windowHandle->getName().c_str() | 
|  | 5646 | : "token without window"); | 
|  | 5647 | } | 
|  | 5648 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 5649 | const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5650 | if (focusedToken != windowToken) { | 
|  | 5651 | ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.", | 
|  | 5652 | enabled ? "enable" : "disable"); | 
|  | 5653 | return; | 
|  | 5654 | } | 
|  | 5655 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 5656 | if (enabled == mCurrentPointerCaptureRequest.enable) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5657 | ALOGW("Ignoring request to %s Pointer Capture: " | 
|  | 5658 | "window has %s requested pointer capture.", | 
|  | 5659 | enabled ? "enable" : "disable", enabled ? "already" : "not"); | 
|  | 5660 | return; | 
|  | 5661 | } | 
|  | 5662 |  | 
| Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5663 | if (enabled) { | 
|  | 5664 | if (std::find(mIneligibleDisplaysForPointerCapture.begin(), | 
|  | 5665 | mIneligibleDisplaysForPointerCapture.end(), | 
|  | 5666 | mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) { | 
|  | 5667 | ALOGW("Ignoring request to enable Pointer Capture: display is not eligible"); | 
|  | 5668 | return; | 
|  | 5669 | } | 
|  | 5670 | } | 
|  | 5671 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 5672 | setPointerCaptureLocked(enabled); | 
|  | 5673 | } // release lock | 
|  | 5674 |  | 
|  | 5675 | // Wake the thread to process command entries. | 
|  | 5676 | mLooper->wake(); | 
|  | 5677 | } | 
|  | 5678 |  | 
| Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 5679 | void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) { | 
|  | 5680 | { // acquire lock | 
|  | 5681 | std::scoped_lock _l(mLock); | 
|  | 5682 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); | 
|  | 5683 | if (!isEligible) { | 
|  | 5684 | mIneligibleDisplaysForPointerCapture.push_back(displayId); | 
|  | 5685 | } | 
|  | 5686 | } // release lock | 
|  | 5687 | } | 
|  | 5688 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5689 | std::optional<int32_t> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) { | 
|  | 5690 | for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) { | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5691 | for (const Monitor& monitor : monitors) { | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 5692 | if (monitor.inputChannel->getConnectionToken() == token) { | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5693 | return monitor.pid; | 
| Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 5694 | } | 
|  | 5695 | } | 
|  | 5696 | } | 
|  | 5697 | return std::nullopt; | 
|  | 5698 | } | 
|  | 5699 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5700 | sp<Connection> InputDispatcher::getConnectionLocked(const sp<IBinder>& inputConnectionToken) const { | 
| Siarhei Vishniakou | d0d71b6 | 2019-10-14 14:50:45 -0700 | [diff] [blame] | 5701 | if (inputConnectionToken == nullptr) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5702 | return nullptr; | 
| Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 5703 | } | 
|  | 5704 |  | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5705 | for (const auto& [token, connection] : mConnectionsByToken) { | 
|  | 5706 | if (token == inputConnectionToken) { | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5707 | return connection; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5708 | } | 
|  | 5709 | } | 
| Robert Carr | 4e670e5 | 2018-08-15 13:26:12 -0700 | [diff] [blame] | 5710 |  | 
| Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 5711 | return nullptr; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5712 | } | 
|  | 5713 |  | 
| Siarhei Vishniakou | ad99140 | 2020-10-28 11:40:09 -0500 | [diff] [blame] | 5714 | std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const { | 
|  | 5715 | sp<Connection> connection = getConnectionLocked(connectionToken); | 
|  | 5716 | if (connection == nullptr) { | 
|  | 5717 | return "<nullptr>"; | 
|  | 5718 | } | 
|  | 5719 | return connection->getInputChannelName(); | 
|  | 5720 | } | 
|  | 5721 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5722 | void InputDispatcher::removeConnectionLocked(const sp<Connection>& connection) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5723 | mAnrTracker.eraseToken(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 5724 | mConnectionsByToken.erase(connection->inputChannel->getConnectionToken()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5725 | } | 
|  | 5726 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5727 | void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime, | 
|  | 5728 | const sp<Connection>& connection, uint32_t seq, | 
|  | 5729 | bool handled, nsecs_t consumeTime) { | 
|  | 5730 | // Handle post-event policy actions. | 
|  | 5731 | std::deque<DispatchEntry*>::iterator dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5732 | if (dispatchEntryIt == connection->waitQueue.end()) { | 
|  | 5733 | return; | 
|  | 5734 | } | 
|  | 5735 | DispatchEntry* dispatchEntry = *dispatchEntryIt; | 
|  | 5736 | const nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; | 
|  | 5737 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { | 
|  | 5738 | ALOGI("%s spent %" PRId64 "ms processing %s", connection->getWindowName().c_str(), | 
|  | 5739 | ns2ms(eventDuration), dispatchEntry->eventEntry->getDescription().c_str()); | 
|  | 5740 | } | 
|  | 5741 | if (shouldReportFinishedEvent(*dispatchEntry, *connection)) { | 
|  | 5742 | mLatencyTracker.trackFinishedEvent(dispatchEntry->eventEntry->id, | 
|  | 5743 | connection->inputChannel->getConnectionToken(), | 
|  | 5744 | dispatchEntry->deliveryTime, consumeTime, finishTime); | 
|  | 5745 | } | 
|  | 5746 |  | 
|  | 5747 | bool restartEvent; | 
|  | 5748 | if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { | 
|  | 5749 | KeyEntry& keyEntry = static_cast<KeyEntry&>(*(dispatchEntry->eventEntry)); | 
|  | 5750 | restartEvent = | 
|  | 5751 | afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled); | 
|  | 5752 | } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { | 
|  | 5753 | MotionEntry& motionEntry = static_cast<MotionEntry&>(*(dispatchEntry->eventEntry)); | 
|  | 5754 | restartEvent = afterMotionEventLockedInterruptable(connection, dispatchEntry, motionEntry, | 
|  | 5755 | handled); | 
|  | 5756 | } else { | 
|  | 5757 | restartEvent = false; | 
|  | 5758 | } | 
|  | 5759 |  | 
|  | 5760 | // Dequeue the event and start the next cycle. | 
|  | 5761 | // Because the lock might have been released, it is possible that the | 
|  | 5762 | // contents of the wait queue to have been drained, so we need to double-check | 
|  | 5763 | // a few things. | 
|  | 5764 | dispatchEntryIt = connection->findWaitQueueEntry(seq); | 
|  | 5765 | if (dispatchEntryIt != connection->waitQueue.end()) { | 
|  | 5766 | dispatchEntry = *dispatchEntryIt; | 
|  | 5767 | connection->waitQueue.erase(dispatchEntryIt); | 
|  | 5768 | const sp<IBinder>& connectionToken = connection->inputChannel->getConnectionToken(); | 
|  | 5769 | mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken); | 
|  | 5770 | if (!connection->responsive) { | 
|  | 5771 | connection->responsive = isConnectionResponsive(*connection); | 
|  | 5772 | if (connection->responsive) { | 
|  | 5773 | // The connection was unresponsive, and now it's responsive. | 
|  | 5774 | processConnectionResponsiveLocked(*connection); | 
|  | 5775 | } | 
|  | 5776 | } | 
|  | 5777 | traceWaitQueueLength(*connection); | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 5778 | if (restartEvent && connection->status == Connection::Status::NORMAL) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5779 | connection->outboundQueue.push_front(dispatchEntry); | 
|  | 5780 | traceOutboundQueueLength(*connection); | 
|  | 5781 | } else { | 
|  | 5782 | releaseDispatchEntry(dispatchEntry); | 
|  | 5783 | } | 
|  | 5784 | } | 
|  | 5785 |  | 
|  | 5786 | // Start the next dispatch cycle for this connection. | 
|  | 5787 | startDispatchCycleLocked(now(), connection); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5788 | } | 
|  | 5789 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5790 | void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, | 
|  | 5791 | const sp<IBinder>& newToken) { | 
|  | 5792 | auto command = [this, oldToken, newToken]() REQUIRES(mLock) { | 
|  | 5793 | scoped_unlock unlock(mLock); | 
|  | 5794 | mPolicy->notifyFocusChanged(oldToken, newToken); | 
|  | 5795 | }; | 
|  | 5796 | postCommandLocked(std::move(command)); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5797 | } | 
|  | 5798 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5799 | void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) { | 
|  | 5800 | auto command = [this, token, x, y]() REQUIRES(mLock) { | 
|  | 5801 | scoped_unlock unlock(mLock); | 
|  | 5802 | mPolicy->notifyDropWindow(token, x, y); | 
|  | 5803 | }; | 
|  | 5804 | postCommandLocked(std::move(command)); | 
| Robert Carr | f759f16 | 2018-11-13 12:57:11 -0800 | [diff] [blame] | 5805 | } | 
|  | 5806 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5807 | void InputDispatcher::sendUntrustedTouchCommandLocked(const std::string& obscuringPackage) { | 
|  | 5808 | auto command = [this, obscuringPackage]() REQUIRES(mLock) { | 
|  | 5809 | scoped_unlock unlock(mLock); | 
|  | 5810 | mPolicy->notifyUntrustedTouch(obscuringPackage); | 
|  | 5811 | }; | 
|  | 5812 | postCommandLocked(std::move(command)); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 5813 | } | 
|  | 5814 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5815 | void InputDispatcher::onAnrLocked(const sp<Connection>& connection) { | 
|  | 5816 | if (connection == nullptr) { | 
|  | 5817 | LOG_ALWAYS_FATAL("Caller must check for nullness"); | 
|  | 5818 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5819 | // Since we are allowing the policy to extend the timeout, maybe the waitQueue | 
|  | 5820 | // is already healthy again. Don't raise ANR in this situation | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5821 | if (connection->waitQueue.empty()) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5822 | ALOGI("Not raising ANR because the connection %s has recovered", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5823 | connection->inputChannel->getName().c_str()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5824 | return; | 
|  | 5825 | } | 
|  | 5826 | /** | 
|  | 5827 | * The "oldestEntry" is the entry that was first sent to the application. That entry, however, | 
|  | 5828 | * may not be the one that caused the timeout to occur. One possibility is that window timeout | 
|  | 5829 | * has changed. This could cause newer entries to time out before the already dispatched | 
|  | 5830 | * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app | 
|  | 5831 | * processes the events linearly. So providing information about the oldest entry seems to be | 
|  | 5832 | * most useful. | 
|  | 5833 | */ | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5834 | DispatchEntry* oldestEntry = *connection->waitQueue.begin(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5835 | const nsecs_t currentWait = now() - oldestEntry->deliveryTime; | 
|  | 5836 | std::string reason = | 
|  | 5837 | android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s", | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5838 | connection->inputChannel->getName().c_str(), | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5839 | ns2ms(currentWait), | 
|  | 5840 | oldestEntry->eventEntry->getDescription().c_str()); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5841 | sp<IBinder> connectionToken = connection->inputChannel->getConnectionToken(); | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 5842 | updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5843 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5844 | processConnectionUnresponsiveLocked(*connection, std::move(reason)); | 
|  | 5845 |  | 
|  | 5846 | // Stop waking up for events on this connection, it is already unresponsive | 
|  | 5847 | cancelEventsForAnrLocked(connection); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5848 | } | 
|  | 5849 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5850 | void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) { | 
|  | 5851 | std::string reason = | 
|  | 5852 | StringPrintf("%s does not have a focused window", application->getName().c_str()); | 
|  | 5853 | updateLastAnrStateLocked(*application, reason); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5854 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5855 | auto command = [this, application = std::move(application)]() REQUIRES(mLock) { | 
|  | 5856 | scoped_unlock unlock(mLock); | 
|  | 5857 | mPolicy->notifyNoFocusedWindowAnr(application); | 
|  | 5858 | }; | 
|  | 5859 | postCommandLocked(std::move(command)); | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 5860 | } | 
|  | 5861 |  | 
| chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 5862 | void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5863 | const std::string& reason) { | 
|  | 5864 | const std::string windowLabel = getApplicationWindowLabel(nullptr, window); | 
|  | 5865 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5866 | } | 
|  | 5867 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5868 | void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application, | 
|  | 5869 | const std::string& reason) { | 
|  | 5870 | const std::string windowLabel = getApplicationWindowLabel(&application, nullptr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5871 | updateLastAnrStateLocked(windowLabel, reason); | 
|  | 5872 | } | 
|  | 5873 |  | 
|  | 5874 | void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel, | 
|  | 5875 | const std::string& reason) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5876 | // 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] | 5877 | time_t t = time(nullptr); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5878 | struct tm tm; | 
|  | 5879 | localtime_r(&t, &tm); | 
|  | 5880 | char timestr[64]; | 
|  | 5881 | strftime(timestr, sizeof(timestr), "%F %T", &tm); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5882 | mLastAnrState.clear(); | 
|  | 5883 | mLastAnrState += INDENT "ANR:\n"; | 
|  | 5884 | mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5885 | mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str()); | 
|  | 5886 | mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str()); | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 5887 | dumpDispatchStateLocked(mLastAnrState); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5888 | } | 
|  | 5889 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5890 | void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, | 
|  | 5891 | KeyEntry& entry) { | 
|  | 5892 | const KeyEvent event = createKeyEvent(entry); | 
|  | 5893 | nsecs_t delay = 0; | 
|  | 5894 | { // release lock | 
|  | 5895 | scoped_unlock unlock(mLock); | 
|  | 5896 | android::base::Timer t; | 
|  | 5897 | delay = mPolicy->interceptKeyBeforeDispatching(focusedWindowToken, &event, | 
|  | 5898 | entry.policyFlags); | 
|  | 5899 | if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) { | 
|  | 5900 | ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms", | 
|  | 5901 | std::to_string(t.duration().count()).c_str()); | 
|  | 5902 | } | 
|  | 5903 | } // acquire lock | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5904 |  | 
|  | 5905 | if (delay < 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5906 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5907 | } else if (delay == 0) { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5908 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5909 | } else { | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5910 | entry.interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; | 
|  | 5911 | entry.interceptKeyWakeupTime = now() + delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5912 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5913 | } | 
|  | 5914 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5915 | void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token, | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5916 | std::optional<int32_t> pid, | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5917 | std::string reason) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5918 | auto command = [this, token, pid, reason = std::move(reason)]() REQUIRES(mLock) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5919 | scoped_unlock unlock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5920 | mPolicy->notifyWindowUnresponsive(token, pid, reason); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5921 | }; | 
|  | 5922 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5923 | } | 
|  | 5924 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5925 | void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token, | 
|  | 5926 | std::optional<int32_t> pid) { | 
|  | 5927 | auto command = [this, token, pid]() REQUIRES(mLock) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5928 | scoped_unlock unlock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5929 | mPolicy->notifyWindowResponsive(token, pid); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5930 | }; | 
|  | 5931 | postCommandLocked(std::move(command)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5932 | } | 
|  | 5933 |  | 
|  | 5934 | /** | 
|  | 5935 | * Tell the policy that a connection has become unresponsive so that it can start ANR. | 
|  | 5936 | * Check whether the connection of interest is a monitor or a window, and add the corresponding | 
|  | 5937 | * command entry to the command queue. | 
|  | 5938 | */ | 
|  | 5939 | void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection, | 
|  | 5940 | std::string reason) { | 
|  | 5941 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5942 | std::optional<int32_t> pid; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5943 | if (connection.monitor) { | 
|  | 5944 | ALOGW("Monitor %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5945 | reason.c_str()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5946 | pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5947 | } else { | 
|  | 5948 | // The connection is a window | 
|  | 5949 | ALOGW("Window %s is unresponsive: %s", connection.inputChannel->getName().c_str(), | 
|  | 5950 | reason.c_str()); | 
|  | 5951 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); | 
|  | 5952 | if (handle != nullptr) { | 
|  | 5953 | pid = handle->getInfo()->ownerPid; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5954 | } | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5955 | } | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5956 | sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason)); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5957 | } | 
|  | 5958 |  | 
|  | 5959 | /** | 
|  | 5960 | * Tell the policy that a connection has become responsive so that it can stop ANR. | 
|  | 5961 | */ | 
|  | 5962 | void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) { | 
|  | 5963 | const sp<IBinder>& connectionToken = connection.inputChannel->getConnectionToken(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5964 | std::optional<int32_t> pid; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5965 | if (connection.monitor) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5966 | pid = findMonitorPidByTokenLocked(connectionToken); | 
|  | 5967 | } else { | 
|  | 5968 | // The connection is a window | 
|  | 5969 | const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken); | 
|  | 5970 | if (handle != nullptr) { | 
|  | 5971 | pid = handle->getInfo()->ownerPid; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5972 | } | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5973 | } | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5974 | sendWindowResponsiveCommandLocked(connectionToken, pid); | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 5975 | } | 
|  | 5976 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 5977 | bool InputDispatcher::afterKeyEventLockedInterruptable(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 5978 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5979 | KeyEntry& keyEntry, bool handled) { | 
|  | 5980 | if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) { | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5981 | if (!handled) { | 
|  | 5982 | // Report the key as unhandled, since the fallback was not handled. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5983 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 5984 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5985 | return false; | 
|  | 5986 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 5987 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5988 | // Get the fallback key state. | 
|  | 5989 | // Clear it out after dispatching the UP. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5990 | int32_t originalKeyCode = keyEntry.keyCode; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5991 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 5992 | if (keyEntry.action == AKEY_EVENT_ACTION_UP) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 5993 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 5994 | } | 
|  | 5995 |  | 
|  | 5996 | if (handled || !dispatchEntry->hasForegroundTarget()) { | 
|  | 5997 | // If the application handles the original key for which we previously | 
|  | 5998 | // generated a fallback or if the window is not a foreground window, | 
|  | 5999 | // then cancel the associated fallback key, if any. | 
|  | 6000 | if (fallbackKeyCode != -1) { | 
|  | 6001 | // Dispatch the unhandled key to the policy with the cancel flag. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6002 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6003 | ALOGD("Unhandled key event: Asking policy to cancel fallback action.  " | 
|  | 6004 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 6005 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, | 
|  | 6006 | keyEntry.policyFlags); | 
|  | 6007 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6008 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6009 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6010 |  | 
|  | 6011 | mLock.unlock(); | 
|  | 6012 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6013 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), &event, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6014 | keyEntry.policyFlags, &event); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6015 |  | 
|  | 6016 | mLock.lock(); | 
|  | 6017 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6018 | // Cancel the fallback key. | 
|  | 6019 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6020 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6021 | "application handled the original non-fallback key " | 
|  | 6022 | "or is no longer a foreground target, " | 
|  | 6023 | "canceling previously dispatched fallback key"); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6024 | options.keyCode = fallbackKeyCode; | 
|  | 6025 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6026 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6027 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 6028 | } | 
|  | 6029 | } else { | 
|  | 6030 | // If the application did not handle a non-fallback key, first check | 
|  | 6031 | // that we are in a good state to perform unhandled key event processing | 
|  | 6032 | // Then ask the policy what to do with it. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6033 | bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6034 | if (fallbackKeyCode == -1 && !initialDown) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6035 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6036 | ALOGD("Unhandled key event: Skipping unhandled key event processing " | 
|  | 6037 | "since this is not an initial down.  " | 
|  | 6038 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 6039 | originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
|  | 6040 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6041 | return false; | 
|  | 6042 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6043 |  | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6044 | // Dispatch the unhandled key to the policy. | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6045 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6046 | ALOGD("Unhandled key event: Asking policy to perform fallback action.  " | 
|  | 6047 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", | 
|  | 6048 | keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags); | 
|  | 6049 | } | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6050 | KeyEvent event = createKeyEvent(keyEntry); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6051 |  | 
|  | 6052 | mLock.unlock(); | 
|  | 6053 |  | 
| Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 6054 | bool fallback = | 
|  | 6055 | mPolicy->dispatchUnhandledKey(connection->inputChannel->getConnectionToken(), | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6056 | &event, keyEntry.policyFlags, &event); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6057 |  | 
|  | 6058 | mLock.lock(); | 
|  | 6059 |  | 
| Siarhei Vishniakou | f12f2f7 | 2021-11-17 17:49:45 -0800 | [diff] [blame] | 6060 | if (connection->status != Connection::Status::NORMAL) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6061 | connection->inputState.removeFallbackKey(originalKeyCode); | 
|  | 6062 | return false; | 
|  | 6063 | } | 
|  | 6064 |  | 
|  | 6065 | // Latch the fallback keycode for this key on an initial down. | 
|  | 6066 | // The fallback keycode cannot change at any other point in the lifecycle. | 
|  | 6067 | if (initialDown) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6068 | if (fallback) { | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6069 | fallbackKeyCode = event.getKeyCode(); | 
|  | 6070 | } else { | 
|  | 6071 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
|  | 6072 | } | 
|  | 6073 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
|  | 6074 | } | 
|  | 6075 |  | 
|  | 6076 | ALOG_ASSERT(fallbackKeyCode != -1); | 
|  | 6077 |  | 
|  | 6078 | // Cancel the fallback key if the policy decides not to send it anymore. | 
|  | 6079 | // We will continue to dispatch the key to the policy but we will no | 
|  | 6080 | // longer dispatch a fallback key to the application. | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6081 | if (fallbackKeyCode != AKEYCODE_UNKNOWN && | 
|  | 6082 | (!fallback || fallbackKeyCode != event.getKeyCode())) { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6083 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6084 | if (fallback) { | 
|  | 6085 | ALOGD("Unhandled key event: Policy requested to send key %d" | 
|  | 6086 | "as a fallback for %d, but on the DOWN it had requested " | 
|  | 6087 | "to send %d instead.  Fallback canceled.", | 
|  | 6088 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); | 
|  | 6089 | } else { | 
|  | 6090 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " | 
|  | 6091 | "but on the DOWN it had requested to send %d.  " | 
|  | 6092 | "Fallback canceled.", | 
|  | 6093 | originalKeyCode, fallbackKeyCode); | 
|  | 6094 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6095 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6096 |  | 
|  | 6097 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, | 
|  | 6098 | "canceling fallback, policy no longer desires it"); | 
|  | 6099 | options.keyCode = fallbackKeyCode; | 
|  | 6100 | synthesizeCancelationEventsForConnectionLocked(connection, options); | 
|  | 6101 |  | 
|  | 6102 | fallback = false; | 
|  | 6103 | fallbackKeyCode = AKEYCODE_UNKNOWN; | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6104 | if (keyEntry.action != AKEY_EVENT_ACTION_UP) { | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6105 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6106 | } | 
|  | 6107 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6108 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6109 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6110 | { | 
|  | 6111 | std::string msg; | 
|  | 6112 | const KeyedVector<int32_t, int32_t>& fallbackKeys = | 
|  | 6113 | connection->inputState.getFallbackKeys(); | 
|  | 6114 | for (size_t i = 0; i < fallbackKeys.size(); i++) { | 
|  | 6115 | msg += StringPrintf(", %d->%d", fallbackKeys.keyAt(i), fallbackKeys.valueAt(i)); | 
|  | 6116 | } | 
|  | 6117 | ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.", | 
|  | 6118 | fallbackKeys.size(), msg.c_str()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6119 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6120 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6121 |  | 
|  | 6122 | if (fallback) { | 
|  | 6123 | // Restart the dispatch cycle using the fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6124 | keyEntry.eventTime = event.getEventTime(); | 
|  | 6125 | keyEntry.deviceId = event.getDeviceId(); | 
|  | 6126 | keyEntry.source = event.getSource(); | 
|  | 6127 | keyEntry.displayId = event.getDisplayId(); | 
|  | 6128 | keyEntry.flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; | 
|  | 6129 | keyEntry.keyCode = fallbackKeyCode; | 
|  | 6130 | keyEntry.scanCode = event.getScanCode(); | 
|  | 6131 | keyEntry.metaState = event.getMetaState(); | 
|  | 6132 | keyEntry.repeatCount = event.getRepeatCount(); | 
|  | 6133 | keyEntry.downTime = event.getDownTime(); | 
|  | 6134 | keyEntry.syntheticRepeat = false; | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6135 |  | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6136 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6137 | ALOGD("Unhandled key event: Dispatching fallback key.  " | 
|  | 6138 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", | 
|  | 6139 | originalKeyCode, fallbackKeyCode, keyEntry.metaState); | 
|  | 6140 | } | 
| Prabir Pradhan | f557dcf | 2018-12-18 16:38:14 -0800 | [diff] [blame] | 6141 | return true; // restart the event | 
|  | 6142 | } else { | 
| Prabir Pradhan | 61a5d24 | 2021-07-26 16:41:09 +0000 | [diff] [blame] | 6143 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { | 
|  | 6144 | ALOGD("Unhandled key event: No fallback key."); | 
|  | 6145 | } | 
| Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 6146 |  | 
|  | 6147 | // Report the key as unhandled, since there is no fallback key. | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6148 | mReporter->reportUnhandledKey(keyEntry.id); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6149 | } | 
|  | 6150 | } | 
|  | 6151 | return false; | 
|  | 6152 | } | 
|  | 6153 |  | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6154 | bool InputDispatcher::afterMotionEventLockedInterruptable(const sp<Connection>& connection, | 
| Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 6155 | DispatchEntry* dispatchEntry, | 
| Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 6156 | MotionEntry& motionEntry, bool handled) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6157 | return false; | 
|  | 6158 | } | 
|  | 6159 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6160 | void InputDispatcher::traceInboundQueueLengthLocked() { | 
|  | 6161 | if (ATRACE_ENABLED()) { | 
| Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 6162 | ATRACE_INT("iq", mInboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6163 | } | 
|  | 6164 | } | 
|  | 6165 |  | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6166 | void InputDispatcher::traceOutboundQueueLength(const Connection& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6167 | if (ATRACE_ENABLED()) { | 
|  | 6168 | char counterName[40]; | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6169 | snprintf(counterName, sizeof(counterName), "oq:%s", connection.getWindowName().c_str()); | 
|  | 6170 | ATRACE_INT(counterName, connection.outboundQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6171 | } | 
|  | 6172 | } | 
|  | 6173 |  | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6174 | void InputDispatcher::traceWaitQueueLength(const Connection& connection) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6175 | if (ATRACE_ENABLED()) { | 
|  | 6176 | char counterName[40]; | 
| Siarhei Vishniakou | 060a727 | 2021-02-03 19:40:10 +0000 | [diff] [blame] | 6177 | snprintf(counterName, sizeof(counterName), "wq:%s", connection.getWindowName().c_str()); | 
|  | 6178 | ATRACE_INT(counterName, connection.waitQueue.size()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6179 | } | 
|  | 6180 | } | 
|  | 6181 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6182 | void InputDispatcher::dump(std::string& dump) { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6183 | std::scoped_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6184 |  | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6185 | dump += "Input Dispatcher State:\n"; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6186 | dumpDispatchStateLocked(dump); | 
|  | 6187 |  | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6188 | if (!mLastAnrState.empty()) { | 
| Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 6189 | dump += "\nInput Dispatcher State at time of last ANR:\n"; | 
| Siarhei Vishniakou | b1a1627 | 2020-05-06 16:09:19 -0700 | [diff] [blame] | 6190 | dump += mLastAnrState; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6191 | } | 
|  | 6192 | } | 
|  | 6193 |  | 
|  | 6194 | void InputDispatcher::monitor() { | 
|  | 6195 | // 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] | 6196 | std::unique_lock _l(mLock); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6197 | mLooper->wake(); | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 6198 | mDispatcherIsAlive.wait(_l); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 6199 | } | 
|  | 6200 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6201 | /** | 
|  | 6202 | * Wake up the dispatcher and wait until it processes all events and commands. | 
|  | 6203 | * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so | 
|  | 6204 | * this method can be safely called from any thread, as long as you've ensured that | 
|  | 6205 | * the work you are interested in completing has already been queued. | 
|  | 6206 | */ | 
|  | 6207 | bool InputDispatcher::waitForIdle() { | 
|  | 6208 | /** | 
|  | 6209 | * Timeout should represent the longest possible time that a device might spend processing | 
|  | 6210 | * events and commands. | 
|  | 6211 | */ | 
|  | 6212 | constexpr std::chrono::duration TIMEOUT = 100ms; | 
|  | 6213 | std::unique_lock lock(mLock); | 
|  | 6214 | mLooper->wake(); | 
|  | 6215 | std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT); | 
|  | 6216 | return result == std::cv_status::no_timeout; | 
|  | 6217 | } | 
|  | 6218 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 6219 | /** | 
|  | 6220 | * Sets focus to the window identified by the token. This must be called | 
|  | 6221 | * after updating any input window handles. | 
|  | 6222 | * | 
|  | 6223 | * Params: | 
|  | 6224 | *  request.token - input channel token used to identify the window that should gain focus. | 
|  | 6225 | *  request.focusedToken - the token that the caller expects currently to be focused. If the | 
|  | 6226 | *  specified token does not match the currently focused window, this request will be dropped. | 
|  | 6227 | *  If the specified focused token matches the currently focused window, the call will succeed. | 
|  | 6228 | *  Set this to "null" if this call should succeed no matter what the currently focused token is. | 
|  | 6229 | *  request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm) | 
|  | 6230 | *  when requesting the focus change. This determines which request gets | 
|  | 6231 | *  precedence if there is a focus change request from another source such as pointer down. | 
|  | 6232 | */ | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6233 | void InputDispatcher::setFocusedWindow(const FocusRequest& request) { | 
|  | 6234 | { // acquire lock | 
|  | 6235 | std::scoped_lock _l(mLock); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6236 | std::optional<FocusResolver::FocusChanges> changes = | 
|  | 6237 | mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId)); | 
|  | 6238 | if (changes) { | 
|  | 6239 | onFocusChangedLocked(*changes); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6240 | } | 
|  | 6241 | } // release lock | 
|  | 6242 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6243 | mLooper->wake(); | 
|  | 6244 | } | 
|  | 6245 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6246 | void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes) { | 
|  | 6247 | if (changes.oldFocus) { | 
|  | 6248 | std::shared_ptr<InputChannel> focusedInputChannel = getInputChannelLocked(changes.oldFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6249 | if (focusedInputChannel) { | 
|  | 6250 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, | 
|  | 6251 | "focus left window"); | 
|  | 6252 | synthesizeCancelationEventsForInputChannelLocked(focusedInputChannel, options); | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6253 | enqueueFocusEventLocked(changes.oldFocus, false /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6254 | } | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6255 | } | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6256 | if (changes.newFocus) { | 
|  | 6257 | enqueueFocusEventLocked(changes.newFocus, true /*hasFocus*/, changes.reason); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6258 | } | 
|  | 6259 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6260 | // If a window has pointer capture, then it must have focus. We need to ensure that this | 
|  | 6261 | // contract is upheld when pointer capture is being disabled due to a loss of window focus. | 
|  | 6262 | // If the window loses focus before it loses pointer capture, then the window can be in a state | 
|  | 6263 | // where it has pointer capture but not focus, violating the contract. Therefore we must | 
|  | 6264 | // dispatch the pointer capture event before the focus event. Since focus events are added to | 
|  | 6265 | // the front of the queue (above), we add the pointer capture event to the front of the queue | 
|  | 6266 | // after the focus events are added. This ensures the pointer capture event ends up at the | 
|  | 6267 | // front. | 
|  | 6268 | disablePointerCaptureForcedLocked(); | 
|  | 6269 |  | 
| Vishnu Nair | c519ff7 | 2021-01-21 08:23:08 -0800 | [diff] [blame] | 6270 | if (mFocusedDisplayId == changes.displayId) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6271 | sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus); | 
| Vishnu Nair | 7d3d00d | 2020-08-03 11:20:42 -0700 | [diff] [blame] | 6272 | } | 
|  | 6273 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6274 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6275 | void InputDispatcher::disablePointerCaptureForcedLocked() { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6276 | if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6277 | return; | 
|  | 6278 | } | 
|  | 6279 |  | 
|  | 6280 | ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus."); | 
|  | 6281 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6282 | if (mCurrentPointerCaptureRequest.enable) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6283 | setPointerCaptureLocked(false); | 
|  | 6284 | } | 
|  | 6285 |  | 
|  | 6286 | if (!mWindowTokenWithPointerCapture) { | 
|  | 6287 | // No need to send capture changes because no window has capture. | 
|  | 6288 | return; | 
|  | 6289 | } | 
|  | 6290 |  | 
|  | 6291 | if (mPendingEvent != nullptr) { | 
|  | 6292 | // Move the pending event to the front of the queue. This will give the chance | 
|  | 6293 | // for the pending event to be dropped if it is a captured event. | 
|  | 6294 | mInboundQueue.push_front(mPendingEvent); | 
|  | 6295 | mPendingEvent = nullptr; | 
|  | 6296 | } | 
|  | 6297 |  | 
|  | 6298 | auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(), | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6299 | mCurrentPointerCaptureRequest); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6300 | mInboundQueue.push_front(std::move(entry)); | 
|  | 6301 | } | 
|  | 6302 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6303 | void InputDispatcher::setPointerCaptureLocked(bool enable) { | 
|  | 6304 | mCurrentPointerCaptureRequest.enable = enable; | 
|  | 6305 | mCurrentPointerCaptureRequest.seq++; | 
|  | 6306 | auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) { | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6307 | scoped_unlock unlock(mLock); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6308 | mPolicy->setPointerCapture(request); | 
| Prabir Pradhan | cef936d | 2021-07-21 16:17:52 +0000 | [diff] [blame] | 6309 | }; | 
|  | 6310 | postCommandLocked(std::move(command)); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6311 | } | 
|  | 6312 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6313 | void InputDispatcher::displayRemoved(int32_t displayId) { | 
|  | 6314 | { // acquire lock | 
|  | 6315 | std::scoped_lock _l(mLock); | 
|  | 6316 | // Set an empty list to remove all handles from the specific display. | 
|  | 6317 | setInputWindowsLocked(/* window handles */ {}, displayId); | 
|  | 6318 | setFocusedApplicationLocked(displayId, nullptr); | 
|  | 6319 | // Call focus resolver to clean up stale requests. This must be called after input windows | 
|  | 6320 | // have been removed for the removed display. | 
|  | 6321 | mFocusResolver.displayRemoved(displayId); | 
| Christine Franks | b768bb4 | 2021-11-29 12:11:31 -0800 | [diff] [blame] | 6322 | // Reset pointer capture eligibility, regardless of previous state. | 
|  | 6323 | std::erase(mIneligibleDisplaysForPointerCapture, displayId); | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6324 | } // release lock | 
|  | 6325 |  | 
|  | 6326 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6327 | mLooper->wake(); | 
|  | 6328 | } | 
|  | 6329 |  | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6330 | void InputDispatcher::onWindowInfosChanged(const std::vector<WindowInfo>& windowInfos, | 
|  | 6331 | const std::vector<DisplayInfo>& displayInfos) { | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6332 | // The listener sends the windows as a flattened array. Separate the windows by display for | 
|  | 6333 | // more convenient parsing. | 
|  | 6334 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6335 | for (const auto& info : windowInfos) { | 
|  | 6336 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); | 
|  | 6337 | handlesPerDisplay[info.displayId].push_back(new WindowInfoHandle(info)); | 
|  | 6338 | } | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6339 |  | 
|  | 6340 | { // acquire lock | 
|  | 6341 | std::scoped_lock _l(mLock); | 
| Prabir Pradhan | 73fe481 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 6342 |  | 
|  | 6343 | // Ensure that we have an entry created for all existing displays so that if a displayId has | 
|  | 6344 | // no windows, we can tell that the windows were removed from the display. | 
|  | 6345 | for (const auto& [displayId, _] : mWindowHandlesByDisplay) { | 
|  | 6346 | handlesPerDisplay[displayId]; | 
|  | 6347 | } | 
|  | 6348 |  | 
| Prabir Pradhan | 48f8cb9 | 2021-08-26 14:05:36 -0700 | [diff] [blame] | 6349 | mDisplayInfos.clear(); | 
|  | 6350 | for (const auto& displayInfo : displayInfos) { | 
|  | 6351 | mDisplayInfos.emplace(displayInfo.displayId, displayInfo); | 
|  | 6352 | } | 
|  | 6353 |  | 
|  | 6354 | for (const auto& [displayId, handles] : handlesPerDisplay) { | 
|  | 6355 | setInputWindowsLocked(handles, displayId); | 
|  | 6356 | } | 
|  | 6357 | } | 
|  | 6358 | // Wake up poll loop since it may need to make new input dispatching choices. | 
|  | 6359 | mLooper->wake(); | 
| chaviw | 15fab6f | 2021-06-07 14:15:52 -0500 | [diff] [blame] | 6360 | } | 
|  | 6361 |  | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6362 | bool InputDispatcher::shouldDropInput( | 
|  | 6363 | const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const { | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6364 | if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) || | 
|  | 6365 | (windowHandle->getInfo()->inputConfig.test( | 
|  | 6366 | WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) && | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6367 | isWindowObscuredLocked(windowHandle))) { | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6368 | ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on " | 
|  | 6369 | "display %" PRId32 ".", | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6370 | ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(), | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6371 | windowHandle->getInfo()->inputConfig.string().c_str(), | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 6372 | windowHandle->getInfo()->displayId); | 
|  | 6373 | return true; | 
|  | 6374 | } | 
|  | 6375 | return false; | 
|  | 6376 | } | 
|  | 6377 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6378 | void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged( | 
|  | 6379 | const std::vector<gui::WindowInfo>& windowInfos, | 
|  | 6380 | const std::vector<DisplayInfo>& displayInfos) { | 
|  | 6381 | mDispatcher.onWindowInfosChanged(windowInfos, displayInfos); | 
|  | 6382 | } | 
|  | 6383 |  | 
| Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6384 | void InputDispatcher::cancelCurrentTouch() { | 
|  | 6385 | { | 
|  | 6386 | std::scoped_lock _l(mLock); | 
|  | 6387 | ALOGD("Canceling all ongoing pointer gestures on all displays."); | 
|  | 6388 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, | 
|  | 6389 | "cancel current touch"); | 
|  | 6390 | synthesizeCancelationEventsForAllConnectionsLocked(options); | 
|  | 6391 |  | 
|  | 6392 | mTouchStatesByDisplay.clear(); | 
|  | 6393 | mLastHoverWindowHandle.clear(); | 
|  | 6394 | } | 
|  | 6395 | // Wake up poll loop since there might be work to do. | 
|  | 6396 | mLooper->wake(); | 
|  | 6397 | } | 
|  | 6398 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6399 | void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) { | 
|  | 6400 | std::scoped_lock _l(mLock); | 
|  | 6401 | mMonitorDispatchingTimeout = timeout; | 
|  | 6402 | } | 
|  | 6403 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 6404 | } // namespace android::inputdispatcher |