blob: d549cbc2d16da66205b2f6e617ea4e9ac9dff09c [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Recke0710582019-09-26 13:46:12 -070020#define LOG_NDEBUG 1
Michael Wrightd02c5b62014-02-10 15:10:22 -080021
Michael Wright2b3c3302018-03-02 17:19:13 +000022#include <android-base/chrono_utils.h>
Siarhei Vishniakoud010b012023-01-18 15:00:53 -080023#include <android-base/logging.h>
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080024#include <android-base/properties.h>
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080025#include <android-base/stringprintf.h>
Siarhei Vishniakou70622952020-07-30 11:17:23 -050026#include <android/os/IInputConstants.h>
Robert Carr4e670e52018-08-15 13:26:12 -070027#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000028#include <com_android_input_flags.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080029#include <ftl/enum.h>
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070030#include <log/log_event_list.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070031#if defined(__ANDROID__)
chaviw15fab6f2021-06-07 14:15:52 -050032#include <gui/SurfaceComposerClient.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070033#endif
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -080034#include <input/InputDevice.h>
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080035#include <input/PrintTools.h>
Prabir Pradhana37bad12023-08-18 15:55:32 +000036#include <input/TraceTools.h>
tyiu1573a672023-02-21 22:38:32 +000037#include <openssl/mem.h>
Prabir Pradhanadc59b42023-12-15 05:34:11 +000038#include <private/android_filesystem_config.h>
Michael Wright44753b12020-07-08 13:48:11 +010039#include <unistd.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070040#include <utils/Trace.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wright44753b12020-07-08 13:48:11 +010042#include <cerrno>
43#include <cinttypes>
44#include <climits>
45#include <cstddef>
46#include <ctime>
47#include <queue>
48#include <sstream>
49
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000050#include "../InputDeviceMetricsSource.h"
51
Michael Wright44753b12020-07-08 13:48:11 +010052#include "Connection.h"
Arthur Hung1a1007b2022-05-11 07:15:01 +000053#include "DebugConfig.h"
Chris Yef59a2f42020-10-16 12:55:26 -070054#include "InputDispatcher.h"
jioana0bdbea12024-08-10 19:26:04 +000055#include "InputEventTimeline.h"
Prabir Pradhandae52792023-12-15 07:36:40 +000056#include "trace/InputTracer.h"
57#include "trace/InputTracingPerfettoBackend.h"
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000058#include "trace/ThreadedBackend.h"
Michael Wright44753b12020-07-08 13:48:11 +010059
Michael Wrightd02c5b62014-02-10 15:10:22 -080060#define INDENT " "
61#define INDENT2 " "
62#define INDENT3 " "
63#define INDENT4 " "
64
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080065using namespace android::ftl::flag_operators;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -070066using android::base::Error;
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080067using android::base::HwTimeoutMultiplier;
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000068using android::base::Result;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080069using android::base::StringPrintf;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -070070using android::gui::DisplayInfo;
chaviw98318de2021-05-19 16:45:23 -050071using android::gui::FocusRequest;
72using android::gui::TouchOcclusionMode;
73using android::gui::WindowInfo;
74using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080075using android::os::InputEventInjectionResult;
76using android::os::InputEventInjectionSync;
Ameer Armalycff4fa52023-10-04 23:45:11 +000077namespace input_flags = com::android::input::flags;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080078
Garfield Tane84e6f92019-08-29 17:28:41 -070079namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Prabir Pradhancef936d2021-07-21 16:17:52 +000081namespace {
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000082
Prabir Pradhandae52792023-12-15 07:36:40 +000083// Input tracing is only available on debuggable builds (userdebug and eng) when the feature
84// flag is enabled. When the flag is changed, tracing will only be available after reboot.
85bool isInputTracingEnabled() {
86 static const std::string buildType = base::GetProperty("ro.build.type", "user");
87 static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
88 return input_flags::enable_input_event_tracing() && isUserdebugOrEng;
89}
90
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000091// Create the input tracing backend that writes to perfetto from a single thread.
Prabir Pradhanf4099452024-04-12 18:34:14 +000092std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled() {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000093 if (!isInputTracingEnabled()) {
94 return nullptr;
95 }
96 return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>(
Prabir Pradhanf4099452024-04-12 18:34:14 +000097 trace::impl::PerfettoBackend());
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000098}
99
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000100template <class Entry>
101void ensureEventTraced(const Entry& entry) {
102 if (!entry.traceTracker) {
103 LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry;
104 }
105}
106
Prabir Pradhan052fb0b2024-02-23 21:03:12 +0000107// Helper to get a trace tracker from a traced key or motion entry.
108const std::unique_ptr<trace::EventTrackerInterface>& getTraceTracker(const EventEntry& entry) {
109 switch (entry.type) {
110 case EventEntry::Type::MOTION: {
111 const auto& motion = static_cast<const MotionEntry&>(entry);
112 ensureEventTraced(motion);
113 return motion.traceTracker;
114 }
115 case EventEntry::Type::KEY: {
116 const auto& key = static_cast<const KeyEntry&>(entry);
117 ensureEventTraced(key);
118 return key.traceTracker;
119 }
120 default: {
121 const static std::unique_ptr<trace::EventTrackerInterface> kNullTracker;
122 return kNullTracker;
123 }
124 }
125}
126
Prabir Pradhancef936d2021-07-21 16:17:52 +0000127// Temporarily releases a held mutex for the lifetime of the instance.
128// Named to match std::scoped_lock
129class scoped_unlock {
130public:
131 explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); }
132 ~scoped_unlock() { mMutex.lock(); }
133
134private:
135 std::mutex& mMutex;
136};
137
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138// Default input dispatching timeout if there is no focused application or paused window
139// from which to determine an appropriate dispatching timeout.
Peter Collingbourneb04b9b82021-02-08 12:09:47 -0800140const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds(
141 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
142 HwTimeoutMultiplier());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800144// The default minimum time gap between two user activity poke events.
145const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms;
146
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800147const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149// Log a warning when an event takes longer than this to process, even if an ANR does not occur.
Michael Wright2b3c3302018-03-02 17:19:13 +0000150constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
151
152// Log a warning when an interception call takes longer than this to process.
153constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
155// Number of recent events to keep for debugging purposes.
Michael Wright2b3c3302018-03-02 17:19:13 +0000156constexpr size_t RECENT_QUEUE_MAX_SIZE = 10;
157
Antonio Kantekea47acb2021-12-23 12:41:25 -0800158// Event log tags. See EventLogTags.logtags for reference.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000159constexpr int LOGTAG_INPUT_INTERACTION = 62000;
160constexpr int LOGTAG_INPUT_FOCUS = 62001;
Arthur Hungb3307ee2021-10-14 10:57:37 +0000161constexpr int LOGTAG_INPUT_CANCEL = 62003;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000162
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000163const ui::Transform kIdentityTransform;
164
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000165inline nsecs_t now() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166 return systemTime(SYSTEM_TIME_MONOTONIC);
167}
168
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700169inline const std::string binderToString(const sp<IBinder>& binder) {
Bernardo Rufino49d99e42021-01-18 15:16:59 +0000170 if (binder == nullptr) {
171 return "<null>";
172 }
173 return StringPrintf("%p", binder.get());
174}
175
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000176static std::string uidString(const gui::Uid& uid) {
177 return uid.toString();
178}
179
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700180Result<void> checkKeyAction(int32_t action) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 switch (action) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700182 case AKEY_EVENT_ACTION_DOWN:
183 case AKEY_EVENT_ACTION_UP:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700184 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700185 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700186 return Error() << "Key event has invalid action code " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 }
188}
189
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700190Result<void> validateKeyEvent(int32_t action) {
191 return checkKeyAction(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192}
193
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700194Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800195 switch (MotionEvent::getActionMasked(action)) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700196 case AMOTION_EVENT_ACTION_DOWN:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700197 case AMOTION_EVENT_ACTION_UP: {
198 if (pointerCount != 1) {
199 return Error() << "invalid pointer count " << pointerCount;
200 }
201 return {};
202 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700203 case AMOTION_EVENT_ACTION_MOVE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700204 case AMOTION_EVENT_ACTION_HOVER_ENTER:
205 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700206 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
207 if (pointerCount < 1) {
208 return Error() << "invalid pointer count " << pointerCount;
209 }
210 return {};
211 }
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800212 case AMOTION_EVENT_ACTION_CANCEL:
213 case AMOTION_EVENT_ACTION_OUTSIDE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700214 case AMOTION_EVENT_ACTION_SCROLL:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700215 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700216 case AMOTION_EVENT_ACTION_POINTER_DOWN:
217 case AMOTION_EVENT_ACTION_POINTER_UP: {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800218 const int32_t index = MotionEvent::getActionIndex(action);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700219 if (index < 0) {
220 return Error() << "invalid index " << index << " for "
221 << MotionEvent::actionToString(action);
222 }
223 if (index >= pointerCount) {
224 return Error() << "invalid index " << index << " for pointerCount " << pointerCount;
225 }
226 if (pointerCount <= 1) {
227 return Error() << "invalid pointer count " << pointerCount << " for "
228 << MotionEvent::actionToString(action);
229 }
230 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700231 }
232 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700233 case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
234 if (actionButton == 0) {
235 return Error() << "action button should be nonzero for "
236 << MotionEvent::actionToString(action);
237 }
238 return {};
239 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700240 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700241 return Error() << "invalid action " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242 }
243}
244
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000245int64_t millis(std::chrono::nanoseconds t) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500246 return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
247}
248
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700249Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount,
250 const PointerProperties* pointerProperties) {
251 Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount);
252 if (!actionCheck.ok()) {
253 return actionCheck;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 }
255 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700256 return Error() << "Motion event has invalid pointer count " << pointerCount
257 << "; value must be between 1 and " << MAX_POINTERS << ".";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800259 std::bitset<MAX_POINTER_ID + 1> pointerIdBits;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 for (size_t i = 0; i < pointerCount; i++) {
261 int32_t id = pointerProperties[i].id;
262 if (id < 0 || id > MAX_POINTER_ID) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700263 return Error() << "Motion event has invalid pointer id " << id
264 << "; value must be between 0 and " << MAX_POINTER_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800266 if (pointerIdBits.test(id)) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700267 return Error() << "Motion event has duplicate pointer id " << id;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800269 pointerIdBits.set(id);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700271 return {};
272}
273
274Result<void> validateInputEvent(const InputEvent& event) {
275 switch (event.getType()) {
276 case InputEventType::KEY: {
277 const KeyEvent& key = static_cast<const KeyEvent&>(event);
278 const int32_t action = key.getAction();
279 return validateKeyEvent(action);
280 }
281 case InputEventType::MOTION: {
282 const MotionEvent& motion = static_cast<const MotionEvent&>(event);
283 const int32_t action = motion.getAction();
284 const size_t pointerCount = motion.getPointerCount();
285 const PointerProperties* pointerProperties = motion.getPointerProperties();
286 const int32_t actionButton = motion.getActionButton();
287 return validateMotionEvent(action, actionButton, pointerCount, pointerProperties);
288 }
289 default: {
290 return {};
291 }
292 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293}
294
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800295std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) {
296 std::bitset<MAX_POINTER_ID + 1> pointerIds;
297 for (const PointerProperties& pointer : pointers) {
298 pointerIds.set(pointer.id);
299 }
300 return pointerIds;
301}
302
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000303std::string dumpRegion(const Region& region) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 if (region.isEmpty()) {
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000305 return "<empty>";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 }
307
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000308 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 bool first = true;
310 Region::const_iterator cur = region.begin();
311 Region::const_iterator const tail = region.end();
312 while (cur != tail) {
313 if (first) {
314 first = false;
315 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800316 dump += "|";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800318 dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 cur++;
320 }
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000321 return dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322}
323
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000324std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue,
325 nsecs_t currentTime) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500326 constexpr size_t maxEntries = 50; // max events to print
327 constexpr size_t skipBegin = maxEntries / 2;
328 const size_t skipEnd = queue.size() - maxEntries / 2;
329 // skip from maxEntries / 2 ... size() - maxEntries/2
330 // only print from 0 .. skipBegin and then from skipEnd .. size()
331
332 std::string dump;
333 for (size_t i = 0; i < queue.size(); i++) {
334 const DispatchEntry& entry = *queue[i];
335 if (i >= skipBegin && i < skipEnd) {
336 dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin);
337 i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue'
338 continue;
339 }
340 dump.append(INDENT4);
341 dump += entry.eventEntry->getDescription();
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +0000342 dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq,
343 entry.targetFlags.string().c_str(),
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500344 ns2ms(currentTime - entry.eventEntry->eventTime));
345 if (entry.deliveryTime != 0) {
346 // This entry was delivered, so add information on how long we've been waiting
347 dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime));
348 }
349 dump.append("\n");
350 }
351 return dump;
352}
353
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700354/**
355 * Find the entry in std::unordered_map by key, and return it.
356 * If the entry is not found, return a default constructed entry.
357 *
358 * Useful when the entries are vectors, since an empty vector will be returned
359 * if the entry is not found.
360 * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned.
361 */
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700362template <typename K, typename V>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000363V getValueByKey(const std::unordered_map<K, V>& map, K key) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700364 auto it = map.find(key);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700365 return it != map.end() ? it->second : V{};
Tiger Huang721e26f2018-07-24 22:26:19 +0800366}
367
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000368bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) {
chaviwaf87b3e2019-10-01 16:59:28 -0700369 if (first == second) {
370 return true;
371 }
372
373 if (first == nullptr || second == nullptr) {
374 return false;
375 }
376
377 return first->getToken() == second->getToken();
378}
379
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000380bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) {
Bernardo Rufino1ff9d592021-01-18 16:58:57 +0000381 if (first == nullptr || second == nullptr) {
382 return false;
383 }
384 return first->applicationInfo.token != nullptr &&
385 first->applicationInfo.token == second->applicationInfo.token;
386}
387
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800388template <typename T>
389size_t firstMarkedBit(T set) {
390 // TODO: replace with std::countr_zero from <bit> when that's available
391 LOG_ALWAYS_FATAL_IF(set.none());
392 size_t i = 0;
393 while (!set.test(i)) {
394 i++;
395 }
396 return i;
397}
398
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000399std::unique_ptr<DispatchEntry> createDispatchEntry(const IdGenerator& idGenerator,
400 const InputTarget& inputTarget,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000401 std::shared_ptr<const EventEntry> eventEntry,
402 ftl::Flags<InputTarget::Flags> inputTargetFlags,
Prabir Pradhana67623c2024-02-21 06:57:36 +0000403 int64_t vsyncId,
404 trace::InputTracerInterface* tracer) {
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000405 const bool zeroCoords = inputTargetFlags.test(InputTarget::Flags::ZERO_COORDS);
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000406 const sp<WindowInfoHandle> win = inputTarget.windowHandle;
407 const std::optional<int32_t> windowId =
408 win ? std::make_optional(win->getInfo()->id) : std::nullopt;
409 // Assume the only targets that are not associated with a window are global monitors, and use
410 // the system UID for global monitors for tracing purposes.
411 const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000412
413 if (inputTarget.useDefaultPointerTransform() && !zeroCoords) {
chaviw1ff3d1e2020-07-01 15:53:47 -0700414 const ui::Transform& transform = inputTarget.getDefaultPointerTransform();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700415 return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700416 inputTarget.displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000417 inputTarget.globalScaleFactor, uid, vsyncId,
418 windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000419 }
420
421 ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION);
422 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
423
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000424 std::vector<PointerCoords> pointerCoords{motionEntry.getPointerCount()};
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000425
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000426 const ui::Transform* transform = &kIdentityTransform;
427 const ui::Transform* displayTransform = &kIdentityTransform;
428 if (zeroCoords) {
429 std::for_each(pointerCoords.begin(), pointerCoords.end(), [](auto& pc) { pc.clear(); });
430 } else {
431 // Use the first pointer information to normalize all other pointers. This could be any
432 // pointer as long as all other pointers are normalized to the same value and the final
433 // DispatchEntry uses the transform for the normalized pointer.
434 transform =
435 &inputTarget.getTransformForPointer(firstMarkedBit(inputTarget.getPointerIds()));
436 const ui::Transform inverseTransform = transform->inverse();
437 displayTransform = &inputTarget.displayTransform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000438
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000439 // Iterate through all pointers in the event to normalize against the first.
440 for (size_t i = 0; i < motionEntry.getPointerCount(); i++) {
441 PointerCoords& newCoords = pointerCoords[i];
442 const auto pointerId = motionEntry.pointerProperties[i].id;
443 const ui::Transform& currTransform = inputTarget.getTransformForPointer(pointerId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000444
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000445 newCoords.copyFrom(motionEntry.pointerCoords[i]);
446 // First, apply the current pointer's transform to update the coordinates into
447 // window space.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +0000448 MotionEvent::calculateTransformedCoordsInPlace(newCoords, motionEntry.source,
449 motionEntry.flags, currTransform);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000450 // Next, apply the inverse transform of the normalized coordinates so the
451 // current coordinates are transformed into the normalized coordinate space.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +0000452 MotionEvent::calculateTransformedCoordsInPlace(newCoords, motionEntry.source,
453 motionEntry.flags, inverseTransform);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000454 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000455 }
456
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700457 std::unique_ptr<MotionEntry> combinedMotionEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000458 std::make_unique<MotionEntry>(idGenerator.nextId(), motionEntry.injectionState,
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000459 motionEntry.eventTime, motionEntry.deviceId,
460 motionEntry.source, motionEntry.displayId,
461 motionEntry.policyFlags, motionEntry.action,
462 motionEntry.actionButton, motionEntry.flags,
463 motionEntry.metaState, motionEntry.buttonState,
464 motionEntry.classification, motionEntry.edgeFlags,
465 motionEntry.xPrecision, motionEntry.yPrecision,
466 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
467 motionEntry.downTime, motionEntry.pointerProperties,
468 pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000469 if (tracer) {
470 combinedMotionEntry->traceTracker =
471 tracer->traceDerivedEvent(*combinedMotionEntry, *motionEntry.traceTracker);
472 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000473
474 std::unique_ptr<DispatchEntry> dispatchEntry =
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700475 std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags,
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000476 *transform, *displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000477 inputTarget.globalScaleFactor, uid, vsyncId, windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000478 return dispatchEntry;
479}
480
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500481template <typename T>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000482bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) {
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500483 if (lhs == nullptr && rhs == nullptr) {
484 return true;
485 }
486 if (lhs == nullptr || rhs == nullptr) {
487 return false;
488 }
489 return *lhs == *rhs;
490}
491
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000492KeyEvent createKeyEvent(const KeyEntry& entry) {
Siarhei Vishniakou2e2ea992020-12-15 02:57:19 +0000493 KeyEvent event;
494 event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC,
495 entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState,
496 entry.repeatCount, entry.downTime, entry.eventTime);
497 return event;
498}
499
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000500bool shouldReportMetricsForConnection(const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000501 // Do not keep track of gesture monitors. They receive every event and would disproportionately
502 // affect the statistics.
503 if (connection.monitor) {
504 return false;
505 }
506 // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics
507 if (!connection.responsive) {
508 return false;
509 }
510 return true;
511}
512
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000513bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000514 const EventEntry& eventEntry = *dispatchEntry.eventEntry;
515 const int32_t& inputEventId = eventEntry.id;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000516 if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) {
517 return false;
518 }
519 // Only track latency for events that originated from hardware
520 if (eventEntry.isSynthesized()) {
521 return false;
522 }
523 const EventEntry::Type& inputEventEntryType = eventEntry.type;
524 if (inputEventEntryType == EventEntry::Type::KEY) {
525 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
526 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
527 return false;
528 }
529 } else if (inputEventEntryType == EventEntry::Type::MOTION) {
530 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
531 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
532 motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
533 return false;
534 }
535 } else {
536 // Not a key or a motion
537 return false;
538 }
539 if (!shouldReportMetricsForConnection(connection)) {
540 return false;
541 }
542 return true;
543}
544
Prabir Pradhancef936d2021-07-21 16:17:52 +0000545/**
546 * Connection is responsive if it has no events in the waitQueue that are older than the
547 * current time.
548 */
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000549bool isConnectionResponsive(const Connection& connection) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000550 const nsecs_t currentTime = now();
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000551 for (const auto& dispatchEntry : connection.waitQueue) {
552 if (dispatchEntry->timeoutTime < currentTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000553 return false;
554 }
555 }
556 return true;
557}
558
Antonio Kantekf16f2832021-09-28 04:39:20 +0000559// Returns true if the event type passed as argument represents a user activity.
560bool isUserActivityEvent(const EventEntry& eventEntry) {
561 switch (eventEntry.type) {
Josep del Riob3981622023-04-18 15:49:45 +0000562 case EventEntry::Type::DEVICE_RESET:
563 case EventEntry::Type::DRAG:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000564 case EventEntry::Type::FOCUS:
565 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000566 case EventEntry::Type::SENSOR:
Josep del Riob3981622023-04-18 15:49:45 +0000567 case EventEntry::Type::TOUCH_MODE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000568 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +0000569 case EventEntry::Type::KEY:
570 case EventEntry::Type::MOTION:
571 return true;
572 }
573}
574
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800575// Returns true if the given window can accept pointer events at the given display location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800576bool windowAcceptsTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
577 float y, bool isStylus, const ui::Transform& displayTransform) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800578 const auto inputConfig = windowInfo.inputConfig;
579 if (windowInfo.displayId != displayId ||
580 inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800581 return false;
582 }
Prabir Pradhand65552b2021-10-07 11:23:50 -0700583 const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800584 if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800585 return false;
586 }
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000587
588 // Window Manager works in the logical display coordinate space. When it specifies bounds for a
589 // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside
590 // the window. Points on the right and bottom edges should not be inside the window, so we need
591 // to be careful about performing a hit test when the display is rotated, since the "right" and
592 // "bottom" of the window will be different in the display (un-rotated) space compared to in the
593 // logical display in which WM determined the bounds. Perform the hit test in the logical
594 // display space to ensure these edges are considered correctly in all orientations.
595 const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion);
596 const auto p = displayTransform.transform(x, y);
597 if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800598 return false;
599 }
600 return true;
601}
602
Linnan Li5e5645e2024-03-05 14:43:05 +0000603// Returns true if the given window's frame can occlude pointer events at the given display
604// location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800605bool windowOccludesTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
606 float y, const ui::Transform& displayTransform) {
Linnan Li5e5645e2024-03-05 14:43:05 +0000607 if (windowInfo.displayId != displayId) {
608 return false;
609 }
610 const auto frame = displayTransform.transform(windowInfo.frame);
611 const auto p = floor(displayTransform.transform(x, y));
612 return p.x >= frame.left && p.x < frame.right && p.y >= frame.top && p.y < frame.bottom;
613}
614
Prabir Pradhand65552b2021-10-07 11:23:50 -0700615bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) {
616 return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) &&
Prabir Pradhane5626962022-10-27 20:30:53 +0000617 isStylusToolType(entry.pointerProperties[pointerIndex].toolType);
Prabir Pradhand65552b2021-10-07 11:23:50 -0700618}
619
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800620// Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000621// Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to
622// such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can
623// be sent to such a window, but it is not a foreground event and doesn't use
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800624// InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000625bool canReceiveForegroundTouches(const WindowInfo& info) {
626 // A non-touchable window can still receive touch events (e.g. in the case of
627 // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches.
628 return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy();
629}
630
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000631bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -0700632 if (windowHandle == nullptr) {
633 return false;
634 }
635 const WindowInfo* windowInfo = windowHandle->getInfo();
636 if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) {
637 return true;
638 }
639 return false;
640}
641
Prabir Pradhan5735a322022-04-11 17:23:34 +0000642// Checks targeted injection using the window's owner's uid.
643// Returns an empty string if an entry can be sent to the given window, or an error message if the
644// entry is a targeted injection whose uid target doesn't match the window owner.
645std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window,
646 const EventEntry& entry) {
647 if (entry.injectionState == nullptr || !entry.injectionState->targetUid) {
648 // The event was not injected, or the injected event does not target a window.
649 return {};
650 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000651 const auto uid = *entry.injectionState->targetUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +0000652 if (window == nullptr) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000653 return StringPrintf("No valid window target for injection into uid %s.",
654 uid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000655 }
656 if (entry.injectionState->targetUid != window->getInfo()->ownerUid) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000657 return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' "
658 "owned by uid %s.",
659 uid.toString().c_str(), window->getName().c_str(),
660 window->getInfo()->ownerUid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000661 }
662 return {};
663}
664
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000665std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700666 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
667 // Always dispatch mouse events to cursor position.
668 if (isFromMouse) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000669 return {entry.xCursorPosition, entry.yCursorPosition};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700670 }
671
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -0700672 const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action);
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000673 return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X),
674 entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700675}
676
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -0700677std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) {
678 if (eventEntry.type == EventEntry::Type::KEY) {
679 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
680 return keyEntry.downTime;
681 } else if (eventEntry.type == EventEntry::Type::MOTION) {
682 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
683 return motionEntry.downTime;
684 }
685 return std::nullopt;
686}
687
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000688/**
689 * Compare the old touch state to the new touch state, and generate the corresponding touched
690 * windows (== input targets).
691 * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window.
692 * If the pointer just entered the new window, produce HOVER_ENTER.
693 * For pointers remaining in the window, produce HOVER_MOVE.
694 */
695std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState,
696 const TouchState& newTouchState,
697 const MotionEntry& entry) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000698 const int32_t maskedAction = MotionEvent::getActionMasked(entry.action);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700699
700 if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
701 // ACTION_SCROLL events should not affect the hovering pointer dispatch
702 return {};
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000703 }
Linnan Li41859c42024-03-05 16:20:34 +0000704 std::vector<TouchedWindow> out;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000705
706 // We should consider all hovering pointers here. But for now, just use the first one
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800707 const PointerProperties& pointer = entry.pointerProperties[0];
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000708
709 std::set<sp<WindowInfoHandle>> oldWindows;
710 if (oldState != nullptr) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800711 oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000712 }
713
714 std::set<sp<WindowInfoHandle>> newWindows =
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800715 newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000716
717 // If the pointer is no longer in the new window set, send HOVER_EXIT.
718 for (const sp<WindowInfoHandle>& oldWindow : oldWindows) {
719 if (newWindows.find(oldWindow) == newWindows.end()) {
720 TouchedWindow touchedWindow;
721 touchedWindow.windowHandle = oldWindow;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000722 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000723 out.push_back(touchedWindow);
724 }
725 }
726
727 for (const sp<WindowInfoHandle>& newWindow : newWindows) {
728 TouchedWindow touchedWindow;
729 touchedWindow.windowHandle = newWindow;
730 if (oldWindows.find(newWindow) == oldWindows.end()) {
731 // Any windows that have this pointer now, and didn't have it before, should get
732 // HOVER_ENTER
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000733 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000734 } else {
735 // This pointer was already sent to the window. Use ACTION_HOVER_MOVE.
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700736 if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700737 android::base::LogSeverity severity = android::base::LogSeverity::FATAL;
Ameer Armalycff4fa52023-10-04 23:45:11 +0000738 if (!input_flags::a11y_crash_on_inconsistent_event_stream() &&
739 entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700740 // The Accessibility injected touch exploration event stream
741 // has known inconsistencies, so log ERROR instead of
742 // crashing the device with FATAL.
Daniel Norman7487dfa2023-08-02 16:39:45 -0700743 severity = android::base::LogSeverity::ERROR;
744 }
745 LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription();
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700746 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000747 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000748 }
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800749 touchedWindow.addHoveringPointer(entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000750 if (canReceiveForegroundTouches(*newWindow->getInfo())) {
751 touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND;
752 }
753 out.push_back(touchedWindow);
754 }
755 return out;
756}
757
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800758template <typename T>
759std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) {
760 left.insert(left.end(), right.begin(), right.end());
761 return left;
762}
763
Harry Cuttsb166c002023-05-09 13:06:05 +0000764// Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from
765// both.
766void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) {
767 std::erase_if(touchState.windows, [&](const TouchedWindow& window) {
768 if (!window.windowHandle->getInfo()->inputConfig.test(
769 WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
770 // In addition to TouchState, erase this window from the input targets! We don't have a
771 // good way to do this today except by adding a nested loop.
772 // TODO(b/282025641): simplify this code once InputTargets are being identified
773 // separately from TouchedWindows.
774 std::erase_if(targets, [&](const InputTarget& target) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800775 return target.connection->getToken() == window.windowHandle->getToken();
Harry Cuttsb166c002023-05-09 13:06:05 +0000776 });
777 return true;
778 }
779 return false;
780 });
781}
782
Siarhei Vishniakouce1fd472023-09-18 18:38:07 -0700783/**
784 * In general, touch should be always split between windows. Some exceptions:
785 * 1. Don't split touch if all of the below is true:
786 * (a) we have an active pointer down *and*
787 * (b) a new pointer is going down that's from the same device *and*
788 * (c) the window that's receiving the current pointer does not support split touch.
789 * 2. Don't split mouse events
790 */
791bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) {
792 if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) {
793 // We should never split mouse events
794 return false;
795 }
796 for (const TouchedWindow& touchedWindow : touchState.windows) {
797 if (touchedWindow.windowHandle->getInfo()->isSpy()) {
798 // Spy windows should not affect whether or not touch is split.
799 continue;
800 }
801 if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) {
802 continue;
803 }
804 if (touchedWindow.windowHandle->getInfo()->inputConfig.test(
805 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
806 // Wallpaper window should not affect whether or not touch is split
807 continue;
808 }
809
810 if (touchedWindow.hasTouchingPointers(entry.deviceId)) {
811 return false;
812 }
813 }
814 return true;
815}
816
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700817/**
818 * Return true if stylus is currently down anywhere on the specified display, and false otherwise.
819 */
Linnan Li13bf76a2024-05-05 19:18:02 +0800820bool isStylusActiveInDisplay(ui::LogicalDisplayId displayId,
821 const std::unordered_map<ui::LogicalDisplayId /*displayId*/,
822 TouchState>& touchStatesByDisplay) {
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700823 const auto it = touchStatesByDisplay.find(displayId);
824 if (it == touchStatesByDisplay.end()) {
825 return false;
826 }
827 const TouchState& state = it->second;
828 return state.hasActiveStylus();
829}
830
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800831Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) {
832 struct HashFunction {
833 size_t operator()(const WindowInfo& info) const { return info.id; }
834 };
835
836 std::unordered_set<WindowInfo, HashFunction> windowSet;
837 for (const WindowInfo& info : update.windowInfos) {
838 const auto [_, inserted] = windowSet.insert(info);
839 if (!inserted) {
840 return Error() << "Duplicate entry for " << info;
841 }
842 }
843 return {};
844}
845
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800846int32_t getUserActivityEventType(const EventEntry& eventEntry) {
847 switch (eventEntry.type) {
848 case EventEntry::Type::KEY: {
849 return USER_ACTIVITY_EVENT_BUTTON;
850 }
851 case EventEntry::Type::MOTION: {
852 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
853 if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) {
854 return USER_ACTIVITY_EVENT_TOUCH;
855 }
856 return USER_ACTIVITY_EVENT_OTHER;
857 }
858 default: {
859 LOG_ALWAYS_FATAL("%s events are not user activity",
860 ftl::enum_string(eventEntry.type).c_str());
861 }
862 }
863}
864
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +0000865std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode(
866 CancelationOptions::Mode mode) {
867 switch (mode) {
868 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
869 return {true, true};
870 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
871 return {true, false};
872 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
873 return {false, true};
874 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
875 return {false, true};
876 }
877}
878
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000879class ScopedSyntheticEventTracer {
880public:
881 ScopedSyntheticEventTracer(std::unique_ptr<trace::InputTracerInterface>& tracer)
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000882 : mTracer(tracer), mProcessingTimestamp(now()) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000883 if (mTracer) {
884 mEventTracker = mTracer->createTrackerForSyntheticEvent();
885 }
886 }
887
888 ~ScopedSyntheticEventTracer() {
889 if (mTracer) {
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000890 mTracer->eventProcessingComplete(*mEventTracker, mProcessingTimestamp);
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000891 }
892 }
893
894 const std::unique_ptr<trace::EventTrackerInterface>& getTracker() const {
895 return mEventTracker;
896 }
897
898private:
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000899 const std::unique_ptr<trace::InputTracerInterface>& mTracer;
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000900 std::unique_ptr<trace::EventTrackerInterface> mEventTracker;
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000901 const nsecs_t mProcessingTimestamp;
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000902};
903
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -0700904/**
905 * This is needed to help use "InputEventInjectionResult" with base::Result.
906 */
907template <typename T>
908struct EnumErrorWrapper {
909 T mVal;
910 EnumErrorWrapper(T&& e) : mVal(std::forward<T>(e)) {}
911 operator const T&() const { return mVal; }
912 T value() const { return mVal; }
913 std::string print() const { return ftl::enum_string(mVal); }
914};
915
916Error<EnumErrorWrapper<InputEventInjectionResult>> injectionError(InputEventInjectionResult&& e) {
917 LOG_ALWAYS_FATAL_IF(e == InputEventInjectionResult::SUCCEEDED);
918 return Error<EnumErrorWrapper<InputEventInjectionResult>>(
919 std::forward<InputEventInjectionResult>(e));
920}
921
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000922} // namespace
923
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924// --- InputDispatcher ---
925
Prabir Pradhana41d2442023-04-20 21:30:40 +0000926InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
Prabir Pradhanf4099452024-04-12 18:34:14 +0000927 : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {}
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000928
929InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
930 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
Garfield Tan00f511d2019-06-12 16:55:40 -0700931 : mPolicy(policy),
932 mPendingEvent(nullptr),
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700933 mLastDropReason(DropReason::NOT_DROPPED),
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800934 mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800935 mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),
Garfield Tan00f511d2019-06-12 16:55:40 -0700936 mNextUnblockedEvent(nullptr),
Prabir Pradhan1376fcd2022-01-21 09:56:35 -0800937 mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),
Garfield Tan00f511d2019-06-12 16:55:40 -0700938 mDispatchEnabled(false),
939 mDispatchFrozen(false),
940 mInputFilterEnabled(false),
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100941 mMaximumObscuringOpacityForTouch(1.0f),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700942 mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
Prabir Pradhan99987712020-11-10 18:43:05 -0800943 mWindowTokenWithPointerCapture(nullptr),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700944 mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +0000945 mLatencyAggregator(),
Antonio Kantek15beb512022-06-13 22:35:41 +0000946 mLatencyTracker(&mLatencyAggregator) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700947 mLooper = sp<Looper>::make(false);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800948 mReporter = createInputReporter();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700950 mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700951#if defined(__ANDROID__)
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700952 SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700953#endif
Yi Kong9b14ac62018-07-17 13:48:38 -0700954 mKeyRepeatState.lastKeyEntry = nullptr;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000955
956 if (traceBackend) {
Prabir Pradhandae52792023-12-15 07:36:40 +0000957 mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000958 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800959
960 mLastUserActivityTimes.fill(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961}
962
963InputDispatcher::~InputDispatcher() {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000964 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Prabir Pradhancef936d2021-07-21 16:17:52 +0000966 resetKeyRepeatLocked();
967 releasePendingEventLocked();
968 drainInboundQueueLocked();
969 mCommandQueue.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +0000971 while (!mConnectionsByToken.empty()) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700972 std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800973 removeInputChannelLocked(connection->getToken(), /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975}
976
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700977status_t InputDispatcher::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700978 if (mThread) {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700979 return ALREADY_EXISTS;
980 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700981 mThread = std::make_unique<InputThread>(
982 "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
983 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700984}
985
986status_t InputDispatcher::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700987 if (mThread && mThread->isCallingThread()) {
988 ALOGE("InputDispatcher cannot be stopped from its own thread!");
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700989 return INVALID_OPERATION;
990 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700991 mThread.reset();
992 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700993}
994
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995void InputDispatcher::dispatchOnce() {
Colin Cross5b799302022-10-18 21:52:41 -0700996 nsecs_t nextWakeupTime = LLONG_MAX;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800998 std::scoped_lock _l(mLock);
999 mDispatcherIsAlive.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
1001 // Run a dispatch loop if there are no pending commands.
1002 // The dispatch loop might enqueue commands to run afterwards.
1003 if (!haveCommandsLocked()) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001004 dispatchOnceInnerLocked(/*byref*/ nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
1007 // Run all pending commands if there are any.
1008 // If any commands were run then force the next poll to wake up immediately.
Prabir Pradhancef936d2021-07-21 16:17:52 +00001009 if (runCommandsLockedInterruptable()) {
Colin Cross5b799302022-10-18 21:52:41 -07001010 nextWakeupTime = LLONG_MIN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 }
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001012
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001013 // If we are still waiting for ack on some events,
1014 // we might have to wake up earlier to check if an app is anr'ing.
1015 const nsecs_t nextAnrCheck = processAnrsLocked();
1016 nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);
1017
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001018 // We are about to enter an infinitely long sleep, because we have no commands or
1019 // pending or queued events
Colin Cross5b799302022-10-18 21:52:41 -07001020 if (nextWakeupTime == LLONG_MAX) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001021 mDispatcherEnteredIdle.notify_all();
1022 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 } // release lock
1024
1025 // Wait for callback or timeout or wake. (make sure we round up, not down)
1026 nsecs_t currentTime = now();
1027 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
1028 mLooper->pollOnce(timeoutMillis);
1029}
1030
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001031/**
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001032 * Raise ANR if there is no focused window.
1033 * Before the ANR is raised, do a final state check:
1034 * 1. The currently focused application must be the same one we are waiting for.
1035 * 2. Ensure we still don't have a focused window.
1036 */
1037void InputDispatcher::processNoFocusedWindowAnrLocked() {
1038 // Check if the application that we are waiting for is still focused.
1039 std::shared_ptr<InputApplicationHandle> focusedApplication =
1040 getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId);
1041 if (focusedApplication == nullptr ||
1042 focusedApplication->getApplicationToken() !=
1043 mAwaitedFocusedApplication->getApplicationToken()) {
1044 // Unexpected because we should have reset the ANR timer when focused application changed
1045 ALOGE("Waited for a focused window, but focused application has already changed to %s",
1046 focusedApplication->getName().c_str());
1047 return; // The focused application has changed.
1048 }
1049
chaviw98318de2021-05-19 16:45:23 -05001050 const sp<WindowInfoHandle>& focusedWindowHandle =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001051 getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId);
1052 if (focusedWindowHandle != nullptr) {
1053 return; // We now have a focused window. No need for ANR.
1054 }
1055 onAnrLocked(mAwaitedFocusedApplication);
1056}
1057
1058/**
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001059 * Check if any of the connections' wait queues have events that are too old.
1060 * If we waited for events to be ack'ed for more than the window timeout, raise an ANR.
1061 * Return the time at which we should wake up next.
1062 */
1063nsecs_t InputDispatcher::processAnrsLocked() {
1064 const nsecs_t currentTime = now();
Colin Cross5b799302022-10-18 21:52:41 -07001065 nsecs_t nextAnrCheck = LLONG_MAX;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001066 // Check if we are waiting for a focused window to appear. Raise ANR if waited too long
1067 if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) {
1068 if (currentTime >= *mNoFocusedWindowTimeoutTime) {
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001069 processNoFocusedWindowAnrLocked();
Chris Yea209fde2020-07-22 13:54:51 -07001070 mAwaitedFocusedApplication.reset();
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001071 mNoFocusedWindowTimeoutTime = std::nullopt;
Colin Cross5b799302022-10-18 21:52:41 -07001072 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001073 } else {
Siarhei Vishniakou38a6d272020-10-20 20:29:33 -05001074 // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075 nextAnrCheck = *mNoFocusedWindowTimeoutTime;
1076 }
1077 }
1078
1079 // Check if any connection ANRs are due
1080 nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout());
1081 if (currentTime < nextAnrCheck) { // most likely scenario
1082 return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck
1083 }
1084
1085 // If we reached here, we have an unresponsive connection.
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001086 std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001087 if (connection == nullptr) {
1088 ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout());
1089 return nextAnrCheck;
1090 }
1091 connection->responsive = false;
1092 // Stop waking up for this unresponsive connection
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001093 mAnrTracker.eraseToken(connection->getToken());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00001094 onAnrLocked(connection);
Colin Cross5b799302022-10-18 21:52:41 -07001095 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001096}
1097
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001098std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001099 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001100 if (connection->monitor) {
1101 return mMonitorDispatchingTimeout;
1102 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001103 const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001104 if (window != nullptr) {
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001105 return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001106 }
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001107 return DEFAULT_INPUT_DISPATCHING_TIMEOUT;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001108}
1109
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001110void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 nsecs_t currentTime = now();
1112
Jeff Browndc5992e2014-04-11 01:27:26 -07001113 // Reset the key repeat timer whenever normal dispatch is suspended while the
1114 // device is in a non-interactive state. This is to ensure that we abort a key
1115 // repeat if the device is just coming out of sleep.
1116 if (!mDispatchEnabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 resetKeyRepeatLocked();
1118 }
1119
1120 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
1121 if (mDispatchFrozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01001122 if (DEBUG_FOCUS) {
1123 ALOGD("Dispatch frozen. Waiting some more.");
1124 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 return;
1126 }
1127
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 // Ready to start a new event.
1129 // If we don't already have a pending event, go grab one.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001130 if (!mPendingEvent) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001131 if (mInboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 // Synthesize a key repeat if appropriate.
1133 if (mKeyRepeatState.lastKeyEntry) {
1134 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
1135 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
1136 } else {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001137 nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 }
1139 }
1140
1141 // Nothing to do if there is no pending event.
1142 if (!mPendingEvent) {
1143 return;
1144 }
1145 } else {
1146 // Inbound queue has at least one entry.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001147 mPendingEvent = mInboundQueue.front();
1148 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 traceInboundQueueLengthLocked();
1150 }
1151
1152 // Poke user activity for this event.
1153 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001154 pokeUserActivityLocked(*mPendingEvent);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 }
1157
1158 // Now we have an event to dispatch.
1159 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Yi Kong9b14ac62018-07-17 13:48:38 -07001160 ALOG_ASSERT(mPendingEvent != nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 bool done = false;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001162 DropReason dropReason = DropReason::NOT_DROPPED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001164 dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 } else if (!mDispatchEnabled) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001166 dropReason = DropReason::DISABLED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 }
1168
1169 if (mNextUnblockedEvent == mPendingEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001170 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 }
1172
1173 switch (mPendingEvent->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001174 case EventEntry::Type::DEVICE_RESET: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001175 const DeviceResetEntry& typedEntry =
1176 static_cast<const DeviceResetEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001177 done = dispatchDeviceResetLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001178 dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001179 break;
1180 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001182 case EventEntry::Type::FOCUS: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001183 std::shared_ptr<const FocusEntry> typedEntry =
1184 std::static_pointer_cast<const FocusEntry>(mPendingEvent);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001185 dispatchFocusLocked(currentTime, typedEntry);
1186 done = true;
1187 dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
1188 break;
1189 }
1190
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001191 case EventEntry::Type::TOUCH_MODE_CHANGED: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001192 const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001193 dispatchTouchModeChangeLocked(currentTime, typedEntry);
1194 done = true;
1195 dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped
1196 break;
1197 }
1198
Prabir Pradhan99987712020-11-10 18:43:05 -08001199 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
1200 const auto typedEntry =
Prabir Pradhan24047542023-11-02 17:14:59 +00001201 std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent);
Prabir Pradhan99987712020-11-10 18:43:05 -08001202 dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason);
1203 done = true;
1204 break;
1205 }
1206
arthurhungb89ccb02020-12-30 16:19:01 +08001207 case EventEntry::Type::DRAG: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001208 std::shared_ptr<const DragEntry> typedEntry =
1209 std::static_pointer_cast<const DragEntry>(mPendingEvent);
arthurhungb89ccb02020-12-30 16:19:01 +08001210 dispatchDragLocked(currentTime, typedEntry);
1211 done = true;
1212 break;
1213 }
1214
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001215 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001216 std::shared_ptr<const KeyEntry> keyEntry =
1217 std::static_pointer_cast<const KeyEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001218 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001219 dropReason = DropReason::STALE;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001220 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001221 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
1222 dropReason = DropReason::BLOCKED;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001223 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001224 done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001225 break;
1226 }
1227
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001228 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001229 std::shared_ptr<const MotionEntry> motionEntry =
1230 std::static_pointer_cast<const MotionEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001231 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07001232 // The event is stale. However, only drop stale events if there isn't an ongoing
1233 // gesture. That would allow us to complete the processing of the current stroke.
1234 const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId);
1235 if (touchStateIt != mTouchStatesByDisplay.end()) {
1236 const TouchState& touchState = touchStateIt->second;
1237 if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
1238 !touchState.hasHoveringPointers(motionEntry->deviceId)) {
1239 dropReason = DropReason::STALE;
1240 }
1241 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001242 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001243 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001244 if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) {
1245 // Only drop events that are focus-dispatched.
1246 dropReason = DropReason::BLOCKED;
1247 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001248 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001249 done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001250 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 }
Chris Yef59a2f42020-10-16 12:55:26 -07001252
1253 case EventEntry::Type::SENSOR: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001254 std::shared_ptr<const SensorEntry> sensorEntry =
1255 std::static_pointer_cast<const SensorEntry>(mPendingEvent);
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001256
Chris Yef59a2f42020-10-16 12:55:26 -07001257 // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use
1258 // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead.
1259 nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME);
1260 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) {
1261 dropReason = DropReason::STALE;
1262 }
1263 dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime);
1264 done = true;
1265 break;
1266 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 }
1268
1269 if (done) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001270 if (dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001271 dropInboundEventLocked(*mPendingEvent, dropReason);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 }
Michael Wright3a981722015-06-10 15:26:13 +01001273 mLastDropReason = dropReason;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
Prabir Pradhan052fb0b2024-02-23 21:03:12 +00001275 if (mTracer) {
1276 if (auto& traceTracker = getTraceTracker(*mPendingEvent); traceTracker != nullptr) {
Prabir Pradhan4fc32e02024-06-14 16:00:58 +00001277 mTracer->eventProcessingComplete(*traceTracker, currentTime);
Prabir Pradhan052fb0b2024-02-23 21:03:12 +00001278 }
1279 }
1280
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 releasePendingEventLocked();
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001282 nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 }
1284}
1285
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001286bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) {
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07001287 return mPolicy.isStaleEvent(currentTime, entry.eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001288}
1289
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001290/**
1291 * Return true if the events preceding this incoming motion event should be dropped
1292 * Return false otherwise (the default behaviour)
1293 */
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001294bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001295 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
Prabir Pradhanaa561d12021-09-24 06:57:33 -07001296 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001297
1298 // Optimize case where the current application is unresponsive and the user
1299 // decides to touch a window in a different application.
1300 // If the application takes too long to catch up then we drop all events preceding
1301 // the touch into the other window.
1302 if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001303 const ui::LogicalDisplayId displayId = motionEntry.displayId;
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001304 const auto [x, y] = resolveTouchedPosition(motionEntry);
Harry Cutts33476232023-01-30 19:57:29 +00001305 const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0);
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001306
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001307 sp<WindowInfoHandle> touchedWindowHandle =
1308 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001309 if (touchedWindowHandle != nullptr &&
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001310 touchedWindowHandle->getApplicationToken() !=
1311 mAwaitedFocusedApplication->getApplicationToken()) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001312 // User touched a different application than the one we are waiting on.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001313 ALOGI("Pruning input queue because user touched a different application while waiting "
1314 "for %s",
1315 mAwaitedFocusedApplication->getName().c_str());
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001316 return true;
1317 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001318
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001319 // Alternatively, maybe there's a spy window that could handle this event.
1320 const std::vector<sp<WindowInfoHandle>> touchedSpies =
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001321 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus, motionEntry.deviceId);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001322 for (const auto& windowHandle : touchedSpies) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001323 const std::shared_ptr<Connection> connection =
1324 getConnectionLocked(windowHandle->getToken());
Siarhei Vishniakou34ed4d42020-06-18 00:43:02 +00001325 if (connection != nullptr && connection->responsive) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001326 // This spy window could take more input. Drop all events preceding this
1327 // event, so that the spy window can get a chance to receive the stream.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001328 ALOGW("Pruning the input queue because %s is unresponsive, but we have a "
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001329 "responsive spy window that may handle the event.",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001330 mAwaitedFocusedApplication->getName().c_str());
1331 return true;
1332 }
1333 }
1334 }
1335
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001336 return false;
1337}
1338
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001339bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001340 bool needWake = mInboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001341 mInboundQueue.push_back(std::move(newEntry));
Prabir Pradhan24047542023-11-02 17:14:59 +00001342 const EventEntry& entry = *(mInboundQueue.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001343 traceInboundQueueLengthLocked();
1344
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001345 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001346 case EventEntry::Type::KEY: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001347 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1348 "Unexpected untrusted event.");
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001349
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001350 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001351 if (mTracer) {
1352 ensureEventTraced(keyEntry);
1353 }
Siarhei Vishniakou6520a582023-10-27 21:53:45 -07001354
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001355 // If a new up event comes in, and the pending event with same key code has been asked
1356 // to try again later because of the policy. We have to reset the intercept key wake up
1357 // time for it may have been handled in the policy and could be dropped.
1358 if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent &&
1359 mPendingEvent->type == EventEntry::Type::KEY) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001360 const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001361 if (pendingKey.keyCode == keyEntry.keyCode &&
1362 pendingKey.interceptKeyResult ==
Michael Wright5caf55a2022-11-24 22:31:42 +00001363 KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
1364 pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001365 pendingKey.interceptKeyWakeupTime = 0;
1366 needWake = true;
1367 }
1368 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001369 break;
1370 }
1371
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001372 case EventEntry::Type::MOTION: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001373 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1374 "Unexpected untrusted event.");
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001375 const auto& motionEntry = static_cast<const MotionEntry&>(entry);
1376 if (mTracer) {
1377 ensureEventTraced(motionEntry);
1378 }
1379 if (shouldPruneInboundQueueLocked(motionEntry)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001380 mNextUnblockedEvent = mInboundQueue.back();
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001381 needWake = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001382 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001383
1384 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
1385 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
1386 if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) {
1387 // Prevent waiting too long for unprocessed events: if we have a pending key event,
1388 // and some other events have not yet been processed, the dispatcher will wait for
1389 // these events to be processed before dispatching the key event. This is because
1390 // the unprocessed events may cause the focus to change (for example, by launching a
1391 // new window or tapping a different window). To prevent waiting too long, we force
1392 // the key to be sent to the currently focused window when a new tap comes in.
1393 ALOGD("Received a new pointer down event, stop waiting for events to process and "
1394 "just send the pending key event to the currently focused window.");
1395 mKeyIsWaitingForEventsTimeout = now();
Linnan Li563916a2024-02-23 16:08:33 +00001396 needWake = true;
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001397 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001398 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001400 case EventEntry::Type::FOCUS: {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001401 LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked");
1402 break;
1403 }
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001404 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08001405 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07001406 case EventEntry::Type::SENSOR:
arthurhungb89ccb02020-12-30 16:19:01 +08001407 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1408 case EventEntry::Type::DRAG: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001409 // nothing to do
1410 break;
1411 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 }
1413
1414 return needWake;
1415}
1416
Prabir Pradhan24047542023-11-02 17:14:59 +00001417void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) {
Chris Yef59a2f42020-10-16 12:55:26 -07001418 // Do not store sensor event in recent queue to avoid flooding the queue.
1419 if (entry->type != EventEntry::Type::SENSOR) {
1420 mRecentQueue.push_back(entry);
1421 }
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001422 if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001423 mRecentQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424 }
1425}
1426
Linnan Li13bf76a2024-05-05 19:18:02 +08001427sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(ui::LogicalDisplayId displayId,
1428 float x, float y, bool isStylus,
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001429 bool ignoreDragWindow) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001430 // Traverse windows from front to back to find touched window.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001431 const auto& windowHandles = getWindowHandlesLocked(displayId);
chaviw98318de2021-05-19 16:45:23 -05001432 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
arthurhung6d4bed92021-03-17 11:59:33 +08001433 if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) {
arthurhungb89ccb02020-12-30 16:19:01 +08001434 continue;
1435 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001436
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001437 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001438 if (!info.isSpy() &&
1439 windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001440 return windowHandle;
1441 }
1442 }
1443 return nullptr;
1444}
1445
1446std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08001447 ui::LogicalDisplayId displayId, const sp<WindowInfoHandle>& touchedWindow,
1448 int32_t pointerId) const {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001449 if (touchedWindow == nullptr) {
1450 return {};
1451 }
1452 // Traverse windows from front to back until we encounter the touched window.
1453 std::vector<InputTarget> outsideTargets;
1454 const auto& windowHandles = getWindowHandlesLocked(displayId);
1455 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1456 if (windowHandle == touchedWindow) {
1457 // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window
1458 // below the touched window will not get ACTION_OUTSIDE event.
1459 return outsideTargets;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001460 }
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001461
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001462 const WindowInfo& info = *windowHandle->getInfo();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08001463 if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) {
Siarhei Vishniakou70f3d8c2023-09-19 15:36:52 -07001464 std::bitset<MAX_POINTER_ID + 1> pointerIds;
1465 pointerIds.set(pointerId);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001466 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE,
1467 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001468 /*firstDownTimeInTarget=*/std::nullopt, outsideTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469 }
1470 }
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001471 return outsideTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001472}
1473
Prabir Pradhand65552b2021-10-07 11:23:50 -07001474std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked(
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001475 ui::LogicalDisplayId displayId, float x, float y, bool isStylus, DeviceId deviceId) const {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001476 // Traverse windows from front to back and gather the touched spy windows.
1477 std::vector<sp<WindowInfoHandle>> spyWindows;
1478 const auto& windowHandles = getWindowHandlesLocked(displayId);
1479 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1480 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001481 if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001482 // Generally, we would skip any pointer that's outside of the window. However, if the
1483 // spy prevents splitting, and already has some of the pointers from this device, then
1484 // it should get more pointers from the same device, even if they are outside of that
1485 // window
1486 if (info.supportsSplitTouch()) {
1487 continue;
1488 }
1489
1490 // We know that split touch is not supported. Skip this window only if it doesn't have
1491 // any touching pointers for this device already.
1492 if (!windowHasTouchingPointersLocked(windowHandle, deviceId)) {
1493 continue;
1494 }
1495 // If it already has pointers down for this device, then give it this pointer, too.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001496 }
1497 if (!info.isSpy()) {
1498 // The first touched non-spy window was found, so return the spy windows touched so far.
1499 return spyWindows;
1500 }
1501 spyWindows.push_back(windowHandle);
1502 }
1503 return spyWindows;
1504}
1505
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001506void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507 const char* reason;
1508 switch (dropReason) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001509 case DropReason::POLICY:
Prabir Pradhan65613802023-02-22 23:36:58 +00001510 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001511 ALOGD("Dropped event because policy consumed it.");
1512 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001513 reason = "inbound event was dropped because the policy consumed it";
1514 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001515 case DropReason::DISABLED:
1516 if (mLastDropReason != DropReason::DISABLED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001517 ALOGI("Dropped event because input dispatch is disabled.");
1518 }
1519 reason = "inbound event was dropped because input dispatch is disabled";
1520 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001521 case DropReason::BLOCKED:
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001522 LOG(INFO) << "Dropping because the current application is not responding and the user "
1523 "has started interacting with a different application: "
1524 << entry.getDescription();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001525 reason = "inbound event was dropped because the current application is not responding "
1526 "and the user has started interacting with a different application";
1527 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001528 case DropReason::STALE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001529 ALOGI("Dropped event because it is stale.");
1530 reason = "inbound event was dropped because it is stale";
1531 break;
Prabir Pradhan99987712020-11-10 18:43:05 -08001532 case DropReason::NO_POINTER_CAPTURE:
1533 ALOGI("Dropped event because there is no window with Pointer Capture.");
1534 reason = "inbound event was dropped because there is no window with Pointer Capture";
1535 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001536 case DropReason::NOT_DROPPED: {
1537 LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event");
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001538 return;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001539 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540 }
1541
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001542 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001543 case EventEntry::Type::KEY: {
Hu Guo3cfa7382023-11-15 09:50:04 +00001544 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001545 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason,
1546 keyEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001547 options.displayId = keyEntry.displayId;
1548 options.deviceId = keyEntry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001549 synthesizeCancelationEventsForAllConnectionsLocked(options);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001550 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001552 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001553 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
1554 if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001555 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason,
1556 motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001557 options.displayId = motionEntry.displayId;
1558 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001559 synthesizeCancelationEventsForAllConnectionsLocked(options);
1560 } else {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001561 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001562 reason, motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001563 options.displayId = motionEntry.displayId;
1564 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001565 synthesizeCancelationEventsForAllConnectionsLocked(options);
1566 }
1567 break;
1568 }
Chris Yef59a2f42020-10-16 12:55:26 -07001569 case EventEntry::Type::SENSOR: {
1570 break;
1571 }
arthurhungb89ccb02020-12-30 16:19:01 +08001572 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1573 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001574 break;
1575 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001576 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001577 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001578 case EventEntry::Type::DEVICE_RESET: {
Dominik Laskowski75788452021-02-09 18:51:25 -08001579 LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001580 break;
1581 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 }
1583}
1584
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585bool InputDispatcher::haveCommandsLocked() const {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001586 return !mCommandQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587}
1588
Prabir Pradhancef936d2021-07-21 16:17:52 +00001589bool InputDispatcher::runCommandsLockedInterruptable() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001590 if (mCommandQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 return false;
1592 }
1593
1594 do {
Prabir Pradhancef936d2021-07-21 16:17:52 +00001595 auto command = std::move(mCommandQueue.front());
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001596 mCommandQueue.pop_front();
Prabir Pradhancef936d2021-07-21 16:17:52 +00001597 // Commands are run with the lock held, but may release and re-acquire the lock from within.
1598 command();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001599 } while (!mCommandQueue.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 return true;
1601}
1602
Prabir Pradhancef936d2021-07-21 16:17:52 +00001603void InputDispatcher::postCommandLocked(Command&& command) {
1604 mCommandQueue.push_back(command);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001605}
1606
1607void InputDispatcher::drainInboundQueueLocked() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001608 while (!mInboundQueue.empty()) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001609 std::shared_ptr<const EventEntry> entry = mInboundQueue.front();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001610 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001611 releaseInboundEventLocked(entry);
1612 }
1613 traceInboundQueueLengthLocked();
1614}
1615
1616void InputDispatcher::releasePendingEventLocked() {
1617 if (mPendingEvent) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 releaseInboundEventLocked(mPendingEvent);
Yi Kong9b14ac62018-07-17 13:48:38 -07001619 mPendingEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 }
1621}
1622
Prabir Pradhan24047542023-11-02 17:14:59 +00001623void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001624 const std::shared_ptr<InjectionState>& injectionState = entry->injectionState;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001625 if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001626 if (DEBUG_DISPATCH_CYCLE) {
1627 ALOGD("Injected inbound event was dropped.");
1628 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001629 setInjectionResult(*entry, InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 }
1631 if (entry == mNextUnblockedEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001632 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633 }
1634 addRecentEventLocked(entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635}
1636
1637void InputDispatcher::resetKeyRepeatLocked() {
1638 if (mKeyRepeatState.lastKeyEntry) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001639 mKeyRepeatState.lastKeyEntry = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 }
1641}
1642
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001643std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001644 std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645
Michael Wright2e732952014-09-24 13:26:59 -07001646 uint32_t policyFlags = entry->policyFlags &
1647 (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001649 std::shared_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001650 std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
1651 currentTime, entry->deviceId, entry->source,
1652 entry->displayId, policyFlags, entry->action, entry->flags,
1653 entry->keyCode, entry->scanCode, entry->metaState,
1654 entry->repeatCount + 1, entry->downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001656 newEntry->syntheticRepeat = true;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001657 if (mTracer) {
1658 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
1659 }
1660
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001661 mKeyRepeatState.lastKeyEntry = newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001662 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001663 return newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664}
1665
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001666bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
1667 const DeviceResetEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001668 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1669 ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime,
1670 entry.deviceId);
1671 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001672
liushenxiang42232912021-05-21 20:24:09 +08001673 // Reset key repeating in case a keyboard device was disabled or enabled.
1674 if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) {
1675 resetKeyRepeatLocked();
1676 }
1677
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001678 ScopedSyntheticEventTracer traceContext(mTracer);
1679 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset",
1680 traceContext.getTracker());
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001681 options.deviceId = entry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682 synthesizeCancelationEventsForAllConnectionsLocked(options);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07001683
1684 // Remove all active pointers from this device
1685 for (auto& [_, touchState] : mTouchStatesByDisplay) {
1686 touchState.removeAllPointersForDevice(entry.deviceId);
1687 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001688 return true;
1689}
1690
Vishnu Nairad321cd2020-08-20 16:40:21 -07001691void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -08001692 const std::string& reason) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001693 if (mPendingEvent != nullptr) {
1694 // Move the pending event to the front of the queue. This will give the chance
1695 // for the pending event to get dispatched to the newly focused window
1696 mInboundQueue.push_front(mPendingEvent);
1697 mPendingEvent = nullptr;
1698 }
1699
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001700 std::unique_ptr<FocusEntry> focusEntry =
1701 std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus,
1702 reason);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001703
1704 // This event should go to the front of the queue, but behind all other focus events
1705 // Find the last focus event, and insert right after it
Prabir Pradhan24047542023-11-02 17:14:59 +00001706 auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(),
1707 [](const std::shared_ptr<const EventEntry>& event) {
1708 return event->type == EventEntry::Type::FOCUS;
1709 });
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001710
1711 // Maintain the order of focus events. Insert the entry after all other focus events.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001712 mInboundQueue.insert(it.base(), std::move(focusEntry));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001713}
1714
Prabir Pradhan24047542023-11-02 17:14:59 +00001715void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime,
1716 std::shared_ptr<const FocusEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001717 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
1718 if (connection == nullptr) {
1719 return; // Connection has gone away
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001720 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001721 entry->dispatchInProgress = true;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00001722 std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") +
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001723 connection->getInputChannelName();
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07001724 std::string reason = std::string("reason=").append(entry->reason);
1725 android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001726 dispatchEventLocked(currentTime, entry, {{connection}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001727}
1728
Prabir Pradhan99987712020-11-10 18:43:05 -08001729void InputDispatcher::dispatchPointerCaptureChangedLocked(
Prabir Pradhan24047542023-11-02 17:14:59 +00001730 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry,
Prabir Pradhan99987712020-11-10 18:43:05 -08001731 DropReason& dropReason) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001732 dropReason = DropReason::NOT_DROPPED;
1733
Prabir Pradhan99987712020-11-10 18:43:05 -08001734 const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr;
Prabir Pradhan99987712020-11-10 18:43:05 -08001735 sp<IBinder> token;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001736
Hiroki Sato25040232024-02-22 17:21:22 +09001737 if (entry->pointerCaptureRequest.isEnable()) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001738 // Enable Pointer Capture.
1739 if (haveWindowWithPointerCapture &&
1740 (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) {
Prabir Pradhan7092e262022-05-03 16:51:09 +00001741 // This can happen if pointer capture is disabled and re-enabled before we notify the
1742 // app of the state change, so there is no need to notify the app.
1743 ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change.");
1744 return;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001745 }
Hiroki Sato25040232024-02-22 17:21:22 +09001746 if (!mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001747 // This can happen if a window requests capture and immediately releases capture.
1748 ALOGW("No window requested Pointer Capture.");
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001749 dropReason = DropReason::NO_POINTER_CAPTURE;
Prabir Pradhan99987712020-11-10 18:43:05 -08001750 return;
1751 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001752 if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) {
1753 ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch.");
1754 return;
1755 }
1756
Vishnu Nairc519ff72021-01-21 08:23:08 -08001757 token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08001758 LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture.");
Hiroki Sato25040232024-02-22 17:21:22 +09001759 LOG_ALWAYS_FATAL_IF(token != entry->pointerCaptureRequest.window,
1760 "Unexpected requested window for Pointer Capture.");
Prabir Pradhan99987712020-11-10 18:43:05 -08001761 mWindowTokenWithPointerCapture = token;
1762 } else {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001763 // Disable Pointer Capture.
1764 // We do not check if the sequence number matches for requests to disable Pointer Capture
1765 // for two reasons:
1766 // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries
1767 // to disable capture with the same sequence number: one generated by
1768 // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer
1769 // Capture being disabled in InputReader.
1770 // 2. We respect any request to disable Pointer Capture generated by InputReader, since the
1771 // actual Pointer Capture state that affects events being generated by input devices is
1772 // in InputReader.
1773 if (!haveWindowWithPointerCapture) {
1774 // Pointer capture was already forcefully disabled because of focus change.
1775 dropReason = DropReason::NOT_DROPPED;
1776 return;
1777 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001778 token = mWindowTokenWithPointerCapture;
1779 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001780 if (mCurrentPointerCaptureRequest.isEnable()) {
1781 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001782 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001783 }
1784
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001785 auto connection = getConnectionLocked(token);
1786 if (connection == nullptr) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001787 // Window has gone away, clean up Pointer Capture state.
1788 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001789 if (mCurrentPointerCaptureRequest.isEnable()) {
1790 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001791 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001792 return;
1793 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001794 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001795 dispatchEventLocked(currentTime, entry, {{connection}});
Prabir Pradhan99987712020-11-10 18:43:05 -08001796
1797 dropReason = DropReason::NOT_DROPPED;
1798}
1799
Prabir Pradhan24047542023-11-02 17:14:59 +00001800void InputDispatcher::dispatchTouchModeChangeLocked(
1801 nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001802 const std::vector<sp<WindowInfoHandle>>& windowHandles =
Antonio Kantek15beb512022-06-13 22:35:41 +00001803 getWindowHandlesLocked(entry->displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001804 if (windowHandles.empty()) {
1805 return;
1806 }
1807 const std::vector<InputTarget> inputTargets =
1808 getInputTargetsFromWindowHandlesLocked(windowHandles);
1809 if (inputTargets.empty()) {
1810 return;
1811 }
1812 entry->dispatchInProgress = true;
1813 dispatchEventLocked(currentTime, entry, inputTargets);
1814}
1815
1816std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked(
1817 const std::vector<sp<WindowInfoHandle>>& windowHandles) const {
1818 std::vector<InputTarget> inputTargets;
1819 for (const sp<WindowInfoHandle>& handle : windowHandles) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001820 const sp<IBinder>& token = handle->getToken();
1821 if (token == nullptr) {
1822 continue;
1823 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001824 std::shared_ptr<Connection> connection = getConnectionLocked(token);
1825 if (connection == nullptr) {
1826 continue; // Connection has gone away
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001827 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001828 inputTargets.emplace_back(connection);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001829 }
1830 return inputTargets;
1831}
1832
Prabir Pradhan24047542023-11-02 17:14:59 +00001833bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001834 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001836 if (!entry->dispatchInProgress) {
Hu Guofe3c8f12023-09-22 17:20:15 +08001837 if (!entry->syntheticRepeat && entry->action == AKEY_EVENT_ACTION_DOWN &&
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001838 (entry->policyFlags & POLICY_FLAG_TRUSTED) &&
1839 (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
1840 if (mKeyRepeatState.lastKeyEntry &&
Chris Ye2ad95392020-09-01 13:44:44 -07001841 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode &&
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842 // We have seen two identical key downs in a row which indicates that the device
1843 // driver is automatically generating key repeats itself. We take note of the
1844 // repeat here, but we disable our own next key repeat timer since it is clear that
1845 // we will not need to synthesize key repeats ourselves.
Chris Ye2ad95392020-09-01 13:44:44 -07001846 mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) {
1847 // Make sure we don't get key down from a different device. If a different
1848 // device Id has same key pressed down, the new device Id will replace the
1849 // current one to hold the key repeat with repeat count reset.
1850 // In the future when got a KEY_UP on the device id, drop it and do not
1851 // stop the key repeat on current device.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
1853 resetKeyRepeatLocked();
Colin Cross5b799302022-10-18 21:52:41 -07001854 mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves
Michael Wrightd02c5b62014-02-10 15:10:22 -08001855 } else {
1856 // Not a repeat. Save key down state in case we do see a repeat later.
1857 resetKeyRepeatLocked();
1858 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
1859 }
1860 mKeyRepeatState.lastKeyEntry = entry;
Chris Ye2ad95392020-09-01 13:44:44 -07001861 } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry &&
1862 mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001863 // The key on device 'deviceId' is still down, do not stop key repeat
Prabir Pradhan65613802023-02-22 23:36:58 +00001864 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001865 ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId);
1866 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001867 } else if (!entry->syntheticRepeat) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001868 resetKeyRepeatLocked();
1869 }
1870
1871 if (entry->repeatCount == 1) {
1872 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
1873 } else {
1874 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
1875 }
1876
1877 entry->dispatchInProgress = true;
1878
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001879 logOutboundKeyDetails("dispatchKey - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 }
1881
1882 // Handle case where the policy asked us to try again later last time.
Michael Wright5caf55a2022-11-24 22:31:42 +00001883 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884 if (currentTime < entry->interceptKeyWakeupTime) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001885 nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 return false; // wait until next wakeup
1887 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001888 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 entry->interceptKeyWakeupTime = 0;
1890 }
1891
1892 // Give the policy a chance to intercept the key.
Michael Wright5caf55a2022-11-24 22:31:42 +00001893 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07001895 sp<IBinder> focusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08001896 mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00001897
1898 auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) {
1899 doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
1900 };
1901 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 return false; // wait for the command to run
1903 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00001904 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001906 } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001907 if (*dropReason == DropReason::NOT_DROPPED) {
1908 *dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001909 }
1910 }
1911
1912 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001913 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001914 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001915 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
1916 : InputEventInjectionResult::FAILED);
Garfield Tan6a5a14e2020-01-28 13:24:04 -08001917 mReporter->reportDroppedKey(entry->id);
Josep del Rioc8fdedb2024-05-21 13:32:43 +00001918 // Poke user activity for consumed keys, as it may have not been reported due to
1919 // the focused window requesting user activity to be disabled
1920 if (*dropReason == DropReason::POLICY &&
1921 mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
1922 pokeUserActivityLocked(*entry);
1923 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924 return true;
1925 }
1926
1927 // Identify targets.
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001928 Result<sp<WindowInfoHandle>, InputEventInjectionResult> result =
1929 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001931 if (!result.ok()) {
1932 if (result.error().code() == InputEventInjectionResult::PENDING) {
1933 return false;
1934 }
1935 setInjectionResult(*entry, result.error().code());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001936 return true;
1937 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001938 sp<WindowInfoHandle>& focusedWindow = *result;
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001939 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
1940
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001941 setInjectionResult(*entry, InputEventInjectionResult::SUCCEEDED);
1942
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001943 std::vector<InputTarget> inputTargets;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001944 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
1945 InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001947 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001948 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001950 if (mTracer) {
1951 ensureEventTraced(*entry);
1952 for (const auto& target : inputTargets) {
1953 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
1954 }
1955 }
1956
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957 // Dispatch the key.
1958 dispatchEventLocked(currentTime, entry, inputTargets);
1959 return true;
1960}
1961
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001962void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001963 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001964 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001965 "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, "
1966 "metaState=0x%x, repeatCount=%d, downTime=%" PRId64,
Linnan Li13bf76a2024-05-05 19:18:02 +08001967 prefix, entry.eventTime, entry.deviceId, entry.source,
1968 entry.displayId.toString().c_str(), entry.policyFlags, entry.action, entry.flags,
1969 entry.keyCode, entry.scanCode, entry.metaState, entry.repeatCount, entry.downTime);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001970 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001971}
1972
Prabir Pradhancef936d2021-07-21 16:17:52 +00001973void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00001974 const std::shared_ptr<const SensorEntry>& entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001975 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001976 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1977 ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, "
1978 "source=0x%x, sensorType=%s",
1979 entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source,
Dominik Laskowski75788452021-02-09 18:51:25 -08001980 ftl::enum_string(entry->sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001981 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00001982 auto command = [this, entry]() REQUIRES(mLock) {
1983 scoped_unlock unlock(mLock);
1984
1985 if (entry->accuracyChanged) {
Prabir Pradhana41d2442023-04-20 21:30:40 +00001986 mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001987 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00001988 mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy,
1989 entry->hwTimestamp, entry->values);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001990 };
1991 postCommandLocked(std::move(command));
Chris Yef59a2f42020-10-16 12:55:26 -07001992}
1993
1994bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001995 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1996 ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId,
Dominik Laskowski75788452021-02-09 18:51:25 -08001997 ftl::enum_string(sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001998 }
Chris Yef59a2f42020-10-16 12:55:26 -07001999 { // acquire lock
2000 std::scoped_lock _l(mLock);
2001
2002 for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) {
Prabir Pradhan24047542023-11-02 17:14:59 +00002003 std::shared_ptr<const EventEntry> entry = *it;
Chris Yef59a2f42020-10-16 12:55:26 -07002004 if (entry->type == EventEntry::Type::SENSOR) {
2005 it = mInboundQueue.erase(it);
2006 releaseInboundEventLocked(entry);
2007 }
2008 }
2009 }
2010 return true;
2011}
2012
Prabir Pradhan24047542023-11-02 17:14:59 +00002013bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime,
2014 std::shared_ptr<const MotionEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002015 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002016 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002017 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002018 if (!entry->dispatchInProgress) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002019 entry->dispatchInProgress = true;
2020
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002021 logOutboundMotionDetails("dispatchMotion - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002022 }
2023
2024 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07002025 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002026 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002027 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
2028 : InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002029 return true;
2030 }
2031
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002032 const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002033
2034 // Identify targets.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002035 std::vector<InputTarget> inputTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002036
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002037 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002038 if (isPointerEvent) {
2039 // Pointer event. (eg. touchscreen)
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00002040
2041 if (mDragState &&
2042 (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2043 // If drag and drop ongoing and pointer down occur: pilfer drag window pointers
2044 pilferPointersLocked(mDragState->dragWindow->getToken());
2045 }
2046
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002047 Result<std::vector<InputTarget>, InputEventInjectionResult> result =
2048 findTouchedWindowTargetsLocked(currentTime, *entry);
2049
2050 if (result.ok()) {
2051 inputTargets = std::move(*result);
2052 injectionResult = InputEventInjectionResult::SUCCEEDED;
2053 } else {
2054 injectionResult = result.error().code();
2055 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056 } else {
2057 // Non touch event. (eg. trackball)
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002058 Result<sp<WindowInfoHandle>, InputEventInjectionResult> result =
2059 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime);
2060 if (result.ok()) {
2061 sp<WindowInfoHandle>& focusedWindow = *result;
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002062 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002063 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
2064 InputTarget::Flags::FOREGROUND, getDownTime(*entry),
2065 inputTargets);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002066 injectionResult = InputEventInjectionResult::SUCCEEDED;
2067 } else {
2068 injectionResult = result.error().code();
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002069 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002071 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072 return false;
2073 }
2074
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002075 setInjectionResult(*entry, injectionResult);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002076 if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) {
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002077 return true;
2078 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002079 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightfb04fd52022-11-24 22:31:11 +00002080 CancelationOptions::Mode mode(
2081 isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS
2082 : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002083 CancelationOptions options(mode, "input event injection failed", entry->traceTracker);
Linnan Lid8150952024-01-26 18:07:17 +00002084 options.displayId = entry->displayId;
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002085 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002086 return true;
2087 }
2088
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002089 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002090 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002091
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00002092 if (mTracer) {
2093 ensureEventTraced(*entry);
2094 for (const auto& target : inputTargets) {
2095 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
2096 }
2097 }
2098
Michael Wrightd02c5b62014-02-10 15:10:22 -08002099 // Dispatch the motion.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002100 dispatchEventLocked(currentTime, entry, inputTargets);
2101 return true;
2102}
2103
chaviw98318de2021-05-19 16:45:23 -05002104void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle,
Arthur Hung54745652022-04-20 07:17:41 +00002105 bool isExiting, const int32_t rawX,
2106 const int32_t rawY) {
2107 const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY));
arthurhungb89ccb02020-12-30 16:19:01 +08002108 std::unique_ptr<DragEntry> dragEntry =
Arthur Hung54745652022-04-20 07:17:41 +00002109 std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(),
2110 isExiting, xy.x, xy.y);
arthurhungb89ccb02020-12-30 16:19:01 +08002111
2112 enqueueInboundEventLocked(std::move(dragEntry));
2113}
2114
Prabir Pradhan24047542023-11-02 17:14:59 +00002115void InputDispatcher::dispatchDragLocked(nsecs_t currentTime,
2116 std::shared_ptr<const DragEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002117 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
2118 if (connection == nullptr) {
2119 return; // Connection has gone away
arthurhungb89ccb02020-12-30 16:19:01 +08002120 }
arthurhungb89ccb02020-12-30 16:19:01 +08002121 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002122 dispatchEventLocked(currentTime, entry, {{connection}});
arthurhungb89ccb02020-12-30 16:19:01 +08002123}
2124
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002125void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002126 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002127 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002128 "action=%s, actionButton=0x%x, flags=0x%x, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002129 "metaState=0x%x, buttonState=0x%x, downTime=%" PRId64,
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002130 prefix, entry.eventTime, entry.deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +08002131 inputEventSourceToString(entry.source).c_str(), entry.displayId.toString().c_str(),
2132 entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(),
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002133 entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002134
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002135 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002136 ALOGD(" Pointer %d: id=%d, toolType=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002137 "x=%f, y=%f, pressure=%f, size=%f, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002138 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002139 i, entry.pointerProperties[i].id,
2140 ftl::enum_string(entry.pointerProperties[i].toolType).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002141 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2142 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2143 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2144 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2145 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2146 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2147 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2148 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2149 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
2150 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002151 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152}
2153
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002154void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00002155 std::shared_ptr<const EventEntry> eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002156 const std::vector<InputTarget>& inputTargets) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002157 ATRACE_CALL();
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002158 if (DEBUG_DISPATCH_CYCLE) {
2159 ALOGD("dispatchEventToCurrentInputTargets");
2160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002161
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002162 processInteractionsLocked(*eventEntry, inputTargets);
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00002163
Michael Wrightd02c5b62014-02-10 15:10:22 -08002164 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
2165
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002166 pokeUserActivityLocked(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002167
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002168 for (const InputTarget& inputTarget : inputTargets) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002169 std::shared_ptr<Connection> connection = inputTarget.connection;
2170 prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002171 }
2172}
2173
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07002174void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002175 // We will not be breaking any connections here, even if the policy wants us to abort dispatch.
2176 // If the policy decides to close the app, we will get a channel removal event via
2177 // unregisterInputChannel, and will clean up the connection that way. We are already not
2178 // sending new pointers to the connection when it blocked, but focused events will continue to
2179 // pile up.
2180 ALOGW("Canceling events for %s because it is unresponsive",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002181 connection->getInputChannelName().c_str());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002182 if (connection->status != Connection::Status::NORMAL) {
2183 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002185 ScopedSyntheticEventTracer traceContext(mTracer);
Prabir Pradhanfc364722024-02-08 17:51:20 +00002186 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002187 "application not responding", traceContext.getTracker());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002188
2189 sp<WindowInfoHandle> windowHandle;
2190 if (!connection->monitor) {
2191 windowHandle = getWindowHandleLocked(connection->getToken());
2192 if (windowHandle == nullptr) {
2193 // The window that is receiving this ANR was removed, so there is no need to generate
2194 // cancellations, because the cancellations would have already been generated when
2195 // the window was removed.
2196 return;
2197 }
2198 }
2199 synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200}
2201
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002202void InputDispatcher::resetNoFocusedWindowTimeoutLocked() {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01002203 if (DEBUG_FOCUS) {
2204 ALOGD("Resetting ANR timeouts.");
2205 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
2207 // Reset input target wait timeout.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002208 mNoFocusedWindowTimeoutTime = std::nullopt;
Chris Yea209fde2020-07-22 13:54:51 -07002209 mAwaitedFocusedApplication.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002210}
2211
Tiger Huang721e26f2018-07-24 22:26:19 +08002212/**
2213 * Get the display id that the given event should go to. If this event specifies a valid display id,
2214 * then it should be dispatched to that display. Otherwise, the event goes to the focused display.
2215 * Focused display is the display that the user most recently interacted with.
2216 */
Linnan Li13bf76a2024-05-05 19:18:02 +08002217ui::LogicalDisplayId InputDispatcher::getTargetDisplayId(const EventEntry& entry) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002218 ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID};
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002219 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002220 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002221 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
2222 displayId = keyEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002223 break;
2224 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002225 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002226 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
2227 displayId = motionEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002228 break;
2229 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00002230 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08002231 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002232 case EventEntry::Type::FOCUS:
Chris Yef59a2f42020-10-16 12:55:26 -07002233 case EventEntry::Type::DEVICE_RESET:
arthurhungb89ccb02020-12-30 16:19:01 +08002234 case EventEntry::Type::SENSOR:
2235 case EventEntry::Type::DRAG: {
Dominik Laskowski75788452021-02-09 18:51:25 -08002236 ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002237 return ui::LogicalDisplayId::INVALID;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002238 }
Tiger Huang721e26f2018-07-24 22:26:19 +08002239 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002240 return displayId == ui::LogicalDisplayId::INVALID ? mFocusedDisplayId : displayId;
Tiger Huang721e26f2018-07-24 22:26:19 +08002241}
2242
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002243bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime,
2244 const char* focusedWindowName) {
2245 if (mAnrTracker.empty()) {
2246 // already processed all events that we waited for
2247 mKeyIsWaitingForEventsTimeout = std::nullopt;
2248 return false;
2249 }
2250
2251 if (!mKeyIsWaitingForEventsTimeout.has_value()) {
2252 // Start the timer
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00002253 // Wait to send key because there are unprocessed events that may cause focus to change
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002254 mKeyIsWaitingForEventsTimeout = currentTime +
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08002255 std::chrono::duration_cast<std::chrono::nanoseconds>(
2256 mPolicy.getKeyWaitingForEventsTimeout())
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002257 .count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002258 return true;
2259 }
2260
2261 // We still have pending events, and already started the timer
2262 if (currentTime < *mKeyIsWaitingForEventsTimeout) {
2263 return true; // Still waiting
2264 }
2265
2266 // Waited too long, and some connection still hasn't processed all motions
2267 // Just send the key to the focused window
2268 ALOGW("Dispatching key to %s even though there are other unprocessed events",
2269 focusedWindowName);
2270 mKeyIsWaitingForEventsTimeout = std::nullopt;
2271 return false;
2272}
2273
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002274Result<sp<WindowInfoHandle>, InputEventInjectionResult>
2275InputDispatcher::findFocusedWindowTargetLocked(nsecs_t currentTime, const EventEntry& entry,
2276 nsecs_t& nextWakeupTime) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002277 ui::LogicalDisplayId displayId = getTargetDisplayId(entry);
chaviw98318de2021-05-19 16:45:23 -05002278 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Chris Yea209fde2020-07-22 13:54:51 -07002279 std::shared_ptr<InputApplicationHandle> focusedApplicationHandle =
Tiger Huang721e26f2018-07-24 22:26:19 +08002280 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
2281
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282 // If there is no currently focused window and no focused application
2283 // then drop the event.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002284 if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) {
2285 ALOGI("Dropping %s event because there is no focused window or focused application in "
Linnan Li13bf76a2024-05-05 19:18:02 +08002286 "display %s.",
2287 ftl::enum_string(entry.type).c_str(), displayId.toString().c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002288 return injectionError(InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 }
2290
Vishnu Nair062a8672021-09-03 16:07:44 -07002291 // Drop key events if requested by input feature
2292 if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) {
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002293 return injectionError(InputEventInjectionResult::FAILED);
Vishnu Nair062a8672021-09-03 16:07:44 -07002294 }
2295
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002296 // Compatibility behavior: raise ANR if there is a focused application, but no focused window.
2297 // Only start counting when we have a focused event to dispatch. The ANR is canceled if we
2298 // start interacting with another application via touch (app switch). This code can be removed
2299 // if the "no focused window ANR" is moved to the policy. Input doesn't know whether
2300 // an app is expected to have a focused window.
2301 if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) {
2302 if (!mNoFocusedWindowTimeoutTime.has_value()) {
2303 // We just discovered that there's no focused window. Start the ANR timer
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002304 std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout(
2305 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
2306 mNoFocusedWindowTimeoutTime = currentTime + timeout.count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002307 mAwaitedFocusedApplication = focusedApplicationHandle;
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05002308 mAwaitedApplicationDisplayId = displayId;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002309 ALOGW("Waiting because no window has focus but %s may eventually add a "
2310 "window when it finishes starting up. Will wait for %" PRId64 "ms",
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002311 mAwaitedFocusedApplication->getName().c_str(), millis(timeout));
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002312 nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002313 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002314 } else if (currentTime > *mNoFocusedWindowTimeoutTime) {
2315 // Already raised ANR. Drop the event
2316 ALOGE("Dropping %s event because there is no focused window",
Dominik Laskowski75788452021-02-09 18:51:25 -08002317 ftl::enum_string(entry.type).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002318 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002319 } else {
2320 // Still waiting for the focused window
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002321 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002322 }
2323 }
2324
2325 // we have a valid, non-null focused window
2326 resetNoFocusedWindowTimeoutLocked();
2327
Prabir Pradhan5735a322022-04-11 17:23:34 +00002328 // Verify targeted injection.
2329 if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) {
2330 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002331 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 }
2333
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002334 if (focusedWindowHandle->getInfo()->inputConfig.test(
2335 WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002336 ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002337 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002338 }
2339
2340 // If the event is a key event, then we must wait for all previous events to
2341 // complete before delivering it because previous events may have the
2342 // side-effect of transferring focus to a different window and we want to
2343 // ensure that the following keys are sent to the new window.
2344 //
2345 // Suppose the user touches a button in a window then immediately presses "A".
2346 // If the button causes a pop-up window to appear then we want to ensure that
2347 // the "A" key is delivered to the new pop-up window. This is because users
2348 // often anticipate pending UI changes when typing on a keyboard.
2349 // To obtain this behavior, we must serialize key events with respect to all
2350 // prior input events.
2351 if (entry.type == EventEntry::Type::KEY) {
2352 if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002353 nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002354 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002355 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002357 // Success!
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002358 return focusedWindowHandle;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002359}
2360
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002361/**
2362 * Given a list of monitors, remove the ones we cannot find a connection for, and the ones
2363 * that are currently unresponsive.
2364 */
Prabir Pradhan0a99c922021-09-03 08:27:53 -07002365std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked(
2366 const std::vector<Monitor>& monitors) const {
2367 std::vector<Monitor> responsiveMonitors;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002368 std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors),
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002369 [](const Monitor& monitor) REQUIRES(mLock) {
2370 std::shared_ptr<Connection> connection = monitor.connection;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002371 if (!connection->responsive) {
2372 ALOGW("Unresponsive monitor %s will not get the new gesture",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002373 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002374 return false;
2375 }
2376 return true;
2377 });
2378 return responsiveMonitors;
2379}
2380
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002381base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult>
2382InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002383 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002385 std::vector<InputTarget> targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386 // For security reasons, we defer updating the touch state until we are sure that
2387 // event injection will be allowed.
Linnan Li13bf76a2024-05-05 19:18:02 +08002388 const ui::LogicalDisplayId displayId = entry.displayId;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002389 const int32_t action = entry.action;
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07002390 const int32_t maskedAction = MotionEvent::getActionMasked(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002391
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002392 // Copy current touch state into tempTouchState.
2393 // This state will be used to update mTouchStatesByDisplay at the end of this function.
2394 // If no state for the specified display exists, then our initial state will be empty.
Yi Kong9b14ac62018-07-17 13:48:38 -07002395 const TouchState* oldState = nullptr;
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002396 TouchState tempTouchState;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002397 if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
2398 oldState = &(it->second);
Prabir Pradhane680f9b2022-02-04 04:24:00 -08002399 tempTouchState = *oldState;
Jeff Brownf086ddb2014-02-11 14:28:48 -08002400 }
2401
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002402 bool isSplit = shouldSplitTouch(tempTouchState, entry);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002403
2404 const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE ||
2405 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2406 maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002407 // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any
2408 // touchable windows.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002409 const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002410 const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) ||
2411 (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002412 const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL ||
2413 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2414 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE;
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002415 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002416
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002417 if (newGesture) {
2418 isSplit = false;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002419 }
2420
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002421 if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) {
2422 // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated.
2423 tempTouchState.clearHoveringPointers(entry.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 }
2425
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002426 if (isHoverAction) {
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002427 if (wasDown) {
2428 // Started hovering, but the device is already down: reject the hover event
2429 LOG(ERROR) << "Got hover event " << entry.getDescription()
2430 << " but the device is already down " << oldState->dump();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002431 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002432 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002433 // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase
2434 // all of the existing hovering pointers and recompute.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002435 tempTouchState.clearHoveringPointers(entry.deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002436 }
2437
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
2439 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002440 const auto [x, y] = resolveTouchedPosition(entry);
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002441 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002442 const PointerProperties& pointer = entry.pointerProperties[pointerIndex];
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002443 // Outside targets should be added upon first dispatched DOWN event. That means, this should
2444 // be a pointer that would generate ACTION_DOWN, *and* touch should not already be down.
Prabir Pradhand65552b2021-10-07 11:23:50 -07002445 const bool isStylus = isPointerFromStylus(entry, pointerIndex);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002446 sp<WindowInfoHandle> newTouchedWindowHandle =
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002447 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Michael Wright3dd60e22019-03-27 22:06:44 +00002448
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002449 if (isDown) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002450 targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002451 }
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07002452 LOG_IF(INFO, newTouchedWindowHandle == nullptr)
2453 << "No new touched window at (" << std::format("{:.1f}, {:.1f}", x, y)
2454 << ") in display " << displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 // Handle the case where we did not find a window.
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07002456 if (!input_flags::split_all_touches()) {
2457 // If we are force splitting all touches, then touches outside of the window should
2458 // be dropped, even if this device already has pointers down in another window.
2459 if (newTouchedWindowHandle == nullptr) {
2460 // Try to assign the pointer to the first foreground window we find, if there is
2461 // one.
2462 newTouchedWindowHandle =
2463 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
2464 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002465 }
2466
Prabir Pradhan5735a322022-04-11 17:23:34 +00002467 // Verify targeted injection.
2468 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2469 ALOGW("Dropping injected touch event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002470 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002471 }
2472
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002473 // Figure out whether splitting will be allowed for this window.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002474 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002475 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
2476 // New window supports splitting, but we should never split mouse events.
2477 isSplit = !isFromMouse;
2478 } else if (isSplit) {
2479 // New window does not support splitting but we have already split events.
2480 // Ignore the new window.
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002481 LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName()
2482 << " because it doesn't support split touch";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002483 newTouchedWindowHandle = nullptr;
2484 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002485 } else {
2486 // No window is touched, so set split to true. This will allow the next pointer down to
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002487 // be delivered to a new window which supports split touch. Pointers from a mouse device
2488 // should never be split.
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002489 isSplit = !isFromMouse;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002490 }
2491
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002492 std::vector<sp<WindowInfoHandle>> newTouchedWindows =
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07002493 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus, entry.deviceId);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002494 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002495 // Process the foreground window first so that it is the first to receive the event.
2496 newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002497 }
2498
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002499 if (newTouchedWindows.empty()) {
Siarhei Vishniakouac42d242024-06-28 10:43:53 -07002500 LOG(INFO) << "Dropping event because there is no touchable window at (" << x << ", "
2501 << y << ") on display " << displayId << ": " << entry;
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002502 return injectionError(InputEventInjectionResult::FAILED);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002503 }
2504
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002505 for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002506 if (!canWindowReceiveMotionLocked(windowHandle, entry)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002507 continue;
2508 }
2509
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002510 if (isHoverAction) {
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07002511 // The "windowHandle" is the target of this hovering pointer.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002512 tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002513 }
2514
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002515 // Set target flags.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002516 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002517
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002518 if (canReceiveForegroundTouches(*windowHandle->getInfo())) {
2519 // There should only be one touched window that can be "foreground" for the pointer.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002520 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002521 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002522
2523 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002524 targetFlags |= InputTarget::Flags::SPLIT;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002525 }
2526 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002527 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002528 } else if (isWindowObscuredLocked(windowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002529 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002530 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002531
2532 // Update the temporary touch state.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002533
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002534 if (!isHoverAction) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002535 const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN ||
2536 maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN;
Siarhei Vishniakoue535f972024-03-29 14:55:21 -07002537 Result<void> addResult =
2538 tempTouchState.addOrUpdateWindow(windowHandle,
2539 InputTarget::DispatchMode::AS_IS,
2540 targetFlags, entry.deviceId, {pointer},
2541 isDownOrPointerDown
2542 ? std::make_optional(
2543 entry.eventTime)
2544 : std::nullopt);
2545 if (!addResult.ok()) {
2546 LOG(ERROR) << "Error while processing " << entry << " for "
2547 << windowHandle->getName();
2548 logDispatchStateLocked();
2549 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002550 // If this is the pointer going down and the touched window has a wallpaper
2551 // then also add the touched wallpaper windows so they are locked in for the
2552 // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or
2553 // SCROLL because the wallpaper engine only supports touch events. We would need to
2554 // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to
2555 // handle these events.
2556 if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) &&
Arthur Hungc539dbb2022-12-08 07:45:36 +00002557 windowHandle->getInfo()->inputConfig.test(
2558 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
2559 sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle);
2560 if (wallpaper != nullptr) {
2561 ftl::Flags<InputTarget::Flags> wallpaperFlags =
2562 InputTarget::Flags::WINDOW_IS_OBSCURED |
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002563 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Arthur Hungc539dbb2022-12-08 07:45:36 +00002564 if (isSplit) {
2565 wallpaperFlags |= InputTarget::Flags::SPLIT;
2566 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002567 tempTouchState.addOrUpdateWindow(wallpaper,
2568 InputTarget::DispatchMode::AS_IS,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002569 wallpaperFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002570 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002571 }
2572 }
2573 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002575
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002576 // If a window is already pilfering some pointers, give it this new pointer as well and
2577 // make it pilfering. This will prevent other non-spy windows from getting this pointer,
2578 // which is a specific behaviour that we want.
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002579 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002580 if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) &&
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002581 touchedWindow.hasPilferingPointers(entry.deviceId)) {
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002582 // This window is already pilfering some pointers, and this new pointer is also
2583 // going to it. Therefore, take over this pointer and don't give it to anyone
2584 // else.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002585 touchedWindow.addPilferingPointer(entry.deviceId, pointer.id);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002586 }
2587 }
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002588
2589 // Restrict all pilfered pointers to the pilfering windows.
2590 tempTouchState.cancelPointersForNonPilferingWindows();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591 } else {
2592 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
2593
2594 // If the pointer is not currently down, then ignore the event.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002595 if (!tempTouchState.isDown(entry.deviceId) &&
2596 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Siarhei Vishniakou31dd1552023-10-30 18:46:10 -07002597 if (DEBUG_DROPPED_EVENTS_VERBOSE) {
2598 LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId
2599 << " is not down or we previously dropped the pointer down event in "
2600 << "display " << displayId << ": " << entry.getDescription();
2601 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002602 return injectionError(InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 }
2604
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002605 // If the pointer is not currently hovering, then ignore the event.
2606 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) {
2607 const int32_t pointerId = entry.pointerProperties[0].id;
2608 if (oldState == nullptr ||
2609 oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) {
2610 LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in "
2611 "display "
2612 << displayId << ": " << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002613 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002614 }
2615 tempTouchState.removeHoveringPointer(entry.deviceId, pointerId);
2616 }
2617
arthurhung6d4bed92021-03-17 11:59:33 +08002618 addDragEventLocked(entry);
arthurhungb89ccb02020-12-30 16:19:01 +08002619
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 // Check whether touches should slip outside of the current foreground window.
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002621 if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 &&
Linnan Li49b2b202024-04-12 12:46:40 +08002622 tempTouchState.isSlippery(entry.deviceId)) {
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07002623 const auto [x, y] = resolveTouchedPosition(entry);
Harry Cutts33476232023-01-30 19:57:29 +00002624 const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0);
chaviw98318de2021-05-19 16:45:23 -05002625 sp<WindowInfoHandle> oldTouchedWindowHandle =
Linnan Liccf6ce32024-04-11 20:32:13 +08002626 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002627 LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002628 sp<WindowInfoHandle> newTouchedWindowHandle =
2629 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Vishnu Nair062a8672021-09-03 16:07:44 -07002630
Prabir Pradhan5735a322022-04-11 17:23:34 +00002631 // Verify targeted injection.
2632 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2633 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002634 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002635 }
2636
Linnan Libf069a32024-02-29 17:22:59 +00002637 // Do not slide events to the window which can not receive motion event
Vishnu Nair062a8672021-09-03 16:07:44 -07002638 if (newTouchedWindowHandle != nullptr &&
Linnan Libf069a32024-02-29 17:22:59 +00002639 !canWindowReceiveMotionLocked(newTouchedWindowHandle, entry)) {
Vishnu Nair062a8672021-09-03 16:07:44 -07002640 newTouchedWindowHandle = nullptr;
2641 }
2642
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07002643 if (newTouchedWindowHandle != nullptr &&
2644 !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002645 ALOGI("Touch is slipping out of window %s into window %s in display %s",
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002646 oldTouchedWindowHandle->getName().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08002647 newTouchedWindowHandle->getName().c_str(), displayId.toString().c_str());
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002648
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 // Make a slippery exit from the old window.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08002650 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002651 const PointerProperties& pointer = entry.pointerProperties[0];
2652 pointerIds.set(pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002653
2654 const TouchedWindow& touchedWindow =
2655 tempTouchState.getTouchedWindow(oldTouchedWindowHandle);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002656 addPointerWindowTargetLocked(oldTouchedWindowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002657 InputTarget::DispatchMode::SLIPPERY_EXIT,
2658 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002659 touchedWindow.getDownTimeInTarget(entry.deviceId),
2660 targets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
2662 // Make a slippery entrance into the new window.
2663 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002664 isSplit = !isFromMouse;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 }
2666
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002667 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002668 if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002669 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002670 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002672 targetFlags |= InputTarget::Flags::SPLIT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002675 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002676 } else if (isWindowObscuredLocked(newTouchedWindowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002677 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 }
2679
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002680 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle,
2681 InputTarget::DispatchMode::SLIPPERY_ENTER,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002682 targetFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002683 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002684
2685 // Check if the wallpaper window should deliver the corresponding event.
2686 slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00002687 tempTouchState, entry, targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002688 tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002689 oldTouchedWindowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 }
2691 }
Arthur Hung96483742022-11-15 03:30:48 +00002692
2693 // Update the pointerIds for non-splittable when it received pointer down.
2694 if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2695 // If no split, we suppose all touched windows should receive pointer down.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002696 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002697 std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]};
2698 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Arthur Hung96483742022-11-15 03:30:48 +00002699 // Ignore drag window for it should just track one pointer.
2700 if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) {
2701 continue;
2702 }
Siarhei Vishniakou96204462024-07-13 23:59:47 -07002703 if (!touchedWindow.hasTouchingPointers(entry.deviceId)) {
2704 continue;
2705 }
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002706 touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers);
Arthur Hung96483742022-11-15 03:30:48 +00002707 }
2708 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 }
2710
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002711 // Update dispatching for hover enter and exit.
Sam Dubeyf886dec2023-01-27 13:28:19 +00002712 {
2713 std::vector<TouchedWindow> hoveringWindows =
2714 getHoveringWindowsLocked(oldState, tempTouchState, entry);
Linnan Li41859c42024-03-05 16:20:34 +00002715 // Hardcode to single hovering pointer for now.
2716 std::bitset<MAX_POINTER_ID + 1> pointerIds;
2717 pointerIds.set(entry.pointerProperties[0].id);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002718 for (const TouchedWindow& touchedWindow : hoveringWindows) {
Linnan Li41859c42024-03-05 16:20:34 +00002719 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
2720 touchedWindow.targetFlags, pointerIds,
2721 touchedWindow.getDownTimeInTarget(entry.deviceId),
2722 targets);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002723 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725
Prabir Pradhan5735a322022-04-11 17:23:34 +00002726 // Ensure that all touched windows are valid for injection.
2727 if (entry.injectionState != nullptr) {
2728 std::string errs;
2729 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00002730 const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry);
2731 if (err) errs += "\n - " + *err;
2732 }
2733 if (!errs.empty()) {
2734 ALOGW("Dropping targeted injection: At least one touched window is not owned by uid "
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002735 "%s:%s",
2736 entry.injectionState->targetUid->toString().c_str(), errs.c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002737 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002738 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002739 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002740
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002741 // Check whether windows listening for outside touches are owned by the same UID. If the owner
2742 // has a different UID, then we will not reveal coordinate information to this window.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
chaviw98318de2021-05-19 16:45:23 -05002744 sp<WindowInfoHandle> foregroundWindowHandle =
Linnan Liccf6ce32024-04-11 20:32:13 +08002745 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Michael Wright3dd60e22019-03-27 22:06:44 +00002746 if (foregroundWindowHandle) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002747 const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002748 for (InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002749 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002750 sp<WindowInfoHandle> targetWindow =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002751 getWindowHandleLocked(target.connection->getToken());
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002752 if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) {
2753 target.flags |= InputTarget::Flags::ZERO_COORDS;
Michael Wright3dd60e22019-03-27 22:06:44 +00002754 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755 }
2756 }
2757 }
2758 }
2759
Harry Cuttsb166c002023-05-09 13:06:05 +00002760 // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we
2761 // only want the system UI to handle these gestures.
2762 const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) &&
2763 entry.classification == MotionClassification::MULTI_FINGER_SWIPE;
2764 if (isTouchpadNavGesture) {
2765 filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets);
2766 }
2767
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002768 // Output targets from the touch state.
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002769 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002770 std::vector<PointerProperties> touchingPointers =
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002771 touchedWindow.getTouchingPointers(entry.deviceId);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002772 if (touchingPointers.empty()) {
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002773 continue;
2774 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002775 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002776 touchedWindow.targetFlags, getPointerIds(touchingPointers),
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002777 touchedWindow.getDownTimeInTarget(entry.deviceId), targets);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002778 }
Sam Dubey39d37cf2022-12-07 18:05:35 +00002779
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002780 // During targeted injection, only allow owned targets to receive events
2781 std::erase_if(targets, [&](const InputTarget& target) {
2782 LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr);
2783 const auto err = verifyTargetedInjection(target.windowHandle, entry);
2784 if (err) {
2785 LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName()
2786 << ": " << (*err);
2787 return true;
2788 }
2789 return false;
2790 });
2791
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002792 if (targets.empty()) {
2793 LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002794 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002795 }
2796
2797 // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no
2798 // window that is actually receiving the entire gesture.
2799 if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002800 return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002801 })) {
2802 LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: "
2803 << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002804 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002805 }
2806
Prabir Pradhan502a7252023-12-01 16:11:24 +00002807 // Now that we have generated all of the input targets for this event, reset the dispatch
2808 // mode for all touched window to AS_IS.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002809 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan502a7252023-12-01 16:11:24 +00002810 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002811 }
2812
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002813 // Update final pieces of touch state if the injector had permission.
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002814 if (maskedAction == AMOTION_EVENT_ACTION_UP) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002815 // Pointer went up.
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002816 tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002817 } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002818 // All pointers up or canceled.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002819 tempTouchState.removeAllPointersForDevice(entry.deviceId);
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002820 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2821 // One pointer went up.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002822 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
2823 const uint32_t pointerId = entry.pointerProperties[pointerIndex].id;
2824 tempTouchState.removeTouchingPointer(entry.deviceId, pointerId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825 }
2826
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002827 // Save changes unless the action was scroll in which case the temporary touch
2828 // state was only valid for this one action.
2829 if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002830 if (displayId >= ui::LogicalDisplayId::DEFAULT) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002831 tempTouchState.clearWindowsWithoutPointers();
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002832 mTouchStatesByDisplay[displayId] = tempTouchState;
2833 } else {
2834 mTouchStatesByDisplay.erase(displayId);
2835 }
2836 }
2837
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002838 if (tempTouchState.windows.empty()) {
2839 mTouchStatesByDisplay.erase(displayId);
2840 }
2841
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002842 return targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843}
2844
Linnan Li13bf76a2024-05-05 19:18:02 +08002845void InputDispatcher::finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07002846 // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we
2847 // have an explicit reason to support it.
2848 constexpr bool isStylus = false;
2849
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002850 sp<WindowInfoHandle> dropWindow =
Harry Cutts33476232023-01-30 19:57:29 +00002851 findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true);
arthurhung6d4bed92021-03-17 11:59:33 +08002852 if (dropWindow) {
2853 vec2 local = dropWindow->getInfo()->transform.transform(x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002854 sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y);
Arthur Hung6d0571e2021-04-09 20:18:16 +08002855 } else {
Arthur Hung54745652022-04-20 07:17:41 +00002856 ALOGW("No window found when drop.");
Prabir Pradhancef936d2021-07-21 16:17:52 +00002857 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08002858 }
2859 mDragState.reset();
2860}
2861
2862void InputDispatcher::addDragEventLocked(const MotionEntry& entry) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00002863 if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) {
arthurhungb89ccb02020-12-30 16:19:01 +08002864 return;
2865 }
2866
arthurhung6d4bed92021-03-17 11:59:33 +08002867 if (!mDragState->isStartDrag) {
2868 mDragState->isStartDrag = true;
2869 mDragState->isStylusButtonDownAtStart =
2870 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2871 }
2872
Arthur Hung54745652022-04-20 07:17:41 +00002873 // Find the pointer index by id.
2874 int32_t pointerIndex = 0;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002875 for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) {
Arthur Hung54745652022-04-20 07:17:41 +00002876 const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex];
2877 if (pointerProperties.id == mDragState->pointerId) {
2878 break;
arthurhung6d4bed92021-03-17 11:59:33 +08002879 }
Arthur Hung54745652022-04-20 07:17:41 +00002880 }
arthurhung6d4bed92021-03-17 11:59:33 +08002881
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002882 if (uint32_t(pointerIndex) == entry.getPointerCount()) {
Arthur Hung54745652022-04-20 07:17:41 +00002883 LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId);
Arthur Hung54745652022-04-20 07:17:41 +00002884 }
2885
2886 const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK;
2887 const int32_t x = entry.pointerCoords[pointerIndex].getX();
2888 const int32_t y = entry.pointerCoords[pointerIndex].getY();
2889
2890 switch (maskedAction) {
2891 case AMOTION_EVENT_ACTION_MOVE: {
2892 // Handle the special case : stylus button no longer pressed.
2893 bool isStylusButtonDown =
2894 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2895 if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) {
2896 finishDragAndDrop(entry.displayId, x, y);
2897 return;
2898 }
2899
2900 // Prevent stylus interceptor windows from affecting drag and drop behavior for now,
2901 // until we have an explicit reason to support it.
2902 constexpr bool isStylus = false;
2903
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002904 sp<WindowInfoHandle> hoverWindowHandle =
2905 findTouchedWindowAtLocked(entry.displayId, x, y, isStylus,
2906 /*ignoreDragWindow=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00002907 // enqueue drag exit if needed.
2908 if (hoverWindowHandle != mDragState->dragHoverWindowHandle &&
2909 !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) {
2910 if (mDragState->dragHoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002911 enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x,
Arthur Hung54745652022-04-20 07:17:41 +00002912 y);
2913 }
2914 mDragState->dragHoverWindowHandle = hoverWindowHandle;
2915 }
2916 // enqueue drag location if needed.
2917 if (hoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002918 enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y);
Arthur Hung54745652022-04-20 07:17:41 +00002919 }
2920 break;
2921 }
2922
2923 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002924 if (MotionEvent::getActionIndex(entry.action) != pointerIndex) {
Arthur Hung54745652022-04-20 07:17:41 +00002925 break;
2926 }
2927 // The drag pointer is up.
2928 [[fallthrough]];
2929 case AMOTION_EVENT_ACTION_UP:
2930 finishDragAndDrop(entry.displayId, x, y);
2931 break;
2932 case AMOTION_EVENT_ACTION_CANCEL: {
2933 ALOGD("Receiving cancel when drag and drop.");
2934 sendDropWindowCommandLocked(nullptr, 0, 0);
2935 mDragState.reset();
2936 break;
2937 }
arthurhungb89ccb02020-12-30 16:19:01 +08002938 }
2939}
2940
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002941std::optional<InputTarget> InputDispatcher::createInputTargetLocked(
2942 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002943 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002944 std::optional<nsecs_t> firstDownTimeInTarget) const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002945 std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken());
2946 if (connection == nullptr) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002947 ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str());
2948 return {};
2949 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002950 InputTarget inputTarget{connection};
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002951 inputTarget.windowHandle = windowHandle;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002952 inputTarget.dispatchMode = dispatchMode;
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002953 inputTarget.flags = targetFlags;
2954 inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor;
2955 inputTarget.firstDownTimeInTarget = firstDownTimeInTarget;
2956 const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId);
2957 if (displayInfoIt != mDisplayInfos.end()) {
2958 inputTarget.displayTransform = displayInfoIt->second.transform;
2959 } else {
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002960 // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId.
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002961 // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed.
2962 }
2963 return inputTarget;
2964}
2965
chaviw98318de2021-05-19 16:45:23 -05002966void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002967 InputTarget::DispatchMode dispatchMode,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002968 ftl::Flags<InputTarget::Flags> targetFlags,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002969 std::optional<nsecs_t> firstDownTimeInTarget,
Siarhei Vishniakouf75cddb2022-10-25 10:42:16 -07002970 std::vector<InputTarget>& inputTargets) const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002971 std::vector<InputTarget>::iterator it =
2972 std::find_if(inputTargets.begin(), inputTargets.end(),
2973 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002974 return inputTarget.connection->getToken() == windowHandle->getToken();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002975 });
Chavi Weingarten97b8eec2020-01-09 18:09:08 +00002976
chaviw98318de2021-05-19 16:45:23 -05002977 const WindowInfo* windowInfo = windowHandle->getInfo();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002978
2979 if (it == inputTargets.end()) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002980 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002981 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
2982 firstDownTimeInTarget);
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002983 if (!target) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002984 return;
2985 }
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002986 inputTargets.push_back(*target);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002987 it = inputTargets.end() - 1;
2988 }
2989
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07002990 if (it->flags != targetFlags) {
2991 LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it;
2992 }
2993 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
2994 LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
2995 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
2996 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002997}
2998
2999void InputDispatcher::addPointerWindowTargetLocked(
3000 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003001 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
3002 std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget,
3003 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003004 if (pointerIds.none()) {
3005 for (const auto& target : inputTargets) {
3006 LOG(INFO) << "Target: " << target;
3007 }
3008 LOG(FATAL) << "No pointers specified for " << windowHandle->getName();
3009 return;
3010 }
3011 std::vector<InputTarget>::iterator it =
3012 std::find_if(inputTargets.begin(), inputTargets.end(),
3013 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003014 return inputTarget.connection->getToken() == windowHandle->getToken();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003015 });
3016
3017 // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that
3018 // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would
3019 // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate
3020 // input targets for hovering pointers and for touching pointers.
3021 // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new
3022 // target instead.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003023 if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003024 // Force the code below to create a new input target
3025 it = inputTargets.end();
3026 }
3027
3028 const WindowInfo* windowInfo = windowHandle->getInfo();
3029
3030 if (it == inputTargets.end()) {
3031 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003032 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
3033 firstDownTimeInTarget);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003034 if (!target) {
3035 return;
3036 }
3037 inputTargets.push_back(*target);
3038 it = inputTargets.end() - 1;
3039 }
3040
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003041 if (it->dispatchMode != dispatchMode) {
3042 LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode="
3043 << ftl::enum_string(dispatchMode) << ", it=" << *it;
3044 }
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003045 if (it->flags != targetFlags) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003046 LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string()
3047 << ", it=" << *it;
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003048 }
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003049 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003050 LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003051 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
3052 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003053
Siarhei Vishniakou59967592024-04-01 16:17:46 -07003054 Result<void> result = it->addPointers(pointerIds, windowInfo->transform);
3055 if (!result.ok()) {
3056 logDispatchStateLocked();
3057 LOG(FATAL) << result.error().message();
3058 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059}
3060
Michael Wright3dd60e22019-03-27 22:06:44 +00003061void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
Linnan Li13bf76a2024-05-05 19:18:02 +08003062 ui::LogicalDisplayId displayId) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003063 auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId);
3064 if (monitorsIt == mGlobalMonitorsByDisplay.end()) return;
Michael Wright3dd60e22019-03-27 22:06:44 +00003065
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003066 for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003067 InputTarget target{monitor.connection};
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003068 // target.firstDownTimeInTarget is not set for global monitors. It is only required in split
3069 // touch and global monitoring works as intended even without setting firstDownTimeInTarget
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003070 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
3071 target.displayTransform = it->second.transform;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003072 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003073 target.setDefaultPointerTransform(target.displayTransform);
3074 inputTargets.push_back(target);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 }
3076}
3077
Robert Carrc9bf1d32020-04-13 17:21:08 -07003078/**
3079 * Indicate whether one window handle should be considered as obscuring
3080 * another window handle. We only check a few preconditions. Actually
3081 * checking the bounds is left to the caller.
3082 */
chaviw98318de2021-05-19 16:45:23 -05003083static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
3084 const sp<WindowInfoHandle>& otherHandle) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003085 // Compare by token so cloned layers aren't counted
3086 if (haveSameToken(windowHandle, otherHandle)) {
3087 return false;
3088 }
3089 auto info = windowHandle->getInfo();
3090 auto otherInfo = otherHandle->getInfo();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003091 if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003092 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003093 } else if (otherInfo->alpha == 0 &&
3094 otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) {
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003095 // Those act as if they were invisible, so we don't need to flag them.
3096 // We do want to potentially flag touchable windows even if they have 0
3097 // opacity, since they can consume touches and alter the effects of the
3098 // user interaction (eg. apps that rely on
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003099 // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003100 // windows), hence we also check for FLAG_NOT_TOUCHABLE.
3101 return false;
Bernardo Rufino8007daf2020-09-22 09:40:01 +00003102 } else if (info->ownerUid == otherInfo->ownerUid) {
3103 // If ownerUid is the same we don't generate occlusion events as there
3104 // is no security boundary within an uid.
Robert Carrc9bf1d32020-04-13 17:21:08 -07003105 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003106 } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003107 return false;
3108 } else if (otherInfo->displayId != info->displayId) {
3109 return false;
3110 }
3111 return true;
3112}
3113
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003114/**
3115 * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is
3116 * untrusted, one should check:
3117 *
3118 * 1. If result.hasBlockingOcclusion is true.
3119 * If it's, it means the touch should be blocked due to a window with occlusion mode of
3120 * BLOCK_UNTRUSTED.
3121 *
3122 * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch.
3123 * If it is (and 1 is false), then the touch should be blocked because a stack of windows
3124 * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed
3125 * obscuring opacity above the threshold. Note that if there was no window of occlusion mode
3126 * USE_OPACITY, result.obscuringOpacity would've been 0 and since
3127 * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true.
3128 *
3129 * If neither of those is true, then it means the touch can be allowed.
3130 */
3131InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
Linnan Li5e5645e2024-03-05 14:43:05 +00003132 const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
chaviw98318de2021-05-19 16:45:23 -05003133 const WindowInfo* windowInfo = windowHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003134 ui::LogicalDisplayId displayId = windowInfo->displayId;
chaviw98318de2021-05-19 16:45:23 -05003135 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003136 TouchOcclusionInfo info;
3137 info.hasBlockingOcclusion = false;
3138 info.obscuringOpacity = 0;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003139 info.obscuringUid = gui::Uid::INVALID;
3140 std::map<gui::Uid, float> opacityByUid;
chaviw98318de2021-05-19 16:45:23 -05003141 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003142 if (windowHandle == otherHandle) {
3143 break; // All future windows are below us. Exit early.
3144 }
chaviw98318de2021-05-19 16:45:23 -05003145 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li5e5645e2024-03-05 14:43:05 +00003146 if (canBeObscuredBy(windowHandle, otherHandle) &&
3147 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId)) &&
Bernardo Rufino1ff9d592021-01-18 16:58:57 +00003148 !haveSameApplicationToken(windowInfo, otherInfo)) {
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003149 if (DEBUG_TOUCH_OCCLUSION) {
3150 info.debugInfo.push_back(
Harry Cutts101ee9b2023-07-06 18:04:14 +00003151 dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003152 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003153 // canBeObscuredBy() has returned true above, which means this window is untrusted, so
3154 // we perform the checks below to see if the touch can be propagated or not based on the
3155 // window's touch occlusion mode
3156 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) {
3157 info.hasBlockingOcclusion = true;
3158 info.obscuringUid = otherInfo->ownerUid;
3159 info.obscuringPackage = otherInfo->packageName;
3160 break;
3161 }
3162 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003163 const auto uid = otherInfo->ownerUid;
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003164 float opacity =
3165 (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid];
3166 // Given windows A and B:
3167 // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)]
3168 opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha);
3169 opacityByUid[uid] = opacity;
3170 if (opacity > info.obscuringOpacity) {
3171 info.obscuringOpacity = opacity;
3172 info.obscuringUid = uid;
3173 info.obscuringPackage = otherInfo->packageName;
3174 }
3175 }
3176 }
3177 }
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003178 if (DEBUG_TOUCH_OCCLUSION) {
Harry Cutts101ee9b2023-07-06 18:04:14 +00003179 info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003180 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003181 return info;
3182}
3183
chaviw98318de2021-05-19 16:45:23 -05003184std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003185 bool isTouchedWindow) const {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003186 return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003187 "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3188 "], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3189 "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003190 isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003191 info->ownerUid.toString().c_str(), info->id,
Chavi Weingarten7f019192023-08-08 20:39:01 +00003192 toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3193 info->frame.top, info->frame.right, info->frame.bottom,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003194 dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3195 info->inputConfig.string().c_str(), toString(info->token != nullptr),
3196 info->applicationInfo.name.c_str(),
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003197 binderToString(info->applicationInfo.token).c_str());
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003198}
3199
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003200bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3201 if (occlusionInfo.hasBlockingOcclusion) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003202 ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
3203 occlusionInfo.obscuringUid.toString().c_str());
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003204 return false;
3205 }
3206 if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003207 ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = "
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003208 "%.2f, maximum allowed = %.2f)",
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003209 occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(),
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003210 occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch);
3211 return false;
3212 }
3213 return true;
3214}
3215
chaviw98318de2021-05-19 16:45:23 -05003216bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle,
Linnan Li5e5645e2024-03-05 14:43:05 +00003217 float x, float y) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003218 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003219 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3220 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003221 if (windowHandle == otherHandle) {
3222 break; // All future windows are below us. Exit early.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 }
chaviw98318de2021-05-19 16:45:23 -05003224 const WindowInfo* otherInfo = otherHandle->getInfo();
Robert Carrc9bf1d32020-04-13 17:21:08 -07003225 if (canBeObscuredBy(windowHandle, otherHandle) &&
Linnan Li5e5645e2024-03-05 14:43:05 +00003226 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 return true;
3228 }
3229 }
3230 return false;
3231}
3232
chaviw98318de2021-05-19 16:45:23 -05003233bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003234 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003235 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3236 const WindowInfo* windowInfo = windowHandle->getInfo();
3237 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003238 if (windowHandle == otherHandle) {
3239 break; // All future windows are below us. Exit early.
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003240 }
chaviw98318de2021-05-19 16:45:23 -05003241 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003242 if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->overlaps(windowInfo)) {
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003243 return true;
3244 }
3245 }
3246 return false;
3247}
3248
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08003249std::string InputDispatcher::getApplicationWindowLabel(
chaviw98318de2021-05-19 16:45:23 -05003250 const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) {
Yi Kong9b14ac62018-07-17 13:48:38 -07003251 if (applicationHandle != nullptr) {
3252 if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003253 return applicationHandle->getName() + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 } else {
3255 return applicationHandle->getName();
3256 }
Yi Kong9b14ac62018-07-17 13:48:38 -07003257 } else if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003258 return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08003260 return "<unknown application or window>";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 }
3262}
3263
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003264void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00003265 if (!isUserActivityEvent(eventEntry)) {
3266 // Not poking user activity if the event type does not represent a user activity
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003267 return;
3268 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003269
3270 const int32_t eventType = getUserActivityEventType(eventEntry);
3271 if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) {
3272 // Note that we're directly getting the time diff between the current event and the previous
3273 // event. This is assuming that the first user event always happens at a timestamp that is
3274 // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will
3275 // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller
3276 // value than the potential first user activity event time, so this is ok.
3277 std::chrono::nanoseconds timeSinceLastEvent =
3278 std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]);
3279 if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) {
3280 return;
3281 }
3282 }
3283
Linnan Li13bf76a2024-05-05 19:18:02 +08003284 ui::LogicalDisplayId displayId = getTargetDisplayId(eventEntry);
chaviw98318de2021-05-19 16:45:23 -05003285 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Josep del Riob3981622023-04-18 15:49:45 +00003286 const WindowInfo* windowDisablingUserActivityInfo = nullptr;
Tiger Huang721e26f2018-07-24 22:26:19 +08003287 if (focusedWindowHandle != nullptr) {
chaviw98318de2021-05-19 16:45:23 -05003288 const WindowInfo* info = focusedWindowHandle->getInfo();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003289 if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) {
Josep del Riob3981622023-04-18 15:49:45 +00003290 windowDisablingUserActivityInfo = info;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 }
3292 }
3293
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003294 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003295 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003296 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3297 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003298 return;
3299 }
Josep del Riob3981622023-04-18 15:49:45 +00003300 if (windowDisablingUserActivityInfo != nullptr) {
3301 if (DEBUG_DISPATCH_CYCLE) {
3302 ALOGD("Not poking user activity: disabled by window '%s'.",
3303 windowDisablingUserActivityInfo->name.c_str());
3304 }
3305 return;
3306 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003307 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003309 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003310 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3311 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003312 return;
3313 }
Josep del Riob3981622023-04-18 15:49:45 +00003314 // Don't inhibit events that were intercepted or are not passed to
3315 // the apps, like system shortcuts
3316 if (windowDisablingUserActivityInfo != nullptr &&
Josep del Rioc8fdedb2024-05-21 13:32:43 +00003317 keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP) {
Josep del Riob3981622023-04-18 15:49:45 +00003318 if (DEBUG_DISPATCH_CYCLE) {
3319 ALOGD("Not poking user activity: disabled by window '%s'.",
3320 windowDisablingUserActivityInfo->name.c_str());
3321 }
3322 return;
3323 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003324 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00003326 default: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003327 LOG_ALWAYS_FATAL("%s events are not user activity",
Dominik Laskowski75788452021-02-09 18:51:25 -08003328 ftl::enum_string(eventEntry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003329 break;
3330 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 }
3332
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003333 mLastUserActivityTimes[eventType] = eventEntry.eventTime;
Prabir Pradhancef936d2021-07-21 16:17:52 +00003334 auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]()
3335 REQUIRES(mLock) {
3336 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003337 mPolicy.pokeUserActivity(eventTime, eventType, displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003338 };
3339 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340}
3341
3342void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003343 const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003344 std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003345 const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003346 ATRACE_NAME_IF(ATRACE_ENABLED(),
3347 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
3348 connection->getInputChannelName().c_str(), eventEntry->id));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003349 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003350 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, "
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08003351 "globalScaleFactor=%f, pointerIds=%s %s",
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003352 connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(),
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003353 inputTarget.globalScaleFactor, bitsetToString(inputTarget.getPointerIds()).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003354 inputTarget.getPointerInfoString().c_str());
3355 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356
3357 // Skip this event if the connection status is not normal.
3358 // We don't want to enqueue additional outbound events if the connection is broken.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003359 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003360 if (DEBUG_DISPATCH_CYCLE) {
3361 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003362 connection->getInputChannelName().c_str(),
3363 ftl::enum_string(connection->status).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003364 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365 return;
3366 }
3367
3368 // Split a motion event if needed.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003369 if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) {
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003370 LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003371 "Entry type %s should not have Flags::SPLIT",
Dominik Laskowski75788452021-02-09 18:51:25 -08003372 ftl::enum_string(eventEntry->type).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003374 const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry);
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003375 if (inputTarget.getPointerIds().count() != originalMotionEntry.getPointerCount()) {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -08003376 if (!inputTarget.firstDownTimeInTarget.has_value()) {
3377 logDispatchStateLocked();
3378 LOG(FATAL) << "Splitting motion events requires a down time to be set for the "
3379 "target on connection "
3380 << connection->getInputChannelName() << " for "
3381 << originalMotionEntry.getDescription();
3382 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003383 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003384 splitMotionEvent(originalMotionEntry, inputTarget.getPointerIds(),
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003385 inputTarget.firstDownTimeInTarget.value());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 if (!splitMotionEntry) {
3387 return; // split event was dropped
3388 }
Arthur Hungb3307ee2021-10-14 10:57:37 +00003389 if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
3390 std::string reason = std::string("reason=pointer cancel on split window");
3391 android_log_event_list(LOGTAG_INPUT_CANCEL)
3392 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
3393 }
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003394 if (DEBUG_FOCUS) {
3395 ALOGD("channel '%s' ~ Split motion event.",
3396 connection->getInputChannelName().c_str());
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003397 logOutboundMotionDetails(" ", *splitMotionEntry);
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003398 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003399 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection,
3400 std::move(splitMotionEntry),
3401 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 return;
3403 }
3404 }
3405
3406 // Not splitting. Enqueue dispatch entries for the event as is.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003407 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry,
3408 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409}
3410
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003411void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked(
3412 nsecs_t currentTime, const std::shared_ptr<Connection>& connection,
3413 std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003414 ATRACE_NAME_IF(ATRACE_ENABLED(),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003415 StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, "
3416 "id=0x%" PRIx32 ")",
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003417 connection->getInputChannelName().c_str(), eventEntry->id));
Michael Wright3dd60e22019-03-27 22:06:44 +00003418
hongzuo liu95785e22022-09-06 02:51:35 +00003419 const bool wasEmpty = connection->outboundQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003421 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
3423 // If the outbound queue was previously empty, start the dispatch cycle going.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003424 if (wasEmpty && !connection->outboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 startDispatchCycleLocked(currentTime, connection);
3426 }
3427}
3428
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003429void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003430 std::shared_ptr<const EventEntry> eventEntry,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003431 const InputTarget& inputTarget) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00003432 const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY ||
3433 eventEntry->type == EventEntry::Type::MOTION;
3434 if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) {
3435 LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: "
3436 << inputTarget << " connection: " << connection->getInputChannelName()
3437 << " entry: " << eventEntry->getDescription();
3438 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 // This is a new event.
3440 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003441 std::unique_ptr<DispatchEntry> dispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003442 createDispatchEntry(mIdGenerator, inputTarget, eventEntry, inputTarget.flags,
Prabir Pradhana67623c2024-02-21 06:57:36 +00003443 mWindowInfosVsyncId, mTracer.get());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003445 // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a
3446 // different EventEntry than what was passed in.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003447 eventEntry = dispatchEntry->eventEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 // Apply target flags and update the connection's input state.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003449 switch (eventEntry->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003450 case EventEntry::Type::KEY: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003451 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry);
3452 if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003453 LOG(WARNING) << "channel " << connection->getInputChannelName()
3454 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003455 return; // skip the inconsistent event
3456 }
3457 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003460 case EventEntry::Type::MOTION: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003461 std::shared_ptr<const MotionEntry> resolvedMotion =
3462 std::static_pointer_cast<const MotionEntry>(eventEntry);
3463 {
3464 // Determine the resolved motion entry.
3465 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
3466 int32_t resolvedAction = motionEntry.action;
3467 int32_t resolvedFlags = motionEntry.flags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003469 if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003470 resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003471 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003472 resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003473 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003474 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003475 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003476 resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003477 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003478 resolvedAction = AMOTION_EVENT_ACTION_DOWN;
3479 }
3480 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE &&
3481 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source,
3482 motionEntry.displayId)) {
3483 if (DEBUG_DISPATCH_CYCLE) {
3484 LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str()
3485 << "' ~ enqueueDispatchEntryLocked: filling in missing hover "
3486 "enter event";
3487 }
3488 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
3489 }
3490
3491 if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3492 resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED;
3493 }
3494 if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) {
3495 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
3496 }
3497 if (dispatchEntry->targetFlags.test(
3498 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) {
3499 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
3500 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003501 if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) {
3502 resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
3503 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003504
3505 dispatchEntry->resolvedFlags = resolvedFlags;
3506 if (resolvedAction != motionEntry.action) {
Siarhei Vishniakoue9ef6bc2023-12-21 19:47:20 -08003507 std::optional<std::vector<PointerProperties>> usingProperties;
3508 std::optional<std::vector<PointerCoords>> usingCoords;
3509 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT ||
3510 resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3511 // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by
3512 // the dispatcher, and therefore the coordinates of this event are currently
3513 // incorrect. These events should use the coordinates of the last dispatched
3514 // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data.
3515 const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT;
3516 std::optional<std::pair<std::vector<PointerProperties>,
3517 std::vector<PointerCoords>>>
3518 pointerInfo =
3519 connection->inputState.getPointersOfLastEvent(motionEntry,
3520 hovering);
3521 if (pointerInfo) {
3522 usingProperties = pointerInfo->first;
3523 usingCoords = pointerInfo->second;
3524 }
3525 }
Prabir Pradhana67623c2024-02-21 06:57:36 +00003526 {
3527 // Generate a new MotionEntry with a new eventId using the resolved action
3528 // and flags, and set it as the resolved entry.
3529 auto newEntry = std::make_shared<
3530 MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState,
3531 motionEntry.eventTime, motionEntry.deviceId,
3532 motionEntry.source, motionEntry.displayId,
3533 motionEntry.policyFlags, resolvedAction,
3534 motionEntry.actionButton, resolvedFlags,
3535 motionEntry.metaState, motionEntry.buttonState,
3536 motionEntry.classification, motionEntry.edgeFlags,
3537 motionEntry.xPrecision, motionEntry.yPrecision,
3538 motionEntry.xCursorPosition,
3539 motionEntry.yCursorPosition, motionEntry.downTime,
3540 usingProperties.value_or(
3541 motionEntry.pointerProperties),
3542 usingCoords.value_or(motionEntry.pointerCoords));
3543 if (mTracer) {
3544 ensureEventTraced(motionEntry);
3545 newEntry->traceTracker =
3546 mTracer->traceDerivedEvent(*newEntry,
3547 *motionEntry.traceTracker);
3548 }
3549 resolvedMotion = newEntry;
3550 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003551 if (ATRACE_ENABLED()) {
3552 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32
3553 ") to MotionEvent(id=0x%" PRIx32 ").",
3554 motionEntry.id, resolvedMotion->id);
3555 ATRACE_NAME(message.c_str());
3556 }
3557
3558 // Set the resolved motion entry in the DispatchEntry.
3559 dispatchEntry->eventEntry = resolvedMotion;
3560 eventEntry = resolvedMotion;
3561 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003562 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003564 // Check if we need to cancel any of the ongoing gestures. We don't support multiple
3565 // devices being active at the same time in the same window, so if a new device is
3566 // active, cancel the gesture from the old device.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003567 std::unique_ptr<EventEntry> cancelEvent =
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003568 connection->inputState.cancelConflictingInputStream(*resolvedMotion);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003569 if (cancelEvent != nullptr) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003570 LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in "
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003571 << connection->getInputChannelName() << " with event "
3572 << cancelEvent->getDescription();
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003573 if (mTracer) {
3574 static_cast<MotionEntry&>(*cancelEvent).traceTracker =
3575 mTracer->traceDerivedEvent(*cancelEvent, *resolvedMotion->traceTracker);
3576 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003577 std::unique_ptr<DispatchEntry> cancelDispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003578 createDispatchEntry(mIdGenerator, inputTarget, std::move(cancelEvent),
Prabir Pradhana67623c2024-02-21 06:57:36 +00003579 ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId,
3580 mTracer.get());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003581
3582 // Send these cancel events to the queue before sending the event from the new
3583 // device.
3584 connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry));
3585 }
3586
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003587 if (!connection->inputState.trackMotion(*resolvedMotion,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003588 dispatchEntry->resolvedFlags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003589 LOG(WARNING) << "channel " << connection->getInputChannelName()
3590 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003591 return; // skip the inconsistent event
3592 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003593 if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) &&
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003594 (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003595 // Skip reporting pointer down outside focus to the policy.
3596 break;
3597 }
3598
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003599 dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003600 inputTarget.connection->getToken());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003601
3602 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003604 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003605 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08003606 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
3607 case EventEntry::Type::DRAG: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003608 break;
3609 }
Chris Yef59a2f42020-10-16 12:55:26 -07003610 case EventEntry::Type::SENSOR: {
3611 LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel");
3612 break;
3613 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003614 case EventEntry::Type::DEVICE_RESET: {
3615 LOG_ALWAYS_FATAL("%s events should not go to apps",
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003616 ftl::enum_string(eventEntry->type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003617 break;
3618 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619 }
3620
3621 // Remember that we are waiting for this dispatch to complete.
3622 if (dispatchEntry->hasForegroundTarget()) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003623 incrementPendingForegroundDispatches(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 }
3625
3626 // Enqueue the dispatch entry.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003627 connection->outboundQueue.emplace_back(std::move(dispatchEntry));
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003628 traceOutboundQueueLength(*connection);
chaviw8c9cf542019-03-25 13:02:48 -07003629}
3630
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003631/**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003632 * This function is for debugging and metrics collection. It has two roles.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003633 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003634 * The first role is to log input interaction with windows, which helps determine what the user was
3635 * interacting with. For example, if user is touching launcher, we will see an input_interaction log
3636 * that user started interacting with launcher window, as well as any other window that received
3637 * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged
3638 * when the set of tokens that received the event changes. It is not logged again as long as the
3639 * user is interacting with the same windows.
3640 *
3641 * The second role is to track input device activity for metrics collection. For each input event,
3642 * we report the set of UIDs that the input device interacted with to the policy. Unlike for the
3643 * input_interaction logs, the device interaction is reported even when the set of interaction
3644 * tokens do not change.
3645 *
3646 * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as
3647 * interaction. This includes up and cancel events for both keys and motions.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003648 */
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003649void InputDispatcher::processInteractionsLocked(const EventEntry& entry,
3650 const std::vector<InputTarget>& targets) {
3651 int32_t deviceId;
3652 nsecs_t eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003653 // Skip ACTION_UP events, and all events other than keys and motions
3654 if (entry.type == EventEntry::Type::KEY) {
3655 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
3656 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
3657 return;
3658 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003659 deviceId = keyEntry.deviceId;
3660 eventTime = keyEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003661 } else if (entry.type == EventEntry::Type::MOTION) {
3662 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
3663 if (motionEntry.action == AMOTION_EVENT_ACTION_UP ||
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003664 motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
3665 MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003666 return;
3667 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003668 deviceId = motionEntry.deviceId;
3669 eventTime = motionEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003670 } else {
3671 return; // Not a key or a motion
3672 }
3673
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003674 std::set<gui::Uid> interactionUids;
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07003675 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003676 std::vector<std::shared_ptr<Connection>> newConnections;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003677 for (const InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003678 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003679 continue; // Skip windows that receive ACTION_OUTSIDE
3680 }
3681
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003682 sp<IBinder> token = target.connection->getToken();
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003683 newConnectionTokens.insert(std::move(token));
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003684 newConnections.emplace_back(target.connection);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003685 if (target.windowHandle) {
3686 interactionUids.emplace(target.windowHandle->getInfo()->ownerUid);
3687 }
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003688 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003689
3690 auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]()
3691 REQUIRES(mLock) {
3692 scoped_unlock unlock(mLock);
3693 mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids);
3694 };
3695 postCommandLocked(std::move(command));
3696
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003697 if (newConnectionTokens == mInteractionConnectionTokens) {
3698 return; // no change
3699 }
3700 mInteractionConnectionTokens = newConnectionTokens;
3701
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003702 std::string targetList;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003703 for (const std::shared_ptr<Connection>& connection : newConnections) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08003704 targetList += connection->getInputChannelName() + ", ";
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003705 }
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003706 std::string message = "Interaction with: " + targetList;
3707 if (targetList.empty()) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003708 message += "<none>";
3709 }
3710 android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS;
3711}
3712
chaviwfd6d3512019-03-25 13:23:49 -07003713void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Vishnu Nairad321cd2020-08-20 16:40:21 -07003714 const sp<IBinder>& token) {
chaviw8c9cf542019-03-25 13:02:48 -07003715 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
chaviwfd6d3512019-03-25 13:23:49 -07003716 uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK;
3717 if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) {
chaviw8c9cf542019-03-25 13:02:48 -07003718 return;
3719 }
3720
Vishnu Nairc519ff72021-01-21 08:23:08 -08003721 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07003722 if (focusedToken == token) {
3723 // ignore since token is focused
chaviw8c9cf542019-03-25 13:02:48 -07003724 return;
3725 }
3726
Prabir Pradhancef936d2021-07-21 16:17:52 +00003727 auto command = [this, token]() REQUIRES(mLock) {
3728 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003729 mPolicy.onPointerDownOutsideFocus(token);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003730 };
3731 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732}
3733
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003734status_t InputDispatcher::publishMotionEvent(Connection& connection,
3735 DispatchEntry& dispatchEntry) const {
3736 const EventEntry& eventEntry = *(dispatchEntry.eventEntry);
3737 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3738
3739 PointerCoords scaledCoords[MAX_POINTERS];
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003740 const PointerCoords* usingCoords = motionEntry.pointerCoords.data();
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003741
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003742 // TODO(b/316355518): Do not modify coords before dispatch.
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003743 // Set the X and Y offset and X and Y scale depending on the input source.
3744 if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) &&
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003745 !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003746 float globalScaleFactor = dispatchEntry.globalScaleFactor;
3747 if (globalScaleFactor != 1.0f) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003748 for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003749 scaledCoords[i] = motionEntry.pointerCoords[i];
3750 // Don't apply window scale here since we don't want scale to affect raw
3751 // coordinates. The scale will be sent back to the client and applied
3752 // later when requesting relative coordinates.
Harry Cutts33476232023-01-30 19:57:29 +00003753 scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003754 }
3755 usingCoords = scaledCoords;
3756 }
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003757 }
3758
3759 std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry);
3760
3761 // Publish the motion event.
3762 return connection.inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003763 .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId,
3764 motionEntry.source, motionEntry.displayId, std::move(hmac),
3765 motionEntry.action, motionEntry.actionButton,
3766 dispatchEntry.resolvedFlags, motionEntry.edgeFlags,
3767 motionEntry.metaState, motionEntry.buttonState,
3768 motionEntry.classification, dispatchEntry.transform,
3769 motionEntry.xPrecision, motionEntry.yPrecision,
3770 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
3771 dispatchEntry.rawTransform, motionEntry.downTime,
3772 motionEntry.eventTime, motionEntry.getPointerCount(),
3773 motionEntry.pointerProperties.data(), usingCoords);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003774}
3775
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003777 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003778 ATRACE_NAME_IF(ATRACE_ENABLED(),
3779 StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
3780 connection->getInputChannelName().c_str()));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003781 if (DEBUG_DISPATCH_CYCLE) {
3782 ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str());
3783 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003785 while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003786 std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787 dispatchEntry->deliveryTime = currentTime;
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003788 const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection);
Siarhei Vishniakou70622952020-07-30 11:17:23 -05003789 dispatchEntry->timeoutTime = currentTime + timeout.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 // Publish the event.
3792 status_t status;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003793 const EventEntry& eventEntry = *(dispatchEntry->eventEntry);
3794 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003795 case EventEntry::Type::KEY: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003796 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3797 std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry);
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003798 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003799 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3800 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003801 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003803 // Publish the key event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003804 status = connection->inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003805 .publishKeyEvent(dispatchEntry->seq, keyEntry.id,
3806 keyEntry.deviceId, keyEntry.source,
3807 keyEntry.displayId, std::move(hmac),
3808 keyEntry.action, dispatchEntry->resolvedFlags,
3809 keyEntry.keyCode, keyEntry.scanCode,
3810 keyEntry.metaState, keyEntry.repeatCount,
3811 keyEntry.downTime, keyEntry.eventTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003812 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003813 ensureEventTraced(keyEntry);
3814 mTracer->traceEventDispatch(*dispatchEntry, *keyEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003815 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003816 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 }
3818
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003819 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003820 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003821 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3822 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003823 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003824 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003825 status = publishMotionEvent(*connection, *dispatchEntry);
Siarhei Vishniakoub0058742024-07-01 16:42:49 -07003826 if (status == BAD_VALUE) {
3827 logDispatchStateLocked();
3828 LOG(FATAL) << "Publisher failed for " << motionEntry;
3829 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003830 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003831 ensureEventTraced(motionEntry);
3832 mTracer->traceEventDispatch(*dispatchEntry, *motionEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003833 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003834 break;
3835 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003836
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003837 case EventEntry::Type::FOCUS: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003838 const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003839 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003840 focusEntry.id,
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07003841 focusEntry.hasFocus);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003842 break;
3843 }
3844
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003845 case EventEntry::Type::TOUCH_MODE_CHANGED: {
3846 const TouchModeEntry& touchModeEntry =
3847 static_cast<const TouchModeEntry&>(eventEntry);
3848 status = connection->inputPublisher
3849 .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id,
3850 touchModeEntry.inTouchMode);
3851
3852 break;
3853 }
3854
Prabir Pradhan99987712020-11-10 18:43:05 -08003855 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
3856 const auto& captureEntry =
3857 static_cast<const PointerCaptureChangedEntry&>(eventEntry);
Hiroki Sato25040232024-02-22 17:21:22 +09003858 status =
3859 connection->inputPublisher
3860 .publishCaptureEvent(dispatchEntry->seq, captureEntry.id,
3861 captureEntry.pointerCaptureRequest.isEnable());
Prabir Pradhan99987712020-11-10 18:43:05 -08003862 break;
3863 }
3864
arthurhungb89ccb02020-12-30 16:19:01 +08003865 case EventEntry::Type::DRAG: {
3866 const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry);
3867 status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq,
3868 dragEntry.id, dragEntry.x,
3869 dragEntry.y,
3870 dragEntry.isExiting);
3871 break;
3872 }
3873
Chris Yef59a2f42020-10-16 12:55:26 -07003874 case EventEntry::Type::DEVICE_RESET:
3875 case EventEntry::Type::SENSOR: {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003876 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events",
Dominik Laskowski75788452021-02-09 18:51:25 -08003877 ftl::enum_string(eventEntry.type).c_str());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003878 return;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003879 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 }
3881
3882 // Check the result.
3883 if (status) {
3884 if (status == WOULD_BLOCK) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003885 if (connection->waitQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 ALOGE("channel '%s' ~ Could not publish event because the pipe is full. "
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003887 "This is unexpected because the wait queue is empty, so the pipe "
3888 "should be empty and we shouldn't have any problems writing an "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003889 "event to it, status=%s(%d)",
3890 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3891 status);
Harry Cutts33476232023-01-30 19:57:29 +00003892 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 } else {
3894 // Pipe is full and we are waiting for the app to finish process some events
3895 // before sending more events to it.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003896 if (DEBUG_DISPATCH_CYCLE) {
3897 ALOGD("channel '%s' ~ Could not publish event because the pipe is full, "
3898 "waiting for the application to catch up",
3899 connection->getInputChannelName().c_str());
3900 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 }
3902 } else {
3903 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003904 "status=%s(%d)",
3905 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3906 status);
Harry Cutts33476232023-01-30 19:57:29 +00003907 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908 }
3909 return;
3910 }
3911
3912 // Re-enqueue the event on the wait queue.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003913 const nsecs_t timeoutTime = dispatchEntry->timeoutTime;
3914 connection->waitQueue.emplace_back(std::move(dispatchEntry));
3915 connection->outboundQueue.erase(connection->outboundQueue.begin());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003916 traceOutboundQueueLength(*connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003917 if (connection->responsive) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003918 mAnrTracker.insert(timeoutTime, connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003919 }
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003920 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921 }
3922}
3923
chaviw09c8d2d2020-08-24 15:48:26 -07003924std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const {
3925 size_t size;
3926 switch (event.type) {
3927 case VerifiedInputEvent::Type::KEY: {
3928 size = sizeof(VerifiedKeyEvent);
3929 break;
3930 }
3931 case VerifiedInputEvent::Type::MOTION: {
3932 size = sizeof(VerifiedMotionEvent);
3933 break;
3934 }
3935 }
3936 const uint8_t* start = reinterpret_cast<const uint8_t*>(&event);
3937 return mHmacKeyManager.sign(start, size);
3938}
3939
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003940const std::array<uint8_t, 32> InputDispatcher::getSignature(
3941 const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003942 const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003943 if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) {
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003944 // Only sign events up and down events as the purely move events
3945 // are tied to their up/down counterparts so signing would be redundant.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003946 return INVALID_HMAC;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003947 }
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003948
3949 VerifiedMotionEvent verifiedEvent =
3950 verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform);
3951 verifiedEvent.actionMasked = actionMasked;
3952 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS;
3953 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003954}
3955
3956const std::array<uint8_t, 32> InputDispatcher::getSignature(
3957 const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const {
3958 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry);
3959 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS;
chaviw09c8d2d2020-08-24 15:48:26 -07003960 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003961}
3962
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003964 const std::shared_ptr<Connection>& connection,
3965 uint32_t seq, bool handled, nsecs_t consumeTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003966 if (DEBUG_DISPATCH_CYCLE) {
3967 ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s",
3968 connection->getInputChannelName().c_str(), seq, toString(handled));
3969 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00003971 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972 return;
3973 }
3974
3975 // Notify other system components and prepare to start the next dispatch cycle.
Prabir Pradhancef936d2021-07-21 16:17:52 +00003976 auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) {
3977 doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime);
3978 };
3979 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980}
3981
3982void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003983 const std::shared_ptr<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003984 bool notify) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003985 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003986 LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__
3987 << " - notify=" << toString(notify);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003988 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989
3990 // Clear the dispatch queues.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003991 drainDispatchQueue(connection->outboundQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003992 traceOutboundQueueLength(*connection);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003993 drainDispatchQueue(connection->waitQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003994 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995
3996 // The connection appears to be unrecoverably broken.
3997 // Ignore already broken or zombie connections.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003998 if (connection->status == Connection::Status::NORMAL) {
3999 connection->status = Connection::Status::BROKEN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000
4001 if (notify) {
4002 // Notify other system components.
Prabir Pradhancef936d2021-07-21 16:17:52 +00004003 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
4004 connection->getInputChannelName().c_str());
4005
4006 auto command = [this, connection]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004007 scoped_unlock unlock(mLock);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004008 mPolicy.notifyInputChannelBroken(connection->getToken());
Prabir Pradhancef936d2021-07-21 16:17:52 +00004009 };
4010 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 }
4012 }
4013}
4014
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004015void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004016 while (!queue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004017 releaseDispatchEntry(std::move(queue.front()));
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004018 queue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020}
4021
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004022void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 if (dispatchEntry->hasForegroundTarget()) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004024 decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026}
4027
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004028int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) {
4029 std::scoped_lock _l(mLock);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004030 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004031 if (connection == nullptr) {
4032 ALOGW("Received looper callback for unknown input channel token %p. events=0x%x",
4033 connectionToken.get(), events);
4034 return 0; // remove the callback
4035 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004037 bool notify;
4038 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
4039 if (!(events & ALOOPER_EVENT_INPUT)) {
4040 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
4041 "events=0x%x",
4042 connection->getInputChannelName().c_str(), events);
4043 return 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 }
4045
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004046 nsecs_t currentTime = now();
4047 bool gotOne = false;
4048 status_t status = OK;
4049 for (;;) {
4050 Result<InputPublisher::ConsumerResponse> result =
4051 connection->inputPublisher.receiveConsumerResponse();
4052 if (!result.ok()) {
4053 status = result.error().code();
4054 break;
4055 }
4056
4057 if (std::holds_alternative<InputPublisher::Finished>(*result)) {
4058 const InputPublisher::Finished& finish =
4059 std::get<InputPublisher::Finished>(*result);
4060 finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled,
4061 finish.consumeTime);
4062 } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004063 if (shouldReportMetricsForConnection(*connection)) {
4064 const InputPublisher::Timeline& timeline =
4065 std::get<InputPublisher::Timeline>(*result);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004066 mLatencyTracker.trackGraphicsLatency(timeline.inputEventId,
4067 connection->getToken(),
4068 std::move(timeline.graphicsTimeline));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004069 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004070 }
4071 gotOne = true;
4072 }
4073 if (gotOne) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004074 runCommandsLockedInterruptable();
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004075 if (status == WOULD_BLOCK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 return 1;
4077 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 }
4079
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004080 notify = status != DEAD_OBJECT || !connection->monitor;
4081 if (notify) {
4082 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)",
4083 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
4084 status);
4085 }
4086 } else {
4087 // Monitor channels are never explicitly unregistered.
4088 // We do it automatically when the remote endpoint is closed so don't warn about them.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004089 const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004090 notify = !connection->monitor && stillHaveWindowHandle;
4091 if (notify) {
4092 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x",
4093 connection->getInputChannelName().c_str(), events);
4094 }
4095 }
4096
4097 // Remove the channel.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004098 removeInputChannelLocked(connection->getToken(), notify);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004099 return 0; // remove the callback
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100}
4101
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004102void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 const CancelationOptions& options) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004104 // Cancel windows (i.e. non-monitors).
4105 // A channel must have at least one window to receive any input. If a window was removed, the
4106 // event streams directed to the window will already have been canceled during window removal.
4107 // So there is no need to generate cancellations for connections without any windows.
4108 const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode);
4109 // Generate cancellations for touched windows first. This is to avoid generating cancellations
4110 // through a non-touched window if there are more than one window for an input channel.
4111 if (cancelPointers) {
4112 for (const auto& [displayId, touchState] : mTouchStatesByDisplay) {
4113 if (options.displayId.has_value() && options.displayId != displayId) {
4114 continue;
4115 }
4116 for (const auto& touchedWindow : touchState.windows) {
4117 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
4118 }
4119 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 }
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004121 // Follow up by generating cancellations for all windows, because we don't explicitly track
4122 // the windows that have an ongoing focus event stream.
4123 if (cancelNonPointers) {
4124 for (const auto& [_, handles] : mWindowHandlesByDisplay) {
4125 for (const auto& windowHandle : handles) {
4126 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4127 }
4128 }
4129 }
4130
4131 // Cancel monitors.
4132 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133}
4134
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004135void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked(
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004136 const CancelationOptions& options) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004137 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00004138 for (const Monitor& monitor : monitors) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004139 synthesizeCancelationEventsForConnectionLocked(monitor.connection, options,
4140 /*window=*/nullptr);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004141 }
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004142 }
4143}
4144
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004145void InputDispatcher::synthesizeCancelationEventsForWindowLocked(
4146 const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options,
4147 const std::shared_ptr<Connection>& connection) {
4148 if (windowHandle == nullptr) {
4149 LOG(FATAL) << __func__ << ": Window handle must not be null";
4150 }
4151 if (connection) {
4152 // The connection can be optionally provided to avoid multiple lookups.
4153 if (windowHandle->getToken() != connection->getToken()) {
4154 LOG(FATAL) << __func__
4155 << ": Wrong connection provided for window: " << windowHandle->getName();
4156 }
4157 }
4158
4159 std::shared_ptr<Connection> resolvedConnection =
4160 connection ? connection : getConnectionLocked(windowHandle->getToken());
4161 if (!resolvedConnection) {
4162 LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName();
4163 return;
4164 }
4165 synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle);
4166}
4167
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004169 const std::shared_ptr<Connection>& connection, const CancelationOptions& options,
4170 const sp<WindowInfoHandle>& window) {
4171 if (!connection->monitor && window == nullptr) {
4172 LOG(FATAL) << __func__
4173 << ": Cannot send event to non-monitor channel without a window - channel: "
4174 << connection->getInputChannelName();
4175 }
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00004176 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 return;
4178 }
4179
4180 nsecs_t currentTime = now();
4181
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004182 std::vector<std::unique_ptr<EventEntry>> cancelationEvents =
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -07004183 connection->inputState.synthesizeCancelationEvents(currentTime, options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004185 if (cancelationEvents.empty()) {
4186 return;
4187 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004188
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004189 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
4190 ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync "
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004191 "with reality: %s, mode=%s.",
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004192 connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004193 ftl::enum_string(options.mode).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004194 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004195
Arthur Hungb3307ee2021-10-14 10:57:37 +00004196 std::string reason = std::string("reason=").append(options.reason);
4197 android_log_event_list(LOGTAG_INPUT_CANCEL)
4198 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
4199
hongzuo liu95785e22022-09-06 02:51:35 +00004200 const bool wasEmpty = connection->outboundQueue.empty();
Prabir Pradhan16463382023-10-12 23:03:19 +00004201 // The target to use if we don't find a window associated with the channel.
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004202 const InputTarget fallbackTarget{connection};
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004203 const auto& token = connection->getToken();
hongzuo liu95785e22022-09-06 02:51:35 +00004204
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004205 for (size_t i = 0; i < cancelationEvents.size(); i++) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004206 std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004207 std::vector<InputTarget> targets{};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004208
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004209 switch (cancelationEventEntry->type) {
4210 case EventEntry::Type::KEY: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004211 if (mTracer) {
4212 static_cast<KeyEntry&>(*cancelationEventEntry).traceTracker =
4213 mTracer->traceDerivedEvent(*cancelationEventEntry,
4214 *options.traceTracker);
4215 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004216 const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004217 if (window) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004218 addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4219 /*targetFlags=*/{}, keyEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004220 } else {
4221 targets.emplace_back(fallbackTarget);
4222 }
4223 logOutboundKeyDetails("cancel - ", keyEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004224 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004226 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004227 if (mTracer) {
4228 static_cast<MotionEntry&>(*cancelationEventEntry).traceTracker =
4229 mTracer->traceDerivedEvent(*cancelationEventEntry,
4230 *options.traceTracker);
4231 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004232 const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004233 if (window) {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004234 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004235 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004236 pointerIndex++) {
4237 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4238 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004239 if (mDragState && mDragState->dragWindow->getToken() == token &&
4240 pointerIds.test(mDragState->pointerId)) {
4241 LOG(INFO) << __func__
4242 << ": Canceling drag and drop because the pointers for the drag "
4243 "window are being canceled.";
4244 sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0);
4245 mDragState.reset();
4246 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004247 addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4248 ftl::Flags<InputTarget::Flags>(), pointerIds,
4249 motionEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004250 } else {
4251 targets.emplace_back(fallbackTarget);
4252 const auto it = mDisplayInfos.find(motionEntry.displayId);
4253 if (it != mDisplayInfos.end()) {
4254 targets.back().displayTransform = it->second.transform;
4255 targets.back().setDefaultPointerTransform(it->second.transform);
4256 }
4257 }
4258 logOutboundMotionDetails("cancel - ", motionEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004259 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 }
Prabir Pradhan99987712020-11-10 18:43:05 -08004261 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004262 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004263 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
4264 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08004265 LOG_ALWAYS_FATAL("Canceling %s events is not supported",
Dominik Laskowski75788452021-02-09 18:51:25 -08004266 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004267 break;
4268 }
Chris Yef59a2f42020-10-16 12:55:26 -07004269 case EventEntry::Type::DEVICE_RESET:
4270 case EventEntry::Type::SENSOR: {
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004271 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004272 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004273 break;
4274 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 }
4276
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004277 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004278 if (mTracer) {
4279 mTracer->dispatchToTargetHint(*options.traceTracker, targets[0]);
4280 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004281 enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004283
hongzuo liu95785e22022-09-06 02:51:35 +00004284 // If the outbound queue was previously empty, start the dispatch cycle going.
4285 if (wasEmpty && !connection->outboundQueue.empty()) {
4286 startDispatchCycleLocked(currentTime, connection);
4287 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288}
4289
Svet Ganov5d3bc372020-01-26 23:11:07 -08004290void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004291 const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004292 ftl::Flags<InputTarget::Flags> targetFlags,
4293 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00004294 if (connection->status != Connection::Status::NORMAL) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004295 return;
4296 }
4297
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004298 std::vector<std::unique_ptr<EventEntry>> downEvents =
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004299 connection->inputState.synthesizePointerDownEvents(downTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004300
4301 if (downEvents.empty()) {
4302 return;
4303 }
4304
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004305 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004306 ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.",
4307 connection->getInputChannelName().c_str(), downEvents.size());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004308 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004309
Prabir Pradhanfeca0572024-02-06 00:43:11 +00004310 const auto [_, touchedWindowState, displayId] =
4311 findTouchStateWindowAndDisplayLocked(connection->getToken());
4312 if (touchedWindowState == nullptr) {
4313 LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
4314 }
4315 const auto& windowHandle = touchedWindowState->windowHandle;
Svet Ganov5d3bc372020-01-26 23:11:07 -08004316
hongzuo liu95785e22022-09-06 02:51:35 +00004317 const bool wasEmpty = connection->outboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004318 for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004319 std::vector<InputTarget> targets{};
Svet Ganov5d3bc372020-01-26 23:11:07 -08004320 switch (downEventEntry->type) {
4321 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004322 if (mTracer) {
4323 static_cast<MotionEntry&>(*downEventEntry).traceTracker =
4324 mTracer->traceDerivedEvent(*downEventEntry, *traceTracker);
4325 }
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004326 const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry);
4327 if (windowHandle != nullptr) {
4328 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004329 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004330 pointerIndex++) {
4331 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4332 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004333 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
4334 targetFlags, pointerIds, motionEntry.downTime,
4335 targets);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004336 } else {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004337 targets.emplace_back(connection, targetFlags);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004338 const auto it = mDisplayInfos.find(motionEntry.displayId);
4339 if (it != mDisplayInfos.end()) {
4340 targets.back().displayTransform = it->second.transform;
4341 targets.back().setDefaultPointerTransform(it->second.transform);
4342 }
4343 }
4344 logOutboundMotionDetails("down - ", motionEntry);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004345 break;
4346 }
4347
4348 case EventEntry::Type::KEY:
4349 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004350 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08004351 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07004352 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004353 case EventEntry::Type::SENSOR:
4354 case EventEntry::Type::DRAG: {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004355 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004356 ftl::enum_string(downEventEntry->type).c_str());
Svet Ganov5d3bc372020-01-26 23:11:07 -08004357 break;
4358 }
4359 }
4360
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004361 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004362 if (mTracer) {
4363 mTracer->dispatchToTargetHint(*traceTracker, targets[0]);
4364 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004365 enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004366 }
4367
hongzuo liu95785e22022-09-06 02:51:35 +00004368 // If the outbound queue was previously empty, start the dispatch cycle going.
4369 if (wasEmpty && !connection->outboundQueue.empty()) {
4370 startDispatchCycleLocked(downTime, connection);
4371 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004372}
4373
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004374std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004375 const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds,
4376 nsecs_t splitDownTime) {
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004377 const auto& [action, pointerProperties, pointerCoords] =
4378 MotionEvent::split(originalMotionEntry.action, originalMotionEntry.flags,
4379 /*historySize=*/0, originalMotionEntry.pointerProperties,
4380 originalMotionEntry.pointerCoords, pointerIds);
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004381 if (pointerIds.count() != pointerCoords.size()) {
4382 // TODO(b/329107108): Determine why some IDs in pointerIds were not in originalMotionEntry.
4383 // This is bad. We are missing some of the pointers that we expected to deliver.
4384 // Most likely this indicates that we received an ACTION_MOVE events that has
4385 // different pointer ids than we expected based on the previous ACTION_DOWN
4386 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
4387 // in this way.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004388 ALOGW("Dropping split motion event because the pointer count is %zu but "
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004389 "we expected there to be %zu pointers. This probably means we received "
4390 "a broken sequence of pointer ids from the input device: %s",
4391 pointerCoords.size(), pointerIds.count(),
4392 originalMotionEntry.getDescription().c_str());
4393 return nullptr;
4394 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004396 // TODO(b/327503168): Move this check inside MotionEvent::split once all callers handle it
4397 // correctly.
Siarhei Vishniakou59e302b2023-06-05 08:04:53 -07004398 if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) {
4399 logDispatchStateLocked();
4400 LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for "
4401 "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64,
4402 originalMotionEntry.getDescription().c_str(), splitDownTime);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004403 }
4404
Garfield Tanff1f1bb2020-01-28 13:24:04 -08004405 int32_t newId = mIdGenerator.nextId();
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00004406 ATRACE_NAME_IF(ATRACE_ENABLED(),
4407 StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32
4408 ").",
4409 originalMotionEntry.id, newId));
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004410 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004411 std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState,
4412 originalMotionEntry.eventTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004413 originalMotionEntry.deviceId, originalMotionEntry.source,
4414 originalMotionEntry.displayId,
4415 originalMotionEntry.policyFlags, action,
4416 originalMotionEntry.actionButton,
4417 originalMotionEntry.flags, originalMotionEntry.metaState,
4418 originalMotionEntry.buttonState,
4419 originalMotionEntry.classification,
4420 originalMotionEntry.edgeFlags,
4421 originalMotionEntry.xPrecision,
4422 originalMotionEntry.yPrecision,
4423 originalMotionEntry.xCursorPosition,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004424 originalMotionEntry.yCursorPosition, splitDownTime,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004425 pointerProperties, pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +00004426 if (mTracer) {
4427 splitMotionEntry->traceTracker =
4428 mTracer->traceDerivedEvent(*splitMotionEntry, *originalMotionEntry.traceTracker);
4429 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 return splitMotionEntry;
4432}
4433
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004434void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
4435 std::scoped_lock _l(mLock);
Liana Kazanova5b8217b2024-07-18 17:44:51 +00004436 // Reset key repeating in case a keyboard device was added or removed or something.
4437 resetKeyRepeatLocked();
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004438 mLatencyTracker.setInputDevices(args.inputDeviceInfos);
4439}
4440
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004441void InputDispatcher::notifyKey(const NotifyKeyArgs& args) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004442 ALOGD_IF(debugInboundEventDetails(),
4443 "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64
Linnan Li13bf76a2024-05-05 19:18:02 +08004444 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, action=%s, flags=0x%x, "
4445 "keyCode=%s, scanCode=0x%x, metaState=0x%x, "
Prabir Pradhan96282b02023-02-24 22:36:17 +00004446 "downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004447 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004448 args.displayId.toString().c_str(), args.policyFlags,
4449 KeyEvent::actionToString(args.action), args.flags, KeyEvent::getLabel(args.keyCode),
4450 args.scanCode, args.metaState, args.downTime);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004451 Result<void> keyCheck = validateKeyEvent(args.action);
4452 if (!keyCheck.ok()) {
4453 LOG(ERROR) << "invalid key event: " << keyCheck.error();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 return;
4455 }
4456
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004457 uint32_t policyFlags = args.policyFlags;
4458 int32_t flags = args.flags;
4459 int32_t metaState = args.metaState;
Siarhei Vishniakou622bd322018-10-29 18:02:27 -07004460 // InputDispatcher tracks and generates key repeats on behalf of
4461 // whatever notifies it, so repeatCount should always be set to 0
4462 constexpr int32_t repeatCount = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
4464 policyFlags |= POLICY_FLAG_VIRTUAL;
4465 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
4466 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 if (policyFlags & POLICY_FLAG_FUNCTION) {
4468 metaState |= AMETA_FUNCTION_ON;
4469 }
4470
4471 policyFlags |= POLICY_FLAG_TRUSTED;
4472
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004473 int32_t keyCode = args.keyCode;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 KeyEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004475 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action,
4476 flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime,
4477 args.eventTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
Michael Wright2b3c3302018-03-02 17:19:13 +00004479 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004480 mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004481 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4482 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004483 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004484 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
Antonio Kantekf16f2832021-09-28 04:39:20 +00004486 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 { // acquire lock
4488 mLock.lock();
4489
4490 if (shouldSendKeyToInputFilterLocked(args)) {
4491 mLock.unlock();
4492
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004493 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004494 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 return; // event was consumed by the filter
4496 }
4497
4498 mLock.lock();
4499 }
4500
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004501 std::unique_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004502 std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4503 args.deviceId, args.source, args.displayId, policyFlags,
4504 args.action, flags, keyCode, args.scanCode, metaState,
4505 repeatCount, args.downTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004506 if (mTracer) {
4507 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4508 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004510 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 mLock.unlock();
4512 } // release lock
4513
4514 if (needWake) {
4515 mLooper->wake();
4516 }
4517}
4518
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004519bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 return mInputFilterEnabled;
4521}
4522
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004523void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004524 if (debugInboundEventDetails()) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004525 ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08004526 "displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakou6ebd0692022-10-20 15:05:45 -07004527 "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07004528 "xCursorPosition=%f, yCursorPosition=%f, downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004529 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004530 args.displayId.toString().c_str(), args.policyFlags,
4531 MotionEvent::actionToString(args.action).c_str(), args.actionButton, args.flags,
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07004532 args.metaState, args.buttonState, args.xCursorPosition, args.yCursorPosition,
4533 args.downTime);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004534 for (uint32_t i = 0; i < args.getPointerCount(); i++) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004535 ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, "
4536 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004537 i, args.pointerProperties[i].id,
4538 ftl::enum_string(args.pointerProperties[i].toolType).c_str(),
4539 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
4540 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
4541 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
4542 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
4543 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
4544 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
4545 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
4546 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
4547 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004548 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004550
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004551 Result<void> motionCheck =
4552 validateMotionEvent(args.action, args.actionButton, args.getPointerCount(),
4553 args.pointerProperties.data());
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004554 if (!motionCheck.ok()) {
4555 LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error();
4556 return;
4557 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004559 if (DEBUG_VERIFY_EVENTS) {
4560 auto [it, _] =
4561 mVerifiersByDisplay.try_emplace(args.displayId,
Linnan Li13bf76a2024-05-05 19:18:02 +08004562 StringPrintf("display %s",
4563 args.displayId.toString().c_str()));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004564 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -07004565 it->second.processMovement(args.deviceId, args.source, args.action,
4566 args.getPointerCount(), args.pointerProperties.data(),
4567 args.pointerCoords.data(), args.flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004568 if (!result.ok()) {
4569 LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump();
4570 }
4571 }
4572
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004573 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 policyFlags |= POLICY_FLAG_TRUSTED;
Michael Wright2b3c3302018-03-02 17:19:13 +00004575
4576 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004577 mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime,
4578 policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004579 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4580 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004581 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004582 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
Antonio Kantekf16f2832021-09-28 04:39:20 +00004584 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 { // acquire lock
4586 mLock.lock();
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004587 if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
4588 // Set the flag anyway if we already have an ongoing gesture. That would allow us to
4589 // complete the processing of the current stroke.
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004590 const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004591 if (touchStateIt != mTouchStatesByDisplay.end()) {
4592 const TouchState& touchState = touchStateIt->second;
Linnan Li907ae732023-09-05 17:14:21 +08004593 if (touchState.hasTouchingPointers(args.deviceId) ||
4594 touchState.hasHoveringPointers(args.deviceId)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004595 policyFlags |= POLICY_FLAG_PASS_TO_USER;
4596 }
4597 }
4598 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
4600 if (shouldSendMotionToInputFilterLocked(args)) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004601 ui::Transform displayTransform;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004602 if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004603 displayTransform = it->second.transform;
4604 }
4605
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 mLock.unlock();
4607
4608 MotionEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004609 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC,
4610 args.action, args.actionButton, args.flags, args.edgeFlags,
4611 args.metaState, args.buttonState, args.classification,
4612 displayTransform, args.xPrecision, args.yPrecision,
4613 args.xCursorPosition, args.yCursorPosition, displayTransform,
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004614 args.downTime, args.eventTime, args.getPointerCount(),
4615 args.pointerProperties.data(), args.pointerCoords.data());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004618 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 return; // event was consumed by the filter
4620 }
4621
4622 mLock.lock();
4623 }
4624
4625 // Just enqueue a new motion event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004626 std::unique_ptr<MotionEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004627 std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4628 args.deviceId, args.source, args.displayId,
4629 policyFlags, args.action, args.actionButton,
4630 args.flags, args.metaState, args.buttonState,
4631 args.classification, args.edgeFlags, args.xPrecision,
4632 args.yPrecision, args.xCursorPosition,
4633 args.yCursorPosition, args.downTime,
4634 args.pointerProperties, args.pointerCoords);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004635 if (mTracer) {
4636 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4637 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004639 if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
4640 IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004641 !mInputFilterEnabled) {
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004642 std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args);
jioana0bdbea12024-08-10 19:26:04 +00004643 mLatencyTracker.trackListener(args.id, args.eventTime, args.readTime, args.deviceId,
4644 sources, args.action, InputEventType::MOTION);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004645 }
4646
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004647 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 mLock.unlock();
4649 } // release lock
4650
4651 if (needWake) {
4652 mLooper->wake();
4653 }
4654}
4655
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004656void InputDispatcher::notifySensor(const NotifySensorArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004657 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004658 ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, "
4659 " sensorType=%s",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004660 args.id, args.eventTime, args.deviceId, args.source,
4661 ftl::enum_string(args.sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004662 }
Chris Yef59a2f42020-10-16 12:55:26 -07004663
Antonio Kantekf16f2832021-09-28 04:39:20 +00004664 bool needWake = false;
Chris Yef59a2f42020-10-16 12:55:26 -07004665 { // acquire lock
4666 mLock.lock();
4667
4668 // Just enqueue a new sensor event.
4669 std::unique_ptr<SensorEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004670 std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source,
4671 /* policyFlags=*/0, args.hwTimestamp, args.sensorType,
4672 args.accuracy, args.accuracyChanged, args.values);
Chris Yef59a2f42020-10-16 12:55:26 -07004673
4674 needWake = enqueueInboundEventLocked(std::move(newEntry));
4675 mLock.unlock();
4676 } // release lock
4677
4678 if (needWake) {
4679 mLooper->wake();
4680 }
4681}
4682
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004683void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004684 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004685 ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime,
4686 args.deviceId, args.isOn);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004687 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00004688 mPolicy.notifyVibratorState(args.deviceId, args.isOn);
Chris Yefb552902021-02-03 17:18:37 -08004689}
4690
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004691bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) {
Jackal Guof9696682018-10-05 12:23:23 +08004692 return mInputFilterEnabled;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693}
4694
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004695void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004696 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004697 ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, "
4698 "switchMask=0x%08x",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004699 args.eventTime, args.policyFlags, args.switchValues, args.switchMask);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004700 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004702 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 policyFlags |= POLICY_FLAG_TRUSTED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004704 mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705}
4706
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004707void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004708 // TODO(b/308677868) Remove device reset from the InputListener interface
Prabir Pradhan65613802023-02-22 23:36:58 +00004709 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004710 ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime,
4711 args.deviceId);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713
Antonio Kantekf16f2832021-09-28 04:39:20 +00004714 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004716 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004718 std::unique_ptr<DeviceResetEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004719 std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004720 needWake = enqueueInboundEventLocked(std::move(newEntry));
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -07004721
4722 for (auto& [_, verifier] : mVerifiersByDisplay) {
4723 verifier.resetDevice(args.deviceId);
4724 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 } // release lock
4726
4727 if (needWake) {
4728 mLooper->wake();
4729 }
4730}
4731
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004732void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004733 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004734 ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime,
Hiroki Sato25040232024-02-22 17:21:22 +09004735 args.request.isEnable() ? "true" : "false");
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004736 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004737
Antonio Kantekf16f2832021-09-28 04:39:20 +00004738 bool needWake = false;
Prabir Pradhan99987712020-11-10 18:43:05 -08004739 { // acquire lock
4740 std::scoped_lock _l(mLock);
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004741 auto entry =
4742 std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004743 needWake = enqueueInboundEventLocked(std::move(entry));
4744 } // release lock
4745
4746 if (needWake) {
4747 mLooper->wake();
4748 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004749}
4750
Prabir Pradhan5735a322022-04-11 17:23:34 +00004751InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00004752 std::optional<gui::Uid> targetUid,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004753 InputEventInjectionSync syncMode,
4754 std::chrono::milliseconds timeout,
4755 uint32_t policyFlags) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004756 Result<void> eventValidation = validateInputEvent(*event);
4757 if (!eventValidation.ok()) {
4758 LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error();
4759 return InputEventInjectionResult::FAILED;
4760 }
4761
Prabir Pradhan65613802023-02-22 23:36:58 +00004762 if (debugInboundEventDetails()) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004763 LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString)
4764 << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count()
4765 << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec
4766 << ", event=" << *event;
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004767 }
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -07004768 nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769
Prabir Pradhan5735a322022-04-11 17:23:34 +00004770 policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004772 // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004773 // that have gone through the InputFilter. If the event passed through the InputFilter, assign
4774 // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
4775 // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
4776 // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
4777 // from events that originate from actual hardware.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -07004778 DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004779 if (policyFlags & POLICY_FLAG_FILTERED) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004780 resolvedDeviceId = event->getDeviceId();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004781 }
4782
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004783 const bool isAsync = syncMode == InputEventInjectionSync::NONE;
4784 auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync);
4785
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004786 std::queue<std::unique_ptr<EventEntry>> injectedEntries;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004788 case InputEventType::KEY: {
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004789 const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004790 const int32_t action = incomingKey.getAction();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004791 int32_t flags = incomingKey.getFlags();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004792 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4793 flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4794 }
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004795 int32_t keyCode = incomingKey.getKeyCode();
4796 int32_t metaState = incomingKey.getMetaState();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004797 KeyEvent keyEvent;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004798 keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(),
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004799 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode,
4800 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
4801 incomingKey.getDownTime(), incomingKey.getEventTime());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004803 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
4804 policyFlags |= POLICY_FLAG_VIRTUAL;
Michael Wright2b3c3302018-03-02 17:19:13 +00004805 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004806
4807 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
4808 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004809 mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004810 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4811 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
4812 std::to_string(t.duration().count()).c_str());
4813 }
4814 }
4815
4816 mLock.lock();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004817 std::unique_ptr<KeyEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004818 std::make_unique<KeyEntry>(incomingKey.getId(), injectionState,
4819 incomingKey.getEventTime(), resolvedDeviceId,
4820 incomingKey.getSource(), incomingKey.getDisplayId(),
4821 policyFlags, action, flags, keyCode,
4822 incomingKey.getScanCode(), metaState,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004823 incomingKey.getRepeatCount(),
4824 incomingKey.getDownTime());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004825 if (mTracer) {
4826 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4827 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004828 injectedEntries.push(std::move(injectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004829 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 }
4831
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004832 case InputEventType::MOTION: {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004833 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004834 const bool isPointerEvent =
4835 isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER);
4836 // If a pointer event has no displayId specified, inject it to the default display.
Linnan Li13bf76a2024-05-05 19:18:02 +08004837 const ui::LogicalDisplayId displayId =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004838 isPointerEvent && (event->getDisplayId() == ui::LogicalDisplayId::INVALID)
4839 ? ui::LogicalDisplayId::DEFAULT
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004840 : event->getDisplayId();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004841 int32_t flags = motionEvent.getFlags();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004842
4843 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004844 nsecs_t eventTime = motionEvent.getEventTime();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004845 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004846 mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(),
4847 motionEvent.getAction(), eventTime,
4848 /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004849 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4850 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
4851 std::to_string(t.duration().count()).c_str());
4852 }
4853 }
4854
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004855 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4856 flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4857 }
4858
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004859 mLock.lock();
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004860
Siarhei Vishniakoueb75bc82024-06-04 13:05:45 -07004861 {
4862 // Verify all injected streams, whether the injection is coming from apps or from
4863 // input filter. Print an error if the stream becomes inconsistent with this event.
4864 // An inconsistent injected event sent could cause a crash in the later stages of
4865 // dispatching pipeline.
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004866 auto [it, _] =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004867 mInputFilterVerifiersByDisplay.try_emplace(displayId,
4868 std::string("Injection on ") +
4869 displayId.toString());
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004870 InputVerifier& verifier = it->second;
4871
4872 Result<void> result =
4873 verifier.processMovement(resolvedDeviceId, motionEvent.getSource(),
4874 motionEvent.getAction(),
4875 motionEvent.getPointerCount(),
4876 motionEvent.getPointerProperties(),
4877 motionEvent.getSamplePointerCoords(), flags);
4878 if (!result.ok()) {
4879 logDispatchStateLocked();
4880 LOG(ERROR) << "Inconsistent event: " << motionEvent
4881 << ", reason: " << result.error();
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -07004882 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4883 mLock.unlock();
4884 return InputEventInjectionResult::FAILED;
4885 }
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004886 }
4887 }
4888
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004889 const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004890 const size_t pointerCount = motionEvent.getPointerCount();
4891 const std::vector<PointerProperties>
4892 pointerProperties(motionEvent.getPointerProperties(),
4893 motionEvent.getPointerProperties() + pointerCount);
4894
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004895 const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004896 std::unique_ptr<MotionEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004897 std::make_unique<MotionEntry>(motionEvent.getId(), injectionState,
4898 *sampleEventTimes, resolvedDeviceId,
4899 motionEvent.getSource(), displayId, policyFlags,
4900 motionEvent.getAction(),
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004901 motionEvent.getActionButton(), flags,
4902 motionEvent.getMetaState(),
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004903 motionEvent.getButtonState(),
4904 motionEvent.getClassification(),
4905 motionEvent.getEdgeFlags(),
4906 motionEvent.getXPrecision(),
4907 motionEvent.getYPrecision(),
4908 motionEvent.getRawXCursorPosition(),
4909 motionEvent.getRawYCursorPosition(),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004910 motionEvent.getDownTime(), pointerProperties,
4911 std::vector<PointerCoords>(samplePointerCoords,
4912 samplePointerCoords +
4913 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004914 transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004915 if (mTracer) {
4916 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4917 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004918 injectedEntries.push(std::move(injectedEntry));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004919 for (size_t i = motionEvent.getHistorySize(); i > 0; i--) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004920 sampleEventTimes += 1;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004921 samplePointerCoords += motionEvent.getPointerCount();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004922 std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique<
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004923 MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes,
4924 resolvedDeviceId, motionEvent.getSource(), displayId,
4925 policyFlags, motionEvent.getAction(),
4926 motionEvent.getActionButton(), flags,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004927 motionEvent.getMetaState(), motionEvent.getButtonState(),
4928 motionEvent.getClassification(), motionEvent.getEdgeFlags(),
4929 motionEvent.getXPrecision(), motionEvent.getYPrecision(),
4930 motionEvent.getRawXCursorPosition(),
4931 motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(),
4932 pointerProperties,
4933 std::vector<PointerCoords>(samplePointerCoords,
4934 samplePointerCoords +
4935 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004936 transformMotionEntryForInjectionLocked(*nextInjectedEntry,
4937 motionEvent.getTransform());
Prabir Pradhan2b2d1bf2024-03-05 16:27:56 +00004938 if (mTracer) {
4939 nextInjectedEntry->traceTracker =
4940 mTracer->traceInboundEvent(*nextInjectedEntry);
4941 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004942 injectedEntries.push(std::move(nextInjectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004943 }
4944 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004947 default:
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004948 LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events";
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004949 return InputEventInjectionResult::FAILED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950 }
4951
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 bool needWake = false;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004953 while (!injectedEntries.empty()) {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004954 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004955 LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004956 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004957 needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front()));
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004958 injectedEntries.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 }
4960
4961 mLock.unlock();
4962
4963 if (needWake) {
4964 mLooper->wake();
4965 }
4966
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004967 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004969 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004971 if (syncMode == InputEventInjectionSync::NONE) {
4972 injectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 } else {
4974 for (;;) {
4975 injectionResult = injectionState->injectionResult;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004976 if (injectionResult != InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 break;
4978 }
4979
4980 nsecs_t remainingTimeout = endTime - now();
4981 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004982 if (DEBUG_INJECTION) {
4983 ALOGD("injectInputEvent - Timed out waiting for injection result "
4984 "to become available.");
4985 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004986 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 break;
4988 }
4989
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004990 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 }
4992
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004993 if (injectionResult == InputEventInjectionResult::SUCCEEDED &&
4994 syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 while (injectionState->pendingForegroundDispatches != 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004996 if (DEBUG_INJECTION) {
4997 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
4998 injectionState->pendingForegroundDispatches);
4999 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 nsecs_t remainingTimeout = endTime - now();
5001 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005002 if (DEBUG_INJECTION) {
5003 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
5004 "dispatches to finish.");
5005 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005006 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 break;
5008 }
5009
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005010 mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 }
5012 }
5013 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 } // release lock
5015
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005016 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005017 LOG(INFO) << "injectInputEvent - Finished with result "
5018 << ftl::enum_string(injectionResult);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005019 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
5021 return injectionResult;
5022}
5023
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005024std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) {
Gang Wange9087892020-01-07 12:17:14 -05005025 std::array<uint8_t, 32> calculatedHmac;
5026 std::unique_ptr<VerifiedInputEvent> result;
5027 switch (event.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005028 case InputEventType::KEY: {
Gang Wange9087892020-01-07 12:17:14 -05005029 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
5030 VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent);
5031 result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005032 calculatedHmac = sign(verifiedKeyEvent);
Gang Wange9087892020-01-07 12:17:14 -05005033 break;
5034 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005035 case InputEventType::MOTION: {
Gang Wange9087892020-01-07 12:17:14 -05005036 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
5037 VerifiedMotionEvent verifiedMotionEvent =
5038 verifiedMotionEventFromMotionEvent(motionEvent);
5039 result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005040 calculatedHmac = sign(verifiedMotionEvent);
Gang Wange9087892020-01-07 12:17:14 -05005041 break;
5042 }
5043 default: {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08005044 LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType());
Gang Wange9087892020-01-07 12:17:14 -05005045 return nullptr;
5046 }
5047 }
5048 if (calculatedHmac == INVALID_HMAC) {
5049 return nullptr;
5050 }
tyiu1573a672023-02-21 22:38:32 +00005051 if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) {
Gang Wange9087892020-01-07 12:17:14 -05005052 return nullptr;
5053 }
5054 return result;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005055}
5056
Prabir Pradhan24047542023-11-02 17:14:59 +00005057void InputDispatcher::setInjectionResult(const EventEntry& entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005058 InputEventInjectionResult injectionResult) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005059 if (!entry.injectionState) {
5060 // Not an injected event.
5061 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 }
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005063
5064 InjectionState& injectionState = *entry.injectionState;
5065 if (DEBUG_INJECTION) {
5066 LOG(INFO) << "Setting input event injection result to "
5067 << ftl::enum_string(injectionResult);
5068 }
5069
5070 if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) {
5071 // Log the outcome since the injector did not wait for the injection result.
5072 switch (injectionResult) {
5073 case InputEventInjectionResult::SUCCEEDED:
5074 ALOGV("Asynchronous input event injection succeeded.");
5075 break;
5076 case InputEventInjectionResult::TARGET_MISMATCH:
5077 ALOGV("Asynchronous input event injection target mismatch.");
5078 break;
5079 case InputEventInjectionResult::FAILED:
5080 ALOGW("Asynchronous input event injection failed.");
5081 break;
5082 case InputEventInjectionResult::TIMED_OUT:
5083 ALOGW("Asynchronous input event injection timed out.");
5084 break;
5085 case InputEventInjectionResult::PENDING:
5086 ALOGE("Setting result to 'PENDING' for asynchronous injection");
5087 break;
5088 }
5089 }
5090
5091 injectionState.injectionResult = injectionResult;
5092 mInjectionResultAvailable.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005095void InputDispatcher::transformMotionEntryForInjectionLocked(
5096 MotionEntry& entry, const ui::Transform& injectedTransform) const {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005097 // Input injection works in the logical display coordinate space, but the input pipeline works
5098 // display space, so we need to transform the injected events accordingly.
5099 const auto it = mDisplayInfos.find(entry.displayId);
5100 if (it == mDisplayInfos.end()) return;
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005101 const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005102
Prabir Pradhand9a2ebe2022-07-20 19:25:13 +00005103 if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
5104 entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
5105 const vec2 cursor =
5106 MotionEvent::calculateTransformedXY(entry.source, transformToDisplay,
5107 {entry.xCursorPosition, entry.yCursorPosition});
5108 entry.xCursorPosition = cursor.x;
5109 entry.yCursorPosition = cursor.y;
5110 }
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07005111 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08005112 entry.pointerCoords[i] =
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005113 MotionEvent::calculateTransformedCoords(entry.source, entry.flags,
5114 transformToDisplay, entry.pointerCoords[i]);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005115 }
5116}
5117
Prabir Pradhan24047542023-11-02 17:14:59 +00005118void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005119 if (entry.injectionState) {
5120 entry.injectionState->pendingForegroundDispatches += 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 }
5122}
5123
Prabir Pradhan24047542023-11-02 17:14:59 +00005124void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005125 if (entry.injectionState) {
5126 entry.injectionState->pendingForegroundDispatches -= 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005128 if (entry.injectionState->pendingForegroundDispatches == 0) {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005129 mInjectionSyncFinished.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 }
5131 }
5132}
5133
chaviw98318de2021-05-19 16:45:23 -05005134const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005135 ui::LogicalDisplayId displayId) const {
chaviw98318de2021-05-19 16:45:23 -05005136 static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES;
Vishnu Nairad321cd2020-08-20 16:40:21 -07005137 auto it = mWindowHandlesByDisplay.find(displayId);
5138 return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES;
Arthur Hungb92218b2018-08-14 12:00:21 +08005139}
5140
chaviw98318de2021-05-19 16:45:23 -05005141sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005142 const sp<IBinder>& windowHandleToken, std::optional<ui::LogicalDisplayId> displayId) const {
arthurhungbe737672020-06-24 12:29:21 +08005143 if (windowHandleToken == nullptr) {
5144 return nullptr;
5145 }
5146
Prabir Pradhan16463382023-10-12 23:03:19 +00005147 if (!displayId) {
5148 // Look through all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +08005149 for (const auto& [_, windowHandles] : mWindowHandlesByDisplay) {
Prabir Pradhan16463382023-10-12 23:03:19 +00005150 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
5151 if (windowHandle->getToken() == windowHandleToken) {
5152 return windowHandle;
5153 }
Arthur Hungb92218b2018-08-14 12:00:21 +08005154 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155 }
Vishnu Nairad321cd2020-08-20 16:40:21 -07005156 return nullptr;
5157 }
5158
Prabir Pradhan16463382023-10-12 23:03:19 +00005159 // Only look through the requested display.
5160 for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005161 if (windowHandle->getToken() == windowHandleToken) {
5162 return windowHandle;
5163 }
5164 }
5165 return nullptr;
5166}
5167
chaviw98318de2021-05-19 16:45:23 -05005168sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
5169 const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08005170 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
chaviw98318de2021-05-19 16:45:23 -05005171 for (const sp<WindowInfoHandle>& handle : windowHandles) {
arthurhungbe737672020-06-24 12:29:21 +08005172 if (handle->getId() == windowHandle->getId() &&
5173 handle->getToken() == windowHandle->getToken()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005174 if (windowHandle->getInfo()->displayId != displayId) {
5175 ALOGE("Found window %s in display %s"
5176 ", but it should belong to display %s",
5177 windowHandle->getName().c_str(), displayId.toString().c_str(),
5178 windowHandle->getInfo()->displayId.toString().c_str());
Mady Mellor017bcd12020-06-23 19:12:00 +00005179 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005180 return handle;
Arthur Hungb92218b2018-08-14 12:00:21 +08005181 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 }
5183 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005184 return nullptr;
5185}
5186
Linnan Li13bf76a2024-05-05 19:18:02 +08005187sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(
5188 ui::LogicalDisplayId displayId) const {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005189 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId);
5190 return getWindowHandleLocked(focusedToken, displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191}
5192
Linnan Li13bf76a2024-05-05 19:18:02 +08005193ui::Transform InputDispatcher::getTransformLocked(ui::LogicalDisplayId displayId) const {
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005194 auto displayInfoIt = mDisplayInfos.find(displayId);
5195 return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform
5196 : kIdentityTransform;
5197}
5198
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005199bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window,
5200 const MotionEntry& motionEntry) const {
5201 const WindowInfo& info = *window->getInfo();
5202
5203 // Skip spy window targets that are not valid for targeted injection.
5204 if (const auto err = verifyTargetedInjection(window, motionEntry); err) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005205 return false;
5206 }
5207
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005208 if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
5209 ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str());
5210 return false;
5211 }
5212
5213 if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
5214 ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL",
5215 window->getName().c_str());
5216 return false;
5217 }
5218
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005219 std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005220 if (connection == nullptr) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005221 ALOGW("Not sending touch to %s because there's no corresponding connection",
5222 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005223 return false;
5224 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005225
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005226 if (!connection->responsive) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005227 ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005228 return false;
5229 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005230
5231 // Drop events that can't be trusted due to occlusion
5232 const auto [x, y] = resolveTouchedPosition(motionEntry);
5233 TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5234 if (!isTouchTrustedLocked(occlusionInfo)) {
5235 if (DEBUG_TOUCH_OCCLUSION) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00005236 ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005237 for (const auto& log : occlusionInfo.debugInfo) {
5238 ALOGD("%s", log.c_str());
5239 }
5240 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005241 ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(),
5242 occlusionInfo.obscuringUid.toString().c_str());
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005243 return false;
5244 }
5245
5246 // Drop touch events if requested by input feature
5247 if (shouldDropInput(motionEntry, window)) {
5248 return false;
5249 }
5250
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07005251 // Ignore touches if stylus is down anywhere on screen
5252 if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) &&
5253 isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) {
5254 LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active";
5255 return false;
5256 }
5257
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005258 return true;
5259}
5260
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005261void InputDispatcher::updateWindowHandlesForDisplayLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005262 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5263 ui::LogicalDisplayId displayId) {
chaviw98318de2021-05-19 16:45:23 -05005264 if (windowInfoHandles.empty()) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005265 // Remove all handles on a display if there are no windows left.
5266 mWindowHandlesByDisplay.erase(displayId);
5267 return;
5268 }
5269
5270 // Since we compare the pointer of input window handles across window updates, we need
5271 // to make sure the handle object for the same window stays unchanged across updates.
chaviw98318de2021-05-19 16:45:23 -05005272 const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId);
5273 std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById;
5274 for (const sp<WindowInfoHandle>& handle : oldHandles) {
chaviwaf87b3e2019-10-01 16:59:28 -07005275 oldHandlesById[handle->getId()] = handle;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005276 }
5277
chaviw98318de2021-05-19 16:45:23 -05005278 std::vector<sp<WindowInfoHandle>> newHandles;
5279 for (const sp<WindowInfoHandle>& handle : windowInfoHandles) {
chaviw98318de2021-05-19 16:45:23 -05005280 const WindowInfo* info = handle->getInfo();
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005281 if (getConnectionLocked(handle->getToken()) == nullptr) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005282 const bool noInputChannel =
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005283 info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005284 const bool canReceiveInput =
5285 !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) ||
5286 !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005287 if (canReceiveInput && !noInputChannel) {
John Recke0710582019-09-26 13:46:12 -07005288 ALOGV("Window handle %s has no registered input channel",
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005289 handle->getName().c_str());
Robert Carr2984b7a2020-04-13 17:06:45 -07005290 continue;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005291 }
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005292 }
5293
5294 if (info->displayId != displayId) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005295 ALOGE("Window %s updated by wrong display %s, should belong to display %s",
5296 handle->getName().c_str(), displayId.toString().c_str(),
5297 info->displayId.toString().c_str());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005298 continue;
5299 }
5300
Robert Carredd13602020-04-13 17:24:34 -07005301 if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) &&
Linnan Li13bf76a2024-05-05 19:18:02 +08005302 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) {
chaviw98318de2021-05-19 16:45:23 -05005303 const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005304 oldHandle->updateFrom(handle);
5305 newHandles.push_back(oldHandle);
5306 } else {
5307 newHandles.push_back(handle);
5308 }
5309 }
5310
5311 // Insert or replace
5312 mWindowHandlesByDisplay[displayId] = newHandles;
5313}
5314
Arthur Hungb92218b2018-08-14 12:00:21 +08005315/**
5316 * Called from InputManagerService, update window handle list by displayId that can receive input.
5317 * A window handle contains information about InputChannel, Touch Region, Types, Focused,...
5318 * If set an empty list, remove all handles from the specific display.
5319 * For focused handle, check if need to change and send a cancel event to previous one.
5320 * For removed handle, check if need to send a cancel event if already in touch.
5321 */
Arthur Hung72d8dc32020-03-28 00:48:39 +00005322void InputDispatcher::setInputWindowsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005323 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5324 ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005325 if (DEBUG_FOCUS) {
5326 std::string windowList;
chaviw98318de2021-05-19 16:45:23 -05005327 for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005328 windowList += iwh->getName() + " ";
5329 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005330 LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList;
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005331 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005332 ScopedSyntheticEventTracer traceContext(mTracer);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333
Prabir Pradhand65552b2021-10-07 11:23:50 -07005334 // Check preconditions for new input windows
chaviw98318de2021-05-19 16:45:23 -05005335 for (const sp<WindowInfoHandle>& window : windowInfoHandles) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07005336 const WindowInfo& info = *window->getInfo();
5337
5338 // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005339 const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005340 if (noInputWindow && window->getToken() != nullptr) {
5341 ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing",
5342 window->getName().c_str());
5343 window->releaseChannel();
5344 }
Prabir Pradhand65552b2021-10-07 11:23:50 -07005345
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005346 // Ensure all spy windows are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005347 LOG_ALWAYS_FATAL_IF(info.isSpy() &&
5348 !info.inputConfig.test(
5349 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005350 "%s has feature SPY, but is not a trusted overlay.",
5351 window->getName().c_str());
5352
Prabir Pradhand65552b2021-10-07 11:23:50 -07005353 // Ensure all stylus interceptors are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005354 LOG_ALWAYS_FATAL_IF(info.interceptsStylus() &&
5355 !info.inputConfig.test(
5356 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhand65552b2021-10-07 11:23:50 -07005357 "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.",
5358 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005359 }
5360
Arthur Hung72d8dc32020-03-28 00:48:39 +00005361 // Copy old handles for release if they are no longer present.
chaviw98318de2021-05-19 16:45:23 -05005362 const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005363 const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364
chaviw98318de2021-05-19 16:45:23 -05005365 updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005366
chaviw98318de2021-05-19 16:45:23 -05005367 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005368
Vishnu Nairc519ff72021-01-21 08:23:08 -08005369 std::optional<FocusResolver::FocusChanges> changes =
5370 mFocusResolver.setInputWindows(displayId, windowHandles);
5371 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005372 onFocusChangedLocked(*changes, traceContext.getTracker(), removedFocusedWindowHandle);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005373 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
Linnan Li72352222024-04-12 18:55:57 +08005375 if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5376 TouchState& state = it->second;
Arthur Hung72d8dc32020-03-28 00:48:39 +00005377 for (size_t i = 0; i < state.windows.size();) {
5378 TouchedWindow& touchedWindow = state.windows[i];
Linnan Li72352222024-04-12 18:55:57 +08005379 if (getWindowHandleLocked(touchedWindow.windowHandle) != nullptr) {
5380 i++;
5381 continue;
5382 }
5383 LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName()
5384 << " in display %" << displayId;
5385 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
5386 "touched window was removed", traceContext.getTracker());
5387 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
5388 // Since we are about to drop the touch, cancel the events for the wallpaper as
5389 // well.
5390 if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) &&
5391 touchedWindow.windowHandle->getInfo()->inputConfig.test(
5392 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
5393 for (const DeviceId deviceId : touchedWindow.getTouchingDeviceIds()) {
5394 if (const auto& ww = state.getWallpaperWindow(deviceId); ww != nullptr) {
5395 options.deviceId = deviceId;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005396 synthesizeCancelationEventsForWindowLocked(ww, options);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005397 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 }
Linnan Li72352222024-04-12 18:55:57 +08005400 state.windows.erase(state.windows.begin() + i);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 }
arthurhungb89ccb02020-12-30 16:19:01 +08005402
arthurhung6d4bed92021-03-17 11:59:33 +08005403 // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We
arthurhungb89ccb02020-12-30 16:19:01 +08005404 // could just clear the state here.
Arthur Hung3915c1f2022-05-31 07:17:17 +00005405 if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId &&
arthurhung6d4bed92021-03-17 11:59:33 +08005406 std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) ==
arthurhungb89ccb02020-12-30 16:19:01 +08005407 windowHandles.end()) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00005408 ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str());
5409 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08005410 mDragState.reset();
arthurhungb89ccb02020-12-30 16:19:01 +08005411 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005412 }
Arthur Hung25e2af12020-03-26 12:58:37 +00005413
Arthur Hung72d8dc32020-03-28 00:48:39 +00005414 // Release information for windows that are no longer present.
5415 // This ensures that unused input channels are released promptly.
5416 // Otherwise, they might stick around until the window handle is destroyed
5417 // which might not happen until the next GC.
chaviw98318de2021-05-19 16:45:23 -05005418 for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005419 if (getWindowHandleLocked(oldWindowHandle) == nullptr) {
Arthur Hung72d8dc32020-03-28 00:48:39 +00005420 if (DEBUG_FOCUS) {
5421 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str());
Arthur Hung25e2af12020-03-26 12:58:37 +00005422 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005423 oldWindowHandle->releaseChannel();
Arthur Hung25e2af12020-03-26 12:58:37 +00005424 }
chaviw291d88a2019-02-14 10:33:58 -08005425 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426}
5427
5428void InputDispatcher::setFocusedApplication(
Linnan Li13bf76a2024-05-05 19:18:02 +08005429 ui::LogicalDisplayId displayId,
5430 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005431 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005432 ALOGD("setFocusedApplication displayId=%s %s", displayId.toString().c_str(),
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005433 inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>");
5434 }
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005435 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005436 std::scoped_lock _l(mLock);
Vishnu Nair599f1412021-06-21 10:39:58 -07005437 setFocusedApplicationLocked(displayId, inputApplicationHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438 } // release lock
5439
5440 // Wake up poll loop since it may need to make new input dispatching choices.
5441 mLooper->wake();
5442}
5443
Vishnu Nair599f1412021-06-21 10:39:58 -07005444void InputDispatcher::setFocusedApplicationLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005445 ui::LogicalDisplayId displayId,
5446 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Vishnu Nair599f1412021-06-21 10:39:58 -07005447 std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle =
5448 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
5449
5450 if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) {
5451 return; // This application is already focused. No need to wake up or change anything.
5452 }
5453
5454 // Set the new application handle.
5455 if (inputApplicationHandle != nullptr) {
5456 mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
5457 } else {
5458 mFocusedApplicationHandlesByDisplay.erase(displayId);
5459 }
5460
5461 // No matter what the old focused application was, stop waiting on it because it is
5462 // no longer focused.
5463 resetNoFocusedWindowTimeoutLocked();
5464}
5465
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08005466void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) {
5467 if (interval.count() < 0) {
5468 LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0");
5469 }
5470 std::scoped_lock _l(mLock);
5471 mMinTimeBetweenUserActivityPokes = interval;
5472}
5473
Tiger Huang721e26f2018-07-24 22:26:19 +08005474/**
5475 * Sets the focused display, which is responsible for receiving focus-dispatched input events where
5476 * the display not specified.
5477 *
5478 * We track any unreleased events for each window. If a window loses the ability to receive the
5479 * released event, we will send a cancel event to it. So when the focused display is changed, we
5480 * cancel all the unreleased display-unspecified events for the focused window on the old focused
5481 * display. The display-specified events won't be affected.
5482 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005483void InputDispatcher::setFocusedDisplay(ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005484 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005485 ALOGD("setFocusedDisplay displayId=%s", displayId.toString().c_str());
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005486 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005487 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005488 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005489 ScopedSyntheticEventTracer traceContext(mTracer);
Tiger Huang721e26f2018-07-24 22:26:19 +08005490
5491 if (mFocusedDisplayId != displayId) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005492 sp<IBinder> oldFocusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08005493 mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07005494 if (oldFocusedWindowToken != nullptr) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005495 const auto windowHandle =
5496 getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId);
5497 if (windowHandle == nullptr) {
5498 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Tiger Huang721e26f2018-07-24 22:26:19 +08005499 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005500 CancelationOptions
5501 options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005502 "The display which contains this window no longer has focus.",
5503 traceContext.getTracker());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005504 options.displayId = ui::LogicalDisplayId::INVALID;
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005505 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
Tiger Huang721e26f2018-07-24 22:26:19 +08005506 }
5507 mFocusedDisplayId = displayId;
Arpit Singhb65e2bd2024-06-03 09:48:16 +00005508 // Enqueue a command to run outside the lock to tell the policy that the focused display
5509 // changed.
5510 auto command = [this]() REQUIRES(mLock) {
5511 scoped_unlock unlock(mLock);
5512 mPolicy.notifyFocusedDisplayChanged(mFocusedDisplayId);
5513 };
5514 postCommandLocked(std::move(command));
Tiger Huang721e26f2018-07-24 22:26:19 +08005515
Prabir Pradhan9d3d5612024-06-06 20:34:26 +00005516 // Only a window on the focused display can have Pointer Capture, so disable the active
5517 // Pointer Capture session if there is one, since the focused display changed.
5518 disablePointerCaptureForcedLocked();
5519
Chris Ye3c2d6f52020-08-09 10:39:48 -07005520 // Find new focused window and validate
Vishnu Nairc519ff72021-01-21 08:23:08 -08005521 sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00005522 sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken);
Robert Carrf759f162018-11-13 12:57:11 -08005523
Vishnu Nairad321cd2020-08-20 16:40:21 -07005524 if (newFocusedWindowToken == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005525 ALOGW("Focused display #%s does not have a focused window.",
5526 displayId.toString().c_str());
Vishnu Nairc519ff72021-01-21 08:23:08 -08005527 if (mFocusResolver.hasFocusedWindowTokens()) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005528 ALOGE("But another display has a focused window\n%s",
Vishnu Nairc519ff72021-01-21 08:23:08 -08005529 mFocusResolver.dumpFocusedWindows().c_str());
Tiger Huang721e26f2018-07-24 22:26:19 +08005530 }
5531 }
5532 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005533 } // release lock
5534
5535 // Wake up poll loop since it may need to make new input dispatching choices.
5536 mLooper->wake();
5537}
5538
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005540 if (DEBUG_FOCUS) {
5541 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
5542 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
5544 bool changed;
5545 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005546 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547
5548 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
5549 if (mDispatchFrozen && !frozen) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005550 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 }
5552
5553 if (mDispatchEnabled && !enabled) {
5554 resetAndDropEverythingLocked("dispatcher is being disabled");
5555 }
5556
5557 mDispatchEnabled = enabled;
5558 mDispatchFrozen = frozen;
5559 changed = true;
5560 } else {
5561 changed = false;
5562 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 } // release lock
5564
5565 if (changed) {
5566 // Wake up poll loop since it may need to make new input dispatching choices.
5567 mLooper->wake();
5568 }
5569}
5570
5571void InputDispatcher::setInputFilterEnabled(bool enabled) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005572 if (DEBUG_FOCUS) {
5573 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
5574 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575
5576 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005577 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578
5579 if (mInputFilterEnabled == enabled) {
5580 return;
5581 }
5582
5583 mInputFilterEnabled = enabled;
5584 resetAndDropEverythingLocked("input filter is being enabled or disabled");
5585 } // release lock
5586
5587 // Wake up poll loop since there might be work to do to drop everything.
5588 mLooper->wake();
5589}
5590
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005591bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Linnan Li13bf76a2024-05-05 19:18:02 +08005592 bool hasPermission, ui::LogicalDisplayId displayId) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00005593 bool needWake = false;
5594 {
5595 std::scoped_lock lock(mLock);
Antonio Kantek15beb512022-06-13 22:35:41 +00005596 ALOGD_IF(DEBUG_TOUCH_MODE,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005597 "Request to change touch mode to %s (calling pid=%s, uid=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08005598 "hasPermission=%s, target displayId=%s, mTouchModePerDisplay[displayId]=%s)",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005599 toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08005600 toString(hasPermission), displayId.toString().c_str(),
Antonio Kantek15beb512022-06-13 22:35:41 +00005601 mTouchModePerDisplay.count(displayId) == 0
5602 ? "not set"
5603 : std::to_string(mTouchModePerDisplay[displayId]).c_str());
5604
Antonio Kantek15beb512022-06-13 22:35:41 +00005605 auto touchModeIt = mTouchModePerDisplay.find(displayId);
5606 if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08005607 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +00005608 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005609 if (!hasPermission) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005610 if (!focusedWindowIsOwnedByLocked(pid, uid) &&
5611 !recentWindowsAreOwnedByLocked(pid, uid)) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005612 ALOGD("Touch mode switch rejected, caller (pid=%s, uid=%s) doesn't own the focused "
Antonio Kantek48710e42022-03-24 14:19:30 -07005613 "window nor none of the previously interacted window",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005614 pid.toString().c_str(), uid.toString().c_str());
Antonio Kantekea47acb2021-12-23 12:41:25 -08005615 return false;
5616 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00005617 }
Antonio Kantek15beb512022-06-13 22:35:41 +00005618 mTouchModePerDisplay[displayId] = inTouchMode;
5619 auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode,
5620 displayId);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005621 needWake = enqueueInboundEventLocked(std::move(entry));
5622 } // release lock
5623
5624 if (needWake) {
5625 mLooper->wake();
5626 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005627 return true;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -08005628}
5629
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005630bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005631 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
5632 if (focusedToken == nullptr) {
5633 return false;
5634 }
5635 sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken);
5636 return isWindowOwnedBy(windowHandle, pid, uid);
5637}
5638
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005639bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005640 return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(),
5641 [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) {
5642 const sp<WindowInfoHandle> windowHandle =
5643 getWindowHandleLocked(connectionToken);
5644 return isWindowOwnedBy(windowHandle, pid, uid);
5645 }) != mInteractionConnectionTokens.end();
5646}
5647
Bernardo Rufinoea97d182020-08-19 14:43:14 +01005648void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
5649 if (opacity < 0 || opacity > 1) {
5650 LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
5651 return;
5652 }
5653
5654 std::scoped_lock lock(mLock);
5655 mMaximumObscuringOpacityForTouch = opacity;
5656}
5657
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07005658std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005659InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005660 for (auto& [displayId, state] : mTouchStatesByDisplay) {
5661 for (TouchedWindow& w : state.windows) {
5662 if (w.windowHandle->getToken() == token) {
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005663 return std::make_tuple(&state, &w, displayId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005664 }
5665 }
5666 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005667 return std::make_tuple(nullptr, nullptr, ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005668}
5669
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07005670std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
5671InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) const {
5672 return const_cast<InputDispatcher*>(this)->findTouchStateWindowAndDisplayLocked(token);
5673}
5674
5675bool InputDispatcher::windowHasTouchingPointersLocked(const sp<WindowInfoHandle>& windowHandle,
5676 DeviceId deviceId) const {
5677 const auto& [touchState, touchedWindow, _] =
5678 findTouchStateWindowAndDisplayLocked(windowHandle->getToken());
5679 if (touchState == nullptr) {
5680 // No touching pointers at all
5681 return false;
5682 }
5683 return touchState->hasTouchingPointers(deviceId);
5684}
5685
Prabir Pradhan367f3432024-02-13 23:05:58 +00005686bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
5687 bool isDragDrop) {
chaviwfbe5d9c2018-12-26 12:23:37 -08005688 if (fromToken == toToken) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005689 if (DEBUG_FOCUS) {
5690 ALOGD("Trivial transfer to same window.");
5691 }
chaviwfbe5d9c2018-12-26 12:23:37 -08005692 return true;
5693 }
5694
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005696 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
Arthur Hungabbb9d82021-09-01 14:52:30 +00005698 // Find the target touch state and touched window by fromToken.
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005699 auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005700
Arthur Hungabbb9d82021-09-01 14:52:30 +00005701 if (state == nullptr || touchedWindow == nullptr) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005702 ALOGD("Touch transfer failed because from window is not being touched.");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 return false;
5704 }
Linnan Li72352222024-04-12 18:55:57 +08005705 std::set<DeviceId> deviceIds = touchedWindow->getTouchingDeviceIds();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005706 if (deviceIds.size() != 1) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005707 LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds)
5708 << " for window: " << touchedWindow->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005709 return false;
5710 }
Linnan Li72352222024-04-12 18:55:57 +08005711 const DeviceId deviceId = *deviceIds.begin();
Arthur Hungabbb9d82021-09-01 14:52:30 +00005712
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005713 const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle;
5714 const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId);
5715 if (!toWindowHandle) {
5716 ALOGW("Cannot transfer touch because the transfer target window was not found.");
Arthur Hungabbb9d82021-09-01 14:52:30 +00005717 return false;
5718 }
5719
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005720 if (DEBUG_FOCUS) {
Prabir Pradhan367f3432024-02-13 23:05:58 +00005721 ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005722 touchedWindow->windowHandle->getName().c_str(),
5723 toWindowHandle->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 }
5725
Arthur Hungabbb9d82021-09-01 14:52:30 +00005726 // Erase old window.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005727 ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005728 std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005729 state->removeWindowByToken(fromToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730
Arthur Hungabbb9d82021-09-01 14:52:30 +00005731 // Add new window.
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00005732 nsecs_t downTimeInTarget = now();
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005733 ftl::Flags<InputTarget::Flags> newTargetFlags =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005734 oldTargetFlags & (InputTarget::Flags::SPLIT);
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005735 if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005736 newTargetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005737 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00005738 // Transferring touch focus using this API should not effect the focused window.
5739 newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005740 state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005741 deviceId, pointers, downTimeInTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
Arthur Hungabbb9d82021-09-01 14:52:30 +00005743 // Store the dragging window.
5744 if (isDragDrop) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005745 if (pointers.size() != 1) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005746 ALOGW("The drag and drop cannot be started when there is no pointer or more than 1"
5747 " pointer on the window.");
Arthur Hung54745652022-04-20 07:17:41 +00005748 return false;
5749 }
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005750 // Track the pointer id for drag window and generate the drag state.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005751 const size_t id = pointers.begin()->id;
Arthur Hung54745652022-04-20 07:17:41 +00005752 mDragState = std::make_unique<DragState>(toWindowHandle, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753 }
5754
Arthur Hungabbb9d82021-09-01 14:52:30 +00005755 // Synthesize cancel for old window and down for new window.
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005756 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005757 std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken);
5758 std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07005759 if (fromConnection != nullptr && toConnection != nullptr) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08005760 fromConnection->inputState.mergePointerStateTo(toConnection->inputState);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005761 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005762 "transferring touch from this window to another window",
5763 traceContext.getTracker());
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005764 synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005765
5766 // Check if the wallpaper window should deliver the corresponding event.
5767 transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005768 *state, deviceId, pointers, traceContext.getTracker());
Linnan Li72352222024-04-12 18:55:57 +08005769
5770 // Because new window may have a wallpaper window, it will merge input state from it
5771 // parent window, after this the firstNewPointerIdx in input state will be reset, then
5772 // it will cause new move event be thought inconsistent, so we should synthesize the
5773 // down event after it reset.
5774 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection,
5775 newTargetFlags,
5776 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778 } // release lock
5779
5780 // Wake up poll loop since it may need to make new input dispatching choices.
5781 mLooper->wake();
5782 return true;
5783}
5784
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005785/**
5786 * Get the touched foreground window on the given display.
5787 * Return null if there are no windows touched on that display, or if more than one foreground
5788 * window is being touched.
5789 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005790sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(
5791 ui::LogicalDisplayId displayId) const {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005792 auto stateIt = mTouchStatesByDisplay.find(displayId);
5793 if (stateIt == mTouchStatesByDisplay.end()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005794 ALOGI("No touch state on display %s", displayId.toString().c_str());
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005795 return nullptr;
5796 }
5797
5798 const TouchState& state = stateIt->second;
5799 sp<WindowInfoHandle> touchedForegroundWindow;
5800 // If multiple foreground windows are touched, return nullptr
5801 for (const TouchedWindow& window : state.windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005802 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005803 if (touchedForegroundWindow != nullptr) {
5804 ALOGI("Two or more foreground windows: %s and %s",
5805 touchedForegroundWindow->getName().c_str(),
5806 window.windowHandle->getName().c_str());
5807 return nullptr;
5808 }
5809 touchedForegroundWindow = window.windowHandle;
5810 }
5811 }
5812 return touchedForegroundWindow;
5813}
5814
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005815// Binder call
Prabir Pradhan367f3432024-02-13 23:05:58 +00005816bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken,
Linnan Li13bf76a2024-05-05 19:18:02 +08005817 ui::LogicalDisplayId displayId) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005818 sp<IBinder> fromToken;
5819 { // acquire lock
5820 std::scoped_lock _l(mLock);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005821 sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005822 if (toWindowHandle == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005823 ALOGW("Could not find window associated with token=%p on display %s",
5824 destChannelToken.get(), displayId.toString().c_str());
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005825 return false;
5826 }
5827
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005828 sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId);
5829 if (from == nullptr) {
5830 ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get());
5831 return false;
5832 }
5833
5834 fromToken = from->getToken();
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005835 } // release lock
5836
Prabir Pradhan367f3432024-02-13 23:05:58 +00005837 return transferTouchGesture(fromToken, destChannelToken);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005838}
5839
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005841 if (DEBUG_FOCUS) {
5842 ALOGD("Resetting and dropping all events (%s).", reason);
5843 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005845 ScopedSyntheticEventTracer traceContext(mTracer);
5846 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason,
5847 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 synthesizeCancelationEventsForAllConnectionsLocked(options);
5849
5850 resetKeyRepeatLocked();
5851 releasePendingEventLocked();
5852 drainInboundQueueLocked();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005853 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005855 mAnrTracker.clear();
Jeff Brownf086ddb2014-02-11 14:28:48 -08005856 mTouchStatesByDisplay.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857}
5858
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005859void InputDispatcher::logDispatchStateLocked() const {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005860 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861 dumpDispatchStateLocked(dump);
5862
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005863 std::istringstream stream(dump);
5864 std::string line;
5865
5866 while (std::getline(stream, line, '\n')) {
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07005867 ALOGI("%s", line.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868 }
5869}
5870
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005871std::string InputDispatcher::dumpPointerCaptureStateLocked() const {
Prabir Pradhan99987712020-11-10 18:43:05 -08005872 std::string dump;
5873
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005874 dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n",
Hiroki Sato25040232024-02-22 17:21:22 +09005875 toString(mCurrentPointerCaptureRequest.isEnable()));
Prabir Pradhan99987712020-11-10 18:43:05 -08005876
5877 std::string windowName = "None";
5878 if (mWindowTokenWithPointerCapture) {
chaviw98318de2021-05-19 16:45:23 -05005879 const sp<WindowInfoHandle> captureWindowHandle =
Prabir Pradhan99987712020-11-10 18:43:05 -08005880 getWindowHandleLocked(mWindowTokenWithPointerCapture);
5881 windowName = captureWindowHandle ? captureWindowHandle->getName().c_str()
5882 : "token has capture without window";
5883 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005884 dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str());
Prabir Pradhan99987712020-11-10 18:43:05 -08005885
5886 return dump;
5887}
5888
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005889void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
Siarhei Vishniakou043a3ec2019-05-01 11:30:46 -07005890 dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled));
5891 dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen));
5892 dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled));
Linnan Li13bf76a2024-05-05 19:18:02 +08005893 dump += StringPrintf(INDENT "FocusedDisplayId: %s\n", mFocusedDisplayId.toString().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894
Tiger Huang721e26f2018-07-24 22:26:19 +08005895 if (!mFocusedApplicationHandlesByDisplay.empty()) {
5896 dump += StringPrintf(INDENT "FocusedApplications:\n");
5897 for (auto& it : mFocusedApplicationHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005898 const ui::LogicalDisplayId displayId = it.first;
Chris Yea209fde2020-07-22 13:54:51 -07005899 const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second;
Siarhei Vishniakou70622952020-07-30 11:17:23 -05005900 const std::chrono::duration timeout =
5901 applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Linnan Li13bf76a2024-05-05 19:18:02 +08005902 dump += StringPrintf(INDENT2 "displayId=%s, name='%s', dispatchingTimeout=%" PRId64
5903 "ms\n",
5904 displayId.toString().c_str(), applicationHandle->getName().c_str(),
5905 millis(timeout));
Tiger Huang721e26f2018-07-24 22:26:19 +08005906 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 } else {
Tiger Huang721e26f2018-07-24 22:26:19 +08005908 dump += StringPrintf(INDENT "FocusedApplications: <none>\n");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005910
Vishnu Nairc519ff72021-01-21 08:23:08 -08005911 dump += mFocusResolver.dump();
Prabir Pradhan99987712020-11-10 18:43:05 -08005912 dump += dumpPointerCaptureStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913
Siarhei Vishniakou4700f822020-03-24 19:05:54 -07005914 if (!mTouchStatesByDisplay.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005915 dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005916 for (const auto& [displayId, state] : mTouchStatesByDisplay) {
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -08005917 std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
Linnan Li13bf76a2024-05-05 19:18:02 +08005918 dump += INDENT2 + displayId.toString() + " : " + touchStateDump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005919 }
5920 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005921 dump += INDENT "TouchStates: <no displays touched>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 }
5923
arthurhung6d4bed92021-03-17 11:59:33 +08005924 if (mDragState) {
5925 dump += StringPrintf(INDENT "DragState:\n");
5926 mDragState->dump(dump, INDENT2);
5927 }
5928
Arthur Hungb92218b2018-08-14 12:00:21 +08005929 if (!mWindowHandlesByDisplay.empty()) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07005930 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005931 dump += StringPrintf(INDENT "Display: %s\n", displayId.toString().c_str());
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07005932 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
5933 const auto& displayInfo = it->second;
5934 dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth,
5935 displayInfo.logicalHeight);
5936 displayInfo.transform.dump(dump, "transform", INDENT4);
5937 } else {
5938 dump += INDENT2 "No DisplayInfo found!\n";
5939 }
5940
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005941 if (!windowHandles.empty()) {
Arthur Hungb92218b2018-08-14 12:00:21 +08005942 dump += INDENT2 "Windows:\n";
5943 for (size_t i = 0; i < windowHandles.size(); i++) {
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005944 dump += StringPrintf(INDENT3 "%zu: %s", i,
5945 streamableToString(*windowHandles[i]).c_str());
Arthur Hungb92218b2018-08-14 12:00:21 +08005946 }
5947 } else {
5948 dump += INDENT2 "Windows: <none>\n";
5949 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 }
5951 } else {
Arthur Hungb92218b2018-08-14 12:00:21 +08005952 dump += INDENT "Displays: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 }
5954
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005955 if (!mGlobalMonitorsByDisplay.empty()) {
5956 for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005957 dump += StringPrintf(INDENT "Global monitors on display %s:\n",
5958 displayId.toString().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00005959 dumpMonitors(dump, monitors);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07005960 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 } else {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005962 dump += INDENT "Global Monitors: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963 }
5964
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005965 const nsecs_t currentTime = now();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966
5967 // Dump recently dispatched or dropped events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005968 if (!mRecentQueue.empty()) {
5969 dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005970 for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005971 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005972 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005973 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974 }
5975 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005976 dump += INDENT "RecentQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 }
5978
5979 // Dump event currently being dispatched.
5980 if (mPendingEvent) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005981 dump += INDENT "PendingEvent:\n";
5982 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005983 dump += mPendingEvent->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005984 dump += StringPrintf(", age=%" PRId64 "ms\n",
5985 ns2ms(currentTime - mPendingEvent->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005987 dump += INDENT "PendingEvent: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 }
5989
5990 // Dump inbound events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005991 if (!mInboundQueue.empty()) {
5992 dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005993 for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005994 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005995 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005996 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 }
5998 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005999 dump += INDENT "InboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 }
6001
Prabir Pradhancef936d2021-07-21 16:17:52 +00006002 if (!mCommandQueue.empty()) {
6003 dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size());
6004 } else {
6005 dump += INDENT "CommandQueue: <empty>\n";
6006 }
6007
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006008 if (!mConnectionsByToken.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006009 dump += INDENT "Connections:\n";
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006010 for (const auto& [token, connection] : mConnectionsByToken) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006011 dump += StringPrintf(INDENT2 "%i: channelName='%s', "
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006012 "status=%s, monitor=%s, responsive=%s\n",
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006013 connection->inputPublisher.getChannel().getFd(),
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006014 connection->getInputChannelName().c_str(),
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006015 ftl::enum_string(connection->status).c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006016 toString(connection->monitor), toString(connection->responsive));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006018 if (!connection->outboundQueue.empty()) {
6019 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n",
6020 connection->outboundQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006021 dump += dumpQueue(connection->outboundQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 }
6023
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006024 if (!connection->waitQueue.empty()) {
6025 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n",
6026 connection->waitQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006027 dump += dumpQueue(connection->waitQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006029 std::string inputStateDump = streamableToString(connection->inputState);
6030 if (!inputStateDump.empty()) {
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006031 dump += INDENT3 "InputState: ";
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006032 dump += inputStateDump + "\n";
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006033 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 }
6035 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006036 dump += INDENT "Connections: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 }
6038
Antonio Kantek15beb512022-06-13 22:35:41 +00006039 if (!mTouchModePerDisplay.empty()) {
6040 dump += INDENT "TouchModePerDisplay:\n";
6041 for (const auto& [displayId, touchMode] : mTouchModePerDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08006042 dump += StringPrintf(INDENT2 "Display: %s TouchMode: %s\n",
6043 displayId.toString().c_str(), std::to_string(touchMode).c_str());
Antonio Kantek15beb512022-06-13 22:35:41 +00006044 }
6045 } else {
6046 dump += INDENT "TouchModePerDisplay: <none>\n";
6047 }
6048
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006049 dump += INDENT "Configuration:\n";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006050 dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay));
6051 dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n",
6052 ns2ms(mConfig.keyRepeatTimeout));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00006053 dump += mLatencyTracker.dump(INDENT2);
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +00006054 dump += mLatencyAggregator.dump(INDENT2);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00006055 dump += INDENT "InputTracer: ";
6056 dump += mTracer == nullptr ? "Disabled" : "Enabled";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057}
6058
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07006059void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const {
Michael Wright3dd60e22019-03-27 22:06:44 +00006060 const size_t numMonitors = monitors.size();
6061 for (size_t i = 0; i < numMonitors; i++) {
6062 const Monitor& monitor = monitors[i];
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006063 const std::shared_ptr<Connection>& connection = monitor.connection;
6064 dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00006065 dump += "\n";
6066 }
6067}
6068
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006069class LooperEventCallback : public LooperCallback {
6070public:
6071 LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {}
6072 int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); }
6073
6074private:
6075 std::function<int(int events)> mCallback;
6076};
6077
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00006078Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006079 if (DEBUG_CHANNEL_CREATION) {
6080 ALOGD("channel '%s' ~ createInputChannel", name.c_str());
6081 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006083 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006084 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006085 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006086
6087 if (result) {
6088 return base::Error(result) << "Failed to open input channel pair with name " << name;
6089 }
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006092 std::scoped_lock _l(mLock);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006093 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006094 const int fd = serverChannel->getFd();
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006095 std::shared_ptr<Connection> connection =
6096 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false,
6097 mIdGenerator);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006099 auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection);
6100 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006101 ALOGE("Created a new connection, but the token %p is already known", token.get());
6102 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006103
6104 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6105 this, std::placeholders::_1, token);
6106
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006107 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006108 nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 } // release lock
6110
6111 // Wake the looper because some connections have changed.
6112 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006113 return clientChannel;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114}
6115
Linnan Li13bf76a2024-05-05 19:18:02 +08006116Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(
6117 ui::LogicalDisplayId displayId, const std::string& name, gui::Pid pid) {
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006118 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006119 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006120 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006121 if (result) {
6122 return base::Error(result) << "Failed to open input channel pair with name " << name;
6123 }
6124
Michael Wright3dd60e22019-03-27 22:06:44 +00006125 { // acquire lock
6126 std::scoped_lock _l(mLock);
6127
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006128 if (displayId < ui::LogicalDisplayId::DEFAULT) {
Garfield Tan15601662020-09-22 15:32:38 -07006129 return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name
6130 << " without a specified display.";
Michael Wright3dd60e22019-03-27 22:06:44 +00006131 }
6132
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006133 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006134 const int fd = serverChannel->getFd();
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006135 std::shared_ptr<Connection> connection =
6136 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true,
6137 mIdGenerator);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006138
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006139 auto [_, inserted] = mConnectionsByToken.emplace(token, connection);
6140 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006141 ALOGE("Created a new connection, but the token %p is already known", token.get());
6142 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006143
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006144 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6145 this, std::placeholders::_1, token);
Michael Wright3dd60e22019-03-27 22:06:44 +00006146
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006147 mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid);
Michael Wright3dd60e22019-03-27 22:06:44 +00006148
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006149 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006150 nullptr);
Michael Wright3dd60e22019-03-27 22:06:44 +00006151 }
Garfield Tan15601662020-09-22 15:32:38 -07006152
Michael Wright3dd60e22019-03-27 22:06:44 +00006153 // Wake the looper because some connections have changed.
6154 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006155 return clientChannel;
Michael Wright3dd60e22019-03-27 22:06:44 +00006156}
6157
Garfield Tan15601662020-09-22 15:32:38 -07006158status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006160 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161
Harry Cutts33476232023-01-30 19:57:29 +00006162 status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 if (status) {
6164 return status;
6165 }
6166 } // release lock
6167
6168 // Wake the poll loop because removing the connection may have changed the current
6169 // synchronization state.
6170 mLooper->wake();
6171 return OK;
6172}
6173
Garfield Tan15601662020-09-22 15:32:38 -07006174status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken,
6175 bool notify) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006176 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006177 if (connection == nullptr) {
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00006178 // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel'
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 return BAD_VALUE;
6180 }
6181
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006182 removeConnectionLocked(connection);
Robert Carr5c8a0262018-10-03 16:30:44 -07006183
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 if (connection->monitor) {
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006185 removeMonitorChannelLocked(connectionToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186 }
6187
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006188 mLooper->removeFd(connection->inputPublisher.getChannel().getFd());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 nsecs_t currentTime = now();
6191 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
6192
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006193 connection->status = Connection::Status::ZOMBIE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 return OK;
6195}
6196
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006197void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006198 for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) {
6199 auto& [displayId, monitors] = *it;
6200 std::erase_if(monitors, [connectionToken](const Monitor& monitor) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006201 return monitor.connection->getToken() == connectionToken;
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006202 });
Michael Wright3dd60e22019-03-27 22:06:44 +00006203
Michael Wright3dd60e22019-03-27 22:06:44 +00006204 if (monitors.empty()) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006205 it = mGlobalMonitorsByDisplay.erase(it);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006206 } else {
6207 ++it;
6208 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 }
6210}
6211
Michael Wright3dd60e22019-03-27 22:06:44 +00006212status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006213 std::scoped_lock _l(mLock);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006214 return pilferPointersLocked(token);
6215}
Michael Wright3dd60e22019-03-27 22:06:44 +00006216
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006217status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006218 const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token);
6219 if (!requestingConnection) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006220 LOG(WARNING)
6221 << "Attempted to pilfer pointers from an un-registered channel or invalid token";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006222 return BAD_VALUE;
Michael Wright3dd60e22019-03-27 22:06:44 +00006223 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006224
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07006225 auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006226 if (statePtr == nullptr || windowPtr == nullptr) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006227 LOG(WARNING)
6228 << "Attempted to pilfer points from a channel without any on-going pointer streams."
6229 " Ignoring.";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006230 return BAD_VALUE;
6231 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006232 std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07006233 if (deviceIds.empty()) {
6234 LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006235 return BAD_VALUE;
6236 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006237
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006238 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006239 for (const DeviceId deviceId : deviceIds) {
6240 TouchState& state = *statePtr;
6241 TouchedWindow& window = *windowPtr;
6242 // Send cancel events to all the input channels we're stealing from.
6243 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006244 "input channel stole pointer stream", traceContext.getTracker());
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006245 options.deviceId = deviceId;
6246 options.displayId = displayId;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006247 std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId);
6248 std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006249 options.pointerIds = pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006250
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006251 std::string canceledWindows;
6252 for (const TouchedWindow& w : state.windows) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006253 if (w.windowHandle->getToken() != token) {
6254 synthesizeCancelationEventsForWindowLocked(w.windowHandle, options);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006255 canceledWindows += canceledWindows.empty() ? "[" : ", ";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006256 canceledWindows += w.windowHandle->getName();
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006257 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006258 }
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006259 canceledWindows += canceledWindows.empty() ? "[]" : "]";
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006260 LOG(INFO) << "Channel " << requestingConnection->getInputChannelName()
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006261 << " is stealing input gesture for device " << deviceId << " from "
6262 << canceledWindows;
6263
6264 // Prevent the gesture from being sent to any other windows.
6265 // This only blocks relevant pointers to be sent to other windows
6266 window.addPilferingPointers(deviceId, pointerIds);
6267
6268 state.cancelPointersForWindowsExcept(deviceId, pointerIds, token);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006269 }
Michael Wright3dd60e22019-03-27 22:06:44 +00006270 return OK;
6271}
6272
Prabir Pradhan99987712020-11-10 18:43:05 -08006273void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) {
6274 { // acquire lock
6275 std::scoped_lock _l(mLock);
6276 if (DEBUG_FOCUS) {
chaviw98318de2021-05-19 16:45:23 -05006277 const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken);
Prabir Pradhan99987712020-11-10 18:43:05 -08006278 ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable",
6279 windowHandle != nullptr ? windowHandle->getName().c_str()
6280 : "token without window");
6281 }
6282
Vishnu Nairc519ff72021-01-21 08:23:08 -08006283 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08006284 if (focusedToken != windowToken) {
6285 ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.",
6286 enabled ? "enable" : "disable");
6287 return;
6288 }
6289
Hiroki Sato25040232024-02-22 17:21:22 +09006290 if (enabled == mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006291 ALOGW("Ignoring request to %s Pointer Capture: "
6292 "window has %s requested pointer capture.",
6293 enabled ? "enable" : "disable", enabled ? "already" : "not");
6294 return;
6295 }
6296
Christine Franksb768bb42021-11-29 12:11:31 -08006297 if (enabled) {
6298 if (std::find(mIneligibleDisplaysForPointerCapture.begin(),
6299 mIneligibleDisplaysForPointerCapture.end(),
6300 mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) {
6301 ALOGW("Ignoring request to enable Pointer Capture: display is not eligible");
6302 return;
6303 }
6304 }
6305
Hiroki Sato25040232024-02-22 17:21:22 +09006306 setPointerCaptureLocked(enabled ? windowToken : nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08006307 } // release lock
6308
6309 // Wake the thread to process command entries.
6310 mLooper->wake();
6311}
6312
Linnan Li13bf76a2024-05-05 19:18:02 +08006313void InputDispatcher::setDisplayEligibilityForPointerCapture(ui::LogicalDisplayId displayId,
6314 bool isEligible) {
Christine Franksb768bb42021-11-29 12:11:31 -08006315 { // acquire lock
6316 std::scoped_lock _l(mLock);
6317 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
6318 if (!isEligible) {
6319 mIneligibleDisplaysForPointerCapture.push_back(displayId);
6320 }
6321 } // release lock
6322}
6323
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006324std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006325 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00006326 for (const Monitor& monitor : monitors) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006327 if (monitor.connection->getToken() == token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006328 return monitor.pid;
Michael Wright3dd60e22019-03-27 22:06:44 +00006329 }
6330 }
6331 }
6332 return std::nullopt;
6333}
6334
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006335std::shared_ptr<Connection> InputDispatcher::getConnectionLocked(
6336 const sp<IBinder>& inputConnectionToken) const {
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -07006337 if (inputConnectionToken == nullptr) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006338 return nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +08006339 }
6340
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006341 for (const auto& [token, connection] : mConnectionsByToken) {
6342 if (token == inputConnectionToken) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006343 return connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 }
6345 }
Robert Carr4e670e52018-08-15 13:26:12 -07006346
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006347 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348}
6349
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006350std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006351 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006352 if (connection == nullptr) {
6353 return "<nullptr>";
6354 }
6355 return connection->getInputChannelName();
6356}
6357
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006358void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006359 mAnrTracker.eraseToken(connection->getToken());
6360 mConnectionsByToken.erase(connection->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006361}
6362
Prabir Pradhancef936d2021-07-21 16:17:52 +00006363void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006364 const std::shared_ptr<Connection>& connection,
6365 uint32_t seq, bool handled,
6366 nsecs_t consumeTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006367 // Handle post-event policy actions.
Prabir Pradhan24047542023-11-02 17:14:59 +00006368 std::unique_ptr<const KeyEntry> fallbackKeyEntry;
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006369
6370 { // Start critical section
6371 auto dispatchEntryIt =
6372 std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6373 [seq](auto& e) { return e->seq == seq; });
6374 if (dispatchEntryIt == connection->waitQueue.end()) {
6375 return;
6376 }
6377
6378 DispatchEntry& dispatchEntry = **dispatchEntryIt;
6379
6380 const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime;
6381 if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006382 ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006383 ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str());
6384 }
6385 if (shouldReportFinishedEvent(dispatchEntry, *connection)) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006386 mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006387 dispatchEntry.deliveryTime, consumeTime, finishTime);
6388 }
6389
6390 if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006391 fallbackKeyEntry =
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006392 afterKeyEventLockedInterruptable(connection, &dispatchEntry, handled);
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006393 }
6394 } // End critical section: The -LockedInterruptable methods may have released the lock.
Prabir Pradhancef936d2021-07-21 16:17:52 +00006395
6396 // Dequeue the event and start the next cycle.
6397 // Because the lock might have been released, it is possible that the
6398 // contents of the wait queue to have been drained, so we need to double-check
6399 // a few things.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006400 auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6401 [seq](auto& e) { return e->seq == seq; });
6402 if (entryIt != connection->waitQueue.end()) {
6403 std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt);
6404 connection->waitQueue.erase(entryIt);
6405
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006406 const sp<IBinder>& connectionToken = connection->getToken();
Prabir Pradhancef936d2021-07-21 16:17:52 +00006407 mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken);
6408 if (!connection->responsive) {
6409 connection->responsive = isConnectionResponsive(*connection);
6410 if (connection->responsive) {
6411 // The connection was unresponsive, and now it's responsive.
6412 processConnectionResponsiveLocked(*connection);
6413 }
6414 }
6415 traceWaitQueueLength(*connection);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006416 if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006417 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6418 // Only dispatch fallbacks if there is a window for the connection.
6419 if (windowHandle != nullptr) {
6420 const auto inputTarget =
6421 createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
6422 dispatchEntry->targetFlags,
6423 fallbackKeyEntry->downTime);
6424 if (inputTarget.has_value()) {
6425 enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry),
6426 *inputTarget);
6427 }
6428 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00006429 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006430 releaseDispatchEntry(std::move(dispatchEntry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00006431 }
6432
6433 // Start the next dispatch cycle for this connection.
6434 startDispatchCycleLocked(now(), connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435}
6436
Prabir Pradhancef936d2021-07-21 16:17:52 +00006437void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken,
6438 const sp<IBinder>& newToken) {
6439 auto command = [this, oldToken, newToken]() REQUIRES(mLock) {
6440 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006441 mPolicy.notifyFocusChanged(oldToken, newToken);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006442 };
6443 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444}
6445
Prabir Pradhancef936d2021-07-21 16:17:52 +00006446void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) {
6447 auto command = [this, token, x, y]() REQUIRES(mLock) {
6448 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006449 mPolicy.notifyDropWindow(token, x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006450 };
6451 postCommandLocked(std::move(command));
Robert Carrf759f162018-11-13 12:57:11 -08006452}
6453
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006454void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006455 if (connection == nullptr) {
6456 LOG_ALWAYS_FATAL("Caller must check for nullness");
6457 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006458 // Since we are allowing the policy to extend the timeout, maybe the waitQueue
6459 // is already healthy again. Don't raise ANR in this situation
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006460 if (connection->waitQueue.empty()) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006461 ALOGI("Not raising ANR because the connection %s has recovered",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006462 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006463 return;
6464 }
6465 /**
6466 * The "oldestEntry" is the entry that was first sent to the application. That entry, however,
6467 * may not be the one that caused the timeout to occur. One possibility is that window timeout
6468 * has changed. This could cause newer entries to time out before the already dispatched
6469 * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app
6470 * processes the events linearly. So providing information about the oldest entry seems to be
6471 * most useful.
6472 */
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006473 DispatchEntry& oldestEntry = *connection->waitQueue.front();
6474 const nsecs_t currentWait = now() - oldestEntry.deliveryTime;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006475 std::string reason =
6476 android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006477 connection->getInputChannelName().c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006478 ns2ms(currentWait),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006479 oldestEntry.eventEntry->getDescription().c_str());
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006480 sp<IBinder> connectionToken = connection->getToken();
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -06006481 updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006482
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006483 processConnectionUnresponsiveLocked(*connection, std::move(reason));
6484
6485 // Stop waking up for events on this connection, it is already unresponsive
6486 cancelEventsForAnrLocked(connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006487}
6488
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006489void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) {
6490 std::string reason =
6491 StringPrintf("%s does not have a focused window", application->getName().c_str());
6492 updateLastAnrStateLocked(*application, reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006493
Yabin Cui8eb9c552023-06-08 18:05:07 +00006494 auto command = [this, app = std::move(application)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006495 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006496 mPolicy.notifyNoFocusedWindowAnr(app);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006497 };
6498 postCommandLocked(std::move(command));
Bernardo Rufino2e1f6512020-10-08 13:42:07 +00006499}
6500
chaviw98318de2021-05-19 16:45:23 -05006501void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006502 const std::string& reason) {
6503 const std::string windowLabel = getApplicationWindowLabel(nullptr, window);
6504 updateLastAnrStateLocked(windowLabel, reason);
6505}
6506
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006507void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application,
6508 const std::string& reason) {
6509 const std::string windowLabel = getApplicationWindowLabel(&application, nullptr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006510 updateLastAnrStateLocked(windowLabel, reason);
6511}
6512
6513void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
6514 const std::string& reason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 // Capture a record of the InputDispatcher state at the time of the ANR.
Yi Kong9b14ac62018-07-17 13:48:38 -07006516 time_t t = time(nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 struct tm tm;
6518 localtime_r(&t, &tm);
6519 char timestr[64];
6520 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006521 mLastAnrState.clear();
6522 mLastAnrState += INDENT "ANR:\n";
6523 mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006524 mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str());
6525 mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str());
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006526 dumpDispatchStateLocked(mLastAnrState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527}
6528
Prabir Pradhancef936d2021-07-21 16:17:52 +00006529void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
Prabir Pradhan24047542023-11-02 17:14:59 +00006530 const KeyEntry& entry) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006531 const KeyEvent event = createKeyEvent(entry);
6532 nsecs_t delay = 0;
6533 { // release lock
6534 scoped_unlock unlock(mLock);
6535 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00006536 delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006537 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
6538 ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
6539 std::to_string(t.duration().count()).c_str());
6540 }
6541 } // acquire lock
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542
6543 if (delay < 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006544 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
Prabir Pradhancef936d2021-07-21 16:17:52 +00006545 } else if (delay == 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006546 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00006548 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006549 entry.interceptKeyWakeupTime = now() + delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551}
6552
Prabir Pradhancef936d2021-07-21 16:17:52 +00006553void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006554 std::optional<gui::Pid> pid,
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006555 std::string reason) {
Yabin Cui8eb9c552023-06-08 18:05:07 +00006556 auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006557 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006558 mPolicy.notifyWindowUnresponsive(token, pid, r);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006559 };
6560 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006561}
6562
Prabir Pradhanedd96402022-02-15 01:46:16 -08006563void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006564 std::optional<gui::Pid> pid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006565 auto command = [this, token, pid]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006566 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006567 mPolicy.notifyWindowResponsive(token, pid);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006568 };
6569 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006570}
6571
6572/**
6573 * Tell the policy that a connection has become unresponsive so that it can start ANR.
6574 * Check whether the connection of interest is a monitor or a window, and add the corresponding
6575 * command entry to the command queue.
6576 */
6577void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection,
6578 std::string reason) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006579 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006580 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006581 if (connection.monitor) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006582 ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006583 reason.c_str());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006584 pid = findMonitorPidByTokenLocked(connectionToken);
6585 } else {
6586 // The connection is a window
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006587 ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Prabir Pradhanedd96402022-02-15 01:46:16 -08006588 reason.c_str());
6589 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6590 if (handle != nullptr) {
6591 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006592 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006593 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006594 sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006595}
6596
6597/**
6598 * Tell the policy that a connection has become responsive so that it can stop ANR.
6599 */
6600void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006601 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006602 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006603 if (connection.monitor) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006604 pid = findMonitorPidByTokenLocked(connectionToken);
6605 } else {
6606 // The connection is a window
6607 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6608 if (handle != nullptr) {
6609 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006610 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006611 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006612 sendWindowResponsiveCommandLocked(connectionToken, pid);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006613}
6614
Prabir Pradhan24047542023-11-02 17:14:59 +00006615std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable(
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006616 const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, bool handled) {
6617 // The dispatchEntry is currently valid, but it might point to a deleted object after we release
6618 // the lock. For simplicity, make copies of the data of interest here and assume that
6619 // 'dispatchEntry' is not valid after this section.
6620 // Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
6621 // function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
6622 std::shared_ptr<const EventEntry> eventEntry = dispatchEntry->eventEntry;
6623 const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget();
6624 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(eventEntry));
6625 // To prevent misuse, ensure dispatchEntry is no longer valid.
6626 dispatchEntry = nullptr;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006627 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006628 if (!handled) {
6629 // Report the key as unhandled, since the fallback was not handled.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006630 mReporter->reportUnhandledKey(keyEntry.id);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006631 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006632 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006633 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006635 // Get the fallback key state.
6636 // Clear it out after dispatching the UP.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006637 int32_t originalKeyCode = keyEntry.keyCode;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006638 std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006639 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006640 connection->inputState.removeFallbackKey(originalKeyCode);
6641 }
6642
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006643 if (handled || !hasForegroundTarget) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006644 // If the application handles the original key for which we previously
6645 // generated a fallback or if the window is not a foreground window,
6646 // then cancel the associated fallback key, if any.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006647 if (fallbackKeyCode) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006648 // Dispatch the unhandled key to the policy with the cancel flag.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006649 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6650 ALOGD("Unhandled key event: Asking policy to cancel fallback action. "
6651 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6652 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount,
6653 keyEntry.policyFlags);
6654 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006655 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006656 event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006657
6658 mLock.unlock();
6659
Prabir Pradhana41d2442023-04-20 21:30:40 +00006660 if (const auto unhandledKeyFallback =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006661 mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6662 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006663 unhandledKeyFallback) {
6664 event = *unhandledKeyFallback;
6665 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006666
6667 mLock.lock();
6668
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006669 // Cancel the fallback key, but only if we still have a window for the channel.
6670 // It could have been removed during the policy call.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006671 if (*fallbackKeyCode != AKEYCODE_UNKNOWN) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006672 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6673 if (windowHandle != nullptr) {
6674 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
6675 "application handled the original non-fallback key "
6676 "or is no longer a foreground target, "
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006677 "canceling previously dispatched fallback key",
6678 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006679 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006680 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006681 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006683 connection->inputState.removeFallbackKey(originalKeyCode);
6684 }
6685 } else {
6686 // If the application did not handle a non-fallback key, first check
6687 // that we are in a good state to perform unhandled key event processing
6688 // Then ask the policy what to do with it.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006689 bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006690 if (!fallbackKeyCode && !initialDown) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006691 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6692 ALOGD("Unhandled key event: Skipping unhandled key event processing "
6693 "since this is not an initial down. "
6694 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6695 originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6696 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006697 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006698 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006699
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006700 // Dispatch the unhandled key to the policy.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006701 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6702 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
6703 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6704 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6705 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006706 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006707
6708 mLock.unlock();
6709
Prabir Pradhana41d2442023-04-20 21:30:40 +00006710 bool fallback = false;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006711 if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6712 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006713 fb) {
6714 fallback = true;
6715 event = *fb;
6716 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006717
6718 mLock.lock();
6719
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006720 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006721 connection->inputState.removeFallbackKey(originalKeyCode);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006722 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006723 }
6724
6725 // Latch the fallback keycode for this key on an initial down.
6726 // The fallback keycode cannot change at any other point in the lifecycle.
6727 if (initialDown) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 if (fallback) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006729 *fallbackKeyCode = event.getKeyCode();
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006730 } else {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006731 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006732 }
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006733 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006734 }
6735
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006736 ALOG_ASSERT(fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006737
6738 // Cancel the fallback key if the policy decides not to send it anymore.
6739 // We will continue to dispatch the key to the policy but we will no
6740 // longer dispatch a fallback key to the application.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006741 if (*fallbackKeyCode != AKEYCODE_UNKNOWN &&
6742 (!fallback || *fallbackKeyCode != event.getKeyCode())) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006743 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6744 if (fallback) {
6745 ALOGD("Unhandled key event: Policy requested to send key %d"
6746 "as a fallback for %d, but on the DOWN it had requested "
6747 "to send %d instead. Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006748 event.getKeyCode(), originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006749 } else {
6750 ALOGD("Unhandled key event: Policy did not request fallback for %d, "
6751 "but on the DOWN it had requested to send %d. "
6752 "Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006753 originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006754 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006755 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006756
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006757 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6758 if (windowHandle != nullptr) {
6759 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006760 "canceling fallback, policy no longer desires it",
6761 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006762 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006763 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006764 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006765
6766 fallback = false;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006767 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006768 if (keyEntry.action != AKEY_EVENT_ACTION_UP) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006769 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006770 }
6771 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006773 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6774 {
6775 std::string msg;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006776 const std::map<int32_t, int32_t>& fallbackKeys =
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006777 connection->inputState.getFallbackKeys();
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006778 for (const auto& [key, value] : fallbackKeys) {
6779 msg += StringPrintf(", %d->%d", key, value);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006780 }
6781 ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.",
6782 fallbackKeys.size(), msg.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006783 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006784 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006785
6786 if (fallback) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006787 // Return the fallback key that we want dispatched to the channel.
6788 std::unique_ptr<KeyEntry> newEntry =
6789 std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState,
6790 event.getEventTime(), event.getDeviceId(),
6791 event.getSource(), event.getDisplayId(),
6792 keyEntry.policyFlags, keyEntry.action,
6793 event.getFlags() | AKEY_EVENT_FLAG_FALLBACK,
6794 *fallbackKeyCode, event.getScanCode(),
6795 event.getMetaState(), event.getRepeatCount(),
6796 event.getDownTime());
Prabir Pradhana67623c2024-02-21 06:57:36 +00006797 if (mTracer) {
6798 newEntry->traceTracker =
6799 mTracer->traceDerivedEvent(*newEntry, *keyEntry.traceTracker);
6800 }
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006801 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6802 ALOGD("Unhandled key event: Dispatching fallback key. "
6803 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006804 originalKeyCode, *fallbackKeyCode, keyEntry.metaState);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006805 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006806 return newEntry;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006807 } else {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006808 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6809 ALOGD("Unhandled key event: No fallback key.");
6810 }
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006811
6812 // Report the key as unhandled, since there is no fallback key.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006813 mReporter->reportUnhandledKey(keyEntry.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006814 }
6815 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006816 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08006817}
6818
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819void InputDispatcher::traceInboundQueueLengthLocked() {
6820 if (ATRACE_ENABLED()) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006821 ATRACE_INT("iq", mInboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822 }
6823}
6824
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006825void InputDispatcher::traceOutboundQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826 if (ATRACE_ENABLED()) {
6827 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006828 snprintf(counterName, sizeof(counterName), "oq:%s",
6829 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006830 ATRACE_INT(counterName, connection.outboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831 }
6832}
6833
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006834void InputDispatcher::traceWaitQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 if (ATRACE_ENABLED()) {
6836 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006837 snprintf(counterName, sizeof(counterName), "wq:%s",
6838 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006839 ATRACE_INT(counterName, connection.waitQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006840 }
6841}
6842
Siarhei Vishniakou5e20f272023-06-08 17:24:44 -07006843void InputDispatcher::dump(std::string& dump) const {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006844 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006846 dump += "Input Dispatcher State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 dumpDispatchStateLocked(dump);
6848
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006849 if (!mLastAnrState.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006850 dump += "\nInput Dispatcher State at time of last ANR:\n";
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006851 dump += mLastAnrState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852 }
6853}
6854
6855void InputDispatcher::monitor() {
6856 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006857 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006858 mLooper->wake();
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006859 mDispatcherIsAlive.wait(_l);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860}
6861
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006862/**
6863 * Wake up the dispatcher and wait until it processes all events and commands.
6864 * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so
6865 * this method can be safely called from any thread, as long as you've ensured that
6866 * the work you are interested in completing has already been queued.
6867 */
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07006868bool InputDispatcher::waitForIdle() const {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006869 /**
6870 * Timeout should represent the longest possible time that a device might spend processing
6871 * events and commands.
6872 */
6873 constexpr std::chrono::duration TIMEOUT = 100ms;
6874 std::unique_lock lock(mLock);
6875 mLooper->wake();
6876 std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT);
6877 return result == std::cv_status::no_timeout;
6878}
6879
Vishnu Naire798b472020-07-23 13:52:21 -07006880/**
6881 * Sets focus to the window identified by the token. This must be called
6882 * after updating any input window handles.
6883 *
6884 * Params:
6885 * request.token - input channel token used to identify the window that should gain focus.
6886 * request.focusedToken - the token that the caller expects currently to be focused. If the
6887 * specified token does not match the currently focused window, this request will be dropped.
6888 * If the specified focused token matches the currently focused window, the call will succeed.
6889 * Set this to "null" if this call should succeed no matter what the currently focused token is.
6890 * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm)
6891 * when requesting the focus change. This determines which request gets
6892 * precedence if there is a focus change request from another source such as pointer down.
6893 */
Vishnu Nair958da932020-08-21 17:12:37 -07006894void InputDispatcher::setFocusedWindow(const FocusRequest& request) {
6895 { // acquire lock
6896 std::scoped_lock _l(mLock);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006897 std::optional<FocusResolver::FocusChanges> changes =
Linnan Li13bf76a2024-05-05 19:18:02 +08006898 mFocusResolver.setFocusedWindow(request,
6899 getWindowHandlesLocked(
6900 ui::LogicalDisplayId{request.displayId}));
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006901 ScopedSyntheticEventTracer traceContext(mTracer);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006902 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006903 onFocusChangedLocked(*changes, traceContext.getTracker());
Vishnu Nair958da932020-08-21 17:12:37 -07006904 }
6905 } // release lock
6906 // Wake up poll loop since it may need to make new input dispatching choices.
6907 mLooper->wake();
6908}
6909
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006910void InputDispatcher::onFocusChangedLocked(
6911 const FocusResolver::FocusChanges& changes,
6912 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker,
6913 const sp<WindowInfoHandle> removedFocusedWindowHandle) {
Vishnu Nairc519ff72021-01-21 08:23:08 -08006914 if (changes.oldFocus) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006915 const auto resolvedWindow = removedFocusedWindowHandle != nullptr
6916 ? removedFocusedWindowHandle
6917 : getWindowHandleLocked(changes.oldFocus, changes.displayId);
6918 if (resolvedWindow == nullptr) {
6919 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006920 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006921 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006922 "focus left window", traceTracker);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006923 synthesizeCancelationEventsForWindowLocked(resolvedWindow, options);
6924 enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006925 }
Vishnu Nairc519ff72021-01-21 08:23:08 -08006926 if (changes.newFocus) {
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07006927 resetNoFocusedWindowTimeoutLocked();
Harry Cutts33476232023-01-30 19:57:29 +00006928 enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006929 }
6930
Vishnu Nairc519ff72021-01-21 08:23:08 -08006931 if (mFocusedDisplayId == changes.displayId) {
Prabir Pradhan9d3d5612024-06-06 20:34:26 +00006932 // If a window has pointer capture, then it must have focus and must be on the top-focused
6933 // display. We need to ensure that this contract is upheld when pointer capture is being
6934 // disabled due to a loss of window focus. If the window loses focus before it loses pointer
6935 // capture, then the window can be in a state where it has pointer capture but not focus,
6936 // violating the contract. Therefore we must dispatch the pointer capture event before the
6937 // focus event. Since focus events are added to the front of the queue (above), we add the
6938 // pointer capture event to the front of the queue after the focus events are added. This
6939 // ensures the pointer capture event ends up at the front.
6940 disablePointerCaptureForcedLocked();
6941
Prabir Pradhancef936d2021-07-21 16:17:52 +00006942 sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006943 }
6944}
Vishnu Nair958da932020-08-21 17:12:37 -07006945
Prabir Pradhan99987712020-11-10 18:43:05 -08006946void InputDispatcher::disablePointerCaptureForcedLocked() {
Hiroki Sato25040232024-02-22 17:21:22 +09006947 if (!mCurrentPointerCaptureRequest.isEnable() && !mWindowTokenWithPointerCapture) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006948 return;
6949 }
6950
6951 ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus.");
6952
Hiroki Sato25040232024-02-22 17:21:22 +09006953 if (mCurrentPointerCaptureRequest.isEnable()) {
6954 setPointerCaptureLocked(nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08006955 }
6956
6957 if (!mWindowTokenWithPointerCapture) {
6958 // No need to send capture changes because no window has capture.
6959 return;
6960 }
6961
6962 if (mPendingEvent != nullptr) {
6963 // Move the pending event to the front of the queue. This will give the chance
6964 // for the pending event to be dropped if it is a captured event.
6965 mInboundQueue.push_front(mPendingEvent);
6966 mPendingEvent = nullptr;
6967 }
6968
6969 auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006970 mCurrentPointerCaptureRequest);
Prabir Pradhan99987712020-11-10 18:43:05 -08006971 mInboundQueue.push_front(std::move(entry));
6972}
6973
Hiroki Sato25040232024-02-22 17:21:22 +09006974void InputDispatcher::setPointerCaptureLocked(const sp<IBinder>& windowToken) {
6975 mCurrentPointerCaptureRequest.window = windowToken;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006976 mCurrentPointerCaptureRequest.seq++;
6977 auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006978 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006979 mPolicy.setPointerCapture(request);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006980 };
6981 postCommandLocked(std::move(command));
Prabir Pradhan99987712020-11-10 18:43:05 -08006982}
6983
Linnan Li13bf76a2024-05-05 19:18:02 +08006984void InputDispatcher::displayRemoved(ui::LogicalDisplayId displayId) {
Vishnu Nair599f1412021-06-21 10:39:58 -07006985 { // acquire lock
6986 std::scoped_lock _l(mLock);
6987 // Set an empty list to remove all handles from the specific display.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006988 setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006989 setFocusedApplicationLocked(displayId, nullptr);
6990 // Call focus resolver to clean up stale requests. This must be called after input windows
6991 // have been removed for the removed display.
6992 mFocusResolver.displayRemoved(displayId);
Christine Franksb768bb42021-11-29 12:11:31 -08006993 // Reset pointer capture eligibility, regardless of previous state.
6994 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
Antonio Kantek15beb512022-06-13 22:35:41 +00006995 // Remove the associated touch mode state.
6996 mTouchModePerDisplay.erase(displayId);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07006997 mVerifiersByDisplay.erase(displayId);
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00006998 mInputFilterVerifiersByDisplay.erase(displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006999 } // release lock
7000
7001 // Wake up poll loop since it may need to make new input dispatching choices.
7002 mLooper->wake();
7003}
7004
Patrick Williamsd828f302023-04-28 17:52:08 -05007005void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) {
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08007006 if (auto result = validateWindowInfosUpdate(update); !result.ok()) {
7007 {
7008 // acquire lock
7009 std::scoped_lock _l(mLock);
7010 logDispatchStateLocked();
7011 }
7012 LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s",
7013 result.error().message().c_str());
7014 };
chaviw15fab6f2021-06-07 14:15:52 -05007015 // The listener sends the windows as a flattened array. Separate the windows by display for
7016 // more convenient parsing.
Linnan Li13bf76a2024-05-05 19:18:02 +08007017 std::unordered_map<ui::LogicalDisplayId, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
Patrick Williamsd828f302023-04-28 17:52:08 -05007018 for (const auto& info : update.windowInfos) {
chaviw15fab6f2021-06-07 14:15:52 -05007019 handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007020 handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info));
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07007021 if (input_flags::split_all_touches()) {
7022 handlesPerDisplay[info.displayId]
7023 .back()
7024 ->editInfo()
7025 ->setInputConfig(android::gui::WindowInfo::InputConfig::PREVENT_SPLITTING,
7026 false);
7027 }
chaviw15fab6f2021-06-07 14:15:52 -05007028 }
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007029
7030 { // acquire lock
7031 std::scoped_lock _l(mLock);
Prabir Pradhan814fe082022-07-22 20:22:18 +00007032
7033 // Ensure that we have an entry created for all existing displays so that if a displayId has
7034 // no windows, we can tell that the windows were removed from the display.
7035 for (const auto& [displayId, _] : mWindowHandlesByDisplay) {
7036 handlesPerDisplay[displayId];
7037 }
7038
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007039 mDisplayInfos.clear();
Patrick Williamsd828f302023-04-28 17:52:08 -05007040 for (const auto& displayInfo : update.displayInfos) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007041 mDisplayInfos.emplace(displayInfo.displayId, displayInfo);
7042 }
7043
7044 for (const auto& [displayId, handles] : handlesPerDisplay) {
7045 setInputWindowsLocked(handles, displayId);
7046 }
Patrick Williams9464b2c2023-05-23 11:22:04 -05007047
7048 if (update.vsyncId < mWindowInfosVsyncId) {
7049 ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64
7050 ", current update vsync id: %" PRId64,
7051 mWindowInfosVsyncId, update.vsyncId);
7052 }
7053 mWindowInfosVsyncId = update.vsyncId;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007054 }
7055 // Wake up poll loop since it may need to make new input dispatching choices.
7056 mLooper->wake();
chaviw15fab6f2021-06-07 14:15:52 -05007057}
7058
Vishnu Nair062a8672021-09-03 16:07:44 -07007059bool InputDispatcher::shouldDropInput(
7060 const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007061 if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) ||
7062 (windowHandle->getInfo()->inputConfig.test(
7063 WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) &&
Vishnu Nair062a8672021-09-03 16:07:44 -07007064 isWindowObscuredLocked(windowHandle))) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007065 ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on "
Linnan Li13bf76a2024-05-05 19:18:02 +08007066 "display %s.",
Vishnu Nair062a8672021-09-03 16:07:44 -07007067 ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(),
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007068 windowHandle->getInfo()->inputConfig.string().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08007069 windowHandle->getInfo()->displayId.toString().c_str());
Vishnu Nair062a8672021-09-03 16:07:44 -07007070 return true;
7071 }
7072 return false;
7073}
7074
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007075void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -05007076 const gui::WindowInfosUpdate& update) {
7077 mDispatcher.onWindowInfosChanged(update);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007078}
7079
Arthur Hungdfd528e2021-12-08 13:23:04 +00007080void InputDispatcher::cancelCurrentTouch() {
7081 {
7082 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007083 ScopedSyntheticEventTracer traceContext(mTracer);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007084 ALOGD("Canceling all ongoing pointer gestures on all displays.");
Michael Wrightfb04fd52022-11-24 22:31:11 +00007085 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007086 "cancel current touch", traceContext.getTracker());
Arthur Hungdfd528e2021-12-08 13:23:04 +00007087 synthesizeCancelationEventsForAllConnectionsLocked(options);
7088
7089 mTouchStatesByDisplay.clear();
Arthur Hungdfd528e2021-12-08 13:23:04 +00007090 }
7091 // Wake up poll loop since there might be work to do.
7092 mLooper->wake();
7093}
7094
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007095void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) {
7096 std::scoped_lock _l(mLock);
7097 mMonitorDispatchingTimeout = timeout;
7098}
7099
Arthur Hungc539dbb2022-12-08 07:45:36 +00007100void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags,
7101 const sp<WindowInfoHandle>& oldWindowHandle,
7102 const sp<WindowInfoHandle>& newWindowHandle,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007103 TouchState& state, const MotionEntry& entry,
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07007104 std::vector<InputTarget>& targets) const {
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007105 LOG_IF(FATAL, entry.getPointerCount() != 1) << "Entry not eligible for slip: " << entry;
7106 const DeviceId deviceId = entry.deviceId;
7107 const PointerProperties& pointerProperties = entry.pointerProperties[0];
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007108 std::vector<PointerProperties> pointers{pointerProperties};
Arthur Hungc539dbb2022-12-08 07:45:36 +00007109 const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test(
7110 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7111 const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) &&
7112 newWindowHandle->getInfo()->inputConfig.test(
7113 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7114 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007115 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007116 const sp<WindowInfoHandle> newWallpaper =
7117 newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr;
7118 if (oldWallpaper == newWallpaper) {
7119 return;
7120 }
7121
7122 if (oldWallpaper != nullptr) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08007123 const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007124 addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007125 oldTouchedWindow.targetFlags, getPointerIds(pointers),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007126 oldTouchedWindow.getDownTimeInTarget(deviceId), targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007127 state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007128 }
7129
7130 if (newWallpaper != nullptr) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007131 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER,
7132 InputTarget::Flags::WINDOW_IS_OBSCURED |
Arthur Hungc539dbb2022-12-08 07:45:36 +00007133 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007134 deviceId, pointers, entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007135 }
7136}
7137
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007138void InputDispatcher::transferWallpaperTouch(
7139 ftl::Flags<InputTarget::Flags> oldTargetFlags,
7140 ftl::Flags<InputTarget::Flags> newTargetFlags, const sp<WindowInfoHandle> fromWindowHandle,
Linnan Li72352222024-04-12 18:55:57 +08007141 const sp<WindowInfoHandle> toWindowHandle, TouchState& state, DeviceId deviceId,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007142 const std::vector<PointerProperties>& pointers,
7143 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Arthur Hungc539dbb2022-12-08 07:45:36 +00007144 const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7145 fromWindowHandle->getInfo()->inputConfig.test(
7146 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7147 const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7148 toWindowHandle->getInfo()->inputConfig.test(
7149 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7150
7151 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007152 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007153 const sp<WindowInfoHandle> newWallpaper =
7154 newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr;
7155 if (oldWallpaper == newWallpaper) {
7156 return;
7157 }
7158
7159 if (oldWallpaper != nullptr) {
7160 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007161 "transferring touch focus to another window", traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007162 state.removeWindowByToken(oldWallpaper->getToken());
7163 synthesizeCancelationEventsForWindowLocked(oldWallpaper, options);
7164 }
7165
7166 if (newWallpaper != nullptr) {
7167 nsecs_t downTimeInTarget = now();
Prabir Pradhan65455c72024-02-13 21:46:41 +00007168 ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags;
7169 wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007170 wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED |
7171 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007172 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007173 deviceId, pointers, downTimeInTarget);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007174 std::shared_ptr<Connection> wallpaperConnection =
7175 getConnectionLocked(newWallpaper->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007176 if (wallpaperConnection != nullptr) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007177 std::shared_ptr<Connection> toConnection =
7178 getConnectionLocked(toWindowHandle->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007179 toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState);
7180 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007181 wallpaperFlags, traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007182 }
7183 }
7184}
7185
7186sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow(
7187 const sp<WindowInfoHandle>& windowHandle) const {
7188 const std::vector<sp<WindowInfoHandle>>& windowHandles =
7189 getWindowHandlesLocked(windowHandle->getInfo()->displayId);
7190 bool foundWindow = false;
7191 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
7192 if (!foundWindow && otherHandle != windowHandle) {
7193 continue;
7194 }
7195 if (windowHandle == otherHandle) {
7196 foundWindow = true;
7197 continue;
7198 }
7199
7200 if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) {
7201 return otherHandle;
7202 }
7203 }
7204 return nullptr;
7205}
7206
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007207void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout,
7208 std::chrono::nanoseconds delay) {
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007209 std::scoped_lock _l(mLock);
7210
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007211 mConfig.keyRepeatTimeout = timeout.count();
7212 mConfig.keyRepeatDelay = delay.count();
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007213}
7214
Linnan Li13bf76a2024-05-05 19:18:02 +08007215bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token,
7216 ui::LogicalDisplayId displayId, DeviceId deviceId,
7217 int32_t pointerId) {
Prabir Pradhan64f21d22023-11-28 21:19:42 +00007218 std::scoped_lock _l(mLock);
7219 auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7220 if (touchStateIt == mTouchStatesByDisplay.end()) {
7221 return false;
7222 }
7223 for (const TouchedWindow& window : touchStateIt->second.windows) {
7224 if (window.windowHandle->getToken() == token &&
7225 (window.hasTouchingPointer(deviceId, pointerId) ||
7226 window.hasHoveringPointer(deviceId, pointerId))) {
7227 return true;
7228 }
7229 }
7230 return false;
7231}
7232
Prabir Pradhan04a66422024-03-15 22:45:27 +00007233void InputDispatcher::setInputMethodConnectionIsActive(bool isActive) {
7234 std::scoped_lock _l(mLock);
7235 if (mTracer) {
7236 mTracer->setInputMethodConnectionIsActive(isActive);
7237 }
7238}
7239
Garfield Tane84e6f92019-08-29 17:28:41 -07007240} // namespace android::inputdispatcher