blob: ffdb1c419a471c67594906f1d3b70438290c10f9 [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"
Prabir Pradhandae52792023-12-15 07:36:40 +000055#include "trace/InputTracer.h"
56#include "trace/InputTracingPerfettoBackend.h"
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000057#include "trace/ThreadedBackend.h"
Michael Wright44753b12020-07-08 13:48:11 +010058
Michael Wrightd02c5b62014-02-10 15:10:22 -080059#define INDENT " "
60#define INDENT2 " "
61#define INDENT3 " "
62#define INDENT4 " "
63
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080064using namespace android::ftl::flag_operators;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -070065using android::base::Error;
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080066using android::base::HwTimeoutMultiplier;
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000067using android::base::Result;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080068using android::base::StringPrintf;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -070069using android::gui::DisplayInfo;
chaviw98318de2021-05-19 16:45:23 -050070using android::gui::FocusRequest;
71using android::gui::TouchOcclusionMode;
72using android::gui::WindowInfo;
73using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080074using android::os::InputEventInjectionResult;
75using android::os::InputEventInjectionSync;
Ameer Armalycff4fa52023-10-04 23:45:11 +000076namespace input_flags = com::android::input::flags;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080077
Garfield Tane84e6f92019-08-29 17:28:41 -070078namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
Prabir Pradhancef936d2021-07-21 16:17:52 +000080namespace {
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000081
Prabir Pradhandae52792023-12-15 07:36:40 +000082// Input tracing is only available on debuggable builds (userdebug and eng) when the feature
83// flag is enabled. When the flag is changed, tracing will only be available after reboot.
84bool isInputTracingEnabled() {
85 static const std::string buildType = base::GetProperty("ro.build.type", "user");
86 static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
87 return input_flags::enable_input_event_tracing() && isUserdebugOrEng;
88}
89
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000090// Create the input tracing backend that writes to perfetto from a single thread.
Prabir Pradhanf4099452024-04-12 18:34:14 +000091std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled() {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000092 if (!isInputTracingEnabled()) {
93 return nullptr;
94 }
95 return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>(
Prabir Pradhanf4099452024-04-12 18:34:14 +000096 trace::impl::PerfettoBackend());
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000097}
98
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000099template <class Entry>
100void ensureEventTraced(const Entry& entry) {
101 if (!entry.traceTracker) {
102 LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry;
103 }
104}
105
Prabir Pradhan052fb0b2024-02-23 21:03:12 +0000106// Helper to get a trace tracker from a traced key or motion entry.
107const std::unique_ptr<trace::EventTrackerInterface>& getTraceTracker(const EventEntry& entry) {
108 switch (entry.type) {
109 case EventEntry::Type::MOTION: {
110 const auto& motion = static_cast<const MotionEntry&>(entry);
111 ensureEventTraced(motion);
112 return motion.traceTracker;
113 }
114 case EventEntry::Type::KEY: {
115 const auto& key = static_cast<const KeyEntry&>(entry);
116 ensureEventTraced(key);
117 return key.traceTracker;
118 }
119 default: {
120 const static std::unique_ptr<trace::EventTrackerInterface> kNullTracker;
121 return kNullTracker;
122 }
123 }
124}
125
Prabir Pradhancef936d2021-07-21 16:17:52 +0000126// Temporarily releases a held mutex for the lifetime of the instance.
127// Named to match std::scoped_lock
128class scoped_unlock {
129public:
130 explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); }
131 ~scoped_unlock() { mMutex.lock(); }
132
133private:
134 std::mutex& mMutex;
135};
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// Default input dispatching timeout if there is no focused application or paused window
138// from which to determine an appropriate dispatching timeout.
Peter Collingbourneb04b9b82021-02-08 12:09:47 -0800139const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds(
140 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
141 HwTimeoutMultiplier());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800143// The default minimum time gap between two user activity poke events.
144const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms;
145
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800146const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148// 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 +0000149constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
150
151// Log a warning when an interception call takes longer than this to process.
152constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153
154// Number of recent events to keep for debugging purposes.
Michael Wright2b3c3302018-03-02 17:19:13 +0000155constexpr size_t RECENT_QUEUE_MAX_SIZE = 10;
156
Antonio Kantekea47acb2021-12-23 12:41:25 -0800157// Event log tags. See EventLogTags.logtags for reference.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000158constexpr int LOGTAG_INPUT_INTERACTION = 62000;
159constexpr int LOGTAG_INPUT_FOCUS = 62001;
Arthur Hungb3307ee2021-10-14 10:57:37 +0000160constexpr int LOGTAG_INPUT_CANCEL = 62003;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000161
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000162const ui::Transform kIdentityTransform;
163
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000164inline nsecs_t now() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 return systemTime(SYSTEM_TIME_MONOTONIC);
166}
167
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700168inline const std::string binderToString(const sp<IBinder>& binder) {
Bernardo Rufino49d99e42021-01-18 15:16:59 +0000169 if (binder == nullptr) {
170 return "<null>";
171 }
172 return StringPrintf("%p", binder.get());
173}
174
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000175static std::string uidString(const gui::Uid& uid) {
176 return uid.toString();
177}
178
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700179Result<void> checkKeyAction(int32_t action) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 switch (action) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700181 case AKEY_EVENT_ACTION_DOWN:
182 case AKEY_EVENT_ACTION_UP:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700183 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700184 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700185 return Error() << "Key event has invalid action code " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 }
187}
188
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700189Result<void> validateKeyEvent(int32_t action) {
190 return checkKeyAction(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191}
192
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700193Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800194 switch (MotionEvent::getActionMasked(action)) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700195 case AMOTION_EVENT_ACTION_DOWN:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700196 case AMOTION_EVENT_ACTION_UP: {
197 if (pointerCount != 1) {
198 return Error() << "invalid pointer count " << pointerCount;
199 }
200 return {};
201 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700202 case AMOTION_EVENT_ACTION_MOVE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700203 case AMOTION_EVENT_ACTION_HOVER_ENTER:
204 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700205 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
206 if (pointerCount < 1) {
207 return Error() << "invalid pointer count " << pointerCount;
208 }
209 return {};
210 }
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800211 case AMOTION_EVENT_ACTION_CANCEL:
212 case AMOTION_EVENT_ACTION_OUTSIDE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700213 case AMOTION_EVENT_ACTION_SCROLL:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700214 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700215 case AMOTION_EVENT_ACTION_POINTER_DOWN:
216 case AMOTION_EVENT_ACTION_POINTER_UP: {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800217 const int32_t index = MotionEvent::getActionIndex(action);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700218 if (index < 0) {
219 return Error() << "invalid index " << index << " for "
220 << MotionEvent::actionToString(action);
221 }
222 if (index >= pointerCount) {
223 return Error() << "invalid index " << index << " for pointerCount " << pointerCount;
224 }
225 if (pointerCount <= 1) {
226 return Error() << "invalid pointer count " << pointerCount << " for "
227 << MotionEvent::actionToString(action);
228 }
229 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700230 }
231 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700232 case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
233 if (actionButton == 0) {
234 return Error() << "action button should be nonzero for "
235 << MotionEvent::actionToString(action);
236 }
237 return {};
238 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700239 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700240 return Error() << "invalid action " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 }
242}
243
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000244int64_t millis(std::chrono::nanoseconds t) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500245 return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
246}
247
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700248Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount,
249 const PointerProperties* pointerProperties) {
250 Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount);
251 if (!actionCheck.ok()) {
252 return actionCheck;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 }
254 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700255 return Error() << "Motion event has invalid pointer count " << pointerCount
256 << "; value must be between 1 and " << MAX_POINTERS << ".";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800258 std::bitset<MAX_POINTER_ID + 1> pointerIdBits;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 for (size_t i = 0; i < pointerCount; i++) {
260 int32_t id = pointerProperties[i].id;
261 if (id < 0 || id > MAX_POINTER_ID) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700262 return Error() << "Motion event has invalid pointer id " << id
263 << "; value must be between 0 and " << MAX_POINTER_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800265 if (pointerIdBits.test(id)) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700266 return Error() << "Motion event has duplicate pointer id " << id;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800268 pointerIdBits.set(id);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700270 return {};
271}
272
273Result<void> validateInputEvent(const InputEvent& event) {
274 switch (event.getType()) {
275 case InputEventType::KEY: {
276 const KeyEvent& key = static_cast<const KeyEvent&>(event);
277 const int32_t action = key.getAction();
278 return validateKeyEvent(action);
279 }
280 case InputEventType::MOTION: {
281 const MotionEvent& motion = static_cast<const MotionEvent&>(event);
282 const int32_t action = motion.getAction();
283 const size_t pointerCount = motion.getPointerCount();
284 const PointerProperties* pointerProperties = motion.getPointerProperties();
285 const int32_t actionButton = motion.getActionButton();
286 return validateMotionEvent(action, actionButton, pointerCount, pointerProperties);
287 }
288 default: {
289 return {};
290 }
291 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292}
293
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800294std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) {
295 std::bitset<MAX_POINTER_ID + 1> pointerIds;
296 for (const PointerProperties& pointer : pointers) {
297 pointerIds.set(pointer.id);
298 }
299 return pointerIds;
300}
301
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000302std::string dumpRegion(const Region& region) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 if (region.isEmpty()) {
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000304 return "<empty>";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 }
306
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000307 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 bool first = true;
309 Region::const_iterator cur = region.begin();
310 Region::const_iterator const tail = region.end();
311 while (cur != tail) {
312 if (first) {
313 first = false;
314 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800315 dump += "|";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800317 dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 cur++;
319 }
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000320 return dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321}
322
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000323std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue,
324 nsecs_t currentTime) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500325 constexpr size_t maxEntries = 50; // max events to print
326 constexpr size_t skipBegin = maxEntries / 2;
327 const size_t skipEnd = queue.size() - maxEntries / 2;
328 // skip from maxEntries / 2 ... size() - maxEntries/2
329 // only print from 0 .. skipBegin and then from skipEnd .. size()
330
331 std::string dump;
332 for (size_t i = 0; i < queue.size(); i++) {
333 const DispatchEntry& entry = *queue[i];
334 if (i >= skipBegin && i < skipEnd) {
335 dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin);
336 i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue'
337 continue;
338 }
339 dump.append(INDENT4);
340 dump += entry.eventEntry->getDescription();
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +0000341 dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq,
342 entry.targetFlags.string().c_str(),
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500343 ns2ms(currentTime - entry.eventEntry->eventTime));
344 if (entry.deliveryTime != 0) {
345 // This entry was delivered, so add information on how long we've been waiting
346 dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime));
347 }
348 dump.append("\n");
349 }
350 return dump;
351}
352
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700353/**
354 * Find the entry in std::unordered_map by key, and return it.
355 * If the entry is not found, return a default constructed entry.
356 *
357 * Useful when the entries are vectors, since an empty vector will be returned
358 * if the entry is not found.
359 * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned.
360 */
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700361template <typename K, typename V>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000362V getValueByKey(const std::unordered_map<K, V>& map, K key) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700363 auto it = map.find(key);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700364 return it != map.end() ? it->second : V{};
Tiger Huang721e26f2018-07-24 22:26:19 +0800365}
366
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000367bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) {
chaviwaf87b3e2019-10-01 16:59:28 -0700368 if (first == second) {
369 return true;
370 }
371
372 if (first == nullptr || second == nullptr) {
373 return false;
374 }
375
376 return first->getToken() == second->getToken();
377}
378
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000379bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) {
Bernardo Rufino1ff9d592021-01-18 16:58:57 +0000380 if (first == nullptr || second == nullptr) {
381 return false;
382 }
383 return first->applicationInfo.token != nullptr &&
384 first->applicationInfo.token == second->applicationInfo.token;
385}
386
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800387template <typename T>
388size_t firstMarkedBit(T set) {
389 // TODO: replace with std::countr_zero from <bit> when that's available
390 LOG_ALWAYS_FATAL_IF(set.none());
391 size_t i = 0;
392 while (!set.test(i)) {
393 i++;
394 }
395 return i;
396}
397
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000398std::unique_ptr<DispatchEntry> createDispatchEntry(const IdGenerator& idGenerator,
399 const InputTarget& inputTarget,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000400 std::shared_ptr<const EventEntry> eventEntry,
401 ftl::Flags<InputTarget::Flags> inputTargetFlags,
Prabir Pradhana67623c2024-02-21 06:57:36 +0000402 int64_t vsyncId,
403 trace::InputTracerInterface* tracer) {
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000404 const bool zeroCoords = inputTargetFlags.test(InputTarget::Flags::ZERO_COORDS);
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000405 const sp<WindowInfoHandle> win = inputTarget.windowHandle;
406 const std::optional<int32_t> windowId =
407 win ? std::make_optional(win->getInfo()->id) : std::nullopt;
408 // Assume the only targets that are not associated with a window are global monitors, and use
409 // the system UID for global monitors for tracing purposes.
410 const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000411
412 if (inputTarget.useDefaultPointerTransform() && !zeroCoords) {
chaviw1ff3d1e2020-07-01 15:53:47 -0700413 const ui::Transform& transform = inputTarget.getDefaultPointerTransform();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700414 return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700415 inputTarget.displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000416 inputTarget.globalScaleFactor, uid, vsyncId,
417 windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000418 }
419
420 ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION);
421 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
422
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000423 std::vector<PointerCoords> pointerCoords{motionEntry.getPointerCount()};
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000424
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000425 const ui::Transform* transform = &kIdentityTransform;
426 const ui::Transform* displayTransform = &kIdentityTransform;
427 if (zeroCoords) {
428 std::for_each(pointerCoords.begin(), pointerCoords.end(), [](auto& pc) { pc.clear(); });
429 } else {
430 // Use the first pointer information to normalize all other pointers. This could be any
431 // pointer as long as all other pointers are normalized to the same value and the final
432 // DispatchEntry uses the transform for the normalized pointer.
433 transform =
434 &inputTarget.getTransformForPointer(firstMarkedBit(inputTarget.getPointerIds()));
435 const ui::Transform inverseTransform = transform->inverse();
436 displayTransform = &inputTarget.displayTransform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000437
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000438 // Iterate through all pointers in the event to normalize against the first.
439 for (size_t i = 0; i < motionEntry.getPointerCount(); i++) {
440 PointerCoords& newCoords = pointerCoords[i];
441 const auto pointerId = motionEntry.pointerProperties[i].id;
442 const ui::Transform& currTransform = inputTarget.getTransformForPointer(pointerId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000443
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000444 newCoords.copyFrom(motionEntry.pointerCoords[i]);
445 // First, apply the current pointer's transform to update the coordinates into
446 // window space.
447 newCoords.transform(currTransform);
448 // Next, apply the inverse transform of the normalized coordinates so the
449 // current coordinates are transformed into the normalized coordinate space.
450 newCoords.transform(inverseTransform);
451 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000452 }
453
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700454 std::unique_ptr<MotionEntry> combinedMotionEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000455 std::make_unique<MotionEntry>(idGenerator.nextId(), motionEntry.injectionState,
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000456 motionEntry.eventTime, motionEntry.deviceId,
457 motionEntry.source, motionEntry.displayId,
458 motionEntry.policyFlags, motionEntry.action,
459 motionEntry.actionButton, motionEntry.flags,
460 motionEntry.metaState, motionEntry.buttonState,
461 motionEntry.classification, motionEntry.edgeFlags,
462 motionEntry.xPrecision, motionEntry.yPrecision,
463 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
464 motionEntry.downTime, motionEntry.pointerProperties,
465 pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000466 if (tracer) {
467 combinedMotionEntry->traceTracker =
468 tracer->traceDerivedEvent(*combinedMotionEntry, *motionEntry.traceTracker);
469 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000470
471 std::unique_ptr<DispatchEntry> dispatchEntry =
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700472 std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags,
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000473 *transform, *displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000474 inputTarget.globalScaleFactor, uid, vsyncId, windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000475 return dispatchEntry;
476}
477
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500478template <typename T>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000479bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) {
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500480 if (lhs == nullptr && rhs == nullptr) {
481 return true;
482 }
483 if (lhs == nullptr || rhs == nullptr) {
484 return false;
485 }
486 return *lhs == *rhs;
487}
488
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000489KeyEvent createKeyEvent(const KeyEntry& entry) {
Siarhei Vishniakou2e2ea992020-12-15 02:57:19 +0000490 KeyEvent event;
491 event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC,
492 entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState,
493 entry.repeatCount, entry.downTime, entry.eventTime);
494 return event;
495}
496
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000497bool shouldReportMetricsForConnection(const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000498 // Do not keep track of gesture monitors. They receive every event and would disproportionately
499 // affect the statistics.
500 if (connection.monitor) {
501 return false;
502 }
503 // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics
504 if (!connection.responsive) {
505 return false;
506 }
507 return true;
508}
509
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000510bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000511 const EventEntry& eventEntry = *dispatchEntry.eventEntry;
512 const int32_t& inputEventId = eventEntry.id;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000513 if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) {
514 return false;
515 }
516 // Only track latency for events that originated from hardware
517 if (eventEntry.isSynthesized()) {
518 return false;
519 }
520 const EventEntry::Type& inputEventEntryType = eventEntry.type;
521 if (inputEventEntryType == EventEntry::Type::KEY) {
522 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
523 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
524 return false;
525 }
526 } else if (inputEventEntryType == EventEntry::Type::MOTION) {
527 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
528 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
529 motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
530 return false;
531 }
532 } else {
533 // Not a key or a motion
534 return false;
535 }
536 if (!shouldReportMetricsForConnection(connection)) {
537 return false;
538 }
539 return true;
540}
541
Prabir Pradhancef936d2021-07-21 16:17:52 +0000542/**
543 * Connection is responsive if it has no events in the waitQueue that are older than the
544 * current time.
545 */
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000546bool isConnectionResponsive(const Connection& connection) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000547 const nsecs_t currentTime = now();
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000548 for (const auto& dispatchEntry : connection.waitQueue) {
549 if (dispatchEntry->timeoutTime < currentTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000550 return false;
551 }
552 }
553 return true;
554}
555
Antonio Kantekf16f2832021-09-28 04:39:20 +0000556// Returns true if the event type passed as argument represents a user activity.
557bool isUserActivityEvent(const EventEntry& eventEntry) {
558 switch (eventEntry.type) {
Josep del Riob3981622023-04-18 15:49:45 +0000559 case EventEntry::Type::CONFIGURATION_CHANGED:
560 case EventEntry::Type::DEVICE_RESET:
561 case EventEntry::Type::DRAG:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000562 case EventEntry::Type::FOCUS:
563 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000564 case EventEntry::Type::SENSOR:
Josep del Riob3981622023-04-18 15:49:45 +0000565 case EventEntry::Type::TOUCH_MODE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000566 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +0000567 case EventEntry::Type::KEY:
568 case EventEntry::Type::MOTION:
569 return true;
570 }
571}
572
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800573// Returns true if the given window can accept pointer events at the given display location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800574bool windowAcceptsTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
575 float y, bool isStylus, const ui::Transform& displayTransform) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800576 const auto inputConfig = windowInfo.inputConfig;
577 if (windowInfo.displayId != displayId ||
578 inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800579 return false;
580 }
Prabir Pradhand65552b2021-10-07 11:23:50 -0700581 const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800582 if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800583 return false;
584 }
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000585
586 // Window Manager works in the logical display coordinate space. When it specifies bounds for a
587 // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside
588 // the window. Points on the right and bottom edges should not be inside the window, so we need
589 // to be careful about performing a hit test when the display is rotated, since the "right" and
590 // "bottom" of the window will be different in the display (un-rotated) space compared to in the
591 // logical display in which WM determined the bounds. Perform the hit test in the logical
592 // display space to ensure these edges are considered correctly in all orientations.
593 const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion);
594 const auto p = displayTransform.transform(x, y);
595 if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800596 return false;
597 }
598 return true;
599}
600
Linnan Li5e5645e2024-03-05 14:43:05 +0000601// Returns true if the given window's frame can occlude pointer events at the given display
602// location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800603bool windowOccludesTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
604 float y, const ui::Transform& displayTransform) {
Linnan Li5e5645e2024-03-05 14:43:05 +0000605 if (windowInfo.displayId != displayId) {
606 return false;
607 }
608 const auto frame = displayTransform.transform(windowInfo.frame);
609 const auto p = floor(displayTransform.transform(x, y));
610 return p.x >= frame.left && p.x < frame.right && p.y >= frame.top && p.y < frame.bottom;
611}
612
Prabir Pradhand65552b2021-10-07 11:23:50 -0700613bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) {
614 return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) &&
Prabir Pradhane5626962022-10-27 20:30:53 +0000615 isStylusToolType(entry.pointerProperties[pointerIndex].toolType);
Prabir Pradhand65552b2021-10-07 11:23:50 -0700616}
617
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800618// Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000619// Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to
620// such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can
621// be sent to such a window, but it is not a foreground event and doesn't use
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800622// InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000623bool canReceiveForegroundTouches(const WindowInfo& info) {
624 // A non-touchable window can still receive touch events (e.g. in the case of
625 // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches.
626 return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy();
627}
628
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000629bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -0700630 if (windowHandle == nullptr) {
631 return false;
632 }
633 const WindowInfo* windowInfo = windowHandle->getInfo();
634 if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) {
635 return true;
636 }
637 return false;
638}
639
Prabir Pradhan5735a322022-04-11 17:23:34 +0000640// Checks targeted injection using the window's owner's uid.
641// Returns an empty string if an entry can be sent to the given window, or an error message if the
642// entry is a targeted injection whose uid target doesn't match the window owner.
643std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window,
644 const EventEntry& entry) {
645 if (entry.injectionState == nullptr || !entry.injectionState->targetUid) {
646 // The event was not injected, or the injected event does not target a window.
647 return {};
648 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000649 const auto uid = *entry.injectionState->targetUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +0000650 if (window == nullptr) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000651 return StringPrintf("No valid window target for injection into uid %s.",
652 uid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000653 }
654 if (entry.injectionState->targetUid != window->getInfo()->ownerUid) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000655 return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' "
656 "owned by uid %s.",
657 uid.toString().c_str(), window->getName().c_str(),
658 window->getInfo()->ownerUid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000659 }
660 return {};
661}
662
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000663std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700664 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
665 // Always dispatch mouse events to cursor position.
666 if (isFromMouse) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000667 return {entry.xCursorPosition, entry.yCursorPosition};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700668 }
669
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -0700670 const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action);
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000671 return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X),
672 entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700673}
674
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -0700675std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) {
676 if (eventEntry.type == EventEntry::Type::KEY) {
677 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
678 return keyEntry.downTime;
679 } else if (eventEntry.type == EventEntry::Type::MOTION) {
680 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
681 return motionEntry.downTime;
682 }
683 return std::nullopt;
684}
685
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000686/**
687 * Compare the old touch state to the new touch state, and generate the corresponding touched
688 * windows (== input targets).
689 * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window.
690 * If the pointer just entered the new window, produce HOVER_ENTER.
691 * For pointers remaining in the window, produce HOVER_MOVE.
692 */
693std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState,
694 const TouchState& newTouchState,
695 const MotionEntry& entry) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000696 const int32_t maskedAction = MotionEvent::getActionMasked(entry.action);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700697
698 if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
699 // ACTION_SCROLL events should not affect the hovering pointer dispatch
700 return {};
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000701 }
Linnan Li41859c42024-03-05 16:20:34 +0000702 std::vector<TouchedWindow> out;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000703
704 // We should consider all hovering pointers here. But for now, just use the first one
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800705 const PointerProperties& pointer = entry.pointerProperties[0];
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000706
707 std::set<sp<WindowInfoHandle>> oldWindows;
708 if (oldState != nullptr) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800709 oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000710 }
711
712 std::set<sp<WindowInfoHandle>> newWindows =
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800713 newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000714
715 // If the pointer is no longer in the new window set, send HOVER_EXIT.
716 for (const sp<WindowInfoHandle>& oldWindow : oldWindows) {
717 if (newWindows.find(oldWindow) == newWindows.end()) {
718 TouchedWindow touchedWindow;
719 touchedWindow.windowHandle = oldWindow;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000720 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000721 out.push_back(touchedWindow);
722 }
723 }
724
725 for (const sp<WindowInfoHandle>& newWindow : newWindows) {
726 TouchedWindow touchedWindow;
727 touchedWindow.windowHandle = newWindow;
728 if (oldWindows.find(newWindow) == oldWindows.end()) {
729 // Any windows that have this pointer now, and didn't have it before, should get
730 // HOVER_ENTER
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000731 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000732 } else {
733 // This pointer was already sent to the window. Use ACTION_HOVER_MOVE.
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700734 if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700735 android::base::LogSeverity severity = android::base::LogSeverity::FATAL;
Ameer Armalycff4fa52023-10-04 23:45:11 +0000736 if (!input_flags::a11y_crash_on_inconsistent_event_stream() &&
737 entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700738 // The Accessibility injected touch exploration event stream
739 // has known inconsistencies, so log ERROR instead of
740 // crashing the device with FATAL.
Daniel Norman7487dfa2023-08-02 16:39:45 -0700741 severity = android::base::LogSeverity::ERROR;
742 }
743 LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription();
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700744 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000745 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000746 }
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800747 touchedWindow.addHoveringPointer(entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000748 if (canReceiveForegroundTouches(*newWindow->getInfo())) {
749 touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND;
750 }
751 out.push_back(touchedWindow);
752 }
753 return out;
754}
755
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800756template <typename T>
757std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) {
758 left.insert(left.end(), right.begin(), right.end());
759 return left;
760}
761
Harry Cuttsb166c002023-05-09 13:06:05 +0000762// Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from
763// both.
764void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) {
765 std::erase_if(touchState.windows, [&](const TouchedWindow& window) {
766 if (!window.windowHandle->getInfo()->inputConfig.test(
767 WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
768 // In addition to TouchState, erase this window from the input targets! We don't have a
769 // good way to do this today except by adding a nested loop.
770 // TODO(b/282025641): simplify this code once InputTargets are being identified
771 // separately from TouchedWindows.
772 std::erase_if(targets, [&](const InputTarget& target) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800773 return target.connection->getToken() == window.windowHandle->getToken();
Harry Cuttsb166c002023-05-09 13:06:05 +0000774 });
775 return true;
776 }
777 return false;
778 });
779}
780
Siarhei Vishniakouce1fd472023-09-18 18:38:07 -0700781/**
782 * In general, touch should be always split between windows. Some exceptions:
783 * 1. Don't split touch if all of the below is true:
784 * (a) we have an active pointer down *and*
785 * (b) a new pointer is going down that's from the same device *and*
786 * (c) the window that's receiving the current pointer does not support split touch.
787 * 2. Don't split mouse events
788 */
789bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) {
790 if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) {
791 // We should never split mouse events
792 return false;
793 }
794 for (const TouchedWindow& touchedWindow : touchState.windows) {
795 if (touchedWindow.windowHandle->getInfo()->isSpy()) {
796 // Spy windows should not affect whether or not touch is split.
797 continue;
798 }
799 if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) {
800 continue;
801 }
802 if (touchedWindow.windowHandle->getInfo()->inputConfig.test(
803 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
804 // Wallpaper window should not affect whether or not touch is split
805 continue;
806 }
807
808 if (touchedWindow.hasTouchingPointers(entry.deviceId)) {
809 return false;
810 }
811 }
812 return true;
813}
814
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700815/**
816 * Return true if stylus is currently down anywhere on the specified display, and false otherwise.
817 */
Linnan Li13bf76a2024-05-05 19:18:02 +0800818bool isStylusActiveInDisplay(ui::LogicalDisplayId displayId,
819 const std::unordered_map<ui::LogicalDisplayId /*displayId*/,
820 TouchState>& touchStatesByDisplay) {
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700821 const auto it = touchStatesByDisplay.find(displayId);
822 if (it == touchStatesByDisplay.end()) {
823 return false;
824 }
825 const TouchState& state = it->second;
826 return state.hasActiveStylus();
827}
828
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800829Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) {
830 struct HashFunction {
831 size_t operator()(const WindowInfo& info) const { return info.id; }
832 };
833
834 std::unordered_set<WindowInfo, HashFunction> windowSet;
835 for (const WindowInfo& info : update.windowInfos) {
836 const auto [_, inserted] = windowSet.insert(info);
837 if (!inserted) {
838 return Error() << "Duplicate entry for " << info;
839 }
840 }
841 return {};
842}
843
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800844int32_t getUserActivityEventType(const EventEntry& eventEntry) {
845 switch (eventEntry.type) {
846 case EventEntry::Type::KEY: {
847 return USER_ACTIVITY_EVENT_BUTTON;
848 }
849 case EventEntry::Type::MOTION: {
850 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
851 if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) {
852 return USER_ACTIVITY_EVENT_TOUCH;
853 }
854 return USER_ACTIVITY_EVENT_OTHER;
855 }
856 default: {
857 LOG_ALWAYS_FATAL("%s events are not user activity",
858 ftl::enum_string(eventEntry.type).c_str());
859 }
860 }
861}
862
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +0000863std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode(
864 CancelationOptions::Mode mode) {
865 switch (mode) {
866 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
867 return {true, true};
868 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
869 return {true, false};
870 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
871 return {false, true};
872 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
873 return {false, true};
874 }
875}
876
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000877class ScopedSyntheticEventTracer {
878public:
879 ScopedSyntheticEventTracer(std::unique_ptr<trace::InputTracerInterface>& tracer)
880 : mTracer(tracer) {
881 if (mTracer) {
882 mEventTracker = mTracer->createTrackerForSyntheticEvent();
883 }
884 }
885
886 ~ScopedSyntheticEventTracer() {
887 if (mTracer) {
888 mTracer->eventProcessingComplete(*mEventTracker);
889 }
890 }
891
892 const std::unique_ptr<trace::EventTrackerInterface>& getTracker() const {
893 return mEventTracker;
894 }
895
896private:
897 std::unique_ptr<trace::InputTracerInterface>& mTracer;
898 std::unique_ptr<trace::EventTrackerInterface> mEventTracker;
899};
900
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000901} // namespace
902
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903// --- InputDispatcher ---
904
Prabir Pradhana41d2442023-04-20 21:30:40 +0000905InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
Prabir Pradhanf4099452024-04-12 18:34:14 +0000906 : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {}
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000907
908InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
909 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
Garfield Tan00f511d2019-06-12 16:55:40 -0700910 : mPolicy(policy),
911 mPendingEvent(nullptr),
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700912 mLastDropReason(DropReason::NOT_DROPPED),
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800913 mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800914 mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),
Garfield Tan00f511d2019-06-12 16:55:40 -0700915 mNextUnblockedEvent(nullptr),
Prabir Pradhan1376fcd2022-01-21 09:56:35 -0800916 mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),
Garfield Tan00f511d2019-06-12 16:55:40 -0700917 mDispatchEnabled(false),
918 mDispatchFrozen(false),
919 mInputFilterEnabled(false),
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100920 mMaximumObscuringOpacityForTouch(1.0f),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700921 mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
Prabir Pradhan99987712020-11-10 18:43:05 -0800922 mWindowTokenWithPointerCapture(nullptr),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700923 mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +0000924 mLatencyAggregator(),
Antonio Kantek15beb512022-06-13 22:35:41 +0000925 mLatencyTracker(&mLatencyAggregator) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700926 mLooper = sp<Looper>::make(false);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800927 mReporter = createInputReporter();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700929 mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700930#if defined(__ANDROID__)
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700931 SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700932#endif
Yi Kong9b14ac62018-07-17 13:48:38 -0700933 mKeyRepeatState.lastKeyEntry = nullptr;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000934
935 if (traceBackend) {
Prabir Pradhandae52792023-12-15 07:36:40 +0000936 mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000937 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800938
939 mLastUserActivityTimes.fill(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940}
941
942InputDispatcher::~InputDispatcher() {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000943 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944
Prabir Pradhancef936d2021-07-21 16:17:52 +0000945 resetKeyRepeatLocked();
946 releasePendingEventLocked();
947 drainInboundQueueLocked();
948 mCommandQueue.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +0000950 while (!mConnectionsByToken.empty()) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700951 std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800952 removeInputChannelLocked(connection->getToken(), /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 }
954}
955
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700956status_t InputDispatcher::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700957 if (mThread) {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700958 return ALREADY_EXISTS;
959 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700960 mThread = std::make_unique<InputThread>(
961 "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
962 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700963}
964
965status_t InputDispatcher::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700966 if (mThread && mThread->isCallingThread()) {
967 ALOGE("InputDispatcher cannot be stopped from its own thread!");
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700968 return INVALID_OPERATION;
969 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700970 mThread.reset();
971 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700972}
973
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974void InputDispatcher::dispatchOnce() {
Colin Cross5b799302022-10-18 21:52:41 -0700975 nsecs_t nextWakeupTime = LLONG_MAX;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800977 std::scoped_lock _l(mLock);
978 mDispatcherIsAlive.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
980 // Run a dispatch loop if there are no pending commands.
981 // The dispatch loop might enqueue commands to run afterwards.
982 if (!haveCommandsLocked()) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -0800983 dispatchOnceInnerLocked(/*byref*/ nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 }
985
986 // Run all pending commands if there are any.
987 // If any commands were run then force the next poll to wake up immediately.
Prabir Pradhancef936d2021-07-21 16:17:52 +0000988 if (runCommandsLockedInterruptable()) {
Colin Cross5b799302022-10-18 21:52:41 -0700989 nextWakeupTime = LLONG_MIN;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 }
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800991
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700992 // If we are still waiting for ack on some events,
993 // we might have to wake up earlier to check if an app is anr'ing.
994 const nsecs_t nextAnrCheck = processAnrsLocked();
995 nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);
996
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800997 // We are about to enter an infinitely long sleep, because we have no commands or
998 // pending or queued events
Colin Cross5b799302022-10-18 21:52:41 -0700999 if (nextWakeupTime == LLONG_MAX) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001000 mDispatcherEnteredIdle.notify_all();
1001 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002 } // release lock
1003
1004 // Wait for callback or timeout or wake. (make sure we round up, not down)
1005 nsecs_t currentTime = now();
1006 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
1007 mLooper->pollOnce(timeoutMillis);
1008}
1009
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001010/**
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001011 * Raise ANR if there is no focused window.
1012 * Before the ANR is raised, do a final state check:
1013 * 1. The currently focused application must be the same one we are waiting for.
1014 * 2. Ensure we still don't have a focused window.
1015 */
1016void InputDispatcher::processNoFocusedWindowAnrLocked() {
1017 // Check if the application that we are waiting for is still focused.
1018 std::shared_ptr<InputApplicationHandle> focusedApplication =
1019 getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId);
1020 if (focusedApplication == nullptr ||
1021 focusedApplication->getApplicationToken() !=
1022 mAwaitedFocusedApplication->getApplicationToken()) {
1023 // Unexpected because we should have reset the ANR timer when focused application changed
1024 ALOGE("Waited for a focused window, but focused application has already changed to %s",
1025 focusedApplication->getName().c_str());
1026 return; // The focused application has changed.
1027 }
1028
chaviw98318de2021-05-19 16:45:23 -05001029 const sp<WindowInfoHandle>& focusedWindowHandle =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001030 getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId);
1031 if (focusedWindowHandle != nullptr) {
1032 return; // We now have a focused window. No need for ANR.
1033 }
1034 onAnrLocked(mAwaitedFocusedApplication);
1035}
1036
1037/**
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001038 * Check if any of the connections' wait queues have events that are too old.
1039 * If we waited for events to be ack'ed for more than the window timeout, raise an ANR.
1040 * Return the time at which we should wake up next.
1041 */
1042nsecs_t InputDispatcher::processAnrsLocked() {
1043 const nsecs_t currentTime = now();
Colin Cross5b799302022-10-18 21:52:41 -07001044 nsecs_t nextAnrCheck = LLONG_MAX;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001045 // Check if we are waiting for a focused window to appear. Raise ANR if waited too long
1046 if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) {
1047 if (currentTime >= *mNoFocusedWindowTimeoutTime) {
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001048 processNoFocusedWindowAnrLocked();
Chris Yea209fde2020-07-22 13:54:51 -07001049 mAwaitedFocusedApplication.reset();
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001050 mNoFocusedWindowTimeoutTime = std::nullopt;
Colin Cross5b799302022-10-18 21:52:41 -07001051 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001052 } else {
Siarhei Vishniakou38a6d272020-10-20 20:29:33 -05001053 // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001054 nextAnrCheck = *mNoFocusedWindowTimeoutTime;
1055 }
1056 }
1057
1058 // Check if any connection ANRs are due
1059 nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout());
1060 if (currentTime < nextAnrCheck) { // most likely scenario
1061 return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck
1062 }
1063
1064 // If we reached here, we have an unresponsive connection.
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001065 std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001066 if (connection == nullptr) {
1067 ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout());
1068 return nextAnrCheck;
1069 }
1070 connection->responsive = false;
1071 // Stop waking up for this unresponsive connection
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001072 mAnrTracker.eraseToken(connection->getToken());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00001073 onAnrLocked(connection);
Colin Cross5b799302022-10-18 21:52:41 -07001074 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001075}
1076
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001077std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001078 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001079 if (connection->monitor) {
1080 return mMonitorDispatchingTimeout;
1081 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001082 const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001083 if (window != nullptr) {
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001084 return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001085 }
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001086 return DEFAULT_INPUT_DISPATCHING_TIMEOUT;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001087}
1088
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001089void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 nsecs_t currentTime = now();
1091
Jeff Browndc5992e2014-04-11 01:27:26 -07001092 // Reset the key repeat timer whenever normal dispatch is suspended while the
1093 // device is in a non-interactive state. This is to ensure that we abort a key
1094 // repeat if the device is just coming out of sleep.
1095 if (!mDispatchEnabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 resetKeyRepeatLocked();
1097 }
1098
1099 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
1100 if (mDispatchFrozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01001101 if (DEBUG_FOCUS) {
1102 ALOGD("Dispatch frozen. Waiting some more.");
1103 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 return;
1105 }
1106
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 // Ready to start a new event.
1108 // If we don't already have a pending event, go grab one.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001109 if (!mPendingEvent) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001110 if (mInboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 // Synthesize a key repeat if appropriate.
1112 if (mKeyRepeatState.lastKeyEntry) {
1113 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
1114 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
1115 } else {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001116 nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 }
1118 }
1119
1120 // Nothing to do if there is no pending event.
1121 if (!mPendingEvent) {
1122 return;
1123 }
1124 } else {
1125 // Inbound queue has at least one entry.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001126 mPendingEvent = mInboundQueue.front();
1127 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 traceInboundQueueLengthLocked();
1129 }
1130
1131 // Poke user activity for this event.
1132 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001133 pokeUserActivityLocked(*mPendingEvent);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 }
1136
1137 // Now we have an event to dispatch.
1138 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Yi Kong9b14ac62018-07-17 13:48:38 -07001139 ALOG_ASSERT(mPendingEvent != nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140 bool done = false;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001141 DropReason dropReason = DropReason::NOT_DROPPED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001143 dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 } else if (!mDispatchEnabled) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001145 dropReason = DropReason::DISABLED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 }
1147
1148 if (mNextUnblockedEvent == mPendingEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001149 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 }
1151
1152 switch (mPendingEvent->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001153 case EventEntry::Type::CONFIGURATION_CHANGED: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001154 const ConfigurationChangedEntry& typedEntry =
1155 static_cast<const ConfigurationChangedEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001156 done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001157 dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001158 break;
1159 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001161 case EventEntry::Type::DEVICE_RESET: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001162 const DeviceResetEntry& typedEntry =
1163 static_cast<const DeviceResetEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001164 done = dispatchDeviceResetLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001165 dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001166 break;
1167 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001169 case EventEntry::Type::FOCUS: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001170 std::shared_ptr<const FocusEntry> typedEntry =
1171 std::static_pointer_cast<const FocusEntry>(mPendingEvent);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001172 dispatchFocusLocked(currentTime, typedEntry);
1173 done = true;
1174 dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
1175 break;
1176 }
1177
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001178 case EventEntry::Type::TOUCH_MODE_CHANGED: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001179 const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001180 dispatchTouchModeChangeLocked(currentTime, typedEntry);
1181 done = true;
1182 dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped
1183 break;
1184 }
1185
Prabir Pradhan99987712020-11-10 18:43:05 -08001186 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
1187 const auto typedEntry =
Prabir Pradhan24047542023-11-02 17:14:59 +00001188 std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent);
Prabir Pradhan99987712020-11-10 18:43:05 -08001189 dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason);
1190 done = true;
1191 break;
1192 }
1193
arthurhungb89ccb02020-12-30 16:19:01 +08001194 case EventEntry::Type::DRAG: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001195 std::shared_ptr<const DragEntry> typedEntry =
1196 std::static_pointer_cast<const DragEntry>(mPendingEvent);
arthurhungb89ccb02020-12-30 16:19:01 +08001197 dispatchDragLocked(currentTime, typedEntry);
1198 done = true;
1199 break;
1200 }
1201
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001202 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001203 std::shared_ptr<const KeyEntry> keyEntry =
1204 std::static_pointer_cast<const KeyEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001205 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001206 dropReason = DropReason::STALE;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001207 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001208 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
1209 dropReason = DropReason::BLOCKED;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001210 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001211 done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001212 break;
1213 }
1214
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001215 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001216 std::shared_ptr<const MotionEntry> motionEntry =
1217 std::static_pointer_cast<const MotionEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001218 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07001219 // The event is stale. However, only drop stale events if there isn't an ongoing
1220 // gesture. That would allow us to complete the processing of the current stroke.
1221 const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId);
1222 if (touchStateIt != mTouchStatesByDisplay.end()) {
1223 const TouchState& touchState = touchStateIt->second;
1224 if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
1225 !touchState.hasHoveringPointers(motionEntry->deviceId)) {
1226 dropReason = DropReason::STALE;
1227 }
1228 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001229 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001230 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001231 if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) {
1232 // Only drop events that are focus-dispatched.
1233 dropReason = DropReason::BLOCKED;
1234 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001235 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001236 done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001237 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 }
Chris Yef59a2f42020-10-16 12:55:26 -07001239
1240 case EventEntry::Type::SENSOR: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001241 std::shared_ptr<const SensorEntry> sensorEntry =
1242 std::static_pointer_cast<const SensorEntry>(mPendingEvent);
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001243
Chris Yef59a2f42020-10-16 12:55:26 -07001244 // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use
1245 // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead.
1246 nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME);
1247 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) {
1248 dropReason = DropReason::STALE;
1249 }
1250 dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime);
1251 done = true;
1252 break;
1253 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 }
1255
1256 if (done) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001257 if (dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001258 dropInboundEventLocked(*mPendingEvent, dropReason);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 }
Michael Wright3a981722015-06-10 15:26:13 +01001260 mLastDropReason = dropReason;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261
Prabir Pradhan052fb0b2024-02-23 21:03:12 +00001262 if (mTracer) {
1263 if (auto& traceTracker = getTraceTracker(*mPendingEvent); traceTracker != nullptr) {
1264 mTracer->eventProcessingComplete(*traceTracker);
1265 }
1266 }
1267
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 releasePendingEventLocked();
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001269 nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 }
1271}
1272
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001273bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) {
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07001274 return mPolicy.isStaleEvent(currentTime, entry.eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001275}
1276
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001277/**
1278 * Return true if the events preceding this incoming motion event should be dropped
1279 * Return false otherwise (the default behaviour)
1280 */
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001281bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001282 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
Prabir Pradhanaa561d12021-09-24 06:57:33 -07001283 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001284
1285 // Optimize case where the current application is unresponsive and the user
1286 // decides to touch a window in a different application.
1287 // If the application takes too long to catch up then we drop all events preceding
1288 // the touch into the other window.
1289 if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001290 const ui::LogicalDisplayId displayId = motionEntry.displayId;
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001291 const auto [x, y] = resolveTouchedPosition(motionEntry);
Harry Cutts33476232023-01-30 19:57:29 +00001292 const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0);
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001293
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001294 sp<WindowInfoHandle> touchedWindowHandle =
1295 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001296 if (touchedWindowHandle != nullptr &&
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001297 touchedWindowHandle->getApplicationToken() !=
1298 mAwaitedFocusedApplication->getApplicationToken()) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001299 // User touched a different application than the one we are waiting on.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001300 ALOGI("Pruning input queue because user touched a different application while waiting "
1301 "for %s",
1302 mAwaitedFocusedApplication->getName().c_str());
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001303 return true;
1304 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001305
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001306 // Alternatively, maybe there's a spy window that could handle this event.
1307 const std::vector<sp<WindowInfoHandle>> touchedSpies =
1308 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus);
1309 for (const auto& windowHandle : touchedSpies) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001310 const std::shared_ptr<Connection> connection =
1311 getConnectionLocked(windowHandle->getToken());
Siarhei Vishniakou34ed4d42020-06-18 00:43:02 +00001312 if (connection != nullptr && connection->responsive) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001313 // This spy window could take more input. Drop all events preceding this
1314 // event, so that the spy window can get a chance to receive the stream.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001315 ALOGW("Pruning the input queue because %s is unresponsive, but we have a "
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001316 "responsive spy window that may handle the event.",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001317 mAwaitedFocusedApplication->getName().c_str());
1318 return true;
1319 }
1320 }
1321 }
1322
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001323 return false;
1324}
1325
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001326bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001327 bool needWake = mInboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001328 mInboundQueue.push_back(std::move(newEntry));
Prabir Pradhan24047542023-11-02 17:14:59 +00001329 const EventEntry& entry = *(mInboundQueue.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 traceInboundQueueLengthLocked();
1331
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001332 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001333 case EventEntry::Type::KEY: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001334 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1335 "Unexpected untrusted event.");
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001336
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001337 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001338 if (mTracer) {
1339 ensureEventTraced(keyEntry);
1340 }
Siarhei Vishniakou6520a582023-10-27 21:53:45 -07001341
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001342 // If a new up event comes in, and the pending event with same key code has been asked
1343 // to try again later because of the policy. We have to reset the intercept key wake up
1344 // time for it may have been handled in the policy and could be dropped.
1345 if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent &&
1346 mPendingEvent->type == EventEntry::Type::KEY) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001347 const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001348 if (pendingKey.keyCode == keyEntry.keyCode &&
1349 pendingKey.interceptKeyResult ==
Michael Wright5caf55a2022-11-24 22:31:42 +00001350 KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
1351 pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001352 pendingKey.interceptKeyWakeupTime = 0;
1353 needWake = true;
1354 }
1355 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001356 break;
1357 }
1358
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001359 case EventEntry::Type::MOTION: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001360 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1361 "Unexpected untrusted event.");
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001362 const auto& motionEntry = static_cast<const MotionEntry&>(entry);
1363 if (mTracer) {
1364 ensureEventTraced(motionEntry);
1365 }
1366 if (shouldPruneInboundQueueLocked(motionEntry)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001367 mNextUnblockedEvent = mInboundQueue.back();
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001368 needWake = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001369 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001370
1371 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
1372 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
1373 if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) {
1374 // Prevent waiting too long for unprocessed events: if we have a pending key event,
1375 // and some other events have not yet been processed, the dispatcher will wait for
1376 // these events to be processed before dispatching the key event. This is because
1377 // the unprocessed events may cause the focus to change (for example, by launching a
1378 // new window or tapping a different window). To prevent waiting too long, we force
1379 // the key to be sent to the currently focused window when a new tap comes in.
1380 ALOGD("Received a new pointer down event, stop waiting for events to process and "
1381 "just send the pending key event to the currently focused window.");
1382 mKeyIsWaitingForEventsTimeout = now();
Linnan Li563916a2024-02-23 16:08:33 +00001383 needWake = true;
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001384 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001385 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001386 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001387 case EventEntry::Type::FOCUS: {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001388 LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked");
1389 break;
1390 }
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001391 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001392 case EventEntry::Type::CONFIGURATION_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08001393 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07001394 case EventEntry::Type::SENSOR:
arthurhungb89ccb02020-12-30 16:19:01 +08001395 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1396 case EventEntry::Type::DRAG: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001397 // nothing to do
1398 break;
1399 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001400 }
1401
1402 return needWake;
1403}
1404
Prabir Pradhan24047542023-11-02 17:14:59 +00001405void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) {
Chris Yef59a2f42020-10-16 12:55:26 -07001406 // Do not store sensor event in recent queue to avoid flooding the queue.
1407 if (entry->type != EventEntry::Type::SENSOR) {
1408 mRecentQueue.push_back(entry);
1409 }
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001410 if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001411 mRecentQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 }
1413}
1414
Linnan Li13bf76a2024-05-05 19:18:02 +08001415sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(ui::LogicalDisplayId displayId,
1416 float x, float y, bool isStylus,
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001417 bool ignoreDragWindow) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001418 // Traverse windows from front to back to find touched window.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001419 const auto& windowHandles = getWindowHandlesLocked(displayId);
chaviw98318de2021-05-19 16:45:23 -05001420 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
arthurhung6d4bed92021-03-17 11:59:33 +08001421 if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) {
arthurhungb89ccb02020-12-30 16:19:01 +08001422 continue;
1423 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001424
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001425 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001426 if (!info.isSpy() &&
1427 windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001428 return windowHandle;
1429 }
1430 }
1431 return nullptr;
1432}
1433
1434std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08001435 ui::LogicalDisplayId displayId, const sp<WindowInfoHandle>& touchedWindow,
1436 int32_t pointerId) const {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001437 if (touchedWindow == nullptr) {
1438 return {};
1439 }
1440 // Traverse windows from front to back until we encounter the touched window.
1441 std::vector<InputTarget> outsideTargets;
1442 const auto& windowHandles = getWindowHandlesLocked(displayId);
1443 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1444 if (windowHandle == touchedWindow) {
1445 // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window
1446 // below the touched window will not get ACTION_OUTSIDE event.
1447 return outsideTargets;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001448 }
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001449
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001450 const WindowInfo& info = *windowHandle->getInfo();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08001451 if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) {
Siarhei Vishniakou70f3d8c2023-09-19 15:36:52 -07001452 std::bitset<MAX_POINTER_ID + 1> pointerIds;
1453 pointerIds.set(pointerId);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001454 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE,
1455 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001456 /*firstDownTimeInTarget=*/std::nullopt, outsideTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001457 }
1458 }
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001459 return outsideTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460}
1461
Prabir Pradhand65552b2021-10-07 11:23:50 -07001462std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08001463 ui::LogicalDisplayId displayId, float x, float y, bool isStylus) const {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001464 // Traverse windows from front to back and gather the touched spy windows.
1465 std::vector<sp<WindowInfoHandle>> spyWindows;
1466 const auto& windowHandles = getWindowHandlesLocked(displayId);
1467 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1468 const WindowInfo& info = *windowHandle->getInfo();
1469
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001470 if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001471 continue;
1472 }
1473 if (!info.isSpy()) {
1474 // The first touched non-spy window was found, so return the spy windows touched so far.
1475 return spyWindows;
1476 }
1477 spyWindows.push_back(windowHandle);
1478 }
1479 return spyWindows;
1480}
1481
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001482void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483 const char* reason;
1484 switch (dropReason) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001485 case DropReason::POLICY:
Prabir Pradhan65613802023-02-22 23:36:58 +00001486 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001487 ALOGD("Dropped event because policy consumed it.");
1488 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001489 reason = "inbound event was dropped because the policy consumed it";
1490 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001491 case DropReason::DISABLED:
1492 if (mLastDropReason != DropReason::DISABLED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001493 ALOGI("Dropped event because input dispatch is disabled.");
1494 }
1495 reason = "inbound event was dropped because input dispatch is disabled";
1496 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001497 case DropReason::BLOCKED:
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001498 LOG(INFO) << "Dropping because the current application is not responding and the user "
1499 "has started interacting with a different application: "
1500 << entry.getDescription();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001501 reason = "inbound event was dropped because the current application is not responding "
1502 "and the user has started interacting with a different application";
1503 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001504 case DropReason::STALE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001505 ALOGI("Dropped event because it is stale.");
1506 reason = "inbound event was dropped because it is stale";
1507 break;
Prabir Pradhan99987712020-11-10 18:43:05 -08001508 case DropReason::NO_POINTER_CAPTURE:
1509 ALOGI("Dropped event because there is no window with Pointer Capture.");
1510 reason = "inbound event was dropped because there is no window with Pointer Capture";
1511 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001512 case DropReason::NOT_DROPPED: {
1513 LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event");
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001514 return;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001515 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 }
1517
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001518 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001519 case EventEntry::Type::KEY: {
Hu Guo3cfa7382023-11-15 09:50:04 +00001520 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001521 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason,
1522 keyEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001523 options.displayId = keyEntry.displayId;
1524 options.deviceId = keyEntry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525 synthesizeCancelationEventsForAllConnectionsLocked(options);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001526 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001528 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001529 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
1530 if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001531 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason,
1532 motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001533 options.displayId = motionEntry.displayId;
1534 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001535 synthesizeCancelationEventsForAllConnectionsLocked(options);
1536 } else {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001537 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001538 reason, motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001539 options.displayId = motionEntry.displayId;
1540 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001541 synthesizeCancelationEventsForAllConnectionsLocked(options);
1542 }
1543 break;
1544 }
Chris Yef59a2f42020-10-16 12:55:26 -07001545 case EventEntry::Type::SENSOR: {
1546 break;
1547 }
arthurhungb89ccb02020-12-30 16:19:01 +08001548 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1549 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001550 break;
1551 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001552 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001553 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001554 case EventEntry::Type::CONFIGURATION_CHANGED:
1555 case EventEntry::Type::DEVICE_RESET: {
Dominik Laskowski75788452021-02-09 18:51:25 -08001556 LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001557 break;
1558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 }
1560}
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562bool InputDispatcher::haveCommandsLocked() const {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001563 return !mCommandQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564}
1565
Prabir Pradhancef936d2021-07-21 16:17:52 +00001566bool InputDispatcher::runCommandsLockedInterruptable() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001567 if (mCommandQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568 return false;
1569 }
1570
1571 do {
Prabir Pradhancef936d2021-07-21 16:17:52 +00001572 auto command = std::move(mCommandQueue.front());
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001573 mCommandQueue.pop_front();
Prabir Pradhancef936d2021-07-21 16:17:52 +00001574 // Commands are run with the lock held, but may release and re-acquire the lock from within.
1575 command();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001576 } while (!mCommandQueue.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577 return true;
1578}
1579
Prabir Pradhancef936d2021-07-21 16:17:52 +00001580void InputDispatcher::postCommandLocked(Command&& command) {
1581 mCommandQueue.push_back(command);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582}
1583
1584void InputDispatcher::drainInboundQueueLocked() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001585 while (!mInboundQueue.empty()) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001586 std::shared_ptr<const EventEntry> entry = mInboundQueue.front();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001587 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588 releaseInboundEventLocked(entry);
1589 }
1590 traceInboundQueueLengthLocked();
1591}
1592
1593void InputDispatcher::releasePendingEventLocked() {
1594 if (mPendingEvent) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 releaseInboundEventLocked(mPendingEvent);
Yi Kong9b14ac62018-07-17 13:48:38 -07001596 mPendingEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598}
1599
Prabir Pradhan24047542023-11-02 17:14:59 +00001600void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001601 const std::shared_ptr<InjectionState>& injectionState = entry->injectionState;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001602 if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001603 if (DEBUG_DISPATCH_CYCLE) {
1604 ALOGD("Injected inbound event was dropped.");
1605 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001606 setInjectionResult(*entry, InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 }
1608 if (entry == mNextUnblockedEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001609 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001610 }
1611 addRecentEventLocked(entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001612}
1613
1614void InputDispatcher::resetKeyRepeatLocked() {
1615 if (mKeyRepeatState.lastKeyEntry) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001616 mKeyRepeatState.lastKeyEntry = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 }
1618}
1619
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001620std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001621 std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
Michael Wright2e732952014-09-24 13:26:59 -07001623 uint32_t policyFlags = entry->policyFlags &
1624 (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001626 std::shared_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001627 std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
1628 currentTime, entry->deviceId, entry->source,
1629 entry->displayId, policyFlags, entry->action, entry->flags,
1630 entry->keyCode, entry->scanCode, entry->metaState,
1631 entry->repeatCount + 1, entry->downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001633 newEntry->syntheticRepeat = true;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001634 if (mTracer) {
1635 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
1636 }
1637
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001638 mKeyRepeatState.lastKeyEntry = newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001640 return newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001641}
1642
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001643bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001644 const ConfigurationChangedEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001645 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1646 ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime);
1647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648
1649 // Reset key repeating in case a keyboard device was added or removed or something.
1650 resetKeyRepeatLocked();
1651
1652 // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
Prabir Pradhancef936d2021-07-21 16:17:52 +00001653 auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) {
1654 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00001655 mPolicy.notifyConfigurationChanged(eventTime);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001656 };
1657 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658 return true;
1659}
1660
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001661bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
1662 const DeviceResetEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001663 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1664 ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime,
1665 entry.deviceId);
1666 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001667
liushenxiang42232912021-05-21 20:24:09 +08001668 // Reset key repeating in case a keyboard device was disabled or enabled.
1669 if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) {
1670 resetKeyRepeatLocked();
1671 }
1672
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001673 ScopedSyntheticEventTracer traceContext(mTracer);
1674 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset",
1675 traceContext.getTracker());
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001676 options.deviceId = entry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001677 synthesizeCancelationEventsForAllConnectionsLocked(options);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07001678
1679 // Remove all active pointers from this device
1680 for (auto& [_, touchState] : mTouchStatesByDisplay) {
1681 touchState.removeAllPointersForDevice(entry.deviceId);
1682 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683 return true;
1684}
1685
Vishnu Nairad321cd2020-08-20 16:40:21 -07001686void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -08001687 const std::string& reason) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001688 if (mPendingEvent != nullptr) {
1689 // Move the pending event to the front of the queue. This will give the chance
1690 // for the pending event to get dispatched to the newly focused window
1691 mInboundQueue.push_front(mPendingEvent);
1692 mPendingEvent = nullptr;
1693 }
1694
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001695 std::unique_ptr<FocusEntry> focusEntry =
1696 std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus,
1697 reason);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001698
1699 // This event should go to the front of the queue, but behind all other focus events
1700 // Find the last focus event, and insert right after it
Prabir Pradhan24047542023-11-02 17:14:59 +00001701 auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(),
1702 [](const std::shared_ptr<const EventEntry>& event) {
1703 return event->type == EventEntry::Type::FOCUS;
1704 });
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001705
1706 // Maintain the order of focus events. Insert the entry after all other focus events.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001707 mInboundQueue.insert(it.base(), std::move(focusEntry));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001708}
1709
Prabir Pradhan24047542023-11-02 17:14:59 +00001710void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime,
1711 std::shared_ptr<const FocusEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001712 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
1713 if (connection == nullptr) {
1714 return; // Connection has gone away
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001715 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001716 entry->dispatchInProgress = true;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00001717 std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") +
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001718 connection->getInputChannelName();
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07001719 std::string reason = std::string("reason=").append(entry->reason);
1720 android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001721 dispatchEventLocked(currentTime, entry, {{connection}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001722}
1723
Prabir Pradhan99987712020-11-10 18:43:05 -08001724void InputDispatcher::dispatchPointerCaptureChangedLocked(
Prabir Pradhan24047542023-11-02 17:14:59 +00001725 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry,
Prabir Pradhan99987712020-11-10 18:43:05 -08001726 DropReason& dropReason) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001727 dropReason = DropReason::NOT_DROPPED;
1728
Prabir Pradhan99987712020-11-10 18:43:05 -08001729 const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr;
Prabir Pradhan99987712020-11-10 18:43:05 -08001730 sp<IBinder> token;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001731
Hiroki Sato25040232024-02-22 17:21:22 +09001732 if (entry->pointerCaptureRequest.isEnable()) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001733 // Enable Pointer Capture.
1734 if (haveWindowWithPointerCapture &&
1735 (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) {
Prabir Pradhan7092e262022-05-03 16:51:09 +00001736 // This can happen if pointer capture is disabled and re-enabled before we notify the
1737 // app of the state change, so there is no need to notify the app.
1738 ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change.");
1739 return;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001740 }
Hiroki Sato25040232024-02-22 17:21:22 +09001741 if (!mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001742 // This can happen if a window requests capture and immediately releases capture.
1743 ALOGW("No window requested Pointer Capture.");
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001744 dropReason = DropReason::NO_POINTER_CAPTURE;
Prabir Pradhan99987712020-11-10 18:43:05 -08001745 return;
1746 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001747 if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) {
1748 ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch.");
1749 return;
1750 }
1751
Vishnu Nairc519ff72021-01-21 08:23:08 -08001752 token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08001753 LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture.");
Hiroki Sato25040232024-02-22 17:21:22 +09001754 LOG_ALWAYS_FATAL_IF(token != entry->pointerCaptureRequest.window,
1755 "Unexpected requested window for Pointer Capture.");
Prabir Pradhan99987712020-11-10 18:43:05 -08001756 mWindowTokenWithPointerCapture = token;
1757 } else {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001758 // Disable Pointer Capture.
1759 // We do not check if the sequence number matches for requests to disable Pointer Capture
1760 // for two reasons:
1761 // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries
1762 // to disable capture with the same sequence number: one generated by
1763 // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer
1764 // Capture being disabled in InputReader.
1765 // 2. We respect any request to disable Pointer Capture generated by InputReader, since the
1766 // actual Pointer Capture state that affects events being generated by input devices is
1767 // in InputReader.
1768 if (!haveWindowWithPointerCapture) {
1769 // Pointer capture was already forcefully disabled because of focus change.
1770 dropReason = DropReason::NOT_DROPPED;
1771 return;
1772 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001773 token = mWindowTokenWithPointerCapture;
1774 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001775 if (mCurrentPointerCaptureRequest.isEnable()) {
1776 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001777 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001778 }
1779
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001780 auto connection = getConnectionLocked(token);
1781 if (connection == nullptr) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001782 // Window has gone away, clean up Pointer Capture state.
1783 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001784 if (mCurrentPointerCaptureRequest.isEnable()) {
1785 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001786 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001787 return;
1788 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001789 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001790 dispatchEventLocked(currentTime, entry, {{connection}});
Prabir Pradhan99987712020-11-10 18:43:05 -08001791
1792 dropReason = DropReason::NOT_DROPPED;
1793}
1794
Prabir Pradhan24047542023-11-02 17:14:59 +00001795void InputDispatcher::dispatchTouchModeChangeLocked(
1796 nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001797 const std::vector<sp<WindowInfoHandle>>& windowHandles =
Antonio Kantek15beb512022-06-13 22:35:41 +00001798 getWindowHandlesLocked(entry->displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001799 if (windowHandles.empty()) {
1800 return;
1801 }
1802 const std::vector<InputTarget> inputTargets =
1803 getInputTargetsFromWindowHandlesLocked(windowHandles);
1804 if (inputTargets.empty()) {
1805 return;
1806 }
1807 entry->dispatchInProgress = true;
1808 dispatchEventLocked(currentTime, entry, inputTargets);
1809}
1810
1811std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked(
1812 const std::vector<sp<WindowInfoHandle>>& windowHandles) const {
1813 std::vector<InputTarget> inputTargets;
1814 for (const sp<WindowInfoHandle>& handle : windowHandles) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001815 const sp<IBinder>& token = handle->getToken();
1816 if (token == nullptr) {
1817 continue;
1818 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001819 std::shared_ptr<Connection> connection = getConnectionLocked(token);
1820 if (connection == nullptr) {
1821 continue; // Connection has gone away
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001822 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001823 inputTargets.emplace_back(connection);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001824 }
1825 return inputTargets;
1826}
1827
Prabir Pradhan24047542023-11-02 17:14:59 +00001828bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001829 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001831 if (!entry->dispatchInProgress) {
Hu Guofe3c8f12023-09-22 17:20:15 +08001832 if (!entry->syntheticRepeat && entry->action == AKEY_EVENT_ACTION_DOWN &&
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001833 (entry->policyFlags & POLICY_FLAG_TRUSTED) &&
1834 (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
1835 if (mKeyRepeatState.lastKeyEntry &&
Chris Ye2ad95392020-09-01 13:44:44 -07001836 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode &&
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837 // We have seen two identical key downs in a row which indicates that the device
1838 // driver is automatically generating key repeats itself. We take note of the
1839 // repeat here, but we disable our own next key repeat timer since it is clear that
1840 // we will not need to synthesize key repeats ourselves.
Chris Ye2ad95392020-09-01 13:44:44 -07001841 mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) {
1842 // Make sure we don't get key down from a different device. If a different
1843 // device Id has same key pressed down, the new device Id will replace the
1844 // current one to hold the key repeat with repeat count reset.
1845 // In the future when got a KEY_UP on the device id, drop it and do not
1846 // stop the key repeat on current device.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
1848 resetKeyRepeatLocked();
Colin Cross5b799302022-10-18 21:52:41 -07001849 mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850 } else {
1851 // Not a repeat. Save key down state in case we do see a repeat later.
1852 resetKeyRepeatLocked();
1853 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
1854 }
1855 mKeyRepeatState.lastKeyEntry = entry;
Chris Ye2ad95392020-09-01 13:44:44 -07001856 } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry &&
1857 mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001858 // The key on device 'deviceId' is still down, do not stop key repeat
Prabir Pradhan65613802023-02-22 23:36:58 +00001859 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001860 ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId);
1861 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001862 } else if (!entry->syntheticRepeat) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 resetKeyRepeatLocked();
1864 }
1865
1866 if (entry->repeatCount == 1) {
1867 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
1868 } else {
1869 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
1870 }
1871
1872 entry->dispatchInProgress = true;
1873
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001874 logOutboundKeyDetails("dispatchKey - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875 }
1876
1877 // Handle case where the policy asked us to try again later last time.
Michael Wright5caf55a2022-11-24 22:31:42 +00001878 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879 if (currentTime < entry->interceptKeyWakeupTime) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001880 nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881 return false; // wait until next wakeup
1882 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001883 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884 entry->interceptKeyWakeupTime = 0;
1885 }
1886
1887 // Give the policy a chance to intercept the key.
Michael Wright5caf55a2022-11-24 22:31:42 +00001888 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07001890 sp<IBinder> focusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08001891 mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00001892
1893 auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) {
1894 doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
1895 };
1896 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001897 return false; // wait for the command to run
1898 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00001899 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001901 } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001902 if (*dropReason == DropReason::NOT_DROPPED) {
1903 *dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 }
1905 }
1906
1907 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001908 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001909 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001910 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
1911 : InputEventInjectionResult::FAILED);
Garfield Tan6a5a14e2020-01-28 13:24:04 -08001912 mReporter->reportDroppedKey(entry->id);
Josep del Rioc8fdedb2024-05-21 13:32:43 +00001913 // Poke user activity for consumed keys, as it may have not been reported due to
1914 // the focused window requesting user activity to be disabled
1915 if (*dropReason == DropReason::POLICY &&
1916 mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
1917 pokeUserActivityLocked(*entry);
1918 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 return true;
1920 }
1921
1922 // Identify targets.
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001923 InputEventInjectionResult injectionResult;
1924 sp<WindowInfoHandle> focusedWindow =
1925 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime,
1926 /*byref*/ injectionResult);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001927 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928 return false;
1929 }
1930
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001931 setInjectionResult(*entry, injectionResult);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001932 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001933 return true;
1934 }
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001935 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
1936
1937 std::vector<InputTarget> inputTargets;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001938 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
1939 InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001940
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001941 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001942 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001944 if (mTracer) {
1945 ensureEventTraced(*entry);
1946 for (const auto& target : inputTargets) {
1947 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
1948 }
1949 }
1950
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951 // Dispatch the key.
1952 dispatchEventLocked(currentTime, entry, inputTargets);
1953 return true;
1954}
1955
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001956void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001957 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001958 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001959 "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, "
1960 "metaState=0x%x, repeatCount=%d, downTime=%" PRId64,
Linnan Li13bf76a2024-05-05 19:18:02 +08001961 prefix, entry.eventTime, entry.deviceId, entry.source,
1962 entry.displayId.toString().c_str(), entry.policyFlags, entry.action, entry.flags,
1963 entry.keyCode, entry.scanCode, entry.metaState, entry.repeatCount, entry.downTime);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001964 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001965}
1966
Prabir Pradhancef936d2021-07-21 16:17:52 +00001967void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00001968 const std::shared_ptr<const SensorEntry>& entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001969 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001970 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1971 ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, "
1972 "source=0x%x, sensorType=%s",
1973 entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source,
Dominik Laskowski75788452021-02-09 18:51:25 -08001974 ftl::enum_string(entry->sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001975 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00001976 auto command = [this, entry]() REQUIRES(mLock) {
1977 scoped_unlock unlock(mLock);
1978
1979 if (entry->accuracyChanged) {
Prabir Pradhana41d2442023-04-20 21:30:40 +00001980 mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001981 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00001982 mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy,
1983 entry->hwTimestamp, entry->values);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001984 };
1985 postCommandLocked(std::move(command));
Chris Yef59a2f42020-10-16 12:55:26 -07001986}
1987
1988bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001989 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1990 ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId,
Dominik Laskowski75788452021-02-09 18:51:25 -08001991 ftl::enum_string(sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001992 }
Chris Yef59a2f42020-10-16 12:55:26 -07001993 { // acquire lock
1994 std::scoped_lock _l(mLock);
1995
1996 for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001997 std::shared_ptr<const EventEntry> entry = *it;
Chris Yef59a2f42020-10-16 12:55:26 -07001998 if (entry->type == EventEntry::Type::SENSOR) {
1999 it = mInboundQueue.erase(it);
2000 releaseInboundEventLocked(entry);
2001 }
2002 }
2003 }
2004 return true;
2005}
2006
Prabir Pradhan24047542023-11-02 17:14:59 +00002007bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime,
2008 std::shared_ptr<const MotionEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002009 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002010 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002011 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002012 if (!entry->dispatchInProgress) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013 entry->dispatchInProgress = true;
2014
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002015 logOutboundMotionDetails("dispatchMotion - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002016 }
2017
2018 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07002019 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002020 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002021 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
2022 : InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002023 return true;
2024 }
2025
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002026 const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002027
2028 // Identify targets.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002029 std::vector<InputTarget> inputTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002030
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002031 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002032 if (isPointerEvent) {
2033 // Pointer event. (eg. touchscreen)
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00002034
2035 if (mDragState &&
2036 (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2037 // If drag and drop ongoing and pointer down occur: pilfer drag window pointers
2038 pilferPointersLocked(mDragState->dragWindow->getToken());
2039 }
2040
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002041 inputTargets =
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002042 findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult);
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002043 LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED &&
2044 !inputTargets.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002045 } else {
2046 // Non touch event. (eg. trackball)
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002047 sp<WindowInfoHandle> focusedWindow =
2048 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult);
2049 if (injectionResult == InputEventInjectionResult::SUCCEEDED) {
2050 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002051 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
2052 InputTarget::Flags::FOREGROUND, getDownTime(*entry),
2053 inputTargets);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002054 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002055 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002056 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002057 return false;
2058 }
2059
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002060 setInjectionResult(*entry, injectionResult);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002061 if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) {
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002062 return true;
2063 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002064 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightfb04fd52022-11-24 22:31:11 +00002065 CancelationOptions::Mode mode(
2066 isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS
2067 : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002068 CancelationOptions options(mode, "input event injection failed", entry->traceTracker);
Linnan Lid8150952024-01-26 18:07:17 +00002069 options.displayId = entry->displayId;
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002070 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071 return true;
2072 }
2073
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002074 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002075 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002076
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00002077 if (mTracer) {
2078 ensureEventTraced(*entry);
2079 for (const auto& target : inputTargets) {
2080 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
2081 }
2082 }
2083
Michael Wrightd02c5b62014-02-10 15:10:22 -08002084 // Dispatch the motion.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002085 dispatchEventLocked(currentTime, entry, inputTargets);
2086 return true;
2087}
2088
chaviw98318de2021-05-19 16:45:23 -05002089void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle,
Arthur Hung54745652022-04-20 07:17:41 +00002090 bool isExiting, const int32_t rawX,
2091 const int32_t rawY) {
2092 const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY));
arthurhungb89ccb02020-12-30 16:19:01 +08002093 std::unique_ptr<DragEntry> dragEntry =
Arthur Hung54745652022-04-20 07:17:41 +00002094 std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(),
2095 isExiting, xy.x, xy.y);
arthurhungb89ccb02020-12-30 16:19:01 +08002096
2097 enqueueInboundEventLocked(std::move(dragEntry));
2098}
2099
Prabir Pradhan24047542023-11-02 17:14:59 +00002100void InputDispatcher::dispatchDragLocked(nsecs_t currentTime,
2101 std::shared_ptr<const DragEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002102 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
2103 if (connection == nullptr) {
2104 return; // Connection has gone away
arthurhungb89ccb02020-12-30 16:19:01 +08002105 }
arthurhungb89ccb02020-12-30 16:19:01 +08002106 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002107 dispatchEventLocked(currentTime, entry, {{connection}});
arthurhungb89ccb02020-12-30 16:19:01 +08002108}
2109
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002110void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002111 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002112 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002113 "action=%s, actionButton=0x%x, flags=0x%x, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002114 "metaState=0x%x, buttonState=0x%x,"
2115 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64,
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002116 prefix, entry.eventTime, entry.deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +08002117 inputEventSourceToString(entry.source).c_str(), entry.displayId.toString().c_str(),
2118 entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(),
2119 entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.edgeFlags,
2120 entry.xPrecision, entry.yPrecision, entry.downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002121
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002122 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002123 ALOGD(" Pointer %d: id=%d, toolType=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002124 "x=%f, y=%f, pressure=%f, size=%f, "
2125 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
2126 "orientation=%f",
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002127 i, entry.pointerProperties[i].id,
2128 ftl::enum_string(entry.pointerProperties[i].toolType).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002129 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2130 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2131 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2132 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2133 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2134 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2135 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2136 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2137 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
2138 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002139 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002140}
2141
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002142void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00002143 std::shared_ptr<const EventEntry> eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002144 const std::vector<InputTarget>& inputTargets) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002145 ATRACE_CALL();
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002146 if (DEBUG_DISPATCH_CYCLE) {
2147 ALOGD("dispatchEventToCurrentInputTargets");
2148 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002149
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002150 processInteractionsLocked(*eventEntry, inputTargets);
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00002151
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
2153
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002154 pokeUserActivityLocked(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002155
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002156 for (const InputTarget& inputTarget : inputTargets) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002157 std::shared_ptr<Connection> connection = inputTarget.connection;
2158 prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002159 }
2160}
2161
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07002162void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002163 // We will not be breaking any connections here, even if the policy wants us to abort dispatch.
2164 // If the policy decides to close the app, we will get a channel removal event via
2165 // unregisterInputChannel, and will clean up the connection that way. We are already not
2166 // sending new pointers to the connection when it blocked, but focused events will continue to
2167 // pile up.
2168 ALOGW("Canceling events for %s because it is unresponsive",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002169 connection->getInputChannelName().c_str());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002170 if (connection->status != Connection::Status::NORMAL) {
2171 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002173 ScopedSyntheticEventTracer traceContext(mTracer);
Prabir Pradhanfc364722024-02-08 17:51:20 +00002174 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002175 "application not responding", traceContext.getTracker());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002176
2177 sp<WindowInfoHandle> windowHandle;
2178 if (!connection->monitor) {
2179 windowHandle = getWindowHandleLocked(connection->getToken());
2180 if (windowHandle == nullptr) {
2181 // The window that is receiving this ANR was removed, so there is no need to generate
2182 // cancellations, because the cancellations would have already been generated when
2183 // the window was removed.
2184 return;
2185 }
2186 }
2187 synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188}
2189
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002190void InputDispatcher::resetNoFocusedWindowTimeoutLocked() {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01002191 if (DEBUG_FOCUS) {
2192 ALOGD("Resetting ANR timeouts.");
2193 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002194
2195 // Reset input target wait timeout.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002196 mNoFocusedWindowTimeoutTime = std::nullopt;
Chris Yea209fde2020-07-22 13:54:51 -07002197 mAwaitedFocusedApplication.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002198}
2199
Tiger Huang721e26f2018-07-24 22:26:19 +08002200/**
2201 * Get the display id that the given event should go to. If this event specifies a valid display id,
2202 * then it should be dispatched to that display. Otherwise, the event goes to the focused display.
2203 * Focused display is the display that the user most recently interacted with.
2204 */
Linnan Li13bf76a2024-05-05 19:18:02 +08002205ui::LogicalDisplayId InputDispatcher::getTargetDisplayId(const EventEntry& entry) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002206 ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID};
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002207 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002208 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002209 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
2210 displayId = keyEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002211 break;
2212 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002213 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002214 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
2215 displayId = motionEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002216 break;
2217 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00002218 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08002219 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002220 case EventEntry::Type::FOCUS:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002221 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07002222 case EventEntry::Type::DEVICE_RESET:
arthurhungb89ccb02020-12-30 16:19:01 +08002223 case EventEntry::Type::SENSOR:
2224 case EventEntry::Type::DRAG: {
Dominik Laskowski75788452021-02-09 18:51:25 -08002225 ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002226 return ui::LogicalDisplayId::INVALID;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002227 }
Tiger Huang721e26f2018-07-24 22:26:19 +08002228 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002229 return displayId == ui::LogicalDisplayId::INVALID ? mFocusedDisplayId : displayId;
Tiger Huang721e26f2018-07-24 22:26:19 +08002230}
2231
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002232bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime,
2233 const char* focusedWindowName) {
2234 if (mAnrTracker.empty()) {
2235 // already processed all events that we waited for
2236 mKeyIsWaitingForEventsTimeout = std::nullopt;
2237 return false;
2238 }
2239
2240 if (!mKeyIsWaitingForEventsTimeout.has_value()) {
2241 // Start the timer
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00002242 // Wait to send key because there are unprocessed events that may cause focus to change
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002243 mKeyIsWaitingForEventsTimeout = currentTime +
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08002244 std::chrono::duration_cast<std::chrono::nanoseconds>(
2245 mPolicy.getKeyWaitingForEventsTimeout())
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002246 .count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002247 return true;
2248 }
2249
2250 // We still have pending events, and already started the timer
2251 if (currentTime < *mKeyIsWaitingForEventsTimeout) {
2252 return true; // Still waiting
2253 }
2254
2255 // Waited too long, and some connection still hasn't processed all motions
2256 // Just send the key to the focused window
2257 ALOGW("Dispatching key to %s even though there are other unprocessed events",
2258 focusedWindowName);
2259 mKeyIsWaitingForEventsTimeout = std::nullopt;
2260 return false;
2261}
2262
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002263sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked(
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002264 nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime,
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002265 InputEventInjectionResult& outInjectionResult) {
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002266 outInjectionResult = InputEventInjectionResult::FAILED; // Default result
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267
Linnan Li13bf76a2024-05-05 19:18:02 +08002268 ui::LogicalDisplayId displayId = getTargetDisplayId(entry);
chaviw98318de2021-05-19 16:45:23 -05002269 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Chris Yea209fde2020-07-22 13:54:51 -07002270 std::shared_ptr<InputApplicationHandle> focusedApplicationHandle =
Tiger Huang721e26f2018-07-24 22:26:19 +08002271 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
2272
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 // If there is no currently focused window and no focused application
2274 // then drop the event.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002275 if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) {
2276 ALOGI("Dropping %s event because there is no focused window or focused application in "
Linnan Li13bf76a2024-05-05 19:18:02 +08002277 "display %s.",
2278 ftl::enum_string(entry.type).c_str(), displayId.toString().c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002279 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280 }
2281
Vishnu Nair062a8672021-09-03 16:07:44 -07002282 // Drop key events if requested by input feature
2283 if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) {
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002284 return nullptr;
Vishnu Nair062a8672021-09-03 16:07:44 -07002285 }
2286
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002287 // Compatibility behavior: raise ANR if there is a focused application, but no focused window.
2288 // Only start counting when we have a focused event to dispatch. The ANR is canceled if we
2289 // start interacting with another application via touch (app switch). This code can be removed
2290 // if the "no focused window ANR" is moved to the policy. Input doesn't know whether
2291 // an app is expected to have a focused window.
2292 if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) {
2293 if (!mNoFocusedWindowTimeoutTime.has_value()) {
2294 // We just discovered that there's no focused window. Start the ANR timer
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002295 std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout(
2296 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
2297 mNoFocusedWindowTimeoutTime = currentTime + timeout.count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002298 mAwaitedFocusedApplication = focusedApplicationHandle;
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05002299 mAwaitedApplicationDisplayId = displayId;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002300 ALOGW("Waiting because no window has focus but %s may eventually add a "
2301 "window when it finishes starting up. Will wait for %" PRId64 "ms",
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002302 mAwaitedFocusedApplication->getName().c_str(), millis(timeout));
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002303 nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002304 outInjectionResult = InputEventInjectionResult::PENDING;
2305 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002306 } else if (currentTime > *mNoFocusedWindowTimeoutTime) {
2307 // Already raised ANR. Drop the event
2308 ALOGE("Dropping %s event because there is no focused window",
Dominik Laskowski75788452021-02-09 18:51:25 -08002309 ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002310 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002311 } else {
2312 // Still waiting for the focused window
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002313 outInjectionResult = InputEventInjectionResult::PENDING;
2314 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002315 }
2316 }
2317
2318 // we have a valid, non-null focused window
2319 resetNoFocusedWindowTimeoutLocked();
2320
Prabir Pradhan5735a322022-04-11 17:23:34 +00002321 // Verify targeted injection.
2322 if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) {
2323 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002324 outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH;
2325 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 }
2327
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002328 if (focusedWindowHandle->getInfo()->inputConfig.test(
2329 WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002330 ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002331 outInjectionResult = InputEventInjectionResult::PENDING;
2332 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002333 }
2334
2335 // If the event is a key event, then we must wait for all previous events to
2336 // complete before delivering it because previous events may have the
2337 // side-effect of transferring focus to a different window and we want to
2338 // ensure that the following keys are sent to the new window.
2339 //
2340 // Suppose the user touches a button in a window then immediately presses "A".
2341 // If the button causes a pop-up window to appear then we want to ensure that
2342 // the "A" key is delivered to the new pop-up window. This is because users
2343 // often anticipate pending UI changes when typing on a keyboard.
2344 // To obtain this behavior, we must serialize key events with respect to all
2345 // prior input events.
2346 if (entry.type == EventEntry::Type::KEY) {
2347 if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002348 nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002349 outInjectionResult = InputEventInjectionResult::PENDING;
2350 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002351 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 }
2353
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002354 outInjectionResult = InputEventInjectionResult::SUCCEEDED;
2355 return focusedWindowHandle;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356}
2357
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002358/**
2359 * Given a list of monitors, remove the ones we cannot find a connection for, and the ones
2360 * that are currently unresponsive.
2361 */
Prabir Pradhan0a99c922021-09-03 08:27:53 -07002362std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked(
2363 const std::vector<Monitor>& monitors) const {
2364 std::vector<Monitor> responsiveMonitors;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002365 std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors),
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002366 [](const Monitor& monitor) REQUIRES(mLock) {
2367 std::shared_ptr<Connection> connection = monitor.connection;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002368 if (!connection->responsive) {
2369 ALOGW("Unresponsive monitor %s will not get the new gesture",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002370 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002371 return false;
2372 }
2373 return true;
2374 });
2375 return responsiveMonitors;
2376}
2377
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002378std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002379 nsecs_t currentTime, const MotionEntry& entry,
Siarhei Vishniakou4fe57392022-10-25 13:44:30 -07002380 InputEventInjectionResult& outInjectionResult) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002381 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002383 std::vector<InputTarget> targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 // For security reasons, we defer updating the touch state until we are sure that
2385 // event injection will be allowed.
Linnan Li13bf76a2024-05-05 19:18:02 +08002386 const ui::LogicalDisplayId displayId = entry.displayId;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002387 const int32_t action = entry.action;
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07002388 const int32_t maskedAction = MotionEvent::getActionMasked(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389
2390 // Update the touch state as needed based on the properties of the touch event.
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002391 outInjectionResult = InputEventInjectionResult::PENDING;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002393 // Copy current touch state into tempTouchState.
2394 // This state will be used to update mTouchStatesByDisplay at the end of this function.
2395 // If no state for the specified display exists, then our initial state will be empty.
Yi Kong9b14ac62018-07-17 13:48:38 -07002396 const TouchState* oldState = nullptr;
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002397 TouchState tempTouchState;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002398 if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
2399 oldState = &(it->second);
Prabir Pradhane680f9b2022-02-04 04:24:00 -08002400 tempTouchState = *oldState;
Jeff Brownf086ddb2014-02-11 14:28:48 -08002401 }
2402
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002403 bool isSplit = shouldSplitTouch(tempTouchState, entry);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002404
2405 const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE ||
2406 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2407 maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002408 // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any
2409 // touchable windows.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002410 const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002411 const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) ||
2412 (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002413 const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL ||
2414 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2415 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE;
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002416 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002417
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002418 if (newGesture) {
2419 isSplit = false;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002420 }
2421
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002422 if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) {
2423 // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated.
2424 tempTouchState.clearHoveringPointers(entry.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 }
2426
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002427 if (isHoverAction) {
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002428 if (wasDown) {
2429 // Started hovering, but the device is already down: reject the hover event
2430 LOG(ERROR) << "Got hover event " << entry.getDescription()
2431 << " but the device is already down " << oldState->dump();
2432 outInjectionResult = InputEventInjectionResult::FAILED;
2433 return {};
2434 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002435 // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase
2436 // all of the existing hovering pointers and recompute.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002437 tempTouchState.clearHoveringPointers(entry.deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002438 }
2439
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
2441 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002442 const auto [x, y] = resolveTouchedPosition(entry);
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002443 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002444 const PointerProperties& pointer = entry.pointerProperties[pointerIndex];
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002445 // Outside targets should be added upon first dispatched DOWN event. That means, this should
2446 // be a pointer that would generate ACTION_DOWN, *and* touch should not already be down.
Prabir Pradhand65552b2021-10-07 11:23:50 -07002447 const bool isStylus = isPointerFromStylus(entry, pointerIndex);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002448 sp<WindowInfoHandle> newTouchedWindowHandle =
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002449 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Michael Wright3dd60e22019-03-27 22:06:44 +00002450
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002451 if (isDown) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002452 targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002453 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 // Handle the case where we did not find a window.
Yi Kong9b14ac62018-07-17 13:48:38 -07002455 if (newTouchedWindowHandle == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002456 ALOGD("No new touched window at (%.1f, %.1f) in display %s", x, y,
2457 displayId.toString().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 // Try to assign the pointer to the first foreground window we find, if there is one.
Linnan Liccf6ce32024-04-11 20:32:13 +08002459 newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Michael Wright3dd60e22019-03-27 22:06:44 +00002460 }
2461
Prabir Pradhan5735a322022-04-11 17:23:34 +00002462 // Verify targeted injection.
2463 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2464 ALOGW("Dropping injected touch event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002465 outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH;
Prabir Pradhan5735a322022-04-11 17:23:34 +00002466 newTouchedWindowHandle = nullptr;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002467 return {};
Prabir Pradhan5735a322022-04-11 17:23:34 +00002468 }
2469
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002470 // Figure out whether splitting will be allowed for this window.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002471 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002472 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
2473 // New window supports splitting, but we should never split mouse events.
2474 isSplit = !isFromMouse;
2475 } else if (isSplit) {
2476 // New window does not support splitting but we have already split events.
2477 // Ignore the new window.
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002478 LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName()
2479 << " because it doesn't support split touch";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002480 newTouchedWindowHandle = nullptr;
2481 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002482 } else {
2483 // No window is touched, so set split to true. This will allow the next pointer down to
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002484 // be delivered to a new window which supports split touch. Pointers from a mouse device
2485 // should never be split.
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002486 isSplit = !isFromMouse;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002487 }
2488
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002489 std::vector<sp<WindowInfoHandle>> newTouchedWindows =
Prabir Pradhand65552b2021-10-07 11:23:50 -07002490 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002491 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002492 // Process the foreground window first so that it is the first to receive the event.
2493 newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002494 }
2495
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002496 if (newTouchedWindows.empty()) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00002497 ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display "
Linnan Li13bf76a2024-05-05 19:18:02 +08002498 "%s.",
2499 x, y, displayId.toString().c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002500 outInjectionResult = InputEventInjectionResult::FAILED;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002501 return {};
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002502 }
2503
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002504 for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002505 if (!canWindowReceiveMotionLocked(windowHandle, entry)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002506 continue;
2507 }
2508
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002509 if (isHoverAction) {
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07002510 // The "windowHandle" is the target of this hovering pointer.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002511 tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002512 }
2513
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002514 // Set target flags.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002515 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002516
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002517 if (canReceiveForegroundTouches(*windowHandle->getInfo())) {
2518 // There should only be one touched window that can be "foreground" for the pointer.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002519 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002520 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002521
2522 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002523 targetFlags |= InputTarget::Flags::SPLIT;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002524 }
2525 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002526 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002527 } else if (isWindowObscuredLocked(windowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002528 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002529 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002530
2531 // Update the temporary touch state.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002532
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002533 if (!isHoverAction) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002534 const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN ||
2535 maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN;
Siarhei Vishniakoue535f972024-03-29 14:55:21 -07002536 Result<void> addResult =
2537 tempTouchState.addOrUpdateWindow(windowHandle,
2538 InputTarget::DispatchMode::AS_IS,
2539 targetFlags, entry.deviceId, {pointer},
2540 isDownOrPointerDown
2541 ? std::make_optional(
2542 entry.eventTime)
2543 : std::nullopt);
2544 if (!addResult.ok()) {
2545 LOG(ERROR) << "Error while processing " << entry << " for "
2546 << windowHandle->getName();
2547 logDispatchStateLocked();
2548 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002549 // If this is the pointer going down and the touched window has a wallpaper
2550 // then also add the touched wallpaper windows so they are locked in for the
2551 // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or
2552 // SCROLL because the wallpaper engine only supports touch events. We would need to
2553 // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to
2554 // handle these events.
2555 if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) &&
Arthur Hungc539dbb2022-12-08 07:45:36 +00002556 windowHandle->getInfo()->inputConfig.test(
2557 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
2558 sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle);
2559 if (wallpaper != nullptr) {
2560 ftl::Flags<InputTarget::Flags> wallpaperFlags =
2561 InputTarget::Flags::WINDOW_IS_OBSCURED |
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002562 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Arthur Hungc539dbb2022-12-08 07:45:36 +00002563 if (isSplit) {
2564 wallpaperFlags |= InputTarget::Flags::SPLIT;
2565 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002566 tempTouchState.addOrUpdateWindow(wallpaper,
2567 InputTarget::DispatchMode::AS_IS,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002568 wallpaperFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002569 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002570 }
2571 }
2572 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002574
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002575 // If a window is already pilfering some pointers, give it this new pointer as well and
2576 // make it pilfering. This will prevent other non-spy windows from getting this pointer,
2577 // which is a specific behaviour that we want.
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002578 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002579 if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) &&
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002580 touchedWindow.hasPilferingPointers(entry.deviceId)) {
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002581 // This window is already pilfering some pointers, and this new pointer is also
2582 // going to it. Therefore, take over this pointer and don't give it to anyone
2583 // else.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002584 touchedWindow.addPilferingPointer(entry.deviceId, pointer.id);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002585 }
2586 }
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002587
2588 // Restrict all pilfered pointers to the pilfering windows.
2589 tempTouchState.cancelPointersForNonPilferingWindows();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 } else {
2591 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
2592
2593 // If the pointer is not currently down, then ignore the event.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002594 if (!tempTouchState.isDown(entry.deviceId) &&
2595 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Siarhei Vishniakou31dd1552023-10-30 18:46:10 -07002596 if (DEBUG_DROPPED_EVENTS_VERBOSE) {
2597 LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId
2598 << " is not down or we previously dropped the pointer down event in "
2599 << "display " << displayId << ": " << entry.getDescription();
2600 }
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002601 outInjectionResult = InputEventInjectionResult::FAILED;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002602 return {};
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();
2613 outInjectionResult = InputEventInjectionResult::FAILED;
2614 return {};
2615 }
2616 tempTouchState.removeHoveringPointer(entry.deviceId, pointerId);
2617 }
2618
arthurhung6d4bed92021-03-17 11:59:33 +08002619 addDragEventLocked(entry);
arthurhungb89ccb02020-12-30 16:19:01 +08002620
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621 // Check whether touches should slip outside of the current foreground window.
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002622 if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 &&
Linnan Li49b2b202024-04-12 12:46:40 +08002623 tempTouchState.isSlippery(entry.deviceId)) {
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07002624 const auto [x, y] = resolveTouchedPosition(entry);
Harry Cutts33476232023-01-30 19:57:29 +00002625 const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0);
chaviw98318de2021-05-19 16:45:23 -05002626 sp<WindowInfoHandle> oldTouchedWindowHandle =
Linnan Liccf6ce32024-04-11 20:32:13 +08002627 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002628 LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002629 sp<WindowInfoHandle> newTouchedWindowHandle =
2630 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Vishnu Nair062a8672021-09-03 16:07:44 -07002631
Prabir Pradhan5735a322022-04-11 17:23:34 +00002632 // Verify targeted injection.
2633 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2634 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002635 outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002636 return {};
Prabir Pradhan5735a322022-04-11 17:23:34 +00002637 }
2638
Linnan Libf069a32024-02-29 17:22:59 +00002639 // Do not slide events to the window which can not receive motion event
Vishnu Nair062a8672021-09-03 16:07:44 -07002640 if (newTouchedWindowHandle != nullptr &&
Linnan Libf069a32024-02-29 17:22:59 +00002641 !canWindowReceiveMotionLocked(newTouchedWindowHandle, entry)) {
Vishnu Nair062a8672021-09-03 16:07:44 -07002642 newTouchedWindowHandle = nullptr;
2643 }
2644
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07002645 if (newTouchedWindowHandle != nullptr &&
2646 !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002647 ALOGI("Touch is slipping out of window %s into window %s in display %s",
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002648 oldTouchedWindowHandle->getName().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08002649 newTouchedWindowHandle->getName().c_str(), displayId.toString().c_str());
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002650
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 // Make a slippery exit from the old window.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08002652 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002653 const PointerProperties& pointer = entry.pointerProperties[0];
2654 pointerIds.set(pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002655
2656 const TouchedWindow& touchedWindow =
2657 tempTouchState.getTouchedWindow(oldTouchedWindowHandle);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002658 addPointerWindowTargetLocked(oldTouchedWindowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002659 InputTarget::DispatchMode::SLIPPERY_EXIT,
2660 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002661 touchedWindow.getDownTimeInTarget(entry.deviceId),
2662 targets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
2664 // Make a slippery entrance into the new window.
2665 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002666 isSplit = !isFromMouse;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 }
2668
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002669 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002670 if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002671 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002672 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002674 targetFlags |= InputTarget::Flags::SPLIT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675 }
2676 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002677 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002678 } else if (isWindowObscuredLocked(newTouchedWindowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002679 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 }
2681
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002682 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle,
2683 InputTarget::DispatchMode::SLIPPERY_ENTER,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002684 targetFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002685 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002686
2687 // Check if the wallpaper window should deliver the corresponding event.
2688 slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002689 tempTouchState, entry.deviceId, pointer, targets);
2690 tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002691 oldTouchedWindowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 }
2693 }
Arthur Hung96483742022-11-15 03:30:48 +00002694
2695 // Update the pointerIds for non-splittable when it received pointer down.
2696 if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2697 // If no split, we suppose all touched windows should receive pointer down.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002698 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002699 std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]};
2700 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Arthur Hung96483742022-11-15 03:30:48 +00002701 // Ignore drag window for it should just track one pointer.
2702 if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) {
2703 continue;
2704 }
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002705 touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers);
Arthur Hung96483742022-11-15 03:30:48 +00002706 }
2707 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002710 // Update dispatching for hover enter and exit.
Sam Dubeyf886dec2023-01-27 13:28:19 +00002711 {
2712 std::vector<TouchedWindow> hoveringWindows =
2713 getHoveringWindowsLocked(oldState, tempTouchState, entry);
Linnan Li41859c42024-03-05 16:20:34 +00002714 // Hardcode to single hovering pointer for now.
2715 std::bitset<MAX_POINTER_ID + 1> pointerIds;
2716 pointerIds.set(entry.pointerProperties[0].id);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002717 for (const TouchedWindow& touchedWindow : hoveringWindows) {
Linnan Li41859c42024-03-05 16:20:34 +00002718 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
2719 touchedWindow.targetFlags, pointerIds,
2720 touchedWindow.getDownTimeInTarget(entry.deviceId),
2721 targets);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002722 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724
Prabir Pradhan5735a322022-04-11 17:23:34 +00002725 // Ensure that all touched windows are valid for injection.
2726 if (entry.injectionState != nullptr) {
2727 std::string errs;
2728 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00002729 const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry);
2730 if (err) errs += "\n - " + *err;
2731 }
2732 if (!errs.empty()) {
2733 ALOGW("Dropping targeted injection: At least one touched window is not owned by uid "
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002734 "%s:%s",
2735 entry.injectionState->targetUid->toString().c_str(), errs.c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002736 outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002737 return {};
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();
2794 outInjectionResult = InputEventInjectionResult::FAILED;
2795 return {};
2796 }
2797
2798 // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no
2799 // window that is actually receiving the entire gesture.
2800 if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002801 return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002802 })) {
2803 LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: "
2804 << entry.getDescription();
2805 outInjectionResult = InputEventInjectionResult::FAILED;
2806 return {};
2807 }
2808
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002809 outInjectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Prabir Pradhan502a7252023-12-01 16:11:24 +00002811 // Now that we have generated all of the input targets for this event, reset the dispatch
2812 // mode for all touched window to AS_IS.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002813 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan502a7252023-12-01 16:11:24 +00002814 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002815 }
2816
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002817 // Update final pieces of touch state if the injector had permission.
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002818 if (maskedAction == AMOTION_EVENT_ACTION_UP) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002819 // Pointer went up.
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002820 tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002821 } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002822 // All pointers up or canceled.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002823 tempTouchState.removeAllPointersForDevice(entry.deviceId);
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002824 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2825 // One pointer went up.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002826 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
2827 const uint32_t pointerId = entry.pointerProperties[pointerIndex].id;
2828 tempTouchState.removeTouchingPointer(entry.deviceId, pointerId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 }
2830
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002831 // Save changes unless the action was scroll in which case the temporary touch
2832 // state was only valid for this one action.
2833 if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002834 if (displayId >= ui::LogicalDisplayId::DEFAULT) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002835 tempTouchState.clearWindowsWithoutPointers();
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002836 mTouchStatesByDisplay[displayId] = tempTouchState;
2837 } else {
2838 mTouchStatesByDisplay.erase(displayId);
2839 }
2840 }
2841
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002842 if (tempTouchState.windows.empty()) {
2843 mTouchStatesByDisplay.erase(displayId);
2844 }
2845
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002846 return targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847}
2848
Linnan Li13bf76a2024-05-05 19:18:02 +08002849void InputDispatcher::finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07002850 // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we
2851 // have an explicit reason to support it.
2852 constexpr bool isStylus = false;
2853
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002854 sp<WindowInfoHandle> dropWindow =
Harry Cutts33476232023-01-30 19:57:29 +00002855 findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true);
arthurhung6d4bed92021-03-17 11:59:33 +08002856 if (dropWindow) {
2857 vec2 local = dropWindow->getInfo()->transform.transform(x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002858 sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y);
Arthur Hung6d0571e2021-04-09 20:18:16 +08002859 } else {
Arthur Hung54745652022-04-20 07:17:41 +00002860 ALOGW("No window found when drop.");
Prabir Pradhancef936d2021-07-21 16:17:52 +00002861 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08002862 }
2863 mDragState.reset();
2864}
2865
2866void InputDispatcher::addDragEventLocked(const MotionEntry& entry) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00002867 if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) {
arthurhungb89ccb02020-12-30 16:19:01 +08002868 return;
2869 }
2870
arthurhung6d4bed92021-03-17 11:59:33 +08002871 if (!mDragState->isStartDrag) {
2872 mDragState->isStartDrag = true;
2873 mDragState->isStylusButtonDownAtStart =
2874 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2875 }
2876
Arthur Hung54745652022-04-20 07:17:41 +00002877 // Find the pointer index by id.
2878 int32_t pointerIndex = 0;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002879 for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) {
Arthur Hung54745652022-04-20 07:17:41 +00002880 const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex];
2881 if (pointerProperties.id == mDragState->pointerId) {
2882 break;
arthurhung6d4bed92021-03-17 11:59:33 +08002883 }
Arthur Hung54745652022-04-20 07:17:41 +00002884 }
arthurhung6d4bed92021-03-17 11:59:33 +08002885
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002886 if (uint32_t(pointerIndex) == entry.getPointerCount()) {
Arthur Hung54745652022-04-20 07:17:41 +00002887 LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId);
Arthur Hung54745652022-04-20 07:17:41 +00002888 }
2889
2890 const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK;
2891 const int32_t x = entry.pointerCoords[pointerIndex].getX();
2892 const int32_t y = entry.pointerCoords[pointerIndex].getY();
2893
2894 switch (maskedAction) {
2895 case AMOTION_EVENT_ACTION_MOVE: {
2896 // Handle the special case : stylus button no longer pressed.
2897 bool isStylusButtonDown =
2898 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2899 if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) {
2900 finishDragAndDrop(entry.displayId, x, y);
2901 return;
2902 }
2903
2904 // Prevent stylus interceptor windows from affecting drag and drop behavior for now,
2905 // until we have an explicit reason to support it.
2906 constexpr bool isStylus = false;
2907
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002908 sp<WindowInfoHandle> hoverWindowHandle =
2909 findTouchedWindowAtLocked(entry.displayId, x, y, isStylus,
2910 /*ignoreDragWindow=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00002911 // enqueue drag exit if needed.
2912 if (hoverWindowHandle != mDragState->dragHoverWindowHandle &&
2913 !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) {
2914 if (mDragState->dragHoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002915 enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x,
Arthur Hung54745652022-04-20 07:17:41 +00002916 y);
2917 }
2918 mDragState->dragHoverWindowHandle = hoverWindowHandle;
2919 }
2920 // enqueue drag location if needed.
2921 if (hoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002922 enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y);
Arthur Hung54745652022-04-20 07:17:41 +00002923 }
2924 break;
2925 }
2926
2927 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002928 if (MotionEvent::getActionIndex(entry.action) != pointerIndex) {
Arthur Hung54745652022-04-20 07:17:41 +00002929 break;
2930 }
2931 // The drag pointer is up.
2932 [[fallthrough]];
2933 case AMOTION_EVENT_ACTION_UP:
2934 finishDragAndDrop(entry.displayId, x, y);
2935 break;
2936 case AMOTION_EVENT_ACTION_CANCEL: {
2937 ALOGD("Receiving cancel when drag and drop.");
2938 sendDropWindowCommandLocked(nullptr, 0, 0);
2939 mDragState.reset();
2940 break;
2941 }
arthurhungb89ccb02020-12-30 16:19:01 +08002942 }
2943}
2944
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002945std::optional<InputTarget> InputDispatcher::createInputTargetLocked(
2946 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002947 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002948 std::optional<nsecs_t> firstDownTimeInTarget) const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002949 std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken());
2950 if (connection == nullptr) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002951 ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str());
2952 return {};
2953 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002954 InputTarget inputTarget{connection};
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002955 inputTarget.windowHandle = windowHandle;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002956 inputTarget.dispatchMode = dispatchMode;
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002957 inputTarget.flags = targetFlags;
2958 inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor;
2959 inputTarget.firstDownTimeInTarget = firstDownTimeInTarget;
2960 const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId);
2961 if (displayInfoIt != mDisplayInfos.end()) {
2962 inputTarget.displayTransform = displayInfoIt->second.transform;
2963 } else {
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002964 // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId.
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002965 // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed.
2966 }
2967 return inputTarget;
2968}
2969
chaviw98318de2021-05-19 16:45:23 -05002970void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002971 InputTarget::DispatchMode dispatchMode,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002972 ftl::Flags<InputTarget::Flags> targetFlags,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002973 std::optional<nsecs_t> firstDownTimeInTarget,
Siarhei Vishniakouf75cddb2022-10-25 10:42:16 -07002974 std::vector<InputTarget>& inputTargets) const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002975 std::vector<InputTarget>::iterator it =
2976 std::find_if(inputTargets.begin(), inputTargets.end(),
2977 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002978 return inputTarget.connection->getToken() == windowHandle->getToken();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002979 });
Chavi Weingarten97b8eec2020-01-09 18:09:08 +00002980
chaviw98318de2021-05-19 16:45:23 -05002981 const WindowInfo* windowInfo = windowHandle->getInfo();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002982
2983 if (it == inputTargets.end()) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002984 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002985 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
2986 firstDownTimeInTarget);
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002987 if (!target) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002988 return;
2989 }
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002990 inputTargets.push_back(*target);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002991 it = inputTargets.end() - 1;
2992 }
2993
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07002994 if (it->flags != targetFlags) {
2995 LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it;
2996 }
2997 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
2998 LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
2999 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
3000 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003001}
3002
3003void InputDispatcher::addPointerWindowTargetLocked(
3004 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003005 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
3006 std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget,
3007 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003008 if (pointerIds.none()) {
3009 for (const auto& target : inputTargets) {
3010 LOG(INFO) << "Target: " << target;
3011 }
3012 LOG(FATAL) << "No pointers specified for " << windowHandle->getName();
3013 return;
3014 }
3015 std::vector<InputTarget>::iterator it =
3016 std::find_if(inputTargets.begin(), inputTargets.end(),
3017 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003018 return inputTarget.connection->getToken() == windowHandle->getToken();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003019 });
3020
3021 // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that
3022 // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would
3023 // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate
3024 // input targets for hovering pointers and for touching pointers.
3025 // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new
3026 // target instead.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003027 if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003028 // Force the code below to create a new input target
3029 it = inputTargets.end();
3030 }
3031
3032 const WindowInfo* windowInfo = windowHandle->getInfo();
3033
3034 if (it == inputTargets.end()) {
3035 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003036 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
3037 firstDownTimeInTarget);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003038 if (!target) {
3039 return;
3040 }
3041 inputTargets.push_back(*target);
3042 it = inputTargets.end() - 1;
3043 }
3044
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003045 if (it->dispatchMode != dispatchMode) {
3046 LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode="
3047 << ftl::enum_string(dispatchMode) << ", it=" << *it;
3048 }
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003049 if (it->flags != targetFlags) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003050 LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string()
3051 << ", it=" << *it;
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003052 }
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003053 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003054 LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003055 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
3056 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003057
Siarhei Vishniakou59967592024-04-01 16:17:46 -07003058 Result<void> result = it->addPointers(pointerIds, windowInfo->transform);
3059 if (!result.ok()) {
3060 logDispatchStateLocked();
3061 LOG(FATAL) << result.error().message();
3062 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063}
3064
Michael Wright3dd60e22019-03-27 22:06:44 +00003065void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
Linnan Li13bf76a2024-05-05 19:18:02 +08003066 ui::LogicalDisplayId displayId) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003067 auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId);
3068 if (monitorsIt == mGlobalMonitorsByDisplay.end()) return;
Michael Wright3dd60e22019-03-27 22:06:44 +00003069
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003070 for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003071 InputTarget target{monitor.connection};
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003072 // target.firstDownTimeInTarget is not set for global monitors. It is only required in split
3073 // touch and global monitoring works as intended even without setting firstDownTimeInTarget
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003074 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
3075 target.displayTransform = it->second.transform;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003076 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003077 target.setDefaultPointerTransform(target.displayTransform);
3078 inputTargets.push_back(target);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 }
3080}
3081
Robert Carrc9bf1d32020-04-13 17:21:08 -07003082/**
3083 * Indicate whether one window handle should be considered as obscuring
3084 * another window handle. We only check a few preconditions. Actually
3085 * checking the bounds is left to the caller.
3086 */
chaviw98318de2021-05-19 16:45:23 -05003087static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
3088 const sp<WindowInfoHandle>& otherHandle) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003089 // Compare by token so cloned layers aren't counted
3090 if (haveSameToken(windowHandle, otherHandle)) {
3091 return false;
3092 }
3093 auto info = windowHandle->getInfo();
3094 auto otherInfo = otherHandle->getInfo();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003095 if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003096 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003097 } else if (otherInfo->alpha == 0 &&
3098 otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) {
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003099 // Those act as if they were invisible, so we don't need to flag them.
3100 // We do want to potentially flag touchable windows even if they have 0
3101 // opacity, since they can consume touches and alter the effects of the
3102 // user interaction (eg. apps that rely on
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003103 // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003104 // windows), hence we also check for FLAG_NOT_TOUCHABLE.
3105 return false;
Bernardo Rufino8007daf2020-09-22 09:40:01 +00003106 } else if (info->ownerUid == otherInfo->ownerUid) {
3107 // If ownerUid is the same we don't generate occlusion events as there
3108 // is no security boundary within an uid.
Robert Carrc9bf1d32020-04-13 17:21:08 -07003109 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003110 } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003111 return false;
3112 } else if (otherInfo->displayId != info->displayId) {
3113 return false;
3114 }
3115 return true;
3116}
3117
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003118/**
3119 * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is
3120 * untrusted, one should check:
3121 *
3122 * 1. If result.hasBlockingOcclusion is true.
3123 * If it's, it means the touch should be blocked due to a window with occlusion mode of
3124 * BLOCK_UNTRUSTED.
3125 *
3126 * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch.
3127 * If it is (and 1 is false), then the touch should be blocked because a stack of windows
3128 * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed
3129 * obscuring opacity above the threshold. Note that if there was no window of occlusion mode
3130 * USE_OPACITY, result.obscuringOpacity would've been 0 and since
3131 * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true.
3132 *
3133 * If neither of those is true, then it means the touch can be allowed.
3134 */
3135InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
Linnan Li5e5645e2024-03-05 14:43:05 +00003136 const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
chaviw98318de2021-05-19 16:45:23 -05003137 const WindowInfo* windowInfo = windowHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003138 ui::LogicalDisplayId displayId = windowInfo->displayId;
chaviw98318de2021-05-19 16:45:23 -05003139 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003140 TouchOcclusionInfo info;
3141 info.hasBlockingOcclusion = false;
3142 info.obscuringOpacity = 0;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003143 info.obscuringUid = gui::Uid::INVALID;
3144 std::map<gui::Uid, float> opacityByUid;
chaviw98318de2021-05-19 16:45:23 -05003145 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003146 if (windowHandle == otherHandle) {
3147 break; // All future windows are below us. Exit early.
3148 }
chaviw98318de2021-05-19 16:45:23 -05003149 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li5e5645e2024-03-05 14:43:05 +00003150 if (canBeObscuredBy(windowHandle, otherHandle) &&
3151 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId)) &&
Bernardo Rufino1ff9d592021-01-18 16:58:57 +00003152 !haveSameApplicationToken(windowInfo, otherInfo)) {
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003153 if (DEBUG_TOUCH_OCCLUSION) {
3154 info.debugInfo.push_back(
Harry Cutts101ee9b2023-07-06 18:04:14 +00003155 dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003156 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003157 // canBeObscuredBy() has returned true above, which means this window is untrusted, so
3158 // we perform the checks below to see if the touch can be propagated or not based on the
3159 // window's touch occlusion mode
3160 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) {
3161 info.hasBlockingOcclusion = true;
3162 info.obscuringUid = otherInfo->ownerUid;
3163 info.obscuringPackage = otherInfo->packageName;
3164 break;
3165 }
3166 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003167 const auto uid = otherInfo->ownerUid;
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003168 float opacity =
3169 (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid];
3170 // Given windows A and B:
3171 // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)]
3172 opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha);
3173 opacityByUid[uid] = opacity;
3174 if (opacity > info.obscuringOpacity) {
3175 info.obscuringOpacity = opacity;
3176 info.obscuringUid = uid;
3177 info.obscuringPackage = otherInfo->packageName;
3178 }
3179 }
3180 }
3181 }
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003182 if (DEBUG_TOUCH_OCCLUSION) {
Harry Cutts101ee9b2023-07-06 18:04:14 +00003183 info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003184 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003185 return info;
3186}
3187
chaviw98318de2021-05-19 16:45:23 -05003188std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003189 bool isTouchedWindow) const {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003190 return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003191 "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3192 "], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3193 "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003194 isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003195 info->ownerUid.toString().c_str(), info->id,
Chavi Weingarten7f019192023-08-08 20:39:01 +00003196 toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3197 info->frame.top, info->frame.right, info->frame.bottom,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003198 dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3199 info->inputConfig.string().c_str(), toString(info->token != nullptr),
3200 info->applicationInfo.name.c_str(),
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003201 binderToString(info->applicationInfo.token).c_str());
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003202}
3203
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003204bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3205 if (occlusionInfo.hasBlockingOcclusion) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003206 ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
3207 occlusionInfo.obscuringUid.toString().c_str());
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003208 return false;
3209 }
3210 if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003211 ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = "
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003212 "%.2f, maximum allowed = %.2f)",
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003213 occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(),
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003214 occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch);
3215 return false;
3216 }
3217 return true;
3218}
3219
chaviw98318de2021-05-19 16:45:23 -05003220bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle,
Linnan Li5e5645e2024-03-05 14:43:05 +00003221 float x, float y) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003222 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003223 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3224 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003225 if (windowHandle == otherHandle) {
3226 break; // All future windows are below us. Exit early.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 }
chaviw98318de2021-05-19 16:45:23 -05003228 const WindowInfo* otherInfo = otherHandle->getInfo();
Robert Carrc9bf1d32020-04-13 17:21:08 -07003229 if (canBeObscuredBy(windowHandle, otherHandle) &&
Linnan Li5e5645e2024-03-05 14:43:05 +00003230 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 return true;
3232 }
3233 }
3234 return false;
3235}
3236
chaviw98318de2021-05-19 16:45:23 -05003237bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003238 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003239 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3240 const WindowInfo* windowInfo = windowHandle->getInfo();
3241 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003242 if (windowHandle == otherHandle) {
3243 break; // All future windows are below us. Exit early.
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003244 }
chaviw98318de2021-05-19 16:45:23 -05003245 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003246 if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->overlaps(windowInfo)) {
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003247 return true;
3248 }
3249 }
3250 return false;
3251}
3252
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08003253std::string InputDispatcher::getApplicationWindowLabel(
chaviw98318de2021-05-19 16:45:23 -05003254 const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) {
Yi Kong9b14ac62018-07-17 13:48:38 -07003255 if (applicationHandle != nullptr) {
3256 if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003257 return applicationHandle->getName() + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 } else {
3259 return applicationHandle->getName();
3260 }
Yi Kong9b14ac62018-07-17 13:48:38 -07003261 } else if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003262 return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08003264 return "<unknown application or window>";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 }
3266}
3267
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003268void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00003269 if (!isUserActivityEvent(eventEntry)) {
3270 // Not poking user activity if the event type does not represent a user activity
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003271 return;
3272 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003273
3274 const int32_t eventType = getUserActivityEventType(eventEntry);
3275 if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) {
3276 // Note that we're directly getting the time diff between the current event and the previous
3277 // event. This is assuming that the first user event always happens at a timestamp that is
3278 // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will
3279 // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller
3280 // value than the potential first user activity event time, so this is ok.
3281 std::chrono::nanoseconds timeSinceLastEvent =
3282 std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]);
3283 if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) {
3284 return;
3285 }
3286 }
3287
Linnan Li13bf76a2024-05-05 19:18:02 +08003288 ui::LogicalDisplayId displayId = getTargetDisplayId(eventEntry);
chaviw98318de2021-05-19 16:45:23 -05003289 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Josep del Riob3981622023-04-18 15:49:45 +00003290 const WindowInfo* windowDisablingUserActivityInfo = nullptr;
Tiger Huang721e26f2018-07-24 22:26:19 +08003291 if (focusedWindowHandle != nullptr) {
chaviw98318de2021-05-19 16:45:23 -05003292 const WindowInfo* info = focusedWindowHandle->getInfo();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003293 if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) {
Josep del Riob3981622023-04-18 15:49:45 +00003294 windowDisablingUserActivityInfo = info;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 }
3296 }
3297
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003298 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003299 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003300 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3301 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003302 return;
3303 }
Josep del Riob3981622023-04-18 15:49:45 +00003304 if (windowDisablingUserActivityInfo != nullptr) {
3305 if (DEBUG_DISPATCH_CYCLE) {
3306 ALOGD("Not poking user activity: disabled by window '%s'.",
3307 windowDisablingUserActivityInfo->name.c_str());
3308 }
3309 return;
3310 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003311 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003313 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003314 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3315 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003316 return;
3317 }
Josep del Riob3981622023-04-18 15:49:45 +00003318 // Don't inhibit events that were intercepted or are not passed to
3319 // the apps, like system shortcuts
3320 if (windowDisablingUserActivityInfo != nullptr &&
Josep del Rioc8fdedb2024-05-21 13:32:43 +00003321 keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP) {
Josep del Riob3981622023-04-18 15:49:45 +00003322 if (DEBUG_DISPATCH_CYCLE) {
3323 ALOGD("Not poking user activity: disabled by window '%s'.",
3324 windowDisablingUserActivityInfo->name.c_str());
3325 }
3326 return;
3327 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003328 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00003330 default: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003331 LOG_ALWAYS_FATAL("%s events are not user activity",
Dominik Laskowski75788452021-02-09 18:51:25 -08003332 ftl::enum_string(eventEntry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003333 break;
3334 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 }
3336
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003337 mLastUserActivityTimes[eventType] = eventEntry.eventTime;
Prabir Pradhancef936d2021-07-21 16:17:52 +00003338 auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]()
3339 REQUIRES(mLock) {
3340 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003341 mPolicy.pokeUserActivity(eventTime, eventType, displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003342 };
3343 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344}
3345
3346void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003347 const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003348 std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003349 const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003350 ATRACE_NAME_IF(ATRACE_ENABLED(),
3351 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
3352 connection->getInputChannelName().c_str(), eventEntry->id));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003353 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003354 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, "
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08003355 "globalScaleFactor=%f, pointerIds=%s %s",
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003356 connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(),
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003357 inputTarget.globalScaleFactor, bitsetToString(inputTarget.getPointerIds()).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003358 inputTarget.getPointerInfoString().c_str());
3359 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
3361 // Skip this event if the connection status is not normal.
3362 // We don't want to enqueue additional outbound events if the connection is broken.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003363 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003364 if (DEBUG_DISPATCH_CYCLE) {
3365 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003366 connection->getInputChannelName().c_str(),
3367 ftl::enum_string(connection->status).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003368 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 return;
3370 }
3371
3372 // Split a motion event if needed.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003373 if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) {
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003374 LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003375 "Entry type %s should not have Flags::SPLIT",
Dominik Laskowski75788452021-02-09 18:51:25 -08003376 ftl::enum_string(eventEntry->type).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003378 const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry);
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003379 if (inputTarget.getPointerIds().count() != originalMotionEntry.getPointerCount()) {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -08003380 if (!inputTarget.firstDownTimeInTarget.has_value()) {
3381 logDispatchStateLocked();
3382 LOG(FATAL) << "Splitting motion events requires a down time to be set for the "
3383 "target on connection "
3384 << connection->getInputChannelName() << " for "
3385 << originalMotionEntry.getDescription();
3386 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003387 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003388 splitMotionEvent(originalMotionEntry, inputTarget.getPointerIds(),
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003389 inputTarget.firstDownTimeInTarget.value());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 if (!splitMotionEntry) {
3391 return; // split event was dropped
3392 }
Arthur Hungb3307ee2021-10-14 10:57:37 +00003393 if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
3394 std::string reason = std::string("reason=pointer cancel on split window");
3395 android_log_event_list(LOGTAG_INPUT_CANCEL)
3396 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
3397 }
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003398 if (DEBUG_FOCUS) {
3399 ALOGD("channel '%s' ~ Split motion event.",
3400 connection->getInputChannelName().c_str());
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003401 logOutboundMotionDetails(" ", *splitMotionEntry);
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003402 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003403 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection,
3404 std::move(splitMotionEntry),
3405 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 return;
3407 }
3408 }
3409
3410 // Not splitting. Enqueue dispatch entries for the event as is.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003411 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry,
3412 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413}
3414
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003415void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked(
3416 nsecs_t currentTime, const std::shared_ptr<Connection>& connection,
3417 std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003418 ATRACE_NAME_IF(ATRACE_ENABLED(),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003419 StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, "
3420 "id=0x%" PRIx32 ")",
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003421 connection->getInputChannelName().c_str(), eventEntry->id));
Michael Wright3dd60e22019-03-27 22:06:44 +00003422
hongzuo liu95785e22022-09-06 02:51:35 +00003423 const bool wasEmpty = connection->outboundQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003425 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
3427 // If the outbound queue was previously empty, start the dispatch cycle going.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003428 if (wasEmpty && !connection->outboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 startDispatchCycleLocked(currentTime, connection);
3430 }
3431}
3432
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003433void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003434 std::shared_ptr<const EventEntry> eventEntry,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003435 const InputTarget& inputTarget) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00003436 const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY ||
3437 eventEntry->type == EventEntry::Type::MOTION;
3438 if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) {
3439 LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: "
3440 << inputTarget << " connection: " << connection->getInputChannelName()
3441 << " entry: " << eventEntry->getDescription();
3442 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 // This is a new event.
3444 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003445 std::unique_ptr<DispatchEntry> dispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003446 createDispatchEntry(mIdGenerator, inputTarget, eventEntry, inputTarget.flags,
Prabir Pradhana67623c2024-02-21 06:57:36 +00003447 mWindowInfosVsyncId, mTracer.get());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003449 // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a
3450 // different EventEntry than what was passed in.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003451 eventEntry = dispatchEntry->eventEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 // Apply target flags and update the connection's input state.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003453 switch (eventEntry->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003454 case EventEntry::Type::KEY: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003455 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry);
3456 if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003457 LOG(WARNING) << "channel " << connection->getInputChannelName()
3458 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003459 return; // skip the inconsistent event
3460 }
3461 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003464 case EventEntry::Type::MOTION: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003465 std::shared_ptr<const MotionEntry> resolvedMotion =
3466 std::static_pointer_cast<const MotionEntry>(eventEntry);
3467 {
3468 // Determine the resolved motion entry.
3469 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
3470 int32_t resolvedAction = motionEntry.action;
3471 int32_t resolvedFlags = motionEntry.flags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003473 if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003474 resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003475 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003476 resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003477 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003478 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003479 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003480 resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003481 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003482 resolvedAction = AMOTION_EVENT_ACTION_DOWN;
3483 }
3484 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE &&
3485 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source,
3486 motionEntry.displayId)) {
3487 if (DEBUG_DISPATCH_CYCLE) {
3488 LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str()
3489 << "' ~ enqueueDispatchEntryLocked: filling in missing hover "
3490 "enter event";
3491 }
3492 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
3493 }
3494
3495 if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3496 resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED;
3497 }
3498 if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) {
3499 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
3500 }
3501 if (dispatchEntry->targetFlags.test(
3502 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) {
3503 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
3504 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003505 if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) {
3506 resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
3507 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003508
3509 dispatchEntry->resolvedFlags = resolvedFlags;
3510 if (resolvedAction != motionEntry.action) {
Siarhei Vishniakoue9ef6bc2023-12-21 19:47:20 -08003511 std::optional<std::vector<PointerProperties>> usingProperties;
3512 std::optional<std::vector<PointerCoords>> usingCoords;
3513 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT ||
3514 resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3515 // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by
3516 // the dispatcher, and therefore the coordinates of this event are currently
3517 // incorrect. These events should use the coordinates of the last dispatched
3518 // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data.
3519 const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT;
3520 std::optional<std::pair<std::vector<PointerProperties>,
3521 std::vector<PointerCoords>>>
3522 pointerInfo =
3523 connection->inputState.getPointersOfLastEvent(motionEntry,
3524 hovering);
3525 if (pointerInfo) {
3526 usingProperties = pointerInfo->first;
3527 usingCoords = pointerInfo->second;
3528 }
3529 }
Prabir Pradhana67623c2024-02-21 06:57:36 +00003530 {
3531 // Generate a new MotionEntry with a new eventId using the resolved action
3532 // and flags, and set it as the resolved entry.
3533 auto newEntry = std::make_shared<
3534 MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState,
3535 motionEntry.eventTime, motionEntry.deviceId,
3536 motionEntry.source, motionEntry.displayId,
3537 motionEntry.policyFlags, resolvedAction,
3538 motionEntry.actionButton, resolvedFlags,
3539 motionEntry.metaState, motionEntry.buttonState,
3540 motionEntry.classification, motionEntry.edgeFlags,
3541 motionEntry.xPrecision, motionEntry.yPrecision,
3542 motionEntry.xCursorPosition,
3543 motionEntry.yCursorPosition, motionEntry.downTime,
3544 usingProperties.value_or(
3545 motionEntry.pointerProperties),
3546 usingCoords.value_or(motionEntry.pointerCoords));
3547 if (mTracer) {
3548 ensureEventTraced(motionEntry);
3549 newEntry->traceTracker =
3550 mTracer->traceDerivedEvent(*newEntry,
3551 *motionEntry.traceTracker);
3552 }
3553 resolvedMotion = newEntry;
3554 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003555 if (ATRACE_ENABLED()) {
3556 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32
3557 ") to MotionEvent(id=0x%" PRIx32 ").",
3558 motionEntry.id, resolvedMotion->id);
3559 ATRACE_NAME(message.c_str());
3560 }
3561
3562 // Set the resolved motion entry in the DispatchEntry.
3563 dispatchEntry->eventEntry = resolvedMotion;
3564 eventEntry = resolvedMotion;
3565 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003566 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003568 // Check if we need to cancel any of the ongoing gestures. We don't support multiple
3569 // devices being active at the same time in the same window, so if a new device is
3570 // active, cancel the gesture from the old device.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003571 std::unique_ptr<EventEntry> cancelEvent =
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003572 connection->inputState.cancelConflictingInputStream(*resolvedMotion);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003573 if (cancelEvent != nullptr) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003574 LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in "
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003575 << connection->getInputChannelName() << " with event "
3576 << cancelEvent->getDescription();
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003577 if (mTracer) {
3578 static_cast<MotionEntry&>(*cancelEvent).traceTracker =
3579 mTracer->traceDerivedEvent(*cancelEvent, *resolvedMotion->traceTracker);
3580 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003581 std::unique_ptr<DispatchEntry> cancelDispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003582 createDispatchEntry(mIdGenerator, inputTarget, std::move(cancelEvent),
Prabir Pradhana67623c2024-02-21 06:57:36 +00003583 ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId,
3584 mTracer.get());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003585
3586 // Send these cancel events to the queue before sending the event from the new
3587 // device.
3588 connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry));
3589 }
3590
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003591 if (!connection->inputState.trackMotion(*resolvedMotion,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003592 dispatchEntry->resolvedFlags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003593 LOG(WARNING) << "channel " << connection->getInputChannelName()
3594 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003595 return; // skip the inconsistent event
3596 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003597 if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) &&
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003598 (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003599 // Skip reporting pointer down outside focus to the policy.
3600 break;
3601 }
3602
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003603 dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003604 inputTarget.connection->getToken());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003605
3606 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003608 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003609 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08003610 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
3611 case EventEntry::Type::DRAG: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003612 break;
3613 }
Chris Yef59a2f42020-10-16 12:55:26 -07003614 case EventEntry::Type::SENSOR: {
3615 LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel");
3616 break;
3617 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003618 case EventEntry::Type::CONFIGURATION_CHANGED:
3619 case EventEntry::Type::DEVICE_RESET: {
3620 LOG_ALWAYS_FATAL("%s events should not go to apps",
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003621 ftl::enum_string(eventEntry->type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003622 break;
3623 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 }
3625
3626 // Remember that we are waiting for this dispatch to complete.
3627 if (dispatchEntry->hasForegroundTarget()) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003628 incrementPendingForegroundDispatches(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 }
3630
3631 // Enqueue the dispatch entry.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003632 connection->outboundQueue.emplace_back(std::move(dispatchEntry));
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003633 traceOutboundQueueLength(*connection);
chaviw8c9cf542019-03-25 13:02:48 -07003634}
3635
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003636/**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003637 * This function is for debugging and metrics collection. It has two roles.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003638 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003639 * The first role is to log input interaction with windows, which helps determine what the user was
3640 * interacting with. For example, if user is touching launcher, we will see an input_interaction log
3641 * that user started interacting with launcher window, as well as any other window that received
3642 * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged
3643 * when the set of tokens that received the event changes. It is not logged again as long as the
3644 * user is interacting with the same windows.
3645 *
3646 * The second role is to track input device activity for metrics collection. For each input event,
3647 * we report the set of UIDs that the input device interacted with to the policy. Unlike for the
3648 * input_interaction logs, the device interaction is reported even when the set of interaction
3649 * tokens do not change.
3650 *
3651 * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as
3652 * interaction. This includes up and cancel events for both keys and motions.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003653 */
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003654void InputDispatcher::processInteractionsLocked(const EventEntry& entry,
3655 const std::vector<InputTarget>& targets) {
3656 int32_t deviceId;
3657 nsecs_t eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003658 // Skip ACTION_UP events, and all events other than keys and motions
3659 if (entry.type == EventEntry::Type::KEY) {
3660 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
3661 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
3662 return;
3663 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003664 deviceId = keyEntry.deviceId;
3665 eventTime = keyEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003666 } else if (entry.type == EventEntry::Type::MOTION) {
3667 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
3668 if (motionEntry.action == AMOTION_EVENT_ACTION_UP ||
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003669 motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
3670 MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003671 return;
3672 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003673 deviceId = motionEntry.deviceId;
3674 eventTime = motionEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003675 } else {
3676 return; // Not a key or a motion
3677 }
3678
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003679 std::set<gui::Uid> interactionUids;
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07003680 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003681 std::vector<std::shared_ptr<Connection>> newConnections;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003682 for (const InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003683 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003684 continue; // Skip windows that receive ACTION_OUTSIDE
3685 }
3686
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003687 sp<IBinder> token = target.connection->getToken();
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003688 newConnectionTokens.insert(std::move(token));
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003689 newConnections.emplace_back(target.connection);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003690 if (target.windowHandle) {
3691 interactionUids.emplace(target.windowHandle->getInfo()->ownerUid);
3692 }
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003693 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003694
3695 auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]()
3696 REQUIRES(mLock) {
3697 scoped_unlock unlock(mLock);
3698 mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids);
3699 };
3700 postCommandLocked(std::move(command));
3701
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003702 if (newConnectionTokens == mInteractionConnectionTokens) {
3703 return; // no change
3704 }
3705 mInteractionConnectionTokens = newConnectionTokens;
3706
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003707 std::string targetList;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003708 for (const std::shared_ptr<Connection>& connection : newConnections) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08003709 targetList += connection->getInputChannelName() + ", ";
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003710 }
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003711 std::string message = "Interaction with: " + targetList;
3712 if (targetList.empty()) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003713 message += "<none>";
3714 }
3715 android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS;
3716}
3717
chaviwfd6d3512019-03-25 13:23:49 -07003718void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Vishnu Nairad321cd2020-08-20 16:40:21 -07003719 const sp<IBinder>& token) {
chaviw8c9cf542019-03-25 13:02:48 -07003720 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
chaviwfd6d3512019-03-25 13:23:49 -07003721 uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK;
3722 if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) {
chaviw8c9cf542019-03-25 13:02:48 -07003723 return;
3724 }
3725
Vishnu Nairc519ff72021-01-21 08:23:08 -08003726 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07003727 if (focusedToken == token) {
3728 // ignore since token is focused
chaviw8c9cf542019-03-25 13:02:48 -07003729 return;
3730 }
3731
Prabir Pradhancef936d2021-07-21 16:17:52 +00003732 auto command = [this, token]() REQUIRES(mLock) {
3733 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003734 mPolicy.onPointerDownOutsideFocus(token);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003735 };
3736 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737}
3738
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003739status_t InputDispatcher::publishMotionEvent(Connection& connection,
3740 DispatchEntry& dispatchEntry) const {
3741 const EventEntry& eventEntry = *(dispatchEntry.eventEntry);
3742 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3743
3744 PointerCoords scaledCoords[MAX_POINTERS];
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003745 const PointerCoords* usingCoords = motionEntry.pointerCoords.data();
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003746
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003747 // TODO(b/316355518): Do not modify coords before dispatch.
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003748 // Set the X and Y offset and X and Y scale depending on the input source.
3749 if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) &&
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003750 !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003751 float globalScaleFactor = dispatchEntry.globalScaleFactor;
3752 if (globalScaleFactor != 1.0f) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003753 for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003754 scaledCoords[i] = motionEntry.pointerCoords[i];
3755 // Don't apply window scale here since we don't want scale to affect raw
3756 // coordinates. The scale will be sent back to the client and applied
3757 // later when requesting relative coordinates.
Harry Cutts33476232023-01-30 19:57:29 +00003758 scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003759 }
3760 usingCoords = scaledCoords;
3761 }
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003762 }
3763
3764 std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry);
3765
3766 // Publish the motion event.
3767 return connection.inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003768 .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId,
3769 motionEntry.source, motionEntry.displayId, std::move(hmac),
3770 motionEntry.action, motionEntry.actionButton,
3771 dispatchEntry.resolvedFlags, motionEntry.edgeFlags,
3772 motionEntry.metaState, motionEntry.buttonState,
3773 motionEntry.classification, dispatchEntry.transform,
3774 motionEntry.xPrecision, motionEntry.yPrecision,
3775 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
3776 dispatchEntry.rawTransform, motionEntry.downTime,
3777 motionEntry.eventTime, motionEntry.getPointerCount(),
3778 motionEntry.pointerProperties.data(), usingCoords);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003779}
3780
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003782 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003783 ATRACE_NAME_IF(ATRACE_ENABLED(),
3784 StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
3785 connection->getInputChannelName().c_str()));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003786 if (DEBUG_DISPATCH_CYCLE) {
3787 ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str());
3788 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003790 while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003791 std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 dispatchEntry->deliveryTime = currentTime;
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003793 const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection);
Siarhei Vishniakou70622952020-07-30 11:17:23 -05003794 dispatchEntry->timeoutTime = currentTime + timeout.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795
3796 // Publish the event.
3797 status_t status;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003798 const EventEntry& eventEntry = *(dispatchEntry->eventEntry);
3799 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003800 case EventEntry::Type::KEY: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003801 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3802 std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry);
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003803 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003804 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3805 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003806 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003808 // Publish the key event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003809 status = connection->inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003810 .publishKeyEvent(dispatchEntry->seq, keyEntry.id,
3811 keyEntry.deviceId, keyEntry.source,
3812 keyEntry.displayId, std::move(hmac),
3813 keyEntry.action, dispatchEntry->resolvedFlags,
3814 keyEntry.keyCode, keyEntry.scanCode,
3815 keyEntry.metaState, keyEntry.repeatCount,
3816 keyEntry.downTime, keyEntry.eventTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003817 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003818 ensureEventTraced(keyEntry);
3819 mTracer->traceEventDispatch(*dispatchEntry, *keyEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003820 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003821 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822 }
3823
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003824 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003825 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003826 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3827 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003828 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003829 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003830 status = publishMotionEvent(*connection, *dispatchEntry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003831 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003832 ensureEventTraced(motionEntry);
3833 mTracer->traceEventDispatch(*dispatchEntry, *motionEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003834 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003835 break;
3836 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003837
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003838 case EventEntry::Type::FOCUS: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003839 const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003840 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003841 focusEntry.id,
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07003842 focusEntry.hasFocus);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003843 break;
3844 }
3845
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003846 case EventEntry::Type::TOUCH_MODE_CHANGED: {
3847 const TouchModeEntry& touchModeEntry =
3848 static_cast<const TouchModeEntry&>(eventEntry);
3849 status = connection->inputPublisher
3850 .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id,
3851 touchModeEntry.inTouchMode);
3852
3853 break;
3854 }
3855
Prabir Pradhan99987712020-11-10 18:43:05 -08003856 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
3857 const auto& captureEntry =
3858 static_cast<const PointerCaptureChangedEntry&>(eventEntry);
Hiroki Sato25040232024-02-22 17:21:22 +09003859 status =
3860 connection->inputPublisher
3861 .publishCaptureEvent(dispatchEntry->seq, captureEntry.id,
3862 captureEntry.pointerCaptureRequest.isEnable());
Prabir Pradhan99987712020-11-10 18:43:05 -08003863 break;
3864 }
3865
arthurhungb89ccb02020-12-30 16:19:01 +08003866 case EventEntry::Type::DRAG: {
3867 const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry);
3868 status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq,
3869 dragEntry.id, dragEntry.x,
3870 dragEntry.y,
3871 dragEntry.isExiting);
3872 break;
3873 }
3874
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003875 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07003876 case EventEntry::Type::DEVICE_RESET:
3877 case EventEntry::Type::SENSOR: {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003878 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events",
Dominik Laskowski75788452021-02-09 18:51:25 -08003879 ftl::enum_string(eventEntry.type).c_str());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003880 return;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003881 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 }
3883
3884 // Check the result.
3885 if (status) {
3886 if (status == WOULD_BLOCK) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003887 if (connection->waitQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888 ALOGE("channel '%s' ~ Could not publish event because the pipe is full. "
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003889 "This is unexpected because the wait queue is empty, so the pipe "
3890 "should be empty and we shouldn't have any problems writing an "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003891 "event to it, status=%s(%d)",
3892 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3893 status);
Harry Cutts33476232023-01-30 19:57:29 +00003894 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 } else {
3896 // Pipe is full and we are waiting for the app to finish process some events
3897 // before sending more events to it.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003898 if (DEBUG_DISPATCH_CYCLE) {
3899 ALOGD("channel '%s' ~ Could not publish event because the pipe is full, "
3900 "waiting for the application to catch up",
3901 connection->getInputChannelName().c_str());
3902 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 }
3904 } else {
3905 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003906 "status=%s(%d)",
3907 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3908 status);
Harry Cutts33476232023-01-30 19:57:29 +00003909 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910 }
3911 return;
3912 }
3913
3914 // Re-enqueue the event on the wait queue.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003915 const nsecs_t timeoutTime = dispatchEntry->timeoutTime;
3916 connection->waitQueue.emplace_back(std::move(dispatchEntry));
3917 connection->outboundQueue.erase(connection->outboundQueue.begin());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003918 traceOutboundQueueLength(*connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003919 if (connection->responsive) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003920 mAnrTracker.insert(timeoutTime, connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003921 }
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003922 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 }
3924}
3925
chaviw09c8d2d2020-08-24 15:48:26 -07003926std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const {
3927 size_t size;
3928 switch (event.type) {
3929 case VerifiedInputEvent::Type::KEY: {
3930 size = sizeof(VerifiedKeyEvent);
3931 break;
3932 }
3933 case VerifiedInputEvent::Type::MOTION: {
3934 size = sizeof(VerifiedMotionEvent);
3935 break;
3936 }
3937 }
3938 const uint8_t* start = reinterpret_cast<const uint8_t*>(&event);
3939 return mHmacKeyManager.sign(start, size);
3940}
3941
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003942const std::array<uint8_t, 32> InputDispatcher::getSignature(
3943 const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003944 const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003945 if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) {
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003946 // Only sign events up and down events as the purely move events
3947 // are tied to their up/down counterparts so signing would be redundant.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003948 return INVALID_HMAC;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003949 }
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003950
3951 VerifiedMotionEvent verifiedEvent =
3952 verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform);
3953 verifiedEvent.actionMasked = actionMasked;
3954 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS;
3955 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003956}
3957
3958const std::array<uint8_t, 32> InputDispatcher::getSignature(
3959 const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const {
3960 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry);
3961 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS;
chaviw09c8d2d2020-08-24 15:48:26 -07003962 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003963}
3964
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003966 const std::shared_ptr<Connection>& connection,
3967 uint32_t seq, bool handled, nsecs_t consumeTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003968 if (DEBUG_DISPATCH_CYCLE) {
3969 ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s",
3970 connection->getInputChannelName().c_str(), seq, toString(handled));
3971 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00003973 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 return;
3975 }
3976
3977 // Notify other system components and prepare to start the next dispatch cycle.
Prabir Pradhancef936d2021-07-21 16:17:52 +00003978 auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) {
3979 doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime);
3980 };
3981 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982}
3983
3984void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003985 const std::shared_ptr<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003986 bool notify) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003987 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003988 LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__
3989 << " - notify=" << toString(notify);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003990 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991
3992 // Clear the dispatch queues.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003993 drainDispatchQueue(connection->outboundQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003994 traceOutboundQueueLength(*connection);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003995 drainDispatchQueue(connection->waitQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003996 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 // The connection appears to be unrecoverably broken.
3999 // Ignore already broken or zombie connections.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08004000 if (connection->status == Connection::Status::NORMAL) {
4001 connection->status = Connection::Status::BROKEN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002
4003 if (notify) {
4004 // Notify other system components.
Prabir Pradhancef936d2021-07-21 16:17:52 +00004005 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
4006 connection->getInputChannelName().c_str());
4007
4008 auto command = [this, connection]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004009 scoped_unlock unlock(mLock);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004010 mPolicy.notifyInputChannelBroken(connection->getToken());
Prabir Pradhancef936d2021-07-21 16:17:52 +00004011 };
4012 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 }
4014 }
4015}
4016
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004017void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004018 while (!queue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004019 releaseDispatchEntry(std::move(queue.front()));
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004020 queue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 }
4022}
4023
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004024void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 if (dispatchEntry->hasForegroundTarget()) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004026 decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028}
4029
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004030int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) {
4031 std::scoped_lock _l(mLock);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004032 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004033 if (connection == nullptr) {
4034 ALOGW("Received looper callback for unknown input channel token %p. events=0x%x",
4035 connectionToken.get(), events);
4036 return 0; // remove the callback
4037 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004039 bool notify;
4040 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
4041 if (!(events & ALOOPER_EVENT_INPUT)) {
4042 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
4043 "events=0x%x",
4044 connection->getInputChannelName().c_str(), events);
4045 return 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 }
4047
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004048 nsecs_t currentTime = now();
4049 bool gotOne = false;
4050 status_t status = OK;
4051 for (;;) {
4052 Result<InputPublisher::ConsumerResponse> result =
4053 connection->inputPublisher.receiveConsumerResponse();
4054 if (!result.ok()) {
4055 status = result.error().code();
4056 break;
4057 }
4058
4059 if (std::holds_alternative<InputPublisher::Finished>(*result)) {
4060 const InputPublisher::Finished& finish =
4061 std::get<InputPublisher::Finished>(*result);
4062 finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled,
4063 finish.consumeTime);
4064 } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004065 if (shouldReportMetricsForConnection(*connection)) {
4066 const InputPublisher::Timeline& timeline =
4067 std::get<InputPublisher::Timeline>(*result);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004068 mLatencyTracker.trackGraphicsLatency(timeline.inputEventId,
4069 connection->getToken(),
4070 std::move(timeline.graphicsTimeline));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004071 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004072 }
4073 gotOne = true;
4074 }
4075 if (gotOne) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004076 runCommandsLockedInterruptable();
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004077 if (status == WOULD_BLOCK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 return 1;
4079 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 }
4081
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004082 notify = status != DEAD_OBJECT || !connection->monitor;
4083 if (notify) {
4084 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)",
4085 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
4086 status);
4087 }
4088 } else {
4089 // Monitor channels are never explicitly unregistered.
4090 // We do it automatically when the remote endpoint is closed so don't warn about them.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004091 const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004092 notify = !connection->monitor && stillHaveWindowHandle;
4093 if (notify) {
4094 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x",
4095 connection->getInputChannelName().c_str(), events);
4096 }
4097 }
4098
4099 // Remove the channel.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004100 removeInputChannelLocked(connection->getToken(), notify);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004101 return 0; // remove the callback
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102}
4103
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004104void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 const CancelationOptions& options) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004106 // Cancel windows (i.e. non-monitors).
4107 // A channel must have at least one window to receive any input. If a window was removed, the
4108 // event streams directed to the window will already have been canceled during window removal.
4109 // So there is no need to generate cancellations for connections without any windows.
4110 const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode);
4111 // Generate cancellations for touched windows first. This is to avoid generating cancellations
4112 // through a non-touched window if there are more than one window for an input channel.
4113 if (cancelPointers) {
4114 for (const auto& [displayId, touchState] : mTouchStatesByDisplay) {
4115 if (options.displayId.has_value() && options.displayId != displayId) {
4116 continue;
4117 }
4118 for (const auto& touchedWindow : touchState.windows) {
4119 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
4120 }
4121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 }
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004123 // Follow up by generating cancellations for all windows, because we don't explicitly track
4124 // the windows that have an ongoing focus event stream.
4125 if (cancelNonPointers) {
4126 for (const auto& [_, handles] : mWindowHandlesByDisplay) {
4127 for (const auto& windowHandle : handles) {
4128 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4129 }
4130 }
4131 }
4132
4133 // Cancel monitors.
4134 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135}
4136
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004137void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked(
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004138 const CancelationOptions& options) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004139 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00004140 for (const Monitor& monitor : monitors) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004141 synthesizeCancelationEventsForConnectionLocked(monitor.connection, options,
4142 /*window=*/nullptr);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004143 }
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004144 }
4145}
4146
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004147void InputDispatcher::synthesizeCancelationEventsForWindowLocked(
4148 const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options,
4149 const std::shared_ptr<Connection>& connection) {
4150 if (windowHandle == nullptr) {
4151 LOG(FATAL) << __func__ << ": Window handle must not be null";
4152 }
4153 if (connection) {
4154 // The connection can be optionally provided to avoid multiple lookups.
4155 if (windowHandle->getToken() != connection->getToken()) {
4156 LOG(FATAL) << __func__
4157 << ": Wrong connection provided for window: " << windowHandle->getName();
4158 }
4159 }
4160
4161 std::shared_ptr<Connection> resolvedConnection =
4162 connection ? connection : getConnectionLocked(windowHandle->getToken());
4163 if (!resolvedConnection) {
4164 LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName();
4165 return;
4166 }
4167 synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle);
4168}
4169
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004171 const std::shared_ptr<Connection>& connection, const CancelationOptions& options,
4172 const sp<WindowInfoHandle>& window) {
4173 if (!connection->monitor && window == nullptr) {
4174 LOG(FATAL) << __func__
4175 << ": Cannot send event to non-monitor channel without a window - channel: "
4176 << connection->getInputChannelName();
4177 }
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00004178 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 return;
4180 }
4181
4182 nsecs_t currentTime = now();
4183
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004184 std::vector<std::unique_ptr<EventEntry>> cancelationEvents =
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -07004185 connection->inputState.synthesizeCancelationEvents(currentTime, options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004187 if (cancelationEvents.empty()) {
4188 return;
4189 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004190
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004191 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
4192 ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync "
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004193 "with reality: %s, mode=%s.",
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004194 connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004195 ftl::enum_string(options.mode).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004196 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004197
Arthur Hungb3307ee2021-10-14 10:57:37 +00004198 std::string reason = std::string("reason=").append(options.reason);
4199 android_log_event_list(LOGTAG_INPUT_CANCEL)
4200 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
4201
hongzuo liu95785e22022-09-06 02:51:35 +00004202 const bool wasEmpty = connection->outboundQueue.empty();
Prabir Pradhan16463382023-10-12 23:03:19 +00004203 // The target to use if we don't find a window associated with the channel.
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004204 const InputTarget fallbackTarget{connection};
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004205 const auto& token = connection->getToken();
hongzuo liu95785e22022-09-06 02:51:35 +00004206
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004207 for (size_t i = 0; i < cancelationEvents.size(); i++) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004208 std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004209 std::vector<InputTarget> targets{};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004210
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004211 switch (cancelationEventEntry->type) {
4212 case EventEntry::Type::KEY: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004213 if (mTracer) {
4214 static_cast<KeyEntry&>(*cancelationEventEntry).traceTracker =
4215 mTracer->traceDerivedEvent(*cancelationEventEntry,
4216 *options.traceTracker);
4217 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004218 const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004219 if (window) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004220 addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4221 /*targetFlags=*/{}, keyEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004222 } else {
4223 targets.emplace_back(fallbackTarget);
4224 }
4225 logOutboundKeyDetails("cancel - ", keyEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004226 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004228 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004229 if (mTracer) {
4230 static_cast<MotionEntry&>(*cancelationEventEntry).traceTracker =
4231 mTracer->traceDerivedEvent(*cancelationEventEntry,
4232 *options.traceTracker);
4233 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004234 const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004235 if (window) {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004236 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004237 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004238 pointerIndex++) {
4239 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4240 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004241 if (mDragState && mDragState->dragWindow->getToken() == token &&
4242 pointerIds.test(mDragState->pointerId)) {
4243 LOG(INFO) << __func__
4244 << ": Canceling drag and drop because the pointers for the drag "
4245 "window are being canceled.";
4246 sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0);
4247 mDragState.reset();
4248 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004249 addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4250 ftl::Flags<InputTarget::Flags>(), pointerIds,
4251 motionEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004252 } else {
4253 targets.emplace_back(fallbackTarget);
4254 const auto it = mDisplayInfos.find(motionEntry.displayId);
4255 if (it != mDisplayInfos.end()) {
4256 targets.back().displayTransform = it->second.transform;
4257 targets.back().setDefaultPointerTransform(it->second.transform);
4258 }
4259 }
4260 logOutboundMotionDetails("cancel - ", motionEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004261 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 }
Prabir Pradhan99987712020-11-10 18:43:05 -08004263 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004264 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004265 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
4266 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08004267 LOG_ALWAYS_FATAL("Canceling %s events is not supported",
Dominik Laskowski75788452021-02-09 18:51:25 -08004268 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004269 break;
4270 }
4271 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07004272 case EventEntry::Type::DEVICE_RESET:
4273 case EventEntry::Type::SENSOR: {
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004274 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004275 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004276 break;
4277 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 }
4279
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004280 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004281 if (mTracer) {
4282 mTracer->dispatchToTargetHint(*options.traceTracker, targets[0]);
4283 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004284 enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004286
hongzuo liu95785e22022-09-06 02:51:35 +00004287 // If the outbound queue was previously empty, start the dispatch cycle going.
4288 if (wasEmpty && !connection->outboundQueue.empty()) {
4289 startDispatchCycleLocked(currentTime, connection);
4290 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291}
4292
Svet Ganov5d3bc372020-01-26 23:11:07 -08004293void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004294 const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004295 ftl::Flags<InputTarget::Flags> targetFlags,
4296 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00004297 if (connection->status != Connection::Status::NORMAL) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004298 return;
4299 }
4300
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004301 std::vector<std::unique_ptr<EventEntry>> downEvents =
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004302 connection->inputState.synthesizePointerDownEvents(downTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004303
4304 if (downEvents.empty()) {
4305 return;
4306 }
4307
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004308 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004309 ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.",
4310 connection->getInputChannelName().c_str(), downEvents.size());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004311 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004312
Prabir Pradhanfeca0572024-02-06 00:43:11 +00004313 const auto [_, touchedWindowState, displayId] =
4314 findTouchStateWindowAndDisplayLocked(connection->getToken());
4315 if (touchedWindowState == nullptr) {
4316 LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
4317 }
4318 const auto& windowHandle = touchedWindowState->windowHandle;
Svet Ganov5d3bc372020-01-26 23:11:07 -08004319
hongzuo liu95785e22022-09-06 02:51:35 +00004320 const bool wasEmpty = connection->outboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004321 for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004322 std::vector<InputTarget> targets{};
Svet Ganov5d3bc372020-01-26 23:11:07 -08004323 switch (downEventEntry->type) {
4324 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004325 if (mTracer) {
4326 static_cast<MotionEntry&>(*downEventEntry).traceTracker =
4327 mTracer->traceDerivedEvent(*downEventEntry, *traceTracker);
4328 }
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004329 const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry);
4330 if (windowHandle != nullptr) {
4331 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004332 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004333 pointerIndex++) {
4334 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4335 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004336 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
4337 targetFlags, pointerIds, motionEntry.downTime,
4338 targets);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004339 } else {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004340 targets.emplace_back(connection, targetFlags);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004341 const auto it = mDisplayInfos.find(motionEntry.displayId);
4342 if (it != mDisplayInfos.end()) {
4343 targets.back().displayTransform = it->second.transform;
4344 targets.back().setDefaultPointerTransform(it->second.transform);
4345 }
4346 }
4347 logOutboundMotionDetails("down - ", motionEntry);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004348 break;
4349 }
4350
4351 case EventEntry::Type::KEY:
4352 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004353 case EventEntry::Type::TOUCH_MODE_CHANGED:
Svet Ganov5d3bc372020-01-26 23:11:07 -08004354 case EventEntry::Type::CONFIGURATION_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08004355 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07004356 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004357 case EventEntry::Type::SENSOR:
4358 case EventEntry::Type::DRAG: {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004359 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004360 ftl::enum_string(downEventEntry->type).c_str());
Svet Ganov5d3bc372020-01-26 23:11:07 -08004361 break;
4362 }
4363 }
4364
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004365 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004366 if (mTracer) {
4367 mTracer->dispatchToTargetHint(*traceTracker, targets[0]);
4368 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004369 enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004370 }
4371
hongzuo liu95785e22022-09-06 02:51:35 +00004372 // If the outbound queue was previously empty, start the dispatch cycle going.
4373 if (wasEmpty && !connection->outboundQueue.empty()) {
4374 startDispatchCycleLocked(downTime, connection);
4375 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004376}
4377
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004378std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004379 const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds,
4380 nsecs_t splitDownTime) {
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004381 const auto& [action, pointerProperties, pointerCoords] =
4382 MotionEvent::split(originalMotionEntry.action, originalMotionEntry.flags,
4383 /*historySize=*/0, originalMotionEntry.pointerProperties,
4384 originalMotionEntry.pointerCoords, pointerIds);
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004385 if (pointerIds.count() != pointerCoords.size()) {
4386 // TODO(b/329107108): Determine why some IDs in pointerIds were not in originalMotionEntry.
4387 // This is bad. We are missing some of the pointers that we expected to deliver.
4388 // Most likely this indicates that we received an ACTION_MOVE events that has
4389 // different pointer ids than we expected based on the previous ACTION_DOWN
4390 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
4391 // in this way.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004392 ALOGW("Dropping split motion event because the pointer count is %zu but "
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004393 "we expected there to be %zu pointers. This probably means we received "
4394 "a broken sequence of pointer ids from the input device: %s",
4395 pointerCoords.size(), pointerIds.count(),
4396 originalMotionEntry.getDescription().c_str());
4397 return nullptr;
4398 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004400 // TODO(b/327503168): Move this check inside MotionEvent::split once all callers handle it
4401 // correctly.
Siarhei Vishniakou59e302b2023-06-05 08:04:53 -07004402 if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) {
4403 logDispatchStateLocked();
4404 LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for "
4405 "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64,
4406 originalMotionEntry.getDescription().c_str(), splitDownTime);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004407 }
4408
Garfield Tanff1f1bb2020-01-28 13:24:04 -08004409 int32_t newId = mIdGenerator.nextId();
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00004410 ATRACE_NAME_IF(ATRACE_ENABLED(),
4411 StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32
4412 ").",
4413 originalMotionEntry.id, newId));
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004414 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004415 std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState,
4416 originalMotionEntry.eventTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004417 originalMotionEntry.deviceId, originalMotionEntry.source,
4418 originalMotionEntry.displayId,
4419 originalMotionEntry.policyFlags, action,
4420 originalMotionEntry.actionButton,
4421 originalMotionEntry.flags, originalMotionEntry.metaState,
4422 originalMotionEntry.buttonState,
4423 originalMotionEntry.classification,
4424 originalMotionEntry.edgeFlags,
4425 originalMotionEntry.xPrecision,
4426 originalMotionEntry.yPrecision,
4427 originalMotionEntry.xCursorPosition,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004428 originalMotionEntry.yCursorPosition, splitDownTime,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004429 pointerProperties, pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +00004430 if (mTracer) {
4431 splitMotionEntry->traceTracker =
4432 mTracer->traceDerivedEvent(*splitMotionEntry, *originalMotionEntry.traceTracker);
4433 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 return splitMotionEntry;
4436}
4437
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004438void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
4439 std::scoped_lock _l(mLock);
4440 mLatencyTracker.setInputDevices(args.inputDeviceInfos);
4441}
4442
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004443void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004444 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004445 ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004446 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
Antonio Kantekf16f2832021-09-28 04:39:20 +00004448 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004450 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004452 std::unique_ptr<ConfigurationChangedEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004453 std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004454 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 } // release lock
4456
4457 if (needWake) {
4458 mLooper->wake();
4459 }
4460}
4461
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004462void InputDispatcher::notifyKey(const NotifyKeyArgs& args) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004463 ALOGD_IF(debugInboundEventDetails(),
4464 "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64
Linnan Li13bf76a2024-05-05 19:18:02 +08004465 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, action=%s, flags=0x%x, "
4466 "keyCode=%s, scanCode=0x%x, metaState=0x%x, "
Prabir Pradhan96282b02023-02-24 22:36:17 +00004467 "downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004468 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004469 args.displayId.toString().c_str(), args.policyFlags,
4470 KeyEvent::actionToString(args.action), args.flags, KeyEvent::getLabel(args.keyCode),
4471 args.scanCode, args.metaState, args.downTime);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004472 Result<void> keyCheck = validateKeyEvent(args.action);
4473 if (!keyCheck.ok()) {
4474 LOG(ERROR) << "invalid key event: " << keyCheck.error();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 return;
4476 }
4477
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004478 uint32_t policyFlags = args.policyFlags;
4479 int32_t flags = args.flags;
4480 int32_t metaState = args.metaState;
Siarhei Vishniakou622bd322018-10-29 18:02:27 -07004481 // InputDispatcher tracks and generates key repeats on behalf of
4482 // whatever notifies it, so repeatCount should always be set to 0
4483 constexpr int32_t repeatCount = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
4485 policyFlags |= POLICY_FLAG_VIRTUAL;
4486 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
4487 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 if (policyFlags & POLICY_FLAG_FUNCTION) {
4489 metaState |= AMETA_FUNCTION_ON;
4490 }
4491
4492 policyFlags |= POLICY_FLAG_TRUSTED;
4493
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004494 int32_t keyCode = args.keyCode;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 KeyEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004496 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action,
4497 flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime,
4498 args.eventTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Michael Wright2b3c3302018-03-02 17:19:13 +00004500 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004501 mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004502 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4503 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004504 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004505 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
Antonio Kantekf16f2832021-09-28 04:39:20 +00004507 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 { // acquire lock
4509 mLock.lock();
4510
4511 if (shouldSendKeyToInputFilterLocked(args)) {
4512 mLock.unlock();
4513
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004514 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004515 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 return; // event was consumed by the filter
4517 }
4518
4519 mLock.lock();
4520 }
4521
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004522 std::unique_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004523 std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4524 args.deviceId, args.source, args.displayId, policyFlags,
4525 args.action, flags, keyCode, args.scanCode, metaState,
4526 repeatCount, args.downTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004527 if (mTracer) {
4528 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4529 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004531 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 mLock.unlock();
4533 } // release lock
4534
4535 if (needWake) {
4536 mLooper->wake();
4537 }
4538}
4539
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004540bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 return mInputFilterEnabled;
4542}
4543
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004544void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004545 if (debugInboundEventDetails()) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004546 ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08004547 "displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakou6ebd0692022-10-20 15:05:45 -07004548 "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004549 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, "
4550 "yCursorPosition=%f, downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004551 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004552 args.displayId.toString().c_str(), args.policyFlags,
4553 MotionEvent::actionToString(args.action).c_str(), args.actionButton, args.flags,
4554 args.metaState, args.buttonState, args.edgeFlags, args.xPrecision, args.yPrecision,
4555 args.xCursorPosition, args.yCursorPosition, args.downTime);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004556 for (uint32_t i = 0; i < args.getPointerCount(); i++) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004557 ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, "
4558 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004559 i, args.pointerProperties[i].id,
4560 ftl::enum_string(args.pointerProperties[i].toolType).c_str(),
4561 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
4562 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
4563 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
4564 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
4565 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
4566 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
4567 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
4568 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
4569 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004570 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004572
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004573 Result<void> motionCheck =
4574 validateMotionEvent(args.action, args.actionButton, args.getPointerCount(),
4575 args.pointerProperties.data());
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004576 if (!motionCheck.ok()) {
4577 LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error();
4578 return;
4579 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004581 if (DEBUG_VERIFY_EVENTS) {
4582 auto [it, _] =
4583 mVerifiersByDisplay.try_emplace(args.displayId,
Linnan Li13bf76a2024-05-05 19:18:02 +08004584 StringPrintf("display %s",
4585 args.displayId.toString().c_str()));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004586 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -07004587 it->second.processMovement(args.deviceId, args.source, args.action,
4588 args.getPointerCount(), args.pointerProperties.data(),
4589 args.pointerCoords.data(), args.flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004590 if (!result.ok()) {
4591 LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump();
4592 }
4593 }
4594
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004595 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 policyFlags |= POLICY_FLAG_TRUSTED;
Michael Wright2b3c3302018-03-02 17:19:13 +00004597
4598 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004599 mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime,
4600 policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004601 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4602 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004603 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004604 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
Antonio Kantekf16f2832021-09-28 04:39:20 +00004606 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607 { // acquire lock
4608 mLock.lock();
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004609 if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
4610 // Set the flag anyway if we already have an ongoing gesture. That would allow us to
4611 // complete the processing of the current stroke.
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004612 const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004613 if (touchStateIt != mTouchStatesByDisplay.end()) {
4614 const TouchState& touchState = touchStateIt->second;
Linnan Li907ae732023-09-05 17:14:21 +08004615 if (touchState.hasTouchingPointers(args.deviceId) ||
4616 touchState.hasHoveringPointers(args.deviceId)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004617 policyFlags |= POLICY_FLAG_PASS_TO_USER;
4618 }
4619 }
4620 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
4622 if (shouldSendMotionToInputFilterLocked(args)) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004623 ui::Transform displayTransform;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004624 if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004625 displayTransform = it->second.transform;
4626 }
4627
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 mLock.unlock();
4629
4630 MotionEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004631 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC,
4632 args.action, args.actionButton, args.flags, args.edgeFlags,
4633 args.metaState, args.buttonState, args.classification,
4634 displayTransform, args.xPrecision, args.yPrecision,
4635 args.xCursorPosition, args.yCursorPosition, displayTransform,
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004636 args.downTime, args.eventTime, args.getPointerCount(),
4637 args.pointerProperties.data(), args.pointerCoords.data());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004640 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 return; // event was consumed by the filter
4642 }
4643
4644 mLock.lock();
4645 }
4646
4647 // Just enqueue a new motion event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004648 std::unique_ptr<MotionEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004649 std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4650 args.deviceId, args.source, args.displayId,
4651 policyFlags, args.action, args.actionButton,
4652 args.flags, args.metaState, args.buttonState,
4653 args.classification, args.edgeFlags, args.xPrecision,
4654 args.yPrecision, args.xCursorPosition,
4655 args.yCursorPosition, args.downTime,
4656 args.pointerProperties, args.pointerCoords);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004657 if (mTracer) {
4658 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4659 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004661 if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
4662 IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004663 !mInputFilterEnabled) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004664 const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN;
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004665 std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args);
4666 mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime,
4667 args.deviceId, sources);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004668 }
4669
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004670 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 mLock.unlock();
4672 } // release lock
4673
4674 if (needWake) {
4675 mLooper->wake();
4676 }
4677}
4678
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004679void InputDispatcher::notifySensor(const NotifySensorArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004680 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004681 ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, "
4682 " sensorType=%s",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004683 args.id, args.eventTime, args.deviceId, args.source,
4684 ftl::enum_string(args.sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004685 }
Chris Yef59a2f42020-10-16 12:55:26 -07004686
Antonio Kantekf16f2832021-09-28 04:39:20 +00004687 bool needWake = false;
Chris Yef59a2f42020-10-16 12:55:26 -07004688 { // acquire lock
4689 mLock.lock();
4690
4691 // Just enqueue a new sensor event.
4692 std::unique_ptr<SensorEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004693 std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source,
4694 /* policyFlags=*/0, args.hwTimestamp, args.sensorType,
4695 args.accuracy, args.accuracyChanged, args.values);
Chris Yef59a2f42020-10-16 12:55:26 -07004696
4697 needWake = enqueueInboundEventLocked(std::move(newEntry));
4698 mLock.unlock();
4699 } // release lock
4700
4701 if (needWake) {
4702 mLooper->wake();
4703 }
4704}
4705
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004706void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004707 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004708 ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime,
4709 args.deviceId, args.isOn);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004710 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00004711 mPolicy.notifyVibratorState(args.deviceId, args.isOn);
Chris Yefb552902021-02-03 17:18:37 -08004712}
4713
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004714bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) {
Jackal Guof9696682018-10-05 12:23:23 +08004715 return mInputFilterEnabled;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716}
4717
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004718void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004719 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004720 ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, "
4721 "switchMask=0x%08x",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004722 args.eventTime, args.policyFlags, args.switchValues, args.switchMask);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004723 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004725 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 policyFlags |= POLICY_FLAG_TRUSTED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004727 mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728}
4729
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004730void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004731 // TODO(b/308677868) Remove device reset from the InputListener interface
Prabir Pradhan65613802023-02-22 23:36:58 +00004732 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004733 ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime,
4734 args.deviceId);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004735 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
Antonio Kantekf16f2832021-09-28 04:39:20 +00004737 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004739 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004741 std::unique_ptr<DeviceResetEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004742 std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004743 needWake = enqueueInboundEventLocked(std::move(newEntry));
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -07004744
4745 for (auto& [_, verifier] : mVerifiersByDisplay) {
4746 verifier.resetDevice(args.deviceId);
4747 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 } // release lock
4749
4750 if (needWake) {
4751 mLooper->wake();
4752 }
4753}
4754
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004755void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004756 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004757 ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime,
Hiroki Sato25040232024-02-22 17:21:22 +09004758 args.request.isEnable() ? "true" : "false");
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004759 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004760
Antonio Kantekf16f2832021-09-28 04:39:20 +00004761 bool needWake = false;
Prabir Pradhan99987712020-11-10 18:43:05 -08004762 { // acquire lock
4763 std::scoped_lock _l(mLock);
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004764 auto entry =
4765 std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004766 needWake = enqueueInboundEventLocked(std::move(entry));
4767 } // release lock
4768
4769 if (needWake) {
4770 mLooper->wake();
4771 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004772}
4773
Prabir Pradhan5735a322022-04-11 17:23:34 +00004774InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00004775 std::optional<gui::Uid> targetUid,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004776 InputEventInjectionSync syncMode,
4777 std::chrono::milliseconds timeout,
4778 uint32_t policyFlags) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004779 Result<void> eventValidation = validateInputEvent(*event);
4780 if (!eventValidation.ok()) {
4781 LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error();
4782 return InputEventInjectionResult::FAILED;
4783 }
4784
Prabir Pradhan65613802023-02-22 23:36:58 +00004785 if (debugInboundEventDetails()) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004786 LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString)
4787 << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count()
4788 << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec
4789 << ", event=" << *event;
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004790 }
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -07004791 nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792
Prabir Pradhan5735a322022-04-11 17:23:34 +00004793 policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004795 // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004796 // that have gone through the InputFilter. If the event passed through the InputFilter, assign
4797 // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
4798 // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
4799 // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
4800 // from events that originate from actual hardware.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -07004801 DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004802 if (policyFlags & POLICY_FLAG_FILTERED) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004803 resolvedDeviceId = event->getDeviceId();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004804 }
4805
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004806 const bool isAsync = syncMode == InputEventInjectionSync::NONE;
4807 auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync);
4808
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004809 std::queue<std::unique_ptr<EventEntry>> injectedEntries;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004811 case InputEventType::KEY: {
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004812 const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004813 const int32_t action = incomingKey.getAction();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004814 int32_t flags = incomingKey.getFlags();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004815 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4816 flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4817 }
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004818 int32_t keyCode = incomingKey.getKeyCode();
4819 int32_t metaState = incomingKey.getMetaState();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004820 KeyEvent keyEvent;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004821 keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(),
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004822 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode,
4823 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
4824 incomingKey.getDownTime(), incomingKey.getEventTime());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004826 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
4827 policyFlags |= POLICY_FLAG_VIRTUAL;
Michael Wright2b3c3302018-03-02 17:19:13 +00004828 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004829
4830 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
4831 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004832 mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004833 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4834 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
4835 std::to_string(t.duration().count()).c_str());
4836 }
4837 }
4838
4839 mLock.lock();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004840 std::unique_ptr<KeyEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004841 std::make_unique<KeyEntry>(incomingKey.getId(), injectionState,
4842 incomingKey.getEventTime(), resolvedDeviceId,
4843 incomingKey.getSource(), incomingKey.getDisplayId(),
4844 policyFlags, action, flags, keyCode,
4845 incomingKey.getScanCode(), metaState,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004846 incomingKey.getRepeatCount(),
4847 incomingKey.getDownTime());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004848 if (mTracer) {
4849 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4850 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004851 injectedEntries.push(std::move(injectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004852 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 }
4854
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004855 case InputEventType::MOTION: {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004856 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004857 const bool isPointerEvent =
4858 isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER);
4859 // If a pointer event has no displayId specified, inject it to the default display.
Linnan Li13bf76a2024-05-05 19:18:02 +08004860 const ui::LogicalDisplayId displayId =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004861 isPointerEvent && (event->getDisplayId() == ui::LogicalDisplayId::INVALID)
4862 ? ui::LogicalDisplayId::DEFAULT
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004863 : event->getDisplayId();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004864 int32_t flags = motionEvent.getFlags();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004865
4866 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004867 nsecs_t eventTime = motionEvent.getEventTime();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004868 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004869 mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(),
4870 motionEvent.getAction(), eventTime,
4871 /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004872 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4873 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
4874 std::to_string(t.duration().count()).c_str());
4875 }
4876 }
4877
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004878 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4879 flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4880 }
4881
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004882 mLock.lock();
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004883
4884 if (policyFlags & POLICY_FLAG_FILTERED) {
4885 // The events from InputFilter impersonate real hardware devices. Check these
4886 // events for consistency and print an error. An inconsistent event sent from
4887 // InputFilter could cause a crash in the later stages of dispatching pipeline.
4888 auto [it, _] =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004889 mInputFilterVerifiersByDisplay.try_emplace(displayId,
4890 std::string("Injection on ") +
4891 displayId.toString());
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004892 InputVerifier& verifier = it->second;
4893
4894 Result<void> result =
4895 verifier.processMovement(resolvedDeviceId, motionEvent.getSource(),
4896 motionEvent.getAction(),
4897 motionEvent.getPointerCount(),
4898 motionEvent.getPointerProperties(),
4899 motionEvent.getSamplePointerCoords(), flags);
4900 if (!result.ok()) {
4901 logDispatchStateLocked();
4902 LOG(ERROR) << "Inconsistent event: " << motionEvent
4903 << ", reason: " << result.error();
4904 }
4905 }
4906
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004907 const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004908 const size_t pointerCount = motionEvent.getPointerCount();
4909 const std::vector<PointerProperties>
4910 pointerProperties(motionEvent.getPointerProperties(),
4911 motionEvent.getPointerProperties() + pointerCount);
4912
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004913 const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004914 std::unique_ptr<MotionEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004915 std::make_unique<MotionEntry>(motionEvent.getId(), injectionState,
4916 *sampleEventTimes, resolvedDeviceId,
4917 motionEvent.getSource(), displayId, policyFlags,
4918 motionEvent.getAction(),
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004919 motionEvent.getActionButton(), flags,
4920 motionEvent.getMetaState(),
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004921 motionEvent.getButtonState(),
4922 motionEvent.getClassification(),
4923 motionEvent.getEdgeFlags(),
4924 motionEvent.getXPrecision(),
4925 motionEvent.getYPrecision(),
4926 motionEvent.getRawXCursorPosition(),
4927 motionEvent.getRawYCursorPosition(),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004928 motionEvent.getDownTime(), pointerProperties,
4929 std::vector<PointerCoords>(samplePointerCoords,
4930 samplePointerCoords +
4931 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004932 transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004933 if (mTracer) {
4934 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4935 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004936 injectedEntries.push(std::move(injectedEntry));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004937 for (size_t i = motionEvent.getHistorySize(); i > 0; i--) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004938 sampleEventTimes += 1;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004939 samplePointerCoords += motionEvent.getPointerCount();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004940 std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique<
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004941 MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes,
4942 resolvedDeviceId, motionEvent.getSource(), displayId,
4943 policyFlags, motionEvent.getAction(),
4944 motionEvent.getActionButton(), flags,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004945 motionEvent.getMetaState(), motionEvent.getButtonState(),
4946 motionEvent.getClassification(), motionEvent.getEdgeFlags(),
4947 motionEvent.getXPrecision(), motionEvent.getYPrecision(),
4948 motionEvent.getRawXCursorPosition(),
4949 motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(),
4950 pointerProperties,
4951 std::vector<PointerCoords>(samplePointerCoords,
4952 samplePointerCoords +
4953 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004954 transformMotionEntryForInjectionLocked(*nextInjectedEntry,
4955 motionEvent.getTransform());
Prabir Pradhan2b2d1bf2024-03-05 16:27:56 +00004956 if (mTracer) {
4957 nextInjectedEntry->traceTracker =
4958 mTracer->traceInboundEvent(*nextInjectedEntry);
4959 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004960 injectedEntries.push(std::move(nextInjectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004961 }
4962 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004965 default:
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004966 LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events";
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004967 return InputEventInjectionResult::FAILED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 }
4969
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970 bool needWake = false;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004971 while (!injectedEntries.empty()) {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004972 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004973 LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004974 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004975 needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front()));
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004976 injectedEntries.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 }
4978
4979 mLock.unlock();
4980
4981 if (needWake) {
4982 mLooper->wake();
4983 }
4984
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004985 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004987 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004989 if (syncMode == InputEventInjectionSync::NONE) {
4990 injectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 } else {
4992 for (;;) {
4993 injectionResult = injectionState->injectionResult;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004994 if (injectionResult != InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 break;
4996 }
4997
4998 nsecs_t remainingTimeout = endTime - now();
4999 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005000 if (DEBUG_INJECTION) {
5001 ALOGD("injectInputEvent - Timed out waiting for injection result "
5002 "to become available.");
5003 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005004 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005 break;
5006 }
5007
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005008 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 }
5010
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005011 if (injectionResult == InputEventInjectionResult::SUCCEEDED &&
5012 syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 while (injectionState->pendingForegroundDispatches != 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005014 if (DEBUG_INJECTION) {
5015 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
5016 injectionState->pendingForegroundDispatches);
5017 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018 nsecs_t remainingTimeout = endTime - now();
5019 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005020 if (DEBUG_INJECTION) {
5021 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
5022 "dispatches to finish.");
5023 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005024 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 break;
5026 }
5027
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005028 mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 }
5030 }
5031 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 } // release lock
5033
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005034 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005035 LOG(INFO) << "injectInputEvent - Finished with result "
5036 << ftl::enum_string(injectionResult);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005037 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
5039 return injectionResult;
5040}
5041
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005042std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) {
Gang Wange9087892020-01-07 12:17:14 -05005043 std::array<uint8_t, 32> calculatedHmac;
5044 std::unique_ptr<VerifiedInputEvent> result;
5045 switch (event.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005046 case InputEventType::KEY: {
Gang Wange9087892020-01-07 12:17:14 -05005047 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
5048 VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent);
5049 result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005050 calculatedHmac = sign(verifiedKeyEvent);
Gang Wange9087892020-01-07 12:17:14 -05005051 break;
5052 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005053 case InputEventType::MOTION: {
Gang Wange9087892020-01-07 12:17:14 -05005054 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
5055 VerifiedMotionEvent verifiedMotionEvent =
5056 verifiedMotionEventFromMotionEvent(motionEvent);
5057 result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005058 calculatedHmac = sign(verifiedMotionEvent);
Gang Wange9087892020-01-07 12:17:14 -05005059 break;
5060 }
5061 default: {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08005062 LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType());
Gang Wange9087892020-01-07 12:17:14 -05005063 return nullptr;
5064 }
5065 }
5066 if (calculatedHmac == INVALID_HMAC) {
5067 return nullptr;
5068 }
tyiu1573a672023-02-21 22:38:32 +00005069 if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) {
Gang Wange9087892020-01-07 12:17:14 -05005070 return nullptr;
5071 }
5072 return result;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005073}
5074
Prabir Pradhan24047542023-11-02 17:14:59 +00005075void InputDispatcher::setInjectionResult(const EventEntry& entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005076 InputEventInjectionResult injectionResult) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005077 if (!entry.injectionState) {
5078 // Not an injected event.
5079 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 }
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005081
5082 InjectionState& injectionState = *entry.injectionState;
5083 if (DEBUG_INJECTION) {
5084 LOG(INFO) << "Setting input event injection result to "
5085 << ftl::enum_string(injectionResult);
5086 }
5087
5088 if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) {
5089 // Log the outcome since the injector did not wait for the injection result.
5090 switch (injectionResult) {
5091 case InputEventInjectionResult::SUCCEEDED:
5092 ALOGV("Asynchronous input event injection succeeded.");
5093 break;
5094 case InputEventInjectionResult::TARGET_MISMATCH:
5095 ALOGV("Asynchronous input event injection target mismatch.");
5096 break;
5097 case InputEventInjectionResult::FAILED:
5098 ALOGW("Asynchronous input event injection failed.");
5099 break;
5100 case InputEventInjectionResult::TIMED_OUT:
5101 ALOGW("Asynchronous input event injection timed out.");
5102 break;
5103 case InputEventInjectionResult::PENDING:
5104 ALOGE("Setting result to 'PENDING' for asynchronous injection");
5105 break;
5106 }
5107 }
5108
5109 injectionState.injectionResult = injectionResult;
5110 mInjectionResultAvailable.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111}
5112
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005113void InputDispatcher::transformMotionEntryForInjectionLocked(
5114 MotionEntry& entry, const ui::Transform& injectedTransform) const {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005115 // Input injection works in the logical display coordinate space, but the input pipeline works
5116 // display space, so we need to transform the injected events accordingly.
5117 const auto it = mDisplayInfos.find(entry.displayId);
5118 if (it == mDisplayInfos.end()) return;
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005119 const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005120
Prabir Pradhand9a2ebe2022-07-20 19:25:13 +00005121 if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
5122 entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
5123 const vec2 cursor =
5124 MotionEvent::calculateTransformedXY(entry.source, transformToDisplay,
5125 {entry.xCursorPosition, entry.yCursorPosition});
5126 entry.xCursorPosition = cursor.x;
5127 entry.yCursorPosition = cursor.y;
5128 }
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07005129 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08005130 entry.pointerCoords[i] =
5131 MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay,
5132 entry.pointerCoords[i]);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005133 }
5134}
5135
Prabir Pradhan24047542023-11-02 17:14:59 +00005136void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005137 if (entry.injectionState) {
5138 entry.injectionState->pendingForegroundDispatches += 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 }
5140}
5141
Prabir Pradhan24047542023-11-02 17:14:59 +00005142void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005143 if (entry.injectionState) {
5144 entry.injectionState->pendingForegroundDispatches -= 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005146 if (entry.injectionState->pendingForegroundDispatches == 0) {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005147 mInjectionSyncFinished.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 }
5149 }
5150}
5151
chaviw98318de2021-05-19 16:45:23 -05005152const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005153 ui::LogicalDisplayId displayId) const {
chaviw98318de2021-05-19 16:45:23 -05005154 static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES;
Vishnu Nairad321cd2020-08-20 16:40:21 -07005155 auto it = mWindowHandlesByDisplay.find(displayId);
5156 return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES;
Arthur Hungb92218b2018-08-14 12:00:21 +08005157}
5158
chaviw98318de2021-05-19 16:45:23 -05005159sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005160 const sp<IBinder>& windowHandleToken, std::optional<ui::LogicalDisplayId> displayId) const {
arthurhungbe737672020-06-24 12:29:21 +08005161 if (windowHandleToken == nullptr) {
5162 return nullptr;
5163 }
5164
Prabir Pradhan16463382023-10-12 23:03:19 +00005165 if (!displayId) {
5166 // Look through all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +08005167 for (const auto& [_, windowHandles] : mWindowHandlesByDisplay) {
Prabir Pradhan16463382023-10-12 23:03:19 +00005168 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
5169 if (windowHandle->getToken() == windowHandleToken) {
5170 return windowHandle;
5171 }
Arthur Hungb92218b2018-08-14 12:00:21 +08005172 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 }
Vishnu Nairad321cd2020-08-20 16:40:21 -07005174 return nullptr;
5175 }
5176
Prabir Pradhan16463382023-10-12 23:03:19 +00005177 // Only look through the requested display.
5178 for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005179 if (windowHandle->getToken() == windowHandleToken) {
5180 return windowHandle;
5181 }
5182 }
5183 return nullptr;
5184}
5185
chaviw98318de2021-05-19 16:45:23 -05005186sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
5187 const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08005188 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
chaviw98318de2021-05-19 16:45:23 -05005189 for (const sp<WindowInfoHandle>& handle : windowHandles) {
arthurhungbe737672020-06-24 12:29:21 +08005190 if (handle->getId() == windowHandle->getId() &&
5191 handle->getToken() == windowHandle->getToken()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005192 if (windowHandle->getInfo()->displayId != displayId) {
5193 ALOGE("Found window %s in display %s"
5194 ", but it should belong to display %s",
5195 windowHandle->getName().c_str(), displayId.toString().c_str(),
5196 windowHandle->getInfo()->displayId.toString().c_str());
Mady Mellor017bcd12020-06-23 19:12:00 +00005197 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005198 return handle;
Arthur Hungb92218b2018-08-14 12:00:21 +08005199 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 }
5201 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005202 return nullptr;
5203}
5204
Linnan Li13bf76a2024-05-05 19:18:02 +08005205sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(
5206 ui::LogicalDisplayId displayId) const {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005207 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId);
5208 return getWindowHandleLocked(focusedToken, displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
Linnan Li13bf76a2024-05-05 19:18:02 +08005211ui::Transform InputDispatcher::getTransformLocked(ui::LogicalDisplayId displayId) const {
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005212 auto displayInfoIt = mDisplayInfos.find(displayId);
5213 return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform
5214 : kIdentityTransform;
5215}
5216
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005217bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window,
5218 const MotionEntry& motionEntry) const {
5219 const WindowInfo& info = *window->getInfo();
5220
5221 // Skip spy window targets that are not valid for targeted injection.
5222 if (const auto err = verifyTargetedInjection(window, motionEntry); err) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005223 return false;
5224 }
5225
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005226 if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
5227 ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str());
5228 return false;
5229 }
5230
5231 if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
5232 ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL",
5233 window->getName().c_str());
5234 return false;
5235 }
5236
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005237 std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005238 if (connection == nullptr) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005239 ALOGW("Not sending touch to %s because there's no corresponding connection",
5240 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005241 return false;
5242 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005243
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005244 if (!connection->responsive) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005245 ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005246 return false;
5247 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005248
5249 // Drop events that can't be trusted due to occlusion
5250 const auto [x, y] = resolveTouchedPosition(motionEntry);
5251 TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5252 if (!isTouchTrustedLocked(occlusionInfo)) {
5253 if (DEBUG_TOUCH_OCCLUSION) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00005254 ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005255 for (const auto& log : occlusionInfo.debugInfo) {
5256 ALOGD("%s", log.c_str());
5257 }
5258 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005259 ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(),
5260 occlusionInfo.obscuringUid.toString().c_str());
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005261 return false;
5262 }
5263
5264 // Drop touch events if requested by input feature
5265 if (shouldDropInput(motionEntry, window)) {
5266 return false;
5267 }
5268
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07005269 // Ignore touches if stylus is down anywhere on screen
5270 if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) &&
5271 isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) {
5272 LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active";
5273 return false;
5274 }
5275
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005276 return true;
5277}
5278
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005279void InputDispatcher::updateWindowHandlesForDisplayLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005280 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5281 ui::LogicalDisplayId displayId) {
chaviw98318de2021-05-19 16:45:23 -05005282 if (windowInfoHandles.empty()) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005283 // Remove all handles on a display if there are no windows left.
5284 mWindowHandlesByDisplay.erase(displayId);
5285 return;
5286 }
5287
5288 // Since we compare the pointer of input window handles across window updates, we need
5289 // to make sure the handle object for the same window stays unchanged across updates.
chaviw98318de2021-05-19 16:45:23 -05005290 const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId);
5291 std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById;
5292 for (const sp<WindowInfoHandle>& handle : oldHandles) {
chaviwaf87b3e2019-10-01 16:59:28 -07005293 oldHandlesById[handle->getId()] = handle;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005294 }
5295
chaviw98318de2021-05-19 16:45:23 -05005296 std::vector<sp<WindowInfoHandle>> newHandles;
5297 for (const sp<WindowInfoHandle>& handle : windowInfoHandles) {
chaviw98318de2021-05-19 16:45:23 -05005298 const WindowInfo* info = handle->getInfo();
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005299 if (getConnectionLocked(handle->getToken()) == nullptr) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005300 const bool noInputChannel =
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005301 info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005302 const bool canReceiveInput =
5303 !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) ||
5304 !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005305 if (canReceiveInput && !noInputChannel) {
John Recke0710582019-09-26 13:46:12 -07005306 ALOGV("Window handle %s has no registered input channel",
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005307 handle->getName().c_str());
Robert Carr2984b7a2020-04-13 17:06:45 -07005308 continue;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005309 }
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005310 }
5311
5312 if (info->displayId != displayId) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005313 ALOGE("Window %s updated by wrong display %s, should belong to display %s",
5314 handle->getName().c_str(), displayId.toString().c_str(),
5315 info->displayId.toString().c_str());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005316 continue;
5317 }
5318
Robert Carredd13602020-04-13 17:24:34 -07005319 if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) &&
Linnan Li13bf76a2024-05-05 19:18:02 +08005320 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) {
chaviw98318de2021-05-19 16:45:23 -05005321 const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005322 oldHandle->updateFrom(handle);
5323 newHandles.push_back(oldHandle);
5324 } else {
5325 newHandles.push_back(handle);
5326 }
5327 }
5328
5329 // Insert or replace
5330 mWindowHandlesByDisplay[displayId] = newHandles;
5331}
5332
Arthur Hungb92218b2018-08-14 12:00:21 +08005333/**
5334 * Called from InputManagerService, update window handle list by displayId that can receive input.
5335 * A window handle contains information about InputChannel, Touch Region, Types, Focused,...
5336 * If set an empty list, remove all handles from the specific display.
5337 * For focused handle, check if need to change and send a cancel event to previous one.
5338 * For removed handle, check if need to send a cancel event if already in touch.
5339 */
Arthur Hung72d8dc32020-03-28 00:48:39 +00005340void InputDispatcher::setInputWindowsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005341 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5342 ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005343 if (DEBUG_FOCUS) {
5344 std::string windowList;
chaviw98318de2021-05-19 16:45:23 -05005345 for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005346 windowList += iwh->getName() + " ";
5347 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005348 LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList;
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005349 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005350 ScopedSyntheticEventTracer traceContext(mTracer);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351
Prabir Pradhand65552b2021-10-07 11:23:50 -07005352 // Check preconditions for new input windows
chaviw98318de2021-05-19 16:45:23 -05005353 for (const sp<WindowInfoHandle>& window : windowInfoHandles) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07005354 const WindowInfo& info = *window->getInfo();
5355
5356 // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005357 const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005358 if (noInputWindow && window->getToken() != nullptr) {
5359 ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing",
5360 window->getName().c_str());
5361 window->releaseChannel();
5362 }
Prabir Pradhand65552b2021-10-07 11:23:50 -07005363
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005364 // Ensure all spy windows are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005365 LOG_ALWAYS_FATAL_IF(info.isSpy() &&
5366 !info.inputConfig.test(
5367 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005368 "%s has feature SPY, but is not a trusted overlay.",
5369 window->getName().c_str());
5370
Prabir Pradhand65552b2021-10-07 11:23:50 -07005371 // Ensure all stylus interceptors are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005372 LOG_ALWAYS_FATAL_IF(info.interceptsStylus() &&
5373 !info.inputConfig.test(
5374 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhand65552b2021-10-07 11:23:50 -07005375 "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.",
5376 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005377 }
5378
Arthur Hung72d8dc32020-03-28 00:48:39 +00005379 // Copy old handles for release if they are no longer present.
chaviw98318de2021-05-19 16:45:23 -05005380 const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005381 const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
chaviw98318de2021-05-19 16:45:23 -05005383 updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005384
chaviw98318de2021-05-19 16:45:23 -05005385 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005386
Vishnu Nairc519ff72021-01-21 08:23:08 -08005387 std::optional<FocusResolver::FocusChanges> changes =
5388 mFocusResolver.setInputWindows(displayId, windowHandles);
5389 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005390 onFocusChangedLocked(*changes, traceContext.getTracker(), removedFocusedWindowHandle);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005391 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392
Linnan Li72352222024-04-12 18:55:57 +08005393 if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5394 TouchState& state = it->second;
Arthur Hung72d8dc32020-03-28 00:48:39 +00005395 for (size_t i = 0; i < state.windows.size();) {
5396 TouchedWindow& touchedWindow = state.windows[i];
Linnan Li72352222024-04-12 18:55:57 +08005397 if (getWindowHandleLocked(touchedWindow.windowHandle) != nullptr) {
5398 i++;
5399 continue;
5400 }
5401 LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName()
5402 << " in display %" << displayId;
5403 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
5404 "touched window was removed", traceContext.getTracker());
5405 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
5406 // Since we are about to drop the touch, cancel the events for the wallpaper as
5407 // well.
5408 if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) &&
5409 touchedWindow.windowHandle->getInfo()->inputConfig.test(
5410 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
5411 for (const DeviceId deviceId : touchedWindow.getTouchingDeviceIds()) {
5412 if (const auto& ww = state.getWallpaperWindow(deviceId); ww != nullptr) {
5413 options.deviceId = deviceId;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005414 synthesizeCancelationEventsForWindowLocked(ww, options);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005415 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 }
Linnan Li72352222024-04-12 18:55:57 +08005418 state.windows.erase(state.windows.begin() + i);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 }
arthurhungb89ccb02020-12-30 16:19:01 +08005420
arthurhung6d4bed92021-03-17 11:59:33 +08005421 // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We
arthurhungb89ccb02020-12-30 16:19:01 +08005422 // could just clear the state here.
Arthur Hung3915c1f2022-05-31 07:17:17 +00005423 if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId &&
arthurhung6d4bed92021-03-17 11:59:33 +08005424 std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) ==
arthurhungb89ccb02020-12-30 16:19:01 +08005425 windowHandles.end()) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00005426 ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str());
5427 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08005428 mDragState.reset();
arthurhungb89ccb02020-12-30 16:19:01 +08005429 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005430 }
Arthur Hung25e2af12020-03-26 12:58:37 +00005431
Arthur Hung72d8dc32020-03-28 00:48:39 +00005432 // Release information for windows that are no longer present.
5433 // This ensures that unused input channels are released promptly.
5434 // Otherwise, they might stick around until the window handle is destroyed
5435 // which might not happen until the next GC.
chaviw98318de2021-05-19 16:45:23 -05005436 for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005437 if (getWindowHandleLocked(oldWindowHandle) == nullptr) {
Arthur Hung72d8dc32020-03-28 00:48:39 +00005438 if (DEBUG_FOCUS) {
5439 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str());
Arthur Hung25e2af12020-03-26 12:58:37 +00005440 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005441 oldWindowHandle->releaseChannel();
Arthur Hung25e2af12020-03-26 12:58:37 +00005442 }
chaviw291d88a2019-02-14 10:33:58 -08005443 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444}
5445
5446void InputDispatcher::setFocusedApplication(
Linnan Li13bf76a2024-05-05 19:18:02 +08005447 ui::LogicalDisplayId displayId,
5448 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005449 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005450 ALOGD("setFocusedApplication displayId=%s %s", displayId.toString().c_str(),
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005451 inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>");
5452 }
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005453 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005454 std::scoped_lock _l(mLock);
Vishnu Nair599f1412021-06-21 10:39:58 -07005455 setFocusedApplicationLocked(displayId, inputApplicationHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 } // release lock
5457
5458 // Wake up poll loop since it may need to make new input dispatching choices.
5459 mLooper->wake();
5460}
5461
Vishnu Nair599f1412021-06-21 10:39:58 -07005462void InputDispatcher::setFocusedApplicationLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005463 ui::LogicalDisplayId displayId,
5464 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Vishnu Nair599f1412021-06-21 10:39:58 -07005465 std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle =
5466 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
5467
5468 if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) {
5469 return; // This application is already focused. No need to wake up or change anything.
5470 }
5471
5472 // Set the new application handle.
5473 if (inputApplicationHandle != nullptr) {
5474 mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
5475 } else {
5476 mFocusedApplicationHandlesByDisplay.erase(displayId);
5477 }
5478
5479 // No matter what the old focused application was, stop waiting on it because it is
5480 // no longer focused.
5481 resetNoFocusedWindowTimeoutLocked();
5482}
5483
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08005484void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) {
5485 if (interval.count() < 0) {
5486 LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0");
5487 }
5488 std::scoped_lock _l(mLock);
5489 mMinTimeBetweenUserActivityPokes = interval;
5490}
5491
Tiger Huang721e26f2018-07-24 22:26:19 +08005492/**
5493 * Sets the focused display, which is responsible for receiving focus-dispatched input events where
5494 * the display not specified.
5495 *
5496 * We track any unreleased events for each window. If a window loses the ability to receive the
5497 * released event, we will send a cancel event to it. So when the focused display is changed, we
5498 * cancel all the unreleased display-unspecified events for the focused window on the old focused
5499 * display. The display-specified events won't be affected.
5500 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005501void InputDispatcher::setFocusedDisplay(ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005502 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005503 ALOGD("setFocusedDisplay displayId=%s", displayId.toString().c_str());
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005504 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005505 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005506 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005507 ScopedSyntheticEventTracer traceContext(mTracer);
Tiger Huang721e26f2018-07-24 22:26:19 +08005508
5509 if (mFocusedDisplayId != displayId) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005510 sp<IBinder> oldFocusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08005511 mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07005512 if (oldFocusedWindowToken != nullptr) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005513 const auto windowHandle =
5514 getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId);
5515 if (windowHandle == nullptr) {
5516 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Tiger Huang721e26f2018-07-24 22:26:19 +08005517 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005518 CancelationOptions
5519 options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005520 "The display which contains this window no longer has focus.",
5521 traceContext.getTracker());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005522 options.displayId = ui::LogicalDisplayId::INVALID;
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005523 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
Tiger Huang721e26f2018-07-24 22:26:19 +08005524 }
5525 mFocusedDisplayId = displayId;
5526
Chris Ye3c2d6f52020-08-09 10:39:48 -07005527 // Find new focused window and validate
Vishnu Nairc519ff72021-01-21 08:23:08 -08005528 sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00005529 sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken);
Robert Carrf759f162018-11-13 12:57:11 -08005530
Vishnu Nairad321cd2020-08-20 16:40:21 -07005531 if (newFocusedWindowToken == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005532 ALOGW("Focused display #%s does not have a focused window.",
5533 displayId.toString().c_str());
Vishnu Nairc519ff72021-01-21 08:23:08 -08005534 if (mFocusResolver.hasFocusedWindowTokens()) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005535 ALOGE("But another display has a focused window\n%s",
Vishnu Nairc519ff72021-01-21 08:23:08 -08005536 mFocusResolver.dumpFocusedWindows().c_str());
Tiger Huang721e26f2018-07-24 22:26:19 +08005537 }
5538 }
5539 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005540 } // release lock
5541
5542 // Wake up poll loop since it may need to make new input dispatching choices.
5543 mLooper->wake();
5544}
5545
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005547 if (DEBUG_FOCUS) {
5548 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
5549 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550
5551 bool changed;
5552 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005553 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
5555 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
5556 if (mDispatchFrozen && !frozen) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005557 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 }
5559
5560 if (mDispatchEnabled && !enabled) {
5561 resetAndDropEverythingLocked("dispatcher is being disabled");
5562 }
5563
5564 mDispatchEnabled = enabled;
5565 mDispatchFrozen = frozen;
5566 changed = true;
5567 } else {
5568 changed = false;
5569 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 } // release lock
5571
5572 if (changed) {
5573 // Wake up poll loop since it may need to make new input dispatching choices.
5574 mLooper->wake();
5575 }
5576}
5577
5578void InputDispatcher::setInputFilterEnabled(bool enabled) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005579 if (DEBUG_FOCUS) {
5580 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
5581 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582
5583 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005584 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585
5586 if (mInputFilterEnabled == enabled) {
5587 return;
5588 }
5589
5590 mInputFilterEnabled = enabled;
5591 resetAndDropEverythingLocked("input filter is being enabled or disabled");
5592 } // release lock
5593
5594 // Wake up poll loop since there might be work to do to drop everything.
5595 mLooper->wake();
5596}
5597
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005598bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Linnan Li13bf76a2024-05-05 19:18:02 +08005599 bool hasPermission, ui::LogicalDisplayId displayId) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00005600 bool needWake = false;
5601 {
5602 std::scoped_lock lock(mLock);
Antonio Kantek15beb512022-06-13 22:35:41 +00005603 ALOGD_IF(DEBUG_TOUCH_MODE,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005604 "Request to change touch mode to %s (calling pid=%s, uid=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08005605 "hasPermission=%s, target displayId=%s, mTouchModePerDisplay[displayId]=%s)",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005606 toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08005607 toString(hasPermission), displayId.toString().c_str(),
Antonio Kantek15beb512022-06-13 22:35:41 +00005608 mTouchModePerDisplay.count(displayId) == 0
5609 ? "not set"
5610 : std::to_string(mTouchModePerDisplay[displayId]).c_str());
5611
Antonio Kantek15beb512022-06-13 22:35:41 +00005612 auto touchModeIt = mTouchModePerDisplay.find(displayId);
5613 if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08005614 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +00005615 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005616 if (!hasPermission) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005617 if (!focusedWindowIsOwnedByLocked(pid, uid) &&
5618 !recentWindowsAreOwnedByLocked(pid, uid)) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005619 ALOGD("Touch mode switch rejected, caller (pid=%s, uid=%s) doesn't own the focused "
Antonio Kantek48710e42022-03-24 14:19:30 -07005620 "window nor none of the previously interacted window",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005621 pid.toString().c_str(), uid.toString().c_str());
Antonio Kantekea47acb2021-12-23 12:41:25 -08005622 return false;
5623 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00005624 }
Antonio Kantek15beb512022-06-13 22:35:41 +00005625 mTouchModePerDisplay[displayId] = inTouchMode;
5626 auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode,
5627 displayId);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005628 needWake = enqueueInboundEventLocked(std::move(entry));
5629 } // release lock
5630
5631 if (needWake) {
5632 mLooper->wake();
5633 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005634 return true;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -08005635}
5636
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005637bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005638 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
5639 if (focusedToken == nullptr) {
5640 return false;
5641 }
5642 sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken);
5643 return isWindowOwnedBy(windowHandle, pid, uid);
5644}
5645
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005646bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005647 return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(),
5648 [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) {
5649 const sp<WindowInfoHandle> windowHandle =
5650 getWindowHandleLocked(connectionToken);
5651 return isWindowOwnedBy(windowHandle, pid, uid);
5652 }) != mInteractionConnectionTokens.end();
5653}
5654
Bernardo Rufinoea97d182020-08-19 14:43:14 +01005655void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
5656 if (opacity < 0 || opacity > 1) {
5657 LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
5658 return;
5659 }
5660
5661 std::scoped_lock lock(mLock);
5662 mMaximumObscuringOpacityForTouch = opacity;
5663}
5664
Linnan Li13bf76a2024-05-05 19:18:02 +08005665std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId /*displayId*/>
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005666InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005667 for (auto& [displayId, state] : mTouchStatesByDisplay) {
5668 for (TouchedWindow& w : state.windows) {
5669 if (w.windowHandle->getToken() == token) {
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005670 return std::make_tuple(&state, &w, displayId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005671 }
5672 }
5673 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005674 return std::make_tuple(nullptr, nullptr, ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005675}
5676
Prabir Pradhan367f3432024-02-13 23:05:58 +00005677bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
5678 bool isDragDrop) {
chaviwfbe5d9c2018-12-26 12:23:37 -08005679 if (fromToken == toToken) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005680 if (DEBUG_FOCUS) {
5681 ALOGD("Trivial transfer to same window.");
5682 }
chaviwfbe5d9c2018-12-26 12:23:37 -08005683 return true;
5684 }
5685
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005687 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
Arthur Hungabbb9d82021-09-01 14:52:30 +00005689 // Find the target touch state and touched window by fromToken.
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005690 auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005691
Arthur Hungabbb9d82021-09-01 14:52:30 +00005692 if (state == nullptr || touchedWindow == nullptr) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005693 ALOGD("Touch transfer failed because from window is not being touched.");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 return false;
5695 }
Linnan Li72352222024-04-12 18:55:57 +08005696 std::set<DeviceId> deviceIds = touchedWindow->getTouchingDeviceIds();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005697 if (deviceIds.size() != 1) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005698 LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds)
5699 << " for window: " << touchedWindow->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005700 return false;
5701 }
Linnan Li72352222024-04-12 18:55:57 +08005702 const DeviceId deviceId = *deviceIds.begin();
Arthur Hungabbb9d82021-09-01 14:52:30 +00005703
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005704 const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle;
5705 const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId);
5706 if (!toWindowHandle) {
5707 ALOGW("Cannot transfer touch because the transfer target window was not found.");
Arthur Hungabbb9d82021-09-01 14:52:30 +00005708 return false;
5709 }
5710
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005711 if (DEBUG_FOCUS) {
Prabir Pradhan367f3432024-02-13 23:05:58 +00005712 ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005713 touchedWindow->windowHandle->getName().c_str(),
5714 toWindowHandle->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 }
5716
Arthur Hungabbb9d82021-09-01 14:52:30 +00005717 // Erase old window.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005718 ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005719 std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005720 state->removeWindowByToken(fromToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721
Arthur Hungabbb9d82021-09-01 14:52:30 +00005722 // Add new window.
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00005723 nsecs_t downTimeInTarget = now();
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005724 ftl::Flags<InputTarget::Flags> newTargetFlags =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005725 oldTargetFlags & (InputTarget::Flags::SPLIT);
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005726 if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005727 newTargetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005728 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00005729 // Transferring touch focus using this API should not effect the focused window.
5730 newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005731 state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005732 deviceId, pointers, downTimeInTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733
Arthur Hungabbb9d82021-09-01 14:52:30 +00005734 // Store the dragging window.
5735 if (isDragDrop) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005736 if (pointers.size() != 1) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005737 ALOGW("The drag and drop cannot be started when there is no pointer or more than 1"
5738 " pointer on the window.");
Arthur Hung54745652022-04-20 07:17:41 +00005739 return false;
5740 }
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005741 // Track the pointer id for drag window and generate the drag state.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005742 const size_t id = pointers.begin()->id;
Arthur Hung54745652022-04-20 07:17:41 +00005743 mDragState = std::make_unique<DragState>(toWindowHandle, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 }
5745
Arthur Hungabbb9d82021-09-01 14:52:30 +00005746 // Synthesize cancel for old window and down for new window.
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005747 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005748 std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken);
5749 std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07005750 if (fromConnection != nullptr && toConnection != nullptr) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08005751 fromConnection->inputState.mergePointerStateTo(toConnection->inputState);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005752 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005753 "transferring touch from this window to another window",
5754 traceContext.getTracker());
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005755 synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005756
5757 // Check if the wallpaper window should deliver the corresponding event.
5758 transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005759 *state, deviceId, pointers, traceContext.getTracker());
Linnan Li72352222024-04-12 18:55:57 +08005760
5761 // Because new window may have a wallpaper window, it will merge input state from it
5762 // parent window, after this the firstNewPointerIdx in input state will be reset, then
5763 // it will cause new move event be thought inconsistent, so we should synthesize the
5764 // down event after it reset.
5765 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection,
5766 newTargetFlags,
5767 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769 } // release lock
5770
5771 // Wake up poll loop since it may need to make new input dispatching choices.
5772 mLooper->wake();
5773 return true;
5774}
5775
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005776/**
5777 * Get the touched foreground window on the given display.
5778 * Return null if there are no windows touched on that display, or if more than one foreground
5779 * window is being touched.
5780 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005781sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(
5782 ui::LogicalDisplayId displayId) const {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005783 auto stateIt = mTouchStatesByDisplay.find(displayId);
5784 if (stateIt == mTouchStatesByDisplay.end()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005785 ALOGI("No touch state on display %s", displayId.toString().c_str());
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005786 return nullptr;
5787 }
5788
5789 const TouchState& state = stateIt->second;
5790 sp<WindowInfoHandle> touchedForegroundWindow;
5791 // If multiple foreground windows are touched, return nullptr
5792 for (const TouchedWindow& window : state.windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005793 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005794 if (touchedForegroundWindow != nullptr) {
5795 ALOGI("Two or more foreground windows: %s and %s",
5796 touchedForegroundWindow->getName().c_str(),
5797 window.windowHandle->getName().c_str());
5798 return nullptr;
5799 }
5800 touchedForegroundWindow = window.windowHandle;
5801 }
5802 }
5803 return touchedForegroundWindow;
5804}
5805
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005806// Binder call
Prabir Pradhan367f3432024-02-13 23:05:58 +00005807bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken,
Linnan Li13bf76a2024-05-05 19:18:02 +08005808 ui::LogicalDisplayId displayId) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005809 sp<IBinder> fromToken;
5810 { // acquire lock
5811 std::scoped_lock _l(mLock);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005812 sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005813 if (toWindowHandle == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005814 ALOGW("Could not find window associated with token=%p on display %s",
5815 destChannelToken.get(), displayId.toString().c_str());
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005816 return false;
5817 }
5818
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005819 sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId);
5820 if (from == nullptr) {
5821 ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get());
5822 return false;
5823 }
5824
5825 fromToken = from->getToken();
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005826 } // release lock
5827
Prabir Pradhan367f3432024-02-13 23:05:58 +00005828 return transferTouchGesture(fromToken, destChannelToken);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005829}
5830
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005832 if (DEBUG_FOCUS) {
5833 ALOGD("Resetting and dropping all events (%s).", reason);
5834 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005836 ScopedSyntheticEventTracer traceContext(mTracer);
5837 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason,
5838 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 synthesizeCancelationEventsForAllConnectionsLocked(options);
5840
5841 resetKeyRepeatLocked();
5842 releasePendingEventLocked();
5843 drainInboundQueueLocked();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005844 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005846 mAnrTracker.clear();
Jeff Brownf086ddb2014-02-11 14:28:48 -08005847 mTouchStatesByDisplay.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848}
5849
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005850void InputDispatcher::logDispatchStateLocked() const {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005851 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852 dumpDispatchStateLocked(dump);
5853
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005854 std::istringstream stream(dump);
5855 std::string line;
5856
5857 while (std::getline(stream, line, '\n')) {
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07005858 ALOGI("%s", line.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 }
5860}
5861
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005862std::string InputDispatcher::dumpPointerCaptureStateLocked() const {
Prabir Pradhan99987712020-11-10 18:43:05 -08005863 std::string dump;
5864
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005865 dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n",
Hiroki Sato25040232024-02-22 17:21:22 +09005866 toString(mCurrentPointerCaptureRequest.isEnable()));
Prabir Pradhan99987712020-11-10 18:43:05 -08005867
5868 std::string windowName = "None";
5869 if (mWindowTokenWithPointerCapture) {
chaviw98318de2021-05-19 16:45:23 -05005870 const sp<WindowInfoHandle> captureWindowHandle =
Prabir Pradhan99987712020-11-10 18:43:05 -08005871 getWindowHandleLocked(mWindowTokenWithPointerCapture);
5872 windowName = captureWindowHandle ? captureWindowHandle->getName().c_str()
5873 : "token has capture without window";
5874 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005875 dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str());
Prabir Pradhan99987712020-11-10 18:43:05 -08005876
5877 return dump;
5878}
5879
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005880void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
Siarhei Vishniakou043a3ec2019-05-01 11:30:46 -07005881 dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled));
5882 dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen));
5883 dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled));
Linnan Li13bf76a2024-05-05 19:18:02 +08005884 dump += StringPrintf(INDENT "FocusedDisplayId: %s\n", mFocusedDisplayId.toString().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
Tiger Huang721e26f2018-07-24 22:26:19 +08005886 if (!mFocusedApplicationHandlesByDisplay.empty()) {
5887 dump += StringPrintf(INDENT "FocusedApplications:\n");
5888 for (auto& it : mFocusedApplicationHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005889 const ui::LogicalDisplayId displayId = it.first;
Chris Yea209fde2020-07-22 13:54:51 -07005890 const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second;
Siarhei Vishniakou70622952020-07-30 11:17:23 -05005891 const std::chrono::duration timeout =
5892 applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Linnan Li13bf76a2024-05-05 19:18:02 +08005893 dump += StringPrintf(INDENT2 "displayId=%s, name='%s', dispatchingTimeout=%" PRId64
5894 "ms\n",
5895 displayId.toString().c_str(), applicationHandle->getName().c_str(),
5896 millis(timeout));
Tiger Huang721e26f2018-07-24 22:26:19 +08005897 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 } else {
Tiger Huang721e26f2018-07-24 22:26:19 +08005899 dump += StringPrintf(INDENT "FocusedApplications: <none>\n");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005901
Vishnu Nairc519ff72021-01-21 08:23:08 -08005902 dump += mFocusResolver.dump();
Prabir Pradhan99987712020-11-10 18:43:05 -08005903 dump += dumpPointerCaptureStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904
Siarhei Vishniakou4700f822020-03-24 19:05:54 -07005905 if (!mTouchStatesByDisplay.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005906 dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005907 for (const auto& [displayId, state] : mTouchStatesByDisplay) {
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -08005908 std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
Linnan Li13bf76a2024-05-05 19:18:02 +08005909 dump += INDENT2 + displayId.toString() + " : " + touchStateDump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 }
5911 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005912 dump += INDENT "TouchStates: <no displays touched>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 }
5914
arthurhung6d4bed92021-03-17 11:59:33 +08005915 if (mDragState) {
5916 dump += StringPrintf(INDENT "DragState:\n");
5917 mDragState->dump(dump, INDENT2);
5918 }
5919
Arthur Hungb92218b2018-08-14 12:00:21 +08005920 if (!mWindowHandlesByDisplay.empty()) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07005921 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005922 dump += StringPrintf(INDENT "Display: %s\n", displayId.toString().c_str());
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07005923 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
5924 const auto& displayInfo = it->second;
5925 dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth,
5926 displayInfo.logicalHeight);
5927 displayInfo.transform.dump(dump, "transform", INDENT4);
5928 } else {
5929 dump += INDENT2 "No DisplayInfo found!\n";
5930 }
5931
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005932 if (!windowHandles.empty()) {
Arthur Hungb92218b2018-08-14 12:00:21 +08005933 dump += INDENT2 "Windows:\n";
5934 for (size_t i = 0; i < windowHandles.size(); i++) {
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005935 dump += StringPrintf(INDENT3 "%zu: %s", i,
5936 streamableToString(*windowHandles[i]).c_str());
Arthur Hungb92218b2018-08-14 12:00:21 +08005937 }
5938 } else {
5939 dump += INDENT2 "Windows: <none>\n";
5940 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 }
5942 } else {
Arthur Hungb92218b2018-08-14 12:00:21 +08005943 dump += INDENT "Displays: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 }
5945
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005946 if (!mGlobalMonitorsByDisplay.empty()) {
5947 for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005948 dump += StringPrintf(INDENT "Global monitors on display %s:\n",
5949 displayId.toString().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00005950 dumpMonitors(dump, monitors);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07005951 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 } else {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005953 dump += INDENT "Global Monitors: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 }
5955
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005956 const nsecs_t currentTime = now();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 // Dump recently dispatched or dropped events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005959 if (!mRecentQueue.empty()) {
5960 dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005961 for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005962 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005963 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005964 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 }
5966 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005967 dump += INDENT "RecentQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 }
5969
5970 // Dump event currently being dispatched.
5971 if (mPendingEvent) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005972 dump += INDENT "PendingEvent:\n";
5973 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005974 dump += mPendingEvent->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005975 dump += StringPrintf(", age=%" PRId64 "ms\n",
5976 ns2ms(currentTime - mPendingEvent->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005978 dump += INDENT "PendingEvent: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 }
5980
5981 // Dump inbound events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005982 if (!mInboundQueue.empty()) {
5983 dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005984 for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005985 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005986 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005987 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 }
5989 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005990 dump += INDENT "InboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 }
5992
Prabir Pradhancef936d2021-07-21 16:17:52 +00005993 if (!mCommandQueue.empty()) {
5994 dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size());
5995 } else {
5996 dump += INDENT "CommandQueue: <empty>\n";
5997 }
5998
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005999 if (!mConnectionsByToken.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006000 dump += INDENT "Connections:\n";
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006001 for (const auto& [token, connection] : mConnectionsByToken) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006002 dump += StringPrintf(INDENT2 "%i: channelName='%s', "
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006003 "status=%s, monitor=%s, responsive=%s\n",
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006004 connection->inputPublisher.getChannel().getFd(),
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006005 connection->getInputChannelName().c_str(),
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006006 ftl::enum_string(connection->status).c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006007 toString(connection->monitor), toString(connection->responsive));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006009 if (!connection->outboundQueue.empty()) {
6010 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n",
6011 connection->outboundQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006012 dump += dumpQueue(connection->outboundQueue, currentTime);
6013
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006015 dump += INDENT3 "OutboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 }
6017
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006018 if (!connection->waitQueue.empty()) {
6019 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n",
6020 connection->waitQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006021 dump += dumpQueue(connection->waitQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006023 dump += INDENT3 "WaitQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006025 std::string inputStateDump = streamableToString(connection->inputState);
6026 if (!inputStateDump.empty()) {
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006027 dump += INDENT3 "InputState: ";
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006028 dump += inputStateDump + "\n";
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006029 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 }
6031 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006032 dump += INDENT "Connections: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 }
6034
Antonio Kantek15beb512022-06-13 22:35:41 +00006035 if (!mTouchModePerDisplay.empty()) {
6036 dump += INDENT "TouchModePerDisplay:\n";
6037 for (const auto& [displayId, touchMode] : mTouchModePerDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08006038 dump += StringPrintf(INDENT2 "Display: %s TouchMode: %s\n",
6039 displayId.toString().c_str(), std::to_string(touchMode).c_str());
Antonio Kantek15beb512022-06-13 22:35:41 +00006040 }
6041 } else {
6042 dump += INDENT "TouchModePerDisplay: <none>\n";
6043 }
6044
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006045 dump += INDENT "Configuration:\n";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006046 dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay));
6047 dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n",
6048 ns2ms(mConfig.keyRepeatTimeout));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00006049 dump += mLatencyTracker.dump(INDENT2);
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +00006050 dump += mLatencyAggregator.dump(INDENT2);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00006051 dump += INDENT "InputTracer: ";
6052 dump += mTracer == nullptr ? "Disabled" : "Enabled";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053}
6054
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07006055void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const {
Michael Wright3dd60e22019-03-27 22:06:44 +00006056 const size_t numMonitors = monitors.size();
6057 for (size_t i = 0; i < numMonitors; i++) {
6058 const Monitor& monitor = monitors[i];
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006059 const std::shared_ptr<Connection>& connection = monitor.connection;
6060 dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00006061 dump += "\n";
6062 }
6063}
6064
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006065class LooperEventCallback : public LooperCallback {
6066public:
6067 LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {}
6068 int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); }
6069
6070private:
6071 std::function<int(int events)> mCallback;
6072};
6073
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00006074Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006075 if (DEBUG_CHANNEL_CREATION) {
6076 ALOGD("channel '%s' ~ createInputChannel", name.c_str());
6077 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006079 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006080 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006081 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006082
6083 if (result) {
6084 return base::Error(result) << "Failed to open input channel pair with name " << name;
6085 }
6086
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006088 std::scoped_lock _l(mLock);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006089 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006090 const int fd = serverChannel->getFd();
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006091 std::shared_ptr<Connection> connection =
6092 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false,
6093 mIdGenerator);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006095 auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection);
6096 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006097 ALOGE("Created a new connection, but the token %p is already known", token.get());
6098 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006099
6100 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6101 this, std::placeholders::_1, token);
6102
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006103 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006104 nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 } // release lock
6106
6107 // Wake the looper because some connections have changed.
6108 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006109 return clientChannel;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110}
6111
Linnan Li13bf76a2024-05-05 19:18:02 +08006112Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(
6113 ui::LogicalDisplayId displayId, const std::string& name, gui::Pid pid) {
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006114 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006115 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006116 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006117 if (result) {
6118 return base::Error(result) << "Failed to open input channel pair with name " << name;
6119 }
6120
Michael Wright3dd60e22019-03-27 22:06:44 +00006121 { // acquire lock
6122 std::scoped_lock _l(mLock);
6123
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006124 if (displayId < ui::LogicalDisplayId::DEFAULT) {
Garfield Tan15601662020-09-22 15:32:38 -07006125 return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name
6126 << " without a specified display.";
Michael Wright3dd60e22019-03-27 22:06:44 +00006127 }
6128
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006129 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006130 const int fd = serverChannel->getFd();
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006131 std::shared_ptr<Connection> connection =
6132 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true,
6133 mIdGenerator);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006134
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006135 auto [_, inserted] = mConnectionsByToken.emplace(token, connection);
6136 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006137 ALOGE("Created a new connection, but the token %p is already known", token.get());
6138 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006139
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006140 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6141 this, std::placeholders::_1, token);
Michael Wright3dd60e22019-03-27 22:06:44 +00006142
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006143 mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid);
Michael Wright3dd60e22019-03-27 22:06:44 +00006144
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006145 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006146 nullptr);
Michael Wright3dd60e22019-03-27 22:06:44 +00006147 }
Garfield Tan15601662020-09-22 15:32:38 -07006148
Michael Wright3dd60e22019-03-27 22:06:44 +00006149 // Wake the looper because some connections have changed.
6150 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006151 return clientChannel;
Michael Wright3dd60e22019-03-27 22:06:44 +00006152}
6153
Garfield Tan15601662020-09-22 15:32:38 -07006154status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006156 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
Harry Cutts33476232023-01-30 19:57:29 +00006158 status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 if (status) {
6160 return status;
6161 }
6162 } // release lock
6163
6164 // Wake the poll loop because removing the connection may have changed the current
6165 // synchronization state.
6166 mLooper->wake();
6167 return OK;
6168}
6169
Garfield Tan15601662020-09-22 15:32:38 -07006170status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken,
6171 bool notify) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006172 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006173 if (connection == nullptr) {
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00006174 // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel'
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 return BAD_VALUE;
6176 }
6177
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006178 removeConnectionLocked(connection);
Robert Carr5c8a0262018-10-03 16:30:44 -07006179
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 if (connection->monitor) {
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006181 removeMonitorChannelLocked(connectionToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 }
6183
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006184 mLooper->removeFd(connection->inputPublisher.getChannel().getFd());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 nsecs_t currentTime = now();
6187 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
6188
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006189 connection->status = Connection::Status::ZOMBIE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 return OK;
6191}
6192
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006193void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006194 for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) {
6195 auto& [displayId, monitors] = *it;
6196 std::erase_if(monitors, [connectionToken](const Monitor& monitor) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006197 return monitor.connection->getToken() == connectionToken;
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006198 });
Michael Wright3dd60e22019-03-27 22:06:44 +00006199
Michael Wright3dd60e22019-03-27 22:06:44 +00006200 if (monitors.empty()) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006201 it = mGlobalMonitorsByDisplay.erase(it);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006202 } else {
6203 ++it;
6204 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 }
6206}
6207
Michael Wright3dd60e22019-03-27 22:06:44 +00006208status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006209 std::scoped_lock _l(mLock);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006210 return pilferPointersLocked(token);
6211}
Michael Wright3dd60e22019-03-27 22:06:44 +00006212
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006213status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006214 const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token);
6215 if (!requestingConnection) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006216 LOG(WARNING)
6217 << "Attempted to pilfer pointers from an un-registered channel or invalid token";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006218 return BAD_VALUE;
Michael Wright3dd60e22019-03-27 22:06:44 +00006219 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006220
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07006221 auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006222 if (statePtr == nullptr || windowPtr == nullptr) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006223 LOG(WARNING)
6224 << "Attempted to pilfer points from a channel without any on-going pointer streams."
6225 " Ignoring.";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006226 return BAD_VALUE;
6227 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006228 std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07006229 if (deviceIds.empty()) {
6230 LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006231 return BAD_VALUE;
6232 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006233
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006234 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006235 for (const DeviceId deviceId : deviceIds) {
6236 TouchState& state = *statePtr;
6237 TouchedWindow& window = *windowPtr;
6238 // Send cancel events to all the input channels we're stealing from.
6239 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006240 "input channel stole pointer stream", traceContext.getTracker());
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006241 options.deviceId = deviceId;
6242 options.displayId = displayId;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006243 std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId);
6244 std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006245 options.pointerIds = pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006246
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006247 std::string canceledWindows;
6248 for (const TouchedWindow& w : state.windows) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006249 if (w.windowHandle->getToken() != token) {
6250 synthesizeCancelationEventsForWindowLocked(w.windowHandle, options);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006251 canceledWindows += canceledWindows.empty() ? "[" : ", ";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006252 canceledWindows += w.windowHandle->getName();
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006253 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006254 }
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006255 canceledWindows += canceledWindows.empty() ? "[]" : "]";
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006256 LOG(INFO) << "Channel " << requestingConnection->getInputChannelName()
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006257 << " is stealing input gesture for device " << deviceId << " from "
6258 << canceledWindows;
6259
6260 // Prevent the gesture from being sent to any other windows.
6261 // This only blocks relevant pointers to be sent to other windows
6262 window.addPilferingPointers(deviceId, pointerIds);
6263
6264 state.cancelPointersForWindowsExcept(deviceId, pointerIds, token);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006265 }
Michael Wright3dd60e22019-03-27 22:06:44 +00006266 return OK;
6267}
6268
Prabir Pradhan99987712020-11-10 18:43:05 -08006269void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) {
6270 { // acquire lock
6271 std::scoped_lock _l(mLock);
6272 if (DEBUG_FOCUS) {
chaviw98318de2021-05-19 16:45:23 -05006273 const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken);
Prabir Pradhan99987712020-11-10 18:43:05 -08006274 ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable",
6275 windowHandle != nullptr ? windowHandle->getName().c_str()
6276 : "token without window");
6277 }
6278
Vishnu Nairc519ff72021-01-21 08:23:08 -08006279 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08006280 if (focusedToken != windowToken) {
6281 ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.",
6282 enabled ? "enable" : "disable");
6283 return;
6284 }
6285
Hiroki Sato25040232024-02-22 17:21:22 +09006286 if (enabled == mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006287 ALOGW("Ignoring request to %s Pointer Capture: "
6288 "window has %s requested pointer capture.",
6289 enabled ? "enable" : "disable", enabled ? "already" : "not");
6290 return;
6291 }
6292
Christine Franksb768bb42021-11-29 12:11:31 -08006293 if (enabled) {
6294 if (std::find(mIneligibleDisplaysForPointerCapture.begin(),
6295 mIneligibleDisplaysForPointerCapture.end(),
6296 mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) {
6297 ALOGW("Ignoring request to enable Pointer Capture: display is not eligible");
6298 return;
6299 }
6300 }
6301
Hiroki Sato25040232024-02-22 17:21:22 +09006302 setPointerCaptureLocked(enabled ? windowToken : nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08006303 } // release lock
6304
6305 // Wake the thread to process command entries.
6306 mLooper->wake();
6307}
6308
Linnan Li13bf76a2024-05-05 19:18:02 +08006309void InputDispatcher::setDisplayEligibilityForPointerCapture(ui::LogicalDisplayId displayId,
6310 bool isEligible) {
Christine Franksb768bb42021-11-29 12:11:31 -08006311 { // acquire lock
6312 std::scoped_lock _l(mLock);
6313 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
6314 if (!isEligible) {
6315 mIneligibleDisplaysForPointerCapture.push_back(displayId);
6316 }
6317 } // release lock
6318}
6319
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006320std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006321 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00006322 for (const Monitor& monitor : monitors) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006323 if (monitor.connection->getToken() == token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006324 return monitor.pid;
Michael Wright3dd60e22019-03-27 22:06:44 +00006325 }
6326 }
6327 }
6328 return std::nullopt;
6329}
6330
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006331std::shared_ptr<Connection> InputDispatcher::getConnectionLocked(
6332 const sp<IBinder>& inputConnectionToken) const {
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -07006333 if (inputConnectionToken == nullptr) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006334 return nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +08006335 }
6336
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006337 for (const auto& [token, connection] : mConnectionsByToken) {
6338 if (token == inputConnectionToken) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006339 return connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 }
6341 }
Robert Carr4e670e52018-08-15 13:26:12 -07006342
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006343 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344}
6345
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006346std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006347 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006348 if (connection == nullptr) {
6349 return "<nullptr>";
6350 }
6351 return connection->getInputChannelName();
6352}
6353
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006354void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006355 mAnrTracker.eraseToken(connection->getToken());
6356 mConnectionsByToken.erase(connection->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006357}
6358
Prabir Pradhancef936d2021-07-21 16:17:52 +00006359void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006360 const std::shared_ptr<Connection>& connection,
6361 uint32_t seq, bool handled,
6362 nsecs_t consumeTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006363 // Handle post-event policy actions.
Prabir Pradhan24047542023-11-02 17:14:59 +00006364 std::unique_ptr<const KeyEntry> fallbackKeyEntry;
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006365
6366 { // Start critical section
6367 auto dispatchEntryIt =
6368 std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6369 [seq](auto& e) { return e->seq == seq; });
6370 if (dispatchEntryIt == connection->waitQueue.end()) {
6371 return;
6372 }
6373
6374 DispatchEntry& dispatchEntry = **dispatchEntryIt;
6375
6376 const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime;
6377 if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006378 ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006379 ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str());
6380 }
6381 if (shouldReportFinishedEvent(dispatchEntry, *connection)) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006382 mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006383 dispatchEntry.deliveryTime, consumeTime, finishTime);
6384 }
6385
6386 if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006387 fallbackKeyEntry =
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006388 afterKeyEventLockedInterruptable(connection, &dispatchEntry, handled);
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006389 }
6390 } // End critical section: The -LockedInterruptable methods may have released the lock.
Prabir Pradhancef936d2021-07-21 16:17:52 +00006391
6392 // Dequeue the event and start the next cycle.
6393 // Because the lock might have been released, it is possible that the
6394 // contents of the wait queue to have been drained, so we need to double-check
6395 // a few things.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006396 auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6397 [seq](auto& e) { return e->seq == seq; });
6398 if (entryIt != connection->waitQueue.end()) {
6399 std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt);
6400 connection->waitQueue.erase(entryIt);
6401
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006402 const sp<IBinder>& connectionToken = connection->getToken();
Prabir Pradhancef936d2021-07-21 16:17:52 +00006403 mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken);
6404 if (!connection->responsive) {
6405 connection->responsive = isConnectionResponsive(*connection);
6406 if (connection->responsive) {
6407 // The connection was unresponsive, and now it's responsive.
6408 processConnectionResponsiveLocked(*connection);
6409 }
6410 }
6411 traceWaitQueueLength(*connection);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006412 if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006413 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6414 // Only dispatch fallbacks if there is a window for the connection.
6415 if (windowHandle != nullptr) {
6416 const auto inputTarget =
6417 createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
6418 dispatchEntry->targetFlags,
6419 fallbackKeyEntry->downTime);
6420 if (inputTarget.has_value()) {
6421 enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry),
6422 *inputTarget);
6423 }
6424 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00006425 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006426 releaseDispatchEntry(std::move(dispatchEntry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00006427 }
6428
6429 // Start the next dispatch cycle for this connection.
6430 startDispatchCycleLocked(now(), connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431}
6432
Prabir Pradhancef936d2021-07-21 16:17:52 +00006433void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken,
6434 const sp<IBinder>& newToken) {
6435 auto command = [this, oldToken, newToken]() REQUIRES(mLock) {
6436 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006437 mPolicy.notifyFocusChanged(oldToken, newToken);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006438 };
6439 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440}
6441
Prabir Pradhancef936d2021-07-21 16:17:52 +00006442void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) {
6443 auto command = [this, token, x, y]() REQUIRES(mLock) {
6444 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006445 mPolicy.notifyDropWindow(token, x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006446 };
6447 postCommandLocked(std::move(command));
Robert Carrf759f162018-11-13 12:57:11 -08006448}
6449
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006450void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006451 if (connection == nullptr) {
6452 LOG_ALWAYS_FATAL("Caller must check for nullness");
6453 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006454 // Since we are allowing the policy to extend the timeout, maybe the waitQueue
6455 // is already healthy again. Don't raise ANR in this situation
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006456 if (connection->waitQueue.empty()) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006457 ALOGI("Not raising ANR because the connection %s has recovered",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006458 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006459 return;
6460 }
6461 /**
6462 * The "oldestEntry" is the entry that was first sent to the application. That entry, however,
6463 * may not be the one that caused the timeout to occur. One possibility is that window timeout
6464 * has changed. This could cause newer entries to time out before the already dispatched
6465 * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app
6466 * processes the events linearly. So providing information about the oldest entry seems to be
6467 * most useful.
6468 */
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006469 DispatchEntry& oldestEntry = *connection->waitQueue.front();
6470 const nsecs_t currentWait = now() - oldestEntry.deliveryTime;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006471 std::string reason =
6472 android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006473 connection->getInputChannelName().c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006474 ns2ms(currentWait),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006475 oldestEntry.eventEntry->getDescription().c_str());
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006476 sp<IBinder> connectionToken = connection->getToken();
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -06006477 updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006478
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006479 processConnectionUnresponsiveLocked(*connection, std::move(reason));
6480
6481 // Stop waking up for events on this connection, it is already unresponsive
6482 cancelEventsForAnrLocked(connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006483}
6484
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006485void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) {
6486 std::string reason =
6487 StringPrintf("%s does not have a focused window", application->getName().c_str());
6488 updateLastAnrStateLocked(*application, reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006489
Yabin Cui8eb9c552023-06-08 18:05:07 +00006490 auto command = [this, app = std::move(application)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006491 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006492 mPolicy.notifyNoFocusedWindowAnr(app);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006493 };
6494 postCommandLocked(std::move(command));
Bernardo Rufino2e1f6512020-10-08 13:42:07 +00006495}
6496
chaviw98318de2021-05-19 16:45:23 -05006497void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006498 const std::string& reason) {
6499 const std::string windowLabel = getApplicationWindowLabel(nullptr, window);
6500 updateLastAnrStateLocked(windowLabel, reason);
6501}
6502
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006503void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application,
6504 const std::string& reason) {
6505 const std::string windowLabel = getApplicationWindowLabel(&application, nullptr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006506 updateLastAnrStateLocked(windowLabel, reason);
6507}
6508
6509void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
6510 const std::string& reason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 // Capture a record of the InputDispatcher state at the time of the ANR.
Yi Kong9b14ac62018-07-17 13:48:38 -07006512 time_t t = time(nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513 struct tm tm;
6514 localtime_r(&t, &tm);
6515 char timestr[64];
6516 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006517 mLastAnrState.clear();
6518 mLastAnrState += INDENT "ANR:\n";
6519 mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006520 mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str());
6521 mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str());
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006522 dumpDispatchStateLocked(mLastAnrState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006523}
6524
Prabir Pradhancef936d2021-07-21 16:17:52 +00006525void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
Prabir Pradhan24047542023-11-02 17:14:59 +00006526 const KeyEntry& entry) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006527 const KeyEvent event = createKeyEvent(entry);
6528 nsecs_t delay = 0;
6529 { // release lock
6530 scoped_unlock unlock(mLock);
6531 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00006532 delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006533 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
6534 ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
6535 std::to_string(t.duration().count()).c_str());
6536 }
6537 } // acquire lock
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538
6539 if (delay < 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006540 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
Prabir Pradhancef936d2021-07-21 16:17:52 +00006541 } else if (delay == 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006542 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006543 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00006544 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006545 entry.interceptKeyWakeupTime = now() + delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547}
6548
Prabir Pradhancef936d2021-07-21 16:17:52 +00006549void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006550 std::optional<gui::Pid> pid,
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006551 std::string reason) {
Yabin Cui8eb9c552023-06-08 18:05:07 +00006552 auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006553 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006554 mPolicy.notifyWindowUnresponsive(token, pid, r);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006555 };
6556 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006557}
6558
Prabir Pradhanedd96402022-02-15 01:46:16 -08006559void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006560 std::optional<gui::Pid> pid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006561 auto command = [this, token, pid]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006562 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006563 mPolicy.notifyWindowResponsive(token, pid);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006564 };
6565 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006566}
6567
6568/**
6569 * Tell the policy that a connection has become unresponsive so that it can start ANR.
6570 * Check whether the connection of interest is a monitor or a window, and add the corresponding
6571 * command entry to the command queue.
6572 */
6573void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection,
6574 std::string reason) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006575 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006576 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006577 if (connection.monitor) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006578 ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006579 reason.c_str());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006580 pid = findMonitorPidByTokenLocked(connectionToken);
6581 } else {
6582 // The connection is a window
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006583 ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Prabir Pradhanedd96402022-02-15 01:46:16 -08006584 reason.c_str());
6585 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6586 if (handle != nullptr) {
6587 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006588 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006589 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006590 sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006591}
6592
6593/**
6594 * Tell the policy that a connection has become responsive so that it can stop ANR.
6595 */
6596void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006597 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006598 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006599 if (connection.monitor) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006600 pid = findMonitorPidByTokenLocked(connectionToken);
6601 } else {
6602 // The connection is a window
6603 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6604 if (handle != nullptr) {
6605 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006606 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006607 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006608 sendWindowResponsiveCommandLocked(connectionToken, pid);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006609}
6610
Prabir Pradhan24047542023-11-02 17:14:59 +00006611std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable(
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006612 const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, bool handled) {
6613 // The dispatchEntry is currently valid, but it might point to a deleted object after we release
6614 // the lock. For simplicity, make copies of the data of interest here and assume that
6615 // 'dispatchEntry' is not valid after this section.
6616 // Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
6617 // function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
6618 std::shared_ptr<const EventEntry> eventEntry = dispatchEntry->eventEntry;
6619 const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget();
6620 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(eventEntry));
6621 // To prevent misuse, ensure dispatchEntry is no longer valid.
6622 dispatchEntry = nullptr;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006623 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006624 if (!handled) {
6625 // Report the key as unhandled, since the fallback was not handled.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006626 mReporter->reportUnhandledKey(keyEntry.id);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006627 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006628 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006629 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006631 // Get the fallback key state.
6632 // Clear it out after dispatching the UP.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006633 int32_t originalKeyCode = keyEntry.keyCode;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006634 std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006635 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006636 connection->inputState.removeFallbackKey(originalKeyCode);
6637 }
6638
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006639 if (handled || !hasForegroundTarget) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006640 // If the application handles the original key for which we previously
6641 // generated a fallback or if the window is not a foreground window,
6642 // then cancel the associated fallback key, if any.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006643 if (fallbackKeyCode) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006644 // Dispatch the unhandled key to the policy with the cancel flag.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006645 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6646 ALOGD("Unhandled key event: Asking policy to cancel fallback action. "
6647 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6648 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount,
6649 keyEntry.policyFlags);
6650 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006651 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006652 event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006653
6654 mLock.unlock();
6655
Prabir Pradhana41d2442023-04-20 21:30:40 +00006656 if (const auto unhandledKeyFallback =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006657 mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6658 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006659 unhandledKeyFallback) {
6660 event = *unhandledKeyFallback;
6661 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006662
6663 mLock.lock();
6664
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006665 // Cancel the fallback key, but only if we still have a window for the channel.
6666 // It could have been removed during the policy call.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006667 if (*fallbackKeyCode != AKEYCODE_UNKNOWN) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006668 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6669 if (windowHandle != nullptr) {
6670 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
6671 "application handled the original non-fallback key "
6672 "or is no longer a foreground target, "
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006673 "canceling previously dispatched fallback key",
6674 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006675 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006676 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006677 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006678 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006679 connection->inputState.removeFallbackKey(originalKeyCode);
6680 }
6681 } else {
6682 // If the application did not handle a non-fallback key, first check
6683 // that we are in a good state to perform unhandled key event processing
6684 // Then ask the policy what to do with it.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006685 bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006686 if (!fallbackKeyCode && !initialDown) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006687 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6688 ALOGD("Unhandled key event: Skipping unhandled key event processing "
6689 "since this is not an initial down. "
6690 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6691 originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6692 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006693 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006694 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006695
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006696 // Dispatch the unhandled key to the policy.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006697 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6698 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
6699 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6700 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6701 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006702 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006703
6704 mLock.unlock();
6705
Prabir Pradhana41d2442023-04-20 21:30:40 +00006706 bool fallback = false;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006707 if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6708 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006709 fb) {
6710 fallback = true;
6711 event = *fb;
6712 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006713
6714 mLock.lock();
6715
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006716 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006717 connection->inputState.removeFallbackKey(originalKeyCode);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006718 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006719 }
6720
6721 // Latch the fallback keycode for this key on an initial down.
6722 // The fallback keycode cannot change at any other point in the lifecycle.
6723 if (initialDown) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724 if (fallback) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006725 *fallbackKeyCode = event.getKeyCode();
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006726 } else {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006727 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006728 }
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006729 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006730 }
6731
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006732 ALOG_ASSERT(fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006733
6734 // Cancel the fallback key if the policy decides not to send it anymore.
6735 // We will continue to dispatch the key to the policy but we will no
6736 // longer dispatch a fallback key to the application.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006737 if (*fallbackKeyCode != AKEYCODE_UNKNOWN &&
6738 (!fallback || *fallbackKeyCode != event.getKeyCode())) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006739 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6740 if (fallback) {
6741 ALOGD("Unhandled key event: Policy requested to send key %d"
6742 "as a fallback for %d, but on the DOWN it had requested "
6743 "to send %d instead. Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006744 event.getKeyCode(), originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006745 } else {
6746 ALOGD("Unhandled key event: Policy did not request fallback for %d, "
6747 "but on the DOWN it had requested to send %d. "
6748 "Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006749 originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006750 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006751 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006752
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006753 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6754 if (windowHandle != nullptr) {
6755 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006756 "canceling fallback, policy no longer desires it",
6757 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006758 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006759 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006760 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006761
6762 fallback = false;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006763 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006764 if (keyEntry.action != AKEY_EVENT_ACTION_UP) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006765 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006766 }
6767 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006769 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6770 {
6771 std::string msg;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006772 const std::map<int32_t, int32_t>& fallbackKeys =
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006773 connection->inputState.getFallbackKeys();
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006774 for (const auto& [key, value] : fallbackKeys) {
6775 msg += StringPrintf(", %d->%d", key, value);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006776 }
6777 ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.",
6778 fallbackKeys.size(), msg.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006779 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006780 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006781
6782 if (fallback) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006783 // Return the fallback key that we want dispatched to the channel.
6784 std::unique_ptr<KeyEntry> newEntry =
6785 std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState,
6786 event.getEventTime(), event.getDeviceId(),
6787 event.getSource(), event.getDisplayId(),
6788 keyEntry.policyFlags, keyEntry.action,
6789 event.getFlags() | AKEY_EVENT_FLAG_FALLBACK,
6790 *fallbackKeyCode, event.getScanCode(),
6791 event.getMetaState(), event.getRepeatCount(),
6792 event.getDownTime());
Prabir Pradhana67623c2024-02-21 06:57:36 +00006793 if (mTracer) {
6794 newEntry->traceTracker =
6795 mTracer->traceDerivedEvent(*newEntry, *keyEntry.traceTracker);
6796 }
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006797 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6798 ALOGD("Unhandled key event: Dispatching fallback key. "
6799 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006800 originalKeyCode, *fallbackKeyCode, keyEntry.metaState);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006801 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006802 return newEntry;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006803 } else {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006804 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6805 ALOGD("Unhandled key event: No fallback key.");
6806 }
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006807
6808 // Report the key as unhandled, since there is no fallback key.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006809 mReporter->reportUnhandledKey(keyEntry.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 }
6811 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006812 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813}
6814
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815void InputDispatcher::traceInboundQueueLengthLocked() {
6816 if (ATRACE_ENABLED()) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006817 ATRACE_INT("iq", mInboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818 }
6819}
6820
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006821void InputDispatcher::traceOutboundQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822 if (ATRACE_ENABLED()) {
6823 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006824 snprintf(counterName, sizeof(counterName), "oq:%s",
6825 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006826 ATRACE_INT(counterName, connection.outboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827 }
6828}
6829
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006830void InputDispatcher::traceWaitQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831 if (ATRACE_ENABLED()) {
6832 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006833 snprintf(counterName, sizeof(counterName), "wq:%s",
6834 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006835 ATRACE_INT(counterName, connection.waitQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 }
6837}
6838
Siarhei Vishniakou5e20f272023-06-08 17:24:44 -07006839void InputDispatcher::dump(std::string& dump) const {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006840 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006842 dump += "Input Dispatcher State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006843 dumpDispatchStateLocked(dump);
6844
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006845 if (!mLastAnrState.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006846 dump += "\nInput Dispatcher State at time of last ANR:\n";
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006847 dump += mLastAnrState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848 }
6849}
6850
6851void InputDispatcher::monitor() {
6852 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006853 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006854 mLooper->wake();
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006855 mDispatcherIsAlive.wait(_l);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856}
6857
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006858/**
6859 * Wake up the dispatcher and wait until it processes all events and commands.
6860 * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so
6861 * this method can be safely called from any thread, as long as you've ensured that
6862 * the work you are interested in completing has already been queued.
6863 */
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07006864bool InputDispatcher::waitForIdle() const {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006865 /**
6866 * Timeout should represent the longest possible time that a device might spend processing
6867 * events and commands.
6868 */
6869 constexpr std::chrono::duration TIMEOUT = 100ms;
6870 std::unique_lock lock(mLock);
6871 mLooper->wake();
6872 std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT);
6873 return result == std::cv_status::no_timeout;
6874}
6875
Vishnu Naire798b472020-07-23 13:52:21 -07006876/**
6877 * Sets focus to the window identified by the token. This must be called
6878 * after updating any input window handles.
6879 *
6880 * Params:
6881 * request.token - input channel token used to identify the window that should gain focus.
6882 * request.focusedToken - the token that the caller expects currently to be focused. If the
6883 * specified token does not match the currently focused window, this request will be dropped.
6884 * If the specified focused token matches the currently focused window, the call will succeed.
6885 * Set this to "null" if this call should succeed no matter what the currently focused token is.
6886 * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm)
6887 * when requesting the focus change. This determines which request gets
6888 * precedence if there is a focus change request from another source such as pointer down.
6889 */
Vishnu Nair958da932020-08-21 17:12:37 -07006890void InputDispatcher::setFocusedWindow(const FocusRequest& request) {
6891 { // acquire lock
6892 std::scoped_lock _l(mLock);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006893 std::optional<FocusResolver::FocusChanges> changes =
Linnan Li13bf76a2024-05-05 19:18:02 +08006894 mFocusResolver.setFocusedWindow(request,
6895 getWindowHandlesLocked(
6896 ui::LogicalDisplayId{request.displayId}));
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006897 ScopedSyntheticEventTracer traceContext(mTracer);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006898 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006899 onFocusChangedLocked(*changes, traceContext.getTracker());
Vishnu Nair958da932020-08-21 17:12:37 -07006900 }
6901 } // release lock
6902 // Wake up poll loop since it may need to make new input dispatching choices.
6903 mLooper->wake();
6904}
6905
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006906void InputDispatcher::onFocusChangedLocked(
6907 const FocusResolver::FocusChanges& changes,
6908 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker,
6909 const sp<WindowInfoHandle> removedFocusedWindowHandle) {
Vishnu Nairc519ff72021-01-21 08:23:08 -08006910 if (changes.oldFocus) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006911 const auto resolvedWindow = removedFocusedWindowHandle != nullptr
6912 ? removedFocusedWindowHandle
6913 : getWindowHandleLocked(changes.oldFocus, changes.displayId);
6914 if (resolvedWindow == nullptr) {
6915 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006916 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006917 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006918 "focus left window", traceTracker);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006919 synthesizeCancelationEventsForWindowLocked(resolvedWindow, options);
6920 enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006921 }
Vishnu Nairc519ff72021-01-21 08:23:08 -08006922 if (changes.newFocus) {
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07006923 resetNoFocusedWindowTimeoutLocked();
Harry Cutts33476232023-01-30 19:57:29 +00006924 enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006925 }
6926
Prabir Pradhan99987712020-11-10 18:43:05 -08006927 // If a window has pointer capture, then it must have focus. We need to ensure that this
6928 // contract is upheld when pointer capture is being disabled due to a loss of window focus.
6929 // If the window loses focus before it loses pointer capture, then the window can be in a state
6930 // where it has pointer capture but not focus, violating the contract. Therefore we must
6931 // dispatch the pointer capture event before the focus event. Since focus events are added to
6932 // the front of the queue (above), we add the pointer capture event to the front of the queue
6933 // after the focus events are added. This ensures the pointer capture event ends up at the
6934 // front.
6935 disablePointerCaptureForcedLocked();
6936
Vishnu Nairc519ff72021-01-21 08:23:08 -08006937 if (mFocusedDisplayId == changes.displayId) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006938 sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006939 }
6940}
Vishnu Nair958da932020-08-21 17:12:37 -07006941
Prabir Pradhan99987712020-11-10 18:43:05 -08006942void InputDispatcher::disablePointerCaptureForcedLocked() {
Hiroki Sato25040232024-02-22 17:21:22 +09006943 if (!mCurrentPointerCaptureRequest.isEnable() && !mWindowTokenWithPointerCapture) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006944 return;
6945 }
6946
6947 ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus.");
6948
Hiroki Sato25040232024-02-22 17:21:22 +09006949 if (mCurrentPointerCaptureRequest.isEnable()) {
6950 setPointerCaptureLocked(nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08006951 }
6952
6953 if (!mWindowTokenWithPointerCapture) {
6954 // No need to send capture changes because no window has capture.
6955 return;
6956 }
6957
6958 if (mPendingEvent != nullptr) {
6959 // Move the pending event to the front of the queue. This will give the chance
6960 // for the pending event to be dropped if it is a captured event.
6961 mInboundQueue.push_front(mPendingEvent);
6962 mPendingEvent = nullptr;
6963 }
6964
6965 auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006966 mCurrentPointerCaptureRequest);
Prabir Pradhan99987712020-11-10 18:43:05 -08006967 mInboundQueue.push_front(std::move(entry));
6968}
6969
Hiroki Sato25040232024-02-22 17:21:22 +09006970void InputDispatcher::setPointerCaptureLocked(const sp<IBinder>& windowToken) {
6971 mCurrentPointerCaptureRequest.window = windowToken;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006972 mCurrentPointerCaptureRequest.seq++;
6973 auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006974 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006975 mPolicy.setPointerCapture(request);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006976 };
6977 postCommandLocked(std::move(command));
Prabir Pradhan99987712020-11-10 18:43:05 -08006978}
6979
Linnan Li13bf76a2024-05-05 19:18:02 +08006980void InputDispatcher::displayRemoved(ui::LogicalDisplayId displayId) {
Vishnu Nair599f1412021-06-21 10:39:58 -07006981 { // acquire lock
6982 std::scoped_lock _l(mLock);
6983 // Set an empty list to remove all handles from the specific display.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006984 setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006985 setFocusedApplicationLocked(displayId, nullptr);
6986 // Call focus resolver to clean up stale requests. This must be called after input windows
6987 // have been removed for the removed display.
6988 mFocusResolver.displayRemoved(displayId);
Christine Franksb768bb42021-11-29 12:11:31 -08006989 // Reset pointer capture eligibility, regardless of previous state.
6990 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
Antonio Kantek15beb512022-06-13 22:35:41 +00006991 // Remove the associated touch mode state.
6992 mTouchModePerDisplay.erase(displayId);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07006993 mVerifiersByDisplay.erase(displayId);
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00006994 mInputFilterVerifiersByDisplay.erase(displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006995 } // release lock
6996
6997 // Wake up poll loop since it may need to make new input dispatching choices.
6998 mLooper->wake();
6999}
7000
Patrick Williamsd828f302023-04-28 17:52:08 -05007001void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) {
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08007002 if (auto result = validateWindowInfosUpdate(update); !result.ok()) {
7003 {
7004 // acquire lock
7005 std::scoped_lock _l(mLock);
7006 logDispatchStateLocked();
7007 }
7008 LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s",
7009 result.error().message().c_str());
7010 };
chaviw15fab6f2021-06-07 14:15:52 -05007011 // The listener sends the windows as a flattened array. Separate the windows by display for
7012 // more convenient parsing.
Linnan Li13bf76a2024-05-05 19:18:02 +08007013 std::unordered_map<ui::LogicalDisplayId, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
Patrick Williamsd828f302023-04-28 17:52:08 -05007014 for (const auto& info : update.windowInfos) {
chaviw15fab6f2021-06-07 14:15:52 -05007015 handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007016 handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info));
chaviw15fab6f2021-06-07 14:15:52 -05007017 }
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007018
7019 { // acquire lock
7020 std::scoped_lock _l(mLock);
Prabir Pradhan814fe082022-07-22 20:22:18 +00007021
7022 // Ensure that we have an entry created for all existing displays so that if a displayId has
7023 // no windows, we can tell that the windows were removed from the display.
7024 for (const auto& [displayId, _] : mWindowHandlesByDisplay) {
7025 handlesPerDisplay[displayId];
7026 }
7027
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007028 mDisplayInfos.clear();
Patrick Williamsd828f302023-04-28 17:52:08 -05007029 for (const auto& displayInfo : update.displayInfos) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007030 mDisplayInfos.emplace(displayInfo.displayId, displayInfo);
7031 }
7032
7033 for (const auto& [displayId, handles] : handlesPerDisplay) {
7034 setInputWindowsLocked(handles, displayId);
7035 }
Patrick Williams9464b2c2023-05-23 11:22:04 -05007036
7037 if (update.vsyncId < mWindowInfosVsyncId) {
7038 ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64
7039 ", current update vsync id: %" PRId64,
7040 mWindowInfosVsyncId, update.vsyncId);
7041 }
7042 mWindowInfosVsyncId = update.vsyncId;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007043 }
7044 // Wake up poll loop since it may need to make new input dispatching choices.
7045 mLooper->wake();
chaviw15fab6f2021-06-07 14:15:52 -05007046}
7047
Vishnu Nair062a8672021-09-03 16:07:44 -07007048bool InputDispatcher::shouldDropInput(
7049 const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007050 if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) ||
7051 (windowHandle->getInfo()->inputConfig.test(
7052 WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) &&
Vishnu Nair062a8672021-09-03 16:07:44 -07007053 isWindowObscuredLocked(windowHandle))) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007054 ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on "
Linnan Li13bf76a2024-05-05 19:18:02 +08007055 "display %s.",
Vishnu Nair062a8672021-09-03 16:07:44 -07007056 ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(),
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007057 windowHandle->getInfo()->inputConfig.string().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08007058 windowHandle->getInfo()->displayId.toString().c_str());
Vishnu Nair062a8672021-09-03 16:07:44 -07007059 return true;
7060 }
7061 return false;
7062}
7063
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007064void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -05007065 const gui::WindowInfosUpdate& update) {
7066 mDispatcher.onWindowInfosChanged(update);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007067}
7068
Arthur Hungdfd528e2021-12-08 13:23:04 +00007069void InputDispatcher::cancelCurrentTouch() {
7070 {
7071 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007072 ScopedSyntheticEventTracer traceContext(mTracer);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007073 ALOGD("Canceling all ongoing pointer gestures on all displays.");
Michael Wrightfb04fd52022-11-24 22:31:11 +00007074 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007075 "cancel current touch", traceContext.getTracker());
Arthur Hungdfd528e2021-12-08 13:23:04 +00007076 synthesizeCancelationEventsForAllConnectionsLocked(options);
7077
7078 mTouchStatesByDisplay.clear();
Arthur Hungdfd528e2021-12-08 13:23:04 +00007079 }
7080 // Wake up poll loop since there might be work to do.
7081 mLooper->wake();
7082}
7083
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007084void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) {
7085 std::scoped_lock _l(mLock);
7086 mMonitorDispatchingTimeout = timeout;
7087}
7088
Arthur Hungc539dbb2022-12-08 07:45:36 +00007089void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags,
7090 const sp<WindowInfoHandle>& oldWindowHandle,
7091 const sp<WindowInfoHandle>& newWindowHandle,
Linnan Li72352222024-04-12 18:55:57 +08007092 TouchState& state, DeviceId deviceId,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007093 const PointerProperties& pointerProperties,
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07007094 std::vector<InputTarget>& targets) const {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007095 std::vector<PointerProperties> pointers{pointerProperties};
Arthur Hungc539dbb2022-12-08 07:45:36 +00007096 const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test(
7097 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7098 const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) &&
7099 newWindowHandle->getInfo()->inputConfig.test(
7100 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7101 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007102 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007103 const sp<WindowInfoHandle> newWallpaper =
7104 newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr;
7105 if (oldWallpaper == newWallpaper) {
7106 return;
7107 }
7108
7109 if (oldWallpaper != nullptr) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08007110 const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007111 addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007112 oldTouchedWindow.targetFlags, getPointerIds(pointers),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007113 oldTouchedWindow.getDownTimeInTarget(deviceId), targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007114 state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007115 }
7116
7117 if (newWallpaper != nullptr) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007118 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER,
7119 InputTarget::Flags::WINDOW_IS_OBSCURED |
Arthur Hungc539dbb2022-12-08 07:45:36 +00007120 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007121 deviceId, pointers);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007122 }
7123}
7124
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007125void InputDispatcher::transferWallpaperTouch(
7126 ftl::Flags<InputTarget::Flags> oldTargetFlags,
7127 ftl::Flags<InputTarget::Flags> newTargetFlags, const sp<WindowInfoHandle> fromWindowHandle,
Linnan Li72352222024-04-12 18:55:57 +08007128 const sp<WindowInfoHandle> toWindowHandle, TouchState& state, DeviceId deviceId,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007129 const std::vector<PointerProperties>& pointers,
7130 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Arthur Hungc539dbb2022-12-08 07:45:36 +00007131 const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7132 fromWindowHandle->getInfo()->inputConfig.test(
7133 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7134 const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7135 toWindowHandle->getInfo()->inputConfig.test(
7136 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7137
7138 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007139 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007140 const sp<WindowInfoHandle> newWallpaper =
7141 newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr;
7142 if (oldWallpaper == newWallpaper) {
7143 return;
7144 }
7145
7146 if (oldWallpaper != nullptr) {
7147 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007148 "transferring touch focus to another window", traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007149 state.removeWindowByToken(oldWallpaper->getToken());
7150 synthesizeCancelationEventsForWindowLocked(oldWallpaper, options);
7151 }
7152
7153 if (newWallpaper != nullptr) {
7154 nsecs_t downTimeInTarget = now();
Prabir Pradhan65455c72024-02-13 21:46:41 +00007155 ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags;
7156 wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007157 wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED |
7158 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007159 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007160 deviceId, pointers, downTimeInTarget);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007161 std::shared_ptr<Connection> wallpaperConnection =
7162 getConnectionLocked(newWallpaper->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007163 if (wallpaperConnection != nullptr) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007164 std::shared_ptr<Connection> toConnection =
7165 getConnectionLocked(toWindowHandle->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007166 toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState);
7167 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007168 wallpaperFlags, traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007169 }
7170 }
7171}
7172
7173sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow(
7174 const sp<WindowInfoHandle>& windowHandle) const {
7175 const std::vector<sp<WindowInfoHandle>>& windowHandles =
7176 getWindowHandlesLocked(windowHandle->getInfo()->displayId);
7177 bool foundWindow = false;
7178 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
7179 if (!foundWindow && otherHandle != windowHandle) {
7180 continue;
7181 }
7182 if (windowHandle == otherHandle) {
7183 foundWindow = true;
7184 continue;
7185 }
7186
7187 if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) {
7188 return otherHandle;
7189 }
7190 }
7191 return nullptr;
7192}
7193
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007194void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout,
7195 std::chrono::nanoseconds delay) {
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007196 std::scoped_lock _l(mLock);
7197
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007198 mConfig.keyRepeatTimeout = timeout.count();
7199 mConfig.keyRepeatDelay = delay.count();
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007200}
7201
Linnan Li13bf76a2024-05-05 19:18:02 +08007202bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token,
7203 ui::LogicalDisplayId displayId, DeviceId deviceId,
7204 int32_t pointerId) {
Prabir Pradhan64f21d22023-11-28 21:19:42 +00007205 std::scoped_lock _l(mLock);
7206 auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7207 if (touchStateIt == mTouchStatesByDisplay.end()) {
7208 return false;
7209 }
7210 for (const TouchedWindow& window : touchStateIt->second.windows) {
7211 if (window.windowHandle->getToken() == token &&
7212 (window.hasTouchingPointer(deviceId, pointerId) ||
7213 window.hasHoveringPointer(deviceId, pointerId))) {
7214 return true;
7215 }
7216 }
7217 return false;
7218}
7219
Prabir Pradhan04a66422024-03-15 22:45:27 +00007220void InputDispatcher::setInputMethodConnectionIsActive(bool isActive) {
7221 std::scoped_lock _l(mLock);
7222 if (mTracer) {
7223 mTracer->setInputMethodConnectionIsActive(isActive);
7224 }
7225}
7226
Garfield Tane84e6f92019-08-29 17:28:41 -07007227} // namespace android::inputdispatcher