blob: b0beeca43ce0579b6342d715717d124a3ed1cbc6 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputDispatcher"
18#define ATRACE_TAG ATRACE_TAG_INPUT
19
John Recke0710582019-09-26 13:46:12 -070020#define LOG_NDEBUG 1
Michael Wrightd02c5b62014-02-10 15:10:22 -080021
Michael Wright2b3c3302018-03-02 17:19:13 +000022#include <android-base/chrono_utils.h>
Siarhei Vishniakoud010b012023-01-18 15:00:53 -080023#include <android-base/logging.h>
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080024#include <android-base/properties.h>
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080025#include <android-base/stringprintf.h>
Siarhei Vishniakou70622952020-07-30 11:17:23 -050026#include <android/os/IInputConstants.h>
Robert Carr4e670e52018-08-15 13:26:12 -070027#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000028#include <com_android_input_flags.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080029#include <ftl/enum.h>
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070030#include <log/log_event_list.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070031#if defined(__ANDROID__)
chaviw15fab6f2021-06-07 14:15:52 -050032#include <gui/SurfaceComposerClient.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070033#endif
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -080034#include <input/InputDevice.h>
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080035#include <input/PrintTools.h>
Prabir Pradhana37bad12023-08-18 15:55:32 +000036#include <input/TraceTools.h>
tyiu1573a672023-02-21 22:38:32 +000037#include <openssl/mem.h>
Prabir Pradhanadc59b42023-12-15 05:34:11 +000038#include <private/android_filesystem_config.h>
Michael Wright44753b12020-07-08 13:48:11 +010039#include <unistd.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070040#include <utils/Trace.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wright44753b12020-07-08 13:48:11 +010042#include <cerrno>
43#include <cinttypes>
44#include <climits>
45#include <cstddef>
46#include <ctime>
47#include <queue>
48#include <sstream>
49
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000050#include "../InputDeviceMetricsSource.h"
51
Michael Wright44753b12020-07-08 13:48:11 +010052#include "Connection.h"
Arthur Hung1a1007b2022-05-11 07:15:01 +000053#include "DebugConfig.h"
Chris Yef59a2f42020-10-16 12:55:26 -070054#include "InputDispatcher.h"
jioana0bdbea12024-08-10 19:26:04 +000055#include "InputEventTimeline.h"
Prabir Pradhandae52792023-12-15 07:36:40 +000056#include "trace/InputTracer.h"
57#include "trace/InputTracingPerfettoBackend.h"
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000058#include "trace/ThreadedBackend.h"
Michael Wright44753b12020-07-08 13:48:11 +010059
Michael Wrightd02c5b62014-02-10 15:10:22 -080060#define INDENT " "
61#define INDENT2 " "
62#define INDENT3 " "
63#define INDENT4 " "
64
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080065using namespace android::ftl::flag_operators;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -070066using android::base::Error;
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080067using android::base::HwTimeoutMultiplier;
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000068using android::base::Result;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080069using android::base::StringPrintf;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -070070using android::gui::DisplayInfo;
chaviw98318de2021-05-19 16:45:23 -050071using android::gui::FocusRequest;
72using android::gui::TouchOcclusionMode;
73using android::gui::WindowInfo;
74using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080075using android::os::InputEventInjectionResult;
76using android::os::InputEventInjectionSync;
Ameer Armalycff4fa52023-10-04 23:45:11 +000077namespace input_flags = com::android::input::flags;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080078
Garfield Tane84e6f92019-08-29 17:28:41 -070079namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Prabir Pradhancef936d2021-07-21 16:17:52 +000081namespace {
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000082
Prabir Pradhandae52792023-12-15 07:36:40 +000083// Input tracing is only available on debuggable builds (userdebug and eng) when the feature
84// flag is enabled. When the flag is changed, tracing will only be available after reboot.
85bool isInputTracingEnabled() {
86 static const std::string buildType = base::GetProperty("ro.build.type", "user");
87 static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
88 return input_flags::enable_input_event_tracing() && isUserdebugOrEng;
89}
90
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000091// Create the input tracing backend that writes to perfetto from a single thread.
Prabir Pradhanf4099452024-04-12 18:34:14 +000092std::unique_ptr<trace::InputTracingBackendInterface> createInputTracingBackendIfEnabled() {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000093 if (!isInputTracingEnabled()) {
94 return nullptr;
95 }
96 return std::make_unique<trace::impl::ThreadedBackend<trace::impl::PerfettoBackend>>(
Prabir Pradhanf4099452024-04-12 18:34:14 +000097 trace::impl::PerfettoBackend());
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000098}
99
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000100template <class Entry>
101void ensureEventTraced(const Entry& entry) {
102 if (!entry.traceTracker) {
103 LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry;
104 }
105}
106
Prabir Pradhan052fb0b2024-02-23 21:03:12 +0000107// Helper to get a trace tracker from a traced key or motion entry.
108const std::unique_ptr<trace::EventTrackerInterface>& getTraceTracker(const EventEntry& entry) {
109 switch (entry.type) {
110 case EventEntry::Type::MOTION: {
111 const auto& motion = static_cast<const MotionEntry&>(entry);
112 ensureEventTraced(motion);
113 return motion.traceTracker;
114 }
115 case EventEntry::Type::KEY: {
116 const auto& key = static_cast<const KeyEntry&>(entry);
117 ensureEventTraced(key);
118 return key.traceTracker;
119 }
120 default: {
121 const static std::unique_ptr<trace::EventTrackerInterface> kNullTracker;
122 return kNullTracker;
123 }
124 }
125}
126
Prabir Pradhancef936d2021-07-21 16:17:52 +0000127// Temporarily releases a held mutex for the lifetime of the instance.
128// Named to match std::scoped_lock
129class scoped_unlock {
130public:
131 explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); }
132 ~scoped_unlock() { mMutex.lock(); }
133
134private:
135 std::mutex& mMutex;
136};
137
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138// Default input dispatching timeout if there is no focused application or paused window
139// from which to determine an appropriate dispatching timeout.
Peter Collingbourneb04b9b82021-02-08 12:09:47 -0800140const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds(
141 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
142 HwTimeoutMultiplier());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800144// The default minimum time gap between two user activity poke events.
145const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms;
146
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800147const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149// Log a warning when an event takes longer than this to process, even if an ANR does not occur.
Michael Wright2b3c3302018-03-02 17:19:13 +0000150constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
151
152// Log a warning when an interception call takes longer than this to process.
153constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154
155// Number of recent events to keep for debugging purposes.
Michael Wright2b3c3302018-03-02 17:19:13 +0000156constexpr size_t RECENT_QUEUE_MAX_SIZE = 10;
157
jioana24878b52024-09-10 10:13:27 +0000158// Interval at which we should push the atom gathering input event latencies in
159// LatencyAggregatorWithHistograms
160constexpr nsecs_t LATENCY_STATISTICS_PUSH_INTERVAL = 6 * 3600 * 1000000000LL; // 6 hours
161
Antonio Kantekea47acb2021-12-23 12:41:25 -0800162// Event log tags. See EventLogTags.logtags for reference.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000163constexpr int LOGTAG_INPUT_INTERACTION = 62000;
164constexpr int LOGTAG_INPUT_FOCUS = 62001;
Arthur Hungb3307ee2021-10-14 10:57:37 +0000165constexpr int LOGTAG_INPUT_CANCEL = 62003;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000166
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000167const ui::Transform kIdentityTransform;
168
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000169inline nsecs_t now() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170 return systemTime(SYSTEM_TIME_MONOTONIC);
171}
172
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700173inline const std::string binderToString(const sp<IBinder>& binder) {
Bernardo Rufino49d99e42021-01-18 15:16:59 +0000174 if (binder == nullptr) {
175 return "<null>";
176 }
177 return StringPrintf("%p", binder.get());
178}
179
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000180static std::string uidString(const gui::Uid& uid) {
181 return uid.toString();
182}
183
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700184Result<void> checkKeyAction(int32_t action) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 switch (action) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700186 case AKEY_EVENT_ACTION_DOWN:
187 case AKEY_EVENT_ACTION_UP:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700188 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700189 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700190 return Error() << "Key event has invalid action code " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 }
192}
193
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700194Result<void> validateKeyEvent(int32_t action) {
195 return checkKeyAction(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196}
197
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700198Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800199 switch (MotionEvent::getActionMasked(action)) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700200 case AMOTION_EVENT_ACTION_DOWN:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700201 case AMOTION_EVENT_ACTION_UP: {
202 if (pointerCount != 1) {
203 return Error() << "invalid pointer count " << pointerCount;
204 }
205 return {};
206 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700207 case AMOTION_EVENT_ACTION_MOVE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700208 case AMOTION_EVENT_ACTION_HOVER_ENTER:
209 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700210 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
211 if (pointerCount < 1) {
212 return Error() << "invalid pointer count " << pointerCount;
213 }
214 return {};
215 }
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800216 case AMOTION_EVENT_ACTION_CANCEL:
217 case AMOTION_EVENT_ACTION_OUTSIDE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700218 case AMOTION_EVENT_ACTION_SCROLL:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700219 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700220 case AMOTION_EVENT_ACTION_POINTER_DOWN:
221 case AMOTION_EVENT_ACTION_POINTER_UP: {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800222 const int32_t index = MotionEvent::getActionIndex(action);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700223 if (index < 0) {
224 return Error() << "invalid index " << index << " for "
225 << MotionEvent::actionToString(action);
226 }
227 if (index >= pointerCount) {
228 return Error() << "invalid index " << index << " for pointerCount " << pointerCount;
229 }
230 if (pointerCount <= 1) {
231 return Error() << "invalid pointer count " << pointerCount << " for "
232 << MotionEvent::actionToString(action);
233 }
234 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700235 }
236 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700237 case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
238 if (actionButton == 0) {
239 return Error() << "action button should be nonzero for "
240 << MotionEvent::actionToString(action);
241 }
242 return {};
243 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700244 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700245 return Error() << "invalid action " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 }
247}
248
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000249int64_t millis(std::chrono::nanoseconds t) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500250 return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
251}
252
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700253Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount,
254 const PointerProperties* pointerProperties) {
255 Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount);
256 if (!actionCheck.ok()) {
257 return actionCheck;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 }
259 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700260 return Error() << "Motion event has invalid pointer count " << pointerCount
261 << "; value must be between 1 and " << MAX_POINTERS << ".";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800263 std::bitset<MAX_POINTER_ID + 1> pointerIdBits;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 for (size_t i = 0; i < pointerCount; i++) {
265 int32_t id = pointerProperties[i].id;
266 if (id < 0 || id > MAX_POINTER_ID) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700267 return Error() << "Motion event has invalid pointer id " << id
268 << "; value must be between 0 and " << MAX_POINTER_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800270 if (pointerIdBits.test(id)) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700271 return Error() << "Motion event has duplicate pointer id " << id;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800273 pointerIdBits.set(id);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700275 return {};
276}
277
278Result<void> validateInputEvent(const InputEvent& event) {
279 switch (event.getType()) {
280 case InputEventType::KEY: {
281 const KeyEvent& key = static_cast<const KeyEvent&>(event);
282 const int32_t action = key.getAction();
283 return validateKeyEvent(action);
284 }
285 case InputEventType::MOTION: {
286 const MotionEvent& motion = static_cast<const MotionEvent&>(event);
287 const int32_t action = motion.getAction();
288 const size_t pointerCount = motion.getPointerCount();
289 const PointerProperties* pointerProperties = motion.getPointerProperties();
290 const int32_t actionButton = motion.getActionButton();
291 return validateMotionEvent(action, actionButton, pointerCount, pointerProperties);
292 }
293 default: {
294 return {};
295 }
296 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297}
298
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800299std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) {
300 std::bitset<MAX_POINTER_ID + 1> pointerIds;
301 for (const PointerProperties& pointer : pointers) {
302 pointerIds.set(pointer.id);
303 }
304 return pointerIds;
305}
306
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000307std::string dumpRegion(const Region& region) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 if (region.isEmpty()) {
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000309 return "<empty>";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000312 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 bool first = true;
314 Region::const_iterator cur = region.begin();
315 Region::const_iterator const tail = region.end();
316 while (cur != tail) {
317 if (first) {
318 first = false;
319 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800320 dump += "|";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800322 dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 cur++;
324 }
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000325 return dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326}
327
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000328std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue,
329 nsecs_t currentTime) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500330 constexpr size_t maxEntries = 50; // max events to print
331 constexpr size_t skipBegin = maxEntries / 2;
332 const size_t skipEnd = queue.size() - maxEntries / 2;
333 // skip from maxEntries / 2 ... size() - maxEntries/2
334 // only print from 0 .. skipBegin and then from skipEnd .. size()
335
336 std::string dump;
337 for (size_t i = 0; i < queue.size(); i++) {
338 const DispatchEntry& entry = *queue[i];
339 if (i >= skipBegin && i < skipEnd) {
340 dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin);
341 i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue'
342 continue;
343 }
344 dump.append(INDENT4);
345 dump += entry.eventEntry->getDescription();
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +0000346 dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq,
347 entry.targetFlags.string().c_str(),
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500348 ns2ms(currentTime - entry.eventEntry->eventTime));
349 if (entry.deliveryTime != 0) {
350 // This entry was delivered, so add information on how long we've been waiting
351 dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime));
352 }
353 dump.append("\n");
354 }
355 return dump;
356}
357
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700358/**
359 * Find the entry in std::unordered_map by key, and return it.
360 * If the entry is not found, return a default constructed entry.
361 *
362 * Useful when the entries are vectors, since an empty vector will be returned
363 * if the entry is not found.
364 * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned.
365 */
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700366template <typename K, typename V>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000367V getValueByKey(const std::unordered_map<K, V>& map, K key) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700368 auto it = map.find(key);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700369 return it != map.end() ? it->second : V{};
Tiger Huang721e26f2018-07-24 22:26:19 +0800370}
371
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000372bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) {
chaviwaf87b3e2019-10-01 16:59:28 -0700373 if (first == second) {
374 return true;
375 }
376
377 if (first == nullptr || second == nullptr) {
378 return false;
379 }
380
381 return first->getToken() == second->getToken();
382}
383
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000384bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) {
Bernardo Rufino1ff9d592021-01-18 16:58:57 +0000385 if (first == nullptr || second == nullptr) {
386 return false;
387 }
388 return first->applicationInfo.token != nullptr &&
389 first->applicationInfo.token == second->applicationInfo.token;
390}
391
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800392template <typename T>
393size_t firstMarkedBit(T set) {
394 // TODO: replace with std::countr_zero from <bit> when that's available
395 LOG_ALWAYS_FATAL_IF(set.none());
396 size_t i = 0;
397 while (!set.test(i)) {
398 i++;
399 }
400 return i;
401}
402
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000403std::unique_ptr<DispatchEntry> createDispatchEntry(const IdGenerator& idGenerator,
404 const InputTarget& inputTarget,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000405 std::shared_ptr<const EventEntry> eventEntry,
406 ftl::Flags<InputTarget::Flags> inputTargetFlags,
Prabir Pradhana67623c2024-02-21 06:57:36 +0000407 int64_t vsyncId,
408 trace::InputTracerInterface* tracer) {
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000409 const bool zeroCoords = inputTargetFlags.test(InputTarget::Flags::ZERO_COORDS);
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000410 const sp<WindowInfoHandle> win = inputTarget.windowHandle;
411 const std::optional<int32_t> windowId =
412 win ? std::make_optional(win->getInfo()->id) : std::nullopt;
413 // Assume the only targets that are not associated with a window are global monitors, and use
414 // the system UID for global monitors for tracing purposes.
415 const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000416
417 if (inputTarget.useDefaultPointerTransform() && !zeroCoords) {
chaviw1ff3d1e2020-07-01 15:53:47 -0700418 const ui::Transform& transform = inputTarget.getDefaultPointerTransform();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700419 return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700420 inputTarget.displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000421 inputTarget.globalScaleFactor, uid, vsyncId,
422 windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000423 }
424
425 ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION);
426 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
427
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000428 std::vector<PointerCoords> pointerCoords{motionEntry.getPointerCount()};
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000429
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000430 const ui::Transform* transform = &kIdentityTransform;
431 const ui::Transform* displayTransform = &kIdentityTransform;
432 if (zeroCoords) {
433 std::for_each(pointerCoords.begin(), pointerCoords.end(), [](auto& pc) { pc.clear(); });
434 } else {
435 // Use the first pointer information to normalize all other pointers. This could be any
436 // pointer as long as all other pointers are normalized to the same value and the final
437 // DispatchEntry uses the transform for the normalized pointer.
438 transform =
439 &inputTarget.getTransformForPointer(firstMarkedBit(inputTarget.getPointerIds()));
440 const ui::Transform inverseTransform = transform->inverse();
441 displayTransform = &inputTarget.displayTransform;
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000442
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000443 // Iterate through all pointers in the event to normalize against the first.
444 for (size_t i = 0; i < motionEntry.getPointerCount(); i++) {
445 PointerCoords& newCoords = pointerCoords[i];
446 const auto pointerId = motionEntry.pointerProperties[i].id;
447 const ui::Transform& currTransform = inputTarget.getTransformForPointer(pointerId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000448
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000449 newCoords.copyFrom(motionEntry.pointerCoords[i]);
450 // First, apply the current pointer's transform to update the coordinates into
451 // window space.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +0000452 MotionEvent::calculateTransformedCoordsInPlace(newCoords, motionEntry.source,
453 motionEntry.flags, currTransform);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000454 // Next, apply the inverse transform of the normalized coordinates so the
455 // current coordinates are transformed into the normalized coordinate space.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +0000456 MotionEvent::calculateTransformedCoordsInPlace(newCoords, motionEntry.source,
457 motionEntry.flags, inverseTransform);
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000458 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000459 }
460
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700461 std::unique_ptr<MotionEntry> combinedMotionEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000462 std::make_unique<MotionEntry>(idGenerator.nextId(), motionEntry.injectionState,
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000463 motionEntry.eventTime, motionEntry.deviceId,
464 motionEntry.source, motionEntry.displayId,
465 motionEntry.policyFlags, motionEntry.action,
466 motionEntry.actionButton, motionEntry.flags,
467 motionEntry.metaState, motionEntry.buttonState,
468 motionEntry.classification, motionEntry.edgeFlags,
469 motionEntry.xPrecision, motionEntry.yPrecision,
470 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
471 motionEntry.downTime, motionEntry.pointerProperties,
472 pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000473 if (tracer) {
474 combinedMotionEntry->traceTracker =
475 tracer->traceDerivedEvent(*combinedMotionEntry, *motionEntry.traceTracker);
476 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000477
478 std::unique_ptr<DispatchEntry> dispatchEntry =
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700479 std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags,
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +0000480 *transform, *displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000481 inputTarget.globalScaleFactor, uid, vsyncId, windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000482 return dispatchEntry;
483}
484
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500485template <typename T>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000486bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) {
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500487 if (lhs == nullptr && rhs == nullptr) {
488 return true;
489 }
490 if (lhs == nullptr || rhs == nullptr) {
491 return false;
492 }
493 return *lhs == *rhs;
494}
495
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000496KeyEvent createKeyEvent(const KeyEntry& entry) {
Siarhei Vishniakou2e2ea992020-12-15 02:57:19 +0000497 KeyEvent event;
498 event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC,
499 entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState,
500 entry.repeatCount, entry.downTime, entry.eventTime);
501 return event;
502}
503
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000504bool shouldReportMetricsForConnection(const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000505 // Do not keep track of gesture monitors. They receive every event and would disproportionately
506 // affect the statistics.
507 if (connection.monitor) {
508 return false;
509 }
510 // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics
511 if (!connection.responsive) {
512 return false;
513 }
514 return true;
515}
516
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000517bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000518 const EventEntry& eventEntry = *dispatchEntry.eventEntry;
519 const int32_t& inputEventId = eventEntry.id;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000520 if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) {
521 return false;
522 }
523 // Only track latency for events that originated from hardware
524 if (eventEntry.isSynthesized()) {
525 return false;
526 }
527 const EventEntry::Type& inputEventEntryType = eventEntry.type;
528 if (inputEventEntryType == EventEntry::Type::KEY) {
529 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
530 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
531 return false;
532 }
533 } else if (inputEventEntryType == EventEntry::Type::MOTION) {
534 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
535 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
536 motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
537 return false;
538 }
539 } else {
540 // Not a key or a motion
541 return false;
542 }
543 if (!shouldReportMetricsForConnection(connection)) {
544 return false;
545 }
546 return true;
547}
548
Prabir Pradhancef936d2021-07-21 16:17:52 +0000549/**
550 * Connection is responsive if it has no events in the waitQueue that are older than the
551 * current time.
552 */
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000553bool isConnectionResponsive(const Connection& connection) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000554 const nsecs_t currentTime = now();
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000555 for (const auto& dispatchEntry : connection.waitQueue) {
556 if (dispatchEntry->timeoutTime < currentTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000557 return false;
558 }
559 }
560 return true;
561}
562
Antonio Kantekf16f2832021-09-28 04:39:20 +0000563// Returns true if the event type passed as argument represents a user activity.
564bool isUserActivityEvent(const EventEntry& eventEntry) {
565 switch (eventEntry.type) {
Josep del Riob3981622023-04-18 15:49:45 +0000566 case EventEntry::Type::DEVICE_RESET:
567 case EventEntry::Type::DRAG:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000568 case EventEntry::Type::FOCUS:
569 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000570 case EventEntry::Type::SENSOR:
Josep del Riob3981622023-04-18 15:49:45 +0000571 case EventEntry::Type::TOUCH_MODE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000572 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +0000573 case EventEntry::Type::KEY:
574 case EventEntry::Type::MOTION:
575 return true;
576 }
577}
578
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800579// Returns true if the given window can accept pointer events at the given display location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800580bool windowAcceptsTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
581 float y, bool isStylus, const ui::Transform& displayTransform) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800582 const auto inputConfig = windowInfo.inputConfig;
583 if (windowInfo.displayId != displayId ||
584 inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800585 return false;
586 }
Prabir Pradhand65552b2021-10-07 11:23:50 -0700587 const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800588 if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800589 return false;
590 }
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000591
592 // Window Manager works in the logical display coordinate space. When it specifies bounds for a
593 // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside
594 // the window. Points on the right and bottom edges should not be inside the window, so we need
595 // to be careful about performing a hit test when the display is rotated, since the "right" and
596 // "bottom" of the window will be different in the display (un-rotated) space compared to in the
597 // logical display in which WM determined the bounds. Perform the hit test in the logical
598 // display space to ensure these edges are considered correctly in all orientations.
599 const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion);
600 const auto p = displayTransform.transform(x, y);
601 if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800602 return false;
603 }
604 return true;
605}
606
Linnan Li5e5645e2024-03-05 14:43:05 +0000607// Returns true if the given window's frame can occlude pointer events at the given display
608// location.
Linnan Li13bf76a2024-05-05 19:18:02 +0800609bool windowOccludesTouchAt(const WindowInfo& windowInfo, ui::LogicalDisplayId displayId, float x,
610 float y, const ui::Transform& displayTransform) {
Linnan Li5e5645e2024-03-05 14:43:05 +0000611 if (windowInfo.displayId != displayId) {
612 return false;
613 }
614 const auto frame = displayTransform.transform(windowInfo.frame);
615 const auto p = floor(displayTransform.transform(x, y));
616 return p.x >= frame.left && p.x < frame.right && p.y >= frame.top && p.y < frame.bottom;
617}
618
Prabir Pradhand65552b2021-10-07 11:23:50 -0700619bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) {
620 return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) &&
Prabir Pradhane5626962022-10-27 20:30:53 +0000621 isStylusToolType(entry.pointerProperties[pointerIndex].toolType);
Prabir Pradhand65552b2021-10-07 11:23:50 -0700622}
623
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800624// Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000625// Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to
626// such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can
627// be sent to such a window, but it is not a foreground event and doesn't use
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800628// InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000629bool canReceiveForegroundTouches(const WindowInfo& info) {
630 // A non-touchable window can still receive touch events (e.g. in the case of
631 // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches.
632 return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy();
633}
634
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000635bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -0700636 if (windowHandle == nullptr) {
637 return false;
638 }
639 const WindowInfo* windowInfo = windowHandle->getInfo();
640 if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) {
641 return true;
642 }
643 return false;
644}
645
Prabir Pradhan5735a322022-04-11 17:23:34 +0000646// Checks targeted injection using the window's owner's uid.
647// Returns an empty string if an entry can be sent to the given window, or an error message if the
648// entry is a targeted injection whose uid target doesn't match the window owner.
649std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window,
650 const EventEntry& entry) {
651 if (entry.injectionState == nullptr || !entry.injectionState->targetUid) {
652 // The event was not injected, or the injected event does not target a window.
653 return {};
654 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000655 const auto uid = *entry.injectionState->targetUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +0000656 if (window == nullptr) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000657 return StringPrintf("No valid window target for injection into uid %s.",
658 uid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000659 }
660 if (entry.injectionState->targetUid != window->getInfo()->ownerUid) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000661 return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' "
662 "owned by uid %s.",
663 uid.toString().c_str(), window->getName().c_str(),
664 window->getInfo()->ownerUid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000665 }
666 return {};
667}
668
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000669std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700670 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
671 // Always dispatch mouse events to cursor position.
672 if (isFromMouse) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000673 return {entry.xCursorPosition, entry.yCursorPosition};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700674 }
675
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -0700676 const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action);
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000677 return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X),
678 entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700679}
680
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -0700681std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) {
682 if (eventEntry.type == EventEntry::Type::KEY) {
683 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
684 return keyEntry.downTime;
685 } else if (eventEntry.type == EventEntry::Type::MOTION) {
686 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
687 return motionEntry.downTime;
688 }
689 return std::nullopt;
690}
691
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000692/**
693 * Compare the old touch state to the new touch state, and generate the corresponding touched
694 * windows (== input targets).
695 * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window.
696 * If the pointer just entered the new window, produce HOVER_ENTER.
697 * For pointers remaining in the window, produce HOVER_MOVE.
698 */
699std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState,
700 const TouchState& newTouchState,
Siarhei Vishniakouf91f3c72024-08-12 09:58:04 -0700701 const MotionEntry& entry,
702 std::function<void()> dump) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000703 const int32_t maskedAction = MotionEvent::getActionMasked(entry.action);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700704
705 if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
706 // ACTION_SCROLL events should not affect the hovering pointer dispatch
707 return {};
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000708 }
Linnan Li41859c42024-03-05 16:20:34 +0000709 std::vector<TouchedWindow> out;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000710
711 // We should consider all hovering pointers here. But for now, just use the first one
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800712 const PointerProperties& pointer = entry.pointerProperties[0];
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000713
714 std::set<sp<WindowInfoHandle>> oldWindows;
715 if (oldState != nullptr) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800716 oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000717 }
718
719 std::set<sp<WindowInfoHandle>> newWindows =
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800720 newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000721
722 // If the pointer is no longer in the new window set, send HOVER_EXIT.
723 for (const sp<WindowInfoHandle>& oldWindow : oldWindows) {
724 if (newWindows.find(oldWindow) == newWindows.end()) {
725 TouchedWindow touchedWindow;
726 touchedWindow.windowHandle = oldWindow;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000727 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000728 out.push_back(touchedWindow);
729 }
730 }
731
732 for (const sp<WindowInfoHandle>& newWindow : newWindows) {
733 TouchedWindow touchedWindow;
734 touchedWindow.windowHandle = newWindow;
735 if (oldWindows.find(newWindow) == oldWindows.end()) {
736 // Any windows that have this pointer now, and didn't have it before, should get
737 // HOVER_ENTER
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000738 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000739 } else {
740 // This pointer was already sent to the window. Use ACTION_HOVER_MOVE.
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700741 if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700742 android::base::LogSeverity severity = android::base::LogSeverity::FATAL;
Ameer Armalycff4fa52023-10-04 23:45:11 +0000743 if (!input_flags::a11y_crash_on_inconsistent_event_stream() &&
744 entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700745 // The Accessibility injected touch exploration event stream
746 // has known inconsistencies, so log ERROR instead of
747 // crashing the device with FATAL.
Daniel Norman7487dfa2023-08-02 16:39:45 -0700748 severity = android::base::LogSeverity::ERROR;
749 }
Siarhei Vishniakouf91f3c72024-08-12 09:58:04 -0700750 dump();
Daniel Norman7487dfa2023-08-02 16:39:45 -0700751 LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription();
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700752 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000753 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000754 }
Siarhei Vishniakoub711e652024-08-12 12:00:28 -0700755 const auto [x, y] = resolveTouchedPosition(entry);
756 touchedWindow.addHoveringPointer(entry.deviceId, pointer, x, y);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000757 if (canReceiveForegroundTouches(*newWindow->getInfo())) {
758 touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND;
759 }
760 out.push_back(touchedWindow);
761 }
762 return out;
763}
764
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800765template <typename T>
766std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) {
767 left.insert(left.end(), right.begin(), right.end());
768 return left;
769}
770
Harry Cuttsb166c002023-05-09 13:06:05 +0000771// Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from
772// both.
773void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) {
774 std::erase_if(touchState.windows, [&](const TouchedWindow& window) {
775 if (!window.windowHandle->getInfo()->inputConfig.test(
776 WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
777 // In addition to TouchState, erase this window from the input targets! We don't have a
778 // good way to do this today except by adding a nested loop.
779 // TODO(b/282025641): simplify this code once InputTargets are being identified
780 // separately from TouchedWindows.
781 std::erase_if(targets, [&](const InputTarget& target) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800782 return target.connection->getToken() == window.windowHandle->getToken();
Harry Cuttsb166c002023-05-09 13:06:05 +0000783 });
784 return true;
785 }
786 return false;
787 });
788}
789
Siarhei Vishniakouce1fd472023-09-18 18:38:07 -0700790/**
791 * In general, touch should be always split between windows. Some exceptions:
792 * 1. Don't split touch if all of the below is true:
793 * (a) we have an active pointer down *and*
794 * (b) a new pointer is going down that's from the same device *and*
795 * (c) the window that's receiving the current pointer does not support split touch.
796 * 2. Don't split mouse events
797 */
798bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) {
799 if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) {
800 // We should never split mouse events
801 return false;
802 }
803 for (const TouchedWindow& touchedWindow : touchState.windows) {
804 if (touchedWindow.windowHandle->getInfo()->isSpy()) {
805 // Spy windows should not affect whether or not touch is split.
806 continue;
807 }
808 if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) {
809 continue;
810 }
811 if (touchedWindow.windowHandle->getInfo()->inputConfig.test(
812 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
813 // Wallpaper window should not affect whether or not touch is split
814 continue;
815 }
816
817 if (touchedWindow.hasTouchingPointers(entry.deviceId)) {
818 return false;
819 }
820 }
821 return true;
822}
823
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700824/**
825 * Return true if stylus is currently down anywhere on the specified display, and false otherwise.
826 */
Linnan Li13bf76a2024-05-05 19:18:02 +0800827bool isStylusActiveInDisplay(ui::LogicalDisplayId displayId,
828 const std::unordered_map<ui::LogicalDisplayId /*displayId*/,
829 TouchState>& touchStatesByDisplay) {
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700830 const auto it = touchStatesByDisplay.find(displayId);
831 if (it == touchStatesByDisplay.end()) {
832 return false;
833 }
834 const TouchState& state = it->second;
835 return state.hasActiveStylus();
836}
837
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800838Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) {
839 struct HashFunction {
840 size_t operator()(const WindowInfo& info) const { return info.id; }
841 };
842
843 std::unordered_set<WindowInfo, HashFunction> windowSet;
844 for (const WindowInfo& info : update.windowInfos) {
845 const auto [_, inserted] = windowSet.insert(info);
846 if (!inserted) {
847 return Error() << "Duplicate entry for " << info;
848 }
849 }
850 return {};
851}
852
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800853int32_t getUserActivityEventType(const EventEntry& eventEntry) {
854 switch (eventEntry.type) {
855 case EventEntry::Type::KEY: {
856 return USER_ACTIVITY_EVENT_BUTTON;
857 }
858 case EventEntry::Type::MOTION: {
859 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
860 if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) {
861 return USER_ACTIVITY_EVENT_TOUCH;
862 }
863 return USER_ACTIVITY_EVENT_OTHER;
864 }
865 default: {
866 LOG_ALWAYS_FATAL("%s events are not user activity",
867 ftl::enum_string(eventEntry.type).c_str());
868 }
869 }
870}
871
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +0000872std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode(
873 CancelationOptions::Mode mode) {
874 switch (mode) {
875 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
876 return {true, true};
877 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
878 return {true, false};
879 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
880 return {false, true};
881 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
882 return {false, true};
Siarhei Vishniakoub711e652024-08-12 12:00:28 -0700883 case CancelationOptions::Mode::CANCEL_HOVER_EVENTS:
884 return {true, false};
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +0000885 }
886}
887
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000888class ScopedSyntheticEventTracer {
889public:
890 ScopedSyntheticEventTracer(std::unique_ptr<trace::InputTracerInterface>& tracer)
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000891 : mTracer(tracer), mProcessingTimestamp(now()) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000892 if (mTracer) {
893 mEventTracker = mTracer->createTrackerForSyntheticEvent();
894 }
895 }
896
897 ~ScopedSyntheticEventTracer() {
898 if (mTracer) {
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000899 mTracer->eventProcessingComplete(*mEventTracker, mProcessingTimestamp);
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000900 }
901 }
902
903 const std::unique_ptr<trace::EventTrackerInterface>& getTracker() const {
904 return mEventTracker;
905 }
906
907private:
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000908 const std::unique_ptr<trace::InputTracerInterface>& mTracer;
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000909 std::unique_ptr<trace::EventTrackerInterface> mEventTracker;
Prabir Pradhan4fc32e02024-06-14 16:00:58 +0000910 const nsecs_t mProcessingTimestamp;
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000911};
912
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -0700913/**
914 * This is needed to help use "InputEventInjectionResult" with base::Result.
915 */
916template <typename T>
917struct EnumErrorWrapper {
918 T mVal;
919 EnumErrorWrapper(T&& e) : mVal(std::forward<T>(e)) {}
920 operator const T&() const { return mVal; }
921 T value() const { return mVal; }
922 std::string print() const { return ftl::enum_string(mVal); }
923};
924
925Error<EnumErrorWrapper<InputEventInjectionResult>> injectionError(InputEventInjectionResult&& e) {
926 LOG_ALWAYS_FATAL_IF(e == InputEventInjectionResult::SUCCEEDED);
927 return Error<EnumErrorWrapper<InputEventInjectionResult>>(
928 std::forward<InputEventInjectionResult>(e));
929}
930
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000931} // namespace
932
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933// --- InputDispatcher ---
934
Prabir Pradhana41d2442023-04-20 21:30:40 +0000935InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
Prabir Pradhanf4099452024-04-12 18:34:14 +0000936 : InputDispatcher(policy, createInputTracingBackendIfEnabled()) {}
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000937
938InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
939 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
Garfield Tan00f511d2019-06-12 16:55:40 -0700940 : mPolicy(policy),
941 mPendingEvent(nullptr),
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700942 mLastDropReason(DropReason::NOT_DROPPED),
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800943 mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800944 mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),
Garfield Tan00f511d2019-06-12 16:55:40 -0700945 mNextUnblockedEvent(nullptr),
Prabir Pradhan1376fcd2022-01-21 09:56:35 -0800946 mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),
Garfield Tan00f511d2019-06-12 16:55:40 -0700947 mDispatchEnabled(false),
948 mDispatchFrozen(false),
949 mInputFilterEnabled(false),
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100950 mMaximumObscuringOpacityForTouch(1.0f),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700951 mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
Prabir Pradhan99987712020-11-10 18:43:05 -0800952 mWindowTokenWithPointerCapture(nullptr),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700953 mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
jioana24878b52024-09-10 10:13:27 +0000954 mInputEventTimelineProcessor(
955 input_flags::enable_per_device_input_latency_metrics()
956 ? std::move(std::unique_ptr<InputEventTimelineProcessor>(
957 new LatencyAggregatorWithHistograms()))
958 : std::move(std::unique_ptr<InputEventTimelineProcessor>(
959 new LatencyAggregator()))),
960 mLatencyTracker(*mInputEventTimelineProcessor) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700961 mLooper = sp<Looper>::make(false);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800962 mReporter = createInputReporter();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700964 mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700965#if defined(__ANDROID__)
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700966 SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700967#endif
Yi Kong9b14ac62018-07-17 13:48:38 -0700968 mKeyRepeatState.lastKeyEntry = nullptr;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000969
970 if (traceBackend) {
Prabir Pradhandae52792023-12-15 07:36:40 +0000971 mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000972 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800973
974 mLastUserActivityTimes.fill(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975}
976
977InputDispatcher::~InputDispatcher() {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000978 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Prabir Pradhancef936d2021-07-21 16:17:52 +0000980 resetKeyRepeatLocked();
981 releasePendingEventLocked();
982 drainInboundQueueLocked();
983 mCommandQueue.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +0000985 while (!mConnectionsByToken.empty()) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700986 std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800987 removeInputChannelLocked(connection->getToken(), /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 }
989}
990
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700991status_t InputDispatcher::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700992 if (mThread) {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700993 return ALREADY_EXISTS;
994 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700995 mThread = std::make_unique<InputThread>(
996 "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
997 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700998}
999
1000status_t InputDispatcher::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -07001001 if (mThread && mThread->isCallingThread()) {
1002 ALOGE("InputDispatcher cannot be stopped from its own thread!");
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001003 return INVALID_OPERATION;
1004 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -07001005 mThread.reset();
1006 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -07001007}
1008
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009void InputDispatcher::dispatchOnce() {
Colin Cross5b799302022-10-18 21:52:41 -07001010 nsecs_t nextWakeupTime = LLONG_MAX;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08001012 std::scoped_lock _l(mLock);
1013 mDispatcherIsAlive.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014
1015 // Run a dispatch loop if there are no pending commands.
1016 // The dispatch loop might enqueue commands to run afterwards.
1017 if (!haveCommandsLocked()) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001018 dispatchOnceInnerLocked(/*byref*/ nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 }
1020
1021 // Run all pending commands if there are any.
1022 // If any commands were run then force the next poll to wake up immediately.
Prabir Pradhancef936d2021-07-21 16:17:52 +00001023 if (runCommandsLockedInterruptable()) {
Colin Cross5b799302022-10-18 21:52:41 -07001024 nextWakeupTime = LLONG_MIN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 }
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001026
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001027 // If we are still waiting for ack on some events,
1028 // we might have to wake up earlier to check if an app is anr'ing.
1029 const nsecs_t nextAnrCheck = processAnrsLocked();
1030 nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);
1031
jioana24878b52024-09-10 10:13:27 +00001032 if (input_flags::enable_per_device_input_latency_metrics()) {
1033 const nsecs_t nextStatisticsPush = processLatencyStatisticsLocked();
1034 nextWakeupTime = std::min(nextWakeupTime, nextStatisticsPush);
1035 }
1036
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001037 // We are about to enter an infinitely long sleep, because we have no commands or
1038 // pending or queued events
Colin Cross5b799302022-10-18 21:52:41 -07001039 if (nextWakeupTime == LLONG_MAX) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08001040 mDispatcherEnteredIdle.notify_all();
1041 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 } // release lock
1043
1044 // Wait for callback or timeout or wake. (make sure we round up, not down)
1045 nsecs_t currentTime = now();
1046 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
1047 mLooper->pollOnce(timeoutMillis);
1048}
1049
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001050/**
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001051 * Raise ANR if there is no focused window.
1052 * Before the ANR is raised, do a final state check:
1053 * 1. The currently focused application must be the same one we are waiting for.
1054 * 2. Ensure we still don't have a focused window.
1055 */
1056void InputDispatcher::processNoFocusedWindowAnrLocked() {
1057 // Check if the application that we are waiting for is still focused.
1058 std::shared_ptr<InputApplicationHandle> focusedApplication =
1059 getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId);
1060 if (focusedApplication == nullptr ||
1061 focusedApplication->getApplicationToken() !=
1062 mAwaitedFocusedApplication->getApplicationToken()) {
1063 // Unexpected because we should have reset the ANR timer when focused application changed
1064 ALOGE("Waited for a focused window, but focused application has already changed to %s",
1065 focusedApplication->getName().c_str());
1066 return; // The focused application has changed.
1067 }
1068
chaviw98318de2021-05-19 16:45:23 -05001069 const sp<WindowInfoHandle>& focusedWindowHandle =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001070 getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId);
1071 if (focusedWindowHandle != nullptr) {
1072 return; // We now have a focused window. No need for ANR.
1073 }
1074 onAnrLocked(mAwaitedFocusedApplication);
1075}
1076
1077/**
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001078 * Check if any of the connections' wait queues have events that are too old.
1079 * If we waited for events to be ack'ed for more than the window timeout, raise an ANR.
1080 * Return the time at which we should wake up next.
1081 */
1082nsecs_t InputDispatcher::processAnrsLocked() {
1083 const nsecs_t currentTime = now();
Colin Cross5b799302022-10-18 21:52:41 -07001084 nsecs_t nextAnrCheck = LLONG_MAX;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001085 // Check if we are waiting for a focused window to appear. Raise ANR if waited too long
1086 if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) {
1087 if (currentTime >= *mNoFocusedWindowTimeoutTime) {
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001088 processNoFocusedWindowAnrLocked();
Chris Yea209fde2020-07-22 13:54:51 -07001089 mAwaitedFocusedApplication.reset();
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05001090 mNoFocusedWindowTimeoutTime = std::nullopt;
Colin Cross5b799302022-10-18 21:52:41 -07001091 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001092 } else {
Siarhei Vishniakou38a6d272020-10-20 20:29:33 -05001093 // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001094 nextAnrCheck = *mNoFocusedWindowTimeoutTime;
1095 }
1096 }
1097
1098 // Check if any connection ANRs are due
1099 nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout());
1100 if (currentTime < nextAnrCheck) { // most likely scenario
1101 return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck
1102 }
1103
1104 // If we reached here, we have an unresponsive connection.
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001105 std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001106 if (connection == nullptr) {
1107 ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout());
1108 return nextAnrCheck;
1109 }
1110 connection->responsive = false;
1111 // Stop waking up for this unresponsive connection
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001112 mAnrTracker.eraseToken(connection->getToken());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00001113 onAnrLocked(connection);
Colin Cross5b799302022-10-18 21:52:41 -07001114 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001115}
1116
jioana24878b52024-09-10 10:13:27 +00001117/**
1118 * Check if enough time has passed since the last latency statistics push.
1119 * Return the time at which we should wake up next.
1120 */
1121nsecs_t InputDispatcher::processLatencyStatisticsLocked() {
1122 const nsecs_t currentTime = now();
1123 // Log the atom recording latency statistics if more than 6 hours passed from the last
1124 // push
1125 if (currentTime - mLastStatisticPushTime >= LATENCY_STATISTICS_PUSH_INTERVAL) {
1126 mInputEventTimelineProcessor->pushLatencyStatistics();
1127 mLastStatisticPushTime = currentTime;
1128 }
1129 return mLastStatisticPushTime + LATENCY_STATISTICS_PUSH_INTERVAL;
1130}
1131
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001132std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001133 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001134 if (connection->monitor) {
1135 return mMonitorDispatchingTimeout;
1136 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001137 const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001138 if (window != nullptr) {
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001139 return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001140 }
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001141 return DEFAULT_INPUT_DISPATCHING_TIMEOUT;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001142}
1143
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001144void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145 nsecs_t currentTime = now();
1146
Jeff Browndc5992e2014-04-11 01:27:26 -07001147 // Reset the key repeat timer whenever normal dispatch is suspended while the
1148 // device is in a non-interactive state. This is to ensure that we abort a key
1149 // repeat if the device is just coming out of sleep.
1150 if (!mDispatchEnabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 resetKeyRepeatLocked();
1152 }
1153
1154 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
1155 if (mDispatchFrozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01001156 if (DEBUG_FOCUS) {
1157 ALOGD("Dispatch frozen. Waiting some more.");
1158 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 return;
1160 }
1161
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 // Ready to start a new event.
1163 // If we don't already have a pending event, go grab one.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001164 if (!mPendingEvent) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001165 if (mInboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166 // Synthesize a key repeat if appropriate.
1167 if (mKeyRepeatState.lastKeyEntry) {
1168 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
1169 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
1170 } else {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001171 nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 }
1173 }
1174
1175 // Nothing to do if there is no pending event.
1176 if (!mPendingEvent) {
1177 return;
1178 }
1179 } else {
1180 // Inbound queue has at least one entry.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001181 mPendingEvent = mInboundQueue.front();
1182 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 traceInboundQueueLengthLocked();
1184 }
1185
1186 // Poke user activity for this event.
1187 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001188 pokeUserActivityLocked(*mPendingEvent);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 }
1191
1192 // Now we have an event to dispatch.
1193 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Yi Kong9b14ac62018-07-17 13:48:38 -07001194 ALOG_ASSERT(mPendingEvent != nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 bool done = false;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001196 DropReason dropReason = DropReason::NOT_DROPPED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001198 dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199 } else if (!mDispatchEnabled) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001200 dropReason = DropReason::DISABLED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 }
1202
1203 if (mNextUnblockedEvent == mPendingEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001204 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
1207 switch (mPendingEvent->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001208 case EventEntry::Type::DEVICE_RESET: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001209 const DeviceResetEntry& typedEntry =
1210 static_cast<const DeviceResetEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001211 done = dispatchDeviceResetLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001212 dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001213 break;
1214 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001216 case EventEntry::Type::FOCUS: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001217 std::shared_ptr<const FocusEntry> typedEntry =
1218 std::static_pointer_cast<const FocusEntry>(mPendingEvent);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001219 dispatchFocusLocked(currentTime, typedEntry);
1220 done = true;
1221 dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
1222 break;
1223 }
1224
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001225 case EventEntry::Type::TOUCH_MODE_CHANGED: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001226 const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001227 dispatchTouchModeChangeLocked(currentTime, typedEntry);
1228 done = true;
1229 dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped
1230 break;
1231 }
1232
Prabir Pradhan99987712020-11-10 18:43:05 -08001233 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
1234 const auto typedEntry =
Prabir Pradhan24047542023-11-02 17:14:59 +00001235 std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent);
Prabir Pradhan99987712020-11-10 18:43:05 -08001236 dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason);
1237 done = true;
1238 break;
1239 }
1240
arthurhungb89ccb02020-12-30 16:19:01 +08001241 case EventEntry::Type::DRAG: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001242 std::shared_ptr<const DragEntry> typedEntry =
1243 std::static_pointer_cast<const DragEntry>(mPendingEvent);
arthurhungb89ccb02020-12-30 16:19:01 +08001244 dispatchDragLocked(currentTime, typedEntry);
1245 done = true;
1246 break;
1247 }
1248
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001249 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001250 std::shared_ptr<const KeyEntry> keyEntry =
1251 std::static_pointer_cast<const KeyEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001252 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001253 dropReason = DropReason::STALE;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001254 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001255 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
1256 dropReason = DropReason::BLOCKED;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001257 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001258 done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001259 break;
1260 }
1261
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001262 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001263 std::shared_ptr<const MotionEntry> motionEntry =
1264 std::static_pointer_cast<const MotionEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001265 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07001266 // The event is stale. However, only drop stale events if there isn't an ongoing
1267 // gesture. That would allow us to complete the processing of the current stroke.
1268 const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId);
1269 if (touchStateIt != mTouchStatesByDisplay.end()) {
1270 const TouchState& touchState = touchStateIt->second;
1271 if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
1272 !touchState.hasHoveringPointers(motionEntry->deviceId)) {
1273 dropReason = DropReason::STALE;
1274 }
1275 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001276 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001277 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001278 if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) {
1279 // Only drop events that are focus-dispatched.
1280 dropReason = DropReason::BLOCKED;
1281 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001282 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001283 done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001284 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 }
Chris Yef59a2f42020-10-16 12:55:26 -07001286
1287 case EventEntry::Type::SENSOR: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001288 std::shared_ptr<const SensorEntry> sensorEntry =
1289 std::static_pointer_cast<const SensorEntry>(mPendingEvent);
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001290
Chris Yef59a2f42020-10-16 12:55:26 -07001291 // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use
1292 // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead.
1293 nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME);
1294 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) {
1295 dropReason = DropReason::STALE;
1296 }
1297 dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime);
1298 done = true;
1299 break;
1300 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 }
1302
1303 if (done) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001304 if (dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001305 dropInboundEventLocked(*mPendingEvent, dropReason);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 }
Michael Wright3a981722015-06-10 15:26:13 +01001307 mLastDropReason = dropReason;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308
Prabir Pradhan052fb0b2024-02-23 21:03:12 +00001309 if (mTracer) {
1310 if (auto& traceTracker = getTraceTracker(*mPendingEvent); traceTracker != nullptr) {
Prabir Pradhan4fc32e02024-06-14 16:00:58 +00001311 mTracer->eventProcessingComplete(*traceTracker, currentTime);
Prabir Pradhan052fb0b2024-02-23 21:03:12 +00001312 }
1313 }
1314
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 releasePendingEventLocked();
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001316 nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317 }
1318}
1319
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001320bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) {
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07001321 return mPolicy.isStaleEvent(currentTime, entry.eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001322}
1323
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001324/**
1325 * Return true if the events preceding this incoming motion event should be dropped
1326 * Return false otherwise (the default behaviour)
1327 */
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001328bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001329 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
Prabir Pradhanaa561d12021-09-24 06:57:33 -07001330 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001331
1332 // Optimize case where the current application is unresponsive and the user
1333 // decides to touch a window in a different application.
1334 // If the application takes too long to catch up then we drop all events preceding
1335 // the touch into the other window.
1336 if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001337 const ui::LogicalDisplayId displayId = motionEntry.displayId;
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001338 const auto [x, y] = resolveTouchedPosition(motionEntry);
Harry Cutts33476232023-01-30 19:57:29 +00001339 const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0);
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001340
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001341 sp<WindowInfoHandle> touchedWindowHandle =
1342 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001343 if (touchedWindowHandle != nullptr &&
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001344 touchedWindowHandle->getApplicationToken() !=
1345 mAwaitedFocusedApplication->getApplicationToken()) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001346 // User touched a different application than the one we are waiting on.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001347 ALOGI("Pruning input queue because user touched a different application while waiting "
1348 "for %s",
1349 mAwaitedFocusedApplication->getName().c_str());
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001350 return true;
1351 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001352
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001353 // Alternatively, maybe there's a spy window that could handle this event.
1354 const std::vector<sp<WindowInfoHandle>> touchedSpies =
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001355 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus, motionEntry.deviceId);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001356 for (const auto& windowHandle : touchedSpies) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001357 const std::shared_ptr<Connection> connection =
1358 getConnectionLocked(windowHandle->getToken());
Siarhei Vishniakou34ed4d42020-06-18 00:43:02 +00001359 if (connection != nullptr && connection->responsive) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001360 // This spy window could take more input. Drop all events preceding this
1361 // event, so that the spy window can get a chance to receive the stream.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001362 ALOGW("Pruning the input queue because %s is unresponsive, but we have a "
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001363 "responsive spy window that may handle the event.",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001364 mAwaitedFocusedApplication->getName().c_str());
1365 return true;
1366 }
1367 }
1368 }
1369
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001370 return false;
1371}
1372
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001373bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001374 bool needWake = mInboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001375 mInboundQueue.push_back(std::move(newEntry));
Prabir Pradhan24047542023-11-02 17:14:59 +00001376 const EventEntry& entry = *(mInboundQueue.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001377 traceInboundQueueLengthLocked();
1378
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001379 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001380 case EventEntry::Type::KEY: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001381 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1382 "Unexpected untrusted event.");
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001383
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001384 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001385 if (mTracer) {
1386 ensureEventTraced(keyEntry);
1387 }
Siarhei Vishniakou6520a582023-10-27 21:53:45 -07001388
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001389 // If a new up event comes in, and the pending event with same key code has been asked
1390 // to try again later because of the policy. We have to reset the intercept key wake up
1391 // time for it may have been handled in the policy and could be dropped.
1392 if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent &&
1393 mPendingEvent->type == EventEntry::Type::KEY) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001394 const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001395 if (pendingKey.keyCode == keyEntry.keyCode &&
1396 pendingKey.interceptKeyResult ==
Michael Wright5caf55a2022-11-24 22:31:42 +00001397 KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
1398 pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001399 pendingKey.interceptKeyWakeupTime = 0;
1400 needWake = true;
1401 }
1402 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001403 break;
1404 }
1405
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001406 case EventEntry::Type::MOTION: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001407 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1408 "Unexpected untrusted event.");
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001409 const auto& motionEntry = static_cast<const MotionEntry&>(entry);
1410 if (mTracer) {
1411 ensureEventTraced(motionEntry);
1412 }
1413 if (shouldPruneInboundQueueLocked(motionEntry)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001414 mNextUnblockedEvent = mInboundQueue.back();
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001415 needWake = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001417
1418 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
1419 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
1420 if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) {
1421 // Prevent waiting too long for unprocessed events: if we have a pending key event,
1422 // and some other events have not yet been processed, the dispatcher will wait for
1423 // these events to be processed before dispatching the key event. This is because
1424 // the unprocessed events may cause the focus to change (for example, by launching a
1425 // new window or tapping a different window). To prevent waiting too long, we force
1426 // the key to be sent to the currently focused window when a new tap comes in.
1427 ALOGD("Received a new pointer down event, stop waiting for events to process and "
1428 "just send the pending key event to the currently focused window.");
1429 mKeyIsWaitingForEventsTimeout = now();
Linnan Li563916a2024-02-23 16:08:33 +00001430 needWake = true;
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001431 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001432 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001433 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001434 case EventEntry::Type::FOCUS: {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001435 LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked");
1436 break;
1437 }
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001438 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08001439 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07001440 case EventEntry::Type::SENSOR:
arthurhungb89ccb02020-12-30 16:19:01 +08001441 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1442 case EventEntry::Type::DRAG: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001443 // nothing to do
1444 break;
1445 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001446 }
1447
1448 return needWake;
1449}
1450
Prabir Pradhan24047542023-11-02 17:14:59 +00001451void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) {
Chris Yef59a2f42020-10-16 12:55:26 -07001452 // Do not store sensor event in recent queue to avoid flooding the queue.
1453 if (entry->type != EventEntry::Type::SENSOR) {
1454 mRecentQueue.push_back(entry);
1455 }
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001456 if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001457 mRecentQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001458 }
1459}
1460
Linnan Li13bf76a2024-05-05 19:18:02 +08001461sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(ui::LogicalDisplayId displayId,
1462 float x, float y, bool isStylus,
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001463 bool ignoreDragWindow) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001464 // Traverse windows from front to back to find touched window.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001465 const auto& windowHandles = getWindowHandlesLocked(displayId);
chaviw98318de2021-05-19 16:45:23 -05001466 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
arthurhung6d4bed92021-03-17 11:59:33 +08001467 if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) {
arthurhungb89ccb02020-12-30 16:19:01 +08001468 continue;
1469 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001471 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001472 if (!info.isSpy() &&
1473 windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001474 return windowHandle;
1475 }
1476 }
1477 return nullptr;
1478}
1479
1480std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08001481 ui::LogicalDisplayId displayId, const sp<WindowInfoHandle>& touchedWindow,
1482 int32_t pointerId) const {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001483 if (touchedWindow == nullptr) {
1484 return {};
1485 }
1486 // Traverse windows from front to back until we encounter the touched window.
1487 std::vector<InputTarget> outsideTargets;
1488 const auto& windowHandles = getWindowHandlesLocked(displayId);
1489 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1490 if (windowHandle == touchedWindow) {
1491 // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window
1492 // below the touched window will not get ACTION_OUTSIDE event.
1493 return outsideTargets;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001494 }
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001495
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001496 const WindowInfo& info = *windowHandle->getInfo();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08001497 if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) {
Siarhei Vishniakou70f3d8c2023-09-19 15:36:52 -07001498 std::bitset<MAX_POINTER_ID + 1> pointerIds;
1499 pointerIds.set(pointerId);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001500 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE,
1501 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001502 /*firstDownTimeInTarget=*/std::nullopt, outsideTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 }
1504 }
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001505 return outsideTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506}
1507
Prabir Pradhand65552b2021-10-07 11:23:50 -07001508std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked(
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001509 ui::LogicalDisplayId displayId, float x, float y, bool isStylus, DeviceId deviceId) const {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001510 // Traverse windows from front to back and gather the touched spy windows.
1511 std::vector<sp<WindowInfoHandle>> spyWindows;
1512 const auto& windowHandles = getWindowHandlesLocked(displayId);
1513 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1514 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001515 if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07001516 // Generally, we would skip any pointer that's outside of the window. However, if the
1517 // spy prevents splitting, and already has some of the pointers from this device, then
1518 // it should get more pointers from the same device, even if they are outside of that
1519 // window
1520 if (info.supportsSplitTouch()) {
1521 continue;
1522 }
1523
1524 // We know that split touch is not supported. Skip this window only if it doesn't have
1525 // any touching pointers for this device already.
1526 if (!windowHasTouchingPointersLocked(windowHandle, deviceId)) {
1527 continue;
1528 }
1529 // If it already has pointers down for this device, then give it this pointer, too.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001530 }
1531 if (!info.isSpy()) {
1532 // The first touched non-spy window was found, so return the spy windows touched so far.
1533 return spyWindows;
1534 }
1535 spyWindows.push_back(windowHandle);
1536 }
1537 return spyWindows;
1538}
1539
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001540void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541 const char* reason;
1542 switch (dropReason) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001543 case DropReason::POLICY:
Prabir Pradhan65613802023-02-22 23:36:58 +00001544 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001545 ALOGD("Dropped event because policy consumed it.");
1546 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001547 reason = "inbound event was dropped because the policy consumed it";
1548 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001549 case DropReason::DISABLED:
1550 if (mLastDropReason != DropReason::DISABLED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001551 ALOGI("Dropped event because input dispatch is disabled.");
1552 }
1553 reason = "inbound event was dropped because input dispatch is disabled";
1554 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001555 case DropReason::BLOCKED:
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001556 LOG(INFO) << "Dropping because the current application is not responding and the user "
1557 "has started interacting with a different application: "
1558 << entry.getDescription();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001559 reason = "inbound event was dropped because the current application is not responding "
1560 "and the user has started interacting with a different application";
1561 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001562 case DropReason::STALE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001563 ALOGI("Dropped event because it is stale.");
1564 reason = "inbound event was dropped because it is stale";
1565 break;
Prabir Pradhan99987712020-11-10 18:43:05 -08001566 case DropReason::NO_POINTER_CAPTURE:
1567 ALOGI("Dropped event because there is no window with Pointer Capture.");
1568 reason = "inbound event was dropped because there is no window with Pointer Capture";
1569 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001570 case DropReason::NOT_DROPPED: {
1571 LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event");
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001572 return;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001573 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001576 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001577 case EventEntry::Type::KEY: {
Hu Guo3cfa7382023-11-15 09:50:04 +00001578 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001579 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason,
1580 keyEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001581 options.displayId = keyEntry.displayId;
1582 options.deviceId = keyEntry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 synthesizeCancelationEventsForAllConnectionsLocked(options);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001584 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001586 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001587 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
1588 if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001589 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason,
1590 motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001591 options.displayId = motionEntry.displayId;
1592 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001593 synthesizeCancelationEventsForAllConnectionsLocked(options);
1594 } else {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001595 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001596 reason, motionEntry.traceTracker);
Hu Guo3cfa7382023-11-15 09:50:04 +00001597 options.displayId = motionEntry.displayId;
1598 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001599 synthesizeCancelationEventsForAllConnectionsLocked(options);
1600 }
1601 break;
1602 }
Chris Yef59a2f42020-10-16 12:55:26 -07001603 case EventEntry::Type::SENSOR: {
1604 break;
1605 }
arthurhungb89ccb02020-12-30 16:19:01 +08001606 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1607 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001608 break;
1609 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001610 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001611 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001612 case EventEntry::Type::DEVICE_RESET: {
Dominik Laskowski75788452021-02-09 18:51:25 -08001613 LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001614 break;
1615 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 }
1617}
1618
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619bool InputDispatcher::haveCommandsLocked() const {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001620 return !mCommandQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621}
1622
Prabir Pradhancef936d2021-07-21 16:17:52 +00001623bool InputDispatcher::runCommandsLockedInterruptable() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001624 if (mCommandQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 return false;
1626 }
1627
1628 do {
Prabir Pradhancef936d2021-07-21 16:17:52 +00001629 auto command = std::move(mCommandQueue.front());
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001630 mCommandQueue.pop_front();
Prabir Pradhancef936d2021-07-21 16:17:52 +00001631 // Commands are run with the lock held, but may release and re-acquire the lock from within.
1632 command();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001633 } while (!mCommandQueue.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634 return true;
1635}
1636
Prabir Pradhancef936d2021-07-21 16:17:52 +00001637void InputDispatcher::postCommandLocked(Command&& command) {
1638 mCommandQueue.push_back(command);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639}
1640
1641void InputDispatcher::drainInboundQueueLocked() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001642 while (!mInboundQueue.empty()) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001643 std::shared_ptr<const EventEntry> entry = mInboundQueue.front();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001644 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 releaseInboundEventLocked(entry);
1646 }
1647 traceInboundQueueLengthLocked();
1648}
1649
1650void InputDispatcher::releasePendingEventLocked() {
1651 if (mPendingEvent) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652 releaseInboundEventLocked(mPendingEvent);
Yi Kong9b14ac62018-07-17 13:48:38 -07001653 mPendingEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 }
1655}
1656
Prabir Pradhan24047542023-11-02 17:14:59 +00001657void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001658 const std::shared_ptr<InjectionState>& injectionState = entry->injectionState;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001659 if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001660 if (DEBUG_DISPATCH_CYCLE) {
1661 ALOGD("Injected inbound event was dropped.");
1662 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001663 setInjectionResult(*entry, InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 }
1665 if (entry == mNextUnblockedEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001666 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001667 }
1668 addRecentEventLocked(entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001669}
1670
1671void InputDispatcher::resetKeyRepeatLocked() {
1672 if (mKeyRepeatState.lastKeyEntry) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001673 mKeyRepeatState.lastKeyEntry = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674 }
1675}
1676
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001677std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001678 std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679
Michael Wright2e732952014-09-24 13:26:59 -07001680 uint32_t policyFlags = entry->policyFlags &
1681 (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001683 std::shared_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001684 std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
1685 currentTime, entry->deviceId, entry->source,
1686 entry->displayId, policyFlags, entry->action, entry->flags,
1687 entry->keyCode, entry->scanCode, entry->metaState,
1688 entry->repeatCount + 1, entry->downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001690 newEntry->syntheticRepeat = true;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001691 if (mTracer) {
1692 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
1693 }
1694
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001695 mKeyRepeatState.lastKeyEntry = newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001696 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001697 return newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698}
1699
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001700bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
1701 const DeviceResetEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001702 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1703 ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime,
1704 entry.deviceId);
1705 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706
liushenxiang42232912021-05-21 20:24:09 +08001707 // Reset key repeating in case a keyboard device was disabled or enabled.
1708 if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) {
1709 resetKeyRepeatLocked();
1710 }
1711
Prabir Pradhand6b2b052024-02-21 23:25:15 +00001712 ScopedSyntheticEventTracer traceContext(mTracer);
1713 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset",
1714 traceContext.getTracker());
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001715 options.deviceId = entry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001716 synthesizeCancelationEventsForAllConnectionsLocked(options);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07001717
1718 // Remove all active pointers from this device
1719 for (auto& [_, touchState] : mTouchStatesByDisplay) {
1720 touchState.removeAllPointersForDevice(entry.deviceId);
1721 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001722 return true;
1723}
1724
Vishnu Nairad321cd2020-08-20 16:40:21 -07001725void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -08001726 const std::string& reason) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001727 if (mPendingEvent != nullptr) {
1728 // Move the pending event to the front of the queue. This will give the chance
1729 // for the pending event to get dispatched to the newly focused window
1730 mInboundQueue.push_front(mPendingEvent);
1731 mPendingEvent = nullptr;
1732 }
1733
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001734 std::unique_ptr<FocusEntry> focusEntry =
1735 std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus,
1736 reason);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001737
1738 // This event should go to the front of the queue, but behind all other focus events
1739 // Find the last focus event, and insert right after it
Prabir Pradhan24047542023-11-02 17:14:59 +00001740 auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(),
1741 [](const std::shared_ptr<const EventEntry>& event) {
1742 return event->type == EventEntry::Type::FOCUS;
1743 });
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001744
1745 // Maintain the order of focus events. Insert the entry after all other focus events.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001746 mInboundQueue.insert(it.base(), std::move(focusEntry));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001747}
1748
Prabir Pradhan24047542023-11-02 17:14:59 +00001749void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime,
1750 std::shared_ptr<const FocusEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001751 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
1752 if (connection == nullptr) {
1753 return; // Connection has gone away
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001754 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001755 entry->dispatchInProgress = true;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00001756 std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") +
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001757 connection->getInputChannelName();
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07001758 std::string reason = std::string("reason=").append(entry->reason);
1759 android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001760 dispatchEventLocked(currentTime, entry, {{connection}});
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001761}
1762
Prabir Pradhan99987712020-11-10 18:43:05 -08001763void InputDispatcher::dispatchPointerCaptureChangedLocked(
Prabir Pradhan24047542023-11-02 17:14:59 +00001764 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry,
Prabir Pradhan99987712020-11-10 18:43:05 -08001765 DropReason& dropReason) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001766 dropReason = DropReason::NOT_DROPPED;
1767
Prabir Pradhan99987712020-11-10 18:43:05 -08001768 const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr;
Prabir Pradhan99987712020-11-10 18:43:05 -08001769 sp<IBinder> token;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001770
Hiroki Sato25040232024-02-22 17:21:22 +09001771 if (entry->pointerCaptureRequest.isEnable()) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001772 // Enable Pointer Capture.
1773 if (haveWindowWithPointerCapture &&
1774 (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) {
Prabir Pradhan7092e262022-05-03 16:51:09 +00001775 // This can happen if pointer capture is disabled and re-enabled before we notify the
1776 // app of the state change, so there is no need to notify the app.
1777 ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change.");
1778 return;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001779 }
Hiroki Sato25040232024-02-22 17:21:22 +09001780 if (!mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001781 // This can happen if a window requests capture and immediately releases capture.
1782 ALOGW("No window requested Pointer Capture.");
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001783 dropReason = DropReason::NO_POINTER_CAPTURE;
Prabir Pradhan99987712020-11-10 18:43:05 -08001784 return;
1785 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001786 if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) {
1787 ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch.");
1788 return;
1789 }
1790
Vishnu Nairc519ff72021-01-21 08:23:08 -08001791 token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08001792 LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture.");
Hiroki Sato25040232024-02-22 17:21:22 +09001793 LOG_ALWAYS_FATAL_IF(token != entry->pointerCaptureRequest.window,
1794 "Unexpected requested window for Pointer Capture.");
Prabir Pradhan99987712020-11-10 18:43:05 -08001795 mWindowTokenWithPointerCapture = token;
1796 } else {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001797 // Disable Pointer Capture.
1798 // We do not check if the sequence number matches for requests to disable Pointer Capture
1799 // for two reasons:
1800 // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries
1801 // to disable capture with the same sequence number: one generated by
1802 // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer
1803 // Capture being disabled in InputReader.
1804 // 2. We respect any request to disable Pointer Capture generated by InputReader, since the
1805 // actual Pointer Capture state that affects events being generated by input devices is
1806 // in InputReader.
1807 if (!haveWindowWithPointerCapture) {
1808 // Pointer capture was already forcefully disabled because of focus change.
1809 dropReason = DropReason::NOT_DROPPED;
1810 return;
1811 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001812 token = mWindowTokenWithPointerCapture;
1813 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001814 if (mCurrentPointerCaptureRequest.isEnable()) {
1815 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001816 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001817 }
1818
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001819 auto connection = getConnectionLocked(token);
1820 if (connection == nullptr) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001821 // Window has gone away, clean up Pointer Capture state.
1822 mWindowTokenWithPointerCapture = nullptr;
Hiroki Sato25040232024-02-22 17:21:22 +09001823 if (mCurrentPointerCaptureRequest.isEnable()) {
1824 setPointerCaptureLocked(nullptr);
Prabir Pradhan7d030382020-12-21 07:58:35 -08001825 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001826 return;
1827 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001828 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001829 dispatchEventLocked(currentTime, entry, {{connection}});
Prabir Pradhan99987712020-11-10 18:43:05 -08001830
1831 dropReason = DropReason::NOT_DROPPED;
1832}
1833
Prabir Pradhan24047542023-11-02 17:14:59 +00001834void InputDispatcher::dispatchTouchModeChangeLocked(
1835 nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001836 const std::vector<sp<WindowInfoHandle>>& windowHandles =
Antonio Kantek15beb512022-06-13 22:35:41 +00001837 getWindowHandlesLocked(entry->displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001838 if (windowHandles.empty()) {
1839 return;
1840 }
1841 const std::vector<InputTarget> inputTargets =
1842 getInputTargetsFromWindowHandlesLocked(windowHandles);
1843 if (inputTargets.empty()) {
1844 return;
1845 }
1846 entry->dispatchInProgress = true;
1847 dispatchEventLocked(currentTime, entry, inputTargets);
1848}
1849
1850std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked(
1851 const std::vector<sp<WindowInfoHandle>>& windowHandles) const {
1852 std::vector<InputTarget> inputTargets;
1853 for (const sp<WindowInfoHandle>& handle : windowHandles) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001854 const sp<IBinder>& token = handle->getToken();
1855 if (token == nullptr) {
1856 continue;
1857 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001858 std::shared_ptr<Connection> connection = getConnectionLocked(token);
1859 if (connection == nullptr) {
1860 continue; // Connection has gone away
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001861 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00001862 inputTargets.emplace_back(connection);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001863 }
1864 return inputTargets;
1865}
1866
Prabir Pradhan24047542023-11-02 17:14:59 +00001867bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001868 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001870 if (!entry->dispatchInProgress) {
Hu Guofe3c8f12023-09-22 17:20:15 +08001871 if (!entry->syntheticRepeat && entry->action == AKEY_EVENT_ACTION_DOWN &&
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001872 (entry->policyFlags & POLICY_FLAG_TRUSTED) &&
1873 (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
1874 if (mKeyRepeatState.lastKeyEntry &&
Chris Ye2ad95392020-09-01 13:44:44 -07001875 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode &&
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 // We have seen two identical key downs in a row which indicates that the device
1877 // driver is automatically generating key repeats itself. We take note of the
1878 // repeat here, but we disable our own next key repeat timer since it is clear that
1879 // we will not need to synthesize key repeats ourselves.
Chris Ye2ad95392020-09-01 13:44:44 -07001880 mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) {
1881 // Make sure we don't get key down from a different device. If a different
1882 // device Id has same key pressed down, the new device Id will replace the
1883 // current one to hold the key repeat with repeat count reset.
1884 // In the future when got a KEY_UP on the device id, drop it and do not
1885 // stop the key repeat on current device.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
1887 resetKeyRepeatLocked();
Colin Cross5b799302022-10-18 21:52:41 -07001888 mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 } else {
1890 // Not a repeat. Save key down state in case we do see a repeat later.
1891 resetKeyRepeatLocked();
1892 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
1893 }
1894 mKeyRepeatState.lastKeyEntry = entry;
Chris Ye2ad95392020-09-01 13:44:44 -07001895 } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry &&
1896 mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001897 // The key on device 'deviceId' is still down, do not stop key repeat
Prabir Pradhan65613802023-02-22 23:36:58 +00001898 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001899 ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId);
1900 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001901 } else if (!entry->syntheticRepeat) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 resetKeyRepeatLocked();
1903 }
1904
1905 if (entry->repeatCount == 1) {
1906 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
1907 } else {
1908 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
1909 }
1910
1911 entry->dispatchInProgress = true;
1912
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001913 logOutboundKeyDetails("dispatchKey - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 }
1915
1916 // Handle case where the policy asked us to try again later last time.
Michael Wright5caf55a2022-11-24 22:31:42 +00001917 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 if (currentTime < entry->interceptKeyWakeupTime) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001919 nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 return false; // wait until next wakeup
1921 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001922 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 entry->interceptKeyWakeupTime = 0;
1924 }
1925
1926 // Give the policy a chance to intercept the key.
Michael Wright5caf55a2022-11-24 22:31:42 +00001927 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001928 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07001929 sp<IBinder> focusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08001930 mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00001931
1932 auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) {
1933 doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
1934 };
1935 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001936 return false; // wait for the command to run
1937 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00001938 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001939 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001940 } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001941 if (*dropReason == DropReason::NOT_DROPPED) {
1942 *dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 }
1944 }
1945
1946 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001947 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001948 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001949 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
1950 : InputEventInjectionResult::FAILED);
Garfield Tan6a5a14e2020-01-28 13:24:04 -08001951 mReporter->reportDroppedKey(entry->id);
Josep del Rioc8fdedb2024-05-21 13:32:43 +00001952 // Poke user activity for consumed keys, as it may have not been reported due to
1953 // the focused window requesting user activity to be disabled
1954 if (*dropReason == DropReason::POLICY &&
1955 mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
1956 pokeUserActivityLocked(*entry);
1957 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001958 return true;
1959 }
1960
1961 // Identify targets.
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001962 Result<sp<WindowInfoHandle>, InputEventInjectionResult> result =
1963 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001965 if (!result.ok()) {
1966 if (result.error().code() == InputEventInjectionResult::PENDING) {
1967 return false;
1968 }
1969 setInjectionResult(*entry, result.error().code());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001970 return true;
1971 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001972 sp<WindowInfoHandle>& focusedWindow = *result;
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001973 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
1974
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07001975 setInjectionResult(*entry, InputEventInjectionResult::SUCCEEDED);
1976
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001977 std::vector<InputTarget> inputTargets;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001978 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
1979 InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001980
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001981 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001982 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001983
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001984 if (mTracer) {
1985 ensureEventTraced(*entry);
1986 for (const auto& target : inputTargets) {
1987 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
1988 }
1989 }
1990
Michael Wrightd02c5b62014-02-10 15:10:22 -08001991 // Dispatch the key.
1992 dispatchEventLocked(currentTime, entry, inputTargets);
1993 return true;
1994}
1995
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001996void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001997 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08001998 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001999 "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, "
2000 "metaState=0x%x, repeatCount=%d, downTime=%" PRId64,
Linnan Li13bf76a2024-05-05 19:18:02 +08002001 prefix, entry.eventTime, entry.deviceId, entry.source,
2002 entry.displayId.toString().c_str(), entry.policyFlags, entry.action, entry.flags,
2003 entry.keyCode, entry.scanCode, entry.metaState, entry.repeatCount, entry.downTime);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002004 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002005}
2006
Prabir Pradhancef936d2021-07-21 16:17:52 +00002007void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00002008 const std::shared_ptr<const SensorEntry>& entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002009 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002010 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
2011 ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, "
2012 "source=0x%x, sensorType=%s",
2013 entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source,
Dominik Laskowski75788452021-02-09 18:51:25 -08002014 ftl::enum_string(entry->sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002015 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00002016 auto command = [this, entry]() REQUIRES(mLock) {
2017 scoped_unlock unlock(mLock);
2018
2019 if (entry->accuracyChanged) {
Prabir Pradhana41d2442023-04-20 21:30:40 +00002020 mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002021 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00002022 mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy,
2023 entry->hwTimestamp, entry->values);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002024 };
2025 postCommandLocked(std::move(command));
Chris Yef59a2f42020-10-16 12:55:26 -07002026}
2027
2028bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002029 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
2030 ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId,
Dominik Laskowski75788452021-02-09 18:51:25 -08002031 ftl::enum_string(sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002032 }
Chris Yef59a2f42020-10-16 12:55:26 -07002033 { // acquire lock
2034 std::scoped_lock _l(mLock);
2035
2036 for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) {
Prabir Pradhan24047542023-11-02 17:14:59 +00002037 std::shared_ptr<const EventEntry> entry = *it;
Chris Yef59a2f42020-10-16 12:55:26 -07002038 if (entry->type == EventEntry::Type::SENSOR) {
2039 it = mInboundQueue.erase(it);
2040 releaseInboundEventLocked(entry);
2041 }
2042 }
2043 }
2044 return true;
2045}
2046
Prabir Pradhan24047542023-11-02 17:14:59 +00002047bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime,
2048 std::shared_ptr<const MotionEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002049 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002050 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002051 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002052 if (!entry->dispatchInProgress) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002053 entry->dispatchInProgress = true;
2054
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002055 logOutboundMotionDetails("dispatchMotion - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002056 }
2057
2058 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07002059 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002060 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002061 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
2062 : InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002063 return true;
2064 }
2065
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002066 const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002067
2068 // Identify targets.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002069 std::vector<InputTarget> inputTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002070
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002071 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002072 if (isPointerEvent) {
2073 // Pointer event. (eg. touchscreen)
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00002074
2075 if (mDragState &&
2076 (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2077 // If drag and drop ongoing and pointer down occur: pilfer drag window pointers
2078 pilferPointersLocked(mDragState->dragWindow->getToken());
2079 }
2080
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002081 Result<std::vector<InputTarget>, InputEventInjectionResult> result =
2082 findTouchedWindowTargetsLocked(currentTime, *entry);
2083
2084 if (result.ok()) {
2085 inputTargets = std::move(*result);
2086 injectionResult = InputEventInjectionResult::SUCCEEDED;
2087 } else {
2088 injectionResult = result.error().code();
2089 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002090 } else {
2091 // Non touch event. (eg. trackball)
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002092 Result<sp<WindowInfoHandle>, InputEventInjectionResult> result =
2093 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime);
2094 if (result.ok()) {
2095 sp<WindowInfoHandle>& focusedWindow = *result;
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002096 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002097 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
2098 InputTarget::Flags::FOREGROUND, getDownTime(*entry),
2099 inputTargets);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002100 injectionResult = InputEventInjectionResult::SUCCEEDED;
2101 } else {
2102 injectionResult = result.error().code();
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002103 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002104 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002105 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002106 return false;
2107 }
2108
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002109 setInjectionResult(*entry, injectionResult);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002110 if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) {
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002111 return true;
2112 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08002113 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightfb04fd52022-11-24 22:31:11 +00002114 CancelationOptions::Mode mode(
2115 isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS
2116 : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002117 CancelationOptions options(mode, "input event injection failed", entry->traceTracker);
Linnan Lid8150952024-01-26 18:07:17 +00002118 options.displayId = entry->displayId;
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07002119 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002120 return true;
2121 }
2122
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002123 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002124 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002125
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00002126 if (mTracer) {
2127 ensureEventTraced(*entry);
2128 for (const auto& target : inputTargets) {
2129 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
2130 }
2131 }
2132
Michael Wrightd02c5b62014-02-10 15:10:22 -08002133 // Dispatch the motion.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002134 dispatchEventLocked(currentTime, entry, inputTargets);
2135 return true;
2136}
2137
chaviw98318de2021-05-19 16:45:23 -05002138void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle,
Arthur Hung54745652022-04-20 07:17:41 +00002139 bool isExiting, const int32_t rawX,
2140 const int32_t rawY) {
2141 const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY));
arthurhungb89ccb02020-12-30 16:19:01 +08002142 std::unique_ptr<DragEntry> dragEntry =
Arthur Hung54745652022-04-20 07:17:41 +00002143 std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(),
2144 isExiting, xy.x, xy.y);
arthurhungb89ccb02020-12-30 16:19:01 +08002145
2146 enqueueInboundEventLocked(std::move(dragEntry));
2147}
2148
Prabir Pradhan24047542023-11-02 17:14:59 +00002149void InputDispatcher::dispatchDragLocked(nsecs_t currentTime,
2150 std::shared_ptr<const DragEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002151 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
2152 if (connection == nullptr) {
2153 return; // Connection has gone away
arthurhungb89ccb02020-12-30 16:19:01 +08002154 }
arthurhungb89ccb02020-12-30 16:19:01 +08002155 entry->dispatchInProgress = true;
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002156 dispatchEventLocked(currentTime, entry, {{connection}});
arthurhungb89ccb02020-12-30 16:19:01 +08002157}
2158
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002159void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002160 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002161 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002162 "action=%s, actionButton=0x%x, flags=0x%x, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002163 "metaState=0x%x, buttonState=0x%x, downTime=%" PRId64,
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002164 prefix, entry.eventTime, entry.deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +08002165 inputEventSourceToString(entry.source).c_str(), entry.displayId.toString().c_str(),
2166 entry.policyFlags, MotionEvent::actionToString(entry.action).c_str(),
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002167 entry.actionButton, entry.flags, entry.metaState, entry.buttonState, entry.downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002168
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002169 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002170 ALOGD(" Pointer %d: id=%d, toolType=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002171 "x=%f, y=%f, pressure=%f, size=%f, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07002172 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002173 i, entry.pointerProperties[i].id,
2174 ftl::enum_string(entry.pointerProperties[i].toolType).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002175 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2176 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2177 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2178 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2179 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2180 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2181 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2182 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2183 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
2184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002185 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186}
2187
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002188void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00002189 std::shared_ptr<const EventEntry> eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002190 const std::vector<InputTarget>& inputTargets) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002191 ATRACE_CALL();
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002192 if (DEBUG_DISPATCH_CYCLE) {
2193 ALOGD("dispatchEventToCurrentInputTargets");
2194 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002196 processInteractionsLocked(*eventEntry, inputTargets);
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00002197
Michael Wrightd02c5b62014-02-10 15:10:22 -08002198 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
2199
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002200 pokeUserActivityLocked(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002201
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002202 for (const InputTarget& inputTarget : inputTargets) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002203 std::shared_ptr<Connection> connection = inputTarget.connection;
2204 prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002205 }
2206}
2207
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07002208void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002209 // We will not be breaking any connections here, even if the policy wants us to abort dispatch.
2210 // If the policy decides to close the app, we will get a channel removal event via
2211 // unregisterInputChannel, and will clean up the connection that way. We are already not
2212 // sending new pointers to the connection when it blocked, but focused events will continue to
2213 // pile up.
2214 ALOGW("Canceling events for %s because it is unresponsive",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002215 connection->getInputChannelName().c_str());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002216 if (connection->status != Connection::Status::NORMAL) {
2217 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002219 ScopedSyntheticEventTracer traceContext(mTracer);
Prabir Pradhanfc364722024-02-08 17:51:20 +00002220 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00002221 "application not responding", traceContext.getTracker());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002222
2223 sp<WindowInfoHandle> windowHandle;
2224 if (!connection->monitor) {
2225 windowHandle = getWindowHandleLocked(connection->getToken());
2226 if (windowHandle == nullptr) {
2227 // The window that is receiving this ANR was removed, so there is no need to generate
2228 // cancellations, because the cancellations would have already been generated when
2229 // the window was removed.
2230 return;
2231 }
2232 }
2233 synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234}
2235
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002236void InputDispatcher::resetNoFocusedWindowTimeoutLocked() {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01002237 if (DEBUG_FOCUS) {
2238 ALOGD("Resetting ANR timeouts.");
2239 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002240
2241 // Reset input target wait timeout.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002242 mNoFocusedWindowTimeoutTime = std::nullopt;
Chris Yea209fde2020-07-22 13:54:51 -07002243 mAwaitedFocusedApplication.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002244}
2245
Tiger Huang721e26f2018-07-24 22:26:19 +08002246/**
2247 * Get the display id that the given event should go to. If this event specifies a valid display id,
2248 * then it should be dispatched to that display. Otherwise, the event goes to the focused display.
2249 * Focused display is the display that the user most recently interacted with.
2250 */
Linnan Li13bf76a2024-05-05 19:18:02 +08002251ui::LogicalDisplayId InputDispatcher::getTargetDisplayId(const EventEntry& entry) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002252 ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID};
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002253 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002254 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002255 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
2256 displayId = keyEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002257 break;
2258 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002259 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002260 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
2261 displayId = motionEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002262 break;
2263 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00002264 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08002265 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002266 case EventEntry::Type::FOCUS:
Chris Yef59a2f42020-10-16 12:55:26 -07002267 case EventEntry::Type::DEVICE_RESET:
arthurhungb89ccb02020-12-30 16:19:01 +08002268 case EventEntry::Type::SENSOR:
2269 case EventEntry::Type::DRAG: {
Dominik Laskowski75788452021-02-09 18:51:25 -08002270 ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002271 return ui::LogicalDisplayId::INVALID;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002272 }
Tiger Huang721e26f2018-07-24 22:26:19 +08002273 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002274 return displayId == ui::LogicalDisplayId::INVALID ? mFocusedDisplayId : displayId;
Tiger Huang721e26f2018-07-24 22:26:19 +08002275}
2276
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002277bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime,
2278 const char* focusedWindowName) {
2279 if (mAnrTracker.empty()) {
2280 // already processed all events that we waited for
2281 mKeyIsWaitingForEventsTimeout = std::nullopt;
2282 return false;
2283 }
2284
2285 if (!mKeyIsWaitingForEventsTimeout.has_value()) {
2286 // Start the timer
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00002287 // Wait to send key because there are unprocessed events that may cause focus to change
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002288 mKeyIsWaitingForEventsTimeout = currentTime +
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08002289 std::chrono::duration_cast<std::chrono::nanoseconds>(
2290 mPolicy.getKeyWaitingForEventsTimeout())
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002291 .count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002292 return true;
2293 }
2294
2295 // We still have pending events, and already started the timer
2296 if (currentTime < *mKeyIsWaitingForEventsTimeout) {
2297 return true; // Still waiting
2298 }
2299
2300 // Waited too long, and some connection still hasn't processed all motions
2301 // Just send the key to the focused window
2302 ALOGW("Dispatching key to %s even though there are other unprocessed events",
2303 focusedWindowName);
2304 mKeyIsWaitingForEventsTimeout = std::nullopt;
2305 return false;
2306}
2307
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002308Result<sp<WindowInfoHandle>, InputEventInjectionResult>
2309InputDispatcher::findFocusedWindowTargetLocked(nsecs_t currentTime, const EventEntry& entry,
2310 nsecs_t& nextWakeupTime) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002311 ui::LogicalDisplayId displayId = getTargetDisplayId(entry);
chaviw98318de2021-05-19 16:45:23 -05002312 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Chris Yea209fde2020-07-22 13:54:51 -07002313 std::shared_ptr<InputApplicationHandle> focusedApplicationHandle =
Tiger Huang721e26f2018-07-24 22:26:19 +08002314 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
2315
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 // If there is no currently focused window and no focused application
2317 // then drop the event.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002318 if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) {
2319 ALOGI("Dropping %s event because there is no focused window or focused application in "
Linnan Li13bf76a2024-05-05 19:18:02 +08002320 "display %s.",
2321 ftl::enum_string(entry.type).c_str(), displayId.toString().c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002322 return injectionError(InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 }
2324
Vishnu Nair062a8672021-09-03 16:07:44 -07002325 // Drop key events if requested by input feature
2326 if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) {
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002327 return injectionError(InputEventInjectionResult::FAILED);
Vishnu Nair062a8672021-09-03 16:07:44 -07002328 }
2329
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002330 // Compatibility behavior: raise ANR if there is a focused application, but no focused window.
2331 // Only start counting when we have a focused event to dispatch. The ANR is canceled if we
2332 // start interacting with another application via touch (app switch). This code can be removed
2333 // if the "no focused window ANR" is moved to the policy. Input doesn't know whether
2334 // an app is expected to have a focused window.
2335 if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) {
2336 if (!mNoFocusedWindowTimeoutTime.has_value()) {
2337 // We just discovered that there's no focused window. Start the ANR timer
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002338 std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout(
2339 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
2340 mNoFocusedWindowTimeoutTime = currentTime + timeout.count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002341 mAwaitedFocusedApplication = focusedApplicationHandle;
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05002342 mAwaitedApplicationDisplayId = displayId;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002343 ALOGW("Waiting because no window has focus but %s may eventually add a "
2344 "window when it finishes starting up. Will wait for %" PRId64 "ms",
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002345 mAwaitedFocusedApplication->getName().c_str(), millis(timeout));
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002346 nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002347 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002348 } else if (currentTime > *mNoFocusedWindowTimeoutTime) {
2349 // Already raised ANR. Drop the event
2350 ALOGE("Dropping %s event because there is no focused window",
Dominik Laskowski75788452021-02-09 18:51:25 -08002351 ftl::enum_string(entry.type).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002352 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002353 } else {
2354 // Still waiting for the focused window
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002355 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002356 }
2357 }
2358
2359 // we have a valid, non-null focused window
2360 resetNoFocusedWindowTimeoutLocked();
2361
Prabir Pradhan5735a322022-04-11 17:23:34 +00002362 // Verify targeted injection.
2363 if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) {
2364 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002365 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366 }
2367
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002368 if (focusedWindowHandle->getInfo()->inputConfig.test(
2369 WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002370 ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002371 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002372 }
2373
2374 // If the event is a key event, then we must wait for all previous events to
2375 // complete before delivering it because previous events may have the
2376 // side-effect of transferring focus to a different window and we want to
2377 // ensure that the following keys are sent to the new window.
2378 //
2379 // Suppose the user touches a button in a window then immediately presses "A".
2380 // If the button causes a pop-up window to appear then we want to ensure that
2381 // the "A" key is delivered to the new pop-up window. This is because users
2382 // often anticipate pending UI changes when typing on a keyboard.
2383 // To obtain this behavior, we must serialize key events with respect to all
2384 // prior input events.
2385 if (entry.type == EventEntry::Type::KEY) {
2386 if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002387 nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout);
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002388 return injectionError(InputEventInjectionResult::PENDING);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002389 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002391 // Success!
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002392 return focusedWindowHandle;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393}
2394
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002395/**
2396 * Given a list of monitors, remove the ones we cannot find a connection for, and the ones
2397 * that are currently unresponsive.
2398 */
Prabir Pradhan0a99c922021-09-03 08:27:53 -07002399std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked(
2400 const std::vector<Monitor>& monitors) const {
2401 std::vector<Monitor> responsiveMonitors;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002402 std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors),
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002403 [](const Monitor& monitor) REQUIRES(mLock) {
2404 std::shared_ptr<Connection> connection = monitor.connection;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002405 if (!connection->responsive) {
2406 ALOGW("Unresponsive monitor %s will not get the new gesture",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002407 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002408 return false;
2409 }
2410 return true;
2411 });
2412 return responsiveMonitors;
2413}
2414
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002415base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult>
2416InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002417 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002419 std::vector<InputTarget> targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 // For security reasons, we defer updating the touch state until we are sure that
2421 // event injection will be allowed.
Linnan Li13bf76a2024-05-05 19:18:02 +08002422 const ui::LogicalDisplayId displayId = entry.displayId;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002423 const int32_t action = entry.action;
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07002424 const int32_t maskedAction = MotionEvent::getActionMasked(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002426 // Copy current touch state into tempTouchState.
2427 // This state will be used to update mTouchStatesByDisplay at the end of this function.
2428 // If no state for the specified display exists, then our initial state will be empty.
Yi Kong9b14ac62018-07-17 13:48:38 -07002429 const TouchState* oldState = nullptr;
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002430 TouchState tempTouchState;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002431 if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
2432 oldState = &(it->second);
Prabir Pradhane680f9b2022-02-04 04:24:00 -08002433 tempTouchState = *oldState;
Jeff Brownf086ddb2014-02-11 14:28:48 -08002434 }
2435
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002436 bool isSplit = shouldSplitTouch(tempTouchState, entry);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002437
2438 const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE ||
2439 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2440 maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002441 // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any
2442 // touchable windows.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002443 const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002444 const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) ||
2445 (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002446 const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL ||
2447 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2448 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE;
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002449 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002450
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002451 if (newGesture) {
2452 isSplit = false;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002453 }
2454
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002455 if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) {
2456 // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated.
2457 tempTouchState.clearHoveringPointers(entry.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458 }
2459
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002460 if (isHoverAction) {
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002461 if (wasDown) {
2462 // Started hovering, but the device is already down: reject the hover event
2463 LOG(ERROR) << "Got hover event " << entry.getDescription()
2464 << " but the device is already down " << oldState->dump();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002465 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002466 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002467 // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase
2468 // all of the existing hovering pointers and recompute.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002469 tempTouchState.clearHoveringPointers(entry.deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002470 }
2471
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
2473 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002474 const auto [x, y] = resolveTouchedPosition(entry);
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002475 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002476 const PointerProperties& pointer = entry.pointerProperties[pointerIndex];
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002477 // Outside targets should be added upon first dispatched DOWN event. That means, this should
2478 // be a pointer that would generate ACTION_DOWN, *and* touch should not already be down.
Prabir Pradhand65552b2021-10-07 11:23:50 -07002479 const bool isStylus = isPointerFromStylus(entry, pointerIndex);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002480 sp<WindowInfoHandle> newTouchedWindowHandle =
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002481 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Michael Wright3dd60e22019-03-27 22:06:44 +00002482
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002483 if (isDown) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002484 targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002485 }
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07002486 LOG_IF(INFO, newTouchedWindowHandle == nullptr)
2487 << "No new touched window at (" << std::format("{:.1f}, {:.1f}", x, y)
2488 << ") in display " << displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 // Handle the case where we did not find a window.
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07002490 if (!input_flags::split_all_touches()) {
2491 // If we are force splitting all touches, then touches outside of the window should
2492 // be dropped, even if this device already has pointers down in another window.
2493 if (newTouchedWindowHandle == nullptr) {
2494 // Try to assign the pointer to the first foreground window we find, if there is
2495 // one.
2496 newTouchedWindowHandle =
2497 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
2498 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002499 }
2500
Prabir Pradhan5735a322022-04-11 17:23:34 +00002501 // Verify targeted injection.
2502 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2503 ALOGW("Dropping injected touch event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002504 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002505 }
2506
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002507 // Figure out whether splitting will be allowed for this window.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002508 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002509 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
2510 // New window supports splitting, but we should never split mouse events.
2511 isSplit = !isFromMouse;
2512 } else if (isSplit) {
2513 // New window does not support splitting but we have already split events.
2514 // Ignore the new window.
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002515 LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName()
2516 << " because it doesn't support split touch";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002517 newTouchedWindowHandle = nullptr;
2518 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002519 } else {
2520 // No window is touched, so set split to true. This will allow the next pointer down to
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002521 // be delivered to a new window which supports split touch. Pointers from a mouse device
2522 // should never be split.
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002523 isSplit = !isFromMouse;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002524 }
2525
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002526 std::vector<sp<WindowInfoHandle>> newTouchedWindows =
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07002527 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus, entry.deviceId);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002528 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002529 // Process the foreground window first so that it is the first to receive the event.
2530 newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002531 }
2532
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002533 if (newTouchedWindows.empty()) {
Siarhei Vishniakouac42d242024-06-28 10:43:53 -07002534 LOG(INFO) << "Dropping event because there is no touchable window at (" << x << ", "
2535 << y << ") on display " << displayId << ": " << entry;
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002536 return injectionError(InputEventInjectionResult::FAILED);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002537 }
2538
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002539 for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002540 if (!canWindowReceiveMotionLocked(windowHandle, entry)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002541 continue;
2542 }
2543
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002544 if (isHoverAction) {
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07002545 // The "windowHandle" is the target of this hovering pointer.
Siarhei Vishniakoub711e652024-08-12 12:00:28 -07002546 tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer, x,
2547 y);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002548 }
2549
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002550 // Set target flags.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002551 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002552
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002553 if (canReceiveForegroundTouches(*windowHandle->getInfo())) {
2554 // There should only be one touched window that can be "foreground" for the pointer.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002555 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002556 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002557
2558 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002559 targetFlags |= InputTarget::Flags::SPLIT;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002560 }
2561 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002562 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002563 } else if (isWindowObscuredLocked(windowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002564 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002565 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002566
2567 // Update the temporary touch state.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002568
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002569 if (!isHoverAction) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002570 const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN ||
2571 maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN;
Siarhei Vishniakoue535f972024-03-29 14:55:21 -07002572 Result<void> addResult =
2573 tempTouchState.addOrUpdateWindow(windowHandle,
2574 InputTarget::DispatchMode::AS_IS,
2575 targetFlags, entry.deviceId, {pointer},
2576 isDownOrPointerDown
2577 ? std::make_optional(
2578 entry.eventTime)
2579 : std::nullopt);
2580 if (!addResult.ok()) {
2581 LOG(ERROR) << "Error while processing " << entry << " for "
2582 << windowHandle->getName();
2583 logDispatchStateLocked();
2584 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002585 // If this is the pointer going down and the touched window has a wallpaper
2586 // then also add the touched wallpaper windows so they are locked in for the
2587 // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or
2588 // SCROLL because the wallpaper engine only supports touch events. We would need to
2589 // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to
2590 // handle these events.
2591 if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) &&
Arthur Hungc539dbb2022-12-08 07:45:36 +00002592 windowHandle->getInfo()->inputConfig.test(
2593 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
2594 sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle);
2595 if (wallpaper != nullptr) {
2596 ftl::Flags<InputTarget::Flags> wallpaperFlags =
2597 InputTarget::Flags::WINDOW_IS_OBSCURED |
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002598 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Arthur Hungc539dbb2022-12-08 07:45:36 +00002599 if (isSplit) {
2600 wallpaperFlags |= InputTarget::Flags::SPLIT;
2601 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002602 tempTouchState.addOrUpdateWindow(wallpaper,
2603 InputTarget::DispatchMode::AS_IS,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002604 wallpaperFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002605 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002606 }
2607 }
2608 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002610
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002611 // If a window is already pilfering some pointers, give it this new pointer as well and
2612 // make it pilfering. This will prevent other non-spy windows from getting this pointer,
2613 // which is a specific behaviour that we want.
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002614 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002615 if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) &&
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002616 touchedWindow.hasPilferingPointers(entry.deviceId)) {
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002617 // This window is already pilfering some pointers, and this new pointer is also
2618 // going to it. Therefore, take over this pointer and don't give it to anyone
2619 // else.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002620 touchedWindow.addPilferingPointer(entry.deviceId, pointer.id);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002621 }
2622 }
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002623
2624 // Restrict all pilfered pointers to the pilfering windows.
2625 tempTouchState.cancelPointersForNonPilferingWindows();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626 } else {
2627 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
2628
2629 // If the pointer is not currently down, then ignore the event.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002630 if (!tempTouchState.isDown(entry.deviceId) &&
2631 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Siarhei Vishniakou31dd1552023-10-30 18:46:10 -07002632 if (DEBUG_DROPPED_EVENTS_VERBOSE) {
2633 LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId
2634 << " is not down or we previously dropped the pointer down event in "
2635 << "display " << displayId << ": " << entry.getDescription();
2636 }
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002637 return injectionError(InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 }
2639
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002640 // If the pointer is not currently hovering, then ignore the event.
2641 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) {
2642 const int32_t pointerId = entry.pointerProperties[0].id;
2643 if (oldState == nullptr ||
2644 oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) {
2645 LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in "
2646 "display "
2647 << displayId << ": " << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002648 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002649 }
2650 tempTouchState.removeHoveringPointer(entry.deviceId, pointerId);
2651 }
2652
arthurhung6d4bed92021-03-17 11:59:33 +08002653 addDragEventLocked(entry);
arthurhungb89ccb02020-12-30 16:19:01 +08002654
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 // Check whether touches should slip outside of the current foreground window.
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002656 if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 &&
Linnan Li49b2b202024-04-12 12:46:40 +08002657 tempTouchState.isSlippery(entry.deviceId)) {
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07002658 const auto [x, y] = resolveTouchedPosition(entry);
Harry Cutts33476232023-01-30 19:57:29 +00002659 const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0);
chaviw98318de2021-05-19 16:45:23 -05002660 sp<WindowInfoHandle> oldTouchedWindowHandle =
Linnan Liccf6ce32024-04-11 20:32:13 +08002661 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002662 LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002663 sp<WindowInfoHandle> newTouchedWindowHandle =
2664 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Vishnu Nair062a8672021-09-03 16:07:44 -07002665
Prabir Pradhan5735a322022-04-11 17:23:34 +00002666 // Verify targeted injection.
2667 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2668 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002669 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002670 }
2671
Linnan Libf069a32024-02-29 17:22:59 +00002672 // Do not slide events to the window which can not receive motion event
Vishnu Nair062a8672021-09-03 16:07:44 -07002673 if (newTouchedWindowHandle != nullptr &&
Linnan Libf069a32024-02-29 17:22:59 +00002674 !canWindowReceiveMotionLocked(newTouchedWindowHandle, entry)) {
Vishnu Nair062a8672021-09-03 16:07:44 -07002675 newTouchedWindowHandle = nullptr;
2676 }
2677
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07002678 if (newTouchedWindowHandle != nullptr &&
2679 !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) {
Linnan Li13bf76a2024-05-05 19:18:02 +08002680 ALOGI("Touch is slipping out of window %s into window %s in display %s",
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002681 oldTouchedWindowHandle->getName().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08002682 newTouchedWindowHandle->getName().c_str(), displayId.toString().c_str());
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002683
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684 // Make a slippery exit from the old window.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08002685 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002686 const PointerProperties& pointer = entry.pointerProperties[0];
2687 pointerIds.set(pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002688
2689 const TouchedWindow& touchedWindow =
2690 tempTouchState.getTouchedWindow(oldTouchedWindowHandle);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002691 addPointerWindowTargetLocked(oldTouchedWindowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002692 InputTarget::DispatchMode::SLIPPERY_EXIT,
2693 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002694 touchedWindow.getDownTimeInTarget(entry.deviceId),
2695 targets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696
2697 // Make a slippery entrance into the new window.
2698 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002699 isSplit = !isFromMouse;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 }
2701
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002702 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002703 if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002704 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002705 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002707 targetFlags |= InputTarget::Flags::SPLIT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002710 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002711 } else if (isWindowObscuredLocked(newTouchedWindowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002712 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 }
2714
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002715 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle,
2716 InputTarget::DispatchMode::SLIPPERY_ENTER,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002717 targetFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002718 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002719
2720 // Check if the wallpaper window should deliver the corresponding event.
2721 slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00002722 tempTouchState, entry, targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002723 tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002724 oldTouchedWindowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 }
2726 }
Arthur Hung96483742022-11-15 03:30:48 +00002727
2728 // Update the pointerIds for non-splittable when it received pointer down.
2729 if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2730 // If no split, we suppose all touched windows should receive pointer down.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002731 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002732 std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]};
2733 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Arthur Hung96483742022-11-15 03:30:48 +00002734 // Ignore drag window for it should just track one pointer.
2735 if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) {
2736 continue;
2737 }
Siarhei Vishniakou96204462024-07-13 23:59:47 -07002738 if (!touchedWindow.hasTouchingPointers(entry.deviceId)) {
2739 continue;
2740 }
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002741 touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers);
Arthur Hung96483742022-11-15 03:30:48 +00002742 }
2743 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 }
2745
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002746 // Update dispatching for hover enter and exit.
Sam Dubeyf886dec2023-01-27 13:28:19 +00002747 {
2748 std::vector<TouchedWindow> hoveringWindows =
Siarhei Vishniakouf91f3c72024-08-12 09:58:04 -07002749 getHoveringWindowsLocked(oldState, tempTouchState, entry,
Siarhei Vishniakou8a69ace2024-08-13 18:04:40 +00002750 std::bind_front(&InputDispatcher::logDispatchStateLocked,
2751 this));
Linnan Li41859c42024-03-05 16:20:34 +00002752 // Hardcode to single hovering pointer for now.
2753 std::bitset<MAX_POINTER_ID + 1> pointerIds;
2754 pointerIds.set(entry.pointerProperties[0].id);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002755 for (const TouchedWindow& touchedWindow : hoveringWindows) {
Linnan Li41859c42024-03-05 16:20:34 +00002756 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
2757 touchedWindow.targetFlags, pointerIds,
2758 touchedWindow.getDownTimeInTarget(entry.deviceId),
2759 targets);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002760 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762
Prabir Pradhan5735a322022-04-11 17:23:34 +00002763 // Ensure that all touched windows are valid for injection.
2764 if (entry.injectionState != nullptr) {
2765 std::string errs;
2766 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00002767 const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry);
2768 if (err) errs += "\n - " + *err;
2769 }
2770 if (!errs.empty()) {
2771 ALOGW("Dropping targeted injection: At least one touched window is not owned by uid "
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002772 "%s:%s",
2773 entry.injectionState->targetUid->toString().c_str(), errs.c_str());
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002774 return injectionError(InputEventInjectionResult::TARGET_MISMATCH);
Prabir Pradhan5735a322022-04-11 17:23:34 +00002775 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002776 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002777
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002778 // Check whether windows listening for outside touches are owned by the same UID. If the owner
2779 // has a different UID, then we will not reveal coordinate information to this window.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
chaviw98318de2021-05-19 16:45:23 -05002781 sp<WindowInfoHandle> foregroundWindowHandle =
Linnan Liccf6ce32024-04-11 20:32:13 +08002782 tempTouchState.getFirstForegroundWindowHandle(entry.deviceId);
Michael Wright3dd60e22019-03-27 22:06:44 +00002783 if (foregroundWindowHandle) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002784 const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002785 for (InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002786 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002787 sp<WindowInfoHandle> targetWindow =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002788 getWindowHandleLocked(target.connection->getToken());
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002789 if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) {
2790 target.flags |= InputTarget::Flags::ZERO_COORDS;
Michael Wright3dd60e22019-03-27 22:06:44 +00002791 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 }
2793 }
2794 }
2795 }
2796
Harry Cuttsb166c002023-05-09 13:06:05 +00002797 // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we
2798 // only want the system UI to handle these gestures.
2799 const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) &&
2800 entry.classification == MotionClassification::MULTI_FINGER_SWIPE;
2801 if (isTouchpadNavGesture) {
2802 filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets);
2803 }
2804
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002805 // Output targets from the touch state.
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002806 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002807 std::vector<PointerProperties> touchingPointers =
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002808 touchedWindow.getTouchingPointers(entry.deviceId);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002809 if (touchingPointers.empty()) {
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002810 continue;
2811 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002812 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002813 touchedWindow.targetFlags, getPointerIds(touchingPointers),
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002814 touchedWindow.getDownTimeInTarget(entry.deviceId), targets);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002815 }
Sam Dubey39d37cf2022-12-07 18:05:35 +00002816
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002817 // During targeted injection, only allow owned targets to receive events
2818 std::erase_if(targets, [&](const InputTarget& target) {
2819 LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr);
2820 const auto err = verifyTargetedInjection(target.windowHandle, entry);
2821 if (err) {
2822 LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName()
2823 << ": " << (*err);
2824 return true;
2825 }
2826 return false;
2827 });
2828
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002829 if (targets.empty()) {
2830 LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002831 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002832 }
2833
2834 // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no
2835 // window that is actually receiving the entire gesture.
2836 if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002837 return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002838 })) {
2839 LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: "
2840 << entry.getDescription();
Siarhei Vishniakoue1855a92024-07-12 13:40:36 -07002841 return injectionError(InputEventInjectionResult::FAILED);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002842 }
2843
Prabir Pradhan502a7252023-12-01 16:11:24 +00002844 // Now that we have generated all of the input targets for this event, reset the dispatch
2845 // mode for all touched window to AS_IS.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002846 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan502a7252023-12-01 16:11:24 +00002847 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002848 }
2849
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002850 // Update final pieces of touch state if the injector had permission.
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002851 if (maskedAction == AMOTION_EVENT_ACTION_UP) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002852 // Pointer went up.
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002853 tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002854 } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002855 // All pointers up or canceled.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002856 tempTouchState.removeAllPointersForDevice(entry.deviceId);
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002857 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2858 // One pointer went up.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002859 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
2860 const uint32_t pointerId = entry.pointerProperties[pointerIndex].id;
2861 tempTouchState.removeTouchingPointer(entry.deviceId, pointerId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862 }
2863
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002864 // Save changes unless the action was scroll in which case the temporary touch
2865 // state was only valid for this one action.
2866 if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07002867 if (displayId >= ui::LogicalDisplayId::DEFAULT) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002868 tempTouchState.clearWindowsWithoutPointers();
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002869 mTouchStatesByDisplay[displayId] = tempTouchState;
2870 } else {
2871 mTouchStatesByDisplay.erase(displayId);
2872 }
2873 }
2874
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002875 if (tempTouchState.windows.empty()) {
2876 mTouchStatesByDisplay.erase(displayId);
2877 }
2878
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002879 return targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880}
2881
Linnan Li13bf76a2024-05-05 19:18:02 +08002882void InputDispatcher::finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07002883 // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we
2884 // have an explicit reason to support it.
2885 constexpr bool isStylus = false;
2886
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002887 sp<WindowInfoHandle> dropWindow =
Harry Cutts33476232023-01-30 19:57:29 +00002888 findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true);
arthurhung6d4bed92021-03-17 11:59:33 +08002889 if (dropWindow) {
2890 vec2 local = dropWindow->getInfo()->transform.transform(x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002891 sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y);
Arthur Hung6d0571e2021-04-09 20:18:16 +08002892 } else {
Arthur Hung54745652022-04-20 07:17:41 +00002893 ALOGW("No window found when drop.");
Prabir Pradhancef936d2021-07-21 16:17:52 +00002894 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08002895 }
2896 mDragState.reset();
2897}
2898
2899void InputDispatcher::addDragEventLocked(const MotionEntry& entry) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00002900 if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) {
arthurhungb89ccb02020-12-30 16:19:01 +08002901 return;
2902 }
2903
arthurhung6d4bed92021-03-17 11:59:33 +08002904 if (!mDragState->isStartDrag) {
2905 mDragState->isStartDrag = true;
2906 mDragState->isStylusButtonDownAtStart =
2907 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2908 }
2909
Arthur Hung54745652022-04-20 07:17:41 +00002910 // Find the pointer index by id.
2911 int32_t pointerIndex = 0;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002912 for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) {
Arthur Hung54745652022-04-20 07:17:41 +00002913 const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex];
2914 if (pointerProperties.id == mDragState->pointerId) {
2915 break;
arthurhung6d4bed92021-03-17 11:59:33 +08002916 }
Arthur Hung54745652022-04-20 07:17:41 +00002917 }
arthurhung6d4bed92021-03-17 11:59:33 +08002918
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002919 if (uint32_t(pointerIndex) == entry.getPointerCount()) {
Arthur Hung54745652022-04-20 07:17:41 +00002920 LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId);
Arthur Hung54745652022-04-20 07:17:41 +00002921 }
2922
2923 const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK;
2924 const int32_t x = entry.pointerCoords[pointerIndex].getX();
2925 const int32_t y = entry.pointerCoords[pointerIndex].getY();
2926
2927 switch (maskedAction) {
2928 case AMOTION_EVENT_ACTION_MOVE: {
2929 // Handle the special case : stylus button no longer pressed.
2930 bool isStylusButtonDown =
2931 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2932 if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) {
2933 finishDragAndDrop(entry.displayId, x, y);
2934 return;
2935 }
2936
2937 // Prevent stylus interceptor windows from affecting drag and drop behavior for now,
2938 // until we have an explicit reason to support it.
2939 constexpr bool isStylus = false;
2940
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002941 sp<WindowInfoHandle> hoverWindowHandle =
2942 findTouchedWindowAtLocked(entry.displayId, x, y, isStylus,
2943 /*ignoreDragWindow=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00002944 // enqueue drag exit if needed.
2945 if (hoverWindowHandle != mDragState->dragHoverWindowHandle &&
2946 !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) {
2947 if (mDragState->dragHoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002948 enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x,
Arthur Hung54745652022-04-20 07:17:41 +00002949 y);
2950 }
2951 mDragState->dragHoverWindowHandle = hoverWindowHandle;
2952 }
2953 // enqueue drag location if needed.
2954 if (hoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002955 enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y);
Arthur Hung54745652022-04-20 07:17:41 +00002956 }
2957 break;
2958 }
2959
2960 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002961 if (MotionEvent::getActionIndex(entry.action) != pointerIndex) {
Arthur Hung54745652022-04-20 07:17:41 +00002962 break;
2963 }
2964 // The drag pointer is up.
2965 [[fallthrough]];
2966 case AMOTION_EVENT_ACTION_UP:
2967 finishDragAndDrop(entry.displayId, x, y);
2968 break;
2969 case AMOTION_EVENT_ACTION_CANCEL: {
2970 ALOGD("Receiving cancel when drag and drop.");
2971 sendDropWindowCommandLocked(nullptr, 0, 0);
2972 mDragState.reset();
2973 break;
2974 }
arthurhungb89ccb02020-12-30 16:19:01 +08002975 }
2976}
2977
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002978std::optional<InputTarget> InputDispatcher::createInputTargetLocked(
2979 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002980 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002981 std::optional<nsecs_t> firstDownTimeInTarget) const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002982 std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken());
2983 if (connection == nullptr) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002984 ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str());
2985 return {};
2986 }
Prabir Pradhanc32a4112024-01-23 23:20:37 +00002987 InputTarget inputTarget{connection};
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002988 inputTarget.windowHandle = windowHandle;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002989 inputTarget.dispatchMode = dispatchMode;
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002990 inputTarget.flags = targetFlags;
2991 inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor;
2992 inputTarget.firstDownTimeInTarget = firstDownTimeInTarget;
2993 const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId);
2994 if (displayInfoIt != mDisplayInfos.end()) {
2995 inputTarget.displayTransform = displayInfoIt->second.transform;
2996 } else {
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002997 // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId.
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002998 // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed.
2999 }
3000 return inputTarget;
3001}
3002
chaviw98318de2021-05-19 16:45:23 -05003003void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003004 InputTarget::DispatchMode dispatchMode,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003005 ftl::Flags<InputTarget::Flags> targetFlags,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003006 std::optional<nsecs_t> firstDownTimeInTarget,
Siarhei Vishniakouf75cddb2022-10-25 10:42:16 -07003007 std::vector<InputTarget>& inputTargets) const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003008 std::vector<InputTarget>::iterator it =
3009 std::find_if(inputTargets.begin(), inputTargets.end(),
3010 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003011 return inputTarget.connection->getToken() == windowHandle->getToken();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003012 });
Chavi Weingarten97b8eec2020-01-09 18:09:08 +00003013
chaviw98318de2021-05-19 16:45:23 -05003014 const WindowInfo* windowInfo = windowHandle->getInfo();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003015
3016 if (it == inputTargets.end()) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07003017 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003018 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
3019 firstDownTimeInTarget);
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07003020 if (!target) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003021 return;
3022 }
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07003023 inputTargets.push_back(*target);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003024 it = inputTargets.end() - 1;
3025 }
3026
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003027 if (it->flags != targetFlags) {
3028 LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it;
3029 }
3030 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
3031 LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
3032 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
3033 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003034}
3035
3036void InputDispatcher::addPointerWindowTargetLocked(
3037 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003038 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
3039 std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget,
3040 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003041 if (pointerIds.none()) {
3042 for (const auto& target : inputTargets) {
3043 LOG(INFO) << "Target: " << target;
3044 }
3045 LOG(FATAL) << "No pointers specified for " << windowHandle->getName();
3046 return;
3047 }
3048 std::vector<InputTarget>::iterator it =
3049 std::find_if(inputTargets.begin(), inputTargets.end(),
3050 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003051 return inputTarget.connection->getToken() == windowHandle->getToken();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003052 });
3053
3054 // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that
3055 // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would
3056 // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate
3057 // input targets for hovering pointers and for touching pointers.
3058 // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new
3059 // target instead.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003060 if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003061 // Force the code below to create a new input target
3062 it = inputTargets.end();
3063 }
3064
3065 const WindowInfo* windowInfo = windowHandle->getInfo();
3066
3067 if (it == inputTargets.end()) {
3068 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003069 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
3070 firstDownTimeInTarget);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003071 if (!target) {
3072 return;
3073 }
3074 inputTargets.push_back(*target);
3075 it = inputTargets.end() - 1;
3076 }
3077
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003078 if (it->dispatchMode != dispatchMode) {
3079 LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode="
3080 << ftl::enum_string(dispatchMode) << ", it=" << *it;
3081 }
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003082 if (it->flags != targetFlags) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003083 LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string()
3084 << ", it=" << *it;
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07003085 }
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003086 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003087 LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07003088 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
3089 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003090
Siarhei Vishniakou59967592024-04-01 16:17:46 -07003091 Result<void> result = it->addPointers(pointerIds, windowInfo->transform);
3092 if (!result.ok()) {
3093 logDispatchStateLocked();
3094 LOG(FATAL) << result.error().message();
3095 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096}
3097
Michael Wright3dd60e22019-03-27 22:06:44 +00003098void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
Linnan Li13bf76a2024-05-05 19:18:02 +08003099 ui::LogicalDisplayId displayId) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003100 auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId);
3101 if (monitorsIt == mGlobalMonitorsByDisplay.end()) return;
Michael Wright3dd60e22019-03-27 22:06:44 +00003102
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003103 for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003104 InputTarget target{monitor.connection};
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003105 // target.firstDownTimeInTarget is not set for global monitors. It is only required in split
3106 // touch and global monitoring works as intended even without setting firstDownTimeInTarget
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003107 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
3108 target.displayTransform = it->second.transform;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08003109 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08003110 target.setDefaultPointerTransform(target.displayTransform);
3111 inputTargets.push_back(target);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 }
3113}
3114
Robert Carrc9bf1d32020-04-13 17:21:08 -07003115/**
3116 * Indicate whether one window handle should be considered as obscuring
3117 * another window handle. We only check a few preconditions. Actually
3118 * checking the bounds is left to the caller.
3119 */
chaviw98318de2021-05-19 16:45:23 -05003120static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
3121 const sp<WindowInfoHandle>& otherHandle) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003122 // Compare by token so cloned layers aren't counted
3123 if (haveSameToken(windowHandle, otherHandle)) {
3124 return false;
3125 }
3126 auto info = windowHandle->getInfo();
3127 auto otherInfo = otherHandle->getInfo();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003128 if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003129 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003130 } else if (otherInfo->alpha == 0 &&
3131 otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) {
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003132 // Those act as if they were invisible, so we don't need to flag them.
3133 // We do want to potentially flag touchable windows even if they have 0
3134 // opacity, since they can consume touches and alter the effects of the
3135 // user interaction (eg. apps that rely on
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003136 // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003137 // windows), hence we also check for FLAG_NOT_TOUCHABLE.
3138 return false;
Bernardo Rufino8007daf2020-09-22 09:40:01 +00003139 } else if (info->ownerUid == otherInfo->ownerUid) {
3140 // If ownerUid is the same we don't generate occlusion events as there
3141 // is no security boundary within an uid.
Robert Carrc9bf1d32020-04-13 17:21:08 -07003142 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003143 } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003144 return false;
3145 } else if (otherInfo->displayId != info->displayId) {
3146 return false;
3147 }
3148 return true;
3149}
3150
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003151/**
3152 * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is
3153 * untrusted, one should check:
3154 *
3155 * 1. If result.hasBlockingOcclusion is true.
3156 * If it's, it means the touch should be blocked due to a window with occlusion mode of
3157 * BLOCK_UNTRUSTED.
3158 *
3159 * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch.
3160 * If it is (and 1 is false), then the touch should be blocked because a stack of windows
3161 * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed
3162 * obscuring opacity above the threshold. Note that if there was no window of occlusion mode
3163 * USE_OPACITY, result.obscuringOpacity would've been 0 and since
3164 * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true.
3165 *
3166 * If neither of those is true, then it means the touch can be allowed.
3167 */
3168InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
Linnan Li5e5645e2024-03-05 14:43:05 +00003169 const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
chaviw98318de2021-05-19 16:45:23 -05003170 const WindowInfo* windowInfo = windowHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003171 ui::LogicalDisplayId displayId = windowInfo->displayId;
chaviw98318de2021-05-19 16:45:23 -05003172 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003173 TouchOcclusionInfo info;
3174 info.hasBlockingOcclusion = false;
3175 info.obscuringOpacity = 0;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003176 info.obscuringUid = gui::Uid::INVALID;
3177 std::map<gui::Uid, float> opacityByUid;
chaviw98318de2021-05-19 16:45:23 -05003178 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003179 if (windowHandle == otherHandle) {
3180 break; // All future windows are below us. Exit early.
3181 }
chaviw98318de2021-05-19 16:45:23 -05003182 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li5e5645e2024-03-05 14:43:05 +00003183 if (canBeObscuredBy(windowHandle, otherHandle) &&
3184 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId)) &&
Bernardo Rufino1ff9d592021-01-18 16:58:57 +00003185 !haveSameApplicationToken(windowInfo, otherInfo)) {
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003186 if (DEBUG_TOUCH_OCCLUSION) {
3187 info.debugInfo.push_back(
Harry Cutts101ee9b2023-07-06 18:04:14 +00003188 dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003189 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003190 // canBeObscuredBy() has returned true above, which means this window is untrusted, so
3191 // we perform the checks below to see if the touch can be propagated or not based on the
3192 // window's touch occlusion mode
3193 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) {
3194 info.hasBlockingOcclusion = true;
3195 info.obscuringUid = otherInfo->ownerUid;
3196 info.obscuringPackage = otherInfo->packageName;
3197 break;
3198 }
3199 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003200 const auto uid = otherInfo->ownerUid;
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003201 float opacity =
3202 (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid];
3203 // Given windows A and B:
3204 // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)]
3205 opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha);
3206 opacityByUid[uid] = opacity;
3207 if (opacity > info.obscuringOpacity) {
3208 info.obscuringOpacity = opacity;
3209 info.obscuringUid = uid;
3210 info.obscuringPackage = otherInfo->packageName;
3211 }
3212 }
3213 }
3214 }
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003215 if (DEBUG_TOUCH_OCCLUSION) {
Harry Cutts101ee9b2023-07-06 18:04:14 +00003216 info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003217 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003218 return info;
3219}
3220
chaviw98318de2021-05-19 16:45:23 -05003221std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003222 bool isTouchedWindow) const {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003223 return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003224 "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3225 "], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3226 "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003227 isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003228 info->ownerUid.toString().c_str(), info->id,
Chavi Weingarten7f019192023-08-08 20:39:01 +00003229 toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3230 info->frame.top, info->frame.right, info->frame.bottom,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003231 dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3232 info->inputConfig.string().c_str(), toString(info->token != nullptr),
3233 info->applicationInfo.name.c_str(),
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003234 binderToString(info->applicationInfo.token).c_str());
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003235}
3236
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003237bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3238 if (occlusionInfo.hasBlockingOcclusion) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003239 ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
3240 occlusionInfo.obscuringUid.toString().c_str());
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003241 return false;
3242 }
3243 if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003244 ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = "
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003245 "%.2f, maximum allowed = %.2f)",
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003246 occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(),
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003247 occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch);
3248 return false;
3249 }
3250 return true;
3251}
3252
chaviw98318de2021-05-19 16:45:23 -05003253bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle,
Linnan Li5e5645e2024-03-05 14:43:05 +00003254 float x, float y) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003255 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003256 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3257 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003258 if (windowHandle == otherHandle) {
3259 break; // All future windows are below us. Exit early.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 }
chaviw98318de2021-05-19 16:45:23 -05003261 const WindowInfo* otherInfo = otherHandle->getInfo();
Robert Carrc9bf1d32020-04-13 17:21:08 -07003262 if (canBeObscuredBy(windowHandle, otherHandle) &&
Linnan Li5e5645e2024-03-05 14:43:05 +00003263 windowOccludesTouchAt(*otherInfo, displayId, x, y, getTransformLocked(displayId))) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 return true;
3265 }
3266 }
3267 return false;
3268}
3269
chaviw98318de2021-05-19 16:45:23 -05003270bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08003271 ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003272 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3273 const WindowInfo* windowInfo = windowHandle->getInfo();
3274 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003275 if (windowHandle == otherHandle) {
3276 break; // All future windows are below us. Exit early.
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003277 }
chaviw98318de2021-05-19 16:45:23 -05003278 const WindowInfo* otherInfo = otherHandle->getInfo();
Linnan Li13bf76a2024-05-05 19:18:02 +08003279 if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->overlaps(windowInfo)) {
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003280 return true;
3281 }
3282 }
3283 return false;
3284}
3285
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08003286std::string InputDispatcher::getApplicationWindowLabel(
chaviw98318de2021-05-19 16:45:23 -05003287 const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) {
Yi Kong9b14ac62018-07-17 13:48:38 -07003288 if (applicationHandle != nullptr) {
3289 if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003290 return applicationHandle->getName() + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 } else {
3292 return applicationHandle->getName();
3293 }
Yi Kong9b14ac62018-07-17 13:48:38 -07003294 } else if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003295 return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08003297 return "<unknown application or window>";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 }
3299}
3300
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003301void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00003302 if (!isUserActivityEvent(eventEntry)) {
3303 // Not poking user activity if the event type does not represent a user activity
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003304 return;
3305 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003306
3307 const int32_t eventType = getUserActivityEventType(eventEntry);
3308 if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) {
3309 // Note that we're directly getting the time diff between the current event and the previous
3310 // event. This is assuming that the first user event always happens at a timestamp that is
3311 // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will
3312 // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller
3313 // value than the potential first user activity event time, so this is ok.
3314 std::chrono::nanoseconds timeSinceLastEvent =
3315 std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]);
3316 if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) {
3317 return;
3318 }
3319 }
3320
Linnan Li13bf76a2024-05-05 19:18:02 +08003321 ui::LogicalDisplayId displayId = getTargetDisplayId(eventEntry);
chaviw98318de2021-05-19 16:45:23 -05003322 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Josep del Riob3981622023-04-18 15:49:45 +00003323 const WindowInfo* windowDisablingUserActivityInfo = nullptr;
Tiger Huang721e26f2018-07-24 22:26:19 +08003324 if (focusedWindowHandle != nullptr) {
chaviw98318de2021-05-19 16:45:23 -05003325 const WindowInfo* info = focusedWindowHandle->getInfo();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003326 if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) {
Josep del Riob3981622023-04-18 15:49:45 +00003327 windowDisablingUserActivityInfo = info;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 }
3329 }
3330
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003331 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003332 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003333 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3334 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003335 return;
3336 }
Josep del Riob3981622023-04-18 15:49:45 +00003337 if (windowDisablingUserActivityInfo != nullptr) {
3338 if (DEBUG_DISPATCH_CYCLE) {
3339 ALOGD("Not poking user activity: disabled by window '%s'.",
3340 windowDisablingUserActivityInfo->name.c_str());
3341 }
3342 return;
3343 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003344 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003346 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003347 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3348 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003349 return;
3350 }
Josep del Riob3981622023-04-18 15:49:45 +00003351 // Don't inhibit events that were intercepted or are not passed to
3352 // the apps, like system shortcuts
3353 if (windowDisablingUserActivityInfo != nullptr &&
Josep del Rioc8fdedb2024-05-21 13:32:43 +00003354 keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP) {
Josep del Riob3981622023-04-18 15:49:45 +00003355 if (DEBUG_DISPATCH_CYCLE) {
3356 ALOGD("Not poking user activity: disabled by window '%s'.",
3357 windowDisablingUserActivityInfo->name.c_str());
3358 }
3359 return;
3360 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003361 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00003363 default: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003364 LOG_ALWAYS_FATAL("%s events are not user activity",
Dominik Laskowski75788452021-02-09 18:51:25 -08003365 ftl::enum_string(eventEntry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003366 break;
3367 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 }
3369
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003370 mLastUserActivityTimes[eventType] = eventEntry.eventTime;
Prabir Pradhancef936d2021-07-21 16:17:52 +00003371 auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]()
3372 REQUIRES(mLock) {
3373 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003374 mPolicy.pokeUserActivity(eventTime, eventType, displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003375 };
3376 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377}
3378
3379void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003380 const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003381 std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003382 const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003383 ATRACE_NAME_IF(ATRACE_ENABLED(),
3384 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
3385 connection->getInputChannelName().c_str(), eventEntry->id));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003386 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003387 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, "
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08003388 "globalScaleFactor=%f, pointerIds=%s %s",
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003389 connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(),
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003390 inputTarget.globalScaleFactor, bitsetToString(inputTarget.getPointerIds()).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003391 inputTarget.getPointerInfoString().c_str());
3392 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
3394 // Skip this event if the connection status is not normal.
3395 // We don't want to enqueue additional outbound events if the connection is broken.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003396 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003397 if (DEBUG_DISPATCH_CYCLE) {
3398 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003399 connection->getInputChannelName().c_str(),
3400 ftl::enum_string(connection->status).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003401 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 return;
3403 }
3404
3405 // Split a motion event if needed.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003406 if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) {
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003407 LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003408 "Entry type %s should not have Flags::SPLIT",
Dominik Laskowski75788452021-02-09 18:51:25 -08003409 ftl::enum_string(eventEntry->type).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003411 const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry);
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003412 if (inputTarget.getPointerIds().count() != originalMotionEntry.getPointerCount()) {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -08003413 if (!inputTarget.firstDownTimeInTarget.has_value()) {
3414 logDispatchStateLocked();
3415 LOG(FATAL) << "Splitting motion events requires a down time to be set for the "
3416 "target on connection "
3417 << connection->getInputChannelName() << " for "
3418 << originalMotionEntry.getDescription();
3419 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003420 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhanc32a4112024-01-23 23:20:37 +00003421 splitMotionEvent(originalMotionEntry, inputTarget.getPointerIds(),
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003422 inputTarget.firstDownTimeInTarget.value());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 if (!splitMotionEntry) {
3424 return; // split event was dropped
3425 }
Arthur Hungb3307ee2021-10-14 10:57:37 +00003426 if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
3427 std::string reason = std::string("reason=pointer cancel on split window");
3428 android_log_event_list(LOGTAG_INPUT_CANCEL)
3429 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
3430 }
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003431 if (DEBUG_FOCUS) {
3432 ALOGD("channel '%s' ~ Split motion event.",
3433 connection->getInputChannelName().c_str());
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003434 logOutboundMotionDetails(" ", *splitMotionEntry);
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003435 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003436 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection,
3437 std::move(splitMotionEntry),
3438 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 return;
3440 }
3441 }
3442
3443 // Not splitting. Enqueue dispatch entries for the event as is.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003444 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry,
3445 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446}
3447
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003448void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked(
3449 nsecs_t currentTime, const std::shared_ptr<Connection>& connection,
3450 std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003451 ATRACE_NAME_IF(ATRACE_ENABLED(),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003452 StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, "
3453 "id=0x%" PRIx32 ")",
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003454 connection->getInputChannelName().c_str(), eventEntry->id));
Michael Wright3dd60e22019-03-27 22:06:44 +00003455
hongzuo liu95785e22022-09-06 02:51:35 +00003456 const bool wasEmpty = connection->outboundQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003458 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
3460 // If the outbound queue was previously empty, start the dispatch cycle going.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003461 if (wasEmpty && !connection->outboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 startDispatchCycleLocked(currentTime, connection);
3463 }
3464}
3465
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003466void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003467 std::shared_ptr<const EventEntry> eventEntry,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003468 const InputTarget& inputTarget) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00003469 const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY ||
3470 eventEntry->type == EventEntry::Type::MOTION;
3471 if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) {
3472 LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: "
3473 << inputTarget << " connection: " << connection->getInputChannelName()
3474 << " entry: " << eventEntry->getDescription();
3475 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476 // This is a new event.
3477 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003478 std::unique_ptr<DispatchEntry> dispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003479 createDispatchEntry(mIdGenerator, inputTarget, eventEntry, inputTarget.flags,
Prabir Pradhana67623c2024-02-21 06:57:36 +00003480 mWindowInfosVsyncId, mTracer.get());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003482 // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a
3483 // different EventEntry than what was passed in.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003484 eventEntry = dispatchEntry->eventEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485 // Apply target flags and update the connection's input state.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003486 switch (eventEntry->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003487 case EventEntry::Type::KEY: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003488 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry);
3489 if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003490 LOG(WARNING) << "channel " << connection->getInputChannelName()
3491 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003492 return; // skip the inconsistent event
3493 }
3494 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003497 case EventEntry::Type::MOTION: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003498 std::shared_ptr<const MotionEntry> resolvedMotion =
3499 std::static_pointer_cast<const MotionEntry>(eventEntry);
3500 {
3501 // Determine the resolved motion entry.
3502 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
3503 int32_t resolvedAction = motionEntry.action;
3504 int32_t resolvedFlags = motionEntry.flags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003506 if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003507 resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003508 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003509 resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003510 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003511 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003512 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003513 resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003514 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003515 resolvedAction = AMOTION_EVENT_ACTION_DOWN;
3516 }
3517 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE &&
3518 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source,
3519 motionEntry.displayId)) {
3520 if (DEBUG_DISPATCH_CYCLE) {
3521 LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str()
3522 << "' ~ enqueueDispatchEntryLocked: filling in missing hover "
3523 "enter event";
3524 }
3525 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
3526 }
3527
3528 if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3529 resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED;
3530 }
3531 if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) {
3532 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
3533 }
3534 if (dispatchEntry->targetFlags.test(
3535 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) {
3536 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
3537 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003538 if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) {
3539 resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
3540 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003541
3542 dispatchEntry->resolvedFlags = resolvedFlags;
3543 if (resolvedAction != motionEntry.action) {
Siarhei Vishniakoue9ef6bc2023-12-21 19:47:20 -08003544 std::optional<std::vector<PointerProperties>> usingProperties;
3545 std::optional<std::vector<PointerCoords>> usingCoords;
3546 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT ||
3547 resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3548 // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by
3549 // the dispatcher, and therefore the coordinates of this event are currently
3550 // incorrect. These events should use the coordinates of the last dispatched
3551 // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data.
3552 const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT;
3553 std::optional<std::pair<std::vector<PointerProperties>,
3554 std::vector<PointerCoords>>>
3555 pointerInfo =
3556 connection->inputState.getPointersOfLastEvent(motionEntry,
3557 hovering);
3558 if (pointerInfo) {
3559 usingProperties = pointerInfo->first;
3560 usingCoords = pointerInfo->second;
3561 }
3562 }
Prabir Pradhana67623c2024-02-21 06:57:36 +00003563 {
3564 // Generate a new MotionEntry with a new eventId using the resolved action
3565 // and flags, and set it as the resolved entry.
3566 auto newEntry = std::make_shared<
3567 MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState,
3568 motionEntry.eventTime, motionEntry.deviceId,
3569 motionEntry.source, motionEntry.displayId,
3570 motionEntry.policyFlags, resolvedAction,
3571 motionEntry.actionButton, resolvedFlags,
3572 motionEntry.metaState, motionEntry.buttonState,
3573 motionEntry.classification, motionEntry.edgeFlags,
3574 motionEntry.xPrecision, motionEntry.yPrecision,
3575 motionEntry.xCursorPosition,
3576 motionEntry.yCursorPosition, motionEntry.downTime,
3577 usingProperties.value_or(
3578 motionEntry.pointerProperties),
3579 usingCoords.value_or(motionEntry.pointerCoords));
3580 if (mTracer) {
3581 ensureEventTraced(motionEntry);
3582 newEntry->traceTracker =
3583 mTracer->traceDerivedEvent(*newEntry,
3584 *motionEntry.traceTracker);
3585 }
3586 resolvedMotion = newEntry;
3587 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003588 if (ATRACE_ENABLED()) {
3589 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32
3590 ") to MotionEvent(id=0x%" PRIx32 ").",
3591 motionEntry.id, resolvedMotion->id);
3592 ATRACE_NAME(message.c_str());
3593 }
3594
3595 // Set the resolved motion entry in the DispatchEntry.
3596 dispatchEntry->eventEntry = resolvedMotion;
3597 eventEntry = resolvedMotion;
3598 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003599 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003601 // Check if we need to cancel any of the ongoing gestures. We don't support multiple
3602 // devices being active at the same time in the same window, so if a new device is
3603 // active, cancel the gesture from the old device.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003604 std::unique_ptr<EventEntry> cancelEvent =
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003605 connection->inputState.cancelConflictingInputStream(*resolvedMotion);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003606 if (cancelEvent != nullptr) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003607 LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in "
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003608 << connection->getInputChannelName() << " with event "
3609 << cancelEvent->getDescription();
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003610 if (mTracer) {
3611 static_cast<MotionEntry&>(*cancelEvent).traceTracker =
3612 mTracer->traceDerivedEvent(*cancelEvent, *resolvedMotion->traceTracker);
3613 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003614 std::unique_ptr<DispatchEntry> cancelDispatchEntry =
Prabir Pradhanc88b1fa2024-01-23 21:48:03 +00003615 createDispatchEntry(mIdGenerator, inputTarget, std::move(cancelEvent),
Prabir Pradhana67623c2024-02-21 06:57:36 +00003616 ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId,
3617 mTracer.get());
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003618
3619 // Send these cancel events to the queue before sending the event from the new
3620 // device.
3621 connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry));
3622 }
3623
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003624 if (!connection->inputState.trackMotion(*resolvedMotion,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003625 dispatchEntry->resolvedFlags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003626 LOG(WARNING) << "channel " << connection->getInputChannelName()
3627 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003628 return; // skip the inconsistent event
3629 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003630 if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) &&
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003631 (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003632 // Skip reporting pointer down outside focus to the policy.
3633 break;
3634 }
3635
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003636 dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003637 inputTarget.connection->getToken());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003638
3639 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003641 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003642 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08003643 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
3644 case EventEntry::Type::DRAG: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003645 break;
3646 }
Chris Yef59a2f42020-10-16 12:55:26 -07003647 case EventEntry::Type::SENSOR: {
3648 LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel");
3649 break;
3650 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003651 case EventEntry::Type::DEVICE_RESET: {
3652 LOG_ALWAYS_FATAL("%s events should not go to apps",
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003653 ftl::enum_string(eventEntry->type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003654 break;
3655 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 }
3657
3658 // Remember that we are waiting for this dispatch to complete.
3659 if (dispatchEntry->hasForegroundTarget()) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003660 incrementPendingForegroundDispatches(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661 }
3662
3663 // Enqueue the dispatch entry.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003664 connection->outboundQueue.emplace_back(std::move(dispatchEntry));
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003665 traceOutboundQueueLength(*connection);
chaviw8c9cf542019-03-25 13:02:48 -07003666}
3667
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003668/**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003669 * This function is for debugging and metrics collection. It has two roles.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003670 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003671 * The first role is to log input interaction with windows, which helps determine what the user was
3672 * interacting with. For example, if user is touching launcher, we will see an input_interaction log
3673 * that user started interacting with launcher window, as well as any other window that received
3674 * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged
3675 * when the set of tokens that received the event changes. It is not logged again as long as the
3676 * user is interacting with the same windows.
3677 *
3678 * The second role is to track input device activity for metrics collection. For each input event,
3679 * we report the set of UIDs that the input device interacted with to the policy. Unlike for the
3680 * input_interaction logs, the device interaction is reported even when the set of interaction
3681 * tokens do not change.
3682 *
3683 * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as
3684 * interaction. This includes up and cancel events for both keys and motions.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003685 */
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003686void InputDispatcher::processInteractionsLocked(const EventEntry& entry,
3687 const std::vector<InputTarget>& targets) {
3688 int32_t deviceId;
3689 nsecs_t eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003690 // Skip ACTION_UP events, and all events other than keys and motions
3691 if (entry.type == EventEntry::Type::KEY) {
3692 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
3693 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
3694 return;
3695 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003696 deviceId = keyEntry.deviceId;
3697 eventTime = keyEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003698 } else if (entry.type == EventEntry::Type::MOTION) {
3699 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
3700 if (motionEntry.action == AMOTION_EVENT_ACTION_UP ||
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003701 motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
3702 MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003703 return;
3704 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003705 deviceId = motionEntry.deviceId;
3706 eventTime = motionEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003707 } else {
3708 return; // Not a key or a motion
3709 }
3710
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003711 std::set<gui::Uid> interactionUids;
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07003712 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003713 std::vector<std::shared_ptr<Connection>> newConnections;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003714 for (const InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003715 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003716 continue; // Skip windows that receive ACTION_OUTSIDE
3717 }
3718
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003719 sp<IBinder> token = target.connection->getToken();
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003720 newConnectionTokens.insert(std::move(token));
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003721 newConnections.emplace_back(target.connection);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003722 if (target.windowHandle) {
3723 interactionUids.emplace(target.windowHandle->getInfo()->ownerUid);
3724 }
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003725 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003726
3727 auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]()
3728 REQUIRES(mLock) {
3729 scoped_unlock unlock(mLock);
3730 mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids);
3731 };
3732 postCommandLocked(std::move(command));
3733
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003734 if (newConnectionTokens == mInteractionConnectionTokens) {
3735 return; // no change
3736 }
3737 mInteractionConnectionTokens = newConnectionTokens;
3738
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003739 std::string targetList;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003740 for (const std::shared_ptr<Connection>& connection : newConnections) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08003741 targetList += connection->getInputChannelName() + ", ";
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003742 }
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003743 std::string message = "Interaction with: " + targetList;
3744 if (targetList.empty()) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003745 message += "<none>";
3746 }
3747 android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS;
3748}
3749
chaviwfd6d3512019-03-25 13:23:49 -07003750void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Vishnu Nairad321cd2020-08-20 16:40:21 -07003751 const sp<IBinder>& token) {
chaviw8c9cf542019-03-25 13:02:48 -07003752 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
chaviwfd6d3512019-03-25 13:23:49 -07003753 uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK;
3754 if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) {
chaviw8c9cf542019-03-25 13:02:48 -07003755 return;
3756 }
3757
Vishnu Nairc519ff72021-01-21 08:23:08 -08003758 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07003759 if (focusedToken == token) {
3760 // ignore since token is focused
chaviw8c9cf542019-03-25 13:02:48 -07003761 return;
3762 }
3763
Prabir Pradhancef936d2021-07-21 16:17:52 +00003764 auto command = [this, token]() REQUIRES(mLock) {
3765 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003766 mPolicy.onPointerDownOutsideFocus(token);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003767 };
3768 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769}
3770
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003771status_t InputDispatcher::publishMotionEvent(Connection& connection,
3772 DispatchEntry& dispatchEntry) const {
3773 const EventEntry& eventEntry = *(dispatchEntry.eventEntry);
3774 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3775
3776 PointerCoords scaledCoords[MAX_POINTERS];
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003777 const PointerCoords* usingCoords = motionEntry.pointerCoords.data();
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003778
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003779 // TODO(b/316355518): Do not modify coords before dispatch.
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003780 // Set the X and Y offset and X and Y scale depending on the input source.
3781 if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) &&
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003782 !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003783 float globalScaleFactor = dispatchEntry.globalScaleFactor;
3784 if (globalScaleFactor != 1.0f) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003785 for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003786 scaledCoords[i] = motionEntry.pointerCoords[i];
3787 // Don't apply window scale here since we don't want scale to affect raw
3788 // coordinates. The scale will be sent back to the client and applied
3789 // later when requesting relative coordinates.
Harry Cutts33476232023-01-30 19:57:29 +00003790 scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003791 }
3792 usingCoords = scaledCoords;
3793 }
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003794 }
3795
3796 std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry);
3797
3798 // Publish the motion event.
3799 return connection.inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003800 .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId,
3801 motionEntry.source, motionEntry.displayId, std::move(hmac),
3802 motionEntry.action, motionEntry.actionButton,
3803 dispatchEntry.resolvedFlags, motionEntry.edgeFlags,
3804 motionEntry.metaState, motionEntry.buttonState,
3805 motionEntry.classification, dispatchEntry.transform,
3806 motionEntry.xPrecision, motionEntry.yPrecision,
3807 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
3808 dispatchEntry.rawTransform, motionEntry.downTime,
3809 motionEntry.eventTime, motionEntry.getPointerCount(),
3810 motionEntry.pointerProperties.data(), usingCoords);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003811}
3812
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003814 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003815 ATRACE_NAME_IF(ATRACE_ENABLED(),
3816 StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
3817 connection->getInputChannelName().c_str()));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003818 if (DEBUG_DISPATCH_CYCLE) {
3819 ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str());
3820 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003822 while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003823 std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824 dispatchEntry->deliveryTime = currentTime;
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003825 const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection);
Siarhei Vishniakou70622952020-07-30 11:17:23 -05003826 dispatchEntry->timeoutTime = currentTime + timeout.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827
3828 // Publish the event.
3829 status_t status;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003830 const EventEntry& eventEntry = *(dispatchEntry->eventEntry);
3831 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003832 case EventEntry::Type::KEY: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003833 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3834 std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry);
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003835 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003836 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3837 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003838 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003840 // Publish the key event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003841 status = connection->inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003842 .publishKeyEvent(dispatchEntry->seq, keyEntry.id,
3843 keyEntry.deviceId, keyEntry.source,
3844 keyEntry.displayId, std::move(hmac),
3845 keyEntry.action, dispatchEntry->resolvedFlags,
3846 keyEntry.keyCode, keyEntry.scanCode,
3847 keyEntry.metaState, keyEntry.repeatCount,
3848 keyEntry.downTime, keyEntry.eventTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003849 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003850 ensureEventTraced(keyEntry);
3851 mTracer->traceEventDispatch(*dispatchEntry, *keyEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003852 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003853 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 }
3855
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003856 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003857 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003858 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3859 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003860 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003861 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003862 status = publishMotionEvent(*connection, *dispatchEntry);
Siarhei Vishniakoub0058742024-07-01 16:42:49 -07003863 if (status == BAD_VALUE) {
3864 logDispatchStateLocked();
3865 LOG(FATAL) << "Publisher failed for " << motionEntry;
3866 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003867 if (mTracer) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00003868 ensureEventTraced(motionEntry);
3869 mTracer->traceEventDispatch(*dispatchEntry, *motionEntry.traceTracker);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003870 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003871 break;
3872 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003873
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003874 case EventEntry::Type::FOCUS: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003875 const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003876 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003877 focusEntry.id,
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07003878 focusEntry.hasFocus);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003879 break;
3880 }
3881
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003882 case EventEntry::Type::TOUCH_MODE_CHANGED: {
3883 const TouchModeEntry& touchModeEntry =
3884 static_cast<const TouchModeEntry&>(eventEntry);
3885 status = connection->inputPublisher
3886 .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id,
3887 touchModeEntry.inTouchMode);
3888
3889 break;
3890 }
3891
Prabir Pradhan99987712020-11-10 18:43:05 -08003892 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
3893 const auto& captureEntry =
3894 static_cast<const PointerCaptureChangedEntry&>(eventEntry);
Hiroki Sato25040232024-02-22 17:21:22 +09003895 status =
3896 connection->inputPublisher
3897 .publishCaptureEvent(dispatchEntry->seq, captureEntry.id,
3898 captureEntry.pointerCaptureRequest.isEnable());
Prabir Pradhan99987712020-11-10 18:43:05 -08003899 break;
3900 }
3901
arthurhungb89ccb02020-12-30 16:19:01 +08003902 case EventEntry::Type::DRAG: {
3903 const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry);
3904 status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq,
3905 dragEntry.id, dragEntry.x,
3906 dragEntry.y,
3907 dragEntry.isExiting);
3908 break;
3909 }
3910
Chris Yef59a2f42020-10-16 12:55:26 -07003911 case EventEntry::Type::DEVICE_RESET:
3912 case EventEntry::Type::SENSOR: {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003913 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events",
Dominik Laskowski75788452021-02-09 18:51:25 -08003914 ftl::enum_string(eventEntry.type).c_str());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003915 return;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003916 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 }
3918
3919 // Check the result.
3920 if (status) {
3921 if (status == WOULD_BLOCK) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003922 if (connection->waitQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 ALOGE("channel '%s' ~ Could not publish event because the pipe is full. "
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003924 "This is unexpected because the wait queue is empty, so the pipe "
3925 "should be empty and we shouldn't have any problems writing an "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003926 "event to it, status=%s(%d)",
3927 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3928 status);
Harry Cutts33476232023-01-30 19:57:29 +00003929 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930 } else {
3931 // Pipe is full and we are waiting for the app to finish process some events
3932 // before sending more events to it.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003933 if (DEBUG_DISPATCH_CYCLE) {
3934 ALOGD("channel '%s' ~ Could not publish event because the pipe is full, "
3935 "waiting for the application to catch up",
3936 connection->getInputChannelName().c_str());
3937 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938 }
3939 } else {
3940 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003941 "status=%s(%d)",
3942 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3943 status);
Harry Cutts33476232023-01-30 19:57:29 +00003944 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 }
3946 return;
3947 }
3948
3949 // Re-enqueue the event on the wait queue.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003950 const nsecs_t timeoutTime = dispatchEntry->timeoutTime;
3951 connection->waitQueue.emplace_back(std::move(dispatchEntry));
3952 connection->outboundQueue.erase(connection->outboundQueue.begin());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003953 traceOutboundQueueLength(*connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003954 if (connection->responsive) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003955 mAnrTracker.insert(timeoutTime, connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003956 }
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003957 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 }
3959}
3960
chaviw09c8d2d2020-08-24 15:48:26 -07003961std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const {
3962 size_t size;
3963 switch (event.type) {
3964 case VerifiedInputEvent::Type::KEY: {
3965 size = sizeof(VerifiedKeyEvent);
3966 break;
3967 }
3968 case VerifiedInputEvent::Type::MOTION: {
3969 size = sizeof(VerifiedMotionEvent);
3970 break;
3971 }
3972 }
3973 const uint8_t* start = reinterpret_cast<const uint8_t*>(&event);
3974 return mHmacKeyManager.sign(start, size);
3975}
3976
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003977const std::array<uint8_t, 32> InputDispatcher::getSignature(
3978 const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003979 const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003980 if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) {
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003981 // Only sign events up and down events as the purely move events
3982 // are tied to their up/down counterparts so signing would be redundant.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003983 return INVALID_HMAC;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003984 }
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003985
3986 VerifiedMotionEvent verifiedEvent =
3987 verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform);
3988 verifiedEvent.actionMasked = actionMasked;
3989 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS;
3990 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003991}
3992
3993const std::array<uint8_t, 32> InputDispatcher::getSignature(
3994 const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const {
3995 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry);
3996 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS;
chaviw09c8d2d2020-08-24 15:48:26 -07003997 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003998}
3999
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004001 const std::shared_ptr<Connection>& connection,
4002 uint32_t seq, bool handled, nsecs_t consumeTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004003 if (DEBUG_DISPATCH_CYCLE) {
4004 ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s",
4005 connection->getInputChannelName().c_str(), seq, toString(handled));
4006 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00004008 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 return;
4010 }
4011
4012 // Notify other system components and prepare to start the next dispatch cycle.
Prabir Pradhancef936d2021-07-21 16:17:52 +00004013 auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) {
4014 doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime);
4015 };
4016 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
4019void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004020 const std::shared_ptr<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004021 bool notify) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004022 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004023 LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__
4024 << " - notify=" << toString(notify);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004025 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 // Clear the dispatch queues.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004028 drainDispatchQueue(connection->outboundQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00004029 traceOutboundQueueLength(*connection);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004030 drainDispatchQueue(connection->waitQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00004031 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032
4033 // The connection appears to be unrecoverably broken.
4034 // Ignore already broken or zombie connections.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08004035 if (connection->status == Connection::Status::NORMAL) {
4036 connection->status = Connection::Status::BROKEN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037
4038 if (notify) {
4039 // Notify other system components.
Prabir Pradhancef936d2021-07-21 16:17:52 +00004040 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
4041 connection->getInputChannelName().c_str());
4042
4043 auto command = [this, connection]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004044 scoped_unlock unlock(mLock);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004045 mPolicy.notifyInputChannelBroken(connection->getToken());
Prabir Pradhancef936d2021-07-21 16:17:52 +00004046 };
4047 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 }
4049 }
4050}
4051
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004052void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004053 while (!queue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004054 releaseDispatchEntry(std::move(queue.front()));
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07004055 queue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 }
4057}
4058
Prabir Pradhan8c90d782023-09-15 21:16:44 +00004059void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 if (dispatchEntry->hasForegroundTarget()) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004061 decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063}
4064
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004065int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) {
4066 std::scoped_lock _l(mLock);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004067 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004068 if (connection == nullptr) {
4069 ALOGW("Received looper callback for unknown input channel token %p. events=0x%x",
4070 connectionToken.get(), events);
4071 return 0; // remove the callback
4072 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004074 bool notify;
4075 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
4076 if (!(events & ALOOPER_EVENT_INPUT)) {
4077 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
4078 "events=0x%x",
4079 connection->getInputChannelName().c_str(), events);
4080 return 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 }
4082
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004083 nsecs_t currentTime = now();
4084 bool gotOne = false;
4085 status_t status = OK;
4086 for (;;) {
4087 Result<InputPublisher::ConsumerResponse> result =
4088 connection->inputPublisher.receiveConsumerResponse();
4089 if (!result.ok()) {
4090 status = result.error().code();
4091 break;
4092 }
4093
4094 if (std::holds_alternative<InputPublisher::Finished>(*result)) {
4095 const InputPublisher::Finished& finish =
4096 std::get<InputPublisher::Finished>(*result);
4097 finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled,
4098 finish.consumeTime);
4099 } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004100 if (shouldReportMetricsForConnection(*connection)) {
4101 const InputPublisher::Timeline& timeline =
4102 std::get<InputPublisher::Timeline>(*result);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004103 mLatencyTracker.trackGraphicsLatency(timeline.inputEventId,
4104 connection->getToken(),
4105 std::move(timeline.graphicsTimeline));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00004106 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004107 }
4108 gotOne = true;
4109 }
4110 if (gotOne) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00004111 runCommandsLockedInterruptable();
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004112 if (status == WOULD_BLOCK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 return 1;
4114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 }
4116
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004117 notify = status != DEAD_OBJECT || !connection->monitor;
4118 if (notify) {
4119 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)",
4120 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
4121 status);
4122 }
4123 } else {
4124 // Monitor channels are never explicitly unregistered.
4125 // We do it automatically when the remote endpoint is closed so don't warn about them.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004126 const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004127 notify = !connection->monitor && stillHaveWindowHandle;
4128 if (notify) {
4129 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x",
4130 connection->getInputChannelName().c_str(), events);
4131 }
4132 }
4133
4134 // Remove the channel.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004135 removeInputChannelLocked(connection->getToken(), notify);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004136 return 0; // remove the callback
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137}
4138
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004139void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 const CancelationOptions& options) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004141 // Cancel windows (i.e. non-monitors).
4142 // A channel must have at least one window to receive any input. If a window was removed, the
4143 // event streams directed to the window will already have been canceled during window removal.
4144 // So there is no need to generate cancellations for connections without any windows.
4145 const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode);
4146 // Generate cancellations for touched windows first. This is to avoid generating cancellations
4147 // through a non-touched window if there are more than one window for an input channel.
4148 if (cancelPointers) {
4149 for (const auto& [displayId, touchState] : mTouchStatesByDisplay) {
4150 if (options.displayId.has_value() && options.displayId != displayId) {
4151 continue;
4152 }
4153 for (const auto& touchedWindow : touchState.windows) {
4154 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
4155 }
4156 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 }
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004158 // Follow up by generating cancellations for all windows, because we don't explicitly track
4159 // the windows that have an ongoing focus event stream.
4160 if (cancelNonPointers) {
4161 for (const auto& [_, handles] : mWindowHandlesByDisplay) {
4162 for (const auto& windowHandle : handles) {
4163 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4164 }
4165 }
4166 }
4167
4168 // Cancel monitors.
4169 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170}
4171
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004172void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked(
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004173 const CancelationOptions& options) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004174 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00004175 for (const Monitor& monitor : monitors) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004176 synthesizeCancelationEventsForConnectionLocked(monitor.connection, options,
4177 /*window=*/nullptr);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004178 }
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004179 }
4180}
4181
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004182void InputDispatcher::synthesizeCancelationEventsForWindowLocked(
4183 const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options,
4184 const std::shared_ptr<Connection>& connection) {
4185 if (windowHandle == nullptr) {
4186 LOG(FATAL) << __func__ << ": Window handle must not be null";
4187 }
4188 if (connection) {
4189 // The connection can be optionally provided to avoid multiple lookups.
4190 if (windowHandle->getToken() != connection->getToken()) {
4191 LOG(FATAL) << __func__
4192 << ": Wrong connection provided for window: " << windowHandle->getName();
4193 }
4194 }
4195
4196 std::shared_ptr<Connection> resolvedConnection =
4197 connection ? connection : getConnectionLocked(windowHandle->getToken());
4198 if (!resolvedConnection) {
4199 LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName();
4200 return;
4201 }
4202 synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle);
4203}
4204
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004206 const std::shared_ptr<Connection>& connection, const CancelationOptions& options,
4207 const sp<WindowInfoHandle>& window) {
4208 if (!connection->monitor && window == nullptr) {
4209 LOG(FATAL) << __func__
4210 << ": Cannot send event to non-monitor channel without a window - channel: "
4211 << connection->getInputChannelName();
4212 }
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00004213 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 return;
4215 }
4216
4217 nsecs_t currentTime = now();
4218
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004219 std::vector<std::unique_ptr<EventEntry>> cancelationEvents =
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -07004220 connection->inputState.synthesizeCancelationEvents(currentTime, options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004222 if (cancelationEvents.empty()) {
4223 return;
4224 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004225
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004226 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
4227 ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync "
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004228 "with reality: %s, mode=%s.",
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004229 connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004230 ftl::enum_string(options.mode).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004231 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004232
Arthur Hungb3307ee2021-10-14 10:57:37 +00004233 std::string reason = std::string("reason=").append(options.reason);
4234 android_log_event_list(LOGTAG_INPUT_CANCEL)
4235 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
4236
hongzuo liu95785e22022-09-06 02:51:35 +00004237 const bool wasEmpty = connection->outboundQueue.empty();
Prabir Pradhan16463382023-10-12 23:03:19 +00004238 // The target to use if we don't find a window associated with the channel.
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004239 const InputTarget fallbackTarget{connection};
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004240 const auto& token = connection->getToken();
hongzuo liu95785e22022-09-06 02:51:35 +00004241
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004242 for (size_t i = 0; i < cancelationEvents.size(); i++) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004243 std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004244 std::vector<InputTarget> targets{};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004245
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004246 switch (cancelationEventEntry->type) {
4247 case EventEntry::Type::KEY: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004248 if (mTracer) {
4249 static_cast<KeyEntry&>(*cancelationEventEntry).traceTracker =
4250 mTracer->traceDerivedEvent(*cancelationEventEntry,
4251 *options.traceTracker);
4252 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004253 const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004254 if (window) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004255 addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4256 /*targetFlags=*/{}, keyEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004257 } else {
4258 targets.emplace_back(fallbackTarget);
4259 }
4260 logOutboundKeyDetails("cancel - ", keyEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004261 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004263 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004264 if (mTracer) {
4265 static_cast<MotionEntry&>(*cancelationEventEntry).traceTracker =
4266 mTracer->traceDerivedEvent(*cancelationEventEntry,
4267 *options.traceTracker);
4268 }
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004269 const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004270 if (window) {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004271 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004272 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004273 pointerIndex++) {
4274 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4275 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004276 if (mDragState && mDragState->dragWindow->getToken() == token &&
4277 pointerIds.test(mDragState->pointerId)) {
4278 LOG(INFO) << __func__
4279 << ": Canceling drag and drop because the pointers for the drag "
4280 "window are being canceled.";
4281 sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0);
4282 mDragState.reset();
4283 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004284 addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4285 ftl::Flags<InputTarget::Flags>(), pointerIds,
4286 motionEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004287 } else {
4288 targets.emplace_back(fallbackTarget);
4289 const auto it = mDisplayInfos.find(motionEntry.displayId);
4290 if (it != mDisplayInfos.end()) {
4291 targets.back().displayTransform = it->second.transform;
4292 targets.back().setDefaultPointerTransform(it->second.transform);
4293 }
4294 }
4295 logOutboundMotionDetails("cancel - ", motionEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004296 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 }
Prabir Pradhan99987712020-11-10 18:43:05 -08004298 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004299 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004300 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
4301 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08004302 LOG_ALWAYS_FATAL("Canceling %s events is not supported",
Dominik Laskowski75788452021-02-09 18:51:25 -08004303 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004304 break;
4305 }
Chris Yef59a2f42020-10-16 12:55:26 -07004306 case EventEntry::Type::DEVICE_RESET:
4307 case EventEntry::Type::SENSOR: {
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004308 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004309 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004310 break;
4311 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 }
4313
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004314 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004315 if (mTracer) {
4316 mTracer->dispatchToTargetHint(*options.traceTracker, targets[0]);
4317 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004318 enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004320
hongzuo liu95785e22022-09-06 02:51:35 +00004321 // If the outbound queue was previously empty, start the dispatch cycle going.
4322 if (wasEmpty && !connection->outboundQueue.empty()) {
4323 startDispatchCycleLocked(currentTime, connection);
4324 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325}
4326
Svet Ganov5d3bc372020-01-26 23:11:07 -08004327void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004328 const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004329 ftl::Flags<InputTarget::Flags> targetFlags,
4330 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00004331 if (connection->status != Connection::Status::NORMAL) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004332 return;
4333 }
4334
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004335 std::vector<std::unique_ptr<EventEntry>> downEvents =
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004336 connection->inputState.synthesizePointerDownEvents(downTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004337
4338 if (downEvents.empty()) {
4339 return;
4340 }
4341
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004342 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004343 ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.",
4344 connection->getInputChannelName().c_str(), downEvents.size());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004345 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004346
Prabir Pradhanfeca0572024-02-06 00:43:11 +00004347 const auto [_, touchedWindowState, displayId] =
4348 findTouchStateWindowAndDisplayLocked(connection->getToken());
4349 if (touchedWindowState == nullptr) {
4350 LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
4351 }
4352 const auto& windowHandle = touchedWindowState->windowHandle;
Svet Ganov5d3bc372020-01-26 23:11:07 -08004353
hongzuo liu95785e22022-09-06 02:51:35 +00004354 const bool wasEmpty = connection->outboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004355 for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004356 std::vector<InputTarget> targets{};
Svet Ganov5d3bc372020-01-26 23:11:07 -08004357 switch (downEventEntry->type) {
4358 case EventEntry::Type::MOTION: {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004359 if (mTracer) {
4360 static_cast<MotionEntry&>(*downEventEntry).traceTracker =
4361 mTracer->traceDerivedEvent(*downEventEntry, *traceTracker);
4362 }
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004363 const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry);
4364 if (windowHandle != nullptr) {
4365 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004366 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004367 pointerIndex++) {
4368 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4369 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004370 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
4371 targetFlags, pointerIds, motionEntry.downTime,
4372 targets);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004373 } else {
Prabir Pradhanc32a4112024-01-23 23:20:37 +00004374 targets.emplace_back(connection, targetFlags);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004375 const auto it = mDisplayInfos.find(motionEntry.displayId);
4376 if (it != mDisplayInfos.end()) {
4377 targets.back().displayTransform = it->second.transform;
4378 targets.back().setDefaultPointerTransform(it->second.transform);
4379 }
4380 }
4381 logOutboundMotionDetails("down - ", motionEntry);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004382 break;
4383 }
4384
4385 case EventEntry::Type::KEY:
4386 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004387 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08004388 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07004389 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004390 case EventEntry::Type::SENSOR:
4391 case EventEntry::Type::DRAG: {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004392 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004393 ftl::enum_string(downEventEntry->type).c_str());
Svet Ganov5d3bc372020-01-26 23:11:07 -08004394 break;
4395 }
4396 }
4397
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004398 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhand6b2b052024-02-21 23:25:15 +00004399 if (mTracer) {
4400 mTracer->dispatchToTargetHint(*traceTracker, targets[0]);
4401 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004402 enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004403 }
4404
hongzuo liu95785e22022-09-06 02:51:35 +00004405 // If the outbound queue was previously empty, start the dispatch cycle going.
4406 if (wasEmpty && !connection->outboundQueue.empty()) {
4407 startDispatchCycleLocked(downTime, connection);
4408 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004409}
4410
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004411std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004412 const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds,
4413 nsecs_t splitDownTime) {
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004414 const auto& [action, pointerProperties, pointerCoords] =
4415 MotionEvent::split(originalMotionEntry.action, originalMotionEntry.flags,
4416 /*historySize=*/0, originalMotionEntry.pointerProperties,
4417 originalMotionEntry.pointerCoords, pointerIds);
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004418 if (pointerIds.count() != pointerCoords.size()) {
4419 // TODO(b/329107108): Determine why some IDs in pointerIds were not in originalMotionEntry.
4420 // This is bad. We are missing some of the pointers that we expected to deliver.
4421 // Most likely this indicates that we received an ACTION_MOVE events that has
4422 // different pointer ids than we expected based on the previous ACTION_DOWN
4423 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
4424 // in this way.
Siarhei Vishniakoued89cbb2023-10-13 10:42:44 -07004425 ALOGW("Dropping split motion event because the pointer count is %zu but "
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00004426 "we expected there to be %zu pointers. This probably means we received "
4427 "a broken sequence of pointer ids from the input device: %s",
4428 pointerCoords.size(), pointerIds.count(),
4429 originalMotionEntry.getDescription().c_str());
4430 return nullptr;
4431 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004433 // TODO(b/327503168): Move this check inside MotionEvent::split once all callers handle it
4434 // correctly.
Siarhei Vishniakou59e302b2023-06-05 08:04:53 -07004435 if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) {
4436 logDispatchStateLocked();
4437 LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for "
4438 "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64,
4439 originalMotionEntry.getDescription().c_str(), splitDownTime);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004440 }
4441
Garfield Tanff1f1bb2020-01-28 13:24:04 -08004442 int32_t newId = mIdGenerator.nextId();
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00004443 ATRACE_NAME_IF(ATRACE_ENABLED(),
4444 StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32
4445 ").",
4446 originalMotionEntry.id, newId));
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004447 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004448 std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState,
4449 originalMotionEntry.eventTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004450 originalMotionEntry.deviceId, originalMotionEntry.source,
4451 originalMotionEntry.displayId,
4452 originalMotionEntry.policyFlags, action,
4453 originalMotionEntry.actionButton,
4454 originalMotionEntry.flags, originalMotionEntry.metaState,
4455 originalMotionEntry.buttonState,
4456 originalMotionEntry.classification,
4457 originalMotionEntry.edgeFlags,
4458 originalMotionEntry.xPrecision,
4459 originalMotionEntry.yPrecision,
4460 originalMotionEntry.xCursorPosition,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004461 originalMotionEntry.yCursorPosition, splitDownTime,
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00004462 pointerProperties, pointerCoords);
Prabir Pradhana67623c2024-02-21 06:57:36 +00004463 if (mTracer) {
4464 splitMotionEntry->traceTracker =
4465 mTracer->traceDerivedEvent(*splitMotionEntry, *originalMotionEntry.traceTracker);
4466 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 return splitMotionEntry;
4469}
4470
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004471void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
4472 std::scoped_lock _l(mLock);
Liana Kazanova5b8217b2024-07-18 17:44:51 +00004473 // Reset key repeating in case a keyboard device was added or removed or something.
4474 resetKeyRepeatLocked();
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004475 mLatencyTracker.setInputDevices(args.inputDeviceInfos);
4476}
4477
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004478void InputDispatcher::notifyKey(const NotifyKeyArgs& args) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004479 ALOGD_IF(debugInboundEventDetails(),
4480 "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64
Linnan Li13bf76a2024-05-05 19:18:02 +08004481 ", deviceId=%d, source=%s, displayId=%s, policyFlags=0x%x, action=%s, flags=0x%x, "
4482 "keyCode=%s, scanCode=0x%x, metaState=0x%x, "
Prabir Pradhan96282b02023-02-24 22:36:17 +00004483 "downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004484 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004485 args.displayId.toString().c_str(), args.policyFlags,
4486 KeyEvent::actionToString(args.action), args.flags, KeyEvent::getLabel(args.keyCode),
4487 args.scanCode, args.metaState, args.downTime);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004488 Result<void> keyCheck = validateKeyEvent(args.action);
4489 if (!keyCheck.ok()) {
4490 LOG(ERROR) << "invalid key event: " << keyCheck.error();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 return;
4492 }
4493
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004494 uint32_t policyFlags = args.policyFlags;
4495 int32_t flags = args.flags;
4496 int32_t metaState = args.metaState;
Siarhei Vishniakou622bd322018-10-29 18:02:27 -07004497 // InputDispatcher tracks and generates key repeats on behalf of
4498 // whatever notifies it, so repeatCount should always be set to 0
4499 constexpr int32_t repeatCount = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
4501 policyFlags |= POLICY_FLAG_VIRTUAL;
4502 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
4503 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 if (policyFlags & POLICY_FLAG_FUNCTION) {
4505 metaState |= AMETA_FUNCTION_ON;
4506 }
4507
4508 policyFlags |= POLICY_FLAG_TRUSTED;
4509
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004510 int32_t keyCode = args.keyCode;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 KeyEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004512 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action,
4513 flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime,
4514 args.eventTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
Michael Wright2b3c3302018-03-02 17:19:13 +00004516 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004517 mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004518 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4519 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004520 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004521 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522
Antonio Kantekf16f2832021-09-28 04:39:20 +00004523 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 { // acquire lock
4525 mLock.lock();
4526
Asmita Poddar2808d7e2024-09-12 13:15:38 +00004527 if (input_flags::keyboard_repeat_keys() && !mConfig.keyRepeatEnabled) {
4528 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
4529 }
4530
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 if (shouldSendKeyToInputFilterLocked(args)) {
4532 mLock.unlock();
4533
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004534 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004535 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 return; // event was consumed by the filter
4537 }
4538
4539 mLock.lock();
4540 }
4541
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004542 std::unique_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004543 std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4544 args.deviceId, args.source, args.displayId, policyFlags,
4545 args.action, flags, keyCode, args.scanCode, metaState,
4546 repeatCount, args.downTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004547 if (mTracer) {
4548 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4549 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
jioana099e19a2024-09-19 15:37:34 +00004551 if (input_flags::enable_per_device_input_latency_metrics()) {
4552 if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
4553 IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
4554 !mInputFilterEnabled) {
4555 mLatencyTracker.trackNotifyKey(args);
4556 }
4557 }
4558
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004559 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 mLock.unlock();
4561 } // release lock
4562
4563 if (needWake) {
4564 mLooper->wake();
4565 }
4566}
4567
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004568bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 return mInputFilterEnabled;
4570}
4571
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004572void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004573 if (debugInboundEventDetails()) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004574 ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08004575 "displayId=%s, policyFlags=0x%x, "
Siarhei Vishniakou6ebd0692022-10-20 15:05:45 -07004576 "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, "
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07004577 "xCursorPosition=%f, yCursorPosition=%f, downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004578 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08004579 args.displayId.toString().c_str(), args.policyFlags,
4580 MotionEvent::actionToString(args.action).c_str(), args.actionButton, args.flags,
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -07004581 args.metaState, args.buttonState, args.xCursorPosition, args.yCursorPosition,
4582 args.downTime);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004583 for (uint32_t i = 0; i < args.getPointerCount(); i++) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004584 ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, "
4585 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004586 i, args.pointerProperties[i].id,
4587 ftl::enum_string(args.pointerProperties[i].toolType).c_str(),
4588 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
4589 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
4590 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
4591 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
4592 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
4593 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
4594 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
4595 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
4596 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004597 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004599
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004600 Result<void> motionCheck =
4601 validateMotionEvent(args.action, args.actionButton, args.getPointerCount(),
4602 args.pointerProperties.data());
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004603 if (!motionCheck.ok()) {
4604 LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error();
4605 return;
4606 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004608 if (DEBUG_VERIFY_EVENTS) {
4609 auto [it, _] =
4610 mVerifiersByDisplay.try_emplace(args.displayId,
Linnan Li13bf76a2024-05-05 19:18:02 +08004611 StringPrintf("display %s",
4612 args.displayId.toString().c_str()));
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004613 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -07004614 it->second.processMovement(args.deviceId, args.source, args.action,
4615 args.getPointerCount(), args.pointerProperties.data(),
4616 args.pointerCoords.data(), args.flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004617 if (!result.ok()) {
4618 LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump();
4619 }
4620 }
4621
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004622 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 policyFlags |= POLICY_FLAG_TRUSTED;
Michael Wright2b3c3302018-03-02 17:19:13 +00004624
4625 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004626 mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime,
4627 policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004628 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4629 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004630 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004631 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
Antonio Kantekf16f2832021-09-28 04:39:20 +00004633 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 { // acquire lock
4635 mLock.lock();
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004636 if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
4637 // Set the flag anyway if we already have an ongoing gesture. That would allow us to
4638 // complete the processing of the current stroke.
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004639 const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004640 if (touchStateIt != mTouchStatesByDisplay.end()) {
4641 const TouchState& touchState = touchStateIt->second;
Linnan Li907ae732023-09-05 17:14:21 +08004642 if (touchState.hasTouchingPointers(args.deviceId) ||
4643 touchState.hasHoveringPointers(args.deviceId)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004644 policyFlags |= POLICY_FLAG_PASS_TO_USER;
4645 }
4646 }
4647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
4649 if (shouldSendMotionToInputFilterLocked(args)) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004650 ui::Transform displayTransform;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004651 if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004652 displayTransform = it->second.transform;
4653 }
4654
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 mLock.unlock();
4656
4657 MotionEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004658 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC,
4659 args.action, args.actionButton, args.flags, args.edgeFlags,
4660 args.metaState, args.buttonState, args.classification,
4661 displayTransform, args.xPrecision, args.yPrecision,
4662 args.xCursorPosition, args.yCursorPosition, displayTransform,
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004663 args.downTime, args.eventTime, args.getPointerCount(),
4664 args.pointerProperties.data(), args.pointerCoords.data());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665
4666 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004667 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 return; // event was consumed by the filter
4669 }
4670
4671 mLock.lock();
4672 }
4673
4674 // Just enqueue a new motion event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004675 std::unique_ptr<MotionEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004676 std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4677 args.deviceId, args.source, args.displayId,
4678 policyFlags, args.action, args.actionButton,
4679 args.flags, args.metaState, args.buttonState,
4680 args.classification, args.edgeFlags, args.xPrecision,
4681 args.yPrecision, args.xCursorPosition,
4682 args.yCursorPosition, args.downTime,
4683 args.pointerProperties, args.pointerCoords);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004684 if (mTracer) {
4685 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4686 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004688 if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
4689 IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004690 !mInputFilterEnabled) {
jioana099e19a2024-09-19 15:37:34 +00004691 mLatencyTracker.trackNotifyMotion(args);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004692 }
4693
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004694 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 mLock.unlock();
4696 } // release lock
4697
4698 if (needWake) {
4699 mLooper->wake();
4700 }
4701}
4702
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004703void InputDispatcher::notifySensor(const NotifySensorArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004704 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004705 ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, "
4706 " sensorType=%s",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004707 args.id, args.eventTime, args.deviceId, args.source,
4708 ftl::enum_string(args.sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004709 }
Chris Yef59a2f42020-10-16 12:55:26 -07004710
Antonio Kantekf16f2832021-09-28 04:39:20 +00004711 bool needWake = false;
Chris Yef59a2f42020-10-16 12:55:26 -07004712 { // acquire lock
4713 mLock.lock();
4714
4715 // Just enqueue a new sensor event.
4716 std::unique_ptr<SensorEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004717 std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source,
4718 /* policyFlags=*/0, args.hwTimestamp, args.sensorType,
4719 args.accuracy, args.accuracyChanged, args.values);
Chris Yef59a2f42020-10-16 12:55:26 -07004720
4721 needWake = enqueueInboundEventLocked(std::move(newEntry));
4722 mLock.unlock();
4723 } // release lock
4724
4725 if (needWake) {
4726 mLooper->wake();
4727 }
4728}
4729
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004730void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004731 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004732 ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime,
4733 args.deviceId, args.isOn);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004734 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00004735 mPolicy.notifyVibratorState(args.deviceId, args.isOn);
Chris Yefb552902021-02-03 17:18:37 -08004736}
4737
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004738bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) {
Jackal Guof9696682018-10-05 12:23:23 +08004739 return mInputFilterEnabled;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740}
4741
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004742void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004743 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004744 ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, "
4745 "switchMask=0x%08x",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004746 args.eventTime, args.policyFlags, args.switchValues, args.switchMask);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004747 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004749 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 policyFlags |= POLICY_FLAG_TRUSTED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004751 mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752}
4753
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004754void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004755 // TODO(b/308677868) Remove device reset from the InputListener interface
Prabir Pradhan65613802023-02-22 23:36:58 +00004756 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004757 ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime,
4758 args.deviceId);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004759 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760
Antonio Kantekf16f2832021-09-28 04:39:20 +00004761 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004763 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004765 std::unique_ptr<DeviceResetEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004766 std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004767 needWake = enqueueInboundEventLocked(std::move(newEntry));
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -07004768
4769 for (auto& [_, verifier] : mVerifiersByDisplay) {
4770 verifier.resetDevice(args.deviceId);
4771 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 } // release lock
4773
4774 if (needWake) {
4775 mLooper->wake();
4776 }
4777}
4778
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004779void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004780 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004781 ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime,
Hiroki Sato25040232024-02-22 17:21:22 +09004782 args.request.isEnable() ? "true" : "false");
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004783 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004784
Antonio Kantekf16f2832021-09-28 04:39:20 +00004785 bool needWake = false;
Prabir Pradhan99987712020-11-10 18:43:05 -08004786 { // acquire lock
4787 std::scoped_lock _l(mLock);
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004788 auto entry =
4789 std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004790 needWake = enqueueInboundEventLocked(std::move(entry));
4791 } // release lock
4792
4793 if (needWake) {
4794 mLooper->wake();
4795 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004796}
4797
Prabir Pradhan5735a322022-04-11 17:23:34 +00004798InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00004799 std::optional<gui::Uid> targetUid,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004800 InputEventInjectionSync syncMode,
4801 std::chrono::milliseconds timeout,
4802 uint32_t policyFlags) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004803 Result<void> eventValidation = validateInputEvent(*event);
4804 if (!eventValidation.ok()) {
4805 LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error();
4806 return InputEventInjectionResult::FAILED;
4807 }
4808
Prabir Pradhan65613802023-02-22 23:36:58 +00004809 if (debugInboundEventDetails()) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004810 LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString)
4811 << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count()
4812 << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec
4813 << ", event=" << *event;
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004814 }
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -07004815 nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816
Prabir Pradhan5735a322022-04-11 17:23:34 +00004817 policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004819 // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004820 // that have gone through the InputFilter. If the event passed through the InputFilter, assign
4821 // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
4822 // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
4823 // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
4824 // from events that originate from actual hardware.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -07004825 DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004826 if (policyFlags & POLICY_FLAG_FILTERED) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004827 resolvedDeviceId = event->getDeviceId();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004828 }
4829
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004830 const bool isAsync = syncMode == InputEventInjectionSync::NONE;
4831 auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync);
4832
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004833 std::queue<std::unique_ptr<EventEntry>> injectedEntries;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004835 case InputEventType::KEY: {
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004836 const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004837 const int32_t action = incomingKey.getAction();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004838 int32_t flags = incomingKey.getFlags();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004839 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4840 flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4841 }
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004842 int32_t keyCode = incomingKey.getKeyCode();
4843 int32_t metaState = incomingKey.getMetaState();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004844 KeyEvent keyEvent;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004845 keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(),
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004846 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode,
4847 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
4848 incomingKey.getDownTime(), incomingKey.getEventTime());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004850 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
4851 policyFlags |= POLICY_FLAG_VIRTUAL;
Michael Wright2b3c3302018-03-02 17:19:13 +00004852 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004853
4854 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
4855 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004856 mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004857 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4858 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
4859 std::to_string(t.duration().count()).c_str());
4860 }
4861 }
4862
4863 mLock.lock();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004864 std::unique_ptr<KeyEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004865 std::make_unique<KeyEntry>(incomingKey.getId(), injectionState,
4866 incomingKey.getEventTime(), resolvedDeviceId,
4867 incomingKey.getSource(), incomingKey.getDisplayId(),
4868 policyFlags, action, flags, keyCode,
4869 incomingKey.getScanCode(), metaState,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004870 incomingKey.getRepeatCount(),
4871 incomingKey.getDownTime());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004872 if (mTracer) {
4873 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4874 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004875 injectedEntries.push(std::move(injectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004876 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 }
4878
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004879 case InputEventType::MOTION: {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004880 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004881 const bool isPointerEvent =
4882 isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER);
4883 // If a pointer event has no displayId specified, inject it to the default display.
Linnan Li13bf76a2024-05-05 19:18:02 +08004884 const ui::LogicalDisplayId displayId =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004885 isPointerEvent && (event->getDisplayId() == ui::LogicalDisplayId::INVALID)
4886 ? ui::LogicalDisplayId::DEFAULT
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004887 : event->getDisplayId();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004888 int32_t flags = motionEvent.getFlags();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004889
4890 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004891 nsecs_t eventTime = motionEvent.getEventTime();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004892 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004893 mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(),
4894 motionEvent.getAction(), eventTime,
4895 /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004896 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4897 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
4898 std::to_string(t.duration().count()).c_str());
4899 }
4900 }
4901
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004902 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4903 flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4904 }
4905
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004906 mLock.lock();
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004907
Siarhei Vishniakoueb75bc82024-06-04 13:05:45 -07004908 {
4909 // Verify all injected streams, whether the injection is coming from apps or from
4910 // input filter. Print an error if the stream becomes inconsistent with this event.
4911 // An inconsistent injected event sent could cause a crash in the later stages of
4912 // dispatching pipeline.
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004913 auto [it, _] =
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07004914 mInputFilterVerifiersByDisplay.try_emplace(displayId,
4915 std::string("Injection on ") +
4916 displayId.toString());
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004917 InputVerifier& verifier = it->second;
4918
4919 Result<void> result =
4920 verifier.processMovement(resolvedDeviceId, motionEvent.getSource(),
4921 motionEvent.getAction(),
4922 motionEvent.getPointerCount(),
4923 motionEvent.getPointerProperties(),
4924 motionEvent.getSamplePointerCoords(), flags);
4925 if (!result.ok()) {
4926 logDispatchStateLocked();
4927 LOG(ERROR) << "Inconsistent event: " << motionEvent
4928 << ", reason: " << result.error();
Siarhei Vishniakou9d0d65e2024-08-02 12:10:05 -07004929 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4930 mLock.unlock();
4931 return InputEventInjectionResult::FAILED;
4932 }
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00004933 }
4934 }
4935
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004936 const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004937 const size_t pointerCount = motionEvent.getPointerCount();
4938 const std::vector<PointerProperties>
4939 pointerProperties(motionEvent.getPointerProperties(),
4940 motionEvent.getPointerProperties() + pointerCount);
4941
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004942 const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004943 std::unique_ptr<MotionEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004944 std::make_unique<MotionEntry>(motionEvent.getId(), injectionState,
4945 *sampleEventTimes, resolvedDeviceId,
4946 motionEvent.getSource(), displayId, policyFlags,
4947 motionEvent.getAction(),
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004948 motionEvent.getActionButton(), flags,
4949 motionEvent.getMetaState(),
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004950 motionEvent.getButtonState(),
4951 motionEvent.getClassification(),
4952 motionEvent.getEdgeFlags(),
4953 motionEvent.getXPrecision(),
4954 motionEvent.getYPrecision(),
4955 motionEvent.getRawXCursorPosition(),
4956 motionEvent.getRawYCursorPosition(),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004957 motionEvent.getDownTime(), pointerProperties,
4958 std::vector<PointerCoords>(samplePointerCoords,
4959 samplePointerCoords +
4960 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004961 transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004962 if (mTracer) {
4963 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4964 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004965 injectedEntries.push(std::move(injectedEntry));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004966 for (size_t i = motionEvent.getHistorySize(); i > 0; i--) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004967 sampleEventTimes += 1;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004968 samplePointerCoords += motionEvent.getPointerCount();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004969 std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique<
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004970 MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes,
4971 resolvedDeviceId, motionEvent.getSource(), displayId,
4972 policyFlags, motionEvent.getAction(),
4973 motionEvent.getActionButton(), flags,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004974 motionEvent.getMetaState(), motionEvent.getButtonState(),
4975 motionEvent.getClassification(), motionEvent.getEdgeFlags(),
4976 motionEvent.getXPrecision(), motionEvent.getYPrecision(),
4977 motionEvent.getRawXCursorPosition(),
4978 motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(),
4979 pointerProperties,
4980 std::vector<PointerCoords>(samplePointerCoords,
4981 samplePointerCoords +
4982 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004983 transformMotionEntryForInjectionLocked(*nextInjectedEntry,
4984 motionEvent.getTransform());
Prabir Pradhan2b2d1bf2024-03-05 16:27:56 +00004985 if (mTracer) {
4986 nextInjectedEntry->traceTracker =
4987 mTracer->traceInboundEvent(*nextInjectedEntry);
4988 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004989 injectedEntries.push(std::move(nextInjectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004990 }
4991 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004994 default:
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004995 LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events";
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004996 return InputEventInjectionResult::FAILED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 }
4998
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 bool needWake = false;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005000 while (!injectedEntries.empty()) {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08005001 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005002 LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08005003 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07005004 needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front()));
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005005 injectedEntries.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 }
5007
5008 mLock.unlock();
5009
5010 if (needWake) {
5011 mLooper->wake();
5012 }
5013
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005014 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005016 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005018 if (syncMode == InputEventInjectionSync::NONE) {
5019 injectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 } else {
5021 for (;;) {
5022 injectionResult = injectionState->injectionResult;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005023 if (injectionResult != InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 break;
5025 }
5026
5027 nsecs_t remainingTimeout = endTime - now();
5028 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005029 if (DEBUG_INJECTION) {
5030 ALOGD("injectInputEvent - Timed out waiting for injection result "
5031 "to become available.");
5032 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005033 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 break;
5035 }
5036
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005037 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 }
5039
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005040 if (injectionResult == InputEventInjectionResult::SUCCEEDED &&
5041 syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 while (injectionState->pendingForegroundDispatches != 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005043 if (DEBUG_INJECTION) {
5044 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
5045 injectionState->pendingForegroundDispatches);
5046 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 nsecs_t remainingTimeout = endTime - now();
5048 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005049 if (DEBUG_INJECTION) {
5050 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
5051 "dispatches to finish.");
5052 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005053 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 break;
5055 }
5056
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005057 mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 }
5059 }
5060 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 } // release lock
5062
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005063 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005064 LOG(INFO) << "injectInputEvent - Finished with result "
5065 << ftl::enum_string(injectionResult);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005066 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067
5068 return injectionResult;
5069}
5070
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005071std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) {
Gang Wange9087892020-01-07 12:17:14 -05005072 std::array<uint8_t, 32> calculatedHmac;
5073 std::unique_ptr<VerifiedInputEvent> result;
5074 switch (event.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005075 case InputEventType::KEY: {
Gang Wange9087892020-01-07 12:17:14 -05005076 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
5077 VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent);
5078 result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005079 calculatedHmac = sign(verifiedKeyEvent);
Gang Wange9087892020-01-07 12:17:14 -05005080 break;
5081 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07005082 case InputEventType::MOTION: {
Gang Wange9087892020-01-07 12:17:14 -05005083 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
5084 VerifiedMotionEvent verifiedMotionEvent =
5085 verifiedMotionEventFromMotionEvent(motionEvent);
5086 result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07005087 calculatedHmac = sign(verifiedMotionEvent);
Gang Wange9087892020-01-07 12:17:14 -05005088 break;
5089 }
5090 default: {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08005091 LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType());
Gang Wange9087892020-01-07 12:17:14 -05005092 return nullptr;
5093 }
5094 }
5095 if (calculatedHmac == INVALID_HMAC) {
5096 return nullptr;
5097 }
tyiu1573a672023-02-21 22:38:32 +00005098 if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) {
Gang Wange9087892020-01-07 12:17:14 -05005099 return nullptr;
5100 }
5101 return result;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08005102}
5103
Prabir Pradhan24047542023-11-02 17:14:59 +00005104void InputDispatcher::setInjectionResult(const EventEntry& entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08005105 InputEventInjectionResult injectionResult) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005106 if (!entry.injectionState) {
5107 // Not an injected event.
5108 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 }
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005110
5111 InjectionState& injectionState = *entry.injectionState;
5112 if (DEBUG_INJECTION) {
5113 LOG(INFO) << "Setting input event injection result to "
5114 << ftl::enum_string(injectionResult);
5115 }
5116
5117 if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) {
5118 // Log the outcome since the injector did not wait for the injection result.
5119 switch (injectionResult) {
5120 case InputEventInjectionResult::SUCCEEDED:
5121 ALOGV("Asynchronous input event injection succeeded.");
5122 break;
5123 case InputEventInjectionResult::TARGET_MISMATCH:
5124 ALOGV("Asynchronous input event injection target mismatch.");
5125 break;
5126 case InputEventInjectionResult::FAILED:
5127 ALOGW("Asynchronous input event injection failed.");
5128 break;
5129 case InputEventInjectionResult::TIMED_OUT:
5130 ALOGW("Asynchronous input event injection timed out.");
5131 break;
5132 case InputEventInjectionResult::PENDING:
5133 ALOGE("Setting result to 'PENDING' for asynchronous injection");
5134 break;
5135 }
5136 }
5137
5138 injectionState.injectionResult = injectionResult;
5139 mInjectionResultAvailable.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140}
5141
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005142void InputDispatcher::transformMotionEntryForInjectionLocked(
5143 MotionEntry& entry, const ui::Transform& injectedTransform) const {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005144 // Input injection works in the logical display coordinate space, but the input pipeline works
5145 // display space, so we need to transform the injected events accordingly.
5146 const auto it = mDisplayInfos.find(entry.displayId);
5147 if (it == mDisplayInfos.end()) return;
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005148 const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005149
Prabir Pradhand9a2ebe2022-07-20 19:25:13 +00005150 if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
5151 entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
5152 const vec2 cursor =
5153 MotionEvent::calculateTransformedXY(entry.source, transformToDisplay,
5154 {entry.xCursorPosition, entry.yCursorPosition});
5155 entry.xCursorPosition = cursor.x;
5156 entry.yCursorPosition = cursor.y;
5157 }
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07005158 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08005159 entry.pointerCoords[i] =
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005160 MotionEvent::calculateTransformedCoords(entry.source, entry.flags,
5161 transformToDisplay, entry.pointerCoords[i]);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005162 }
5163}
5164
Prabir Pradhan24047542023-11-02 17:14:59 +00005165void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005166 if (entry.injectionState) {
5167 entry.injectionState->pendingForegroundDispatches += 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 }
5169}
5170
Prabir Pradhan24047542023-11-02 17:14:59 +00005171void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005172 if (entry.injectionState) {
5173 entry.injectionState->pendingForegroundDispatches -= 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005175 if (entry.injectionState->pendingForegroundDispatches == 0) {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005176 mInjectionSyncFinished.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177 }
5178 }
5179}
5180
chaviw98318de2021-05-19 16:45:23 -05005181const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005182 ui::LogicalDisplayId displayId) const {
chaviw98318de2021-05-19 16:45:23 -05005183 static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES;
Vishnu Nairad321cd2020-08-20 16:40:21 -07005184 auto it = mWindowHandlesByDisplay.find(displayId);
5185 return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES;
Arthur Hungb92218b2018-08-14 12:00:21 +08005186}
5187
chaviw98318de2021-05-19 16:45:23 -05005188sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005189 const sp<IBinder>& windowHandleToken, std::optional<ui::LogicalDisplayId> displayId) const {
arthurhungbe737672020-06-24 12:29:21 +08005190 if (windowHandleToken == nullptr) {
5191 return nullptr;
5192 }
5193
Prabir Pradhan16463382023-10-12 23:03:19 +00005194 if (!displayId) {
5195 // Look through all displays.
Linnan Li13bf76a2024-05-05 19:18:02 +08005196 for (const auto& [_, windowHandles] : mWindowHandlesByDisplay) {
Prabir Pradhan16463382023-10-12 23:03:19 +00005197 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
5198 if (windowHandle->getToken() == windowHandleToken) {
5199 return windowHandle;
5200 }
Arthur Hungb92218b2018-08-14 12:00:21 +08005201 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 }
Vishnu Nairad321cd2020-08-20 16:40:21 -07005203 return nullptr;
5204 }
5205
Prabir Pradhan16463382023-10-12 23:03:19 +00005206 // Only look through the requested display.
5207 for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005208 if (windowHandle->getToken() == windowHandleToken) {
5209 return windowHandle;
5210 }
5211 }
5212 return nullptr;
5213}
5214
chaviw98318de2021-05-19 16:45:23 -05005215sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
5216 const sp<WindowInfoHandle>& windowHandle) const {
Linnan Li13bf76a2024-05-05 19:18:02 +08005217 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
chaviw98318de2021-05-19 16:45:23 -05005218 for (const sp<WindowInfoHandle>& handle : windowHandles) {
arthurhungbe737672020-06-24 12:29:21 +08005219 if (handle->getId() == windowHandle->getId() &&
5220 handle->getToken() == windowHandle->getToken()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005221 if (windowHandle->getInfo()->displayId != displayId) {
5222 ALOGE("Found window %s in display %s"
5223 ", but it should belong to display %s",
5224 windowHandle->getName().c_str(), displayId.toString().c_str(),
5225 windowHandle->getInfo()->displayId.toString().c_str());
Mady Mellor017bcd12020-06-23 19:12:00 +00005226 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005227 return handle;
Arthur Hungb92218b2018-08-14 12:00:21 +08005228 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 }
5230 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005231 return nullptr;
5232}
5233
Linnan Li13bf76a2024-05-05 19:18:02 +08005234sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(
5235 ui::LogicalDisplayId displayId) const {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005236 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId);
5237 return getWindowHandleLocked(focusedToken, displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238}
5239
Linnan Li13bf76a2024-05-05 19:18:02 +08005240ui::Transform InputDispatcher::getTransformLocked(ui::LogicalDisplayId displayId) const {
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005241 auto displayInfoIt = mDisplayInfos.find(displayId);
5242 return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform
5243 : kIdentityTransform;
5244}
5245
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005246bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window,
5247 const MotionEntry& motionEntry) const {
5248 const WindowInfo& info = *window->getInfo();
5249
5250 // Skip spy window targets that are not valid for targeted injection.
5251 if (const auto err = verifyTargetedInjection(window, motionEntry); err) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005252 return false;
5253 }
5254
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005255 if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
5256 ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str());
5257 return false;
5258 }
5259
5260 if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
5261 ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL",
5262 window->getName().c_str());
5263 return false;
5264 }
5265
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005266 std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005267 if (connection == nullptr) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005268 ALOGW("Not sending touch to %s because there's no corresponding connection",
5269 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005270 return false;
5271 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005272
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005273 if (!connection->responsive) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005274 ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005275 return false;
5276 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005277
5278 // Drop events that can't be trusted due to occlusion
5279 const auto [x, y] = resolveTouchedPosition(motionEntry);
5280 TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5281 if (!isTouchTrustedLocked(occlusionInfo)) {
5282 if (DEBUG_TOUCH_OCCLUSION) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00005283 ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005284 for (const auto& log : occlusionInfo.debugInfo) {
5285 ALOGD("%s", log.c_str());
5286 }
5287 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005288 ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(),
5289 occlusionInfo.obscuringUid.toString().c_str());
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005290 return false;
5291 }
5292
5293 // Drop touch events if requested by input feature
5294 if (shouldDropInput(motionEntry, window)) {
5295 return false;
5296 }
5297
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07005298 // Ignore touches if stylus is down anywhere on screen
5299 if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) &&
5300 isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) {
5301 LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active";
5302 return false;
5303 }
5304
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005305 return true;
5306}
5307
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005308void InputDispatcher::updateWindowHandlesForDisplayLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005309 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5310 ui::LogicalDisplayId displayId) {
chaviw98318de2021-05-19 16:45:23 -05005311 if (windowInfoHandles.empty()) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005312 // Remove all handles on a display if there are no windows left.
5313 mWindowHandlesByDisplay.erase(displayId);
5314 return;
5315 }
5316
5317 // Since we compare the pointer of input window handles across window updates, we need
5318 // to make sure the handle object for the same window stays unchanged across updates.
chaviw98318de2021-05-19 16:45:23 -05005319 const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId);
5320 std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById;
5321 for (const sp<WindowInfoHandle>& handle : oldHandles) {
chaviwaf87b3e2019-10-01 16:59:28 -07005322 oldHandlesById[handle->getId()] = handle;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005323 }
5324
chaviw98318de2021-05-19 16:45:23 -05005325 std::vector<sp<WindowInfoHandle>> newHandles;
5326 for (const sp<WindowInfoHandle>& handle : windowInfoHandles) {
chaviw98318de2021-05-19 16:45:23 -05005327 const WindowInfo* info = handle->getInfo();
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005328 if (getConnectionLocked(handle->getToken()) == nullptr) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005329 const bool noInputChannel =
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005330 info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005331 const bool canReceiveInput =
5332 !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) ||
5333 !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005334 if (canReceiveInput && !noInputChannel) {
John Recke0710582019-09-26 13:46:12 -07005335 ALOGV("Window handle %s has no registered input channel",
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005336 handle->getName().c_str());
Robert Carr2984b7a2020-04-13 17:06:45 -07005337 continue;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005338 }
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005339 }
5340
5341 if (info->displayId != displayId) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005342 ALOGE("Window %s updated by wrong display %s, should belong to display %s",
5343 handle->getName().c_str(), displayId.toString().c_str(),
5344 info->displayId.toString().c_str());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005345 continue;
5346 }
5347
Robert Carredd13602020-04-13 17:24:34 -07005348 if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) &&
Linnan Li13bf76a2024-05-05 19:18:02 +08005349 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) {
chaviw98318de2021-05-19 16:45:23 -05005350 const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005351 oldHandle->updateFrom(handle);
5352 newHandles.push_back(oldHandle);
5353 } else {
5354 newHandles.push_back(handle);
5355 }
5356 }
5357
5358 // Insert or replace
5359 mWindowHandlesByDisplay[displayId] = newHandles;
5360}
5361
Arthur Hungb92218b2018-08-14 12:00:21 +08005362/**
5363 * Called from InputManagerService, update window handle list by displayId that can receive input.
5364 * A window handle contains information about InputChannel, Touch Region, Types, Focused,...
5365 * If set an empty list, remove all handles from the specific display.
5366 * For focused handle, check if need to change and send a cancel event to previous one.
5367 * For removed handle, check if need to send a cancel event if already in touch.
5368 */
Arthur Hung72d8dc32020-03-28 00:48:39 +00005369void InputDispatcher::setInputWindowsLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005370 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles,
5371 ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005372 if (DEBUG_FOCUS) {
5373 std::string windowList;
chaviw98318de2021-05-19 16:45:23 -05005374 for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005375 windowList += iwh->getName() + " ";
5376 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005377 LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList;
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005378 }
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005379 ScopedSyntheticEventTracer traceContext(mTracer);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380
Prabir Pradhand65552b2021-10-07 11:23:50 -07005381 // Check preconditions for new input windows
chaviw98318de2021-05-19 16:45:23 -05005382 for (const sp<WindowInfoHandle>& window : windowInfoHandles) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07005383 const WindowInfo& info = *window->getInfo();
5384
5385 // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005386 const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005387 if (noInputWindow && window->getToken() != nullptr) {
5388 ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing",
5389 window->getName().c_str());
5390 window->releaseChannel();
5391 }
Prabir Pradhand65552b2021-10-07 11:23:50 -07005392
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005393 // Ensure all spy windows are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005394 LOG_ALWAYS_FATAL_IF(info.isSpy() &&
5395 !info.inputConfig.test(
5396 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005397 "%s has feature SPY, but is not a trusted overlay.",
5398 window->getName().c_str());
5399
Prabir Pradhand65552b2021-10-07 11:23:50 -07005400 // Ensure all stylus interceptors are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005401 LOG_ALWAYS_FATAL_IF(info.interceptsStylus() &&
5402 !info.inputConfig.test(
5403 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhand65552b2021-10-07 11:23:50 -07005404 "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.",
5405 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005406 }
5407
Arthur Hung72d8dc32020-03-28 00:48:39 +00005408 // Copy old handles for release if they are no longer present.
chaviw98318de2021-05-19 16:45:23 -05005409 const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005410 const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
chaviw98318de2021-05-19 16:45:23 -05005412 updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005413
chaviw98318de2021-05-19 16:45:23 -05005414 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005415
Vishnu Nairc519ff72021-01-21 08:23:08 -08005416 std::optional<FocusResolver::FocusChanges> changes =
5417 mFocusResolver.setInputWindows(displayId, windowHandles);
5418 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005419 onFocusChangedLocked(*changes, traceContext.getTracker(), removedFocusedWindowHandle);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005420 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421
Linnan Li72352222024-04-12 18:55:57 +08005422 if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5423 TouchState& state = it->second;
Arthur Hung72d8dc32020-03-28 00:48:39 +00005424 for (size_t i = 0; i < state.windows.size();) {
5425 TouchedWindow& touchedWindow = state.windows[i];
Linnan Li72352222024-04-12 18:55:57 +08005426 if (getWindowHandleLocked(touchedWindow.windowHandle) != nullptr) {
5427 i++;
5428 continue;
5429 }
5430 LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName()
5431 << " in display %" << displayId;
5432 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
5433 "touched window was removed", traceContext.getTracker());
5434 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
5435 // Since we are about to drop the touch, cancel the events for the wallpaper as
5436 // well.
5437 if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) &&
5438 touchedWindow.windowHandle->getInfo()->inputConfig.test(
5439 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
5440 for (const DeviceId deviceId : touchedWindow.getTouchingDeviceIds()) {
5441 if (const auto& ww = state.getWallpaperWindow(deviceId); ww != nullptr) {
5442 options.deviceId = deviceId;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005443 synthesizeCancelationEventsForWindowLocked(ww, options);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005444 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446 }
Linnan Li72352222024-04-12 18:55:57 +08005447 state.windows.erase(state.windows.begin() + i);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 }
arthurhungb89ccb02020-12-30 16:19:01 +08005449
arthurhung6d4bed92021-03-17 11:59:33 +08005450 // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We
arthurhungb89ccb02020-12-30 16:19:01 +08005451 // could just clear the state here.
Arthur Hung3915c1f2022-05-31 07:17:17 +00005452 if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId &&
arthurhung6d4bed92021-03-17 11:59:33 +08005453 std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) ==
arthurhungb89ccb02020-12-30 16:19:01 +08005454 windowHandles.end()) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00005455 ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str());
5456 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08005457 mDragState.reset();
arthurhungb89ccb02020-12-30 16:19:01 +08005458 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005459 }
Arthur Hung25e2af12020-03-26 12:58:37 +00005460
Siarhei Vishniakoub711e652024-08-12 12:00:28 -07005461 // Check if the hovering should stop because the window is no longer eligible to receive it
5462 // (for example, if the touchable region changed)
5463 if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5464 TouchState& state = it->second;
5465 for (TouchedWindow& touchedWindow : state.windows) {
5466 std::vector<DeviceId> erasedDevices = touchedWindow.eraseHoveringPointersIf(
5467 [this, displayId, &touchedWindow](const PointerProperties& properties, float x,
5468 float y) REQUIRES(mLock) {
5469 const bool isStylus = properties.toolType == ToolType::STYLUS;
5470 const ui::Transform displayTransform = getTransformLocked(displayId);
5471 const bool stillAcceptsTouch =
5472 windowAcceptsTouchAt(*touchedWindow.windowHandle->getInfo(),
5473 displayId, x, y, isStylus, displayTransform);
5474 return !stillAcceptsTouch;
5475 });
5476
5477 for (DeviceId deviceId : erasedDevices) {
5478 CancelationOptions options(CancelationOptions::Mode::CANCEL_HOVER_EVENTS,
5479 "WindowInfo changed",
5480 traceContext.getTracker());
5481 options.deviceId = deviceId;
5482 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
5483 }
5484 }
5485 }
5486
Arthur Hung72d8dc32020-03-28 00:48:39 +00005487 // Release information for windows that are no longer present.
5488 // This ensures that unused input channels are released promptly.
5489 // Otherwise, they might stick around until the window handle is destroyed
5490 // which might not happen until the next GC.
chaviw98318de2021-05-19 16:45:23 -05005491 for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005492 if (getWindowHandleLocked(oldWindowHandle) == nullptr) {
Arthur Hung72d8dc32020-03-28 00:48:39 +00005493 if (DEBUG_FOCUS) {
5494 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str());
Arthur Hung25e2af12020-03-26 12:58:37 +00005495 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005496 oldWindowHandle->releaseChannel();
Arthur Hung25e2af12020-03-26 12:58:37 +00005497 }
chaviw291d88a2019-02-14 10:33:58 -08005498 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499}
5500
5501void InputDispatcher::setFocusedApplication(
Linnan Li13bf76a2024-05-05 19:18:02 +08005502 ui::LogicalDisplayId displayId,
5503 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005504 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005505 ALOGD("setFocusedApplication displayId=%s %s", displayId.toString().c_str(),
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005506 inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>");
5507 }
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005508 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005509 std::scoped_lock _l(mLock);
Vishnu Nair599f1412021-06-21 10:39:58 -07005510 setFocusedApplicationLocked(displayId, inputApplicationHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 } // release lock
5512
5513 // Wake up poll loop since it may need to make new input dispatching choices.
5514 mLooper->wake();
5515}
5516
Vishnu Nair599f1412021-06-21 10:39:58 -07005517void InputDispatcher::setFocusedApplicationLocked(
Linnan Li13bf76a2024-05-05 19:18:02 +08005518 ui::LogicalDisplayId displayId,
5519 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Vishnu Nair599f1412021-06-21 10:39:58 -07005520 std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle =
5521 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
5522
5523 if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) {
5524 return; // This application is already focused. No need to wake up or change anything.
5525 }
5526
5527 // Set the new application handle.
5528 if (inputApplicationHandle != nullptr) {
5529 mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
5530 } else {
5531 mFocusedApplicationHandlesByDisplay.erase(displayId);
5532 }
5533
5534 // No matter what the old focused application was, stop waiting on it because it is
5535 // no longer focused.
5536 resetNoFocusedWindowTimeoutLocked();
5537}
5538
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08005539void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) {
5540 if (interval.count() < 0) {
5541 LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0");
5542 }
5543 std::scoped_lock _l(mLock);
5544 mMinTimeBetweenUserActivityPokes = interval;
5545}
5546
Tiger Huang721e26f2018-07-24 22:26:19 +08005547/**
5548 * Sets the focused display, which is responsible for receiving focus-dispatched input events where
5549 * the display not specified.
5550 *
5551 * We track any unreleased events for each window. If a window loses the ability to receive the
5552 * released event, we will send a cancel event to it. So when the focused display is changed, we
5553 * cancel all the unreleased display-unspecified events for the focused window on the old focused
5554 * display. The display-specified events won't be affected.
5555 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005556void InputDispatcher::setFocusedDisplay(ui::LogicalDisplayId displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005557 if (DEBUG_FOCUS) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005558 ALOGD("setFocusedDisplay displayId=%s", displayId.toString().c_str());
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005559 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005560 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005561 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005562 ScopedSyntheticEventTracer traceContext(mTracer);
Tiger Huang721e26f2018-07-24 22:26:19 +08005563
5564 if (mFocusedDisplayId != displayId) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005565 sp<IBinder> oldFocusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08005566 mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07005567 if (oldFocusedWindowToken != nullptr) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005568 const auto windowHandle =
5569 getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId);
5570 if (windowHandle == nullptr) {
5571 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Tiger Huang721e26f2018-07-24 22:26:19 +08005572 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005573 CancelationOptions
5574 options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005575 "The display which contains this window no longer has focus.",
5576 traceContext.getTracker());
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005577 options.displayId = ui::LogicalDisplayId::INVALID;
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005578 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
Tiger Huang721e26f2018-07-24 22:26:19 +08005579 }
5580 mFocusedDisplayId = displayId;
Arpit Singhb65e2bd2024-06-03 09:48:16 +00005581 // Enqueue a command to run outside the lock to tell the policy that the focused display
5582 // changed.
5583 auto command = [this]() REQUIRES(mLock) {
5584 scoped_unlock unlock(mLock);
5585 mPolicy.notifyFocusedDisplayChanged(mFocusedDisplayId);
5586 };
5587 postCommandLocked(std::move(command));
Tiger Huang721e26f2018-07-24 22:26:19 +08005588
Prabir Pradhan9d3d5612024-06-06 20:34:26 +00005589 // Only a window on the focused display can have Pointer Capture, so disable the active
5590 // Pointer Capture session if there is one, since the focused display changed.
5591 disablePointerCaptureForcedLocked();
5592
Chris Ye3c2d6f52020-08-09 10:39:48 -07005593 // Find new focused window and validate
Vishnu Nairc519ff72021-01-21 08:23:08 -08005594 sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00005595 sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken);
Robert Carrf759f162018-11-13 12:57:11 -08005596
Vishnu Nairad321cd2020-08-20 16:40:21 -07005597 if (newFocusedWindowToken == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005598 ALOGW("Focused display #%s does not have a focused window.",
5599 displayId.toString().c_str());
Vishnu Nairc519ff72021-01-21 08:23:08 -08005600 if (mFocusResolver.hasFocusedWindowTokens()) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005601 ALOGE("But another display has a focused window\n%s",
Vishnu Nairc519ff72021-01-21 08:23:08 -08005602 mFocusResolver.dumpFocusedWindows().c_str());
Tiger Huang721e26f2018-07-24 22:26:19 +08005603 }
5604 }
5605 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005606 } // release lock
5607
5608 // Wake up poll loop since it may need to make new input dispatching choices.
5609 mLooper->wake();
5610}
5611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005613 if (DEBUG_FOCUS) {
5614 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
5615 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616
5617 bool changed;
5618 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005619 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620
5621 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
5622 if (mDispatchFrozen && !frozen) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005623 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 }
5625
5626 if (mDispatchEnabled && !enabled) {
5627 resetAndDropEverythingLocked("dispatcher is being disabled");
5628 }
5629
5630 mDispatchEnabled = enabled;
5631 mDispatchFrozen = frozen;
5632 changed = true;
5633 } else {
5634 changed = false;
5635 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 } // release lock
5637
5638 if (changed) {
5639 // Wake up poll loop since it may need to make new input dispatching choices.
5640 mLooper->wake();
5641 }
5642}
5643
5644void InputDispatcher::setInputFilterEnabled(bool enabled) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005645 if (DEBUG_FOCUS) {
5646 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
5647 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648
5649 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005650 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651
5652 if (mInputFilterEnabled == enabled) {
5653 return;
5654 }
5655
5656 mInputFilterEnabled = enabled;
5657 resetAndDropEverythingLocked("input filter is being enabled or disabled");
5658 } // release lock
5659
5660 // Wake up poll loop since there might be work to do to drop everything.
5661 mLooper->wake();
5662}
5663
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005664bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Linnan Li13bf76a2024-05-05 19:18:02 +08005665 bool hasPermission, ui::LogicalDisplayId displayId) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00005666 bool needWake = false;
5667 {
5668 std::scoped_lock lock(mLock);
Antonio Kantek15beb512022-06-13 22:35:41 +00005669 ALOGD_IF(DEBUG_TOUCH_MODE,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005670 "Request to change touch mode to %s (calling pid=%s, uid=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +08005671 "hasPermission=%s, target displayId=%s, mTouchModePerDisplay[displayId]=%s)",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005672 toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08005673 toString(hasPermission), displayId.toString().c_str(),
Antonio Kantek15beb512022-06-13 22:35:41 +00005674 mTouchModePerDisplay.count(displayId) == 0
5675 ? "not set"
5676 : std::to_string(mTouchModePerDisplay[displayId]).c_str());
5677
Antonio Kantek15beb512022-06-13 22:35:41 +00005678 auto touchModeIt = mTouchModePerDisplay.find(displayId);
5679 if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08005680 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +00005681 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005682 if (!hasPermission) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005683 if (!focusedWindowIsOwnedByLocked(pid, uid) &&
5684 !recentWindowsAreOwnedByLocked(pid, uid)) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005685 ALOGD("Touch mode switch rejected, caller (pid=%s, uid=%s) doesn't own the focused "
Antonio Kantek48710e42022-03-24 14:19:30 -07005686 "window nor none of the previously interacted window",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005687 pid.toString().c_str(), uid.toString().c_str());
Antonio Kantekea47acb2021-12-23 12:41:25 -08005688 return false;
5689 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00005690 }
Antonio Kantek15beb512022-06-13 22:35:41 +00005691 mTouchModePerDisplay[displayId] = inTouchMode;
5692 auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode,
5693 displayId);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005694 needWake = enqueueInboundEventLocked(std::move(entry));
5695 } // release lock
5696
5697 if (needWake) {
5698 mLooper->wake();
5699 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005700 return true;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -08005701}
5702
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005703bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005704 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
5705 if (focusedToken == nullptr) {
5706 return false;
5707 }
5708 sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken);
5709 return isWindowOwnedBy(windowHandle, pid, uid);
5710}
5711
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005712bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005713 return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(),
5714 [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) {
5715 const sp<WindowInfoHandle> windowHandle =
5716 getWindowHandleLocked(connectionToken);
5717 return isWindowOwnedBy(windowHandle, pid, uid);
5718 }) != mInteractionConnectionTokens.end();
5719}
5720
Bernardo Rufinoea97d182020-08-19 14:43:14 +01005721void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
5722 if (opacity < 0 || opacity > 1) {
5723 LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
5724 return;
5725 }
5726
5727 std::scoped_lock lock(mLock);
5728 mMaximumObscuringOpacityForTouch = opacity;
5729}
5730
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07005731std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005732InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005733 for (auto& [displayId, state] : mTouchStatesByDisplay) {
5734 for (TouchedWindow& w : state.windows) {
5735 if (w.windowHandle->getToken() == token) {
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005736 return std::make_tuple(&state, &w, displayId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005737 }
5738 }
5739 }
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07005740 return std::make_tuple(nullptr, nullptr, ui::LogicalDisplayId::DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005741}
5742
Siarhei Vishniakou07cdda92024-07-01 16:45:08 -07005743std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
5744InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) const {
5745 return const_cast<InputDispatcher*>(this)->findTouchStateWindowAndDisplayLocked(token);
5746}
5747
5748bool InputDispatcher::windowHasTouchingPointersLocked(const sp<WindowInfoHandle>& windowHandle,
5749 DeviceId deviceId) const {
5750 const auto& [touchState, touchedWindow, _] =
5751 findTouchStateWindowAndDisplayLocked(windowHandle->getToken());
5752 if (touchState == nullptr) {
5753 // No touching pointers at all
5754 return false;
5755 }
5756 return touchState->hasTouchingPointers(deviceId);
5757}
5758
Prabir Pradhan367f3432024-02-13 23:05:58 +00005759bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
5760 bool isDragDrop) {
chaviwfbe5d9c2018-12-26 12:23:37 -08005761 if (fromToken == toToken) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005762 if (DEBUG_FOCUS) {
5763 ALOGD("Trivial transfer to same window.");
5764 }
chaviwfbe5d9c2018-12-26 12:23:37 -08005765 return true;
5766 }
5767
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005769 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
Arthur Hungabbb9d82021-09-01 14:52:30 +00005771 // Find the target touch state and touched window by fromToken.
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005772 auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005773
Arthur Hungabbb9d82021-09-01 14:52:30 +00005774 if (state == nullptr || touchedWindow == nullptr) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005775 ALOGD("Touch transfer failed because from window is not being touched.");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 return false;
5777 }
Linnan Li72352222024-04-12 18:55:57 +08005778 std::set<DeviceId> deviceIds = touchedWindow->getTouchingDeviceIds();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005779 if (deviceIds.size() != 1) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005780 LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds)
5781 << " for window: " << touchedWindow->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005782 return false;
5783 }
Linnan Li72352222024-04-12 18:55:57 +08005784 const DeviceId deviceId = *deviceIds.begin();
Arthur Hungabbb9d82021-09-01 14:52:30 +00005785
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005786 const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle;
5787 const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId);
5788 if (!toWindowHandle) {
5789 ALOGW("Cannot transfer touch because the transfer target window was not found.");
Arthur Hungabbb9d82021-09-01 14:52:30 +00005790 return false;
5791 }
5792
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005793 if (DEBUG_FOCUS) {
Prabir Pradhan367f3432024-02-13 23:05:58 +00005794 ALOGD("%s: fromWindowHandle=%s, toWindowHandle=%s", __func__,
Arthur Hungabbb9d82021-09-01 14:52:30 +00005795 touchedWindow->windowHandle->getName().c_str(),
5796 toWindowHandle->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797 }
5798
Arthur Hungabbb9d82021-09-01 14:52:30 +00005799 // Erase old window.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005800 ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005801 std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005802 state->removeWindowByToken(fromToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803
Arthur Hungabbb9d82021-09-01 14:52:30 +00005804 // Add new window.
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00005805 nsecs_t downTimeInTarget = now();
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005806 ftl::Flags<InputTarget::Flags> newTargetFlags =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005807 oldTargetFlags & (InputTarget::Flags::SPLIT);
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005808 if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005809 newTargetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005810 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00005811 // Transferring touch focus using this API should not effect the focused window.
5812 newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005813 state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005814 deviceId, pointers, downTimeInTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815
Arthur Hungabbb9d82021-09-01 14:52:30 +00005816 // Store the dragging window.
5817 if (isDragDrop) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005818 if (pointers.size() != 1) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005819 ALOGW("The drag and drop cannot be started when there is no pointer or more than 1"
5820 " pointer on the window.");
Arthur Hung54745652022-04-20 07:17:41 +00005821 return false;
5822 }
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005823 // Track the pointer id for drag window and generate the drag state.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005824 const size_t id = pointers.begin()->id;
Arthur Hung54745652022-04-20 07:17:41 +00005825 mDragState = std::make_unique<DragState>(toWindowHandle, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826 }
5827
Arthur Hungabbb9d82021-09-01 14:52:30 +00005828 // Synthesize cancel for old window and down for new window.
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005829 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005830 std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken);
5831 std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07005832 if (fromConnection != nullptr && toConnection != nullptr) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08005833 fromConnection->inputState.mergePointerStateTo(toConnection->inputState);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005834 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005835 "transferring touch from this window to another window",
5836 traceContext.getTracker());
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005837 synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005838
5839 // Check if the wallpaper window should deliver the corresponding event.
5840 transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005841 *state, deviceId, pointers, traceContext.getTracker());
Linnan Li72352222024-04-12 18:55:57 +08005842
5843 // Because new window may have a wallpaper window, it will merge input state from it
5844 // parent window, after this the firstNewPointerIdx in input state will be reset, then
5845 // it will cause new move event be thought inconsistent, so we should synthesize the
5846 // down event after it reset.
5847 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection,
5848 newTargetFlags,
5849 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851 } // release lock
5852
5853 // Wake up poll loop since it may need to make new input dispatching choices.
5854 mLooper->wake();
5855 return true;
5856}
5857
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005858/**
5859 * Get the touched foreground window on the given display.
5860 * Return null if there are no windows touched on that display, or if more than one foreground
5861 * window is being touched.
5862 */
Linnan Li13bf76a2024-05-05 19:18:02 +08005863sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(
5864 ui::LogicalDisplayId displayId) const {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005865 auto stateIt = mTouchStatesByDisplay.find(displayId);
5866 if (stateIt == mTouchStatesByDisplay.end()) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005867 ALOGI("No touch state on display %s", displayId.toString().c_str());
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005868 return nullptr;
5869 }
5870
5871 const TouchState& state = stateIt->second;
5872 sp<WindowInfoHandle> touchedForegroundWindow;
5873 // If multiple foreground windows are touched, return nullptr
5874 for (const TouchedWindow& window : state.windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005875 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005876 if (touchedForegroundWindow != nullptr) {
5877 ALOGI("Two or more foreground windows: %s and %s",
5878 touchedForegroundWindow->getName().c_str(),
5879 window.windowHandle->getName().c_str());
5880 return nullptr;
5881 }
5882 touchedForegroundWindow = window.windowHandle;
5883 }
5884 }
5885 return touchedForegroundWindow;
5886}
5887
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005888// Binder call
Prabir Pradhan367f3432024-02-13 23:05:58 +00005889bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken,
Linnan Li13bf76a2024-05-05 19:18:02 +08005890 ui::LogicalDisplayId displayId) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005891 sp<IBinder> fromToken;
5892 { // acquire lock
5893 std::scoped_lock _l(mLock);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005894 sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005895 if (toWindowHandle == nullptr) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005896 ALOGW("Could not find window associated with token=%p on display %s",
5897 destChannelToken.get(), displayId.toString().c_str());
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005898 return false;
5899 }
5900
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005901 sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId);
5902 if (from == nullptr) {
5903 ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get());
5904 return false;
5905 }
5906
5907 fromToken = from->getToken();
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005908 } // release lock
5909
Prabir Pradhan367f3432024-02-13 23:05:58 +00005910 return transferTouchGesture(fromToken, destChannelToken);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005911}
5912
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005914 if (DEBUG_FOCUS) {
5915 ALOGD("Resetting and dropping all events (%s).", reason);
5916 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917
Prabir Pradhand6b2b052024-02-21 23:25:15 +00005918 ScopedSyntheticEventTracer traceContext(mTracer);
5919 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason,
5920 traceContext.getTracker());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921 synthesizeCancelationEventsForAllConnectionsLocked(options);
5922
5923 resetKeyRepeatLocked();
5924 releasePendingEventLocked();
5925 drainInboundQueueLocked();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005926 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005928 mAnrTracker.clear();
Jeff Brownf086ddb2014-02-11 14:28:48 -08005929 mTouchStatesByDisplay.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930}
5931
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005932void InputDispatcher::logDispatchStateLocked() const {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005933 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 dumpDispatchStateLocked(dump);
5935
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005936 std::istringstream stream(dump);
5937 std::string line;
5938
5939 while (std::getline(stream, line, '\n')) {
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07005940 ALOGI("%s", line.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 }
5942}
5943
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005944std::string InputDispatcher::dumpPointerCaptureStateLocked() const {
Prabir Pradhan99987712020-11-10 18:43:05 -08005945 std::string dump;
5946
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005947 dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n",
Hiroki Sato25040232024-02-22 17:21:22 +09005948 toString(mCurrentPointerCaptureRequest.isEnable()));
Prabir Pradhan99987712020-11-10 18:43:05 -08005949
5950 std::string windowName = "None";
5951 if (mWindowTokenWithPointerCapture) {
chaviw98318de2021-05-19 16:45:23 -05005952 const sp<WindowInfoHandle> captureWindowHandle =
Prabir Pradhan99987712020-11-10 18:43:05 -08005953 getWindowHandleLocked(mWindowTokenWithPointerCapture);
5954 windowName = captureWindowHandle ? captureWindowHandle->getName().c_str()
5955 : "token has capture without window";
5956 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005957 dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str());
Prabir Pradhan99987712020-11-10 18:43:05 -08005958
5959 return dump;
5960}
5961
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005962void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
Siarhei Vishniakou043a3ec2019-05-01 11:30:46 -07005963 dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled));
5964 dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen));
5965 dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled));
Linnan Li13bf76a2024-05-05 19:18:02 +08005966 dump += StringPrintf(INDENT "FocusedDisplayId: %s\n", mFocusedDisplayId.toString().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967
Tiger Huang721e26f2018-07-24 22:26:19 +08005968 if (!mFocusedApplicationHandlesByDisplay.empty()) {
5969 dump += StringPrintf(INDENT "FocusedApplications:\n");
5970 for (auto& it : mFocusedApplicationHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08005971 const ui::LogicalDisplayId displayId = it.first;
Chris Yea209fde2020-07-22 13:54:51 -07005972 const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second;
Siarhei Vishniakou70622952020-07-30 11:17:23 -05005973 const std::chrono::duration timeout =
5974 applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Linnan Li13bf76a2024-05-05 19:18:02 +08005975 dump += StringPrintf(INDENT2 "displayId=%s, name='%s', dispatchingTimeout=%" PRId64
5976 "ms\n",
5977 displayId.toString().c_str(), applicationHandle->getName().c_str(),
5978 millis(timeout));
Tiger Huang721e26f2018-07-24 22:26:19 +08005979 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 } else {
Tiger Huang721e26f2018-07-24 22:26:19 +08005981 dump += StringPrintf(INDENT "FocusedApplications: <none>\n");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005983
Vishnu Nairc519ff72021-01-21 08:23:08 -08005984 dump += mFocusResolver.dump();
Prabir Pradhan99987712020-11-10 18:43:05 -08005985 dump += dumpPointerCaptureStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
Siarhei Vishniakou4700f822020-03-24 19:05:54 -07005987 if (!mTouchStatesByDisplay.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005988 dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005989 for (const auto& [displayId, state] : mTouchStatesByDisplay) {
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -08005990 std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
Linnan Li13bf76a2024-05-05 19:18:02 +08005991 dump += INDENT2 + displayId.toString() + " : " + touchStateDump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 }
5993 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005994 dump += INDENT "TouchStates: <no displays touched>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 }
5996
arthurhung6d4bed92021-03-17 11:59:33 +08005997 if (mDragState) {
5998 dump += StringPrintf(INDENT "DragState:\n");
5999 mDragState->dump(dump, INDENT2);
6000 }
6001
Arthur Hungb92218b2018-08-14 12:00:21 +08006002 if (!mWindowHandlesByDisplay.empty()) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006003 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08006004 dump += StringPrintf(INDENT "Display: %s\n", displayId.toString().c_str());
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006005 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
6006 const auto& displayInfo = it->second;
6007 dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth,
6008 displayInfo.logicalHeight);
6009 displayInfo.transform.dump(dump, "transform", INDENT4);
6010 } else {
6011 dump += INDENT2 "No DisplayInfo found!\n";
6012 }
6013
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006014 if (!windowHandles.empty()) {
Arthur Hungb92218b2018-08-14 12:00:21 +08006015 dump += INDENT2 "Windows:\n";
6016 for (size_t i = 0; i < windowHandles.size(); i++) {
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006017 dump += StringPrintf(INDENT3 "%zu: %s", i,
6018 streamableToString(*windowHandles[i]).c_str());
Arthur Hungb92218b2018-08-14 12:00:21 +08006019 }
6020 } else {
6021 dump += INDENT2 "Windows: <none>\n";
6022 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 }
6024 } else {
Arthur Hungb92218b2018-08-14 12:00:21 +08006025 dump += INDENT "Displays: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026 }
6027
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006028 if (!mGlobalMonitorsByDisplay.empty()) {
6029 for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08006030 dump += StringPrintf(INDENT "Global monitors on display %s:\n",
6031 displayId.toString().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00006032 dumpMonitors(dump, monitors);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07006033 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 } else {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006035 dump += INDENT "Global Monitors: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 }
6037
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006038 const nsecs_t currentTime = now();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
6040 // Dump recently dispatched or dropped events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006041 if (!mRecentQueue.empty()) {
6042 dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00006043 for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006044 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006045 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006046 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 }
6048 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006049 dump += INDENT "RecentQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 }
6051
6052 // Dump event currently being dispatched.
6053 if (mPendingEvent) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006054 dump += INDENT "PendingEvent:\n";
6055 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006056 dump += mPendingEvent->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006057 dump += StringPrintf(", age=%" PRId64 "ms\n",
6058 ns2ms(currentTime - mPendingEvent->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006060 dump += INDENT "PendingEvent: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 }
6062
6063 // Dump inbound events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006064 if (!mInboundQueue.empty()) {
6065 dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00006066 for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006067 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006068 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006069 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 }
6071 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006072 dump += INDENT "InboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 }
6074
Prabir Pradhancef936d2021-07-21 16:17:52 +00006075 if (!mCommandQueue.empty()) {
6076 dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size());
6077 } else {
6078 dump += INDENT "CommandQueue: <empty>\n";
6079 }
6080
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006081 if (!mConnectionsByToken.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006082 dump += INDENT "Connections:\n";
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006083 for (const auto& [token, connection] : mConnectionsByToken) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006084 dump += StringPrintf(INDENT2 "%i: channelName='%s', "
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006085 "status=%s, monitor=%s, responsive=%s\n",
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006086 connection->inputPublisher.getChannel().getFd(),
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006087 connection->getInputChannelName().c_str(),
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006088 ftl::enum_string(connection->status).c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006089 toString(connection->monitor), toString(connection->responsive));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006091 if (!connection->outboundQueue.empty()) {
6092 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n",
6093 connection->outboundQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006094 dump += dumpQueue(connection->outboundQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 }
6096
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07006097 if (!connection->waitQueue.empty()) {
6098 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n",
6099 connection->waitQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05006100 dump += dumpQueue(connection->waitQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006102 std::string inputStateDump = streamableToString(connection->inputState);
6103 if (!inputStateDump.empty()) {
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006104 dump += INDENT3 "InputState: ";
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08006105 dump += inputStateDump + "\n";
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07006106 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 }
6108 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006109 dump += INDENT "Connections: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 }
6111
Antonio Kantek15beb512022-06-13 22:35:41 +00006112 if (!mTouchModePerDisplay.empty()) {
6113 dump += INDENT "TouchModePerDisplay:\n";
6114 for (const auto& [displayId, touchMode] : mTouchModePerDisplay) {
Linnan Li13bf76a2024-05-05 19:18:02 +08006115 dump += StringPrintf(INDENT2 "Display: %s TouchMode: %s\n",
6116 displayId.toString().c_str(), std::to_string(touchMode).c_str());
Antonio Kantek15beb512022-06-13 22:35:41 +00006117 }
6118 } else {
6119 dump += INDENT "TouchModePerDisplay: <none>\n";
6120 }
6121
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006122 dump += INDENT "Configuration:\n";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006123 dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay));
6124 dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n",
6125 ns2ms(mConfig.keyRepeatTimeout));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00006126 dump += mLatencyTracker.dump(INDENT2);
jioana24878b52024-09-10 10:13:27 +00006127 dump += mInputEventTimelineProcessor->dump(INDENT2);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00006128 dump += INDENT "InputTracer: ";
6129 dump += mTracer == nullptr ? "Disabled" : "Enabled";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130}
6131
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07006132void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const {
Michael Wright3dd60e22019-03-27 22:06:44 +00006133 const size_t numMonitors = monitors.size();
6134 for (size_t i = 0; i < numMonitors; i++) {
6135 const Monitor& monitor = monitors[i];
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006136 const std::shared_ptr<Connection>& connection = monitor.connection;
6137 dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00006138 dump += "\n";
6139 }
6140}
6141
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006142class LooperEventCallback : public LooperCallback {
6143public:
6144 LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {}
6145 int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); }
6146
6147private:
6148 std::function<int(int events)> mCallback;
6149};
6150
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00006151Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006152 if (DEBUG_CHANNEL_CREATION) {
6153 ALOGD("channel '%s' ~ createInputChannel", name.c_str());
6154 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006156 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006157 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006158 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006159
6160 if (result) {
6161 return base::Error(result) << "Failed to open input channel pair with name " << name;
6162 }
6163
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006165 std::scoped_lock _l(mLock);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006166 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006167 const int fd = serverChannel->getFd();
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006168 std::shared_ptr<Connection> connection =
6169 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false,
6170 mIdGenerator);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006172 auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection);
6173 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006174 ALOGE("Created a new connection, but the token %p is already known", token.get());
6175 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006176
6177 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6178 this, std::placeholders::_1, token);
6179
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006180 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006181 nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 } // release lock
6183
6184 // Wake the looper because some connections have changed.
6185 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006186 return clientChannel;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187}
6188
Linnan Li13bf76a2024-05-05 19:18:02 +08006189Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(
6190 ui::LogicalDisplayId displayId, const std::string& name, gui::Pid pid) {
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006191 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07006192 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006193 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07006194 if (result) {
6195 return base::Error(result) << "Failed to open input channel pair with name " << name;
6196 }
6197
Michael Wright3dd60e22019-03-27 22:06:44 +00006198 { // acquire lock
6199 std::scoped_lock _l(mLock);
6200
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07006201 if (displayId < ui::LogicalDisplayId::DEFAULT) {
Garfield Tan15601662020-09-22 15:32:38 -07006202 return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name
6203 << " without a specified display.";
Michael Wright3dd60e22019-03-27 22:06:44 +00006204 }
6205
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006206 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006207 const int fd = serverChannel->getFd();
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006208 std::shared_ptr<Connection> connection =
6209 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true,
6210 mIdGenerator);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006211
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006212 auto [_, inserted] = mConnectionsByToken.emplace(token, connection);
6213 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006214 ALOGE("Created a new connection, but the token %p is already known", token.get());
6215 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006216
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006217 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6218 this, std::placeholders::_1, token);
Michael Wright3dd60e22019-03-27 22:06:44 +00006219
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006220 mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid);
Michael Wright3dd60e22019-03-27 22:06:44 +00006221
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006222 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006223 nullptr);
Michael Wright3dd60e22019-03-27 22:06:44 +00006224 }
Garfield Tan15601662020-09-22 15:32:38 -07006225
Michael Wright3dd60e22019-03-27 22:06:44 +00006226 // Wake the looper because some connections have changed.
6227 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006228 return clientChannel;
Michael Wright3dd60e22019-03-27 22:06:44 +00006229}
6230
Garfield Tan15601662020-09-22 15:32:38 -07006231status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006233 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234
Harry Cutts33476232023-01-30 19:57:29 +00006235 status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 if (status) {
6237 return status;
6238 }
6239 } // release lock
6240
6241 // Wake the poll loop because removing the connection may have changed the current
6242 // synchronization state.
6243 mLooper->wake();
6244 return OK;
6245}
6246
Garfield Tan15601662020-09-22 15:32:38 -07006247status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken,
6248 bool notify) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006249 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006250 if (connection == nullptr) {
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00006251 // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel'
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 return BAD_VALUE;
6253 }
6254
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006255 removeConnectionLocked(connection);
Robert Carr5c8a0262018-10-03 16:30:44 -07006256
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 if (connection->monitor) {
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006258 removeMonitorChannelLocked(connectionToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 }
6260
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006261 mLooper->removeFd(connection->inputPublisher.getChannel().getFd());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262
6263 nsecs_t currentTime = now();
6264 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
6265
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006266 connection->status = Connection::Status::ZOMBIE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 return OK;
6268}
6269
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006270void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006271 for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) {
6272 auto& [displayId, monitors] = *it;
6273 std::erase_if(monitors, [connectionToken](const Monitor& monitor) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006274 return monitor.connection->getToken() == connectionToken;
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006275 });
Michael Wright3dd60e22019-03-27 22:06:44 +00006276
Michael Wright3dd60e22019-03-27 22:06:44 +00006277 if (monitors.empty()) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006278 it = mGlobalMonitorsByDisplay.erase(it);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006279 } else {
6280 ++it;
6281 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 }
6283}
6284
Michael Wright3dd60e22019-03-27 22:06:44 +00006285status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006286 std::scoped_lock _l(mLock);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006287 return pilferPointersLocked(token);
6288}
Michael Wright3dd60e22019-03-27 22:06:44 +00006289
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006290status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006291 const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token);
6292 if (!requestingConnection) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006293 LOG(WARNING)
6294 << "Attempted to pilfer pointers from an un-registered channel or invalid token";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006295 return BAD_VALUE;
Michael Wright3dd60e22019-03-27 22:06:44 +00006296 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006297
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07006298 auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006299 if (statePtr == nullptr || windowPtr == nullptr) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006300 LOG(WARNING)
6301 << "Attempted to pilfer points from a channel without any on-going pointer streams."
6302 " Ignoring.";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006303 return BAD_VALUE;
6304 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006305 std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07006306 if (deviceIds.empty()) {
6307 LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006308 return BAD_VALUE;
6309 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006310
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006311 ScopedSyntheticEventTracer traceContext(mTracer);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006312 for (const DeviceId deviceId : deviceIds) {
6313 TouchState& state = *statePtr;
6314 TouchedWindow& window = *windowPtr;
6315 // Send cancel events to all the input channels we're stealing from.
6316 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006317 "input channel stole pointer stream", traceContext.getTracker());
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006318 options.deviceId = deviceId;
6319 options.displayId = displayId;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006320 std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId);
6321 std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006322 options.pointerIds = pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006323
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006324 std::string canceledWindows;
6325 for (const TouchedWindow& w : state.windows) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006326 if (w.windowHandle->getToken() != token) {
6327 synthesizeCancelationEventsForWindowLocked(w.windowHandle, options);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006328 canceledWindows += canceledWindows.empty() ? "[" : ", ";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006329 canceledWindows += w.windowHandle->getName();
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006330 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006331 }
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006332 canceledWindows += canceledWindows.empty() ? "[]" : "]";
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006333 LOG(INFO) << "Channel " << requestingConnection->getInputChannelName()
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006334 << " is stealing input gesture for device " << deviceId << " from "
6335 << canceledWindows;
6336
6337 // Prevent the gesture from being sent to any other windows.
6338 // This only blocks relevant pointers to be sent to other windows
6339 window.addPilferingPointers(deviceId, pointerIds);
6340
6341 state.cancelPointersForWindowsExcept(deviceId, pointerIds, token);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006342 }
Michael Wright3dd60e22019-03-27 22:06:44 +00006343 return OK;
6344}
6345
Prabir Pradhan99987712020-11-10 18:43:05 -08006346void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) {
6347 { // acquire lock
6348 std::scoped_lock _l(mLock);
6349 if (DEBUG_FOCUS) {
chaviw98318de2021-05-19 16:45:23 -05006350 const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken);
Prabir Pradhan99987712020-11-10 18:43:05 -08006351 ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable",
6352 windowHandle != nullptr ? windowHandle->getName().c_str()
6353 : "token without window");
6354 }
6355
Vishnu Nairc519ff72021-01-21 08:23:08 -08006356 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08006357 if (focusedToken != windowToken) {
6358 ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.",
6359 enabled ? "enable" : "disable");
6360 return;
6361 }
6362
Hiroki Sato25040232024-02-22 17:21:22 +09006363 if (enabled == mCurrentPointerCaptureRequest.isEnable()) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006364 ALOGW("Ignoring request to %s Pointer Capture: "
6365 "window has %s requested pointer capture.",
6366 enabled ? "enable" : "disable", enabled ? "already" : "not");
6367 return;
6368 }
6369
Christine Franksb768bb42021-11-29 12:11:31 -08006370 if (enabled) {
6371 if (std::find(mIneligibleDisplaysForPointerCapture.begin(),
6372 mIneligibleDisplaysForPointerCapture.end(),
6373 mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) {
6374 ALOGW("Ignoring request to enable Pointer Capture: display is not eligible");
6375 return;
6376 }
6377 }
6378
Hiroki Sato25040232024-02-22 17:21:22 +09006379 setPointerCaptureLocked(enabled ? windowToken : nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08006380 } // release lock
6381
6382 // Wake the thread to process command entries.
6383 mLooper->wake();
6384}
6385
Linnan Li13bf76a2024-05-05 19:18:02 +08006386void InputDispatcher::setDisplayEligibilityForPointerCapture(ui::LogicalDisplayId displayId,
6387 bool isEligible) {
Christine Franksb768bb42021-11-29 12:11:31 -08006388 { // acquire lock
6389 std::scoped_lock _l(mLock);
6390 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
6391 if (!isEligible) {
6392 mIneligibleDisplaysForPointerCapture.push_back(displayId);
6393 }
6394 } // release lock
6395}
6396
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006397std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006398 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00006399 for (const Monitor& monitor : monitors) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006400 if (monitor.connection->getToken() == token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006401 return monitor.pid;
Michael Wright3dd60e22019-03-27 22:06:44 +00006402 }
6403 }
6404 }
6405 return std::nullopt;
6406}
6407
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006408std::shared_ptr<Connection> InputDispatcher::getConnectionLocked(
6409 const sp<IBinder>& inputConnectionToken) const {
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -07006410 if (inputConnectionToken == nullptr) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006411 return nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +08006412 }
6413
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006414 for (const auto& [token, connection] : mConnectionsByToken) {
6415 if (token == inputConnectionToken) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006416 return connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 }
6418 }
Robert Carr4e670e52018-08-15 13:26:12 -07006419
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006420 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421}
6422
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006423std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006424 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006425 if (connection == nullptr) {
6426 return "<nullptr>";
6427 }
6428 return connection->getInputChannelName();
6429}
6430
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006431void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006432 mAnrTracker.eraseToken(connection->getToken());
6433 mConnectionsByToken.erase(connection->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006434}
6435
Prabir Pradhancef936d2021-07-21 16:17:52 +00006436void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006437 const std::shared_ptr<Connection>& connection,
6438 uint32_t seq, bool handled,
6439 nsecs_t consumeTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006440 // Handle post-event policy actions.
Prabir Pradhan24047542023-11-02 17:14:59 +00006441 std::unique_ptr<const KeyEntry> fallbackKeyEntry;
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006442
6443 { // Start critical section
6444 auto dispatchEntryIt =
6445 std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6446 [seq](auto& e) { return e->seq == seq; });
6447 if (dispatchEntryIt == connection->waitQueue.end()) {
6448 return;
6449 }
6450
6451 DispatchEntry& dispatchEntry = **dispatchEntryIt;
6452
6453 const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime;
6454 if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006455 ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006456 ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str());
6457 }
6458 if (shouldReportFinishedEvent(dispatchEntry, *connection)) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006459 mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006460 dispatchEntry.deliveryTime, consumeTime, finishTime);
6461 }
6462
6463 if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006464 fallbackKeyEntry =
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006465 afterKeyEventLockedInterruptable(connection, &dispatchEntry, handled);
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006466 }
6467 } // End critical section: The -LockedInterruptable methods may have released the lock.
Prabir Pradhancef936d2021-07-21 16:17:52 +00006468
6469 // Dequeue the event and start the next cycle.
6470 // Because the lock might have been released, it is possible that the
6471 // contents of the wait queue to have been drained, so we need to double-check
6472 // a few things.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006473 auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6474 [seq](auto& e) { return e->seq == seq; });
6475 if (entryIt != connection->waitQueue.end()) {
6476 std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt);
6477 connection->waitQueue.erase(entryIt);
6478
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006479 const sp<IBinder>& connectionToken = connection->getToken();
Prabir Pradhancef936d2021-07-21 16:17:52 +00006480 mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken);
6481 if (!connection->responsive) {
6482 connection->responsive = isConnectionResponsive(*connection);
6483 if (connection->responsive) {
6484 // The connection was unresponsive, and now it's responsive.
6485 processConnectionResponsiveLocked(*connection);
6486 }
6487 }
6488 traceWaitQueueLength(*connection);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006489 if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006490 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6491 // Only dispatch fallbacks if there is a window for the connection.
6492 if (windowHandle != nullptr) {
6493 const auto inputTarget =
6494 createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
6495 dispatchEntry->targetFlags,
6496 fallbackKeyEntry->downTime);
6497 if (inputTarget.has_value()) {
6498 enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry),
6499 *inputTarget);
6500 }
6501 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00006502 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006503 releaseDispatchEntry(std::move(dispatchEntry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00006504 }
6505
6506 // Start the next dispatch cycle for this connection.
6507 startDispatchCycleLocked(now(), connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508}
6509
Prabir Pradhancef936d2021-07-21 16:17:52 +00006510void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken,
6511 const sp<IBinder>& newToken) {
6512 auto command = [this, oldToken, newToken]() REQUIRES(mLock) {
6513 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006514 mPolicy.notifyFocusChanged(oldToken, newToken);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006515 };
6516 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517}
6518
Prabir Pradhancef936d2021-07-21 16:17:52 +00006519void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) {
6520 auto command = [this, token, x, y]() REQUIRES(mLock) {
6521 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006522 mPolicy.notifyDropWindow(token, x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006523 };
6524 postCommandLocked(std::move(command));
Robert Carrf759f162018-11-13 12:57:11 -08006525}
6526
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006527void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006528 if (connection == nullptr) {
6529 LOG_ALWAYS_FATAL("Caller must check for nullness");
6530 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006531 // Since we are allowing the policy to extend the timeout, maybe the waitQueue
6532 // is already healthy again. Don't raise ANR in this situation
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006533 if (connection->waitQueue.empty()) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006534 ALOGI("Not raising ANR because the connection %s has recovered",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006535 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006536 return;
6537 }
6538 /**
6539 * The "oldestEntry" is the entry that was first sent to the application. That entry, however,
6540 * may not be the one that caused the timeout to occur. One possibility is that window timeout
6541 * has changed. This could cause newer entries to time out before the already dispatched
6542 * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app
6543 * processes the events linearly. So providing information about the oldest entry seems to be
6544 * most useful.
6545 */
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006546 DispatchEntry& oldestEntry = *connection->waitQueue.front();
6547 const nsecs_t currentWait = now() - oldestEntry.deliveryTime;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006548 std::string reason =
6549 android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006550 connection->getInputChannelName().c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006551 ns2ms(currentWait),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006552 oldestEntry.eventEntry->getDescription().c_str());
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006553 sp<IBinder> connectionToken = connection->getToken();
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -06006554 updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006555
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006556 processConnectionUnresponsiveLocked(*connection, std::move(reason));
6557
6558 // Stop waking up for events on this connection, it is already unresponsive
6559 cancelEventsForAnrLocked(connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006560}
6561
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006562void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) {
6563 std::string reason =
6564 StringPrintf("%s does not have a focused window", application->getName().c_str());
6565 updateLastAnrStateLocked(*application, reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006566
Yabin Cui8eb9c552023-06-08 18:05:07 +00006567 auto command = [this, app = std::move(application)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006568 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006569 mPolicy.notifyNoFocusedWindowAnr(app);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006570 };
6571 postCommandLocked(std::move(command));
Bernardo Rufino2e1f6512020-10-08 13:42:07 +00006572}
6573
chaviw98318de2021-05-19 16:45:23 -05006574void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006575 const std::string& reason) {
6576 const std::string windowLabel = getApplicationWindowLabel(nullptr, window);
6577 updateLastAnrStateLocked(windowLabel, reason);
6578}
6579
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006580void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application,
6581 const std::string& reason) {
6582 const std::string windowLabel = getApplicationWindowLabel(&application, nullptr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006583 updateLastAnrStateLocked(windowLabel, reason);
6584}
6585
6586void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
6587 const std::string& reason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588 // Capture a record of the InputDispatcher state at the time of the ANR.
Yi Kong9b14ac62018-07-17 13:48:38 -07006589 time_t t = time(nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590 struct tm tm;
6591 localtime_r(&t, &tm);
6592 char timestr[64];
6593 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006594 mLastAnrState.clear();
6595 mLastAnrState += INDENT "ANR:\n";
6596 mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006597 mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str());
6598 mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str());
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006599 dumpDispatchStateLocked(mLastAnrState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600}
6601
Prabir Pradhancef936d2021-07-21 16:17:52 +00006602void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
Prabir Pradhan24047542023-11-02 17:14:59 +00006603 const KeyEntry& entry) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006604 const KeyEvent event = createKeyEvent(entry);
6605 nsecs_t delay = 0;
6606 { // release lock
6607 scoped_unlock unlock(mLock);
6608 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00006609 delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006610 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
6611 ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
6612 std::to_string(t.duration().count()).c_str());
6613 }
6614 } // acquire lock
Michael Wrightd02c5b62014-02-10 15:10:22 -08006615
6616 if (delay < 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006617 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
Prabir Pradhancef936d2021-07-21 16:17:52 +00006618 } else if (delay == 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006619 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006620 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00006621 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006622 entry.interceptKeyWakeupTime = now() + delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006624}
6625
Prabir Pradhancef936d2021-07-21 16:17:52 +00006626void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006627 std::optional<gui::Pid> pid,
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006628 std::string reason) {
Yabin Cui8eb9c552023-06-08 18:05:07 +00006629 auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006630 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006631 mPolicy.notifyWindowUnresponsive(token, pid, r);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006632 };
6633 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006634}
6635
Prabir Pradhanedd96402022-02-15 01:46:16 -08006636void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006637 std::optional<gui::Pid> pid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006638 auto command = [this, token, pid]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006639 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006640 mPolicy.notifyWindowResponsive(token, pid);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006641 };
6642 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006643}
6644
6645/**
6646 * Tell the policy that a connection has become unresponsive so that it can start ANR.
6647 * Check whether the connection of interest is a monitor or a window, and add the corresponding
6648 * command entry to the command queue.
6649 */
6650void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection,
6651 std::string reason) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006652 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006653 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006654 if (connection.monitor) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006655 ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006656 reason.c_str());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006657 pid = findMonitorPidByTokenLocked(connectionToken);
6658 } else {
6659 // The connection is a window
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006660 ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Prabir Pradhanedd96402022-02-15 01:46:16 -08006661 reason.c_str());
6662 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6663 if (handle != nullptr) {
6664 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006665 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006666 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006667 sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006668}
6669
6670/**
6671 * Tell the policy that a connection has become responsive so that it can stop ANR.
6672 */
6673void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006674 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006675 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006676 if (connection.monitor) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006677 pid = findMonitorPidByTokenLocked(connectionToken);
6678 } else {
6679 // The connection is a window
6680 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6681 if (handle != nullptr) {
6682 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006683 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006684 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006685 sendWindowResponsiveCommandLocked(connectionToken, pid);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006686}
6687
Prabir Pradhan24047542023-11-02 17:14:59 +00006688std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable(
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006689 const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, bool handled) {
6690 // The dispatchEntry is currently valid, but it might point to a deleted object after we release
6691 // the lock. For simplicity, make copies of the data of interest here and assume that
6692 // 'dispatchEntry' is not valid after this section.
6693 // Hold a strong reference to the EventEntry to ensure it's valid for the duration of this
6694 // function, even if the DispatchEntry gets destroyed and releases its share of the ownership.
6695 std::shared_ptr<const EventEntry> eventEntry = dispatchEntry->eventEntry;
6696 const bool hasForegroundTarget = dispatchEntry->hasForegroundTarget();
6697 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(eventEntry));
6698 // To prevent misuse, ensure dispatchEntry is no longer valid.
6699 dispatchEntry = nullptr;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006700 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006701 if (!handled) {
6702 // Report the key as unhandled, since the fallback was not handled.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006703 mReporter->reportUnhandledKey(keyEntry.id);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006704 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006705 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006706 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006708 // Get the fallback key state.
6709 // Clear it out after dispatching the UP.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006710 int32_t originalKeyCode = keyEntry.keyCode;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006711 std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006712 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006713 connection->inputState.removeFallbackKey(originalKeyCode);
6714 }
6715
Siarhei Vishniakouf50323a2024-05-29 08:52:08 -07006716 if (handled || !hasForegroundTarget) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006717 // If the application handles the original key for which we previously
6718 // generated a fallback or if the window is not a foreground window,
6719 // then cancel the associated fallback key, if any.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006720 if (fallbackKeyCode) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006721 // Dispatch the unhandled key to the policy with the cancel flag.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006722 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6723 ALOGD("Unhandled key event: Asking policy to cancel fallback action. "
6724 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6725 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount,
6726 keyEntry.policyFlags);
6727 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006728 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006729 event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730
6731 mLock.unlock();
6732
Prabir Pradhana41d2442023-04-20 21:30:40 +00006733 if (const auto unhandledKeyFallback =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006734 mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6735 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006736 unhandledKeyFallback) {
6737 event = *unhandledKeyFallback;
6738 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006739
6740 mLock.lock();
6741
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006742 // Cancel the fallback key, but only if we still have a window for the channel.
6743 // It could have been removed during the policy call.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006744 if (*fallbackKeyCode != AKEYCODE_UNKNOWN) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006745 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6746 if (windowHandle != nullptr) {
6747 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
6748 "application handled the original non-fallback key "
6749 "or is no longer a foreground target, "
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006750 "canceling previously dispatched fallback key",
6751 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006752 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006753 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006754 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006755 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006756 connection->inputState.removeFallbackKey(originalKeyCode);
6757 }
6758 } else {
6759 // If the application did not handle a non-fallback key, first check
6760 // that we are in a good state to perform unhandled key event processing
6761 // Then ask the policy what to do with it.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006762 bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006763 if (!fallbackKeyCode && !initialDown) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006764 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6765 ALOGD("Unhandled key event: Skipping unhandled key event processing "
6766 "since this is not an initial down. "
6767 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6768 originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6769 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006770 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006771 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006773 // Dispatch the unhandled key to the policy.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006774 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6775 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
6776 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6777 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6778 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006779 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006780
6781 mLock.unlock();
6782
Prabir Pradhana41d2442023-04-20 21:30:40 +00006783 bool fallback = false;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006784 if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6785 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006786 fb) {
6787 fallback = true;
6788 event = *fb;
6789 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006790
6791 mLock.lock();
6792
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006793 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006794 connection->inputState.removeFallbackKey(originalKeyCode);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006795 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006796 }
6797
6798 // Latch the fallback keycode for this key on an initial down.
6799 // The fallback keycode cannot change at any other point in the lifecycle.
6800 if (initialDown) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006801 if (fallback) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006802 *fallbackKeyCode = event.getKeyCode();
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006803 } else {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006804 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006805 }
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006806 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006807 }
6808
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006809 ALOG_ASSERT(fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006810
6811 // Cancel the fallback key if the policy decides not to send it anymore.
6812 // We will continue to dispatch the key to the policy but we will no
6813 // longer dispatch a fallback key to the application.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006814 if (*fallbackKeyCode != AKEYCODE_UNKNOWN &&
6815 (!fallback || *fallbackKeyCode != event.getKeyCode())) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006816 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6817 if (fallback) {
6818 ALOGD("Unhandled key event: Policy requested to send key %d"
6819 "as a fallback for %d, but on the DOWN it had requested "
6820 "to send %d instead. Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006821 event.getKeyCode(), originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006822 } else {
6823 ALOGD("Unhandled key event: Policy did not request fallback for %d, "
6824 "but on the DOWN it had requested to send %d. "
6825 "Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006826 originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006827 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006828 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006829
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006830 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6831 if (windowHandle != nullptr) {
6832 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006833 "canceling fallback, policy no longer desires it",
6834 keyEntry.traceTracker);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006835 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006836 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006837 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006838
6839 fallback = false;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006840 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006841 if (keyEntry.action != AKEY_EVENT_ACTION_UP) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006842 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006843 }
6844 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006846 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6847 {
6848 std::string msg;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006849 const std::map<int32_t, int32_t>& fallbackKeys =
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006850 connection->inputState.getFallbackKeys();
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006851 for (const auto& [key, value] : fallbackKeys) {
6852 msg += StringPrintf(", %d->%d", key, value);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006853 }
6854 ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.",
6855 fallbackKeys.size(), msg.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006857 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006858
6859 if (fallback) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006860 // Return the fallback key that we want dispatched to the channel.
6861 std::unique_ptr<KeyEntry> newEntry =
6862 std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState,
6863 event.getEventTime(), event.getDeviceId(),
6864 event.getSource(), event.getDisplayId(),
6865 keyEntry.policyFlags, keyEntry.action,
6866 event.getFlags() | AKEY_EVENT_FLAG_FALLBACK,
6867 *fallbackKeyCode, event.getScanCode(),
6868 event.getMetaState(), event.getRepeatCount(),
6869 event.getDownTime());
Prabir Pradhana67623c2024-02-21 06:57:36 +00006870 if (mTracer) {
6871 newEntry->traceTracker =
6872 mTracer->traceDerivedEvent(*newEntry, *keyEntry.traceTracker);
6873 }
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006874 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6875 ALOGD("Unhandled key event: Dispatching fallback key. "
6876 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006877 originalKeyCode, *fallbackKeyCode, keyEntry.metaState);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006878 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006879 return newEntry;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006880 } else {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006881 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6882 ALOGD("Unhandled key event: No fallback key.");
6883 }
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006884
6885 // Report the key as unhandled, since there is no fallback key.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006886 mReporter->reportUnhandledKey(keyEntry.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006887 }
6888 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006889 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890}
6891
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892void InputDispatcher::traceInboundQueueLengthLocked() {
6893 if (ATRACE_ENABLED()) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006894 ATRACE_INT("iq", mInboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006895 }
6896}
6897
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006898void InputDispatcher::traceOutboundQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899 if (ATRACE_ENABLED()) {
6900 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006901 snprintf(counterName, sizeof(counterName), "oq:%s",
6902 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006903 ATRACE_INT(counterName, connection.outboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904 }
6905}
6906
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006907void InputDispatcher::traceWaitQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908 if (ATRACE_ENABLED()) {
6909 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006910 snprintf(counterName, sizeof(counterName), "wq:%s",
6911 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006912 ATRACE_INT(counterName, connection.waitQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913 }
6914}
6915
Siarhei Vishniakou5e20f272023-06-08 17:24:44 -07006916void InputDispatcher::dump(std::string& dump) const {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006917 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006919 dump += "Input Dispatcher State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920 dumpDispatchStateLocked(dump);
6921
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006922 if (!mLastAnrState.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006923 dump += "\nInput Dispatcher State at time of last ANR:\n";
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006924 dump += mLastAnrState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 }
6926}
6927
6928void InputDispatcher::monitor() {
6929 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006930 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 mLooper->wake();
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006932 mDispatcherIsAlive.wait(_l);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933}
6934
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006935/**
6936 * Wake up the dispatcher and wait until it processes all events and commands.
6937 * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so
6938 * this method can be safely called from any thread, as long as you've ensured that
6939 * the work you are interested in completing has already been queued.
6940 */
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07006941bool InputDispatcher::waitForIdle() const {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006942 /**
6943 * Timeout should represent the longest possible time that a device might spend processing
6944 * events and commands.
6945 */
6946 constexpr std::chrono::duration TIMEOUT = 100ms;
6947 std::unique_lock lock(mLock);
6948 mLooper->wake();
6949 std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT);
6950 return result == std::cv_status::no_timeout;
6951}
6952
Vishnu Naire798b472020-07-23 13:52:21 -07006953/**
6954 * Sets focus to the window identified by the token. This must be called
6955 * after updating any input window handles.
6956 *
6957 * Params:
6958 * request.token - input channel token used to identify the window that should gain focus.
6959 * request.focusedToken - the token that the caller expects currently to be focused. If the
6960 * specified token does not match the currently focused window, this request will be dropped.
6961 * If the specified focused token matches the currently focused window, the call will succeed.
6962 * Set this to "null" if this call should succeed no matter what the currently focused token is.
6963 * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm)
6964 * when requesting the focus change. This determines which request gets
6965 * precedence if there is a focus change request from another source such as pointer down.
6966 */
Vishnu Nair958da932020-08-21 17:12:37 -07006967void InputDispatcher::setFocusedWindow(const FocusRequest& request) {
6968 { // acquire lock
6969 std::scoped_lock _l(mLock);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006970 std::optional<FocusResolver::FocusChanges> changes =
Linnan Li13bf76a2024-05-05 19:18:02 +08006971 mFocusResolver.setFocusedWindow(request,
6972 getWindowHandlesLocked(
6973 ui::LogicalDisplayId{request.displayId}));
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006974 ScopedSyntheticEventTracer traceContext(mTracer);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006975 if (changes) {
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006976 onFocusChangedLocked(*changes, traceContext.getTracker());
Vishnu Nair958da932020-08-21 17:12:37 -07006977 }
6978 } // release lock
6979 // Wake up poll loop since it may need to make new input dispatching choices.
6980 mLooper->wake();
6981}
6982
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006983void InputDispatcher::onFocusChangedLocked(
6984 const FocusResolver::FocusChanges& changes,
6985 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker,
6986 const sp<WindowInfoHandle> removedFocusedWindowHandle) {
Vishnu Nairc519ff72021-01-21 08:23:08 -08006987 if (changes.oldFocus) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006988 const auto resolvedWindow = removedFocusedWindowHandle != nullptr
6989 ? removedFocusedWindowHandle
6990 : getWindowHandleLocked(changes.oldFocus, changes.displayId);
6991 if (resolvedWindow == nullptr) {
6992 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006993 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006994 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00006995 "focus left window", traceTracker);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006996 synthesizeCancelationEventsForWindowLocked(resolvedWindow, options);
6997 enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006998 }
Vishnu Nairc519ff72021-01-21 08:23:08 -08006999 if (changes.newFocus) {
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07007000 resetNoFocusedWindowTimeoutLocked();
Harry Cutts33476232023-01-30 19:57:29 +00007001 enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07007002 }
7003
Vishnu Nairc519ff72021-01-21 08:23:08 -08007004 if (mFocusedDisplayId == changes.displayId) {
Prabir Pradhan9d3d5612024-06-06 20:34:26 +00007005 // If a window has pointer capture, then it must have focus and must be on the top-focused
7006 // display. We need to ensure that this contract is upheld when pointer capture is being
7007 // disabled due to a loss of window focus. If the window loses focus before it loses pointer
7008 // capture, then the window can be in a state where it has pointer capture but not focus,
7009 // violating the contract. Therefore we must dispatch the pointer capture event before the
7010 // focus event. Since focus events are added to the front of the queue (above), we add the
7011 // pointer capture event to the front of the queue after the focus events are added. This
7012 // ensures the pointer capture event ends up at the front.
7013 disablePointerCaptureForcedLocked();
7014
Prabir Pradhancef936d2021-07-21 16:17:52 +00007015 sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07007016 }
7017}
Vishnu Nair958da932020-08-21 17:12:37 -07007018
Prabir Pradhan99987712020-11-10 18:43:05 -08007019void InputDispatcher::disablePointerCaptureForcedLocked() {
Hiroki Sato25040232024-02-22 17:21:22 +09007020 if (!mCurrentPointerCaptureRequest.isEnable() && !mWindowTokenWithPointerCapture) {
Prabir Pradhan99987712020-11-10 18:43:05 -08007021 return;
7022 }
7023
7024 ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus.");
7025
Hiroki Sato25040232024-02-22 17:21:22 +09007026 if (mCurrentPointerCaptureRequest.isEnable()) {
7027 setPointerCaptureLocked(nullptr);
Prabir Pradhan99987712020-11-10 18:43:05 -08007028 }
7029
7030 if (!mWindowTokenWithPointerCapture) {
7031 // No need to send capture changes because no window has capture.
7032 return;
7033 }
7034
7035 if (mPendingEvent != nullptr) {
7036 // Move the pending event to the front of the queue. This will give the chance
7037 // for the pending event to be dropped if it is a captured event.
7038 mInboundQueue.push_front(mPendingEvent);
7039 mPendingEvent = nullptr;
7040 }
7041
7042 auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007043 mCurrentPointerCaptureRequest);
Prabir Pradhan99987712020-11-10 18:43:05 -08007044 mInboundQueue.push_front(std::move(entry));
7045}
7046
Hiroki Sato25040232024-02-22 17:21:22 +09007047void InputDispatcher::setPointerCaptureLocked(const sp<IBinder>& windowToken) {
7048 mCurrentPointerCaptureRequest.window = windowToken;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00007049 mCurrentPointerCaptureRequest.seq++;
7050 auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00007051 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00007052 mPolicy.setPointerCapture(request);
Prabir Pradhancef936d2021-07-21 16:17:52 +00007053 };
7054 postCommandLocked(std::move(command));
Prabir Pradhan99987712020-11-10 18:43:05 -08007055}
7056
Linnan Li13bf76a2024-05-05 19:18:02 +08007057void InputDispatcher::displayRemoved(ui::LogicalDisplayId displayId) {
Vishnu Nair599f1412021-06-21 10:39:58 -07007058 { // acquire lock
7059 std::scoped_lock _l(mLock);
7060 // Set an empty list to remove all handles from the specific display.
Harry Cutts101ee9b2023-07-06 18:04:14 +00007061 setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07007062 setFocusedApplicationLocked(displayId, nullptr);
7063 // Call focus resolver to clean up stale requests. This must be called after input windows
7064 // have been removed for the removed display.
7065 mFocusResolver.displayRemoved(displayId);
Christine Franksb768bb42021-11-29 12:11:31 -08007066 // Reset pointer capture eligibility, regardless of previous state.
7067 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
Antonio Kantek15beb512022-06-13 22:35:41 +00007068 // Remove the associated touch mode state.
7069 mTouchModePerDisplay.erase(displayId);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07007070 mVerifiersByDisplay.erase(displayId);
Siarhei Vishniakou870f4e72024-05-10 20:59:35 +00007071 mInputFilterVerifiersByDisplay.erase(displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07007072 } // release lock
7073
7074 // Wake up poll loop since it may need to make new input dispatching choices.
7075 mLooper->wake();
7076}
7077
Patrick Williamsd828f302023-04-28 17:52:08 -05007078void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) {
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08007079 if (auto result = validateWindowInfosUpdate(update); !result.ok()) {
7080 {
7081 // acquire lock
7082 std::scoped_lock _l(mLock);
7083 logDispatchStateLocked();
7084 }
7085 LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s",
7086 result.error().message().c_str());
7087 };
chaviw15fab6f2021-06-07 14:15:52 -05007088 // The listener sends the windows as a flattened array. Separate the windows by display for
7089 // more convenient parsing.
Linnan Li13bf76a2024-05-05 19:18:02 +08007090 std::unordered_map<ui::LogicalDisplayId, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
Patrick Williamsd828f302023-04-28 17:52:08 -05007091 for (const auto& info : update.windowInfos) {
chaviw15fab6f2021-06-07 14:15:52 -05007092 handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07007093 handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info));
Siarhei Vishniakoua3fe6642024-07-18 11:58:25 -07007094 if (input_flags::split_all_touches()) {
7095 handlesPerDisplay[info.displayId]
7096 .back()
7097 ->editInfo()
7098 ->setInputConfig(android::gui::WindowInfo::InputConfig::PREVENT_SPLITTING,
7099 false);
7100 }
chaviw15fab6f2021-06-07 14:15:52 -05007101 }
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007102
7103 { // acquire lock
7104 std::scoped_lock _l(mLock);
Prabir Pradhan814fe082022-07-22 20:22:18 +00007105
7106 // Ensure that we have an entry created for all existing displays so that if a displayId has
7107 // no windows, we can tell that the windows were removed from the display.
7108 for (const auto& [displayId, _] : mWindowHandlesByDisplay) {
7109 handlesPerDisplay[displayId];
7110 }
7111
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007112 mDisplayInfos.clear();
Patrick Williamsd828f302023-04-28 17:52:08 -05007113 for (const auto& displayInfo : update.displayInfos) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007114 mDisplayInfos.emplace(displayInfo.displayId, displayInfo);
7115 }
7116
7117 for (const auto& [displayId, handles] : handlesPerDisplay) {
7118 setInputWindowsLocked(handles, displayId);
7119 }
Patrick Williams9464b2c2023-05-23 11:22:04 -05007120
7121 if (update.vsyncId < mWindowInfosVsyncId) {
7122 ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64
7123 ", current update vsync id: %" PRId64,
7124 mWindowInfosVsyncId, update.vsyncId);
7125 }
7126 mWindowInfosVsyncId = update.vsyncId;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07007127 }
7128 // Wake up poll loop since it may need to make new input dispatching choices.
7129 mLooper->wake();
chaviw15fab6f2021-06-07 14:15:52 -05007130}
7131
Vishnu Nair062a8672021-09-03 16:07:44 -07007132bool InputDispatcher::shouldDropInput(
7133 const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007134 if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) ||
7135 (windowHandle->getInfo()->inputConfig.test(
7136 WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) &&
Vishnu Nair062a8672021-09-03 16:07:44 -07007137 isWindowObscuredLocked(windowHandle))) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007138 ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on "
Linnan Li13bf76a2024-05-05 19:18:02 +08007139 "display %s.",
Vishnu Nair062a8672021-09-03 16:07:44 -07007140 ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(),
Prabir Pradhan51e7db02022-02-07 06:02:57 -08007141 windowHandle->getInfo()->inputConfig.string().c_str(),
Linnan Li13bf76a2024-05-05 19:18:02 +08007142 windowHandle->getInfo()->displayId.toString().c_str());
Vishnu Nair062a8672021-09-03 16:07:44 -07007143 return true;
7144 }
7145 return false;
7146}
7147
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007148void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -05007149 const gui::WindowInfosUpdate& update) {
7150 mDispatcher.onWindowInfosChanged(update);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07007151}
7152
Arthur Hungdfd528e2021-12-08 13:23:04 +00007153void InputDispatcher::cancelCurrentTouch() {
7154 {
7155 std::scoped_lock _l(mLock);
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007156 ScopedSyntheticEventTracer traceContext(mTracer);
Arthur Hungdfd528e2021-12-08 13:23:04 +00007157 ALOGD("Canceling all ongoing pointer gestures on all displays.");
Michael Wrightfb04fd52022-11-24 22:31:11 +00007158 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007159 "cancel current touch", traceContext.getTracker());
Arthur Hungdfd528e2021-12-08 13:23:04 +00007160 synthesizeCancelationEventsForAllConnectionsLocked(options);
7161
7162 mTouchStatesByDisplay.clear();
Arthur Hungdfd528e2021-12-08 13:23:04 +00007163 }
7164 // Wake up poll loop since there might be work to do.
7165 mLooper->wake();
7166}
7167
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08007168void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) {
7169 std::scoped_lock _l(mLock);
7170 mMonitorDispatchingTimeout = timeout;
7171}
7172
Arthur Hungc539dbb2022-12-08 07:45:36 +00007173void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags,
7174 const sp<WindowInfoHandle>& oldWindowHandle,
7175 const sp<WindowInfoHandle>& newWindowHandle,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007176 TouchState& state, const MotionEntry& entry,
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07007177 std::vector<InputTarget>& targets) const {
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007178 LOG_IF(FATAL, entry.getPointerCount() != 1) << "Entry not eligible for slip: " << entry;
7179 const DeviceId deviceId = entry.deviceId;
7180 const PointerProperties& pointerProperties = entry.pointerProperties[0];
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007181 std::vector<PointerProperties> pointers{pointerProperties};
Arthur Hungc539dbb2022-12-08 07:45:36 +00007182 const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test(
7183 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7184 const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) &&
7185 newWindowHandle->getInfo()->inputConfig.test(
7186 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7187 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007188 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007189 const sp<WindowInfoHandle> newWallpaper =
7190 newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr;
7191 if (oldWallpaper == newWallpaper) {
7192 return;
7193 }
7194
7195 if (oldWallpaper != nullptr) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08007196 const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007197 addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007198 oldTouchedWindow.targetFlags, getPointerIds(pointers),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007199 oldTouchedWindow.getDownTimeInTarget(deviceId), targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007200 state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007201 }
7202
7203 if (newWallpaper != nullptr) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007204 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER,
7205 InputTarget::Flags::WINDOW_IS_OBSCURED |
Arthur Hungc539dbb2022-12-08 07:45:36 +00007206 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED,
Siarhei Vishniakoufe346982024-08-07 00:17:19 +00007207 deviceId, pointers, entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007208 }
7209}
7210
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007211void InputDispatcher::transferWallpaperTouch(
7212 ftl::Flags<InputTarget::Flags> oldTargetFlags,
7213 ftl::Flags<InputTarget::Flags> newTargetFlags, const sp<WindowInfoHandle> fromWindowHandle,
Linnan Li72352222024-04-12 18:55:57 +08007214 const sp<WindowInfoHandle> toWindowHandle, TouchState& state, DeviceId deviceId,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007215 const std::vector<PointerProperties>& pointers,
7216 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) {
Arthur Hungc539dbb2022-12-08 07:45:36 +00007217 const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7218 fromWindowHandle->getInfo()->inputConfig.test(
7219 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7220 const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
7221 toWindowHandle->getInfo()->inputConfig.test(
7222 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
7223
7224 const sp<WindowInfoHandle> oldWallpaper =
Linnan Li72352222024-04-12 18:55:57 +08007225 oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007226 const sp<WindowInfoHandle> newWallpaper =
7227 newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr;
7228 if (oldWallpaper == newWallpaper) {
7229 return;
7230 }
7231
7232 if (oldWallpaper != nullptr) {
7233 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007234 "transferring touch focus to another window", traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007235 state.removeWindowByToken(oldWallpaper->getToken());
7236 synthesizeCancelationEventsForWindowLocked(oldWallpaper, options);
7237 }
7238
7239 if (newWallpaper != nullptr) {
7240 nsecs_t downTimeInTarget = now();
Prabir Pradhan65455c72024-02-13 21:46:41 +00007241 ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags;
7242 wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007243 wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED |
7244 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007245 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007246 deviceId, pointers, downTimeInTarget);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007247 std::shared_ptr<Connection> wallpaperConnection =
7248 getConnectionLocked(newWallpaper->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007249 if (wallpaperConnection != nullptr) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007250 std::shared_ptr<Connection> toConnection =
7251 getConnectionLocked(toWindowHandle->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007252 toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState);
7253 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection,
Prabir Pradhand6b2b052024-02-21 23:25:15 +00007254 wallpaperFlags, traceTracker);
Arthur Hungc539dbb2022-12-08 07:45:36 +00007255 }
7256 }
7257}
7258
7259sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow(
7260 const sp<WindowInfoHandle>& windowHandle) const {
7261 const std::vector<sp<WindowInfoHandle>>& windowHandles =
7262 getWindowHandlesLocked(windowHandle->getInfo()->displayId);
7263 bool foundWindow = false;
7264 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
7265 if (!foundWindow && otherHandle != windowHandle) {
7266 continue;
7267 }
7268 if (windowHandle == otherHandle) {
7269 foundWindow = true;
7270 continue;
7271 }
7272
7273 if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) {
7274 return otherHandle;
7275 }
7276 }
7277 return nullptr;
7278}
7279
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007280void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout,
Asmita Poddar2808d7e2024-09-12 13:15:38 +00007281 std::chrono::nanoseconds delay,
7282 bool keyRepeatEnabled) {
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007283 std::scoped_lock _l(mLock);
7284
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007285 mConfig.keyRepeatTimeout = timeout.count();
7286 mConfig.keyRepeatDelay = delay.count();
Asmita Poddar2808d7e2024-09-12 13:15:38 +00007287 mConfig.keyRepeatEnabled = keyRepeatEnabled;
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007288}
7289
Linnan Li13bf76a2024-05-05 19:18:02 +08007290bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token,
7291 ui::LogicalDisplayId displayId, DeviceId deviceId,
7292 int32_t pointerId) {
Prabir Pradhan64f21d22023-11-28 21:19:42 +00007293 std::scoped_lock _l(mLock);
7294 auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7295 if (touchStateIt == mTouchStatesByDisplay.end()) {
7296 return false;
7297 }
7298 for (const TouchedWindow& window : touchStateIt->second.windows) {
7299 if (window.windowHandle->getToken() == token &&
7300 (window.hasTouchingPointer(deviceId, pointerId) ||
7301 window.hasHoveringPointer(deviceId, pointerId))) {
7302 return true;
7303 }
7304 }
7305 return false;
7306}
7307
Prabir Pradhan04a66422024-03-15 22:45:27 +00007308void InputDispatcher::setInputMethodConnectionIsActive(bool isActive) {
7309 std::scoped_lock _l(mLock);
7310 if (mTracer) {
7311 mTracer->setInputMethodConnectionIsActive(isActive);
7312 }
7313}
7314
Garfield Tane84e6f92019-08-29 17:28:41 -07007315} // namespace android::inputdispatcher