blob: 3a18f256e284f6e9508517d66bfb14ff9d510f7d [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputDispatcher"
18#define ATRACE_TAG ATRACE_TAG_INPUT
19
John Recke0710582019-09-26 13:46:12 -070020#define LOG_NDEBUG 1
Michael Wrightd02c5b62014-02-10 15:10:22 -080021
Michael Wright2b3c3302018-03-02 17:19:13 +000022#include <android-base/chrono_utils.h>
Siarhei Vishniakoud010b012023-01-18 15:00:53 -080023#include <android-base/logging.h>
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080024#include <android-base/properties.h>
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080025#include <android-base/stringprintf.h>
Siarhei Vishniakou70622952020-07-30 11:17:23 -050026#include <android/os/IInputConstants.h>
Robert Carr4e670e52018-08-15 13:26:12 -070027#include <binder/Binder.h>
Ameer Armalycff4fa52023-10-04 23:45:11 +000028#include <com_android_input_flags.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080029#include <ftl/enum.h>
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070030#include <log/log_event_list.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070031#if defined(__ANDROID__)
chaviw15fab6f2021-06-07 14:15:52 -050032#include <gui/SurfaceComposerClient.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070033#endif
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -080034#include <input/InputDevice.h>
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080035#include <input/PrintTools.h>
Prabir Pradhana37bad12023-08-18 15:55:32 +000036#include <input/TraceTools.h>
tyiu1573a672023-02-21 22:38:32 +000037#include <openssl/mem.h>
Prabir Pradhanadc59b42023-12-15 05:34:11 +000038#include <private/android_filesystem_config.h>
Michael Wright44753b12020-07-08 13:48:11 +010039#include <unistd.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070040#include <utils/Trace.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wright44753b12020-07-08 13:48:11 +010042#include <cerrno>
43#include <cinttypes>
44#include <climits>
45#include <cstddef>
46#include <ctime>
47#include <queue>
48#include <sstream>
49
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000050#include "../InputDeviceMetricsSource.h"
51
Michael Wright44753b12020-07-08 13:48:11 +010052#include "Connection.h"
Arthur Hung1a1007b2022-05-11 07:15:01 +000053#include "DebugConfig.h"
Chris Yef59a2f42020-10-16 12:55:26 -070054#include "InputDispatcher.h"
Prabir Pradhandae52792023-12-15 07:36:40 +000055#include "trace/InputTracer.h"
56#include "trace/InputTracingPerfettoBackend.h"
Michael Wright44753b12020-07-08 13:48:11 +010057
Michael Wrightd02c5b62014-02-10 15:10:22 -080058#define INDENT " "
59#define INDENT2 " "
60#define INDENT3 " "
61#define INDENT4 " "
62
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080063using namespace android::ftl::flag_operators;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -070064using android::base::Error;
Peter Collingbourneb04b9b82021-02-08 12:09:47 -080065using android::base::HwTimeoutMultiplier;
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000066using android::base::Result;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080067using android::base::StringPrintf;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -070068using android::gui::DisplayInfo;
chaviw98318de2021-05-19 16:45:23 -050069using android::gui::FocusRequest;
70using android::gui::TouchOcclusionMode;
71using android::gui::WindowInfo;
72using android::gui::WindowInfoHandle;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080073using android::os::InputEventInjectionResult;
74using android::os::InputEventInjectionSync;
Ameer Armalycff4fa52023-10-04 23:45:11 +000075namespace input_flags = com::android::input::flags;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080076
Garfield Tane84e6f92019-08-29 17:28:41 -070077namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
Prabir Pradhancef936d2021-07-21 16:17:52 +000079namespace {
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000080
Prabir Pradhandae52792023-12-15 07:36:40 +000081// Input tracing is only available on debuggable builds (userdebug and eng) when the feature
82// flag is enabled. When the flag is changed, tracing will only be available after reboot.
83bool isInputTracingEnabled() {
84 static const std::string buildType = base::GetProperty("ro.build.type", "user");
85 static const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng";
86 return input_flags::enable_input_event_tracing() && isUserdebugOrEng;
87}
88
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000089template <class Entry>
90void ensureEventTraced(const Entry& entry) {
91 if (!entry.traceTracker) {
92 LOG(FATAL) << "Expected event entry to be traced, but it wasn't: " << entry;
93 }
94}
95
Prabir Pradhancef936d2021-07-21 16:17:52 +000096// Temporarily releases a held mutex for the lifetime of the instance.
97// Named to match std::scoped_lock
98class scoped_unlock {
99public:
100 explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); }
101 ~scoped_unlock() { mMutex.lock(); }
102
103private:
104 std::mutex& mMutex;
105};
106
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107// Default input dispatching timeout if there is no focused application or paused window
108// from which to determine an appropriate dispatching timeout.
Peter Collingbourneb04b9b82021-02-08 12:09:47 -0800109const std::chrono::duration DEFAULT_INPUT_DISPATCHING_TIMEOUT = std::chrono::milliseconds(
110 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
111 HwTimeoutMultiplier());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800113// The default minimum time gap between two user activity poke events.
114const std::chrono::milliseconds DEFAULT_USER_ACTIVITY_POKE_INTERVAL = 100ms;
115
Siarhei Vishniakou289e9242022-02-15 14:50:16 -0800116const std::chrono::duration STALE_EVENT_TIMEOUT = std::chrono::seconds(10) * HwTimeoutMultiplier();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118// 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 +0000119constexpr nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
120
121// Log a warning when an interception call takes longer than this to process.
122constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123
124// Number of recent events to keep for debugging purposes.
Michael Wright2b3c3302018-03-02 17:19:13 +0000125constexpr size_t RECENT_QUEUE_MAX_SIZE = 10;
126
Antonio Kantekea47acb2021-12-23 12:41:25 -0800127// Event log tags. See EventLogTags.logtags for reference.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000128constexpr int LOGTAG_INPUT_INTERACTION = 62000;
129constexpr int LOGTAG_INPUT_FOCUS = 62001;
Arthur Hungb3307ee2021-10-14 10:57:37 +0000130constexpr int LOGTAG_INPUT_CANCEL = 62003;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000131
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000132const ui::Transform kIdentityTransform;
133
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000134inline nsecs_t now() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 return systemTime(SYSTEM_TIME_MONOTONIC);
136}
137
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700138inline const std::string binderToString(const sp<IBinder>& binder) {
Bernardo Rufino49d99e42021-01-18 15:16:59 +0000139 if (binder == nullptr) {
140 return "<null>";
141 }
142 return StringPrintf("%p", binder.get());
143}
144
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000145static std::string uidString(const gui::Uid& uid) {
146 return uid.toString();
147}
148
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700149Result<void> checkKeyAction(int32_t action) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 switch (action) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700151 case AKEY_EVENT_ACTION_DOWN:
152 case AKEY_EVENT_ACTION_UP:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700153 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700154 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700155 return Error() << "Key event has invalid action code " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 }
157}
158
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700159Result<void> validateKeyEvent(int32_t action) {
160 return checkKeyAction(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161}
162
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700163Result<void> checkMotionAction(int32_t action, int32_t actionButton, int32_t pointerCount) {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800164 switch (MotionEvent::getActionMasked(action)) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700165 case AMOTION_EVENT_ACTION_DOWN:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700166 case AMOTION_EVENT_ACTION_UP: {
167 if (pointerCount != 1) {
168 return Error() << "invalid pointer count " << pointerCount;
169 }
170 return {};
171 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700172 case AMOTION_EVENT_ACTION_MOVE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700173 case AMOTION_EVENT_ACTION_HOVER_ENTER:
174 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700175 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
176 if (pointerCount < 1) {
177 return Error() << "invalid pointer count " << pointerCount;
178 }
179 return {};
180 }
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800181 case AMOTION_EVENT_ACTION_CANCEL:
182 case AMOTION_EVENT_ACTION_OUTSIDE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700183 case AMOTION_EVENT_ACTION_SCROLL:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700184 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700185 case AMOTION_EVENT_ACTION_POINTER_DOWN:
186 case AMOTION_EVENT_ACTION_POINTER_UP: {
Siarhei Vishniakou2f61bdc2022-12-02 08:55:51 -0800187 const int32_t index = MotionEvent::getActionIndex(action);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700188 if (index < 0) {
189 return Error() << "invalid index " << index << " for "
190 << MotionEvent::actionToString(action);
191 }
192 if (index >= pointerCount) {
193 return Error() << "invalid index " << index << " for pointerCount " << pointerCount;
194 }
195 if (pointerCount <= 1) {
196 return Error() << "invalid pointer count " << pointerCount << " for "
197 << MotionEvent::actionToString(action);
198 }
199 return {};
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700200 }
201 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700202 case AMOTION_EVENT_ACTION_BUTTON_RELEASE: {
203 if (actionButton == 0) {
204 return Error() << "action button should be nonzero for "
205 << MotionEvent::actionToString(action);
206 }
207 return {};
208 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700209 default:
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700210 return Error() << "invalid action " << action;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211 }
212}
213
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000214int64_t millis(std::chrono::nanoseconds t) {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500215 return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
216}
217
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700218Result<void> validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount,
219 const PointerProperties* pointerProperties) {
220 Result<void> actionCheck = checkMotionAction(action, actionButton, pointerCount);
221 if (!actionCheck.ok()) {
222 return actionCheck;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223 }
224 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700225 return Error() << "Motion event has invalid pointer count " << pointerCount
226 << "; value must be between 1 and " << MAX_POINTERS << ".";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800228 std::bitset<MAX_POINTER_ID + 1> pointerIdBits;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229 for (size_t i = 0; i < pointerCount; i++) {
230 int32_t id = pointerProperties[i].id;
231 if (id < 0 || id > MAX_POINTER_ID) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700232 return Error() << "Motion event has invalid pointer id " << id
233 << "; value must be between 0 and " << MAX_POINTER_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800235 if (pointerIdBits.test(id)) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700236 return Error() << "Motion event has duplicate pointer id " << id;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800237 }
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800238 pointerIdBits.set(id);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -0700240 return {};
241}
242
243Result<void> validateInputEvent(const InputEvent& event) {
244 switch (event.getType()) {
245 case InputEventType::KEY: {
246 const KeyEvent& key = static_cast<const KeyEvent&>(event);
247 const int32_t action = key.getAction();
248 return validateKeyEvent(action);
249 }
250 case InputEventType::MOTION: {
251 const MotionEvent& motion = static_cast<const MotionEvent&>(event);
252 const int32_t action = motion.getAction();
253 const size_t pointerCount = motion.getPointerCount();
254 const PointerProperties* pointerProperties = motion.getPointerProperties();
255 const int32_t actionButton = motion.getActionButton();
256 return validateMotionEvent(action, actionButton, pointerCount, pointerProperties);
257 }
258 default: {
259 return {};
260 }
261 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262}
263
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800264std::bitset<MAX_POINTER_ID + 1> getPointerIds(const std::vector<PointerProperties>& pointers) {
265 std::bitset<MAX_POINTER_ID + 1> pointerIds;
266 for (const PointerProperties& pointer : pointers) {
267 pointerIds.set(pointer.id);
268 }
269 return pointerIds;
270}
271
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000272std::string dumpRegion(const Region& region) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 if (region.isEmpty()) {
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000274 return "<empty>";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 }
276
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000277 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 bool first = true;
279 Region::const_iterator cur = region.begin();
280 Region::const_iterator const tail = region.end();
281 while (cur != tail) {
282 if (first) {
283 first = false;
284 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800285 dump += "|";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800287 dump += StringPrintf("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 cur++;
289 }
Bernardo Rufino53fc31e2020-11-03 11:01:07 +0000290 return dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291}
292
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000293std::string dumpQueue(const std::deque<std::unique_ptr<DispatchEntry>>& queue,
294 nsecs_t currentTime) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500295 constexpr size_t maxEntries = 50; // max events to print
296 constexpr size_t skipBegin = maxEntries / 2;
297 const size_t skipEnd = queue.size() - maxEntries / 2;
298 // skip from maxEntries / 2 ... size() - maxEntries/2
299 // only print from 0 .. skipBegin and then from skipEnd .. size()
300
301 std::string dump;
302 for (size_t i = 0; i < queue.size(); i++) {
303 const DispatchEntry& entry = *queue[i];
304 if (i >= skipBegin && i < skipEnd) {
305 dump += StringPrintf(INDENT4 "<skipped %zu entries>\n", skipEnd - skipBegin);
306 i = skipEnd - 1; // it will be incremented to "skipEnd" by 'continue'
307 continue;
308 }
309 dump.append(INDENT4);
310 dump += entry.eventEntry->getDescription();
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +0000311 dump += StringPrintf(", seq=%" PRIu32 ", targetFlags=%s, age=%" PRId64 "ms", entry.seq,
312 entry.targetFlags.string().c_str(),
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500313 ns2ms(currentTime - entry.eventEntry->eventTime));
314 if (entry.deliveryTime != 0) {
315 // This entry was delivered, so add information on how long we've been waiting
316 dump += StringPrintf(", wait=%" PRId64 "ms", ns2ms(currentTime - entry.deliveryTime));
317 }
318 dump.append("\n");
319 }
320 return dump;
321}
322
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700323/**
324 * Find the entry in std::unordered_map by key, and return it.
325 * If the entry is not found, return a default constructed entry.
326 *
327 * Useful when the entries are vectors, since an empty vector will be returned
328 * if the entry is not found.
329 * Also useful when the entries are sp<>. If an entry is not found, nullptr is returned.
330 */
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700331template <typename K, typename V>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000332V getValueByKey(const std::unordered_map<K, V>& map, K key) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700333 auto it = map.find(key);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700334 return it != map.end() ? it->second : V{};
Tiger Huang721e26f2018-07-24 22:26:19 +0800335}
336
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000337bool haveSameToken(const sp<WindowInfoHandle>& first, const sp<WindowInfoHandle>& second) {
chaviwaf87b3e2019-10-01 16:59:28 -0700338 if (first == second) {
339 return true;
340 }
341
342 if (first == nullptr || second == nullptr) {
343 return false;
344 }
345
346 return first->getToken() == second->getToken();
347}
348
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000349bool haveSameApplicationToken(const WindowInfo* first, const WindowInfo* second) {
Bernardo Rufino1ff9d592021-01-18 16:58:57 +0000350 if (first == nullptr || second == nullptr) {
351 return false;
352 }
353 return first->applicationInfo.token != nullptr &&
354 first->applicationInfo.token == second->applicationInfo.token;
355}
356
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800357template <typename T>
358size_t firstMarkedBit(T set) {
359 // TODO: replace with std::countr_zero from <bit> when that's available
360 LOG_ALWAYS_FATAL_IF(set.none());
361 size_t i = 0;
362 while (!set.test(i)) {
363 i++;
364 }
365 return i;
366}
367
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000368std::unique_ptr<DispatchEntry> createDispatchEntry(const InputTarget& inputTarget,
369 std::shared_ptr<const EventEntry> eventEntry,
370 ftl::Flags<InputTarget::Flags> inputTargetFlags,
371 int64_t vsyncId) {
372 const sp<WindowInfoHandle> win = inputTarget.windowHandle;
373 const std::optional<int32_t> windowId =
374 win ? std::make_optional(win->getInfo()->id) : std::nullopt;
375 // Assume the only targets that are not associated with a window are global monitors, and use
376 // the system UID for global monitors for tracing purposes.
377 const gui::Uid uid = win ? win->getInfo()->ownerUid : gui::Uid(AID_SYSTEM);
chaviw1ff3d1e2020-07-01 15:53:47 -0700378 if (inputTarget.useDefaultPointerTransform()) {
379 const ui::Transform& transform = inputTarget.getDefaultPointerTransform();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700380 return std::make_unique<DispatchEntry>(eventEntry, inputTargetFlags, transform,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700381 inputTarget.displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000382 inputTarget.globalScaleFactor, uid, vsyncId,
383 windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000384 }
385
386 ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION);
387 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
388
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700389 std::vector<PointerCoords> pointerCoords;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700390 pointerCoords.resize(motionEntry.getPointerCount());
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000391
392 // Use the first pointer information to normalize all other pointers. This could be any pointer
393 // as long as all other pointers are normalized to the same value and the final DispatchEntry
chaviw1ff3d1e2020-07-01 15:53:47 -0700394 // uses the transform for the normalized pointer.
395 const ui::Transform& firstPointerTransform =
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800396 inputTarget.pointerTransforms[firstMarkedBit(inputTarget.pointerIds)];
chaviw1ff3d1e2020-07-01 15:53:47 -0700397 ui::Transform inverseFirstTransform = firstPointerTransform.inverse();
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000398
399 // Iterate through all pointers in the event to normalize against the first.
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700400 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount(); pointerIndex++) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000401 const PointerProperties& pointerProperties = motionEntry.pointerProperties[pointerIndex];
402 uint32_t pointerId = uint32_t(pointerProperties.id);
chaviw1ff3d1e2020-07-01 15:53:47 -0700403 const ui::Transform& currTransform = inputTarget.pointerTransforms[pointerId];
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000404
405 pointerCoords[pointerIndex].copyFrom(motionEntry.pointerCoords[pointerIndex]);
chaviw1ff3d1e2020-07-01 15:53:47 -0700406 // First, apply the current pointer's transform to update the coordinates into
407 // window space.
408 pointerCoords[pointerIndex].transform(currTransform);
409 // Next, apply the inverse transform of the normalized coordinates so the
410 // current coordinates are transformed into the normalized coordinate space.
411 pointerCoords[pointerIndex].transform(inverseFirstTransform);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000412 }
413
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700414 std::unique_ptr<MotionEntry> combinedMotionEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000415 std::make_unique<MotionEntry>(motionEntry.id, motionEntry.injectionState,
416 motionEntry.eventTime, motionEntry.deviceId,
417 motionEntry.source, motionEntry.displayId,
418 motionEntry.policyFlags, motionEntry.action,
419 motionEntry.actionButton, motionEntry.flags,
420 motionEntry.metaState, motionEntry.buttonState,
421 motionEntry.classification, motionEntry.edgeFlags,
422 motionEntry.xPrecision, motionEntry.yPrecision,
423 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
424 motionEntry.downTime, motionEntry.pointerProperties,
425 pointerCoords);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000426
427 std::unique_ptr<DispatchEntry> dispatchEntry =
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700428 std::make_unique<DispatchEntry>(std::move(combinedMotionEntry), inputTargetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700429 firstPointerTransform, inputTarget.displayTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000430 inputTarget.globalScaleFactor, uid, vsyncId, windowId);
Chavi Weingarten65f98b82020-01-16 18:56:50 +0000431 return dispatchEntry;
432}
433
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500434template <typename T>
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000435bool sharedPointersEqual(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) {
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -0500436 if (lhs == nullptr && rhs == nullptr) {
437 return true;
438 }
439 if (lhs == nullptr || rhs == nullptr) {
440 return false;
441 }
442 return *lhs == *rhs;
443}
444
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000445KeyEvent createKeyEvent(const KeyEntry& entry) {
Siarhei Vishniakou2e2ea992020-12-15 02:57:19 +0000446 KeyEvent event;
447 event.initialize(entry.id, entry.deviceId, entry.source, entry.displayId, INVALID_HMAC,
448 entry.action, entry.flags, entry.keyCode, entry.scanCode, entry.metaState,
449 entry.repeatCount, entry.downTime, entry.eventTime);
450 return event;
451}
452
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000453bool shouldReportMetricsForConnection(const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000454 // Do not keep track of gesture monitors. They receive every event and would disproportionately
455 // affect the statistics.
456 if (connection.monitor) {
457 return false;
458 }
459 // If the connection is experiencing ANR, let's skip it. We have separate ANR metrics
460 if (!connection.responsive) {
461 return false;
462 }
463 return true;
464}
465
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000466bool shouldReportFinishedEvent(const DispatchEntry& dispatchEntry, const Connection& connection) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000467 const EventEntry& eventEntry = *dispatchEntry.eventEntry;
468 const int32_t& inputEventId = eventEntry.id;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000469 if (inputEventId == android::os::IInputConstants::INVALID_INPUT_EVENT_ID) {
470 return false;
471 }
472 // Only track latency for events that originated from hardware
473 if (eventEntry.isSynthesized()) {
474 return false;
475 }
476 const EventEntry::Type& inputEventEntryType = eventEntry.type;
477 if (inputEventEntryType == EventEntry::Type::KEY) {
478 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
479 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
480 return false;
481 }
482 } else if (inputEventEntryType == EventEntry::Type::MOTION) {
483 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
484 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
485 motionEntry.action == AMOTION_EVENT_ACTION_HOVER_EXIT) {
486 return false;
487 }
488 } else {
489 // Not a key or a motion
490 return false;
491 }
492 if (!shouldReportMetricsForConnection(connection)) {
493 return false;
494 }
495 return true;
496}
497
Prabir Pradhancef936d2021-07-21 16:17:52 +0000498/**
499 * Connection is responsive if it has no events in the waitQueue that are older than the
500 * current time.
501 */
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000502bool isConnectionResponsive(const Connection& connection) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000503 const nsecs_t currentTime = now();
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000504 for (const auto& dispatchEntry : connection.waitQueue) {
505 if (dispatchEntry->timeoutTime < currentTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000506 return false;
507 }
508 }
509 return true;
510}
511
Antonio Kantekf16f2832021-09-28 04:39:20 +0000512// Returns true if the event type passed as argument represents a user activity.
513bool isUserActivityEvent(const EventEntry& eventEntry) {
514 switch (eventEntry.type) {
Josep del Riob3981622023-04-18 15:49:45 +0000515 case EventEntry::Type::CONFIGURATION_CHANGED:
516 case EventEntry::Type::DEVICE_RESET:
517 case EventEntry::Type::DRAG:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000518 case EventEntry::Type::FOCUS:
519 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000520 case EventEntry::Type::SENSOR:
Josep del Riob3981622023-04-18 15:49:45 +0000521 case EventEntry::Type::TOUCH_MODE_CHANGED:
Antonio Kantekf16f2832021-09-28 04:39:20 +0000522 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +0000523 case EventEntry::Type::KEY:
524 case EventEntry::Type::MOTION:
525 return true;
526 }
527}
528
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800529// Returns true if the given window can accept pointer events at the given display location.
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000530bool windowAcceptsTouchAt(const WindowInfo& windowInfo, int32_t displayId, float x, float y,
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000531 bool isStylus, const ui::Transform& displayTransform) {
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800532 const auto inputConfig = windowInfo.inputConfig;
533 if (windowInfo.displayId != displayId ||
534 inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800535 return false;
536 }
Prabir Pradhand65552b2021-10-07 11:23:50 -0700537 const bool windowCanInterceptTouch = isStylus && windowInfo.interceptsStylus();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800538 if (inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) && !windowCanInterceptTouch) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800539 return false;
540 }
Prabir Pradhan33e3baa2022-12-06 20:30:22 +0000541
542 // Window Manager works in the logical display coordinate space. When it specifies bounds for a
543 // window as (l, t, r, b), the range of x in [l, r) and y in [t, b) are considered to be inside
544 // the window. Points on the right and bottom edges should not be inside the window, so we need
545 // to be careful about performing a hit test when the display is rotated, since the "right" and
546 // "bottom" of the window will be different in the display (un-rotated) space compared to in the
547 // logical display in which WM determined the bounds. Perform the hit test in the logical
548 // display space to ensure these edges are considered correctly in all orientations.
549 const auto touchableRegion = displayTransform.transform(windowInfo.touchableRegion);
550 const auto p = displayTransform.transform(x, y);
551 if (!touchableRegion.contains(std::floor(p.x), std::floor(p.y))) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -0800552 return false;
553 }
554 return true;
555}
556
Prabir Pradhand65552b2021-10-07 11:23:50 -0700557bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) {
558 return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) &&
Prabir Pradhane5626962022-10-27 20:30:53 +0000559 isStylusToolType(entry.pointerProperties[pointerIndex].toolType);
Prabir Pradhand65552b2021-10-07 11:23:50 -0700560}
561
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800562// Determines if the given window can be targeted as InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000563// Foreground events are only sent to "foreground targetable" windows, but not all gestures sent to
564// such window are necessarily targeted with the flag. For example, an event with ACTION_OUTSIDE can
565// be sent to such a window, but it is not a foreground event and doesn't use
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800566// InputTarget::Flags::FOREGROUND.
Prabir Pradhan6dfbf262022-03-14 15:24:30 +0000567bool canReceiveForegroundTouches(const WindowInfo& info) {
568 // A non-touchable window can still receive touch events (e.g. in the case of
569 // STYLUS_INTERCEPTOR), so prevent such windows from receiving foreground events for touches.
570 return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy();
571}
572
Prabir Pradhanaeebeb42023-06-13 19:53:03 +0000573bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -0700574 if (windowHandle == nullptr) {
575 return false;
576 }
577 const WindowInfo* windowInfo = windowHandle->getInfo();
578 if (pid == windowInfo->ownerPid && uid == windowInfo->ownerUid) {
579 return true;
580 }
581 return false;
582}
583
Prabir Pradhan5735a322022-04-11 17:23:34 +0000584// Checks targeted injection using the window's owner's uid.
585// Returns an empty string if an entry can be sent to the given window, or an error message if the
586// entry is a targeted injection whose uid target doesn't match the window owner.
587std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& window,
588 const EventEntry& entry) {
589 if (entry.injectionState == nullptr || !entry.injectionState->targetUid) {
590 // The event was not injected, or the injected event does not target a window.
591 return {};
592 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000593 const auto uid = *entry.injectionState->targetUid;
Prabir Pradhan5735a322022-04-11 17:23:34 +0000594 if (window == nullptr) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000595 return StringPrintf("No valid window target for injection into uid %s.",
596 uid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000597 }
598 if (entry.injectionState->targetUid != window->getInfo()->ownerUid) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +0000599 return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' "
600 "owned by uid %s.",
601 uid.toString().c_str(), window->getName().c_str(),
602 window->getInfo()->ownerUid.toString().c_str());
Prabir Pradhan5735a322022-04-11 17:23:34 +0000603 }
604 return {};
605}
606
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000607std::pair<float, float> resolveTouchedPosition(const MotionEntry& entry) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700608 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
609 // Always dispatch mouse events to cursor position.
610 if (isFromMouse) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000611 return {entry.xCursorPosition, entry.yCursorPosition};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700612 }
613
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -0700614 const int32_t pointerIndex = MotionEvent::getActionIndex(entry.action);
Prabir Pradhan82e081e2022-12-06 09:50:09 +0000615 return {entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_X),
616 entry.pointerCoords[pointerIndex].getAxisValue(AMOTION_EVENT_AXIS_Y)};
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -0700617}
618
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -0700619std::optional<nsecs_t> getDownTime(const EventEntry& eventEntry) {
620 if (eventEntry.type == EventEntry::Type::KEY) {
621 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
622 return keyEntry.downTime;
623 } else if (eventEntry.type == EventEntry::Type::MOTION) {
624 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
625 return motionEntry.downTime;
626 }
627 return std::nullopt;
628}
629
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000630/**
631 * Compare the old touch state to the new touch state, and generate the corresponding touched
632 * windows (== input targets).
633 * If a window had the hovering pointer, but now it doesn't, produce HOVER_EXIT for that window.
634 * If the pointer just entered the new window, produce HOVER_ENTER.
635 * For pointers remaining in the window, produce HOVER_MOVE.
636 */
637std::vector<TouchedWindow> getHoveringWindowsLocked(const TouchState* oldState,
638 const TouchState& newTouchState,
639 const MotionEntry& entry) {
640 std::vector<TouchedWindow> out;
641 const int32_t maskedAction = MotionEvent::getActionMasked(entry.action);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700642
643 if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
644 // ACTION_SCROLL events should not affect the hovering pointer dispatch
645 return {};
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000646 }
647
648 // We should consider all hovering pointers here. But for now, just use the first one
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800649 const PointerProperties& pointer = entry.pointerProperties[0];
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000650
651 std::set<sp<WindowInfoHandle>> oldWindows;
652 if (oldState != nullptr) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800653 oldWindows = oldState->getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000654 }
655
656 std::set<sp<WindowInfoHandle>> newWindows =
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800657 newTouchState.getWindowsWithHoveringPointer(entry.deviceId, pointer.id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000658
659 // If the pointer is no longer in the new window set, send HOVER_EXIT.
660 for (const sp<WindowInfoHandle>& oldWindow : oldWindows) {
661 if (newWindows.find(oldWindow) == newWindows.end()) {
662 TouchedWindow touchedWindow;
663 touchedWindow.windowHandle = oldWindow;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000664 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_EXIT;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000665 out.push_back(touchedWindow);
666 }
667 }
668
669 for (const sp<WindowInfoHandle>& newWindow : newWindows) {
670 TouchedWindow touchedWindow;
671 touchedWindow.windowHandle = newWindow;
672 if (oldWindows.find(newWindow) == oldWindows.end()) {
673 // Any windows that have this pointer now, and didn't have it before, should get
674 // HOVER_ENTER
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000675 touchedWindow.dispatchMode = InputTarget::DispatchMode::HOVER_ENTER;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000676 } else {
677 // This pointer was already sent to the window. Use ACTION_HOVER_MOVE.
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700678 if (CC_UNLIKELY(maskedAction != AMOTION_EVENT_ACTION_HOVER_MOVE)) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700679 android::base::LogSeverity severity = android::base::LogSeverity::FATAL;
Ameer Armalycff4fa52023-10-04 23:45:11 +0000680 if (!input_flags::a11y_crash_on_inconsistent_event_stream() &&
681 entry.flags & AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) {
Daniel Norman7487dfa2023-08-02 16:39:45 -0700682 // The Accessibility injected touch exploration event stream
683 // has known inconsistencies, so log ERROR instead of
684 // crashing the device with FATAL.
Daniel Norman7487dfa2023-08-02 16:39:45 -0700685 severity = android::base::LogSeverity::ERROR;
686 }
687 LOG(severity) << "Expected ACTION_HOVER_MOVE instead of " << entry.getDescription();
Siarhei Vishniakouc2eb8502023-04-11 18:33:36 -0700688 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000689 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000690 }
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800691 touchedWindow.addHoveringPointer(entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000692 if (canReceiveForegroundTouches(*newWindow->getInfo())) {
693 touchedWindow.targetFlags |= InputTarget::Flags::FOREGROUND;
694 }
695 out.push_back(touchedWindow);
696 }
697 return out;
698}
699
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800700template <typename T>
701std::vector<T>& operator+=(std::vector<T>& left, const std::vector<T>& right) {
702 left.insert(left.end(), right.begin(), right.end());
703 return left;
704}
705
Harry Cuttsb166c002023-05-09 13:06:05 +0000706// Filter windows in a TouchState and targets in a vector to remove untrusted windows/targets from
707// both.
708void filterUntrustedTargets(TouchState& touchState, std::vector<InputTarget>& targets) {
709 std::erase_if(touchState.windows, [&](const TouchedWindow& window) {
710 if (!window.windowHandle->getInfo()->inputConfig.test(
711 WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
712 // In addition to TouchState, erase this window from the input targets! We don't have a
713 // good way to do this today except by adding a nested loop.
714 // TODO(b/282025641): simplify this code once InputTargets are being identified
715 // separately from TouchedWindows.
716 std::erase_if(targets, [&](const InputTarget& target) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800717 return target.connection->getToken() == window.windowHandle->getToken();
Harry Cuttsb166c002023-05-09 13:06:05 +0000718 });
719 return true;
720 }
721 return false;
722 });
723}
724
Siarhei Vishniakouce1fd472023-09-18 18:38:07 -0700725/**
726 * In general, touch should be always split between windows. Some exceptions:
727 * 1. Don't split touch if all of the below is true:
728 * (a) we have an active pointer down *and*
729 * (b) a new pointer is going down that's from the same device *and*
730 * (c) the window that's receiving the current pointer does not support split touch.
731 * 2. Don't split mouse events
732 */
733bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) {
734 if (isFromSource(entry.source, AINPUT_SOURCE_MOUSE)) {
735 // We should never split mouse events
736 return false;
737 }
738 for (const TouchedWindow& touchedWindow : touchState.windows) {
739 if (touchedWindow.windowHandle->getInfo()->isSpy()) {
740 // Spy windows should not affect whether or not touch is split.
741 continue;
742 }
743 if (touchedWindow.windowHandle->getInfo()->supportsSplitTouch()) {
744 continue;
745 }
746 if (touchedWindow.windowHandle->getInfo()->inputConfig.test(
747 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
748 // Wallpaper window should not affect whether or not touch is split
749 continue;
750 }
751
752 if (touchedWindow.hasTouchingPointers(entry.deviceId)) {
753 return false;
754 }
755 }
756 return true;
757}
758
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700759/**
760 * Return true if stylus is currently down anywhere on the specified display, and false otherwise.
761 */
762bool isStylusActiveInDisplay(
763 int32_t displayId,
764 const std::unordered_map<int32_t /*displayId*/, TouchState>& touchStatesByDisplay) {
765 const auto it = touchStatesByDisplay.find(displayId);
766 if (it == touchStatesByDisplay.end()) {
767 return false;
768 }
769 const TouchState& state = it->second;
770 return state.hasActiveStylus();
771}
772
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -0800773Result<void> validateWindowInfosUpdate(const gui::WindowInfosUpdate& update) {
774 struct HashFunction {
775 size_t operator()(const WindowInfo& info) const { return info.id; }
776 };
777
778 std::unordered_set<WindowInfo, HashFunction> windowSet;
779 for (const WindowInfo& info : update.windowInfos) {
780 const auto [_, inserted] = windowSet.insert(info);
781 if (!inserted) {
782 return Error() << "Duplicate entry for " << info;
783 }
784 }
785 return {};
786}
787
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800788int32_t getUserActivityEventType(const EventEntry& eventEntry) {
789 switch (eventEntry.type) {
790 case EventEntry::Type::KEY: {
791 return USER_ACTIVITY_EVENT_BUTTON;
792 }
793 case EventEntry::Type::MOTION: {
794 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
795 if (MotionEvent::isTouchEvent(motionEntry.source, motionEntry.action)) {
796 return USER_ACTIVITY_EVENT_TOUCH;
797 }
798 return USER_ACTIVITY_EVENT_OTHER;
799 }
800 default: {
801 LOG_ALWAYS_FATAL("%s events are not user activity",
802 ftl::enum_string(eventEntry.type).c_str());
803 }
804 }
805}
806
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +0000807std::pair<bool /*cancelPointers*/, bool /*cancelNonPointers*/> expandCancellationMode(
808 CancelationOptions::Mode mode) {
809 switch (mode) {
810 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
811 return {true, true};
812 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
813 return {true, false};
814 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
815 return {false, true};
816 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
817 return {false, true};
818 }
819}
820
Prabir Pradhan61a5d242021-07-26 16:41:09 +0000821} // namespace
822
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823// --- InputDispatcher ---
824
Prabir Pradhana41d2442023-04-20 21:30:40 +0000825InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
Prabir Pradhandae52792023-12-15 07:36:40 +0000826 : InputDispatcher(policy,
827 isInputTracingEnabled() ? std::make_unique<trace::impl::PerfettoBackend>()
828 : nullptr) {}
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000829
830InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
831 std::unique_ptr<trace::InputTracingBackendInterface> traceBackend)
Garfield Tan00f511d2019-06-12 16:55:40 -0700832 : mPolicy(policy),
833 mPendingEvent(nullptr),
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700834 mLastDropReason(DropReason::NOT_DROPPED),
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800835 mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800836 mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),
Garfield Tan00f511d2019-06-12 16:55:40 -0700837 mNextUnblockedEvent(nullptr),
Prabir Pradhan1376fcd2022-01-21 09:56:35 -0800838 mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),
Garfield Tan00f511d2019-06-12 16:55:40 -0700839 mDispatchEnabled(false),
840 mDispatchFrozen(false),
841 mInputFilterEnabled(false),
Bernardo Rufinoea97d182020-08-19 14:43:14 +0100842 mMaximumObscuringOpacityForTouch(1.0f),
Siarhei Vishniakou2508b872020-12-03 16:33:53 -1000843 mFocusedDisplayId(ADISPLAY_ID_DEFAULT),
Prabir Pradhan99987712020-11-10 18:43:05 -0800844 mWindowTokenWithPointerCapture(nullptr),
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +0000845 mLatencyAggregator(),
Antonio Kantek15beb512022-06-13 22:35:41 +0000846 mLatencyTracker(&mLatencyAggregator) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700847 mLooper = sp<Looper>::make(false);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800848 mReporter = createInputReporter();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700850 mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700851#if defined(__ANDROID__)
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700852 SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700853#endif
Yi Kong9b14ac62018-07-17 13:48:38 -0700854 mKeyRepeatState.lastKeyEntry = nullptr;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000855
856 if (traceBackend) {
Prabir Pradhandae52792023-12-15 07:36:40 +0000857 mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000858 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -0800859
860 mLastUserActivityTimes.fill(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861}
862
863InputDispatcher::~InputDispatcher() {
Prabir Pradhancef936d2021-07-21 16:17:52 +0000864 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Prabir Pradhancef936d2021-07-21 16:17:52 +0000866 resetKeyRepeatLocked();
867 releasePendingEventLocked();
868 drainInboundQueueLocked();
869 mCommandQueue.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +0000871 while (!mConnectionsByToken.empty()) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700872 std::shared_ptr<Connection> connection = mConnectionsByToken.begin()->second;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800873 removeInputChannelLocked(connection->getToken(), /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 }
875}
876
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700877status_t InputDispatcher::start() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700878 if (mThread) {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700879 return ALREADY_EXISTS;
880 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700881 mThread = std::make_unique<InputThread>(
882 "InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
883 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700884}
885
886status_t InputDispatcher::stop() {
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700887 if (mThread && mThread->isCallingThread()) {
888 ALOGE("InputDispatcher cannot be stopped from its own thread!");
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700889 return INVALID_OPERATION;
890 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700891 mThread.reset();
892 return OK;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700893}
894
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895void InputDispatcher::dispatchOnce() {
Colin Cross5b799302022-10-18 21:52:41 -0700896 nsecs_t nextWakeupTime = LLONG_MAX;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800898 std::scoped_lock _l(mLock);
899 mDispatcherIsAlive.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900
901 // Run a dispatch loop if there are no pending commands.
902 // The dispatch loop might enqueue commands to run afterwards.
903 if (!haveCommandsLocked()) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -0800904 dispatchOnceInnerLocked(/*byref*/ nextWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906
907 // Run all pending commands if there are any.
908 // If any commands were run then force the next poll to wake up immediately.
Prabir Pradhancef936d2021-07-21 16:17:52 +0000909 if (runCommandsLockedInterruptable()) {
Colin Cross5b799302022-10-18 21:52:41 -0700910 nextWakeupTime = LLONG_MIN;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911 }
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800912
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700913 // If we are still waiting for ack on some events,
914 // we might have to wake up earlier to check if an app is anr'ing.
915 const nsecs_t nextAnrCheck = processAnrsLocked();
916 nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);
917
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800918 // We are about to enter an infinitely long sleep, because we have no commands or
919 // pending or queued events
Colin Cross5b799302022-10-18 21:52:41 -0700920 if (nextWakeupTime == LLONG_MAX) {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800921 mDispatcherEnteredIdle.notify_all();
922 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 } // release lock
924
925 // Wait for callback or timeout or wake. (make sure we round up, not down)
926 nsecs_t currentTime = now();
927 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
928 mLooper->pollOnce(timeoutMillis);
929}
930
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700931/**
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -0500932 * Raise ANR if there is no focused window.
933 * Before the ANR is raised, do a final state check:
934 * 1. The currently focused application must be the same one we are waiting for.
935 * 2. Ensure we still don't have a focused window.
936 */
937void InputDispatcher::processNoFocusedWindowAnrLocked() {
938 // Check if the application that we are waiting for is still focused.
939 std::shared_ptr<InputApplicationHandle> focusedApplication =
940 getValueByKey(mFocusedApplicationHandlesByDisplay, mAwaitedApplicationDisplayId);
941 if (focusedApplication == nullptr ||
942 focusedApplication->getApplicationToken() !=
943 mAwaitedFocusedApplication->getApplicationToken()) {
944 // Unexpected because we should have reset the ANR timer when focused application changed
945 ALOGE("Waited for a focused window, but focused application has already changed to %s",
946 focusedApplication->getName().c_str());
947 return; // The focused application has changed.
948 }
949
chaviw98318de2021-05-19 16:45:23 -0500950 const sp<WindowInfoHandle>& focusedWindowHandle =
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -0500951 getFocusedWindowHandleLocked(mAwaitedApplicationDisplayId);
952 if (focusedWindowHandle != nullptr) {
953 return; // We now have a focused window. No need for ANR.
954 }
955 onAnrLocked(mAwaitedFocusedApplication);
956}
957
958/**
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700959 * Check if any of the connections' wait queues have events that are too old.
960 * If we waited for events to be ack'ed for more than the window timeout, raise an ANR.
961 * Return the time at which we should wake up next.
962 */
963nsecs_t InputDispatcher::processAnrsLocked() {
964 const nsecs_t currentTime = now();
Colin Cross5b799302022-10-18 21:52:41 -0700965 nsecs_t nextAnrCheck = LLONG_MAX;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700966 // Check if we are waiting for a focused window to appear. Raise ANR if waited too long
967 if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) {
968 if (currentTime >= *mNoFocusedWindowTimeoutTime) {
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -0500969 processNoFocusedWindowAnrLocked();
Chris Yea209fde2020-07-22 13:54:51 -0700970 mAwaitedFocusedApplication.reset();
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -0500971 mNoFocusedWindowTimeoutTime = std::nullopt;
Colin Cross5b799302022-10-18 21:52:41 -0700972 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700973 } else {
Siarhei Vishniakou38a6d272020-10-20 20:29:33 -0500974 // Keep waiting. We will drop the event when mNoFocusedWindowTimeoutTime comes.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700975 nextAnrCheck = *mNoFocusedWindowTimeoutTime;
976 }
977 }
978
979 // Check if any connection ANRs are due
980 nextAnrCheck = std::min(nextAnrCheck, mAnrTracker.firstTimeout());
981 if (currentTime < nextAnrCheck) { // most likely scenario
982 return nextAnrCheck; // everything is normal. Let's check again at nextAnrCheck
983 }
984
985 // If we reached here, we have an unresponsive connection.
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700986 std::shared_ptr<Connection> connection = getConnectionLocked(mAnrTracker.firstToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700987 if (connection == nullptr) {
988 ALOGE("Could not find connection for entry %" PRId64, mAnrTracker.firstTimeout());
989 return nextAnrCheck;
990 }
991 connection->responsive = false;
992 // Stop waking up for this unresponsive connection
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800993 mAnrTracker.eraseToken(connection->getToken());
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000994 onAnrLocked(connection);
Colin Cross5b799302022-10-18 21:52:41 -0700995 return LLONG_MIN;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700996}
997
Prabir Pradhan1376fcd2022-01-21 09:56:35 -0800998std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -0700999 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08001000 if (connection->monitor) {
1001 return mMonitorDispatchingTimeout;
1002 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001003 const sp<WindowInfoHandle> window = getWindowHandleLocked(connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001004 if (window != nullptr) {
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001005 return window->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001006 }
Siarhei Vishniakou70622952020-07-30 11:17:23 -05001007 return DEFAULT_INPUT_DISPATCHING_TIMEOUT;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001008}
1009
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001010void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 nsecs_t currentTime = now();
1012
Jeff Browndc5992e2014-04-11 01:27:26 -07001013 // Reset the key repeat timer whenever normal dispatch is suspended while the
1014 // device is in a non-interactive state. This is to ensure that we abort a key
1015 // repeat if the device is just coming out of sleep.
1016 if (!mDispatchEnabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017 resetKeyRepeatLocked();
1018 }
1019
1020 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
1021 if (mDispatchFrozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01001022 if (DEBUG_FOCUS) {
1023 ALOGD("Dispatch frozen. Waiting some more.");
1024 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 return;
1026 }
1027
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028 // Ready to start a new event.
1029 // If we don't already have a pending event, go grab one.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001030 if (!mPendingEvent) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001031 if (mInboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 // Synthesize a key repeat if appropriate.
1033 if (mKeyRepeatState.lastKeyEntry) {
1034 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
1035 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
1036 } else {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001037 nextWakeupTime = std::min(nextWakeupTime, mKeyRepeatState.nextRepeatTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038 }
1039 }
1040
1041 // Nothing to do if there is no pending event.
1042 if (!mPendingEvent) {
1043 return;
1044 }
1045 } else {
1046 // Inbound queue has at least one entry.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001047 mPendingEvent = mInboundQueue.front();
1048 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049 traceInboundQueueLengthLocked();
1050 }
1051
1052 // Poke user activity for this event.
1053 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001054 pokeUserActivityLocked(*mPendingEvent);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 }
1057
1058 // Now we have an event to dispatch.
1059 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Yi Kong9b14ac62018-07-17 13:48:38 -07001060 ALOG_ASSERT(mPendingEvent != nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 bool done = false;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001062 DropReason dropReason = DropReason::NOT_DROPPED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001064 dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065 } else if (!mDispatchEnabled) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001066 dropReason = DropReason::DISABLED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 }
1068
1069 if (mNextUnblockedEvent == mPendingEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001070 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 }
1072
1073 switch (mPendingEvent->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001074 case EventEntry::Type::CONFIGURATION_CHANGED: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001075 const ConfigurationChangedEntry& typedEntry =
1076 static_cast<const ConfigurationChangedEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001077 done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001078 dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001079 break;
1080 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001082 case EventEntry::Type::DEVICE_RESET: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001083 const DeviceResetEntry& typedEntry =
1084 static_cast<const DeviceResetEntry&>(*mPendingEvent);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001085 done = dispatchDeviceResetLocked(currentTime, typedEntry);
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001086 dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001087 break;
1088 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001090 case EventEntry::Type::FOCUS: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001091 std::shared_ptr<const FocusEntry> typedEntry =
1092 std::static_pointer_cast<const FocusEntry>(mPendingEvent);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001093 dispatchFocusLocked(currentTime, typedEntry);
1094 done = true;
1095 dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
1096 break;
1097 }
1098
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001099 case EventEntry::Type::TOUCH_MODE_CHANGED: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001100 const auto typedEntry = std::static_pointer_cast<const TouchModeEntry>(mPendingEvent);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001101 dispatchTouchModeChangeLocked(currentTime, typedEntry);
1102 done = true;
1103 dropReason = DropReason::NOT_DROPPED; // touch mode events are never dropped
1104 break;
1105 }
1106
Prabir Pradhan99987712020-11-10 18:43:05 -08001107 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
1108 const auto typedEntry =
Prabir Pradhan24047542023-11-02 17:14:59 +00001109 std::static_pointer_cast<const PointerCaptureChangedEntry>(mPendingEvent);
Prabir Pradhan99987712020-11-10 18:43:05 -08001110 dispatchPointerCaptureChangedLocked(currentTime, typedEntry, dropReason);
1111 done = true;
1112 break;
1113 }
1114
arthurhungb89ccb02020-12-30 16:19:01 +08001115 case EventEntry::Type::DRAG: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001116 std::shared_ptr<const DragEntry> typedEntry =
1117 std::static_pointer_cast<const DragEntry>(mPendingEvent);
arthurhungb89ccb02020-12-30 16:19:01 +08001118 dispatchDragLocked(currentTime, typedEntry);
1119 done = true;
1120 break;
1121 }
1122
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001123 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001124 std::shared_ptr<const KeyEntry> keyEntry =
1125 std::static_pointer_cast<const KeyEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001126 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *keyEntry)) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001127 dropReason = DropReason::STALE;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001128 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001129 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
1130 dropReason = DropReason::BLOCKED;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001131 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001132 done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001133 if (done && mTracer) {
1134 ensureEventTraced(*keyEntry);
1135 mTracer->eventProcessingComplete(*keyEntry->traceTracker);
1136 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001137 break;
1138 }
1139
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001140 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001141 std::shared_ptr<const MotionEntry> motionEntry =
1142 std::static_pointer_cast<const MotionEntry>(mPendingEvent);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001143 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
Siarhei Vishniakou6b71b632023-10-27 21:34:46 -07001144 // The event is stale. However, only drop stale events if there isn't an ongoing
1145 // gesture. That would allow us to complete the processing of the current stroke.
1146 const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId);
1147 if (touchStateIt != mTouchStatesByDisplay.end()) {
1148 const TouchState& touchState = touchStateIt->second;
1149 if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
1150 !touchState.hasHoveringPointers(motionEntry->deviceId)) {
1151 dropReason = DropReason::STALE;
1152 }
1153 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001154 }
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001155 if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001156 if (!isFromSource(motionEntry->source, AINPUT_SOURCE_CLASS_POINTER)) {
1157 // Only drop events that are focus-dispatched.
1158 dropReason = DropReason::BLOCKED;
1159 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001160 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001161 done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001162 if (done && mTracer) {
1163 ensureEventTraced(*motionEntry);
1164 mTracer->eventProcessingComplete(*motionEntry->traceTracker);
1165 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001166 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 }
Chris Yef59a2f42020-10-16 12:55:26 -07001168
1169 case EventEntry::Type::SENSOR: {
Prabir Pradhan24047542023-11-02 17:14:59 +00001170 std::shared_ptr<const SensorEntry> sensorEntry =
1171 std::static_pointer_cast<const SensorEntry>(mPendingEvent);
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001172
Chris Yef59a2f42020-10-16 12:55:26 -07001173 // Sensor timestamps use SYSTEM_TIME_BOOTTIME time base, so we can't use
1174 // 'currentTime' here, get SYSTEM_TIME_BOOTTIME instead.
1175 nsecs_t bootTime = systemTime(SYSTEM_TIME_BOOTTIME);
1176 if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(bootTime, *sensorEntry)) {
1177 dropReason = DropReason::STALE;
1178 }
1179 dispatchSensorLocked(currentTime, sensorEntry, &dropReason, nextWakeupTime);
1180 done = true;
1181 break;
1182 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 }
1184
1185 if (done) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001186 if (dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001187 dropInboundEventLocked(*mPendingEvent, dropReason);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 }
Michael Wright3a981722015-06-10 15:26:13 +01001189 mLastDropReason = dropReason;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190
1191 releasePendingEventLocked();
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001192 nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 }
1194}
1195
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001196bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) {
Siarhei Vishniakoua7333112023-10-27 13:33:29 -07001197 return mPolicy.isStaleEvent(currentTime, entry.eventTime);
Siarhei Vishniakou289e9242022-02-15 14:50:16 -08001198}
1199
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001200/**
1201 * Return true if the events preceding this incoming motion event should be dropped
1202 * Return false otherwise (the default behaviour)
1203 */
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001204bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001205 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
Prabir Pradhanaa561d12021-09-24 06:57:33 -07001206 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001207
1208 // Optimize case where the current application is unresponsive and the user
1209 // decides to touch a window in a different application.
1210 // If the application takes too long to catch up then we drop all events preceding
1211 // the touch into the other window.
1212 if (isPointerDownEvent && mAwaitedFocusedApplication != nullptr) {
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001213 const int32_t displayId = motionEntry.displayId;
1214 const auto [x, y] = resolveTouchedPosition(motionEntry);
Harry Cutts33476232023-01-30 19:57:29 +00001215 const bool isStylus = isPointerFromStylus(motionEntry, /*pointerIndex=*/0);
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07001216
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001217 sp<WindowInfoHandle> touchedWindowHandle =
1218 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001219 if (touchedWindowHandle != nullptr &&
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001220 touchedWindowHandle->getApplicationToken() !=
1221 mAwaitedFocusedApplication->getApplicationToken()) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001222 // User touched a different application than the one we are waiting on.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001223 ALOGI("Pruning input queue because user touched a different application while waiting "
1224 "for %s",
1225 mAwaitedFocusedApplication->getName().c_str());
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001226 return true;
1227 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001228
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001229 // Alternatively, maybe there's a spy window that could handle this event.
1230 const std::vector<sp<WindowInfoHandle>> touchedSpies =
1231 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus);
1232 for (const auto& windowHandle : touchedSpies) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07001233 const std::shared_ptr<Connection> connection =
1234 getConnectionLocked(windowHandle->getToken());
Siarhei Vishniakou34ed4d42020-06-18 00:43:02 +00001235 if (connection != nullptr && connection->responsive) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001236 // This spy window could take more input. Drop all events preceding this
1237 // event, so that the spy window can get a chance to receive the stream.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001238 ALOGW("Pruning the input queue because %s is unresponsive, but we have a "
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08001239 "responsive spy window that may handle the event.",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001240 mAwaitedFocusedApplication->getName().c_str());
1241 return true;
1242 }
1243 }
1244 }
1245
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001246 return false;
1247}
1248
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001249bool InputDispatcher::enqueueInboundEventLocked(std::unique_ptr<EventEntry> newEntry) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001250 bool needWake = mInboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001251 mInboundQueue.push_back(std::move(newEntry));
Prabir Pradhan24047542023-11-02 17:14:59 +00001252 const EventEntry& entry = *(mInboundQueue.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 traceInboundQueueLengthLocked();
1254
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001255 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001256 case EventEntry::Type::KEY: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001257 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1258 "Unexpected untrusted event.");
Siarhei Vishniakoue2404a12024-01-16 18:38:39 -08001259
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001260 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001261 if (mTracer) {
1262 ensureEventTraced(keyEntry);
1263 }
Siarhei Vishniakou6520a582023-10-27 21:53:45 -07001264
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001265 // If a new up event comes in, and the pending event with same key code has been asked
1266 // to try again later because of the policy. We have to reset the intercept key wake up
1267 // time for it may have been handled in the policy and could be dropped.
1268 if (keyEntry.action == AKEY_EVENT_ACTION_UP && mPendingEvent &&
1269 mPendingEvent->type == EventEntry::Type::KEY) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001270 const KeyEntry& pendingKey = static_cast<const KeyEntry&>(*mPendingEvent);
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001271 if (pendingKey.keyCode == keyEntry.keyCode &&
1272 pendingKey.interceptKeyResult ==
Michael Wright5caf55a2022-11-24 22:31:42 +00001273 KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
1274 pendingKey.interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Arthur Hung2ee6d0b2022-03-03 20:19:38 +08001275 pendingKey.interceptKeyWakeupTime = 0;
1276 needWake = true;
1277 }
1278 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001279 break;
1280 }
1281
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001282 case EventEntry::Type::MOTION: {
Prabir Pradhan5735a322022-04-11 17:23:34 +00001283 LOG_ALWAYS_FATAL_IF((entry.policyFlags & POLICY_FLAG_TRUSTED) == 0,
1284 "Unexpected untrusted event.");
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001285 const auto& motionEntry = static_cast<const MotionEntry&>(entry);
1286 if (mTracer) {
1287 ensureEventTraced(motionEntry);
1288 }
1289 if (shouldPruneInboundQueueLocked(motionEntry)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001290 mNextUnblockedEvent = mInboundQueue.back();
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001291 needWake = true;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292 }
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08001293
1294 const bool isPointerDownEvent = motionEntry.action == AMOTION_EVENT_ACTION_DOWN &&
1295 isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER);
1296 if (isPointerDownEvent && mKeyIsWaitingForEventsTimeout) {
1297 // Prevent waiting too long for unprocessed events: if we have a pending key event,
1298 // and some other events have not yet been processed, the dispatcher will wait for
1299 // these events to be processed before dispatching the key event. This is because
1300 // the unprocessed events may cause the focus to change (for example, by launching a
1301 // new window or tapping a different window). To prevent waiting too long, we force
1302 // the key to be sent to the currently focused window when a new tap comes in.
1303 ALOGD("Received a new pointer down event, stop waiting for events to process and "
1304 "just send the pending key event to the currently focused window.");
1305 mKeyIsWaitingForEventsTimeout = now();
1306 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001307 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001308 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001309 case EventEntry::Type::FOCUS: {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001310 LOG_ALWAYS_FATAL("Focus events should be inserted using enqueueFocusEventLocked");
1311 break;
1312 }
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001313 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001314 case EventEntry::Type::CONFIGURATION_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08001315 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07001316 case EventEntry::Type::SENSOR:
arthurhungb89ccb02020-12-30 16:19:01 +08001317 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1318 case EventEntry::Type::DRAG: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001319 // nothing to do
1320 break;
1321 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 }
1323
1324 return needWake;
1325}
1326
Prabir Pradhan24047542023-11-02 17:14:59 +00001327void InputDispatcher::addRecentEventLocked(std::shared_ptr<const EventEntry> entry) {
Chris Yef59a2f42020-10-16 12:55:26 -07001328 // Do not store sensor event in recent queue to avoid flooding the queue.
1329 if (entry->type != EventEntry::Type::SENSOR) {
1330 mRecentQueue.push_back(entry);
1331 }
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001332 if (mRecentQueue.size() > RECENT_QUEUE_MAX_SIZE) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001333 mRecentQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001334 }
1335}
1336
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001337sp<WindowInfoHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, float x, float y,
1338 bool isStylus,
1339 bool ignoreDragWindow) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001340 // Traverse windows from front to back to find touched window.
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001341 const auto& windowHandles = getWindowHandlesLocked(displayId);
chaviw98318de2021-05-19 16:45:23 -05001342 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
arthurhung6d4bed92021-03-17 11:59:33 +08001343 if (ignoreDragWindow && haveSameToken(windowHandle, mDragState->dragWindow)) {
arthurhungb89ccb02020-12-30 16:19:01 +08001344 continue;
1345 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001346
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001347 const WindowInfo& info = *windowHandle->getInfo();
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001348 if (!info.isSpy() &&
1349 windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001350 return windowHandle;
1351 }
1352 }
1353 return nullptr;
1354}
1355
1356std::vector<InputTarget> InputDispatcher::findOutsideTargetsLocked(
Siarhei Vishniakou70f3d8c2023-09-19 15:36:52 -07001357 int32_t displayId, const sp<WindowInfoHandle>& touchedWindow, int32_t pointerId) const {
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001358 if (touchedWindow == nullptr) {
1359 return {};
1360 }
1361 // Traverse windows from front to back until we encounter the touched window.
1362 std::vector<InputTarget> outsideTargets;
1363 const auto& windowHandles = getWindowHandlesLocked(displayId);
1364 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1365 if (windowHandle == touchedWindow) {
1366 // Stop iterating once we found a touched window. Any WATCH_OUTSIDE_TOUCH window
1367 // below the touched window will not get ACTION_OUTSIDE event.
1368 return outsideTargets;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08001369 }
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001370
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001371 const WindowInfo& info = *windowHandle->getInfo();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08001372 if (info.inputConfig.test(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH)) {
Siarhei Vishniakou70f3d8c2023-09-19 15:36:52 -07001373 std::bitset<MAX_POINTER_ID + 1> pointerIds;
1374 pointerIds.set(pointerId);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001375 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::OUTSIDE,
1376 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001377 /*firstDownTimeInTarget=*/std::nullopt, outsideTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001378 }
1379 }
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07001380 return outsideTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381}
1382
Prabir Pradhand65552b2021-10-07 11:23:50 -07001383std::vector<sp<WindowInfoHandle>> InputDispatcher::findTouchedSpyWindowsAtLocked(
Prabir Pradhan82e081e2022-12-06 09:50:09 +00001384 int32_t displayId, float x, float y, bool isStylus) const {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001385 // Traverse windows from front to back and gather the touched spy windows.
1386 std::vector<sp<WindowInfoHandle>> spyWindows;
1387 const auto& windowHandles = getWindowHandlesLocked(displayId);
1388 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
1389 const WindowInfo& info = *windowHandle->getInfo();
1390
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00001391 if (!windowAcceptsTouchAt(info, displayId, x, y, isStylus, getTransformLocked(displayId))) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08001392 continue;
1393 }
1394 if (!info.isSpy()) {
1395 // The first touched non-spy window was found, so return the spy windows touched so far.
1396 return spyWindows;
1397 }
1398 spyWindows.push_back(windowHandle);
1399 }
1400 return spyWindows;
1401}
1402
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001403void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001404 const char* reason;
1405 switch (dropReason) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001406 case DropReason::POLICY:
Prabir Pradhan65613802023-02-22 23:36:58 +00001407 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001408 ALOGD("Dropped event because policy consumed it.");
1409 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001410 reason = "inbound event was dropped because the policy consumed it";
1411 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001412 case DropReason::DISABLED:
1413 if (mLastDropReason != DropReason::DISABLED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001414 ALOGI("Dropped event because input dispatch is disabled.");
1415 }
1416 reason = "inbound event was dropped because input dispatch is disabled";
1417 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001418 case DropReason::BLOCKED:
Siarhei Vishniakou99e407b2023-12-26 18:09:32 -08001419 LOG(INFO) << "Dropping because the current application is not responding and the user "
1420 "has started interacting with a different application: "
1421 << entry.getDescription();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001422 reason = "inbound event was dropped because the current application is not responding "
1423 "and the user has started interacting with a different application";
1424 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001425 case DropReason::STALE:
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001426 ALOGI("Dropped event because it is stale.");
1427 reason = "inbound event was dropped because it is stale";
1428 break;
Prabir Pradhan99987712020-11-10 18:43:05 -08001429 case DropReason::NO_POINTER_CAPTURE:
1430 ALOGI("Dropped event because there is no window with Pointer Capture.");
1431 reason = "inbound event was dropped because there is no window with Pointer Capture";
1432 break;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001433 case DropReason::NOT_DROPPED: {
1434 LOG_ALWAYS_FATAL("Should not be dropping a NOT_DROPPED event");
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001435 return;
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001436 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001437 }
1438
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001439 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001440 case EventEntry::Type::KEY: {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001441 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS, reason);
Hu Guo3cfa7382023-11-15 09:50:04 +00001442 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
1443 options.displayId = keyEntry.displayId;
1444 options.deviceId = keyEntry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001445 synthesizeCancelationEventsForAllConnectionsLocked(options);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001446 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001447 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001448 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001449 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
1450 if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001451 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS, reason);
Hu Guo3cfa7382023-11-15 09:50:04 +00001452 options.displayId = motionEntry.displayId;
1453 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001454 synthesizeCancelationEventsForAllConnectionsLocked(options);
1455 } else {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001456 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
1457 reason);
Hu Guo3cfa7382023-11-15 09:50:04 +00001458 options.displayId = motionEntry.displayId;
1459 options.deviceId = motionEntry.deviceId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001460 synthesizeCancelationEventsForAllConnectionsLocked(options);
1461 }
1462 break;
1463 }
Chris Yef59a2f42020-10-16 12:55:26 -07001464 case EventEntry::Type::SENSOR: {
1465 break;
1466 }
arthurhungb89ccb02020-12-30 16:19:01 +08001467 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
1468 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08001469 break;
1470 }
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001471 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001472 case EventEntry::Type::TOUCH_MODE_CHANGED:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001473 case EventEntry::Type::CONFIGURATION_CHANGED:
1474 case EventEntry::Type::DEVICE_RESET: {
Dominik Laskowski75788452021-02-09 18:51:25 -08001475 LOG_ALWAYS_FATAL("Should not drop %s events", ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07001476 break;
1477 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001478 }
1479}
1480
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481bool InputDispatcher::haveCommandsLocked() const {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001482 return !mCommandQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001483}
1484
Prabir Pradhancef936d2021-07-21 16:17:52 +00001485bool InputDispatcher::runCommandsLockedInterruptable() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001486 if (mCommandQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001487 return false;
1488 }
1489
1490 do {
Prabir Pradhancef936d2021-07-21 16:17:52 +00001491 auto command = std::move(mCommandQueue.front());
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001492 mCommandQueue.pop_front();
Prabir Pradhancef936d2021-07-21 16:17:52 +00001493 // Commands are run with the lock held, but may release and re-acquire the lock from within.
1494 command();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001495 } while (!mCommandQueue.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001496 return true;
1497}
1498
Prabir Pradhancef936d2021-07-21 16:17:52 +00001499void InputDispatcher::postCommandLocked(Command&& command) {
1500 mCommandQueue.push_back(command);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001501}
1502
1503void InputDispatcher::drainInboundQueueLocked() {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001504 while (!mInboundQueue.empty()) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001505 std::shared_ptr<const EventEntry> entry = mInboundQueue.front();
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07001506 mInboundQueue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507 releaseInboundEventLocked(entry);
1508 }
1509 traceInboundQueueLengthLocked();
1510}
1511
1512void InputDispatcher::releasePendingEventLocked() {
1513 if (mPendingEvent) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514 releaseInboundEventLocked(mPendingEvent);
Yi Kong9b14ac62018-07-17 13:48:38 -07001515 mPendingEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 }
1517}
1518
Prabir Pradhan24047542023-11-02 17:14:59 +00001519void InputDispatcher::releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001520 const std::shared_ptr<InjectionState>& injectionState = entry->injectionState;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001521 if (injectionState && injectionState->injectionResult == InputEventInjectionResult::PENDING) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001522 if (DEBUG_DISPATCH_CYCLE) {
1523 ALOGD("Injected inbound event was dropped.");
1524 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001525 setInjectionResult(*entry, InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001526 }
1527 if (entry == mNextUnblockedEvent) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001528 mNextUnblockedEvent = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 }
1530 addRecentEventLocked(entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001531}
1532
1533void InputDispatcher::resetKeyRepeatLocked() {
1534 if (mKeyRepeatState.lastKeyEntry) {
Yi Kong9b14ac62018-07-17 13:48:38 -07001535 mKeyRepeatState.lastKeyEntry = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 }
1537}
1538
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001539std::shared_ptr<KeyEntry> InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001540 std::shared_ptr<const KeyEntry> entry = mKeyRepeatState.lastKeyEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001541
Michael Wright2e732952014-09-24 13:26:59 -07001542 uint32_t policyFlags = entry->policyFlags &
1543 (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001545 std::shared_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00001546 std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
1547 currentTime, entry->deviceId, entry->source,
1548 entry->displayId, policyFlags, entry->action, entry->flags,
1549 entry->keyCode, entry->scanCode, entry->metaState,
1550 entry->repeatCount + 1, entry->downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001552 newEntry->syntheticRepeat = true;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001553 if (mTracer) {
1554 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
1555 }
1556
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001557 mKeyRepeatState.lastKeyEntry = newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001559 return newEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560}
1561
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001562bool InputDispatcher::dispatchConfigurationChangedLocked(nsecs_t currentTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001563 const ConfigurationChangedEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001564 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1565 ALOGD("dispatchConfigurationChanged - eventTime=%" PRId64, entry.eventTime);
1566 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
1568 // Reset key repeating in case a keyboard device was added or removed or something.
1569 resetKeyRepeatLocked();
1570
1571 // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
Prabir Pradhancef936d2021-07-21 16:17:52 +00001572 auto command = [this, eventTime = entry.eventTime]() REQUIRES(mLock) {
1573 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00001574 mPolicy.notifyConfigurationChanged(eventTime);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001575 };
1576 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577 return true;
1578}
1579
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001580bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
1581 const DeviceResetEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001582 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1583 ALOGD("dispatchDeviceReset - eventTime=%" PRId64 ", deviceId=%d", entry.eventTime,
1584 entry.deviceId);
1585 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
liushenxiang42232912021-05-21 20:24:09 +08001587 // Reset key repeating in case a keyboard device was disabled or enabled.
1588 if (mKeyRepeatState.lastKeyEntry && mKeyRepeatState.lastKeyEntry->deviceId == entry.deviceId) {
1589 resetKeyRepeatLocked();
1590 }
1591
Michael Wrightfb04fd52022-11-24 22:31:11 +00001592 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, "device was reset");
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001593 options.deviceId = entry.deviceId;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 synthesizeCancelationEventsForAllConnectionsLocked(options);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -07001595
1596 // Remove all active pointers from this device
1597 for (auto& [_, touchState] : mTouchStatesByDisplay) {
1598 touchState.removeAllPointersForDevice(entry.deviceId);
1599 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001600 return true;
1601}
1602
Vishnu Nairad321cd2020-08-20 16:40:21 -07001603void InputDispatcher::enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -08001604 const std::string& reason) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001605 if (mPendingEvent != nullptr) {
1606 // Move the pending event to the front of the queue. This will give the chance
1607 // for the pending event to get dispatched to the newly focused window
1608 mInboundQueue.push_front(mPendingEvent);
1609 mPendingEvent = nullptr;
1610 }
1611
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001612 std::unique_ptr<FocusEntry> focusEntry =
1613 std::make_unique<FocusEntry>(mIdGenerator.nextId(), now(), windowToken, hasFocus,
1614 reason);
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001615
1616 // This event should go to the front of the queue, but behind all other focus events
1617 // Find the last focus event, and insert right after it
Prabir Pradhan24047542023-11-02 17:14:59 +00001618 auto it = std::find_if(mInboundQueue.rbegin(), mInboundQueue.rend(),
1619 [](const std::shared_ptr<const EventEntry>& event) {
1620 return event->type == EventEntry::Type::FOCUS;
1621 });
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07001622
1623 // Maintain the order of focus events. Insert the entry after all other focus events.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001624 mInboundQueue.insert(it.base(), std::move(focusEntry));
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001625}
1626
Prabir Pradhan24047542023-11-02 17:14:59 +00001627void InputDispatcher::dispatchFocusLocked(nsecs_t currentTime,
1628 std::shared_ptr<const FocusEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001629 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
1630 if (connection == nullptr) {
1631 return; // Connection has gone away
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001632 }
1633 InputTarget target;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001634 target.connection = connection;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001635 entry->dispatchInProgress = true;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00001636 std::string message = std::string("Focus ") + (entry->hasFocus ? "entering " : "leaving ") +
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001637 connection->getInputChannelName();
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07001638 std::string reason = std::string("reason=").append(entry->reason);
1639 android_log_event_list(LOGTAG_INPUT_FOCUS) << message << reason << LOG_ID_EVENTS;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01001640 dispatchEventLocked(currentTime, entry, {target});
1641}
1642
Prabir Pradhan99987712020-11-10 18:43:05 -08001643void InputDispatcher::dispatchPointerCaptureChangedLocked(
Prabir Pradhan24047542023-11-02 17:14:59 +00001644 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry,
Prabir Pradhan99987712020-11-10 18:43:05 -08001645 DropReason& dropReason) {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001646 dropReason = DropReason::NOT_DROPPED;
1647
Prabir Pradhan99987712020-11-10 18:43:05 -08001648 const bool haveWindowWithPointerCapture = mWindowTokenWithPointerCapture != nullptr;
Prabir Pradhan99987712020-11-10 18:43:05 -08001649 sp<IBinder> token;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001650
1651 if (entry->pointerCaptureRequest.enable) {
1652 // Enable Pointer Capture.
1653 if (haveWindowWithPointerCapture &&
1654 (entry->pointerCaptureRequest == mCurrentPointerCaptureRequest)) {
Prabir Pradhan7092e262022-05-03 16:51:09 +00001655 // This can happen if pointer capture is disabled and re-enabled before we notify the
1656 // app of the state change, so there is no need to notify the app.
1657 ALOGI("Skipping dispatch of Pointer Capture being enabled: no state change.");
1658 return;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001659 }
1660 if (!mCurrentPointerCaptureRequest.enable) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001661 // This can happen if a window requests capture and immediately releases capture.
1662 ALOGW("No window requested Pointer Capture.");
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001663 dropReason = DropReason::NO_POINTER_CAPTURE;
Prabir Pradhan99987712020-11-10 18:43:05 -08001664 return;
1665 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001666 if (entry->pointerCaptureRequest.seq != mCurrentPointerCaptureRequest.seq) {
1667 ALOGI("Skipping dispatch of Pointer Capture being enabled: sequence number mismatch.");
1668 return;
1669 }
1670
Vishnu Nairc519ff72021-01-21 08:23:08 -08001671 token = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08001672 LOG_ALWAYS_FATAL_IF(!token, "Cannot find focused window for Pointer Capture.");
1673 mWindowTokenWithPointerCapture = token;
1674 } else {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001675 // Disable Pointer Capture.
1676 // We do not check if the sequence number matches for requests to disable Pointer Capture
1677 // for two reasons:
1678 // 1. Pointer Capture can be disabled by a focus change, which means we can get two entries
1679 // to disable capture with the same sequence number: one generated by
1680 // disablePointerCaptureForcedLocked() and another as an acknowledgement of Pointer
1681 // Capture being disabled in InputReader.
1682 // 2. We respect any request to disable Pointer Capture generated by InputReader, since the
1683 // actual Pointer Capture state that affects events being generated by input devices is
1684 // in InputReader.
1685 if (!haveWindowWithPointerCapture) {
1686 // Pointer capture was already forcefully disabled because of focus change.
1687 dropReason = DropReason::NOT_DROPPED;
1688 return;
1689 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001690 token = mWindowTokenWithPointerCapture;
1691 mWindowTokenWithPointerCapture = nullptr;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001692 if (mCurrentPointerCaptureRequest.enable) {
Prabir Pradhan7d030382020-12-21 07:58:35 -08001693 setPointerCaptureLocked(false);
1694 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001695 }
1696
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001697 auto connection = getConnectionLocked(token);
1698 if (connection == nullptr) {
Prabir Pradhan99987712020-11-10 18:43:05 -08001699 // Window has gone away, clean up Pointer Capture state.
1700 mWindowTokenWithPointerCapture = nullptr;
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001701 if (mCurrentPointerCaptureRequest.enable) {
Prabir Pradhan7d030382020-12-21 07:58:35 -08001702 setPointerCaptureLocked(false);
1703 }
Prabir Pradhan99987712020-11-10 18:43:05 -08001704 return;
1705 }
1706 InputTarget target;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001707 target.connection = connection;
Prabir Pradhan99987712020-11-10 18:43:05 -08001708 entry->dispatchInProgress = true;
1709 dispatchEventLocked(currentTime, entry, {target});
1710
1711 dropReason = DropReason::NOT_DROPPED;
1712}
1713
Prabir Pradhan24047542023-11-02 17:14:59 +00001714void InputDispatcher::dispatchTouchModeChangeLocked(
1715 nsecs_t currentTime, const std::shared_ptr<const TouchModeEntry>& entry) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001716 const std::vector<sp<WindowInfoHandle>>& windowHandles =
Antonio Kantek15beb512022-06-13 22:35:41 +00001717 getWindowHandlesLocked(entry->displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001718 if (windowHandles.empty()) {
1719 return;
1720 }
1721 const std::vector<InputTarget> inputTargets =
1722 getInputTargetsFromWindowHandlesLocked(windowHandles);
1723 if (inputTargets.empty()) {
1724 return;
1725 }
1726 entry->dispatchInProgress = true;
1727 dispatchEventLocked(currentTime, entry, inputTargets);
1728}
1729
1730std::vector<InputTarget> InputDispatcher::getInputTargetsFromWindowHandlesLocked(
1731 const std::vector<sp<WindowInfoHandle>>& windowHandles) const {
1732 std::vector<InputTarget> inputTargets;
1733 for (const sp<WindowInfoHandle>& handle : windowHandles) {
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001734 const sp<IBinder>& token = handle->getToken();
1735 if (token == nullptr) {
1736 continue;
1737 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001738 std::shared_ptr<Connection> connection = getConnectionLocked(token);
1739 if (connection == nullptr) {
1740 continue; // Connection has gone away
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001741 }
1742 InputTarget target;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08001743 target.connection = connection;
Antonio Kantek7242d8b2021-08-05 16:07:20 -07001744 inputTargets.push_back(target);
1745 }
1746 return inputTargets;
1747}
1748
Prabir Pradhan24047542023-11-02 17:14:59 +00001749bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001750 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001751 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001752 if (!entry->dispatchInProgress) {
1753 if (entry->repeatCount == 0 && entry->action == AKEY_EVENT_ACTION_DOWN &&
1754 (entry->policyFlags & POLICY_FLAG_TRUSTED) &&
1755 (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
1756 if (mKeyRepeatState.lastKeyEntry &&
Chris Ye2ad95392020-09-01 13:44:44 -07001757 mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode &&
Michael Wrightd02c5b62014-02-10 15:10:22 -08001758 // We have seen two identical key downs in a row which indicates that the device
1759 // driver is automatically generating key repeats itself. We take note of the
1760 // repeat here, but we disable our own next key repeat timer since it is clear that
1761 // we will not need to synthesize key repeats ourselves.
Chris Ye2ad95392020-09-01 13:44:44 -07001762 mKeyRepeatState.lastKeyEntry->deviceId == entry->deviceId) {
1763 // Make sure we don't get key down from a different device. If a different
1764 // device Id has same key pressed down, the new device Id will replace the
1765 // current one to hold the key repeat with repeat count reset.
1766 // In the future when got a KEY_UP on the device id, drop it and do not
1767 // stop the key repeat on current device.
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
1769 resetKeyRepeatLocked();
Colin Cross5b799302022-10-18 21:52:41 -07001770 mKeyRepeatState.nextRepeatTime = LLONG_MAX; // don't generate repeats ourselves
Michael Wrightd02c5b62014-02-10 15:10:22 -08001771 } else {
1772 // Not a repeat. Save key down state in case we do see a repeat later.
1773 resetKeyRepeatLocked();
1774 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
1775 }
1776 mKeyRepeatState.lastKeyEntry = entry;
Chris Ye2ad95392020-09-01 13:44:44 -07001777 } else if (entry->action == AKEY_EVENT_ACTION_UP && mKeyRepeatState.lastKeyEntry &&
1778 mKeyRepeatState.lastKeyEntry->deviceId != entry->deviceId) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001779 // The key on device 'deviceId' is still down, do not stop key repeat
Prabir Pradhan65613802023-02-22 23:36:58 +00001780 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001781 ALOGD("deviceId=%d got KEY_UP as stale", entry->deviceId);
1782 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001783 } else if (!entry->syntheticRepeat) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784 resetKeyRepeatLocked();
1785 }
1786
1787 if (entry->repeatCount == 1) {
1788 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
1789 } else {
1790 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
1791 }
1792
1793 entry->dispatchInProgress = true;
1794
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001795 logOutboundKeyDetails("dispatchKey - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001796 }
1797
1798 // Handle case where the policy asked us to try again later last time.
Michael Wright5caf55a2022-11-24 22:31:42 +00001799 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800 if (currentTime < entry->interceptKeyWakeupTime) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001801 nextWakeupTime = std::min(nextWakeupTime, entry->interceptKeyWakeupTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802 return false; // wait until next wakeup
1803 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001804 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 entry->interceptKeyWakeupTime = 0;
1806 }
1807
1808 // Give the policy a chance to intercept the key.
Michael Wright5caf55a2022-11-24 22:31:42 +00001809 if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07001811 sp<IBinder> focusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08001812 mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00001813
1814 auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) {
1815 doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
1816 };
1817 postCommandLocked(std::move(command));
Josep del Riob3981622023-04-18 15:49:45 +00001818 // Poke user activity for keys not passed to user
1819 pokeUserActivityLocked(*entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820 return false; // wait for the command to run
1821 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00001822 entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823 }
Michael Wright5caf55a2022-11-24 22:31:42 +00001824 } else if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::SKIP) {
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001825 if (*dropReason == DropReason::NOT_DROPPED) {
1826 *dropReason = DropReason::POLICY;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827 }
1828 }
1829
1830 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001831 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001832 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001833 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
1834 : InputEventInjectionResult::FAILED);
Garfield Tan6a5a14e2020-01-28 13:24:04 -08001835 mReporter->reportDroppedKey(entry->id);
Josep del Riob3981622023-04-18 15:49:45 +00001836 // Poke user activity for undispatched keys
1837 pokeUserActivityLocked(*entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838 return true;
1839 }
1840
1841 // Identify targets.
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001842 InputEventInjectionResult injectionResult;
1843 sp<WindowInfoHandle> focusedWindow =
1844 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime,
1845 /*byref*/ injectionResult);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001846 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847 return false;
1848 }
1849
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001850 setInjectionResult(*entry, injectionResult);
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001851 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 return true;
1853 }
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001854 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
1855
1856 std::vector<InputTarget> inputTargets;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001857 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
1858 InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001860 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001861 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001863 if (mTracer) {
1864 ensureEventTraced(*entry);
1865 for (const auto& target : inputTargets) {
1866 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
1867 }
1868 }
1869
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870 // Dispatch the key.
1871 dispatchEventLocked(currentTime, entry, inputTargets);
1872 return true;
1873}
1874
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001875void InputDispatcher::logOutboundKeyDetails(const char* prefix, const KeyEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001876 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1877 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=0x%x, displayId=%" PRId32 ", "
1878 "policyFlags=0x%x, action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, "
1879 "metaState=0x%x, repeatCount=%d, downTime=%" PRId64,
1880 prefix, entry.eventTime, entry.deviceId, entry.source, entry.displayId,
1881 entry.policyFlags, entry.action, entry.flags, entry.keyCode, entry.scanCode,
1882 entry.metaState, entry.repeatCount, entry.downTime);
1883 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884}
1885
Prabir Pradhancef936d2021-07-21 16:17:52 +00001886void InputDispatcher::dispatchSensorLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00001887 const std::shared_ptr<const SensorEntry>& entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001888 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001889 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1890 ALOGD("notifySensorEvent eventTime=%" PRId64 ", hwTimestamp=%" PRId64 ", deviceId=%d, "
1891 "source=0x%x, sensorType=%s",
1892 entry->eventTime, entry->hwTimestamp, entry->deviceId, entry->source,
Dominik Laskowski75788452021-02-09 18:51:25 -08001893 ftl::enum_string(entry->sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001894 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00001895 auto command = [this, entry]() REQUIRES(mLock) {
1896 scoped_unlock unlock(mLock);
1897
1898 if (entry->accuracyChanged) {
Prabir Pradhana41d2442023-04-20 21:30:40 +00001899 mPolicy.notifySensorAccuracy(entry->deviceId, entry->sensorType, entry->accuracy);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001900 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00001901 mPolicy.notifySensorEvent(entry->deviceId, entry->sensorType, entry->accuracy,
1902 entry->hwTimestamp, entry->values);
Prabir Pradhancef936d2021-07-21 16:17:52 +00001903 };
1904 postCommandLocked(std::move(command));
Chris Yef59a2f42020-10-16 12:55:26 -07001905}
1906
1907bool InputDispatcher::flushSensor(int deviceId, InputDeviceSensorType sensorType) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001908 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
1909 ALOGD("flushSensor deviceId=%d, sensorType=%s", deviceId,
Dominik Laskowski75788452021-02-09 18:51:25 -08001910 ftl::enum_string(sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00001911 }
Chris Yef59a2f42020-10-16 12:55:26 -07001912 { // acquire lock
1913 std::scoped_lock _l(mLock);
1914
1915 for (auto it = mInboundQueue.begin(); it != mInboundQueue.end(); it++) {
Prabir Pradhan24047542023-11-02 17:14:59 +00001916 std::shared_ptr<const EventEntry> entry = *it;
Chris Yef59a2f42020-10-16 12:55:26 -07001917 if (entry->type == EventEntry::Type::SENSOR) {
1918 it = mInboundQueue.erase(it);
1919 releaseInboundEventLocked(entry);
1920 }
1921 }
1922 }
1923 return true;
1924}
1925
Prabir Pradhan24047542023-11-02 17:14:59 +00001926bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime,
1927 std::shared_ptr<const MotionEntry> entry,
Siarhei Vishniakou69505962023-12-28 12:07:04 -08001928 DropReason* dropReason, nsecs_t& nextWakeupTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +00001929 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 // Preprocessing.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001931 if (!entry->dispatchInProgress) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 entry->dispatchInProgress = true;
1933
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001934 logOutboundMotionDetails("dispatchMotion - ", *entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001935 }
1936
1937 // Clean up if dropping the event.
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -07001938 if (*dropReason != DropReason::NOT_DROPPED) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001939 setInjectionResult(*entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001940 *dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
1941 : InputEventInjectionResult::FAILED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001942 return true;
1943 }
1944
Prabir Pradhanaa561d12021-09-24 06:57:33 -07001945 const bool isPointerEvent = isFromSource(entry->source, AINPUT_SOURCE_CLASS_POINTER);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001946
1947 // Identify targets.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001948 std::vector<InputTarget> inputTargets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001950 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951 if (isPointerEvent) {
1952 // Pointer event. (eg. touchscreen)
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00001953
1954 if (mDragState &&
1955 (entry->action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_POINTER_DOWN) {
1956 // If drag and drop ongoing and pointer down occur: pilfer drag window pointers
1957 pilferPointersLocked(mDragState->dragWindow->getToken());
1958 }
1959
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08001960 inputTargets =
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07001961 findTouchedWindowTargetsLocked(currentTime, *entry, /*byref*/ injectionResult);
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08001962 LOG_ALWAYS_FATAL_IF(injectionResult != InputEventInjectionResult::SUCCEEDED &&
1963 !inputTargets.empty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 } else {
1965 // Non touch event. (eg. trackball)
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001966 sp<WindowInfoHandle> focusedWindow =
1967 findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime, injectionResult);
1968 if (injectionResult == InputEventInjectionResult::SUCCEEDED) {
1969 LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00001970 addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,
1971 InputTarget::Flags::FOREGROUND, getDownTime(*entry),
1972 inputTargets);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07001973 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001974 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001975 if (injectionResult == InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001976 return false;
1977 }
1978
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07001979 setInjectionResult(*entry, injectionResult);
Prabir Pradhan5735a322022-04-11 17:23:34 +00001980 if (injectionResult == InputEventInjectionResult::TARGET_MISMATCH) {
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07001981 return true;
1982 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08001983 if (injectionResult != InputEventInjectionResult::SUCCEEDED) {
Michael Wrightfb04fd52022-11-24 22:31:11 +00001984 CancelationOptions::Mode mode(
1985 isPointerEvent ? CancelationOptions::Mode::CANCEL_POINTER_EVENTS
1986 : CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS);
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07001987 CancelationOptions options(mode, "input event injection failed");
Linnan Lid8150952024-01-26 18:07:17 +00001988 options.displayId = entry->displayId;
Siarhei Vishniakou767917f2020-03-24 20:49:09 -07001989 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001990 return true;
1991 }
1992
Arthur Hung2fbf37f2018-09-13 18:16:41 +08001993 // Add monitor channels from event's or focused display.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07001994 addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08001995
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00001996 if (mTracer) {
1997 ensureEventTraced(*entry);
1998 for (const auto& target : inputTargets) {
1999 mTracer->dispatchToTargetHint(*entry->traceTracker, target);
2000 }
2001 }
2002
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003 // Dispatch the motion.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002004 dispatchEventLocked(currentTime, entry, inputTargets);
2005 return true;
2006}
2007
chaviw98318de2021-05-19 16:45:23 -05002008void InputDispatcher::enqueueDragEventLocked(const sp<WindowInfoHandle>& windowHandle,
Arthur Hung54745652022-04-20 07:17:41 +00002009 bool isExiting, const int32_t rawX,
2010 const int32_t rawY) {
2011 const vec2 xy = windowHandle->getInfo()->transform.transform(vec2(rawX, rawY));
arthurhungb89ccb02020-12-30 16:19:01 +08002012 std::unique_ptr<DragEntry> dragEntry =
Arthur Hung54745652022-04-20 07:17:41 +00002013 std::make_unique<DragEntry>(mIdGenerator.nextId(), now(), windowHandle->getToken(),
2014 isExiting, xy.x, xy.y);
arthurhungb89ccb02020-12-30 16:19:01 +08002015
2016 enqueueInboundEventLocked(std::move(dragEntry));
2017}
2018
Prabir Pradhan24047542023-11-02 17:14:59 +00002019void InputDispatcher::dispatchDragLocked(nsecs_t currentTime,
2020 std::shared_ptr<const DragEntry> entry) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002021 std::shared_ptr<Connection> connection = getConnectionLocked(entry->connectionToken);
2022 if (connection == nullptr) {
2023 return; // Connection has gone away
arthurhungb89ccb02020-12-30 16:19:01 +08002024 }
2025 InputTarget target;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002026 target.connection = connection;
arthurhungb89ccb02020-12-30 16:19:01 +08002027 entry->dispatchInProgress = true;
2028 dispatchEventLocked(currentTime, entry, {target});
2029}
2030
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002031void InputDispatcher::logOutboundMotionDetails(const char* prefix, const MotionEntry& entry) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002032 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002033 ALOGD("%seventTime=%" PRId64 ", deviceId=%d, source=%s, displayId=%" PRId32
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002034 ", policyFlags=0x%x, "
Siarhei Vishniakouca205502021-07-16 21:31:58 +00002035 "action=%s, actionButton=0x%x, flags=0x%x, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002036 "metaState=0x%x, buttonState=0x%x,"
2037 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%" PRId64,
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002038 prefix, entry.eventTime, entry.deviceId,
2039 inputEventSourceToString(entry.source).c_str(), entry.displayId, entry.policyFlags,
2040 MotionEvent::actionToString(entry.action).c_str(), entry.actionButton, entry.flags,
2041 entry.metaState, entry.buttonState, entry.edgeFlags, entry.xPrecision,
2042 entry.yPrecision, entry.downTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002043
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002044 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002045 ALOGD(" Pointer %d: id=%d, toolType=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002046 "x=%f, y=%f, pressure=%f, size=%f, "
2047 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
2048 "orientation=%f",
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -07002049 i, entry.pointerProperties[i].id,
2050 ftl::enum_string(entry.pointerProperties[i].toolType).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002051 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2052 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2053 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2054 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2055 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2056 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2057 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2058 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2059 entry.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
2060 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002061 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002062}
2063
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07002064void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
Prabir Pradhan24047542023-11-02 17:14:59 +00002065 std::shared_ptr<const EventEntry> eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002066 const std::vector<InputTarget>& inputTargets) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002067 ATRACE_CALL();
Prabir Pradhan61a5d242021-07-26 16:41:09 +00002068 if (DEBUG_DISPATCH_CYCLE) {
2069 ALOGD("dispatchEventToCurrentInputTargets");
2070 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002071
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002072 processInteractionsLocked(*eventEntry, inputTargets);
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00002073
Michael Wrightd02c5b62014-02-10 15:10:22 -08002074 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
2075
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002076 pokeUserActivityLocked(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002077
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08002078 for (const InputTarget& inputTarget : inputTargets) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002079 std::shared_ptr<Connection> connection = inputTarget.connection;
2080 prepareDispatchCycleLocked(currentTime, connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002081 }
2082}
2083
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07002084void InputDispatcher::cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002085 // We will not be breaking any connections here, even if the policy wants us to abort dispatch.
2086 // If the policy decides to close the app, we will get a channel removal event via
2087 // unregisterInputChannel, and will clean up the connection that way. We are already not
2088 // sending new pointers to the connection when it blocked, but focused events will continue to
2089 // pile up.
2090 ALOGW("Canceling events for %s because it is unresponsive",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002091 connection->getInputChannelName().c_str());
Prabir Pradhanfc364722024-02-08 17:51:20 +00002092 if (connection->status != Connection::Status::NORMAL) {
2093 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002094 }
Prabir Pradhanfc364722024-02-08 17:51:20 +00002095 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS,
2096 "application not responding");
2097
2098 sp<WindowInfoHandle> windowHandle;
2099 if (!connection->monitor) {
2100 windowHandle = getWindowHandleLocked(connection->getToken());
2101 if (windowHandle == nullptr) {
2102 // The window that is receiving this ANR was removed, so there is no need to generate
2103 // cancellations, because the cancellations would have already been generated when
2104 // the window was removed.
2105 return;
2106 }
2107 }
2108 synthesizeCancelationEventsForConnectionLocked(connection, options, windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002109}
2110
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002111void InputDispatcher::resetNoFocusedWindowTimeoutLocked() {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01002112 if (DEBUG_FOCUS) {
2113 ALOGD("Resetting ANR timeouts.");
2114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002115
2116 // Reset input target wait timeout.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002117 mNoFocusedWindowTimeoutTime = std::nullopt;
Chris Yea209fde2020-07-22 13:54:51 -07002118 mAwaitedFocusedApplication.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002119}
2120
Tiger Huang721e26f2018-07-24 22:26:19 +08002121/**
2122 * Get the display id that the given event should go to. If this event specifies a valid display id,
2123 * then it should be dispatched to that display. Otherwise, the event goes to the focused display.
2124 * Focused display is the display that the user most recently interacted with.
2125 */
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002126int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) {
Tiger Huang721e26f2018-07-24 22:26:19 +08002127 int32_t displayId;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002128 switch (entry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002129 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002130 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
2131 displayId = keyEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002132 break;
2133 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002134 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07002135 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
2136 displayId = motionEntry.displayId;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002137 break;
2138 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00002139 case EventEntry::Type::TOUCH_MODE_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08002140 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01002141 case EventEntry::Type::FOCUS:
Siarhei Vishniakou49483272019-10-22 13:13:47 -07002142 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07002143 case EventEntry::Type::DEVICE_RESET:
arthurhungb89ccb02020-12-30 16:19:01 +08002144 case EventEntry::Type::SENSOR:
2145 case EventEntry::Type::DRAG: {
Dominik Laskowski75788452021-02-09 18:51:25 -08002146 ALOGE("%s events do not have a target display", ftl::enum_string(entry.type).c_str());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07002147 return ADISPLAY_ID_NONE;
2148 }
Tiger Huang721e26f2018-07-24 22:26:19 +08002149 }
2150 return displayId == ADISPLAY_ID_NONE ? mFocusedDisplayId : displayId;
2151}
2152
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002153bool InputDispatcher::shouldWaitToSendKeyLocked(nsecs_t currentTime,
2154 const char* focusedWindowName) {
2155 if (mAnrTracker.empty()) {
2156 // already processed all events that we waited for
2157 mKeyIsWaitingForEventsTimeout = std::nullopt;
2158 return false;
2159 }
2160
2161 if (!mKeyIsWaitingForEventsTimeout.has_value()) {
2162 // Start the timer
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00002163 // Wait to send key because there are unprocessed events that may cause focus to change
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002164 mKeyIsWaitingForEventsTimeout = currentTime +
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08002165 std::chrono::duration_cast<std::chrono::nanoseconds>(
2166 mPolicy.getKeyWaitingForEventsTimeout())
Siarhei Vishniakou70622952020-07-30 11:17:23 -05002167 .count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002168 return true;
2169 }
2170
2171 // We still have pending events, and already started the timer
2172 if (currentTime < *mKeyIsWaitingForEventsTimeout) {
2173 return true; // Still waiting
2174 }
2175
2176 // Waited too long, and some connection still hasn't processed all motions
2177 // Just send the key to the focused window
2178 ALOGW("Dispatching key to %s even though there are other unprocessed events",
2179 focusedWindowName);
2180 mKeyIsWaitingForEventsTimeout = std::nullopt;
2181 return false;
2182}
2183
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002184sp<WindowInfoHandle> InputDispatcher::findFocusedWindowTargetLocked(
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002185 nsecs_t currentTime, const EventEntry& entry, nsecs_t& nextWakeupTime,
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002186 InputEventInjectionResult& outInjectionResult) {
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002187 outInjectionResult = InputEventInjectionResult::FAILED; // Default result
Michael Wrightd02c5b62014-02-10 15:10:22 -08002188
Tiger Huang721e26f2018-07-24 22:26:19 +08002189 int32_t displayId = getTargetDisplayId(entry);
chaviw98318de2021-05-19 16:45:23 -05002190 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Chris Yea209fde2020-07-22 13:54:51 -07002191 std::shared_ptr<InputApplicationHandle> focusedApplicationHandle =
Tiger Huang721e26f2018-07-24 22:26:19 +08002192 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
2193
Michael Wrightd02c5b62014-02-10 15:10:22 -08002194 // If there is no currently focused window and no focused application
2195 // then drop the event.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002196 if (focusedWindowHandle == nullptr && focusedApplicationHandle == nullptr) {
2197 ALOGI("Dropping %s event because there is no focused window or focused application in "
2198 "display %" PRId32 ".",
Dominik Laskowski75788452021-02-09 18:51:25 -08002199 ftl::enum_string(entry.type).c_str(), displayId);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002200 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002201 }
2202
Vishnu Nair062a8672021-09-03 16:07:44 -07002203 // Drop key events if requested by input feature
2204 if (focusedWindowHandle != nullptr && shouldDropInput(entry, focusedWindowHandle)) {
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002205 return nullptr;
Vishnu Nair062a8672021-09-03 16:07:44 -07002206 }
2207
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002208 // Compatibility behavior: raise ANR if there is a focused application, but no focused window.
2209 // Only start counting when we have a focused event to dispatch. The ANR is canceled if we
2210 // start interacting with another application via touch (app switch). This code can be removed
2211 // if the "no focused window ANR" is moved to the policy. Input doesn't know whether
2212 // an app is expected to have a focused window.
2213 if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) {
2214 if (!mNoFocusedWindowTimeoutTime.has_value()) {
2215 // We just discovered that there's no focused window. Start the ANR timer
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002216 std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout(
2217 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
2218 mNoFocusedWindowTimeoutTime = currentTime + timeout.count();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002219 mAwaitedFocusedApplication = focusedApplicationHandle;
Siarhei Vishniakouf56b2692020-09-08 19:43:33 -05002220 mAwaitedApplicationDisplayId = displayId;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002221 ALOGW("Waiting because no window has focus but %s may eventually add a "
2222 "window when it finishes starting up. Will wait for %" PRId64 "ms",
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -05002223 mAwaitedFocusedApplication->getName().c_str(), millis(timeout));
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002224 nextWakeupTime = std::min(nextWakeupTime, *mNoFocusedWindowTimeoutTime);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002225 outInjectionResult = InputEventInjectionResult::PENDING;
2226 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002227 } else if (currentTime > *mNoFocusedWindowTimeoutTime) {
2228 // Already raised ANR. Drop the event
2229 ALOGE("Dropping %s event because there is no focused window",
Dominik Laskowski75788452021-02-09 18:51:25 -08002230 ftl::enum_string(entry.type).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002231 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002232 } else {
2233 // Still waiting for the focused window
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002234 outInjectionResult = InputEventInjectionResult::PENDING;
2235 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002236 }
2237 }
2238
2239 // we have a valid, non-null focused window
2240 resetNoFocusedWindowTimeoutLocked();
2241
Prabir Pradhan5735a322022-04-11 17:23:34 +00002242 // Verify targeted injection.
2243 if (const auto err = verifyTargetedInjection(focusedWindowHandle, entry); err) {
2244 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002245 outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH;
2246 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002247 }
2248
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08002249 if (focusedWindowHandle->getInfo()->inputConfig.test(
2250 WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002251 ALOGI("Waiting because %s is paused", focusedWindowHandle->getName().c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002252 outInjectionResult = InputEventInjectionResult::PENDING;
2253 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002254 }
2255
2256 // If the event is a key event, then we must wait for all previous events to
2257 // complete before delivering it because previous events may have the
2258 // side-effect of transferring focus to a different window and we want to
2259 // ensure that the following keys are sent to the new window.
2260 //
2261 // Suppose the user touches a button in a window then immediately presses "A".
2262 // If the button causes a pop-up window to appear then we want to ensure that
2263 // the "A" key is delivered to the new pop-up window. This is because users
2264 // often anticipate pending UI changes when typing on a keyboard.
2265 // To obtain this behavior, we must serialize key events with respect to all
2266 // prior input events.
2267 if (entry.type == EventEntry::Type::KEY) {
2268 if (shouldWaitToSendKeyLocked(currentTime, focusedWindowHandle->getName().c_str())) {
Siarhei Vishniakou69505962023-12-28 12:07:04 -08002269 nextWakeupTime = std::min(nextWakeupTime, *mKeyIsWaitingForEventsTimeout);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002270 outInjectionResult = InputEventInjectionResult::PENDING;
2271 return nullptr;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002272 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002273 }
2274
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002275 outInjectionResult = InputEventInjectionResult::SUCCEEDED;
2276 return focusedWindowHandle;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002277}
2278
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002279/**
2280 * Given a list of monitors, remove the ones we cannot find a connection for, and the ones
2281 * that are currently unresponsive.
2282 */
Prabir Pradhan0a99c922021-09-03 08:27:53 -07002283std::vector<Monitor> InputDispatcher::selectResponsiveMonitorsLocked(
2284 const std::vector<Monitor>& monitors) const {
2285 std::vector<Monitor> responsiveMonitors;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002286 std::copy_if(monitors.begin(), monitors.end(), std::back_inserter(responsiveMonitors),
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002287 [](const Monitor& monitor) REQUIRES(mLock) {
2288 std::shared_ptr<Connection> connection = monitor.connection;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002289 if (!connection->responsive) {
2290 ALOGW("Unresponsive monitor %s will not get the new gesture",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08002291 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002292 return false;
2293 }
2294 return true;
2295 });
2296 return responsiveMonitors;
2297}
2298
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002299std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked(
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002300 nsecs_t currentTime, const MotionEntry& entry,
Siarhei Vishniakou4fe57392022-10-25 13:44:30 -07002301 InputEventInjectionResult& outInjectionResult) {
Michael Wright3dd60e22019-03-27 22:06:44 +00002302 ATRACE_CALL();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002304 std::vector<InputTarget> targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 // For security reasons, we defer updating the touch state until we are sure that
2306 // event injection will be allowed.
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002307 const int32_t displayId = entry.displayId;
2308 const int32_t action = entry.action;
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07002309 const int32_t maskedAction = MotionEvent::getActionMasked(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310
2311 // Update the touch state as needed based on the properties of the touch event.
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002312 outInjectionResult = InputEventInjectionResult::PENDING;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002314 // Copy current touch state into tempTouchState.
2315 // This state will be used to update mTouchStatesByDisplay at the end of this function.
2316 // If no state for the specified display exists, then our initial state will be empty.
Yi Kong9b14ac62018-07-17 13:48:38 -07002317 const TouchState* oldState = nullptr;
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002318 TouchState tempTouchState;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002319 if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
2320 oldState = &(it->second);
Prabir Pradhane680f9b2022-02-04 04:24:00 -08002321 tempTouchState = *oldState;
Jeff Brownf086ddb2014-02-11 14:28:48 -08002322 }
2323
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002324 bool isSplit = shouldSplitTouch(tempTouchState, entry);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002325
2326 const bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE ||
2327 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2328 maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002329 // A DOWN could be generated from POINTER_DOWN if the initial pointers did not land into any
2330 // touchable windows.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002331 const bool wasDown = oldState != nullptr && oldState->isDown(entry.deviceId);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002332 const bool isDown = (maskedAction == AMOTION_EVENT_ACTION_DOWN) ||
2333 (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN && !wasDown);
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002334 const bool newGesture = isDown || maskedAction == AMOTION_EVENT_ACTION_SCROLL ||
2335 maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER ||
2336 maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE;
Prabir Pradhanaa561d12021-09-24 06:57:33 -07002337 const bool isFromMouse = isFromSource(entry.source, AINPUT_SOURCE_MOUSE);
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -08002338
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002339 if (newGesture) {
2340 isSplit = false;
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002341 }
2342
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002343 if (isDown && tempTouchState.hasHoveringPointers(entry.deviceId)) {
2344 // Compatibility behaviour: ACTION_DOWN causes HOVER_EXIT to get generated.
2345 tempTouchState.clearHoveringPointers(entry.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 }
2347
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002348 if (isHoverAction) {
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002349 if (wasDown) {
2350 // Started hovering, but the device is already down: reject the hover event
2351 LOG(ERROR) << "Got hover event " << entry.getDescription()
2352 << " but the device is already down " << oldState->dump();
2353 outInjectionResult = InputEventInjectionResult::FAILED;
2354 return {};
2355 }
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002356 // For hover actions, we will treat 'tempTouchState' as a new state, so let's erase
2357 // all of the existing hovering pointers and recompute.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002358 tempTouchState.clearHoveringPointers(entry.deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002359 }
2360
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
2362 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002363 const auto [x, y] = resolveTouchedPosition(entry);
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002364 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002365 const PointerProperties& pointer = entry.pointerProperties[pointerIndex];
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002366 // Outside targets should be added upon first dispatched DOWN event. That means, this should
2367 // be a pointer that would generate ACTION_DOWN, *and* touch should not already be down.
Prabir Pradhand65552b2021-10-07 11:23:50 -07002368 const bool isStylus = isPointerFromStylus(entry, pointerIndex);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002369 sp<WindowInfoHandle> newTouchedWindowHandle =
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002370 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Michael Wright3dd60e22019-03-27 22:06:44 +00002371
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002372 if (isDown) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002373 targets += findOutsideTargetsLocked(displayId, newTouchedWindowHandle, pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002374 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375 // Handle the case where we did not find a window.
Yi Kong9b14ac62018-07-17 13:48:38 -07002376 if (newTouchedWindowHandle == nullptr) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00002377 ALOGD("No new touched window at (%.1f, %.1f) in display %" PRId32, x, y, displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378 // Try to assign the pointer to the first foreground window we find, if there is one.
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002379 newTouchedWindowHandle = tempTouchState.getFirstForegroundWindowHandle();
Michael Wright3dd60e22019-03-27 22:06:44 +00002380 }
2381
Prabir Pradhan5735a322022-04-11 17:23:34 +00002382 // Verify targeted injection.
2383 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2384 ALOGW("Dropping injected touch event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002385 outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH;
Prabir Pradhan5735a322022-04-11 17:23:34 +00002386 newTouchedWindowHandle = nullptr;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002387 return {};
Prabir Pradhan5735a322022-04-11 17:23:34 +00002388 }
2389
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002390 // Figure out whether splitting will be allowed for this window.
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002391 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002392 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
2393 // New window supports splitting, but we should never split mouse events.
2394 isSplit = !isFromMouse;
2395 } else if (isSplit) {
2396 // New window does not support splitting but we have already split events.
2397 // Ignore the new window.
Siarhei Vishniakou25537f82023-07-18 14:35:47 -07002398 LOG(INFO) << "Skipping " << newTouchedWindowHandle->getName()
2399 << " because it doesn't support split touch";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002400 newTouchedWindowHandle = nullptr;
2401 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002402 } else {
2403 // No window is touched, so set split to true. This will allow the next pointer down to
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002404 // be delivered to a new window which supports split touch. Pointers from a mouse device
2405 // should never be split.
Siarhei Vishniakou64a98532022-10-25 15:20:24 -07002406 isSplit = !isFromMouse;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07002407 }
2408
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002409 std::vector<sp<WindowInfoHandle>> newTouchedWindows =
Prabir Pradhand65552b2021-10-07 11:23:50 -07002410 findTouchedSpyWindowsAtLocked(displayId, x, y, isStylus);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002411 if (newTouchedWindowHandle != nullptr) {
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002412 // Process the foreground window first so that it is the first to receive the event.
2413 newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle);
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002414 }
2415
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002416 if (newTouchedWindows.empty()) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00002417 ALOGI("Dropping event because there is no touchable window at (%.1f, %.1f) on display "
2418 "%d.",
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002419 x, y, displayId);
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002420 outInjectionResult = InputEventInjectionResult::FAILED;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002421 return {};
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002422 }
2423
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002424 for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07002425 if (!canWindowReceiveMotionLocked(windowHandle, entry)) {
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002426 continue;
2427 }
2428
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002429 if (isHoverAction) {
Siarhei Vishniakoub681c202023-05-01 11:22:33 -07002430 // The "windowHandle" is the target of this hovering pointer.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002431 tempTouchState.addHoveringPointerToWindow(windowHandle, entry.deviceId, pointer);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002432 }
2433
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002434 // Set target flags.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002435 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002436
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002437 if (canReceiveForegroundTouches(*windowHandle->getInfo())) {
2438 // There should only be one touched window that can be "foreground" for the pointer.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002439 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan07e05b62021-11-19 03:57:24 -08002440 }
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002441
2442 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002443 targetFlags |= InputTarget::Flags::SPLIT;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002444 }
2445 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002446 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002447 } else if (isWindowObscuredLocked(windowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002448 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002449 }
Michael Wright3dd60e22019-03-27 22:06:44 +00002450
2451 // Update the temporary touch state.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002452
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002453 if (!isHoverAction) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002454 const bool isDownOrPointerDown = maskedAction == AMOTION_EVENT_ACTION_DOWN ||
2455 maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002456 tempTouchState.addOrUpdateWindow(windowHandle, InputTarget::DispatchMode::AS_IS,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002457 targetFlags, entry.deviceId, {pointer},
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002458 isDownOrPointerDown
2459 ? std::make_optional(entry.eventTime)
2460 : std::nullopt);
2461 // If this is the pointer going down and the touched window has a wallpaper
2462 // then also add the touched wallpaper windows so they are locked in for the
2463 // duration of the touch gesture. We do not collect wallpapers during HOVER_MOVE or
2464 // SCROLL because the wallpaper engine only supports touch events. We would need to
2465 // add a mechanism similar to View.onGenericMotionEvent to enable wallpapers to
2466 // handle these events.
2467 if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) &&
Arthur Hungc539dbb2022-12-08 07:45:36 +00002468 windowHandle->getInfo()->inputConfig.test(
2469 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
2470 sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle);
2471 if (wallpaper != nullptr) {
2472 ftl::Flags<InputTarget::Flags> wallpaperFlags =
2473 InputTarget::Flags::WINDOW_IS_OBSCURED |
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002474 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Arthur Hungc539dbb2022-12-08 07:45:36 +00002475 if (isSplit) {
2476 wallpaperFlags |= InputTarget::Flags::SPLIT;
2477 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002478 tempTouchState.addOrUpdateWindow(wallpaper,
2479 InputTarget::DispatchMode::AS_IS,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002480 wallpaperFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002481 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002482 }
2483 }
2484 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002486
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002487 // If a window is already pilfering some pointers, give it this new pointer as well and
2488 // make it pilfering. This will prevent other non-spy windows from getting this pointer,
2489 // which is a specific behaviour that we want.
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002490 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002491 if (touchedWindow.hasTouchingPointer(entry.deviceId, pointer.id) &&
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002492 touchedWindow.hasPilferingPointers(entry.deviceId)) {
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002493 // This window is already pilfering some pointers, and this new pointer is also
2494 // going to it. Therefore, take over this pointer and don't give it to anyone
2495 // else.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002496 touchedWindow.addPilferingPointer(entry.deviceId, pointer.id);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +00002497 }
2498 }
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -08002499
2500 // Restrict all pilfered pointers to the pilfering windows.
2501 tempTouchState.cancelPointersForNonPilferingWindows();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502 } else {
2503 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
2504
2505 // If the pointer is not currently down, then ignore the event.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002506 if (!tempTouchState.isDown(entry.deviceId) &&
2507 maskedAction != AMOTION_EVENT_ACTION_HOVER_EXIT) {
Siarhei Vishniakou31dd1552023-10-30 18:46:10 -07002508 if (DEBUG_DROPPED_EVENTS_VERBOSE) {
2509 LOG(INFO) << "Dropping event because the pointer for device " << entry.deviceId
2510 << " is not down or we previously dropped the pointer down event in "
2511 << "display " << displayId << ": " << entry.getDescription();
2512 }
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002513 outInjectionResult = InputEventInjectionResult::FAILED;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002514 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 }
2516
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002517 // If the pointer is not currently hovering, then ignore the event.
2518 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT) {
2519 const int32_t pointerId = entry.pointerProperties[0].id;
2520 if (oldState == nullptr ||
2521 oldState->getWindowsWithHoveringPointer(entry.deviceId, pointerId).empty()) {
2522 LOG(INFO) << "Dropping event because the hovering pointer is not in any windows in "
2523 "display "
2524 << displayId << ": " << entry.getDescription();
2525 outInjectionResult = InputEventInjectionResult::FAILED;
2526 return {};
2527 }
2528 tempTouchState.removeHoveringPointer(entry.deviceId, pointerId);
2529 }
2530
arthurhung6d4bed92021-03-17 11:59:33 +08002531 addDragEventLocked(entry);
arthurhungb89ccb02020-12-30 16:19:01 +08002532
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533 // Check whether touches should slip outside of the current foreground window.
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002534 if (maskedAction == AMOTION_EVENT_ACTION_MOVE && entry.getPointerCount() == 1 &&
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002535 tempTouchState.isSlippery()) {
Siarhei Vishniakou9306c382022-09-30 15:30:31 -07002536 const auto [x, y] = resolveTouchedPosition(entry);
Harry Cutts33476232023-01-30 19:57:29 +00002537 const bool isStylus = isPointerFromStylus(entry, /*pointerIndex=*/0);
chaviw98318de2021-05-19 16:45:23 -05002538 sp<WindowInfoHandle> oldTouchedWindowHandle =
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002539 tempTouchState.getFirstForegroundWindowHandle();
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002540 LOG_ALWAYS_FATAL_IF(oldTouchedWindowHandle == nullptr);
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002541 sp<WindowInfoHandle> newTouchedWindowHandle =
2542 findTouchedWindowAtLocked(displayId, x, y, isStylus);
Vishnu Nair062a8672021-09-03 16:07:44 -07002543
Prabir Pradhan5735a322022-04-11 17:23:34 +00002544 // Verify targeted injection.
2545 if (const auto err = verifyTargetedInjection(newTouchedWindowHandle, entry); err) {
2546 ALOGW("Dropping injected event: %s", (*err).c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002547 outInjectionResult = os::InputEventInjectionResult::TARGET_MISMATCH;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002548 return {};
Prabir Pradhan5735a322022-04-11 17:23:34 +00002549 }
2550
Vishnu Nair062a8672021-09-03 16:07:44 -07002551 // Drop touch events if requested by input feature
2552 if (newTouchedWindowHandle != nullptr &&
2553 shouldDropInput(entry, newTouchedWindowHandle)) {
2554 newTouchedWindowHandle = nullptr;
2555 }
2556
Siarhei Vishniakouafa08cc2023-05-08 22:35:50 -07002557 if (newTouchedWindowHandle != nullptr &&
2558 !haveSameToken(oldTouchedWindowHandle, newTouchedWindowHandle)) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002559 ALOGI("Touch is slipping out of window %s into window %s in display %" PRId32,
Siarhei Vishniakou0f6558d2023-04-21 12:05:13 -07002560 oldTouchedWindowHandle->getName().c_str(),
2561 newTouchedWindowHandle->getName().c_str(), displayId);
2562
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 // Make a slippery exit from the old window.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08002564 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002565 const PointerProperties& pointer = entry.pointerProperties[0];
2566 pointerIds.set(pointer.id);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002567
2568 const TouchedWindow& touchedWindow =
2569 tempTouchState.getTouchedWindow(oldTouchedWindowHandle);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002570 addPointerWindowTargetLocked(oldTouchedWindowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002571 InputTarget::DispatchMode::SLIPPERY_EXIT,
2572 ftl::Flags<InputTarget::Flags>(), pointerIds,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002573 touchedWindow.getDownTimeInTarget(entry.deviceId),
2574 targets);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575
2576 // Make a slippery entrance into the new window.
2577 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Prabir Pradhan713bb3e2021-12-20 02:07:40 -08002578 isSplit = !isFromMouse;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579 }
2580
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002581 ftl::Flags<InputTarget::Flags> targetFlags;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002582 if (canReceiveForegroundTouches(*newTouchedWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002583 targetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00002584 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 if (isSplit) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002586 targetFlags |= InputTarget::Flags::SPLIT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 }
2588 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002589 targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
Siarhei Vishniakou870ecec2020-12-09 08:07:46 -10002590 } else if (isWindowObscuredLocked(newTouchedWindowHandle)) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002591 targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 }
2593
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002594 tempTouchState.addOrUpdateWindow(newTouchedWindowHandle,
2595 InputTarget::DispatchMode::SLIPPERY_ENTER,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002596 targetFlags, entry.deviceId, {pointer},
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002597 entry.eventTime);
Arthur Hungc539dbb2022-12-08 07:45:36 +00002598
2599 // Check if the wallpaper window should deliver the corresponding event.
2600 slipWallpaperTouch(targetFlags, oldTouchedWindowHandle, newTouchedWindowHandle,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002601 tempTouchState, entry.deviceId, pointer, targets);
2602 tempTouchState.removeTouchingPointerFromWindow(entry.deviceId, pointer.id,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002603 oldTouchedWindowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 }
2605 }
Arthur Hung96483742022-11-15 03:30:48 +00002606
2607 // Update the pointerIds for non-splittable when it received pointer down.
2608 if (!isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN) {
2609 // If no split, we suppose all touched windows should receive pointer down.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002610 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002611 std::vector<PointerProperties> touchingPointers{entry.pointerProperties[pointerIndex]};
2612 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Arthur Hung96483742022-11-15 03:30:48 +00002613 // Ignore drag window for it should just track one pointer.
2614 if (mDragState && mDragState->dragWindow == touchedWindow.windowHandle) {
2615 continue;
2616 }
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002617 touchedWindow.addTouchingPointers(entry.deviceId, touchingPointers);
Arthur Hung96483742022-11-15 03:30:48 +00002618 }
2619 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 }
2621
Prabir Pradhan3f90d312021-11-19 03:57:24 -08002622 // Update dispatching for hover enter and exit.
Sam Dubeyf886dec2023-01-27 13:28:19 +00002623 {
2624 std::vector<TouchedWindow> hoveringWindows =
2625 getHoveringWindowsLocked(oldState, tempTouchState, entry);
2626 for (const TouchedWindow& touchedWindow : hoveringWindows) {
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07002627 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002628 createInputTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
2629 touchedWindow.targetFlags,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002630 touchedWindow.getDownTimeInTarget(entry.deviceId));
Siarhei Vishniakoud5876ba2023-05-15 17:58:34 -07002631 if (!target) {
2632 continue;
2633 }
2634 // Hardcode to single hovering pointer for now.
2635 std::bitset<MAX_POINTER_ID + 1> pointerIds;
2636 pointerIds.set(entry.pointerProperties[0].id);
2637 target->addPointers(pointerIds, touchedWindow.windowHandle->getInfo()->transform);
2638 targets.push_back(*target);
Sam Dubeyf886dec2023-01-27 13:28:19 +00002639 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Prabir Pradhan5735a322022-04-11 17:23:34 +00002642 // Ensure that all touched windows are valid for injection.
2643 if (entry.injectionState != nullptr) {
2644 std::string errs;
2645 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan5735a322022-04-11 17:23:34 +00002646 const auto err = verifyTargetedInjection(touchedWindow.windowHandle, entry);
2647 if (err) errs += "\n - " + *err;
2648 }
2649 if (!errs.empty()) {
2650 ALOGW("Dropping targeted injection: At least one touched window is not owned by uid "
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002651 "%s:%s",
2652 entry.injectionState->targetUid->toString().c_str(), errs.c_str());
Siarhei Vishniakou6278ca22022-10-25 11:19:19 -07002653 outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH;
Siarhei Vishniakou8619eb32022-12-01 21:30:59 -08002654 return {};
Prabir Pradhan5735a322022-04-11 17:23:34 +00002655 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002656 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002657
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002658 // Check whether windows listening for outside touches are owned by the same UID. If the owner
2659 // has a different UID, then we will not reveal coordinate information to this window.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
chaviw98318de2021-05-19 16:45:23 -05002661 sp<WindowInfoHandle> foregroundWindowHandle =
Siarhei Vishniakou33eceeb2020-03-24 19:50:03 -07002662 tempTouchState.getFirstForegroundWindowHandle();
Michael Wright3dd60e22019-03-27 22:06:44 +00002663 if (foregroundWindowHandle) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00002664 const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002665 for (InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002666 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002667 sp<WindowInfoHandle> targetWindow =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002668 getWindowHandleLocked(target.connection->getToken());
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002669 if (targetWindow->getInfo()->ownerUid != foregroundWindowUid) {
2670 target.flags |= InputTarget::Flags::ZERO_COORDS;
Michael Wright3dd60e22019-03-27 22:06:44 +00002671 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 }
2673 }
2674 }
2675 }
2676
Harry Cuttsb166c002023-05-09 13:06:05 +00002677 // If this is a touchpad navigation gesture, it needs to only be sent to trusted targets, as we
2678 // only want the system UI to handle these gestures.
2679 const bool isTouchpadNavGesture = isFromSource(entry.source, AINPUT_SOURCE_MOUSE) &&
2680 entry.classification == MotionClassification::MULTI_FINGER_SWIPE;
2681 if (isTouchpadNavGesture) {
2682 filterUntrustedTargets(/* byref */ tempTouchState, /* byref */ targets);
2683 }
2684
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002685 // Output targets from the touch state.
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002686 for (const TouchedWindow& touchedWindow : tempTouchState.windows) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002687 std::vector<PointerProperties> touchingPointers =
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002688 touchedWindow.getTouchingPointers(entry.deviceId);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002689 if (touchingPointers.empty()) {
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -08002690 continue;
2691 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002692 addPointerWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.dispatchMode,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002693 touchedWindow.targetFlags, getPointerIds(touchingPointers),
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002694 touchedWindow.getDownTimeInTarget(entry.deviceId), targets);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002695 }
Sam Dubey39d37cf2022-12-07 18:05:35 +00002696
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002697 // During targeted injection, only allow owned targets to receive events
2698 std::erase_if(targets, [&](const InputTarget& target) {
2699 LOG_ALWAYS_FATAL_IF(target.windowHandle == nullptr);
2700 const auto err = verifyTargetedInjection(target.windowHandle, entry);
2701 if (err) {
2702 LOG(WARNING) << "Dropping injected event from " << target.windowHandle->getName()
2703 << ": " << (*err);
2704 return true;
2705 }
2706 return false;
2707 });
2708
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002709 if (targets.empty()) {
2710 LOG(INFO) << "Dropping event because no targets were found: " << entry.getDescription();
2711 outInjectionResult = InputEventInjectionResult::FAILED;
2712 return {};
2713 }
2714
2715 // If we only have windows getting ACTION_OUTSIDE, then drop the event, because there is no
2716 // window that is actually receiving the entire gesture.
2717 if (std::all_of(targets.begin(), targets.end(), [](const InputTarget& target) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002718 return target.dispatchMode == InputTarget::DispatchMode::OUTSIDE;
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07002719 })) {
2720 LOG(INFO) << "Dropping event because all windows would just receive ACTION_OUTSIDE: "
2721 << entry.getDescription();
2722 outInjectionResult = InputEventInjectionResult::FAILED;
2723 return {};
2724 }
2725
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002726 outInjectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727
Prabir Pradhan502a7252023-12-01 16:11:24 +00002728 // Now that we have generated all of the input targets for this event, reset the dispatch
2729 // mode for all touched window to AS_IS.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002730 for (TouchedWindow& touchedWindow : tempTouchState.windows) {
Prabir Pradhan502a7252023-12-01 16:11:24 +00002731 touchedWindow.dispatchMode = InputTarget::DispatchMode::AS_IS;
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002732 }
2733
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002734 // Update final pieces of touch state if the injector had permission.
Siarhei Vishniakou7be50c92023-11-17 17:09:08 -08002735 if (maskedAction == AMOTION_EVENT_ACTION_UP) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002736 // Pointer went up.
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07002737 tempTouchState.removeTouchingPointer(entry.deviceId, entry.pointerProperties[0].id);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +00002738 } else if (maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002739 // All pointers up or canceled.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002740 tempTouchState.removeAllPointersForDevice(entry.deviceId);
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002741 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2742 // One pointer went up.
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002743 const int32_t pointerIndex = MotionEvent::getActionIndex(action);
2744 const uint32_t pointerId = entry.pointerProperties[pointerIndex].id;
2745 tempTouchState.removeTouchingPointer(entry.deviceId, pointerId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002746 }
2747
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002748 // Save changes unless the action was scroll in which case the temporary touch
2749 // state was only valid for this one action.
2750 if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) {
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07002751 if (displayId >= 0) {
Siarhei Vishniakouf372b812023-02-14 18:06:51 -08002752 tempTouchState.clearWindowsWithoutPointers();
Siarhei Vishniakou79c32662022-10-25 17:56:25 -07002753 mTouchStatesByDisplay[displayId] = tempTouchState;
2754 } else {
2755 mTouchStatesByDisplay.erase(displayId);
2756 }
2757 }
2758
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -08002759 if (tempTouchState.windows.empty()) {
2760 mTouchStatesByDisplay.erase(displayId);
2761 }
2762
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08002763 return targets;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764}
2765
arthurhung6d4bed92021-03-17 11:59:33 +08002766void InputDispatcher::finishDragAndDrop(int32_t displayId, float x, float y) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07002767 // Prevent stylus interceptor windows from affecting drag and drop behavior for now, until we
2768 // have an explicit reason to support it.
2769 constexpr bool isStylus = false;
2770
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002771 sp<WindowInfoHandle> dropWindow =
Harry Cutts33476232023-01-30 19:57:29 +00002772 findTouchedWindowAtLocked(displayId, x, y, isStylus, /*ignoreDragWindow=*/true);
arthurhung6d4bed92021-03-17 11:59:33 +08002773 if (dropWindow) {
2774 vec2 local = dropWindow->getInfo()->transform.transform(x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00002775 sendDropWindowCommandLocked(dropWindow->getToken(), local.x, local.y);
Arthur Hung6d0571e2021-04-09 20:18:16 +08002776 } else {
Arthur Hung54745652022-04-20 07:17:41 +00002777 ALOGW("No window found when drop.");
Prabir Pradhancef936d2021-07-21 16:17:52 +00002778 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08002779 }
2780 mDragState.reset();
2781}
2782
2783void InputDispatcher::addDragEventLocked(const MotionEntry& entry) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00002784 if (!mDragState || mDragState->dragWindow->getInfo()->displayId != entry.displayId) {
arthurhungb89ccb02020-12-30 16:19:01 +08002785 return;
2786 }
2787
arthurhung6d4bed92021-03-17 11:59:33 +08002788 if (!mDragState->isStartDrag) {
2789 mDragState->isStartDrag = true;
2790 mDragState->isStylusButtonDownAtStart =
2791 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2792 }
2793
Arthur Hung54745652022-04-20 07:17:41 +00002794 // Find the pointer index by id.
2795 int32_t pointerIndex = 0;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002796 for (; static_cast<uint32_t>(pointerIndex) < entry.getPointerCount(); pointerIndex++) {
Arthur Hung54745652022-04-20 07:17:41 +00002797 const PointerProperties& pointerProperties = entry.pointerProperties[pointerIndex];
2798 if (pointerProperties.id == mDragState->pointerId) {
2799 break;
arthurhung6d4bed92021-03-17 11:59:33 +08002800 }
Arthur Hung54745652022-04-20 07:17:41 +00002801 }
arthurhung6d4bed92021-03-17 11:59:33 +08002802
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07002803 if (uint32_t(pointerIndex) == entry.getPointerCount()) {
Arthur Hung54745652022-04-20 07:17:41 +00002804 LOG_ALWAYS_FATAL("Should find a valid pointer index by id %d", mDragState->pointerId);
Arthur Hung54745652022-04-20 07:17:41 +00002805 }
2806
2807 const int32_t maskedAction = entry.action & AMOTION_EVENT_ACTION_MASK;
2808 const int32_t x = entry.pointerCoords[pointerIndex].getX();
2809 const int32_t y = entry.pointerCoords[pointerIndex].getY();
2810
2811 switch (maskedAction) {
2812 case AMOTION_EVENT_ACTION_MOVE: {
2813 // Handle the special case : stylus button no longer pressed.
2814 bool isStylusButtonDown =
2815 (entry.buttonState & AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) != 0;
2816 if (mDragState->isStylusButtonDownAtStart && !isStylusButtonDown) {
2817 finishDragAndDrop(entry.displayId, x, y);
2818 return;
2819 }
2820
2821 // Prevent stylus interceptor windows from affecting drag and drop behavior for now,
2822 // until we have an explicit reason to support it.
2823 constexpr bool isStylus = false;
2824
Siarhei Vishniakoue1ada272022-11-03 10:47:08 -07002825 sp<WindowInfoHandle> hoverWindowHandle =
2826 findTouchedWindowAtLocked(entry.displayId, x, y, isStylus,
2827 /*ignoreDragWindow=*/true);
Arthur Hung54745652022-04-20 07:17:41 +00002828 // enqueue drag exit if needed.
2829 if (hoverWindowHandle != mDragState->dragHoverWindowHandle &&
2830 !haveSameToken(hoverWindowHandle, mDragState->dragHoverWindowHandle)) {
2831 if (mDragState->dragHoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002832 enqueueDragEventLocked(mDragState->dragHoverWindowHandle, /*isExiting=*/true, x,
Arthur Hung54745652022-04-20 07:17:41 +00002833 y);
2834 }
2835 mDragState->dragHoverWindowHandle = hoverWindowHandle;
2836 }
2837 // enqueue drag location if needed.
2838 if (hoverWindowHandle != nullptr) {
Harry Cutts33476232023-01-30 19:57:29 +00002839 enqueueDragEventLocked(hoverWindowHandle, /*isExiting=*/false, x, y);
Arthur Hung54745652022-04-20 07:17:41 +00002840 }
2841 break;
2842 }
2843
2844 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07002845 if (MotionEvent::getActionIndex(entry.action) != pointerIndex) {
Arthur Hung54745652022-04-20 07:17:41 +00002846 break;
2847 }
2848 // The drag pointer is up.
2849 [[fallthrough]];
2850 case AMOTION_EVENT_ACTION_UP:
2851 finishDragAndDrop(entry.displayId, x, y);
2852 break;
2853 case AMOTION_EVENT_ACTION_CANCEL: {
2854 ALOGD("Receiving cancel when drag and drop.");
2855 sendDropWindowCommandLocked(nullptr, 0, 0);
2856 mDragState.reset();
2857 break;
2858 }
arthurhungb89ccb02020-12-30 16:19:01 +08002859 }
2860}
2861
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002862std::optional<InputTarget> InputDispatcher::createInputTargetLocked(
2863 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002864 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002865 std::optional<nsecs_t> firstDownTimeInTarget) const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002866 std::shared_ptr<Connection> connection = getConnectionLocked(windowHandle->getToken());
2867 if (connection == nullptr) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002868 ALOGW("Not creating InputTarget for %s, no input channel", windowHandle->getName().c_str());
2869 return {};
2870 }
2871 InputTarget inputTarget;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002872 inputTarget.connection = connection;
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00002873 inputTarget.windowHandle = windowHandle;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002874 inputTarget.dispatchMode = dispatchMode;
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002875 inputTarget.flags = targetFlags;
2876 inputTarget.globalScaleFactor = windowHandle->getInfo()->globalScaleFactor;
2877 inputTarget.firstDownTimeInTarget = firstDownTimeInTarget;
2878 const auto& displayInfoIt = mDisplayInfos.find(windowHandle->getInfo()->displayId);
2879 if (displayInfoIt != mDisplayInfos.end()) {
2880 inputTarget.displayTransform = displayInfoIt->second.transform;
2881 } else {
Siarhei Vishniakou580fb3a2023-05-05 15:02:20 -07002882 // DisplayInfo not found for this window on display windowHandle->getInfo()->displayId.
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002883 // TODO(b/198444055): Make this an error message after 'setInputWindows' API is removed.
2884 }
2885 return inputTarget;
2886}
2887
chaviw98318de2021-05-19 16:45:23 -05002888void InputDispatcher::addWindowTargetLocked(const sp<WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002889 InputTarget::DispatchMode dispatchMode,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08002890 ftl::Flags<InputTarget::Flags> targetFlags,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002891 std::optional<nsecs_t> firstDownTimeInTarget,
Siarhei Vishniakouf75cddb2022-10-25 10:42:16 -07002892 std::vector<InputTarget>& inputTargets) const {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002893 std::vector<InputTarget>::iterator it =
2894 std::find_if(inputTargets.begin(), inputTargets.end(),
2895 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002896 return inputTarget.connection->getToken() == windowHandle->getToken();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002897 });
Chavi Weingarten97b8eec2020-01-09 18:09:08 +00002898
chaviw98318de2021-05-19 16:45:23 -05002899 const WindowInfo* windowInfo = windowHandle->getInfo();
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002900
2901 if (it == inputTargets.end()) {
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002902 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002903 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
2904 firstDownTimeInTarget);
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002905 if (!target) {
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002906 return;
2907 }
Siarhei Vishniakou6c377b32023-05-15 17:03:39 -07002908 inputTargets.push_back(*target);
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002909 it = inputTargets.end() - 1;
2910 }
2911
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07002912 if (it->flags != targetFlags) {
2913 LOG(ERROR) << "Flags don't match! targetFlags=" << targetFlags.string() << ", it=" << *it;
2914 }
2915 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
2916 LOG(ERROR) << "Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
2917 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
2918 }
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002919}
2920
2921void InputDispatcher::addPointerWindowTargetLocked(
2922 const sp<android::gui::WindowInfoHandle>& windowHandle,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002923 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags,
2924 std::bitset<MAX_POINTER_ID + 1> pointerIds, std::optional<nsecs_t> firstDownTimeInTarget,
2925 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002926 if (pointerIds.none()) {
2927 for (const auto& target : inputTargets) {
2928 LOG(INFO) << "Target: " << target;
2929 }
2930 LOG(FATAL) << "No pointers specified for " << windowHandle->getName();
2931 return;
2932 }
2933 std::vector<InputTarget>::iterator it =
2934 std::find_if(inputTargets.begin(), inputTargets.end(),
2935 [&windowHandle](const InputTarget& inputTarget) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002936 return inputTarget.connection->getToken() == windowHandle->getToken();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002937 });
2938
2939 // This is a hack, because the actual entry could potentially be an ACTION_DOWN event that
2940 // causes a HOVER_EXIT to be generated. That means that the same entry of ACTION_DOWN would
2941 // have DISPATCH_AS_HOVER_EXIT and DISPATCH_AS_IS. And therefore, we have to create separate
2942 // input targets for hovering pointers and for touching pointers.
2943 // If we picked an existing input target above, but it's for HOVER_EXIT - let's use a new
2944 // target instead.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002945 if (it != inputTargets.end() && it->dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002946 // Force the code below to create a new input target
2947 it = inputTargets.end();
2948 }
2949
2950 const WindowInfo* windowInfo = windowHandle->getInfo();
2951
2952 if (it == inputTargets.end()) {
2953 std::optional<InputTarget> target =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002954 createInputTargetLocked(windowHandle, dispatchMode, targetFlags,
2955 firstDownTimeInTarget);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07002956 if (!target) {
2957 return;
2958 }
2959 inputTargets.push_back(*target);
2960 it = inputTargets.end() - 1;
2961 }
2962
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002963 if (it->dispatchMode != dispatchMode) {
2964 LOG(ERROR) << __func__ << ": DispatchMode doesn't match! ignoring new mode="
2965 << ftl::enum_string(dispatchMode) << ", it=" << *it;
2966 }
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07002967 if (it->flags != targetFlags) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002968 LOG(ERROR) << __func__ << ": Flags don't match! new targetFlags=" << targetFlags.string()
2969 << ", it=" << *it;
Siarhei Vishniakou4bd0b7c2023-10-27 00:51:14 -07002970 }
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07002971 if (it->globalScaleFactor != windowInfo->globalScaleFactor) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00002972 LOG(ERROR) << __func__ << ": Mismatch! it->globalScaleFactor=" << it->globalScaleFactor
Siarhei Vishniakou23d73fb2023-10-29 13:27:46 -07002973 << ", windowInfo->globalScaleFactor=" << windowInfo->globalScaleFactor;
2974 }
Chavi Weingarten65f98b82020-01-16 18:56:50 +00002975
chaviw1ff3d1e2020-07-01 15:53:47 -07002976 it->addPointers(pointerIds, windowInfo->transform);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977}
2978
Michael Wright3dd60e22019-03-27 22:06:44 +00002979void InputDispatcher::addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
Prabir Pradhan0a99c922021-09-03 08:27:53 -07002980 int32_t displayId) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002981 auto monitorsIt = mGlobalMonitorsByDisplay.find(displayId);
2982 if (monitorsIt == mGlobalMonitorsByDisplay.end()) return;
Michael Wright3dd60e22019-03-27 22:06:44 +00002983
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002984 for (const Monitor& monitor : selectResponsiveMonitorsLocked(monitorsIt->second)) {
2985 InputTarget target;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08002986 target.connection = monitor.connection;
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00002987 // target.firstDownTimeInTarget is not set for global monitors. It is only required in split
2988 // touch and global monitoring works as intended even without setting firstDownTimeInTarget
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002989 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
2990 target.displayTransform = it->second.transform;
Arthur Hung2fbf37f2018-09-13 18:16:41 +08002991 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08002992 target.setDefaultPointerTransform(target.displayTransform);
2993 inputTargets.push_back(target);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 }
2995}
2996
Robert Carrc9bf1d32020-04-13 17:21:08 -07002997/**
2998 * Indicate whether one window handle should be considered as obscuring
2999 * another window handle. We only check a few preconditions. Actually
3000 * checking the bounds is left to the caller.
3001 */
chaviw98318de2021-05-19 16:45:23 -05003002static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
3003 const sp<WindowInfoHandle>& otherHandle) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003004 // Compare by token so cloned layers aren't counted
3005 if (haveSameToken(windowHandle, otherHandle)) {
3006 return false;
3007 }
3008 auto info = windowHandle->getInfo();
3009 auto otherInfo = otherHandle->getInfo();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003010 if (otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_VISIBLE)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003011 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003012 } else if (otherInfo->alpha == 0 &&
3013 otherInfo->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE)) {
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003014 // Those act as if they were invisible, so we don't need to flag them.
3015 // We do want to potentially flag touchable windows even if they have 0
3016 // opacity, since they can consume touches and alter the effects of the
3017 // user interaction (eg. apps that rely on
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003018 // Flags::WINDOW_IS_PARTIALLY_OBSCURED should still be told about those
Bernardo Rufino653d2e02020-10-20 17:32:40 +00003019 // windows), hence we also check for FLAG_NOT_TOUCHABLE.
3020 return false;
Bernardo Rufino8007daf2020-09-22 09:40:01 +00003021 } else if (info->ownerUid == otherInfo->ownerUid) {
3022 // If ownerUid is the same we don't generate occlusion events as there
3023 // is no security boundary within an uid.
Robert Carrc9bf1d32020-04-13 17:21:08 -07003024 return false;
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003025 } else if (otherInfo->inputConfig.test(gui::WindowInfo::InputConfig::TRUSTED_OVERLAY)) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003026 return false;
3027 } else if (otherInfo->displayId != info->displayId) {
3028 return false;
3029 }
3030 return true;
3031}
3032
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003033/**
3034 * Returns touch occlusion information in the form of TouchOcclusionInfo. To check if the touch is
3035 * untrusted, one should check:
3036 *
3037 * 1. If result.hasBlockingOcclusion is true.
3038 * If it's, it means the touch should be blocked due to a window with occlusion mode of
3039 * BLOCK_UNTRUSTED.
3040 *
3041 * 2. If result.obscuringOpacity > mMaximumObscuringOpacityForTouch.
3042 * If it is (and 1 is false), then the touch should be blocked because a stack of windows
3043 * (possibly only one) with occlusion mode of USE_OPACITY from one UID resulted in a composed
3044 * obscuring opacity above the threshold. Note that if there was no window of occlusion mode
3045 * USE_OPACITY, result.obscuringOpacity would've been 0 and since
3046 * mMaximumObscuringOpacityForTouch >= 0, the condition above would never be true.
3047 *
3048 * If neither of those is true, then it means the touch can be allowed.
3049 */
3050InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
chaviw98318de2021-05-19 16:45:23 -05003051 const sp<WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const {
3052 const WindowInfo* windowInfo = windowHandle->getInfo();
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003053 int32_t displayId = windowInfo->displayId;
chaviw98318de2021-05-19 16:45:23 -05003054 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003055 TouchOcclusionInfo info;
3056 info.hasBlockingOcclusion = false;
3057 info.obscuringOpacity = 0;
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003058 info.obscuringUid = gui::Uid::INVALID;
3059 std::map<gui::Uid, float> opacityByUid;
chaviw98318de2021-05-19 16:45:23 -05003060 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003061 if (windowHandle == otherHandle) {
3062 break; // All future windows are below us. Exit early.
3063 }
chaviw98318de2021-05-19 16:45:23 -05003064 const WindowInfo* otherInfo = otherHandle->getInfo();
Bernardo Rufino1ff9d592021-01-18 16:58:57 +00003065 if (canBeObscuredBy(windowHandle, otherHandle) && otherInfo->frameContainsPoint(x, y) &&
3066 !haveSameApplicationToken(windowInfo, otherInfo)) {
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003067 if (DEBUG_TOUCH_OCCLUSION) {
3068 info.debugInfo.push_back(
Harry Cutts101ee9b2023-07-06 18:04:14 +00003069 dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003070 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003071 // canBeObscuredBy() has returned true above, which means this window is untrusted, so
3072 // we perform the checks below to see if the touch can be propagated or not based on the
3073 // window's touch occlusion mode
3074 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::BLOCK_UNTRUSTED) {
3075 info.hasBlockingOcclusion = true;
3076 info.obscuringUid = otherInfo->ownerUid;
3077 info.obscuringPackage = otherInfo->packageName;
3078 break;
3079 }
3080 if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003081 const auto uid = otherInfo->ownerUid;
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003082 float opacity =
3083 (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid];
3084 // Given windows A and B:
3085 // opacity(A, B) = 1 - [1 - opacity(A)] * [1 - opacity(B)]
3086 opacity = 1 - (1 - opacity) * (1 - otherInfo->alpha);
3087 opacityByUid[uid] = opacity;
3088 if (opacity > info.obscuringOpacity) {
3089 info.obscuringOpacity = opacity;
3090 info.obscuringUid = uid;
3091 info.obscuringPackage = otherInfo->packageName;
3092 }
3093 }
3094 }
3095 }
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003096 if (DEBUG_TOUCH_OCCLUSION) {
Harry Cutts101ee9b2023-07-06 18:04:14 +00003097 info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003098 }
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003099 return info;
3100}
3101
chaviw98318de2021-05-19 16:45:23 -05003102std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003103 bool isTouchedWindow) const {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003104 return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003105 "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3106 "], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3107 "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08003108 isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003109 info->ownerUid.toString().c_str(), info->id,
Chavi Weingarten7f019192023-08-08 20:39:01 +00003110 toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3111 info->frame.top, info->frame.right, info->frame.bottom,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003112 dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3113 info->inputConfig.string().c_str(), toString(info->token != nullptr),
3114 info->applicationInfo.name.c_str(),
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07003115 binderToString(info->applicationInfo.token).c_str());
Bernardo Rufino4bae0ac2020-10-14 18:33:46 +00003116}
3117
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003118bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3119 if (occlusionInfo.hasBlockingOcclusion) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003120 ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
3121 occlusionInfo.obscuringUid.toString().c_str());
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003122 return false;
3123 }
3124 if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) {
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003125 ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = "
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003126 "%.2f, maximum allowed = %.2f)",
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003127 occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(),
Bernardo Rufinoea97d182020-08-19 14:43:14 +01003128 occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch);
3129 return false;
3130 }
3131 return true;
3132}
3133
chaviw98318de2021-05-19 16:45:23 -05003134bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003135 int32_t x, int32_t y) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 int32_t displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003137 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3138 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003139 if (windowHandle == otherHandle) {
3140 break; // All future windows are below us. Exit early.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 }
chaviw98318de2021-05-19 16:45:23 -05003142 const WindowInfo* otherInfo = otherHandle->getInfo();
Robert Carrc9bf1d32020-04-13 17:21:08 -07003143 if (canBeObscuredBy(windowHandle, otherHandle) &&
minchelif28cc4e2020-03-19 11:18:11 +08003144 otherInfo->frameContainsPoint(x, y)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 return true;
3146 }
3147 }
3148 return false;
3149}
3150
chaviw98318de2021-05-19 16:45:23 -05003151bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const {
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003152 int32_t displayId = windowHandle->getInfo()->displayId;
chaviw98318de2021-05-19 16:45:23 -05003153 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
3154 const WindowInfo* windowInfo = windowHandle->getInfo();
3155 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
Robert Carrc9bf1d32020-04-13 17:21:08 -07003156 if (windowHandle == otherHandle) {
3157 break; // All future windows are below us. Exit early.
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003158 }
chaviw98318de2021-05-19 16:45:23 -05003159 const WindowInfo* otherInfo = otherHandle->getInfo();
Robert Carrc9bf1d32020-04-13 17:21:08 -07003160 if (canBeObscuredBy(windowHandle, otherHandle) &&
minchelif28cc4e2020-03-19 11:18:11 +08003161 otherInfo->overlaps(windowInfo)) {
Michael Wrightcdcd8f22016-03-22 16:52:13 -07003162 return true;
3163 }
3164 }
3165 return false;
3166}
3167
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08003168std::string InputDispatcher::getApplicationWindowLabel(
chaviw98318de2021-05-19 16:45:23 -05003169 const InputApplicationHandle* applicationHandle, const sp<WindowInfoHandle>& windowHandle) {
Yi Kong9b14ac62018-07-17 13:48:38 -07003170 if (applicationHandle != nullptr) {
3171 if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003172 return applicationHandle->getName() + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 } else {
3174 return applicationHandle->getName();
3175 }
Yi Kong9b14ac62018-07-17 13:48:38 -07003176 } else if (windowHandle != nullptr) {
Siarhei Vishniakou850ce122020-05-26 22:39:43 -07003177 return windowHandle->getInfo()->applicationInfo.name + " - " + windowHandle->getName();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08003179 return "<unknown application or window>";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 }
3181}
3182
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003183void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00003184 if (!isUserActivityEvent(eventEntry)) {
3185 // Not poking user activity if the event type does not represent a user activity
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003186 return;
3187 }
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003188
3189 const int32_t eventType = getUserActivityEventType(eventEntry);
3190 if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) {
3191 // Note that we're directly getting the time diff between the current event and the previous
3192 // event. This is assuming that the first user event always happens at a timestamp that is
3193 // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will
3194 // wrongly be dropped). In real life, `mMinTimeBetweenUserActivityPokes` is a much smaller
3195 // value than the potential first user activity event time, so this is ok.
3196 std::chrono::nanoseconds timeSinceLastEvent =
3197 std::chrono::nanoseconds(eventEntry.eventTime - mLastUserActivityTimes[eventType]);
3198 if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) {
3199 return;
3200 }
3201 }
3202
Tiger Huang721e26f2018-07-24 22:26:19 +08003203 int32_t displayId = getTargetDisplayId(eventEntry);
chaviw98318de2021-05-19 16:45:23 -05003204 sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Josep del Riob3981622023-04-18 15:49:45 +00003205 const WindowInfo* windowDisablingUserActivityInfo = nullptr;
Tiger Huang721e26f2018-07-24 22:26:19 +08003206 if (focusedWindowHandle != nullptr) {
chaviw98318de2021-05-19 16:45:23 -05003207 const WindowInfo* info = focusedWindowHandle->getInfo();
Prabir Pradhan51e7db02022-02-07 06:02:57 -08003208 if (info->inputConfig.test(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY)) {
Josep del Riob3981622023-04-18 15:49:45 +00003209 windowDisablingUserActivityInfo = info;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 }
3211 }
3212
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003213 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003214 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003215 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3216 if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003217 return;
3218 }
Josep del Riob3981622023-04-18 15:49:45 +00003219 if (windowDisablingUserActivityInfo != nullptr) {
3220 if (DEBUG_DISPATCH_CYCLE) {
3221 ALOGD("Not poking user activity: disabled by window '%s'.",
3222 windowDisablingUserActivityInfo->name.c_str());
3223 }
3224 return;
3225 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003226 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003228 case EventEntry::Type::KEY: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003229 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3230 if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003231 return;
3232 }
Josep del Riob3981622023-04-18 15:49:45 +00003233 // If the key code is unknown, we don't consider it user activity
3234 if (keyEntry.keyCode == AKEYCODE_UNKNOWN) {
3235 return;
3236 }
3237 // Don't inhibit events that were intercepted or are not passed to
3238 // the apps, like system shortcuts
3239 if (windowDisablingUserActivityInfo != nullptr &&
3240 keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP &&
3241 keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) {
3242 if (DEBUG_DISPATCH_CYCLE) {
3243 ALOGD("Not poking user activity: disabled by window '%s'.",
3244 windowDisablingUserActivityInfo->name.c_str());
3245 }
3246 return;
3247 }
3248
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003249 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00003251 default: {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003252 LOG_ALWAYS_FATAL("%s events are not user activity",
Dominik Laskowski75788452021-02-09 18:51:25 -08003253 ftl::enum_string(eventEntry.type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003254 break;
3255 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 }
3257
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08003258 mLastUserActivityTimes[eventType] = eventEntry.eventTime;
Prabir Pradhancef936d2021-07-21 16:17:52 +00003259 auto command = [this, eventTime = eventEntry.eventTime, eventType, displayId]()
3260 REQUIRES(mLock) {
3261 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003262 mPolicy.pokeUserActivity(eventTime, eventType, displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003263 };
3264 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265}
3266
3267void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003268 const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003269 std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003270 const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003271 ATRACE_NAME_IF(ATRACE_ENABLED(),
3272 StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
3273 connection->getInputChannelName().c_str(), eventEntry->id));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003274 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003275 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=%s, "
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08003276 "globalScaleFactor=%f, pointerIds=%s %s",
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003277 connection->getInputChannelName().c_str(), inputTarget.flags.string().c_str(),
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08003278 inputTarget.globalScaleFactor, bitsetToString(inputTarget.pointerIds).c_str(),
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003279 inputTarget.getPointerInfoString().c_str());
3280 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
3282 // Skip this event if the connection status is not normal.
3283 // We don't want to enqueue additional outbound events if the connection is broken.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003284 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003285 if (DEBUG_DISPATCH_CYCLE) {
3286 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003287 connection->getInputChannelName().c_str(),
3288 ftl::enum_string(connection->status).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003289 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 return;
3291 }
3292
3293 // Split a motion event if needed.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003294 if (inputTarget.flags.test(InputTarget::Flags::SPLIT)) {
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003295 LOG_ALWAYS_FATAL_IF(eventEntry->type != EventEntry::Type::MOTION,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003296 "Entry type %s should not have Flags::SPLIT",
Dominik Laskowski75788452021-02-09 18:51:25 -08003297 ftl::enum_string(eventEntry->type).c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003299 const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry);
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003300 if (inputTarget.pointerIds.count() != originalMotionEntry.getPointerCount()) {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -08003301 if (!inputTarget.firstDownTimeInTarget.has_value()) {
3302 logDispatchStateLocked();
3303 LOG(FATAL) << "Splitting motion events requires a down time to be set for the "
3304 "target on connection "
3305 << connection->getInputChannelName() << " for "
3306 << originalMotionEntry.getDescription();
3307 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003308 std::unique_ptr<MotionEntry> splitMotionEntry =
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00003309 splitMotionEvent(originalMotionEntry, inputTarget.pointerIds,
3310 inputTarget.firstDownTimeInTarget.value());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 if (!splitMotionEntry) {
3312 return; // split event was dropped
3313 }
Arthur Hungb3307ee2021-10-14 10:57:37 +00003314 if (splitMotionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
3315 std::string reason = std::string("reason=pointer cancel on split window");
3316 android_log_event_list(LOGTAG_INPUT_CANCEL)
3317 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
3318 }
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003319 if (DEBUG_FOCUS) {
3320 ALOGD("channel '%s' ~ Split motion event.",
3321 connection->getInputChannelName().c_str());
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07003322 logOutboundMotionDetails(" ", *splitMotionEntry);
Siarhei Vishniakou86587282019-09-09 18:20:15 +01003323 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003324 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection,
3325 std::move(splitMotionEntry),
3326 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 return;
3328 }
3329 }
3330
3331 // Not splitting. Enqueue dispatch entries for the event as is.
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003332 enqueueDispatchEntryAndStartDispatchCycleLocked(currentTime, connection, eventEntry,
3333 inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334}
3335
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003336void InputDispatcher::enqueueDispatchEntryAndStartDispatchCycleLocked(
3337 nsecs_t currentTime, const std::shared_ptr<Connection>& connection,
3338 std::shared_ptr<const EventEntry> eventEntry, const InputTarget& inputTarget) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003339 ATRACE_NAME_IF(ATRACE_ENABLED(),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003340 StringPrintf("enqueueDispatchEntryAndStartDispatchCycleLocked(inputChannel=%s, "
3341 "id=0x%" PRIx32 ")",
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003342 connection->getInputChannelName().c_str(), eventEntry->id));
Michael Wright3dd60e22019-03-27 22:06:44 +00003343
hongzuo liu95785e22022-09-06 02:51:35 +00003344 const bool wasEmpty = connection->outboundQueue.empty();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003346 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
3348 // If the outbound queue was previously empty, start the dispatch cycle going.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003349 if (wasEmpty && !connection->outboundQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 startDispatchCycleLocked(currentTime, connection);
3351 }
3352}
3353
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003354void InputDispatcher::enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection,
Prabir Pradhan24047542023-11-02 17:14:59 +00003355 std::shared_ptr<const EventEntry> eventEntry,
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003356 const InputTarget& inputTarget) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00003357 const bool isKeyOrMotion = eventEntry->type == EventEntry::Type::KEY ||
3358 eventEntry->type == EventEntry::Type::MOTION;
3359 if (isKeyOrMotion && !inputTarget.windowHandle && !connection->monitor) {
3360 LOG(FATAL) << "All InputTargets for non-monitors must be associated with a window; target: "
3361 << inputTarget << " connection: " << connection->getInputChannelName()
3362 << " entry: " << eventEntry->getDescription();
3363 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 // This is a new event.
3365 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -08003366 std::unique_ptr<DispatchEntry> dispatchEntry =
Prabir Pradhanadc59b42023-12-15 05:34:11 +00003367 createDispatchEntry(inputTarget, eventEntry, inputTarget.flags, mWindowInfosVsyncId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368
Chavi Weingarten65f98b82020-01-16 18:56:50 +00003369 // Use the eventEntry from dispatchEntry since the entry may have changed and can now be a
3370 // different EventEntry than what was passed in.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003371 eventEntry = dispatchEntry->eventEntry;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 // Apply target flags and update the connection's input state.
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003373 switch (eventEntry->type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003374 case EventEntry::Type::KEY: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003375 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry);
3376 if (!connection->inputState.trackKey(keyEntry, keyEntry.flags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003377 LOG(WARNING) << "channel " << connection->getInputChannelName()
3378 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003379 return; // skip the inconsistent event
3380 }
3381 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003384 case EventEntry::Type::MOTION: {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003385 std::shared_ptr<const MotionEntry> resolvedMotion =
3386 std::static_pointer_cast<const MotionEntry>(eventEntry);
3387 {
3388 // Determine the resolved motion entry.
3389 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry);
3390 int32_t resolvedAction = motionEntry.action;
3391 int32_t resolvedFlags = motionEntry.flags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003393 if (inputTarget.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003394 resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003395 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003396 resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003397 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::HOVER_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003398 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003399 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_EXIT) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003400 resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003401 } else if (inputTarget.dispatchMode == InputTarget::DispatchMode::SLIPPERY_ENTER) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003402 resolvedAction = AMOTION_EVENT_ACTION_DOWN;
3403 }
3404 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE &&
3405 !connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source,
3406 motionEntry.displayId)) {
3407 if (DEBUG_DISPATCH_CYCLE) {
3408 LOG(DEBUG) << "channel '" << connection->getInputChannelName().c_str()
3409 << "' ~ enqueueDispatchEntryLocked: filling in missing hover "
3410 "enter event";
3411 }
3412 resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
3413 }
3414
3415 if (resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3416 resolvedFlags |= AMOTION_EVENT_FLAG_CANCELED;
3417 }
3418 if (dispatchEntry->targetFlags.test(InputTarget::Flags::WINDOW_IS_OBSCURED)) {
3419 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
3420 }
3421 if (dispatchEntry->targetFlags.test(
3422 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED)) {
3423 resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
3424 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003425 if (dispatchEntry->targetFlags.test(InputTarget::Flags::NO_FOCUS_CHANGE)) {
3426 resolvedFlags |= AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
3427 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003428
3429 dispatchEntry->resolvedFlags = resolvedFlags;
3430 if (resolvedAction != motionEntry.action) {
Siarhei Vishniakoue9ef6bc2023-12-21 19:47:20 -08003431 std::optional<std::vector<PointerProperties>> usingProperties;
3432 std::optional<std::vector<PointerCoords>> usingCoords;
3433 if (resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT ||
3434 resolvedAction == AMOTION_EVENT_ACTION_CANCEL) {
3435 // This is a HOVER_EXIT or an ACTION_CANCEL event that was synthesized by
3436 // the dispatcher, and therefore the coordinates of this event are currently
3437 // incorrect. These events should use the coordinates of the last dispatched
3438 // ACTION_MOVE or HOVER_MOVE. We need to query InputState to get this data.
3439 const bool hovering = resolvedAction == AMOTION_EVENT_ACTION_HOVER_EXIT;
3440 std::optional<std::pair<std::vector<PointerProperties>,
3441 std::vector<PointerCoords>>>
3442 pointerInfo =
3443 connection->inputState.getPointersOfLastEvent(motionEntry,
3444 hovering);
3445 if (pointerInfo) {
3446 usingProperties = pointerInfo->first;
3447 usingCoords = pointerInfo->second;
3448 }
3449 }
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003450 // Generate a new MotionEntry with a new eventId using the resolved action and
3451 // flags.
Siarhei Vishniakoue9ef6bc2023-12-21 19:47:20 -08003452 resolvedMotion = std::make_shared<
3453 MotionEntry>(mIdGenerator.nextId(), motionEntry.injectionState,
3454 motionEntry.eventTime, motionEntry.deviceId,
3455 motionEntry.source, motionEntry.displayId,
3456 motionEntry.policyFlags, resolvedAction,
3457 motionEntry.actionButton, resolvedFlags,
3458 motionEntry.metaState, motionEntry.buttonState,
3459 motionEntry.classification, motionEntry.edgeFlags,
3460 motionEntry.xPrecision, motionEntry.yPrecision,
3461 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
3462 motionEntry.downTime,
3463 usingProperties.value_or(motionEntry.pointerProperties),
3464 usingCoords.value_or(motionEntry.pointerCoords));
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003465 if (ATRACE_ENABLED()) {
3466 std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32
3467 ") to MotionEvent(id=0x%" PRIx32 ").",
3468 motionEntry.id, resolvedMotion->id);
3469 ATRACE_NAME(message.c_str());
3470 }
3471
3472 // Set the resolved motion entry in the DispatchEntry.
3473 dispatchEntry->eventEntry = resolvedMotion;
3474 eventEntry = resolvedMotion;
3475 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003476 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003478 // Check if we need to cancel any of the ongoing gestures. We don't support multiple
3479 // devices being active at the same time in the same window, so if a new device is
3480 // active, cancel the gesture from the old device.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003481 std::unique_ptr<EventEntry> cancelEvent =
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003482 connection->inputState.cancelConflictingInputStream(*resolvedMotion);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003483 if (cancelEvent != nullptr) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003484 LOG(INFO) << "Canceling pointers for device " << resolvedMotion->deviceId << " in "
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003485 << connection->getInputChannelName() << " with event "
3486 << cancelEvent->getDescription();
3487 std::unique_ptr<DispatchEntry> cancelDispatchEntry =
3488 createDispatchEntry(inputTarget, std::move(cancelEvent),
Prabir Pradhanadc59b42023-12-15 05:34:11 +00003489 ftl::Flags<InputTarget::Flags>(), mWindowInfosVsyncId);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07003490
3491 // Send these cancel events to the queue before sending the event from the new
3492 // device.
3493 connection->outboundQueue.emplace_back(std::move(cancelDispatchEntry));
3494 }
3495
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003496 if (!connection->inputState.trackMotion(*resolvedMotion,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003497 dispatchEntry->resolvedFlags)) {
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -07003498 LOG(WARNING) << "channel " << connection->getInputChannelName()
3499 << "~ dropping inconsistent event: " << *dispatchEntry;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003500 return; // skip the inconsistent event
3501 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00003502 if ((dispatchEntry->resolvedFlags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) &&
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003503 (resolvedMotion->policyFlags & POLICY_FLAG_TRUSTED)) {
Prabir Pradhan47cf0a02021-03-11 20:30:57 -08003504 // Skip reporting pointer down outside focus to the policy.
3505 break;
3506 }
3507
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003508 dispatchPointerDownOutsideFocus(resolvedMotion->source, resolvedMotion->action,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003509 inputTarget.connection->getToken());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003510
3511 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003513 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003514 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08003515 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
3516 case EventEntry::Type::DRAG: {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003517 break;
3518 }
Chris Yef59a2f42020-10-16 12:55:26 -07003519 case EventEntry::Type::SENSOR: {
3520 LOG_ALWAYS_FATAL("SENSOR events should not go to apps via input channel");
3521 break;
3522 }
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003523 case EventEntry::Type::CONFIGURATION_CHANGED:
3524 case EventEntry::Type::DEVICE_RESET: {
3525 LOG_ALWAYS_FATAL("%s events should not go to apps",
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003526 ftl::enum_string(eventEntry->type).c_str());
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003527 break;
3528 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 }
3530
3531 // Remember that we are waiting for this dispatch to complete.
3532 if (dispatchEntry->hasForegroundTarget()) {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003533 incrementPendingForegroundDispatches(*eventEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 }
3535
3536 // Enqueue the dispatch entry.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003537 connection->outboundQueue.emplace_back(std::move(dispatchEntry));
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003538 traceOutboundQueueLength(*connection);
chaviw8c9cf542019-03-25 13:02:48 -07003539}
3540
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003541/**
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003542 * This function is for debugging and metrics collection. It has two roles.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003543 *
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003544 * The first role is to log input interaction with windows, which helps determine what the user was
3545 * interacting with. For example, if user is touching launcher, we will see an input_interaction log
3546 * that user started interacting with launcher window, as well as any other window that received
3547 * that gesture, such as the wallpaper or other spy windows. A new input_interaction is only logged
3548 * when the set of tokens that received the event changes. It is not logged again as long as the
3549 * user is interacting with the same windows.
3550 *
3551 * The second role is to track input device activity for metrics collection. For each input event,
3552 * we report the set of UIDs that the input device interacted with to the policy. Unlike for the
3553 * input_interaction logs, the device interaction is reported even when the set of interaction
3554 * tokens do not change.
3555 *
3556 * For these purposes, we do not count ACTION_OUTSIDE, ACTION_UP and ACTION_CANCEL actions as
3557 * interaction. This includes up and cancel events for both keys and motions.
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003558 */
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003559void InputDispatcher::processInteractionsLocked(const EventEntry& entry,
3560 const std::vector<InputTarget>& targets) {
3561 int32_t deviceId;
3562 nsecs_t eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003563 // Skip ACTION_UP events, and all events other than keys and motions
3564 if (entry.type == EventEntry::Type::KEY) {
3565 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry);
3566 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
3567 return;
3568 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003569 deviceId = keyEntry.deviceId;
3570 eventTime = keyEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003571 } else if (entry.type == EventEntry::Type::MOTION) {
3572 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry);
3573 if (motionEntry.action == AMOTION_EVENT_ACTION_UP ||
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003574 motionEntry.action == AMOTION_EVENT_ACTION_CANCEL ||
3575 MotionEvent::getActionMasked(motionEntry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003576 return;
3577 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003578 deviceId = motionEntry.deviceId;
3579 eventTime = motionEntry.eventTime;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003580 } else {
3581 return; // Not a key or a motion
3582 }
3583
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00003584 std::set<gui::Uid> interactionUids;
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07003585 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003586 std::vector<std::shared_ptr<Connection>> newConnections;
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003587 for (const InputTarget& target : targets) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00003588 if (target.dispatchMode == InputTarget::DispatchMode::OUTSIDE) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003589 continue; // Skip windows that receive ACTION_OUTSIDE
3590 }
3591
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003592 sp<IBinder> token = target.connection->getToken();
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003593 newConnectionTokens.insert(std::move(token));
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003594 newConnections.emplace_back(target.connection);
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003595 if (target.windowHandle) {
3596 interactionUids.emplace(target.windowHandle->getInfo()->ownerUid);
3597 }
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003598 }
Prabir Pradhan8ede1d12023-05-08 19:37:44 +00003599
3600 auto command = [this, deviceId, eventTime, uids = std::move(interactionUids)]()
3601 REQUIRES(mLock) {
3602 scoped_unlock unlock(mLock);
3603 mPolicy.notifyDeviceInteraction(deviceId, eventTime, uids);
3604 };
3605 postCommandLocked(std::move(command));
3606
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003607 if (newConnectionTokens == mInteractionConnectionTokens) {
3608 return; // no change
3609 }
3610 mInteractionConnectionTokens = newConnectionTokens;
3611
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003612 std::string targetList;
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003613 for (const std::shared_ptr<Connection>& connection : newConnections) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08003614 targetList += connection->getInputChannelName() + ", ";
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003615 }
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00003616 std::string message = "Interaction with: " + targetList;
3617 if (targetList.empty()) {
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +00003618 message += "<none>";
3619 }
3620 android_log_event_list(LOGTAG_INPUT_INTERACTION) << message << LOG_ID_EVENTS;
3621}
3622
chaviwfd6d3512019-03-25 13:23:49 -07003623void InputDispatcher::dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Vishnu Nairad321cd2020-08-20 16:40:21 -07003624 const sp<IBinder>& token) {
chaviw8c9cf542019-03-25 13:02:48 -07003625 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
chaviwfd6d3512019-03-25 13:23:49 -07003626 uint32_t maskedSource = source & AINPUT_SOURCE_CLASS_MASK;
3627 if (maskedSource != AINPUT_SOURCE_CLASS_POINTER || maskedAction != AMOTION_EVENT_ACTION_DOWN) {
chaviw8c9cf542019-03-25 13:02:48 -07003628 return;
3629 }
3630
Vishnu Nairc519ff72021-01-21 08:23:08 -08003631 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07003632 if (focusedToken == token) {
3633 // ignore since token is focused
chaviw8c9cf542019-03-25 13:02:48 -07003634 return;
3635 }
3636
Prabir Pradhancef936d2021-07-21 16:17:52 +00003637 auto command = [this, token]() REQUIRES(mLock) {
3638 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00003639 mPolicy.onPointerDownOutsideFocus(token);
Prabir Pradhancef936d2021-07-21 16:17:52 +00003640 };
3641 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642}
3643
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003644status_t InputDispatcher::publishMotionEvent(Connection& connection,
3645 DispatchEntry& dispatchEntry) const {
3646 const EventEntry& eventEntry = *(dispatchEntry.eventEntry);
3647 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
3648
3649 PointerCoords scaledCoords[MAX_POINTERS];
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003650 const PointerCoords* usingCoords = motionEntry.pointerCoords.data();
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003651
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003652 // TODO(b/316355518): Do not modify coords before dispatch.
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003653 // Set the X and Y offset and X and Y scale depending on the input source.
3654 if ((motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) &&
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003655 !(dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS))) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003656 float globalScaleFactor = dispatchEntry.globalScaleFactor;
3657 if (globalScaleFactor != 1.0f) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003658 for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003659 scaledCoords[i] = motionEntry.pointerCoords[i];
3660 // Don't apply window scale here since we don't want scale to affect raw
3661 // coordinates. The scale will be sent back to the client and applied
3662 // later when requesting relative coordinates.
Harry Cutts33476232023-01-30 19:57:29 +00003663 scaledCoords[i].scale(globalScaleFactor, /*windowXScale=*/1, /*windowYScale=*/1);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003664 }
3665 usingCoords = scaledCoords;
3666 }
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08003667 } else if (dispatchEntry.targetFlags.test(InputTarget::Flags::ZERO_COORDS)) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003668 // We don't want the dispatch target to know the coordinates
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07003669 for (uint32_t i = 0; i < motionEntry.getPointerCount(); i++) {
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003670 scaledCoords[i].clear();
3671 }
3672 usingCoords = scaledCoords;
3673 }
3674
3675 std::array<uint8_t, 32> hmac = getSignature(motionEntry, dispatchEntry);
3676
3677 // Publish the motion event.
3678 return connection.inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003679 .publishMotionEvent(dispatchEntry.seq, motionEntry.id, motionEntry.deviceId,
3680 motionEntry.source, motionEntry.displayId, std::move(hmac),
3681 motionEntry.action, motionEntry.actionButton,
3682 dispatchEntry.resolvedFlags, motionEntry.edgeFlags,
3683 motionEntry.metaState, motionEntry.buttonState,
3684 motionEntry.classification, dispatchEntry.transform,
3685 motionEntry.xPrecision, motionEntry.yPrecision,
3686 motionEntry.xCursorPosition, motionEntry.yCursorPosition,
3687 dispatchEntry.rawTransform, motionEntry.downTime,
3688 motionEntry.eventTime, motionEntry.getPointerCount(),
3689 motionEntry.pointerProperties.data(), usingCoords);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003690}
3691
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003693 const std::shared_ptr<Connection>& connection) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00003694 ATRACE_NAME_IF(ATRACE_ENABLED(),
3695 StringPrintf("startDispatchCycleLocked(inputChannel=%s)",
3696 connection->getInputChannelName().c_str()));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003697 if (DEBUG_DISPATCH_CYCLE) {
3698 ALOGD("channel '%s' ~ startDispatchCycle", connection->getInputChannelName().c_str());
3699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003701 while (connection->status == Connection::Status::NORMAL && !connection->outboundQueue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003702 std::unique_ptr<DispatchEntry>& dispatchEntry = connection->outboundQueue.front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703 dispatchEntry->deliveryTime = currentTime;
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08003704 const std::chrono::nanoseconds timeout = getDispatchingTimeoutLocked(connection);
Siarhei Vishniakou70622952020-07-30 11:17:23 -05003705 dispatchEntry->timeoutTime = currentTime + timeout.count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
3707 // Publish the event.
3708 status_t status;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003709 const EventEntry& eventEntry = *(dispatchEntry->eventEntry);
3710 switch (eventEntry.type) {
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003711 case EventEntry::Type::KEY: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003712 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry);
3713 std::array<uint8_t, 32> hmac = getSignature(keyEntry, *dispatchEntry);
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003714 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003715 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3716 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003717 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003719 // Publish the key event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003720 status = connection->inputPublisher
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003721 .publishKeyEvent(dispatchEntry->seq, keyEntry.id,
3722 keyEntry.deviceId, keyEntry.source,
3723 keyEntry.displayId, std::move(hmac),
3724 keyEntry.action, dispatchEntry->resolvedFlags,
3725 keyEntry.keyCode, keyEntry.scanCode,
3726 keyEntry.metaState, keyEntry.repeatCount,
3727 keyEntry.downTime, keyEntry.eventTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003728 if (mTracer) {
3729 mTracer->traceEventDispatch(*dispatchEntry, keyEntry.traceTracker.get());
3730 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003731 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 }
3733
Siarhei Vishniakou49483272019-10-22 13:13:47 -07003734 case EventEntry::Type::MOTION: {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003735 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003736 LOG(INFO) << "Publishing " << *dispatchEntry << " to "
3737 << connection->getInputChannelName();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08003738 }
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003739 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry);
Siarhei Vishniakoucce7e112022-10-25 13:31:17 -07003740 status = publishMotionEvent(*connection, *dispatchEntry);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00003741 if (mTracer) {
3742 mTracer->traceEventDispatch(*dispatchEntry, motionEntry.traceTracker.get());
3743 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003744 break;
3745 }
Prabir Pradhan99987712020-11-10 18:43:05 -08003746
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003747 case EventEntry::Type::FOCUS: {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003748 const FocusEntry& focusEntry = static_cast<const FocusEntry&>(eventEntry);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003749 status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003750 focusEntry.id,
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07003751 focusEntry.hasFocus);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +01003752 break;
3753 }
3754
Antonio Kantek7242d8b2021-08-05 16:07:20 -07003755 case EventEntry::Type::TOUCH_MODE_CHANGED: {
3756 const TouchModeEntry& touchModeEntry =
3757 static_cast<const TouchModeEntry&>(eventEntry);
3758 status = connection->inputPublisher
3759 .publishTouchModeEvent(dispatchEntry->seq, touchModeEntry.id,
3760 touchModeEntry.inTouchMode);
3761
3762 break;
3763 }
3764
Prabir Pradhan99987712020-11-10 18:43:05 -08003765 case EventEntry::Type::POINTER_CAPTURE_CHANGED: {
3766 const auto& captureEntry =
3767 static_cast<const PointerCaptureChangedEntry&>(eventEntry);
3768 status = connection->inputPublisher
3769 .publishCaptureEvent(dispatchEntry->seq, captureEntry.id,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00003770 captureEntry.pointerCaptureRequest.enable);
Prabir Pradhan99987712020-11-10 18:43:05 -08003771 break;
3772 }
3773
arthurhungb89ccb02020-12-30 16:19:01 +08003774 case EventEntry::Type::DRAG: {
3775 const DragEntry& dragEntry = static_cast<const DragEntry&>(eventEntry);
3776 status = connection->inputPublisher.publishDragEvent(dispatchEntry->seq,
3777 dragEntry.id, dragEntry.x,
3778 dragEntry.y,
3779 dragEntry.isExiting);
3780 break;
3781 }
3782
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003783 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07003784 case EventEntry::Type::DEVICE_RESET:
3785 case EventEntry::Type::SENSOR: {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003786 LOG_ALWAYS_FATAL("Should never start dispatch cycles for %s events",
Dominik Laskowski75788452021-02-09 18:51:25 -08003787 ftl::enum_string(eventEntry.type).c_str());
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003788 return;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08003789 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790 }
3791
3792 // Check the result.
3793 if (status) {
3794 if (status == WOULD_BLOCK) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003795 if (connection->waitQueue.empty()) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ALOGE("channel '%s' ~ Could not publish event because the pipe is full. "
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003797 "This is unexpected because the wait queue is empty, so the pipe "
3798 "should be empty and we shouldn't have any problems writing an "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003799 "event to it, status=%s(%d)",
3800 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3801 status);
Harry Cutts33476232023-01-30 19:57:29 +00003802 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 } else {
3804 // Pipe is full and we are waiting for the app to finish process some events
3805 // before sending more events to it.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003806 if (DEBUG_DISPATCH_CYCLE) {
3807 ALOGD("channel '%s' ~ Could not publish event because the pipe is full, "
3808 "waiting for the application to catch up",
3809 connection->getInputChannelName().c_str());
3810 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 }
3812 } else {
3813 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, "
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +00003814 "status=%s(%d)",
3815 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3816 status);
Harry Cutts33476232023-01-30 19:57:29 +00003817 abortBrokenDispatchCycleLocked(currentTime, connection, /*notify=*/true);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 }
3819 return;
3820 }
3821
3822 // Re-enqueue the event on the wait queue.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003823 const nsecs_t timeoutTime = dispatchEntry->timeoutTime;
3824 connection->waitQueue.emplace_back(std::move(dispatchEntry));
3825 connection->outboundQueue.erase(connection->outboundQueue.begin());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003826 traceOutboundQueueLength(*connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003827 if (connection->responsive) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003828 mAnrTracker.insert(timeoutTime, connection->getToken());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07003829 }
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003830 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 }
3832}
3833
chaviw09c8d2d2020-08-24 15:48:26 -07003834std::array<uint8_t, 32> InputDispatcher::sign(const VerifiedInputEvent& event) const {
3835 size_t size;
3836 switch (event.type) {
3837 case VerifiedInputEvent::Type::KEY: {
3838 size = sizeof(VerifiedKeyEvent);
3839 break;
3840 }
3841 case VerifiedInputEvent::Type::MOTION: {
3842 size = sizeof(VerifiedMotionEvent);
3843 break;
3844 }
3845 }
3846 const uint8_t* start = reinterpret_cast<const uint8_t*>(&event);
3847 return mHmacKeyManager.sign(start, size);
3848}
3849
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003850const std::array<uint8_t, 32> InputDispatcher::getSignature(
3851 const MotionEntry& motionEntry, const DispatchEntry& dispatchEntry) const {
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +00003852 const int32_t actionMasked = MotionEvent::getActionMasked(motionEntry.action);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003853 if (actionMasked != AMOTION_EVENT_ACTION_UP && actionMasked != AMOTION_EVENT_ACTION_DOWN) {
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003854 // Only sign events up and down events as the purely move events
3855 // are tied to their up/down counterparts so signing would be redundant.
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003856 return INVALID_HMAC;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003857 }
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07003858
3859 VerifiedMotionEvent verifiedEvent =
3860 verifiedMotionEventFromMotionEntry(motionEntry, dispatchEntry.rawTransform);
3861 verifiedEvent.actionMasked = actionMasked;
3862 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_MOTION_EVENT_FLAGS;
3863 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003864}
3865
3866const std::array<uint8_t, 32> InputDispatcher::getSignature(
3867 const KeyEntry& keyEntry, const DispatchEntry& dispatchEntry) const {
3868 VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEntry(keyEntry);
3869 verifiedEvent.flags = dispatchEntry.resolvedFlags & VERIFIED_KEY_EVENT_FLAGS;
chaviw09c8d2d2020-08-24 15:48:26 -07003870 return sign(verifiedEvent);
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -07003871}
3872
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003874 const std::shared_ptr<Connection>& connection,
3875 uint32_t seq, bool handled, nsecs_t consumeTime) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003876 if (DEBUG_DISPATCH_CYCLE) {
3877 ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s",
3878 connection->getInputChannelName().c_str(), seq, toString(handled));
3879 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00003881 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 return;
3883 }
3884
3885 // Notify other system components and prepare to start the next dispatch cycle.
Prabir Pradhancef936d2021-07-21 16:17:52 +00003886 auto command = [this, currentTime, connection, seq, handled, consumeTime]() REQUIRES(mLock) {
3887 doDispatchCycleFinishedCommand(currentTime, connection, seq, handled, consumeTime);
3888 };
3889 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890}
3891
3892void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003893 const std::shared_ptr<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07003894 bool notify) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003895 if (DEBUG_DISPATCH_CYCLE) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07003896 LOG(INFO) << "channel '" << connection->getInputChannelName() << "'~ " << __func__
3897 << " - notify=" << toString(notify);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00003898 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899
3900 // Clear the dispatch queues.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003901 drainDispatchQueue(connection->outboundQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003902 traceOutboundQueueLength(*connection);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003903 drainDispatchQueue(connection->waitQueue);
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00003904 traceWaitQueueLength(*connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906 // The connection appears to be unrecoverably broken.
3907 // Ignore already broken or zombie connections.
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08003908 if (connection->status == Connection::Status::NORMAL) {
3909 connection->status = Connection::Status::BROKEN;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910
3911 if (notify) {
3912 // Notify other system components.
Prabir Pradhancef936d2021-07-21 16:17:52 +00003913 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
3914 connection->getInputChannelName().c_str());
3915
3916 auto command = [this, connection]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00003917 scoped_unlock unlock(mLock);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003918 mPolicy.notifyInputChannelBroken(connection->getToken());
Prabir Pradhancef936d2021-07-21 16:17:52 +00003919 };
3920 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921 }
3922 }
3923}
3924
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003925void InputDispatcher::drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue) {
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003926 while (!queue.empty()) {
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003927 releaseDispatchEntry(std::move(queue.front()));
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07003928 queue.pop_front();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 }
3930}
3931
Prabir Pradhan8c90d782023-09-15 21:16:44 +00003932void InputDispatcher::releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933 if (dispatchEntry->hasForegroundTarget()) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07003934 decrementPendingForegroundDispatches(*(dispatchEntry->eventEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936}
3937
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003938int InputDispatcher::handleReceiveCallback(int events, sp<IBinder> connectionToken) {
3939 std::scoped_lock _l(mLock);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07003940 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003941 if (connection == nullptr) {
3942 ALOGW("Received looper callback for unknown input channel token %p. events=0x%x",
3943 connectionToken.get(), events);
3944 return 0; // remove the callback
3945 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003947 bool notify;
3948 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
3949 if (!(events & ALOOPER_EVENT_INPUT)) {
3950 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
3951 "events=0x%x",
3952 connection->getInputChannelName().c_str(), events);
3953 return 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 }
3955
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003956 nsecs_t currentTime = now();
3957 bool gotOne = false;
3958 status_t status = OK;
3959 for (;;) {
3960 Result<InputPublisher::ConsumerResponse> result =
3961 connection->inputPublisher.receiveConsumerResponse();
3962 if (!result.ok()) {
3963 status = result.error().code();
3964 break;
3965 }
3966
3967 if (std::holds_alternative<InputPublisher::Finished>(*result)) {
3968 const InputPublisher::Finished& finish =
3969 std::get<InputPublisher::Finished>(*result);
3970 finishDispatchCycleLocked(currentTime, connection, finish.seq, finish.handled,
3971 finish.consumeTime);
3972 } else if (std::holds_alternative<InputPublisher::Timeline>(*result)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00003973 if (shouldReportMetricsForConnection(*connection)) {
3974 const InputPublisher::Timeline& timeline =
3975 std::get<InputPublisher::Timeline>(*result);
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003976 mLatencyTracker.trackGraphicsLatency(timeline.inputEventId,
3977 connection->getToken(),
3978 std::move(timeline.graphicsTimeline));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00003979 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003980 }
3981 gotOne = true;
3982 }
3983 if (gotOne) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00003984 runCommandsLockedInterruptable();
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003985 if (status == WOULD_BLOCK) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 return 1;
3987 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 }
3989
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00003990 notify = status != DEAD_OBJECT || !connection->monitor;
3991 if (notify) {
3992 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%s(%d)",
3993 connection->getInputChannelName().c_str(), statusToString(status).c_str(),
3994 status);
3995 }
3996 } else {
3997 // Monitor channels are never explicitly unregistered.
3998 // We do it automatically when the remote endpoint is closed so don't warn about them.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08003999 const bool stillHaveWindowHandle = getWindowHandleLocked(connection->getToken()) != nullptr;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004000 notify = !connection->monitor && stillHaveWindowHandle;
4001 if (notify) {
4002 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x",
4003 connection->getInputChannelName().c_str(), events);
4004 }
4005 }
4006
4007 // Remove the channel.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004008 removeInputChannelLocked(connection->getToken(), notify);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00004009 return 0; // remove the callback
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010}
4011
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004012void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 const CancelationOptions& options) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004014 // Cancel windows (i.e. non-monitors).
4015 // A channel must have at least one window to receive any input. If a window was removed, the
4016 // event streams directed to the window will already have been canceled during window removal.
4017 // So there is no need to generate cancellations for connections without any windows.
4018 const auto [cancelPointers, cancelNonPointers] = expandCancellationMode(options.mode);
4019 // Generate cancellations for touched windows first. This is to avoid generating cancellations
4020 // through a non-touched window if there are more than one window for an input channel.
4021 if (cancelPointers) {
4022 for (const auto& [displayId, touchState] : mTouchStatesByDisplay) {
4023 if (options.displayId.has_value() && options.displayId != displayId) {
4024 continue;
4025 }
4026 for (const auto& touchedWindow : touchState.windows) {
4027 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
4028 }
4029 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 }
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004031 // Follow up by generating cancellations for all windows, because we don't explicitly track
4032 // the windows that have an ongoing focus event stream.
4033 if (cancelNonPointers) {
4034 for (const auto& [_, handles] : mWindowHandlesByDisplay) {
4035 for (const auto& windowHandle : handles) {
4036 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4037 }
4038 }
4039 }
4040
4041 // Cancel monitors.
4042 synthesizeCancelationEventsForMonitorsLocked(options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004045void InputDispatcher::synthesizeCancelationEventsForMonitorsLocked(
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004046 const CancelationOptions& options) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08004047 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00004048 for (const Monitor& monitor : monitors) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004049 synthesizeCancelationEventsForConnectionLocked(monitor.connection, options,
4050 /*window=*/nullptr);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08004051 }
Michael Wrightfa13dcf2015-06-12 13:25:11 +01004052 }
4053}
4054
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004055void InputDispatcher::synthesizeCancelationEventsForWindowLocked(
4056 const sp<WindowInfoHandle>& windowHandle, const CancelationOptions& options,
4057 const std::shared_ptr<Connection>& connection) {
4058 if (windowHandle == nullptr) {
4059 LOG(FATAL) << __func__ << ": Window handle must not be null";
4060 }
4061 if (connection) {
4062 // The connection can be optionally provided to avoid multiple lookups.
4063 if (windowHandle->getToken() != connection->getToken()) {
4064 LOG(FATAL) << __func__
4065 << ": Wrong connection provided for window: " << windowHandle->getName();
4066 }
4067 }
4068
4069 std::shared_ptr<Connection> resolvedConnection =
4070 connection ? connection : getConnectionLocked(windowHandle->getToken());
4071 if (!resolvedConnection) {
4072 LOG(DEBUG) << __func__ << "No connection found for window: " << windowHandle->getName();
4073 return;
4074 }
4075 synthesizeCancelationEventsForConnectionLocked(resolvedConnection, options, windowHandle);
4076}
4077
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004079 const std::shared_ptr<Connection>& connection, const CancelationOptions& options,
4080 const sp<WindowInfoHandle>& window) {
4081 if (!connection->monitor && window == nullptr) {
4082 LOG(FATAL) << __func__
4083 << ": Cannot send event to non-monitor channel without a window - channel: "
4084 << connection->getInputChannelName();
4085 }
Prabir Pradhanb13da8f2024-01-09 23:10:13 +00004086 if (connection->status != Connection::Status::NORMAL) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 return;
4088 }
4089
4090 nsecs_t currentTime = now();
4091
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004092 std::vector<std::unique_ptr<EventEntry>> cancelationEvents =
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -07004093 connection->inputState.synthesizeCancelationEvents(currentTime, options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004095 if (cancelationEvents.empty()) {
4096 return;
4097 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004098
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004099 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
4100 ALOGD("channel '%s' ~ Synthesized %zu cancelation events to bring channel back in sync "
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004101 "with reality: %s, mode=%s.",
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004102 connection->getInputChannelName().c_str(), cancelationEvents.size(), options.reason,
Siarhei Vishniakou2e3e4432023-02-09 18:34:11 -08004103 ftl::enum_string(options.mode).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004104 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004105
Arthur Hungb3307ee2021-10-14 10:57:37 +00004106 std::string reason = std::string("reason=").append(options.reason);
4107 android_log_event_list(LOGTAG_INPUT_CANCEL)
4108 << connection->getInputChannelName().c_str() << reason << LOG_ID_EVENTS;
4109
hongzuo liu95785e22022-09-06 02:51:35 +00004110 const bool wasEmpty = connection->outboundQueue.empty();
Prabir Pradhan16463382023-10-12 23:03:19 +00004111 // The target to use if we don't find a window associated with the channel.
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004112 const InputTarget fallbackTarget{.connection = connection};
4113 const auto& token = connection->getToken();
hongzuo liu95785e22022-09-06 02:51:35 +00004114
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004115 for (size_t i = 0; i < cancelationEvents.size(); i++) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004116 std::unique_ptr<EventEntry> cancelationEventEntry = std::move(cancelationEvents[i]);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004117 std::vector<InputTarget> targets{};
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004118
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004119 switch (cancelationEventEntry->type) {
4120 case EventEntry::Type::KEY: {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004121 const auto& keyEntry = static_cast<const KeyEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004122 if (window) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004123 addWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4124 /*targetFlags=*/{}, keyEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004125 } else {
4126 targets.emplace_back(fallbackTarget);
4127 }
4128 logOutboundKeyDetails("cancel - ", keyEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004129 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004131 case EventEntry::Type::MOTION: {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004132 const auto& motionEntry = static_cast<const MotionEntry&>(*cancelationEventEntry);
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00004133 if (window) {
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004134 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004135 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004136 pointerIndex++) {
4137 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4138 }
Vaibhav Devmurari110ba322023-11-17 10:47:16 +00004139 if (mDragState && mDragState->dragWindow->getToken() == token &&
4140 pointerIds.test(mDragState->pointerId)) {
4141 LOG(INFO) << __func__
4142 << ": Canceling drag and drop because the pointers for the drag "
4143 "window are being canceled.";
4144 sendDropWindowCommandLocked(nullptr, /*x=*/0, /*y=*/0);
4145 mDragState.reset();
4146 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004147 addPointerWindowTargetLocked(window, InputTarget::DispatchMode::AS_IS,
4148 ftl::Flags<InputTarget::Flags>(), pointerIds,
4149 motionEntry.downTime, targets);
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004150 } else {
4151 targets.emplace_back(fallbackTarget);
4152 const auto it = mDisplayInfos.find(motionEntry.displayId);
4153 if (it != mDisplayInfos.end()) {
4154 targets.back().displayTransform = it->second.transform;
4155 targets.back().setDefaultPointerTransform(it->second.transform);
4156 }
4157 }
4158 logOutboundMotionDetails("cancel - ", motionEntry);
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004159 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 }
Prabir Pradhan99987712020-11-10 18:43:05 -08004161 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004162 case EventEntry::Type::TOUCH_MODE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004163 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
4164 case EventEntry::Type::DRAG: {
Prabir Pradhan99987712020-11-10 18:43:05 -08004165 LOG_ALWAYS_FATAL("Canceling %s events is not supported",
Dominik Laskowski75788452021-02-09 18:51:25 -08004166 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004167 break;
4168 }
4169 case EventEntry::Type::CONFIGURATION_CHANGED:
Chris Yef59a2f42020-10-16 12:55:26 -07004170 case EventEntry::Type::DEVICE_RESET:
4171 case EventEntry::Type::SENSOR: {
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004172 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004173 ftl::enum_string(cancelationEventEntry->type).c_str());
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004174 break;
4175 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 }
4177
Prabir Pradhan112b1ad2023-09-21 09:53:53 +00004178 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004179 enqueueDispatchEntryLocked(connection, std::move(cancelationEventEntry), targets[0]);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 }
Siarhei Vishniakoubd118892020-01-10 14:08:28 -08004181
hongzuo liu95785e22022-09-06 02:51:35 +00004182 // If the outbound queue was previously empty, start the dispatch cycle going.
4183 if (wasEmpty && !connection->outboundQueue.empty()) {
4184 startDispatchCycleLocked(currentTime, connection);
4185 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186}
4187
Svet Ganov5d3bc372020-01-26 23:11:07 -08004188void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07004189 const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
Arthur Hungc539dbb2022-12-08 07:45:36 +00004190 ftl::Flags<InputTarget::Flags> targetFlags) {
Prabir Pradhan98ca4a22024-01-09 23:51:50 +00004191 if (connection->status != Connection::Status::NORMAL) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004192 return;
4193 }
4194
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004195 std::vector<std::unique_ptr<EventEntry>> downEvents =
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004196 connection->inputState.synthesizePointerDownEvents(downTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004197
4198 if (downEvents.empty()) {
4199 return;
4200 }
4201
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004202 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004203 ALOGD("channel '%s' ~ Synthesized %zu down events to ensure consistent event stream.",
4204 connection->getInputChannelName().c_str(), downEvents.size());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004205 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004206
Prabir Pradhanfeca0572024-02-06 00:43:11 +00004207 const auto [_, touchedWindowState, displayId] =
4208 findTouchStateWindowAndDisplayLocked(connection->getToken());
4209 if (touchedWindowState == nullptr) {
4210 LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
4211 }
4212 const auto& windowHandle = touchedWindowState->windowHandle;
Svet Ganov5d3bc372020-01-26 23:11:07 -08004213
hongzuo liu95785e22022-09-06 02:51:35 +00004214 const bool wasEmpty = connection->outboundQueue.empty();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004215 for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004216 std::vector<InputTarget> targets{};
Svet Ganov5d3bc372020-01-26 23:11:07 -08004217 switch (downEventEntry->type) {
4218 case EventEntry::Type::MOTION: {
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004219 const auto& motionEntry = static_cast<const MotionEntry&>(*downEventEntry);
4220 if (windowHandle != nullptr) {
4221 std::bitset<MAX_POINTER_ID + 1> pointerIds;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004222 for (uint32_t pointerIndex = 0; pointerIndex < motionEntry.getPointerCount();
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004223 pointerIndex++) {
4224 pointerIds.set(motionEntry.pointerProperties[pointerIndex].id);
4225 }
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004226 addPointerWindowTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
4227 targetFlags, pointerIds, motionEntry.downTime,
4228 targets);
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004229 } else {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08004230 targets.emplace_back(
4231 InputTarget{.connection = connection, .flags = targetFlags});
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004232 const auto it = mDisplayInfos.find(motionEntry.displayId);
4233 if (it != mDisplayInfos.end()) {
4234 targets.back().displayTransform = it->second.transform;
4235 targets.back().setDefaultPointerTransform(it->second.transform);
4236 }
4237 }
4238 logOutboundMotionDetails("down - ", motionEntry);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004239 break;
4240 }
4241
4242 case EventEntry::Type::KEY:
4243 case EventEntry::Type::FOCUS:
Antonio Kantek7242d8b2021-08-05 16:07:20 -07004244 case EventEntry::Type::TOUCH_MODE_CHANGED:
Svet Ganov5d3bc372020-01-26 23:11:07 -08004245 case EventEntry::Type::CONFIGURATION_CHANGED:
Prabir Pradhan99987712020-11-10 18:43:05 -08004246 case EventEntry::Type::DEVICE_RESET:
Chris Yef59a2f42020-10-16 12:55:26 -07004247 case EventEntry::Type::POINTER_CAPTURE_CHANGED:
arthurhungb89ccb02020-12-30 16:19:01 +08004248 case EventEntry::Type::SENSOR:
4249 case EventEntry::Type::DRAG: {
Svet Ganov5d3bc372020-01-26 23:11:07 -08004250 LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue",
Dominik Laskowski75788452021-02-09 18:51:25 -08004251 ftl::enum_string(downEventEntry->type).c_str());
Svet Ganov5d3bc372020-01-26 23:11:07 -08004252 break;
4253 }
4254 }
4255
Prabir Pradhan1c29a092023-09-21 10:29:29 +00004256 if (targets.size() != 1) LOG(FATAL) << __func__ << ": InputTarget not created";
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00004257 enqueueDispatchEntryLocked(connection, std::move(downEventEntry), targets[0]);
Svet Ganov5d3bc372020-01-26 23:11:07 -08004258 }
4259
hongzuo liu95785e22022-09-06 02:51:35 +00004260 // If the outbound queue was previously empty, start the dispatch cycle going.
4261 if (wasEmpty && !connection->outboundQueue.empty()) {
4262 startDispatchCycleLocked(downTime, connection);
4263 }
Svet Ganov5d3bc372020-01-26 23:11:07 -08004264}
4265
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004266std::unique_ptr<MotionEntry> InputDispatcher::splitMotionEvent(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004267 const MotionEntry& originalMotionEntry, std::bitset<MAX_POINTER_ID + 1> pointerIds,
4268 nsecs_t splitDownTime) {
4269 ALOG_ASSERT(pointerIds.any());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 uint32_t splitPointerIndexMap[MAX_POINTERS];
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004272 std::vector<PointerProperties> splitPointerProperties;
4273 std::vector<PointerCoords> splitPointerCoords;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004275 uint32_t originalPointerCount = originalMotionEntry.getPointerCount();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 uint32_t splitPointerCount = 0;
4277
4278 for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004279 originalPointerIndex++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 const PointerProperties& pointerProperties =
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07004281 originalMotionEntry.pointerProperties[originalPointerIndex];
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 uint32_t pointerId = uint32_t(pointerProperties.id);
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004283 if (pointerIds.test(pointerId)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 splitPointerIndexMap[splitPointerCount] = originalPointerIndex;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004285 splitPointerProperties.push_back(pointerProperties);
4286 splitPointerCoords.push_back(originalMotionEntry.pointerCoords[originalPointerIndex]);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 splitPointerCount += 1;
4288 }
4289 }
4290
4291 if (splitPointerCount != pointerIds.count()) {
4292 // This is bad. We are missing some of the pointers that we expected to deliver.
4293 // Most likely this indicates that we received an ACTION_MOVE events that has
4294 // different pointer ids than we expected based on the previous ACTION_DOWN
4295 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
4296 // in this way.
4297 ALOGW("Dropping split motion event because the pointer count is %d but "
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004298 "we expected there to be %zu pointers. This probably means we received "
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -08004299 "a broken sequence of pointer ids from the input device: %s",
4300 splitPointerCount, pointerIds.count(), originalMotionEntry.getDescription().c_str());
Yi Kong9b14ac62018-07-17 13:48:38 -07004301 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 }
4303
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07004304 int32_t action = originalMotionEntry.action;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004306 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN ||
4307 maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
Siarhei Vishniakou5b9766d2023-07-18 14:06:29 -07004308 int32_t originalPointerIndex = MotionEvent::getActionIndex(action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 const PointerProperties& pointerProperties =
Siarhei Vishniakoud2770042019-10-29 11:08:14 -07004310 originalMotionEntry.pointerProperties[originalPointerIndex];
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 uint32_t pointerId = uint32_t(pointerProperties.id);
Siarhei Vishniakou8a878352023-01-30 14:05:01 -08004312 if (pointerIds.test(pointerId)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 if (pointerIds.count() == 1) {
4314 // The first/last pointer went down/up.
4315 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004316 ? AMOTION_EVENT_ACTION_DOWN
arthurhungea3f4fc2020-12-21 23:18:53 +08004317 : (originalMotionEntry.flags & AMOTION_EVENT_FLAG_CANCELED) != 0
4318 ? AMOTION_EVENT_ACTION_CANCEL
4319 : AMOTION_EVENT_ACTION_UP;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 } else {
4321 // A secondary pointer went down/up.
4322 uint32_t splitPointerIndex = 0;
4323 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) {
4324 splitPointerIndex += 1;
4325 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004326 action = maskedAction |
4327 (splitPointerIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 }
4329 } else {
4330 // An unrelated pointer changed.
4331 action = AMOTION_EVENT_ACTION_MOVE;
4332 }
4333 }
4334
Siarhei Vishniakou59e302b2023-06-05 08:04:53 -07004335 if (action == AMOTION_EVENT_ACTION_DOWN && splitDownTime != originalMotionEntry.eventTime) {
4336 logDispatchStateLocked();
4337 LOG_ALWAYS_FATAL("Split motion event has mismatching downTime and eventTime for "
4338 "ACTION_DOWN, motionEntry=%s, splitDownTime=%" PRId64,
4339 originalMotionEntry.getDescription().c_str(), splitDownTime);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004340 }
4341
Garfield Tanff1f1bb2020-01-28 13:24:04 -08004342 int32_t newId = mIdGenerator.nextId();
Prabir Pradhan2dac8b82023-09-06 01:11:51 +00004343 ATRACE_NAME_IF(ATRACE_ENABLED(),
4344 StringPrintf("Split MotionEvent(id=0x%" PRIx32 ") to MotionEvent(id=0x%" PRIx32
4345 ").",
4346 originalMotionEntry.id, newId));
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004347 std::unique_ptr<MotionEntry> splitMotionEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004348 std::make_unique<MotionEntry>(newId, originalMotionEntry.injectionState,
4349 originalMotionEntry.eventTime,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004350 originalMotionEntry.deviceId, originalMotionEntry.source,
4351 originalMotionEntry.displayId,
4352 originalMotionEntry.policyFlags, action,
4353 originalMotionEntry.actionButton,
4354 originalMotionEntry.flags, originalMotionEntry.metaState,
4355 originalMotionEntry.buttonState,
4356 originalMotionEntry.classification,
4357 originalMotionEntry.edgeFlags,
4358 originalMotionEntry.xPrecision,
4359 originalMotionEntry.yPrecision,
4360 originalMotionEntry.xCursorPosition,
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00004361 originalMotionEntry.yCursorPosition, splitDownTime,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004362 splitPointerProperties, splitPointerCoords);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 return splitMotionEntry;
4365}
4366
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004367void InputDispatcher::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
4368 std::scoped_lock _l(mLock);
4369 mLatencyTracker.setInputDevices(args.inputDeviceInfos);
4370}
4371
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004372void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004373 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004374 ALOGD("notifyConfigurationChanged - eventTime=%" PRId64, args.eventTime);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004375 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
Antonio Kantekf16f2832021-09-28 04:39:20 +00004377 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004379 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004381 std::unique_ptr<ConfigurationChangedEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004382 std::make_unique<ConfigurationChangedEntry>(args.id, args.eventTime);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004383 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 } // release lock
4385
4386 if (needWake) {
4387 mLooper->wake();
4388 }
4389}
4390
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004391void InputDispatcher::notifyKey(const NotifyKeyArgs& args) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004392 ALOGD_IF(debugInboundEventDetails(),
4393 "notifyKey - id=%" PRIx32 ", eventTime=%" PRId64
4394 ", deviceId=%d, source=%s, displayId=%" PRId32
4395 "policyFlags=0x%x, action=%s, flags=0x%x, keyCode=%s, scanCode=0x%x, metaState=0x%x, "
4396 "downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004397 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
4398 args.displayId, args.policyFlags, KeyEvent::actionToString(args.action), args.flags,
4399 KeyEvent::getLabel(args.keyCode), args.scanCode, args.metaState, args.downTime);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004400 Result<void> keyCheck = validateKeyEvent(args.action);
4401 if (!keyCheck.ok()) {
4402 LOG(ERROR) << "invalid key event: " << keyCheck.error();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 return;
4404 }
4405
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004406 uint32_t policyFlags = args.policyFlags;
4407 int32_t flags = args.flags;
4408 int32_t metaState = args.metaState;
Siarhei Vishniakou622bd322018-10-29 18:02:27 -07004409 // InputDispatcher tracks and generates key repeats on behalf of
4410 // whatever notifies it, so repeatCount should always be set to 0
4411 constexpr int32_t repeatCount = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
4413 policyFlags |= POLICY_FLAG_VIRTUAL;
4414 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
4415 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 if (policyFlags & POLICY_FLAG_FUNCTION) {
4417 metaState |= AMETA_FUNCTION_ON;
4418 }
4419
4420 policyFlags |= POLICY_FLAG_TRUSTED;
4421
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004422 int32_t keyCode = args.keyCode;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 KeyEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004424 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC, args.action,
4425 flags, keyCode, args.scanCode, metaState, repeatCount, args.downTime,
4426 args.eventTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
Michael Wright2b3c3302018-03-02 17:19:13 +00004428 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004429 mPolicy.interceptKeyBeforeQueueing(event, /*byref*/ policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004430 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4431 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004432 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004433 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Antonio Kantekf16f2832021-09-28 04:39:20 +00004435 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 { // acquire lock
4437 mLock.lock();
4438
4439 if (shouldSendKeyToInputFilterLocked(args)) {
4440 mLock.unlock();
4441
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004442 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004443 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 return; // event was consumed by the filter
4445 }
4446
4447 mLock.lock();
4448 }
4449
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004450 std::unique_ptr<KeyEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004451 std::make_unique<KeyEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4452 args.deviceId, args.source, args.displayId, policyFlags,
4453 args.action, flags, keyCode, args.scanCode, metaState,
4454 repeatCount, args.downTime);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004455 if (mTracer) {
4456 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4457 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004459 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 mLock.unlock();
4461 } // release lock
4462
4463 if (needWake) {
4464 mLooper->wake();
4465 }
4466}
4467
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004468bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 return mInputFilterEnabled;
4470}
4471
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004472void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004473 if (debugInboundEventDetails()) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004474 ALOGD("notifyMotion - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=%s, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004475 "displayId=%" PRId32 ", policyFlags=0x%x, "
Siarhei Vishniakou6ebd0692022-10-20 15:05:45 -07004476 "action=%s, actionButton=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, "
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004477 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, xCursorPosition=%f, "
4478 "yCursorPosition=%f, downTime=%" PRId64,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004479 args.id, args.eventTime, args.deviceId, inputEventSourceToString(args.source).c_str(),
4480 args.displayId, args.policyFlags, MotionEvent::actionToString(args.action).c_str(),
4481 args.actionButton, args.flags, args.metaState, args.buttonState, args.edgeFlags,
4482 args.xPrecision, args.yPrecision, args.xCursorPosition, args.yCursorPosition,
4483 args.downTime);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004484 for (uint32_t i = 0; i < args.getPointerCount(); i++) {
Prabir Pradhan96282b02023-02-24 22:36:17 +00004485 ALOGD(" Pointer %d: id=%d, toolType=%s, x=%f, y=%f, pressure=%f, size=%f, "
4486 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, orientation=%f",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004487 i, args.pointerProperties[i].id,
4488 ftl::enum_string(args.pointerProperties[i].toolType).c_str(),
4489 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
4490 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
4491 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
4492 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
4493 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
4494 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
4495 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
4496 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
4497 args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004498 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 }
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004500
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004501 Result<void> motionCheck =
4502 validateMotionEvent(args.action, args.actionButton, args.getPointerCount(),
4503 args.pointerProperties.data());
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004504 if (!motionCheck.ok()) {
4505 LOG(FATAL) << "Invalid event: " << args.dump() << "; reason: " << motionCheck.error();
4506 return;
4507 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004509 if (DEBUG_VERIFY_EVENTS) {
4510 auto [it, _] =
4511 mVerifiersByDisplay.try_emplace(args.displayId,
4512 StringPrintf("display %" PRId32, args.displayId));
4513 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -07004514 it->second.processMovement(args.deviceId, args.source, args.action,
4515 args.getPointerCount(), args.pointerProperties.data(),
4516 args.pointerCoords.data(), args.flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07004517 if (!result.ok()) {
4518 LOG(FATAL) << "Bad stream: " << result.error() << " caused by " << args.dump();
4519 }
4520 }
4521
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004522 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 policyFlags |= POLICY_FLAG_TRUSTED;
Michael Wright2b3c3302018-03-02 17:19:13 +00004524
4525 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004526 mPolicy.interceptMotionBeforeQueueing(args.displayId, args.source, args.action, args.eventTime,
4527 policyFlags);
Michael Wright2b3c3302018-03-02 17:19:13 +00004528 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4529 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004530 std::to_string(t.duration().count()).c_str());
Michael Wright2b3c3302018-03-02 17:19:13 +00004531 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532
Antonio Kantekf16f2832021-09-28 04:39:20 +00004533 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 { // acquire lock
4535 mLock.lock();
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004536 if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
4537 // Set the flag anyway if we already have an ongoing gesture. That would allow us to
4538 // complete the processing of the current stroke.
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004539 const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId);
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004540 if (touchStateIt != mTouchStatesByDisplay.end()) {
4541 const TouchState& touchState = touchStateIt->second;
Linnan Li907ae732023-09-05 17:14:21 +08004542 if (touchState.hasTouchingPointers(args.deviceId) ||
4543 touchState.hasHoveringPointers(args.deviceId)) {
Siarhei Vishniakou5bf25d92023-02-08 15:43:38 -08004544 policyFlags |= POLICY_FLAG_PASS_TO_USER;
4545 }
4546 }
4547 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
4549 if (shouldSendMotionToInputFilterLocked(args)) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004550 ui::Transform displayTransform;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004551 if (const auto it = mDisplayInfos.find(args.displayId); it != mDisplayInfos.end()) {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07004552 displayTransform = it->second.transform;
4553 }
4554
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 mLock.unlock();
4556
4557 MotionEvent event;
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004558 event.initialize(args.id, args.deviceId, args.source, args.displayId, INVALID_HMAC,
4559 args.action, args.actionButton, args.flags, args.edgeFlags,
4560 args.metaState, args.buttonState, args.classification,
4561 displayTransform, args.xPrecision, args.yPrecision,
4562 args.xCursorPosition, args.yCursorPosition, displayTransform,
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004563 args.downTime, args.eventTime, args.getPointerCount(),
4564 args.pointerProperties.data(), args.pointerCoords.data());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565
4566 policyFlags |= POLICY_FLAG_FILTERED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004567 if (!mPolicy.filterInputEvent(event, policyFlags)) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 return; // event was consumed by the filter
4569 }
4570
4571 mLock.lock();
4572 }
4573
4574 // Just enqueue a new motion event.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004575 std::unique_ptr<MotionEntry> newEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004576 std::make_unique<MotionEntry>(args.id, /*injectionState=*/nullptr, args.eventTime,
4577 args.deviceId, args.source, args.displayId,
4578 policyFlags, args.action, args.actionButton,
4579 args.flags, args.metaState, args.buttonState,
4580 args.classification, args.edgeFlags, args.xPrecision,
4581 args.yPrecision, args.xCursorPosition,
4582 args.yCursorPosition, args.downTime,
4583 args.pointerProperties, args.pointerCoords);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004584 if (mTracer) {
4585 newEntry->traceTracker = mTracer->traceInboundEvent(*newEntry);
4586 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004588 if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
4589 IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004590 !mInputFilterEnabled) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004591 const bool isDown = args.action == AMOTION_EVENT_ACTION_DOWN;
Asmita Poddardd9a6cd2023-09-26 15:35:12 +00004592 std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args);
4593 mLatencyTracker.trackListener(args.id, isDown, args.eventTime, args.readTime,
4594 args.deviceId, sources);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00004595 }
4596
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004597 needWake = enqueueInboundEventLocked(std::move(newEntry));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 mLock.unlock();
4599 } // release lock
4600
4601 if (needWake) {
4602 mLooper->wake();
4603 }
4604}
4605
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004606void InputDispatcher::notifySensor(const NotifySensorArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004607 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004608 ALOGD("notifySensor - id=%" PRIx32 " eventTime=%" PRId64 ", deviceId=%d, source=0x%x, "
4609 " sensorType=%s",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004610 args.id, args.eventTime, args.deviceId, args.source,
4611 ftl::enum_string(args.sensorType).c_str());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004612 }
Chris Yef59a2f42020-10-16 12:55:26 -07004613
Antonio Kantekf16f2832021-09-28 04:39:20 +00004614 bool needWake = false;
Chris Yef59a2f42020-10-16 12:55:26 -07004615 { // acquire lock
4616 mLock.lock();
4617
4618 // Just enqueue a new sensor event.
4619 std::unique_ptr<SensorEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004620 std::make_unique<SensorEntry>(args.id, args.eventTime, args.deviceId, args.source,
4621 /* policyFlags=*/0, args.hwTimestamp, args.sensorType,
4622 args.accuracy, args.accuracyChanged, args.values);
Chris Yef59a2f42020-10-16 12:55:26 -07004623
4624 needWake = enqueueInboundEventLocked(std::move(newEntry));
4625 mLock.unlock();
4626 } // release lock
4627
4628 if (needWake) {
4629 mLooper->wake();
4630 }
4631}
4632
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004633void InputDispatcher::notifyVibratorState(const NotifyVibratorStateArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004634 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004635 ALOGD("notifyVibratorState - eventTime=%" PRId64 ", device=%d, isOn=%d", args.eventTime,
4636 args.deviceId, args.isOn);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004637 }
Prabir Pradhana41d2442023-04-20 21:30:40 +00004638 mPolicy.notifyVibratorState(args.deviceId, args.isOn);
Chris Yefb552902021-02-03 17:18:37 -08004639}
4640
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004641bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) {
Jackal Guof9696682018-10-05 12:23:23 +08004642 return mInputFilterEnabled;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643}
4644
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004645void InputDispatcher::notifySwitch(const NotifySwitchArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004646 if (debugInboundEventDetails()) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004647 ALOGD("notifySwitch - eventTime=%" PRId64 ", policyFlags=0x%x, switchValues=0x%08x, "
4648 "switchMask=0x%08x",
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004649 args.eventTime, args.policyFlags, args.switchValues, args.switchMask);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004650 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004652 uint32_t policyFlags = args.policyFlags;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 policyFlags |= POLICY_FLAG_TRUSTED;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004654 mPolicy.notifySwitch(args.eventTime, args.switchValues, args.switchMask, policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655}
4656
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004657void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004658 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004659 ALOGD("notifyDeviceReset - eventTime=%" PRId64 ", deviceId=%d", args.eventTime,
4660 args.deviceId);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004661 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
Antonio Kantekf16f2832021-09-28 04:39:20 +00004663 bool needWake = false;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004665 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004667 std::unique_ptr<DeviceResetEntry> newEntry =
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004668 std::make_unique<DeviceResetEntry>(args.id, args.eventTime, args.deviceId);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004669 needWake = enqueueInboundEventLocked(std::move(newEntry));
Siarhei Vishniakou1160ecd2023-06-28 15:57:47 -07004670
4671 for (auto& [_, verifier] : mVerifiersByDisplay) {
4672 verifier.resetDevice(args.deviceId);
4673 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 } // release lock
4675
4676 if (needWake) {
4677 mLooper->wake();
4678 }
4679}
4680
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004681void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
Prabir Pradhan65613802023-02-22 23:36:58 +00004682 if (debugInboundEventDetails()) {
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004683 ALOGD("notifyPointerCaptureChanged - eventTime=%" PRId64 ", enabled=%s", args.eventTime,
4684 args.request.enable ? "true" : "false");
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004685 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004686
Antonio Kantekf16f2832021-09-28 04:39:20 +00004687 bool needWake = false;
Prabir Pradhan99987712020-11-10 18:43:05 -08004688 { // acquire lock
4689 std::scoped_lock _l(mLock);
Prabir Pradhanc392d8f2023-04-13 19:32:51 +00004690 auto entry =
4691 std::make_unique<PointerCaptureChangedEntry>(args.id, args.eventTime, args.request);
Prabir Pradhan99987712020-11-10 18:43:05 -08004692 needWake = enqueueInboundEventLocked(std::move(entry));
4693 } // release lock
4694
4695 if (needWake) {
4696 mLooper->wake();
4697 }
Prabir Pradhan7e186182020-11-10 13:56:45 -08004698}
4699
Prabir Pradhan5735a322022-04-11 17:23:34 +00004700InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00004701 std::optional<gui::Uid> targetUid,
Prabir Pradhan5735a322022-04-11 17:23:34 +00004702 InputEventInjectionSync syncMode,
4703 std::chrono::milliseconds timeout,
4704 uint32_t policyFlags) {
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004705 Result<void> eventValidation = validateInputEvent(*event);
4706 if (!eventValidation.ok()) {
4707 LOG(INFO) << "Injection failed: invalid event: " << eventValidation.error();
4708 return InputEventInjectionResult::FAILED;
4709 }
4710
Prabir Pradhan65613802023-02-22 23:36:58 +00004711 if (debugInboundEventDetails()) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004712 LOG(INFO) << __func__ << ": targetUid=" << toString(targetUid, &uidString)
4713 << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count()
4714 << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec
4715 << ", event=" << *event;
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004716 }
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -07004717 nsecs_t endTime = now() + std::chrono::duration_cast<std::chrono::nanoseconds>(timeout).count();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
Prabir Pradhan5735a322022-04-11 17:23:34 +00004719 policyFlags |= POLICY_FLAG_INJECTED | POLICY_FLAG_TRUSTED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004721 // For all injected events, set device id = VIRTUAL_KEYBOARD_ID. The only exception is events
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004722 // that have gone through the InputFilter. If the event passed through the InputFilter, assign
4723 // the provided device id. If the InputFilter is accessibility, and it modifies or synthesizes
4724 // the injected event, it is responsible for setting POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY.
4725 // For those events, we will set FLAG_IS_ACCESSIBILITY_EVENT to allow apps to distinguish them
4726 // from events that originate from actual hardware.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -07004727 DeviceId resolvedDeviceId = VIRTUAL_KEYBOARD_ID;
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004728 if (policyFlags & POLICY_FLAG_FILTERED) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004729 resolvedDeviceId = event->getDeviceId();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004730 }
4731
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004732 const bool isAsync = syncMode == InputEventInjectionSync::NONE;
4733 auto injectionState = std::make_shared<InjectionState>(targetUid, isAsync);
4734
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004735 std::queue<std::unique_ptr<EventEntry>> injectedEntries;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004737 case InputEventType::KEY: {
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004738 const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event);
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004739 const int32_t action = incomingKey.getAction();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004740 int32_t flags = incomingKey.getFlags();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004741 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4742 flags |= AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4743 }
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004744 int32_t keyCode = incomingKey.getKeyCode();
4745 int32_t metaState = incomingKey.getMetaState();
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004746 KeyEvent keyEvent;
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004747 keyEvent.initialize(incomingKey.getId(), resolvedDeviceId, incomingKey.getSource(),
Siarhei Vishniakou0d8ed6e2020-01-17 15:48:59 -08004748 incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode,
4749 incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
4750 incomingKey.getDownTime(), incomingKey.getEventTime());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004752 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
4753 policyFlags |= POLICY_FLAG_VIRTUAL;
Michael Wright2b3c3302018-03-02 17:19:13 +00004754 }
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004755
4756 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
4757 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00004758 mPolicy.interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004759 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4760 ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
4761 std::to_string(t.duration().count()).c_str());
4762 }
4763 }
4764
4765 mLock.lock();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004766 std::unique_ptr<KeyEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004767 std::make_unique<KeyEntry>(incomingKey.getId(), injectionState,
4768 incomingKey.getEventTime(), resolvedDeviceId,
4769 incomingKey.getSource(), incomingKey.getDisplayId(),
4770 policyFlags, action, flags, keyCode,
4771 incomingKey.getScanCode(), metaState,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004772 incomingKey.getRepeatCount(),
4773 incomingKey.getDownTime());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004774 if (mTracer) {
4775 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4776 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004777 injectedEntries.push(std::move(injectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004778 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 }
4780
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004781 case InputEventType::MOTION: {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004782 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
Prabir Pradhanaa561d12021-09-24 06:57:33 -07004783 const bool isPointerEvent =
4784 isFromSource(event->getSource(), AINPUT_SOURCE_CLASS_POINTER);
4785 // If a pointer event has no displayId specified, inject it to the default display.
4786 const uint32_t displayId = isPointerEvent && (event->getDisplayId() == ADISPLAY_ID_NONE)
4787 ? ADISPLAY_ID_DEFAULT
4788 : event->getDisplayId();
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004789 int32_t flags = motionEvent.getFlags();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004790
4791 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004792 nsecs_t eventTime = motionEvent.getEventTime();
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004793 android::base::Timer t;
Yeabkal Wubshit88a90412023-12-21 18:23:04 -08004794 mPolicy.interceptMotionBeforeQueueing(displayId, motionEvent.getSource(),
4795 motionEvent.getAction(), eventTime,
4796 /*byref*/ policyFlags);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004797 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
4798 ALOGW("Excessive delay in interceptMotionBeforeQueueing; took %s ms",
4799 std::to_string(t.duration().count()).c_str());
4800 }
4801 }
4802
Siarhei Vishniakouf00a4ec2021-06-16 03:55:32 +00004803 if (policyFlags & POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY) {
4804 flags |= AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
4805 }
4806
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004807 mLock.lock();
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004808 const nsecs_t* sampleEventTimes = motionEvent.getSampleEventTimes();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004809 const size_t pointerCount = motionEvent.getPointerCount();
4810 const std::vector<PointerProperties>
4811 pointerProperties(motionEvent.getPointerProperties(),
4812 motionEvent.getPointerProperties() + pointerCount);
4813
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004814 const PointerCoords* samplePointerCoords = motionEvent.getSamplePointerCoords();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004815 std::unique_ptr<MotionEntry> injectedEntry =
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004816 std::make_unique<MotionEntry>(motionEvent.getId(), injectionState,
4817 *sampleEventTimes, resolvedDeviceId,
4818 motionEvent.getSource(), displayId, policyFlags,
4819 motionEvent.getAction(),
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004820 motionEvent.getActionButton(), flags,
4821 motionEvent.getMetaState(),
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004822 motionEvent.getButtonState(),
4823 motionEvent.getClassification(),
4824 motionEvent.getEdgeFlags(),
4825 motionEvent.getXPrecision(),
4826 motionEvent.getYPrecision(),
4827 motionEvent.getRawXCursorPosition(),
4828 motionEvent.getRawYCursorPosition(),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004829 motionEvent.getDownTime(), pointerProperties,
4830 std::vector<PointerCoords>(samplePointerCoords,
4831 samplePointerCoords +
4832 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004833 transformMotionEntryForInjectionLocked(*injectedEntry, motionEvent.getTransform());
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00004834 if (mTracer) {
4835 injectedEntry->traceTracker = mTracer->traceInboundEvent(*injectedEntry);
4836 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004837 injectedEntries.push(std::move(injectedEntry));
Siarhei Vishniakou5d552c42021-05-21 05:02:22 +00004838 for (size_t i = motionEvent.getHistorySize(); i > 0; i--) {
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004839 sampleEventTimes += 1;
Siarhei Vishniakou23740b92023-04-21 11:30:20 -07004840 samplePointerCoords += motionEvent.getPointerCount();
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004841 std::unique_ptr<MotionEntry> nextInjectedEntry = std::make_unique<
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004842 MotionEntry>(motionEvent.getId(), injectionState, *sampleEventTimes,
4843 resolvedDeviceId, motionEvent.getSource(), displayId,
4844 policyFlags, motionEvent.getAction(),
4845 motionEvent.getActionButton(), flags,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07004846 motionEvent.getMetaState(), motionEvent.getButtonState(),
4847 motionEvent.getClassification(), motionEvent.getEdgeFlags(),
4848 motionEvent.getXPrecision(), motionEvent.getYPrecision(),
4849 motionEvent.getRawXCursorPosition(),
4850 motionEvent.getRawYCursorPosition(), motionEvent.getDownTime(),
4851 pointerProperties,
4852 std::vector<PointerCoords>(samplePointerCoords,
4853 samplePointerCoords +
4854 pointerCount));
Prabir Pradhandaa2f142021-12-10 09:30:08 +00004855 transformMotionEntryForInjectionLocked(*nextInjectedEntry,
4856 motionEvent.getTransform());
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004857 injectedEntries.push(std::move(nextInjectedEntry));
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004858 }
4859 break;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861
Garfield Tan0fc2fa72019-08-29 17:22:15 -07004862 default:
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004863 LOG(WARNING) << "Cannot inject " << ftl::enum_string(event->getType()) << " events";
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004864 return InputEventInjectionResult::FAILED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 }
4866
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 bool needWake = false;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004868 while (!injectedEntries.empty()) {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004869 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004870 LOG(INFO) << "Injecting " << injectedEntries.front()->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -08004871 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07004872 needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front()));
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07004873 injectedEntries.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 }
4875
4876 mLock.unlock();
4877
4878 if (needWake) {
4879 mLooper->wake();
4880 }
4881
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004882 InputEventInjectionResult injectionResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004884 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004886 if (syncMode == InputEventInjectionSync::NONE) {
4887 injectionResult = InputEventInjectionResult::SUCCEEDED;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888 } else {
4889 for (;;) {
4890 injectionResult = injectionState->injectionResult;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004891 if (injectionResult != InputEventInjectionResult::PENDING) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 break;
4893 }
4894
4895 nsecs_t remainingTimeout = endTime - now();
4896 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004897 if (DEBUG_INJECTION) {
4898 ALOGD("injectInputEvent - Timed out waiting for injection result "
4899 "to become available.");
4900 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004901 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 break;
4903 }
4904
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004905 mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 }
4907
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004908 if (injectionResult == InputEventInjectionResult::SUCCEEDED &&
4909 syncMode == InputEventInjectionSync::WAIT_FOR_FINISHED) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 while (injectionState->pendingForegroundDispatches != 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004911 if (DEBUG_INJECTION) {
4912 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
4913 injectionState->pendingForegroundDispatches);
4914 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 nsecs_t remainingTimeout = endTime - now();
4916 if (remainingTimeout <= 0) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004917 if (DEBUG_INJECTION) {
4918 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
4919 "dispatches to finish.");
4920 }
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004921 injectionResult = InputEventInjectionResult::TIMED_OUT;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 break;
4923 }
4924
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08004925 mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926 }
4927 }
4928 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 } // release lock
4930
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004931 if (DEBUG_INJECTION) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07004932 LOG(INFO) << "injectInputEvent - Finished with result "
4933 << ftl::enum_string(injectionResult);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00004934 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935
4936 return injectionResult;
4937}
4938
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08004939std::unique_ptr<VerifiedInputEvent> InputDispatcher::verifyInputEvent(const InputEvent& event) {
Gang Wange9087892020-01-07 12:17:14 -05004940 std::array<uint8_t, 32> calculatedHmac;
4941 std::unique_ptr<VerifiedInputEvent> result;
4942 switch (event.getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004943 case InputEventType::KEY: {
Gang Wange9087892020-01-07 12:17:14 -05004944 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
4945 VerifiedKeyEvent verifiedKeyEvent = verifiedKeyEventFromKeyEvent(keyEvent);
4946 result = std::make_unique<VerifiedKeyEvent>(verifiedKeyEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07004947 calculatedHmac = sign(verifiedKeyEvent);
Gang Wange9087892020-01-07 12:17:14 -05004948 break;
4949 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07004950 case InputEventType::MOTION: {
Gang Wange9087892020-01-07 12:17:14 -05004951 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
4952 VerifiedMotionEvent verifiedMotionEvent =
4953 verifiedMotionEventFromMotionEvent(motionEvent);
4954 result = std::make_unique<VerifiedMotionEvent>(verifiedMotionEvent);
chaviw09c8d2d2020-08-24 15:48:26 -07004955 calculatedHmac = sign(verifiedMotionEvent);
Gang Wange9087892020-01-07 12:17:14 -05004956 break;
4957 }
4958 default: {
Siarhei Vishniakouef2b4502023-12-28 11:51:47 -08004959 LOG(ERROR) << "Cannot verify events of type " << ftl::enum_string(event.getType());
Gang Wange9087892020-01-07 12:17:14 -05004960 return nullptr;
4961 }
4962 }
4963 if (calculatedHmac == INVALID_HMAC) {
4964 return nullptr;
4965 }
tyiu1573a672023-02-21 22:38:32 +00004966 if (0 != CRYPTO_memcmp(calculatedHmac.data(), event.getHmac().data(), calculatedHmac.size())) {
Gang Wange9087892020-01-07 12:17:14 -05004967 return nullptr;
4968 }
4969 return result;
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08004970}
4971
Prabir Pradhan24047542023-11-02 17:14:59 +00004972void InputDispatcher::setInjectionResult(const EventEntry& entry,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -08004973 InputEventInjectionResult injectionResult) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004974 if (!entry.injectionState) {
4975 // Not an injected event.
4976 return;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 }
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00004978
4979 InjectionState& injectionState = *entry.injectionState;
4980 if (DEBUG_INJECTION) {
4981 LOG(INFO) << "Setting input event injection result to "
4982 << ftl::enum_string(injectionResult);
4983 }
4984
4985 if (injectionState.injectionIsAsync && !(entry.policyFlags & POLICY_FLAG_FILTERED)) {
4986 // Log the outcome since the injector did not wait for the injection result.
4987 switch (injectionResult) {
4988 case InputEventInjectionResult::SUCCEEDED:
4989 ALOGV("Asynchronous input event injection succeeded.");
4990 break;
4991 case InputEventInjectionResult::TARGET_MISMATCH:
4992 ALOGV("Asynchronous input event injection target mismatch.");
4993 break;
4994 case InputEventInjectionResult::FAILED:
4995 ALOGW("Asynchronous input event injection failed.");
4996 break;
4997 case InputEventInjectionResult::TIMED_OUT:
4998 ALOGW("Asynchronous input event injection timed out.");
4999 break;
5000 case InputEventInjectionResult::PENDING:
5001 ALOGE("Setting result to 'PENDING' for asynchronous injection");
5002 break;
5003 }
5004 }
5005
5006 injectionState.injectionResult = injectionResult;
5007 mInjectionResultAvailable.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005010void InputDispatcher::transformMotionEntryForInjectionLocked(
5011 MotionEntry& entry, const ui::Transform& injectedTransform) const {
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005012 // Input injection works in the logical display coordinate space, but the input pipeline works
5013 // display space, so we need to transform the injected events accordingly.
5014 const auto it = mDisplayInfos.find(entry.displayId);
5015 if (it == mDisplayInfos.end()) return;
Prabir Pradhandaa2f142021-12-10 09:30:08 +00005016 const auto& transformToDisplay = it->second.transform.inverse() * injectedTransform;
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005017
Prabir Pradhand9a2ebe2022-07-20 19:25:13 +00005018 if (entry.xCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
5019 entry.yCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
5020 const vec2 cursor =
5021 MotionEvent::calculateTransformedXY(entry.source, transformToDisplay,
5022 {entry.xCursorPosition, entry.yCursorPosition});
5023 entry.xCursorPosition = cursor.x;
5024 entry.yCursorPosition = cursor.y;
5025 }
Siarhei Vishniakouedd61202023-10-18 11:22:40 -07005026 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08005027 entry.pointerCoords[i] =
5028 MotionEvent::calculateTransformedCoords(entry.source, transformToDisplay,
5029 entry.pointerCoords[i]);
Prabir Pradhan81420cc2021-09-06 10:28:50 -07005030 }
5031}
5032
Prabir Pradhan24047542023-11-02 17:14:59 +00005033void InputDispatcher::incrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005034 if (entry.injectionState) {
5035 entry.injectionState->pendingForegroundDispatches += 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036 }
5037}
5038
Prabir Pradhan24047542023-11-02 17:14:59 +00005039void InputDispatcher::decrementPendingForegroundDispatches(const EventEntry& entry) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005040 if (entry.injectionState) {
5041 entry.injectionState->pendingForegroundDispatches -= 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042
Prabir Pradhana8cdbe12023-11-01 21:30:02 +00005043 if (entry.injectionState->pendingForegroundDispatches == 0) {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005044 mInjectionSyncFinished.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 }
5046 }
5047}
5048
chaviw98318de2021-05-19 16:45:23 -05005049const std::vector<sp<WindowInfoHandle>>& InputDispatcher::getWindowHandlesLocked(
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005050 int32_t displayId) const {
chaviw98318de2021-05-19 16:45:23 -05005051 static const std::vector<sp<WindowInfoHandle>> EMPTY_WINDOW_HANDLES;
Vishnu Nairad321cd2020-08-20 16:40:21 -07005052 auto it = mWindowHandlesByDisplay.find(displayId);
5053 return it != mWindowHandlesByDisplay.end() ? it->second : EMPTY_WINDOW_HANDLES;
Arthur Hungb92218b2018-08-14 12:00:21 +08005054}
5055
chaviw98318de2021-05-19 16:45:23 -05005056sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
Prabir Pradhan16463382023-10-12 23:03:19 +00005057 const sp<IBinder>& windowHandleToken, std::optional<int32_t> displayId) const {
arthurhungbe737672020-06-24 12:29:21 +08005058 if (windowHandleToken == nullptr) {
5059 return nullptr;
5060 }
5061
Prabir Pradhan16463382023-10-12 23:03:19 +00005062 if (!displayId) {
5063 // Look through all displays.
5064 for (auto& it : mWindowHandlesByDisplay) {
5065 const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second;
5066 for (const sp<WindowInfoHandle>& windowHandle : windowHandles) {
5067 if (windowHandle->getToken() == windowHandleToken) {
5068 return windowHandle;
5069 }
Arthur Hungb92218b2018-08-14 12:00:21 +08005070 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 }
Vishnu Nairad321cd2020-08-20 16:40:21 -07005072 return nullptr;
5073 }
5074
Prabir Pradhan16463382023-10-12 23:03:19 +00005075 // Only look through the requested display.
5076 for (const sp<WindowInfoHandle>& windowHandle : getWindowHandlesLocked(*displayId)) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005077 if (windowHandle->getToken() == windowHandleToken) {
5078 return windowHandle;
5079 }
5080 }
5081 return nullptr;
5082}
5083
chaviw98318de2021-05-19 16:45:23 -05005084sp<WindowInfoHandle> InputDispatcher::getWindowHandleLocked(
5085 const sp<WindowInfoHandle>& windowHandle) const {
Mady Mellor017bcd12020-06-23 19:12:00 +00005086 for (auto& it : mWindowHandlesByDisplay) {
chaviw98318de2021-05-19 16:45:23 -05005087 const std::vector<sp<WindowInfoHandle>>& windowHandles = it.second;
5088 for (const sp<WindowInfoHandle>& handle : windowHandles) {
arthurhungbe737672020-06-24 12:29:21 +08005089 if (handle->getId() == windowHandle->getId() &&
5090 handle->getToken() == windowHandle->getToken()) {
Mady Mellor017bcd12020-06-23 19:12:00 +00005091 if (windowHandle->getInfo()->displayId != it.first) {
5092 ALOGE("Found window %s in display %" PRId32
5093 ", but it should belong to display %" PRId32,
5094 windowHandle->getName().c_str(), it.first,
5095 windowHandle->getInfo()->displayId);
5096 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005097 return handle;
Arthur Hungb92218b2018-08-14 12:00:21 +08005098 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 }
5100 }
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005101 return nullptr;
5102}
5103
chaviw98318de2021-05-19 16:45:23 -05005104sp<WindowInfoHandle> InputDispatcher::getFocusedWindowHandleLocked(int displayId) const {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005105 sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(displayId);
5106 return getWindowHandleLocked(focusedToken, displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107}
5108
Prabir Pradhan33e3baa2022-12-06 20:30:22 +00005109ui::Transform InputDispatcher::getTransformLocked(int32_t displayId) const {
5110 auto displayInfoIt = mDisplayInfos.find(displayId);
5111 return displayInfoIt != mDisplayInfos.end() ? displayInfoIt->second.transform
5112 : kIdentityTransform;
5113}
5114
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005115bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window,
5116 const MotionEntry& motionEntry) const {
5117 const WindowInfo& info = *window->getInfo();
5118
5119 // Skip spy window targets that are not valid for targeted injection.
5120 if (const auto err = verifyTargetedInjection(window, motionEntry); err) {
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005121 return false;
5122 }
5123
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005124 if (info.inputConfig.test(WindowInfo::InputConfig::PAUSE_DISPATCHING)) {
5125 ALOGI("Not sending touch event to %s because it is paused", window->getName().c_str());
5126 return false;
5127 }
5128
5129 if (info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) {
5130 ALOGW("Not sending touch gesture to %s because it has config NO_INPUT_CHANNEL",
5131 window->getName().c_str());
5132 return false;
5133 }
5134
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005135 std::shared_ptr<Connection> connection = getConnectionLocked(window->getToken());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005136 if (connection == nullptr) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005137 ALOGW("Not sending touch to %s because there's no corresponding connection",
5138 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005139 return false;
5140 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005141
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005142 if (!connection->responsive) {
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005143 ALOGW("Not sending touch to %s because it is not responsive", window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005144 return false;
5145 }
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005146
5147 // Drop events that can't be trusted due to occlusion
5148 const auto [x, y] = resolveTouchedPosition(motionEntry);
5149 TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5150 if (!isTouchTrustedLocked(occlusionInfo)) {
5151 if (DEBUG_TOUCH_OCCLUSION) {
Prabir Pradhan82e081e2022-12-06 09:50:09 +00005152 ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005153 for (const auto& log : occlusionInfo.debugInfo) {
5154 ALOGD("%s", log.c_str());
5155 }
5156 }
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005157 ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(),
5158 occlusionInfo.obscuringUid.toString().c_str());
Siarhei Vishniakoud4e3f3a2022-09-27 14:31:05 -07005159 return false;
5160 }
5161
5162 // Drop touch events if requested by input feature
5163 if (shouldDropInput(motionEntry, window)) {
5164 return false;
5165 }
5166
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -07005167 // Ignore touches if stylus is down anywhere on screen
5168 if (info.inputConfig.test(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH) &&
5169 isStylusActiveInDisplay(info.displayId, mTouchStatesByDisplay)) {
5170 LOG(INFO) << "Dropping touch from " << window->getName() << " because stylus is active";
5171 return false;
5172 }
5173
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005174 return true;
5175}
5176
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005177void InputDispatcher::updateWindowHandlesForDisplayLocked(
chaviw98318de2021-05-19 16:45:23 -05005178 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) {
5179 if (windowInfoHandles.empty()) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005180 // Remove all handles on a display if there are no windows left.
5181 mWindowHandlesByDisplay.erase(displayId);
5182 return;
5183 }
5184
5185 // Since we compare the pointer of input window handles across window updates, we need
5186 // to make sure the handle object for the same window stays unchanged across updates.
chaviw98318de2021-05-19 16:45:23 -05005187 const std::vector<sp<WindowInfoHandle>>& oldHandles = getWindowHandlesLocked(displayId);
5188 std::unordered_map<int32_t /*id*/, sp<WindowInfoHandle>> oldHandlesById;
5189 for (const sp<WindowInfoHandle>& handle : oldHandles) {
chaviwaf87b3e2019-10-01 16:59:28 -07005190 oldHandlesById[handle->getId()] = handle;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005191 }
5192
chaviw98318de2021-05-19 16:45:23 -05005193 std::vector<sp<WindowInfoHandle>> newHandles;
5194 for (const sp<WindowInfoHandle>& handle : windowInfoHandles) {
chaviw98318de2021-05-19 16:45:23 -05005195 const WindowInfo* info = handle->getInfo();
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005196 if (getConnectionLocked(handle->getToken()) == nullptr) {
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005197 const bool noInputChannel =
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005198 info->inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005199 const bool canReceiveInput =
5200 !info->inputConfig.test(WindowInfo::InputConfig::NOT_TOUCHABLE) ||
5201 !info->inputConfig.test(WindowInfo::InputConfig::NOT_FOCUSABLE);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005202 if (canReceiveInput && !noInputChannel) {
John Recke0710582019-09-26 13:46:12 -07005203 ALOGV("Window handle %s has no registered input channel",
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005204 handle->getName().c_str());
Robert Carr2984b7a2020-04-13 17:06:45 -07005205 continue;
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005206 }
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005207 }
5208
5209 if (info->displayId != displayId) {
5210 ALOGE("Window %s updated by wrong display %d, should belong to display %d",
5211 handle->getName().c_str(), displayId, info->displayId);
5212 continue;
5213 }
5214
Robert Carredd13602020-04-13 17:24:34 -07005215 if ((oldHandlesById.find(handle->getId()) != oldHandlesById.end()) &&
5216 (oldHandlesById.at(handle->getId())->getToken() == handle->getToken())) {
chaviw98318de2021-05-19 16:45:23 -05005217 const sp<WindowInfoHandle>& oldHandle = oldHandlesById.at(handle->getId());
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005218 oldHandle->updateFrom(handle);
5219 newHandles.push_back(oldHandle);
5220 } else {
5221 newHandles.push_back(handle);
5222 }
5223 }
5224
5225 // Insert or replace
5226 mWindowHandlesByDisplay[displayId] = newHandles;
5227}
5228
Arthur Hungb92218b2018-08-14 12:00:21 +08005229/**
5230 * Called from InputManagerService, update window handle list by displayId that can receive input.
5231 * A window handle contains information about InputChannel, Touch Region, Types, Focused,...
5232 * If set an empty list, remove all handles from the specific display.
5233 * For focused handle, check if need to change and send a cancel event to previous one.
5234 * For removed handle, check if need to send a cancel event if already in touch.
5235 */
Arthur Hung72d8dc32020-03-28 00:48:39 +00005236void InputDispatcher::setInputWindowsLocked(
chaviw98318de2021-05-19 16:45:23 -05005237 const std::vector<sp<WindowInfoHandle>>& windowInfoHandles, int32_t displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005238 if (DEBUG_FOCUS) {
5239 std::string windowList;
chaviw98318de2021-05-19 16:45:23 -05005240 for (const sp<WindowInfoHandle>& iwh : windowInfoHandles) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005241 windowList += iwh->getName() + " ";
5242 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005243 LOG(INFO) << "setInputWindows displayId=" << displayId << " " << windowList;
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245
Prabir Pradhand65552b2021-10-07 11:23:50 -07005246 // Check preconditions for new input windows
chaviw98318de2021-05-19 16:45:23 -05005247 for (const sp<WindowInfoHandle>& window : windowInfoHandles) {
Prabir Pradhand65552b2021-10-07 11:23:50 -07005248 const WindowInfo& info = *window->getInfo();
5249
5250 // Ensure all tokens are null if the window has feature NO_INPUT_CHANNEL
Prabir Pradhan51e7db02022-02-07 06:02:57 -08005251 const bool noInputWindow = info.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005252 if (noInputWindow && window->getToken() != nullptr) {
5253 ALOGE("%s has feature NO_INPUT_WINDOW, but a non-null token. Clearing",
5254 window->getName().c_str());
5255 window->releaseChannel();
5256 }
Prabir Pradhand65552b2021-10-07 11:23:50 -07005257
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005258 // Ensure all spy windows are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005259 LOG_ALWAYS_FATAL_IF(info.isSpy() &&
5260 !info.inputConfig.test(
5261 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhan5c85e052021-12-22 02:27:12 -08005262 "%s has feature SPY, but is not a trusted overlay.",
5263 window->getName().c_str());
5264
Prabir Pradhand65552b2021-10-07 11:23:50 -07005265 // Ensure all stylus interceptors are trusted overlays
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -08005266 LOG_ALWAYS_FATAL_IF(info.interceptsStylus() &&
5267 !info.inputConfig.test(
5268 WindowInfo::InputConfig::TRUSTED_OVERLAY),
Prabir Pradhand65552b2021-10-07 11:23:50 -07005269 "%s has feature INTERCEPTS_STYLUS, but is not a trusted overlay.",
5270 window->getName().c_str());
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -05005271 }
5272
Arthur Hung72d8dc32020-03-28 00:48:39 +00005273 // Copy old handles for release if they are no longer present.
chaviw98318de2021-05-19 16:45:23 -05005274 const std::vector<sp<WindowInfoHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005275 const sp<WindowInfoHandle> removedFocusedWindowHandle = getFocusedWindowHandleLocked(displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276
chaviw98318de2021-05-19 16:45:23 -05005277 updateWindowHandlesForDisplayLocked(windowInfoHandles, displayId);
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07005278
chaviw98318de2021-05-19 16:45:23 -05005279 const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesLocked(displayId);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005280
Vishnu Nairc519ff72021-01-21 08:23:08 -08005281 std::optional<FocusResolver::FocusChanges> changes =
5282 mFocusResolver.setInputWindows(displayId, windowHandles);
5283 if (changes) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005284 onFocusChangedLocked(*changes, removedFocusedWindowHandle);
Arthur Hung72d8dc32020-03-28 00:48:39 +00005285 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286
Siarhei Vishniakou4700f822020-03-24 19:05:54 -07005287 std::unordered_map<int32_t, TouchState>::iterator stateIt =
5288 mTouchStatesByDisplay.find(displayId);
5289 if (stateIt != mTouchStatesByDisplay.end()) {
5290 TouchState& state = stateIt->second;
Arthur Hung72d8dc32020-03-28 00:48:39 +00005291 for (size_t i = 0; i < state.windows.size();) {
5292 TouchedWindow& touchedWindow = state.windows[i];
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005293 if (getWindowHandleLocked(touchedWindow.windowHandle) == nullptr) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07005294 LOG(INFO) << "Touched window was removed: " << touchedWindow.windowHandle->getName()
5295 << " in display %" << displayId;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005296 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
5297 "touched window was removed");
5298 synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
5299 // Since we are about to drop the touch, cancel the events for the wallpaper as
5300 // well.
5301 if (touchedWindow.targetFlags.test(InputTarget::Flags::FOREGROUND) &&
5302 touchedWindow.windowHandle->getInfo()->inputConfig.test(
5303 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
5304 if (const auto& ww = state.getWallpaperWindow(); ww) {
5305 synthesizeCancelationEventsForWindowLocked(ww, options);
Siarhei Vishniakouca205502021-07-16 21:31:58 +00005306 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005308 state.windows.erase(state.windows.begin() + i);
5309 } else {
5310 ++i;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 }
5312 }
arthurhungb89ccb02020-12-30 16:19:01 +08005313
arthurhung6d4bed92021-03-17 11:59:33 +08005314 // If drag window is gone, it would receive a cancel event and broadcast the DRAG_END. We
arthurhungb89ccb02020-12-30 16:19:01 +08005315 // could just clear the state here.
Arthur Hung3915c1f2022-05-31 07:17:17 +00005316 if (mDragState && mDragState->dragWindow->getInfo()->displayId == displayId &&
arthurhung6d4bed92021-03-17 11:59:33 +08005317 std::find(windowHandles.begin(), windowHandles.end(), mDragState->dragWindow) ==
arthurhungb89ccb02020-12-30 16:19:01 +08005318 windowHandles.end()) {
Arthur Hung3915c1f2022-05-31 07:17:17 +00005319 ALOGI("Drag window went away: %s", mDragState->dragWindow->getName().c_str());
5320 sendDropWindowCommandLocked(nullptr, 0, 0);
arthurhung6d4bed92021-03-17 11:59:33 +08005321 mDragState.reset();
arthurhungb89ccb02020-12-30 16:19:01 +08005322 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005323 }
Arthur Hung25e2af12020-03-26 12:58:37 +00005324
Arthur Hung72d8dc32020-03-28 00:48:39 +00005325 // Release information for windows that are no longer present.
5326 // This ensures that unused input channels are released promptly.
5327 // Otherwise, they might stick around until the window handle is destroyed
5328 // which might not happen until the next GC.
chaviw98318de2021-05-19 16:45:23 -05005329 for (const sp<WindowInfoHandle>& oldWindowHandle : oldWindowHandles) {
Prabir Pradhan6a9a8312021-04-23 11:59:31 -07005330 if (getWindowHandleLocked(oldWindowHandle) == nullptr) {
Arthur Hung72d8dc32020-03-28 00:48:39 +00005331 if (DEBUG_FOCUS) {
5332 ALOGD("Window went away: %s", oldWindowHandle->getName().c_str());
Arthur Hung25e2af12020-03-26 12:58:37 +00005333 }
Arthur Hung72d8dc32020-03-28 00:48:39 +00005334 oldWindowHandle->releaseChannel();
Arthur Hung25e2af12020-03-26 12:58:37 +00005335 }
chaviw291d88a2019-02-14 10:33:58 -08005336 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337}
5338
5339void InputDispatcher::setFocusedApplication(
Chris Yea209fde2020-07-22 13:54:51 -07005340 int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005341 if (DEBUG_FOCUS) {
5342 ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId,
5343 inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>");
5344 }
Siarhei Vishniakoue41c4512020-09-08 19:35:58 -05005345 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005346 std::scoped_lock _l(mLock);
Vishnu Nair599f1412021-06-21 10:39:58 -07005347 setFocusedApplicationLocked(displayId, inputApplicationHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 } // release lock
5349
5350 // Wake up poll loop since it may need to make new input dispatching choices.
5351 mLooper->wake();
5352}
5353
Vishnu Nair599f1412021-06-21 10:39:58 -07005354void InputDispatcher::setFocusedApplicationLocked(
5355 int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
5356 std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle =
5357 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
5358
5359 if (sharedPointersEqual(oldFocusedApplicationHandle, inputApplicationHandle)) {
5360 return; // This application is already focused. No need to wake up or change anything.
5361 }
5362
5363 // Set the new application handle.
5364 if (inputApplicationHandle != nullptr) {
5365 mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
5366 } else {
5367 mFocusedApplicationHandlesByDisplay.erase(displayId);
5368 }
5369
5370 // No matter what the old focused application was, stop waiting on it because it is
5371 // no longer focused.
5372 resetNoFocusedWindowTimeoutLocked();
5373}
5374
Yeabkal Wubshitb8aadfa2024-01-17 17:03:42 -08005375void InputDispatcher::setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) {
5376 if (interval.count() < 0) {
5377 LOG_ALWAYS_FATAL("Minimum time between user activity pokes should be >= 0");
5378 }
5379 std::scoped_lock _l(mLock);
5380 mMinTimeBetweenUserActivityPokes = interval;
5381}
5382
Tiger Huang721e26f2018-07-24 22:26:19 +08005383/**
5384 * Sets the focused display, which is responsible for receiving focus-dispatched input events where
5385 * the display not specified.
5386 *
5387 * We track any unreleased events for each window. If a window loses the ability to receive the
5388 * released event, we will send a cancel event to it. So when the focused display is changed, we
5389 * cancel all the unreleased display-unspecified events for the focused window on the old focused
5390 * display. The display-specified events won't be affected.
5391 */
5392void InputDispatcher::setFocusedDisplay(int32_t displayId) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005393 if (DEBUG_FOCUS) {
5394 ALOGD("setFocusedDisplay displayId=%" PRId32, displayId);
5395 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005396 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005397 std::scoped_lock _l(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08005398
5399 if (mFocusedDisplayId != displayId) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005400 sp<IBinder> oldFocusedWindowToken =
Vishnu Nairc519ff72021-01-21 08:23:08 -08005401 mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Vishnu Nairad321cd2020-08-20 16:40:21 -07005402 if (oldFocusedWindowToken != nullptr) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005403 const auto windowHandle =
5404 getWindowHandleLocked(oldFocusedWindowToken, mFocusedDisplayId);
5405 if (windowHandle == nullptr) {
5406 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Tiger Huang721e26f2018-07-24 22:26:19 +08005407 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00005408 CancelationOptions
5409 options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
5410 "The display which contains this window no longer has focus.");
5411 options.displayId = ADISPLAY_ID_NONE;
5412 synthesizeCancelationEventsForWindowLocked(windowHandle, options);
Tiger Huang721e26f2018-07-24 22:26:19 +08005413 }
5414 mFocusedDisplayId = displayId;
5415
Chris Ye3c2d6f52020-08-09 10:39:48 -07005416 // Find new focused window and validate
Vishnu Nairc519ff72021-01-21 08:23:08 -08005417 sp<IBinder> newFocusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId);
Prabir Pradhancef936d2021-07-21 16:17:52 +00005418 sendFocusChangedCommandLocked(oldFocusedWindowToken, newFocusedWindowToken);
Robert Carrf759f162018-11-13 12:57:11 -08005419
Vishnu Nairad321cd2020-08-20 16:40:21 -07005420 if (newFocusedWindowToken == nullptr) {
Tiger Huang721e26f2018-07-24 22:26:19 +08005421 ALOGW("Focused display #%" PRId32 " does not have a focused window.", displayId);
Vishnu Nairc519ff72021-01-21 08:23:08 -08005422 if (mFocusResolver.hasFocusedWindowTokens()) {
Vishnu Nairad321cd2020-08-20 16:40:21 -07005423 ALOGE("But another display has a focused window\n%s",
Vishnu Nairc519ff72021-01-21 08:23:08 -08005424 mFocusResolver.dumpFocusedWindows().c_str());
Tiger Huang721e26f2018-07-24 22:26:19 +08005425 }
5426 }
5427 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005428 } // release lock
5429
5430 // Wake up poll loop since it may need to make new input dispatching choices.
5431 mLooper->wake();
5432}
5433
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005435 if (DEBUG_FOCUS) {
5436 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
5437 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438
5439 bool changed;
5440 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005441 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
5443 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
5444 if (mDispatchFrozen && !frozen) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005445 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446 }
5447
5448 if (mDispatchEnabled && !enabled) {
5449 resetAndDropEverythingLocked("dispatcher is being disabled");
5450 }
5451
5452 mDispatchEnabled = enabled;
5453 mDispatchFrozen = frozen;
5454 changed = true;
5455 } else {
5456 changed = false;
5457 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 } // release lock
5459
5460 if (changed) {
5461 // Wake up poll loop since it may need to make new input dispatching choices.
5462 mLooper->wake();
5463 }
5464}
5465
5466void InputDispatcher::setInputFilterEnabled(bool enabled) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005467 if (DEBUG_FOCUS) {
5468 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
5469 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470
5471 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005472 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 if (mInputFilterEnabled == enabled) {
5475 return;
5476 }
5477
5478 mInputFilterEnabled = enabled;
5479 resetAndDropEverythingLocked("input filter is being enabled or disabled");
5480 } // release lock
5481
5482 // Wake up poll loop since there might be work to do to drop everything.
5483 mLooper->wake();
5484}
5485
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005486bool InputDispatcher::setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid,
Prabir Pradhan8a5c41d2023-06-08 19:13:46 +00005487 bool hasPermission, int32_t displayId) {
Antonio Kantekf16f2832021-09-28 04:39:20 +00005488 bool needWake = false;
5489 {
5490 std::scoped_lock lock(mLock);
Antonio Kantek15beb512022-06-13 22:35:41 +00005491 ALOGD_IF(DEBUG_TOUCH_MODE,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005492 "Request to change touch mode to %s (calling pid=%s, uid=%s, "
Antonio Kantek15beb512022-06-13 22:35:41 +00005493 "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005494 toString(inTouchMode), pid.toString().c_str(), uid.toString().c_str(),
5495 toString(hasPermission), displayId,
Antonio Kantek15beb512022-06-13 22:35:41 +00005496 mTouchModePerDisplay.count(displayId) == 0
5497 ? "not set"
5498 : std::to_string(mTouchModePerDisplay[displayId]).c_str());
5499
Antonio Kantek15beb512022-06-13 22:35:41 +00005500 auto touchModeIt = mTouchModePerDisplay.find(displayId);
5501 if (touchModeIt != mTouchModePerDisplay.end() && touchModeIt->second == inTouchMode) {
Antonio Kantekea47acb2021-12-23 12:41:25 -08005502 return false;
Antonio Kantekf16f2832021-09-28 04:39:20 +00005503 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005504 if (!hasPermission) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005505 if (!focusedWindowIsOwnedByLocked(pid, uid) &&
5506 !recentWindowsAreOwnedByLocked(pid, uid)) {
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005507 ALOGD("Touch mode switch rejected, caller (pid=%s, uid=%s) doesn't own the focused "
Antonio Kantek48710e42022-03-24 14:19:30 -07005508 "window nor none of the previously interacted window",
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005509 pid.toString().c_str(), uid.toString().c_str());
Antonio Kantekea47acb2021-12-23 12:41:25 -08005510 return false;
5511 }
Antonio Kantekf16f2832021-09-28 04:39:20 +00005512 }
Antonio Kantek15beb512022-06-13 22:35:41 +00005513 mTouchModePerDisplay[displayId] = inTouchMode;
5514 auto entry = std::make_unique<TouchModeEntry>(mIdGenerator.nextId(), now(), inTouchMode,
5515 displayId);
Antonio Kantekf16f2832021-09-28 04:39:20 +00005516 needWake = enqueueInboundEventLocked(std::move(entry));
5517 } // release lock
5518
5519 if (needWake) {
5520 mLooper->wake();
5521 }
Antonio Kantekea47acb2021-12-23 12:41:25 -08005522 return true;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -08005523}
5524
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005525bool InputDispatcher::focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005526 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
5527 if (focusedToken == nullptr) {
5528 return false;
5529 }
5530 sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(focusedToken);
5531 return isWindowOwnedBy(windowHandle, pid, uid);
5532}
5533
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005534bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) {
Antonio Kantek48710e42022-03-24 14:19:30 -07005535 return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(),
5536 [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) {
5537 const sp<WindowInfoHandle> windowHandle =
5538 getWindowHandleLocked(connectionToken);
5539 return isWindowOwnedBy(windowHandle, pid, uid);
5540 }) != mInteractionConnectionTokens.end();
5541}
5542
Bernardo Rufinoea97d182020-08-19 14:43:14 +01005543void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
5544 if (opacity < 0 || opacity > 1) {
5545 LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
5546 return;
5547 }
5548
5549 std::scoped_lock lock(mLock);
5550 mMaximumObscuringOpacityForTouch = opacity;
5551}
5552
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005553std::tuple<TouchState*, TouchedWindow*, int32_t /*displayId*/>
5554InputDispatcher::findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) {
Arthur Hungabbb9d82021-09-01 14:52:30 +00005555 for (auto& [displayId, state] : mTouchStatesByDisplay) {
5556 for (TouchedWindow& w : state.windows) {
5557 if (w.windowHandle->getToken() == token) {
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005558 return std::make_tuple(&state, &w, displayId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005559 }
5560 }
5561 }
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005562 return std::make_tuple(nullptr, nullptr, ADISPLAY_ID_DEFAULT);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005563}
5564
arthurhungb89ccb02020-12-30 16:19:01 +08005565bool InputDispatcher::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
5566 bool isDragDrop) {
chaviwfbe5d9c2018-12-26 12:23:37 -08005567 if (fromToken == toToken) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005568 if (DEBUG_FOCUS) {
5569 ALOGD("Trivial transfer to same window.");
5570 }
chaviwfbe5d9c2018-12-26 12:23:37 -08005571 return true;
5572 }
5573
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005575 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
Arthur Hungabbb9d82021-09-01 14:52:30 +00005577 // Find the target touch state and touched window by fromToken.
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005578 auto [state, touchedWindow, displayId] = findTouchStateWindowAndDisplayLocked(fromToken);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005579
Arthur Hungabbb9d82021-09-01 14:52:30 +00005580 if (state == nullptr || touchedWindow == nullptr) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005581 ALOGD("Touch transfer failed because from window is not being touched.");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 return false;
5583 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005584 std::set<int32_t> deviceIds = touchedWindow->getTouchingDeviceIds();
5585 if (deviceIds.size() != 1) {
Siarhei Vishniakou827d1ac2023-07-21 16:37:51 -07005586 LOG(INFO) << "Can't transfer touch. Currently touching devices: " << dumpSet(deviceIds)
5587 << " for window: " << touchedWindow->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005588 return false;
5589 }
5590 const int32_t deviceId = *deviceIds.begin();
Arthur Hungabbb9d82021-09-01 14:52:30 +00005591
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005592 const sp<WindowInfoHandle> fromWindowHandle = touchedWindow->windowHandle;
5593 const sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(toToken, displayId);
5594 if (!toWindowHandle) {
5595 ALOGW("Cannot transfer touch because the transfer target window was not found.");
Arthur Hungabbb9d82021-09-01 14:52:30 +00005596 return false;
5597 }
5598
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005599 if (DEBUG_FOCUS) {
5600 ALOGD("transferTouchFocus: fromWindowHandle=%s, toWindowHandle=%s",
Arthur Hungabbb9d82021-09-01 14:52:30 +00005601 touchedWindow->windowHandle->getName().c_str(),
5602 toWindowHandle->getName().c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 }
5604
Arthur Hungabbb9d82021-09-01 14:52:30 +00005605 // Erase old window.
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005606 ftl::Flags<InputTarget::Flags> oldTargetFlags = touchedWindow->targetFlags;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005607 std::vector<PointerProperties> pointers = touchedWindow->getTouchingPointers(deviceId);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005608 state->removeWindowByToken(fromToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609
Arthur Hungabbb9d82021-09-01 14:52:30 +00005610 // Add new window.
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +00005611 nsecs_t downTimeInTarget = now();
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005612 ftl::Flags<InputTarget::Flags> newTargetFlags =
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005613 oldTargetFlags & (InputTarget::Flags::SPLIT);
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005614 if (canReceiveForegroundTouches(*toWindowHandle->getInfo())) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005615 newTargetFlags |= InputTarget::Flags::FOREGROUND;
Prabir Pradhan6dfbf262022-03-14 15:24:30 +00005616 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00005617 // Transferring touch focus using this API should not effect the focused window.
5618 newTargetFlags |= InputTarget::Flags::NO_FOCUS_CHANGE;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00005619 state->addOrUpdateWindow(toWindowHandle, InputTarget::DispatchMode::AS_IS, newTargetFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005620 deviceId, pointers, downTimeInTarget);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621
Arthur Hungabbb9d82021-09-01 14:52:30 +00005622 // Store the dragging window.
5623 if (isDragDrop) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005624 if (pointers.size() != 1) {
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005625 ALOGW("The drag and drop cannot be started when there is no pointer or more than 1"
5626 " pointer on the window.");
Arthur Hung54745652022-04-20 07:17:41 +00005627 return false;
5628 }
Arthur Hungb75c2aa2022-07-15 09:35:36 +00005629 // Track the pointer id for drag window and generate the drag state.
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005630 const size_t id = pointers.begin()->id;
Arthur Hung54745652022-04-20 07:17:41 +00005631 mDragState = std::make_unique<DragState>(toWindowHandle, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632 }
5633
Arthur Hungabbb9d82021-09-01 14:52:30 +00005634 // Synthesize cancel for old window and down for new window.
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005635 std::shared_ptr<Connection> fromConnection = getConnectionLocked(fromToken);
5636 std::shared_ptr<Connection> toConnection = getConnectionLocked(toToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07005637 if (fromConnection != nullptr && toConnection != nullptr) {
Svet Ganov5d3bc372020-01-26 23:11:07 -08005638 fromConnection->inputState.mergePointerStateTo(toConnection->inputState);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07005639 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
5640 "transferring touch from this window to another window");
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00005641 synthesizeCancelationEventsForWindowLocked(fromWindowHandle, options, fromConnection);
Arthur Hungc539dbb2022-12-08 07:45:36 +00005642 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, toConnection,
5643 newTargetFlags);
5644
5645 // Check if the wallpaper window should deliver the corresponding event.
5646 transferWallpaperTouch(oldTargetFlags, newTargetFlags, fromWindowHandle, toWindowHandle,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08005647 *state, deviceId, pointers);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 } // release lock
5650
5651 // Wake up poll loop since it may need to make new input dispatching choices.
5652 mLooper->wake();
5653 return true;
5654}
5655
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005656/**
5657 * Get the touched foreground window on the given display.
5658 * Return null if there are no windows touched on that display, or if more than one foreground
5659 * window is being touched.
5660 */
5661sp<WindowInfoHandle> InputDispatcher::findTouchedForegroundWindowLocked(int32_t displayId) const {
5662 auto stateIt = mTouchStatesByDisplay.find(displayId);
5663 if (stateIt == mTouchStatesByDisplay.end()) {
5664 ALOGI("No touch state on display %" PRId32, displayId);
5665 return nullptr;
5666 }
5667
5668 const TouchState& state = stateIt->second;
5669 sp<WindowInfoHandle> touchedForegroundWindow;
5670 // If multiple foreground windows are touched, return nullptr
5671 for (const TouchedWindow& window : state.windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -08005672 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005673 if (touchedForegroundWindow != nullptr) {
5674 ALOGI("Two or more foreground windows: %s and %s",
5675 touchedForegroundWindow->getName().c_str(),
5676 window.windowHandle->getName().c_str());
5677 return nullptr;
5678 }
5679 touchedForegroundWindow = window.windowHandle;
5680 }
5681 }
5682 return touchedForegroundWindow;
5683}
5684
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005685// Binder call
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005686bool InputDispatcher::transferTouch(const sp<IBinder>& destChannelToken, int32_t displayId) {
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005687 sp<IBinder> fromToken;
5688 { // acquire lock
5689 std::scoped_lock _l(mLock);
Arthur Hungabbb9d82021-09-01 14:52:30 +00005690 sp<WindowInfoHandle> toWindowHandle = getWindowHandleLocked(destChannelToken, displayId);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005691 if (toWindowHandle == nullptr) {
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005692 ALOGW("Could not find window associated with token=%p on display %" PRId32,
5693 destChannelToken.get(), displayId);
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005694 return false;
5695 }
5696
Siarhei Vishniakou7ae7afd2022-03-31 15:26:13 -07005697 sp<WindowInfoHandle> from = findTouchedForegroundWindowLocked(displayId);
5698 if (from == nullptr) {
5699 ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get());
5700 return false;
5701 }
5702
5703 fromToken = from->getToken();
Siarhei Vishniakoud0c6bc82021-03-13 03:14:52 +00005704 } // release lock
5705
5706 return transferTouchFocus(fromToken, destChannelToken);
5707}
5708
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
Siarhei Vishniakou86587282019-09-09 18:20:15 +01005710 if (DEBUG_FOCUS) {
5711 ALOGD("Resetting and dropping all events (%s).", reason);
5712 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
Michael Wrightfb04fd52022-11-24 22:31:11 +00005714 CancelationOptions options(CancelationOptions::Mode::CANCEL_ALL_EVENTS, reason);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 synthesizeCancelationEventsForAllConnectionsLocked(options);
5716
5717 resetKeyRepeatLocked();
5718 releasePendingEventLocked();
5719 drainInboundQueueLocked();
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005720 resetNoFocusedWindowTimeoutLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005722 mAnrTracker.clear();
Jeff Brownf086ddb2014-02-11 14:28:48 -08005723 mTouchStatesByDisplay.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724}
5725
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005726void InputDispatcher::logDispatchStateLocked() const {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005727 std::string dump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728 dumpDispatchStateLocked(dump);
5729
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005730 std::istringstream stream(dump);
5731 std::string line;
5732
5733 while (std::getline(stream, line, '\n')) {
Siarhei Vishniakoua235c042023-05-02 09:59:09 -07005734 ALOGI("%s", line.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 }
5736}
5737
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005738std::string InputDispatcher::dumpPointerCaptureStateLocked() const {
Prabir Pradhan99987712020-11-10 18:43:05 -08005739 std::string dump;
5740
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005741 dump += StringPrintf(INDENT "Pointer Capture Requested: %s\n",
5742 toString(mCurrentPointerCaptureRequest.enable));
Prabir Pradhan99987712020-11-10 18:43:05 -08005743
5744 std::string windowName = "None";
5745 if (mWindowTokenWithPointerCapture) {
chaviw98318de2021-05-19 16:45:23 -05005746 const sp<WindowInfoHandle> captureWindowHandle =
Prabir Pradhan99987712020-11-10 18:43:05 -08005747 getWindowHandleLocked(mWindowTokenWithPointerCapture);
5748 windowName = captureWindowHandle ? captureWindowHandle->getName().c_str()
5749 : "token has capture without window";
5750 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00005751 dump += StringPrintf(INDENT "Current Window with Pointer Capture: %s\n", windowName.c_str());
Prabir Pradhan99987712020-11-10 18:43:05 -08005752
5753 return dump;
5754}
5755
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005756void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
Siarhei Vishniakou043a3ec2019-05-01 11:30:46 -07005757 dump += StringPrintf(INDENT "DispatchEnabled: %s\n", toString(mDispatchEnabled));
5758 dump += StringPrintf(INDENT "DispatchFrozen: %s\n", toString(mDispatchFrozen));
5759 dump += StringPrintf(INDENT "InputFilterEnabled: %s\n", toString(mInputFilterEnabled));
Tiger Huang721e26f2018-07-24 22:26:19 +08005760 dump += StringPrintf(INDENT "FocusedDisplayId: %" PRId32 "\n", mFocusedDisplayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761
Tiger Huang721e26f2018-07-24 22:26:19 +08005762 if (!mFocusedApplicationHandlesByDisplay.empty()) {
5763 dump += StringPrintf(INDENT "FocusedApplications:\n");
5764 for (auto& it : mFocusedApplicationHandlesByDisplay) {
5765 const int32_t displayId = it.first;
Chris Yea209fde2020-07-22 13:54:51 -07005766 const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second;
Siarhei Vishniakou70622952020-07-30 11:17:23 -05005767 const std::chrono::duration timeout =
5768 applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07005769 dump += StringPrintf(INDENT2 "displayId=%" PRId32
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005770 ", name='%s', dispatchingTimeout=%" PRId64 "ms\n",
Siarhei Vishniakou70622952020-07-30 11:17:23 -05005771 displayId, applicationHandle->getName().c_str(), millis(timeout));
Tiger Huang721e26f2018-07-24 22:26:19 +08005772 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 } else {
Tiger Huang721e26f2018-07-24 22:26:19 +08005774 dump += StringPrintf(INDENT "FocusedApplications: <none>\n");
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 }
Tiger Huang721e26f2018-07-24 22:26:19 +08005776
Vishnu Nairc519ff72021-01-21 08:23:08 -08005777 dump += mFocusResolver.dump();
Prabir Pradhan99987712020-11-10 18:43:05 -08005778 dump += dumpPointerCaptureStateLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779
Siarhei Vishniakou4700f822020-03-24 19:05:54 -07005780 if (!mTouchStatesByDisplay.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005781 dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07005782 for (const auto& [displayId, state] : mTouchStatesByDisplay) {
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -08005783 std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
5784 dump += INDENT2 + std::to_string(displayId) + " : " + touchStateDump;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785 }
5786 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005787 dump += INDENT "TouchStates: <no displays touched>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788 }
5789
arthurhung6d4bed92021-03-17 11:59:33 +08005790 if (mDragState) {
5791 dump += StringPrintf(INDENT "DragState:\n");
5792 mDragState->dump(dump, INDENT2);
5793 }
5794
Arthur Hungb92218b2018-08-14 12:00:21 +08005795 if (!mWindowHandlesByDisplay.empty()) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07005796 for (const auto& [displayId, windowHandles] : mWindowHandlesByDisplay) {
5797 dump += StringPrintf(INDENT "Display: %" PRId32 "\n", displayId);
5798 if (const auto& it = mDisplayInfos.find(displayId); it != mDisplayInfos.end()) {
5799 const auto& displayInfo = it->second;
5800 dump += StringPrintf(INDENT2 "logicalSize=%dx%d\n", displayInfo.logicalWidth,
5801 displayInfo.logicalHeight);
5802 displayInfo.transform.dump(dump, "transform", INDENT4);
5803 } else {
5804 dump += INDENT2 "No DisplayInfo found!\n";
5805 }
5806
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005807 if (!windowHandles.empty()) {
Arthur Hungb92218b2018-08-14 12:00:21 +08005808 dump += INDENT2 "Windows:\n";
5809 for (size_t i = 0; i < windowHandles.size(); i++) {
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005810 dump += StringPrintf(INDENT3 "%zu: %s", i,
5811 streamableToString(*windowHandles[i]).c_str());
Arthur Hungb92218b2018-08-14 12:00:21 +08005812 }
5813 } else {
5814 dump += INDENT2 "Windows: <none>\n";
5815 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 }
5817 } else {
Arthur Hungb92218b2018-08-14 12:00:21 +08005818 dump += INDENT "Displays: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 }
5820
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005821 if (!mGlobalMonitorsByDisplay.empty()) {
5822 for (const auto& [displayId, monitors] : mGlobalMonitorsByDisplay) {
5823 dump += StringPrintf(INDENT "Global monitors on display %d:\n", displayId);
Michael Wright3dd60e22019-03-27 22:06:44 +00005824 dumpMonitors(dump, monitors);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07005825 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826 } else {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005827 dump += INDENT "Global Monitors: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 }
5829
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08005830 const nsecs_t currentTime = now();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831
5832 // Dump recently dispatched or dropped events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005833 if (!mRecentQueue.empty()) {
5834 dump += StringPrintf(INDENT "RecentQueue: length=%zu\n", mRecentQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005835 for (const std::shared_ptr<const EventEntry>& entry : mRecentQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005836 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005837 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005838 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 }
5840 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005841 dump += INDENT "RecentQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 }
5843
5844 // Dump event currently being dispatched.
5845 if (mPendingEvent) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005846 dump += INDENT "PendingEvent:\n";
5847 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005848 dump += mPendingEvent->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005849 dump += StringPrintf(", age=%" PRId64 "ms\n",
5850 ns2ms(currentTime - mPendingEvent->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005852 dump += INDENT "PendingEvent: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 }
5854
5855 // Dump inbound events from oldest to newest.
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07005856 if (!mInboundQueue.empty()) {
5857 dump += StringPrintf(INDENT "InboundQueue: length=%zu\n", mInboundQueue.size());
Prabir Pradhan24047542023-11-02 17:14:59 +00005858 for (const std::shared_ptr<const EventEntry>& entry : mInboundQueue) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005859 dump += INDENT2;
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005860 dump += entry->getDescription();
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005861 dump += StringPrintf(", age=%" PRId64 "ms\n", ns2ms(currentTime - entry->eventTime));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 }
5863 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005864 dump += INDENT "InboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 }
5866
Prabir Pradhancef936d2021-07-21 16:17:52 +00005867 if (!mCommandQueue.empty()) {
5868 dump += StringPrintf(INDENT "CommandQueue: size=%zu\n", mCommandQueue.size());
5869 } else {
5870 dump += INDENT "CommandQueue: <empty>\n";
5871 }
5872
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005873 if (!mConnectionsByToken.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005874 dump += INDENT "Connections:\n";
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005875 for (const auto& [token, connection] : mConnectionsByToken) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08005876 dump += StringPrintf(INDENT2 "%i: channelName='%s', "
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005877 "status=%s, monitor=%s, responsive=%s\n",
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08005878 connection->inputPublisher.getChannel().getFd(),
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005879 connection->getInputChannelName().c_str(),
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08005880 ftl::enum_string(connection->status).c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07005881 toString(connection->monitor), toString(connection->responsive));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07005883 if (!connection->outboundQueue.empty()) {
5884 dump += StringPrintf(INDENT3 "OutboundQueue: length=%zu\n",
5885 connection->outboundQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005886 dump += dumpQueue(connection->outboundQueue, currentTime);
5887
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005889 dump += INDENT3 "OutboundQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 }
5891
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07005892 if (!connection->waitQueue.empty()) {
5893 dump += StringPrintf(INDENT3 "WaitQueue: length=%zu\n",
5894 connection->waitQueue.size());
Siarhei Vishniakou14411c92020-09-18 21:15:05 -05005895 dump += dumpQueue(connection->waitQueue, currentTime);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005897 dump += INDENT3 "WaitQueue: <empty>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 }
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005899 std::string inputStateDump = streamableToString(connection->inputState);
5900 if (!inputStateDump.empty()) {
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07005901 dump += INDENT3 "InputState: ";
Siarhei Vishniakou366fb5b2023-12-06 11:23:41 -08005902 dump += inputStateDump + "\n";
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -07005903 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 }
5905 } else {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005906 dump += INDENT "Connections: <none>\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 }
5908
Antonio Kantek15beb512022-06-13 22:35:41 +00005909 if (!mTouchModePerDisplay.empty()) {
5910 dump += INDENT "TouchModePerDisplay:\n";
5911 for (const auto& [displayId, touchMode] : mTouchModePerDisplay) {
5912 dump += StringPrintf(INDENT2 "Display: %" PRId32 " TouchMode: %s\n", displayId,
5913 std::to_string(touchMode).c_str());
5914 }
5915 } else {
5916 dump += INDENT "TouchModePerDisplay: <none>\n";
5917 }
5918
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08005919 dump += INDENT "Configuration:\n";
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07005920 dump += StringPrintf(INDENT2 "KeyRepeatDelay: %" PRId64 "ms\n", ns2ms(mConfig.keyRepeatDelay));
5921 dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n",
5922 ns2ms(mConfig.keyRepeatTimeout));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00005923 dump += mLatencyTracker.dump(INDENT2);
Siarhei Vishniakoua04181f2021-03-26 05:56:49 +00005924 dump += mLatencyAggregator.dump(INDENT2);
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +00005925 dump += INDENT "InputTracer: ";
5926 dump += mTracer == nullptr ? "Disabled" : "Enabled";
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927}
5928
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -07005929void InputDispatcher::dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const {
Michael Wright3dd60e22019-03-27 22:06:44 +00005930 const size_t numMonitors = monitors.size();
5931 for (size_t i = 0; i < numMonitors; i++) {
5932 const Monitor& monitor = monitors[i];
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005933 const std::shared_ptr<Connection>& connection = monitor.connection;
5934 dump += StringPrintf(INDENT2 "%zu: '%s', ", i, connection->getInputChannelName().c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +00005935 dump += "\n";
5936 }
5937}
5938
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005939class LooperEventCallback : public LooperCallback {
5940public:
5941 LooperEventCallback(std::function<int(int events)> callback) : mCallback(callback) {}
5942 int handleEvent(int /*fd*/, int events, void* /*data*/) override { return mCallback(events); }
5943
5944private:
5945 std::function<int(int events)> mCallback;
5946};
5947
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00005948Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00005949 if (DEBUG_CHANNEL_CREATION) {
5950 ALOGD("channel '%s' ~ createInputChannel", name.c_str());
5951 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005953 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07005954 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005955 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07005956
5957 if (result) {
5958 return base::Error(result) << "Failed to open input channel pair with name " << name;
5959 }
5960
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08005962 std::scoped_lock _l(mLock);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005963 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08005964 const int fd = serverChannel->getFd();
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07005965 std::shared_ptr<Connection> connection =
5966 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/false,
5967 mIdGenerator);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08005969 auto [_, inserted] = mConnectionsByToken.try_emplace(token, connection);
5970 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005971 ALOGE("Created a new connection, but the token %p is already known", token.get());
5972 }
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00005973
5974 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
5975 this, std::placeholders::_1, token);
5976
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08005977 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07005978 nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 } // release lock
5980
5981 // Wake the looper because some connections have changed.
5982 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07005983 return clientChannel;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984}
5985
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00005986Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputMonitor(int32_t displayId,
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +00005987 const std::string& name,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00005988 gui::Pid pid) {
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08005989 std::unique_ptr<InputChannel> serverChannel;
Garfield Tan15601662020-09-22 15:32:38 -07005990 std::unique_ptr<InputChannel> clientChannel;
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08005991 status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
Garfield Tan15601662020-09-22 15:32:38 -07005992 if (result) {
5993 return base::Error(result) << "Failed to open input channel pair with name " << name;
5994 }
5995
Michael Wright3dd60e22019-03-27 22:06:44 +00005996 { // acquire lock
5997 std::scoped_lock _l(mLock);
5998
5999 if (displayId < 0) {
Garfield Tan15601662020-09-22 15:32:38 -07006000 return base::Error(BAD_VALUE) << "Attempted to create input monitor with name " << name
6001 << " without a specified display.";
Michael Wright3dd60e22019-03-27 22:06:44 +00006002 }
6003
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006004 const sp<IBinder>& token = serverChannel->getConnectionToken();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006005 const int fd = serverChannel->getFd();
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006006 std::shared_ptr<Connection> connection =
6007 std::make_shared<Connection>(std::move(serverChannel), /*monitor=*/true,
6008 mIdGenerator);
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006009
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006010 auto [_, inserted] = mConnectionsByToken.emplace(token, connection);
6011 if (!inserted) {
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006012 ALOGE("Created a new connection, but the token %p is already known", token.get());
6013 }
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006014
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006015 std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
6016 this, std::placeholders::_1, token);
Michael Wright3dd60e22019-03-27 22:06:44 +00006017
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006018 mGlobalMonitorsByDisplay[displayId].emplace_back(connection, pid);
Michael Wright3dd60e22019-03-27 22:06:44 +00006019
Siarhei Vishniakou8d660132024-01-11 16:48:44 -08006020 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, sp<LooperEventCallback>::make(callback),
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006021 nullptr);
Michael Wright3dd60e22019-03-27 22:06:44 +00006022 }
Garfield Tan15601662020-09-22 15:32:38 -07006023
Michael Wright3dd60e22019-03-27 22:06:44 +00006024 // Wake the looper because some connections have changed.
6025 mLooper->wake();
Garfield Tan15601662020-09-22 15:32:38 -07006026 return clientChannel;
Michael Wright3dd60e22019-03-27 22:06:44 +00006027}
6028
Garfield Tan15601662020-09-22 15:32:38 -07006029status_t InputDispatcher::removeInputChannel(const sp<IBinder>& connectionToken) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 { // acquire lock
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006031 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032
Harry Cutts33476232023-01-30 19:57:29 +00006033 status_t status = removeInputChannelLocked(connectionToken, /*notify=*/false);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 if (status) {
6035 return status;
6036 }
6037 } // release lock
6038
6039 // Wake the poll loop because removing the connection may have changed the current
6040 // synchronization state.
6041 mLooper->wake();
6042 return OK;
6043}
6044
Garfield Tan15601662020-09-22 15:32:38 -07006045status_t InputDispatcher::removeInputChannelLocked(const sp<IBinder>& connectionToken,
6046 bool notify) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006047 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006048 if (connection == nullptr) {
Siarhei Vishniakoud706a282021-02-13 00:08:48 +00006049 // Connection can be removed via socket hang up or an explicit call to 'removeInputChannel'
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 return BAD_VALUE;
6051 }
6052
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006053 removeConnectionLocked(connection);
Robert Carr5c8a0262018-10-03 16:30:44 -07006054
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 if (connection->monitor) {
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006056 removeMonitorChannelLocked(connectionToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 }
6058
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -08006059 mLooper->removeFd(connection->inputPublisher.getChannel().getFd());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060
6061 nsecs_t currentTime = now();
6062 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
6063
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006064 connection->status = Connection::Status::ZOMBIE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 return OK;
6066}
6067
Siarhei Vishniakouadefc3e2020-09-02 22:28:29 -05006068void InputDispatcher::removeMonitorChannelLocked(const sp<IBinder>& connectionToken) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006069 for (auto it = mGlobalMonitorsByDisplay.begin(); it != mGlobalMonitorsByDisplay.end();) {
6070 auto& [displayId, monitors] = *it;
6071 std::erase_if(monitors, [connectionToken](const Monitor& monitor) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006072 return monitor.connection->getToken() == connectionToken;
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006073 });
Michael Wright3dd60e22019-03-27 22:06:44 +00006074
Michael Wright3dd60e22019-03-27 22:06:44 +00006075 if (monitors.empty()) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006076 it = mGlobalMonitorsByDisplay.erase(it);
Arthur Hung2fbf37f2018-09-13 18:16:41 +08006077 } else {
6078 ++it;
6079 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 }
6081}
6082
Michael Wright3dd60e22019-03-27 22:06:44 +00006083status_t InputDispatcher::pilferPointers(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006084 std::scoped_lock _l(mLock);
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006085 return pilferPointersLocked(token);
6086}
Michael Wright3dd60e22019-03-27 22:06:44 +00006087
Vaibhav Devmurari6abcf8f2022-06-06 10:08:05 +00006088status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006089 const std::shared_ptr<Connection> requestingConnection = getConnectionLocked(token);
6090 if (!requestingConnection) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006091 LOG(WARNING)
6092 << "Attempted to pilfer pointers from an un-registered channel or invalid token";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006093 return BAD_VALUE;
Michael Wright3dd60e22019-03-27 22:06:44 +00006094 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006095
Siarhei Vishniakou40b8fbd2022-11-04 10:50:26 -07006096 auto [statePtr, windowPtr, displayId] = findTouchStateWindowAndDisplayLocked(token);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006097 if (statePtr == nullptr || windowPtr == nullptr) {
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006098 LOG(WARNING)
6099 << "Attempted to pilfer points from a channel without any on-going pointer streams."
6100 " Ignoring.";
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006101 return BAD_VALUE;
6102 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006103 std::set<int32_t> deviceIds = windowPtr->getTouchingDeviceIds();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -07006104 if (deviceIds.empty()) {
6105 LOG(WARNING) << "Can't pilfer: no touching devices in window: " << windowPtr->dump();
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006106 return BAD_VALUE;
6107 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006108
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006109 for (const DeviceId deviceId : deviceIds) {
6110 TouchState& state = *statePtr;
6111 TouchedWindow& window = *windowPtr;
6112 // Send cancel events to all the input channels we're stealing from.
6113 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
6114 "input channel stole pointer stream");
6115 options.deviceId = deviceId;
6116 options.displayId = displayId;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006117 std::vector<PointerProperties> pointers = window.getTouchingPointers(deviceId);
6118 std::bitset<MAX_POINTER_ID + 1> pointerIds = getPointerIds(pointers);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006119 options.pointerIds = pointerIds;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006120
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006121 std::string canceledWindows;
6122 for (const TouchedWindow& w : state.windows) {
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006123 if (w.windowHandle->getToken() != token) {
6124 synthesizeCancelationEventsForWindowLocked(w.windowHandle, options);
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006125 canceledWindows += canceledWindows.empty() ? "[" : ", ";
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006126 canceledWindows += w.windowHandle->getName();
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006127 }
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006128 }
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006129 canceledWindows += canceledWindows.empty() ? "[]" : "]";
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006130 LOG(INFO) << "Channel " << requestingConnection->getInputChannelName()
Siarhei Vishniakou8384e0d2023-09-18 18:48:27 -07006131 << " is stealing input gesture for device " << deviceId << " from "
6132 << canceledWindows;
6133
6134 // Prevent the gesture from being sent to any other windows.
6135 // This only blocks relevant pointers to be sent to other windows
6136 window.addPilferingPointers(deviceId, pointerIds);
6137
6138 state.cancelPointersForWindowsExcept(deviceId, pointerIds, token);
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006139 }
Michael Wright3dd60e22019-03-27 22:06:44 +00006140 return OK;
6141}
6142
Prabir Pradhan99987712020-11-10 18:43:05 -08006143void InputDispatcher::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) {
6144 { // acquire lock
6145 std::scoped_lock _l(mLock);
6146 if (DEBUG_FOCUS) {
chaviw98318de2021-05-19 16:45:23 -05006147 const sp<WindowInfoHandle> windowHandle = getWindowHandleLocked(windowToken);
Prabir Pradhan99987712020-11-10 18:43:05 -08006148 ALOGI("Request to %s Pointer Capture from: %s.", enabled ? "enable" : "disable",
6149 windowHandle != nullptr ? windowHandle->getName().c_str()
6150 : "token without window");
6151 }
6152
Vishnu Nairc519ff72021-01-21 08:23:08 -08006153 const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId);
Prabir Pradhan99987712020-11-10 18:43:05 -08006154 if (focusedToken != windowToken) {
6155 ALOGW("Ignoring request to %s Pointer Capture: window does not have focus.",
6156 enabled ? "enable" : "disable");
6157 return;
6158 }
6159
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006160 if (enabled == mCurrentPointerCaptureRequest.enable) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006161 ALOGW("Ignoring request to %s Pointer Capture: "
6162 "window has %s requested pointer capture.",
6163 enabled ? "enable" : "disable", enabled ? "already" : "not");
6164 return;
6165 }
6166
Christine Franksb768bb42021-11-29 12:11:31 -08006167 if (enabled) {
6168 if (std::find(mIneligibleDisplaysForPointerCapture.begin(),
6169 mIneligibleDisplaysForPointerCapture.end(),
6170 mFocusedDisplayId) != mIneligibleDisplaysForPointerCapture.end()) {
6171 ALOGW("Ignoring request to enable Pointer Capture: display is not eligible");
6172 return;
6173 }
6174 }
6175
Prabir Pradhan99987712020-11-10 18:43:05 -08006176 setPointerCaptureLocked(enabled);
6177 } // release lock
6178
6179 // Wake the thread to process command entries.
6180 mLooper->wake();
6181}
6182
Christine Franksb768bb42021-11-29 12:11:31 -08006183void InputDispatcher::setDisplayEligibilityForPointerCapture(int32_t displayId, bool isEligible) {
6184 { // acquire lock
6185 std::scoped_lock _l(mLock);
6186 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
6187 if (!isEligible) {
6188 mIneligibleDisplaysForPointerCapture.push_back(displayId);
6189 }
6190 } // release lock
6191}
6192
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006193std::optional<gui::Pid> InputDispatcher::findMonitorPidByTokenLocked(const sp<IBinder>& token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006194 for (const auto& [_, monitors] : mGlobalMonitorsByDisplay) {
Michael Wright3dd60e22019-03-27 22:06:44 +00006195 for (const Monitor& monitor : monitors) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006196 if (monitor.connection->getToken() == token) {
Prabir Pradhandfabf8a2022-01-21 08:19:30 -08006197 return monitor.pid;
Michael Wright3dd60e22019-03-27 22:06:44 +00006198 }
6199 }
6200 }
6201 return std::nullopt;
6202}
6203
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006204std::shared_ptr<Connection> InputDispatcher::getConnectionLocked(
6205 const sp<IBinder>& inputConnectionToken) const {
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -07006206 if (inputConnectionToken == nullptr) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006207 return nullptr;
Arthur Hung3b413f22018-10-26 18:05:34 +08006208 }
6209
Siarhei Vishniakouae02a1f2021-05-01 23:14:04 +00006210 for (const auto& [token, connection] : mConnectionsByToken) {
6211 if (token == inputConnectionToken) {
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006212 return connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213 }
6214 }
Robert Carr4e670e52018-08-15 13:26:12 -07006215
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -07006216 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217}
6218
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006219std::string InputDispatcher::getConnectionNameLocked(const sp<IBinder>& connectionToken) const {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006220 std::shared_ptr<Connection> connection = getConnectionLocked(connectionToken);
Siarhei Vishniakouad991402020-10-28 11:40:09 -05006221 if (connection == nullptr) {
6222 return "<nullptr>";
6223 }
6224 return connection->getInputChannelName();
6225}
6226
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006227void InputDispatcher::removeConnectionLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006228 mAnrTracker.eraseToken(connection->getToken());
6229 mConnectionsByToken.erase(connection->getToken());
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -07006230}
6231
Prabir Pradhancef936d2021-07-21 16:17:52 +00006232void InputDispatcher::doDispatchCycleFinishedCommand(nsecs_t finishTime,
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006233 const std::shared_ptr<Connection>& connection,
6234 uint32_t seq, bool handled,
6235 nsecs_t consumeTime) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006236 // Handle post-event policy actions.
Prabir Pradhan24047542023-11-02 17:14:59 +00006237 std::unique_ptr<const KeyEntry> fallbackKeyEntry;
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006238
6239 { // Start critical section
6240 auto dispatchEntryIt =
6241 std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6242 [seq](auto& e) { return e->seq == seq; });
6243 if (dispatchEntryIt == connection->waitQueue.end()) {
6244 return;
6245 }
6246
6247 DispatchEntry& dispatchEntry = **dispatchEntryIt;
6248
6249 const nsecs_t eventDuration = finishTime - dispatchEntry.deliveryTime;
6250 if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006251 ALOGI("%s spent %" PRId64 "ms processing %s", connection->getInputChannelName().c_str(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006252 ns2ms(eventDuration), dispatchEntry.eventEntry->getDescription().c_str());
6253 }
6254 if (shouldReportFinishedEvent(dispatchEntry, *connection)) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006255 mLatencyTracker.trackFinishedEvent(dispatchEntry.eventEntry->id, connection->getToken(),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006256 dispatchEntry.deliveryTime, consumeTime, finishTime);
6257 }
6258
6259 if (dispatchEntry.eventEntry->type == EventEntry::Type::KEY) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006260 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*(dispatchEntry.eventEntry));
6261 fallbackKeyEntry =
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006262 afterKeyEventLockedInterruptable(connection, dispatchEntry, keyEntry, handled);
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006263 }
6264 } // End critical section: The -LockedInterruptable methods may have released the lock.
Prabir Pradhancef936d2021-07-21 16:17:52 +00006265
6266 // Dequeue the event and start the next cycle.
6267 // Because the lock might have been released, it is possible that the
6268 // contents of the wait queue to have been drained, so we need to double-check
6269 // a few things.
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006270 auto entryIt = std::find_if(connection->waitQueue.begin(), connection->waitQueue.end(),
6271 [seq](auto& e) { return e->seq == seq; });
6272 if (entryIt != connection->waitQueue.end()) {
6273 std::unique_ptr<DispatchEntry> dispatchEntry = std::move(*entryIt);
6274 connection->waitQueue.erase(entryIt);
6275
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006276 const sp<IBinder>& connectionToken = connection->getToken();
Prabir Pradhancef936d2021-07-21 16:17:52 +00006277 mAnrTracker.erase(dispatchEntry->timeoutTime, connectionToken);
6278 if (!connection->responsive) {
6279 connection->responsive = isConnectionResponsive(*connection);
6280 if (connection->responsive) {
6281 // The connection was unresponsive, and now it's responsive.
6282 processConnectionResponsiveLocked(*connection);
6283 }
6284 }
6285 traceWaitQueueLength(*connection);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006286 if (fallbackKeyEntry && connection->status == Connection::Status::NORMAL) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006287 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6288 // Only dispatch fallbacks if there is a window for the connection.
6289 if (windowHandle != nullptr) {
6290 const auto inputTarget =
6291 createInputTargetLocked(windowHandle, InputTarget::DispatchMode::AS_IS,
6292 dispatchEntry->targetFlags,
6293 fallbackKeyEntry->downTime);
6294 if (inputTarget.has_value()) {
6295 enqueueDispatchEntryLocked(connection, std::move(fallbackKeyEntry),
6296 *inputTarget);
6297 }
6298 }
Prabir Pradhancef936d2021-07-21 16:17:52 +00006299 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006300 releaseDispatchEntry(std::move(dispatchEntry));
Prabir Pradhancef936d2021-07-21 16:17:52 +00006301 }
6302
6303 // Start the next dispatch cycle for this connection.
6304 startDispatchCycleLocked(now(), connection);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305}
6306
Prabir Pradhancef936d2021-07-21 16:17:52 +00006307void InputDispatcher::sendFocusChangedCommandLocked(const sp<IBinder>& oldToken,
6308 const sp<IBinder>& newToken) {
6309 auto command = [this, oldToken, newToken]() REQUIRES(mLock) {
6310 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006311 mPolicy.notifyFocusChanged(oldToken, newToken);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006312 };
6313 postCommandLocked(std::move(command));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314}
6315
Prabir Pradhancef936d2021-07-21 16:17:52 +00006316void InputDispatcher::sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) {
6317 auto command = [this, token, x, y]() REQUIRES(mLock) {
6318 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006319 mPolicy.notifyDropWindow(token, x, y);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006320 };
6321 postCommandLocked(std::move(command));
Robert Carrf759f162018-11-13 12:57:11 -08006322}
6323
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07006324void InputDispatcher::onAnrLocked(const std::shared_ptr<Connection>& connection) {
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006325 if (connection == nullptr) {
6326 LOG_ALWAYS_FATAL("Caller must check for nullness");
6327 }
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006328 // Since we are allowing the policy to extend the timeout, maybe the waitQueue
6329 // is already healthy again. Don't raise ANR in this situation
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006330 if (connection->waitQueue.empty()) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006331 ALOGI("Not raising ANR because the connection %s has recovered",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006332 connection->getInputChannelName().c_str());
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006333 return;
6334 }
6335 /**
6336 * The "oldestEntry" is the entry that was first sent to the application. That entry, however,
6337 * may not be the one that caused the timeout to occur. One possibility is that window timeout
6338 * has changed. This could cause newer entries to time out before the already dispatched
6339 * entries. In that situation, the newest entries caused ANR. But in all likelihood, the app
6340 * processes the events linearly. So providing information about the oldest entry seems to be
6341 * most useful.
6342 */
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006343 DispatchEntry& oldestEntry = *connection->waitQueue.front();
6344 const nsecs_t currentWait = now() - oldestEntry.deliveryTime;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006345 std::string reason =
6346 android::base::StringPrintf("%s is not responding. Waited %" PRId64 "ms for %s",
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006347 connection->getInputChannelName().c_str(),
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006348 ns2ms(currentWait),
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006349 oldestEntry.eventEntry->getDescription().c_str());
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006350 sp<IBinder> connectionToken = connection->getToken();
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -06006351 updateLastAnrStateLocked(getWindowHandleLocked(connectionToken), reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006352
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006353 processConnectionUnresponsiveLocked(*connection, std::move(reason));
6354
6355 // Stop waking up for events on this connection, it is already unresponsive
6356 cancelEventsForAnrLocked(connection);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006357}
6358
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006359void InputDispatcher::onAnrLocked(std::shared_ptr<InputApplicationHandle> application) {
6360 std::string reason =
6361 StringPrintf("%s does not have a focused window", application->getName().c_str());
6362 updateLastAnrStateLocked(*application, reason);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006363
Yabin Cui8eb9c552023-06-08 18:05:07 +00006364 auto command = [this, app = std::move(application)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006365 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006366 mPolicy.notifyNoFocusedWindowAnr(app);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006367 };
6368 postCommandLocked(std::move(command));
Bernardo Rufino2e1f6512020-10-08 13:42:07 +00006369}
6370
chaviw98318de2021-05-19 16:45:23 -05006371void InputDispatcher::updateLastAnrStateLocked(const sp<WindowInfoHandle>& window,
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006372 const std::string& reason) {
6373 const std::string windowLabel = getApplicationWindowLabel(nullptr, window);
6374 updateLastAnrStateLocked(windowLabel, reason);
6375}
6376
Siarhei Vishniakou234129c2020-10-22 22:28:12 -05006377void InputDispatcher::updateLastAnrStateLocked(const InputApplicationHandle& application,
6378 const std::string& reason) {
6379 const std::string windowLabel = getApplicationWindowLabel(&application, nullptr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006380 updateLastAnrStateLocked(windowLabel, reason);
6381}
6382
6383void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
6384 const std::string& reason) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 // Capture a record of the InputDispatcher state at the time of the ANR.
Yi Kong9b14ac62018-07-17 13:48:38 -07006386 time_t t = time(nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 struct tm tm;
6388 localtime_r(&t, &tm);
6389 char timestr[64];
6390 strftime(timestr, sizeof(timestr), "%F %T", &tm);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006391 mLastAnrState.clear();
6392 mLastAnrState += INDENT "ANR:\n";
6393 mLastAnrState += StringPrintf(INDENT2 "Time: %s\n", timestr);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07006394 mLastAnrState += StringPrintf(INDENT2 "Reason: %s\n", reason.c_str());
6395 mLastAnrState += StringPrintf(INDENT2 "Window: %s\n", windowLabel.c_str());
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006396 dumpDispatchStateLocked(mLastAnrState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397}
6398
Prabir Pradhancef936d2021-07-21 16:17:52 +00006399void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
Prabir Pradhan24047542023-11-02 17:14:59 +00006400 const KeyEntry& entry) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006401 const KeyEvent event = createKeyEvent(entry);
6402 nsecs_t delay = 0;
6403 { // release lock
6404 scoped_unlock unlock(mLock);
6405 android::base::Timer t;
Prabir Pradhana41d2442023-04-20 21:30:40 +00006406 delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006407 if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
6408 ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
6409 std::to_string(t.duration().count()).c_str());
6410 }
6411 } // acquire lock
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 if (delay < 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006414 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
Prabir Pradhancef936d2021-07-21 16:17:52 +00006415 } else if (delay == 0) {
Michael Wright5caf55a2022-11-24 22:31:42 +00006416 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 } else {
Michael Wright5caf55a2022-11-24 22:31:42 +00006418 entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006419 entry.interceptKeyWakeupTime = now() + delay;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421}
6422
Prabir Pradhancef936d2021-07-21 16:17:52 +00006423void InputDispatcher::sendWindowUnresponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006424 std::optional<gui::Pid> pid,
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006425 std::string reason) {
Yabin Cui8eb9c552023-06-08 18:05:07 +00006426 auto command = [this, token, pid, r = std::move(reason)]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006427 scoped_unlock unlock(mLock);
Yabin Cuiced952f2023-06-09 21:12:51 +00006428 mPolicy.notifyWindowUnresponsive(token, pid, r);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006429 };
6430 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006431}
6432
Prabir Pradhanedd96402022-02-15 01:46:16 -08006433void InputDispatcher::sendWindowResponsiveCommandLocked(const sp<IBinder>& token,
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006434 std::optional<gui::Pid> pid) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006435 auto command = [this, token, pid]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006436 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006437 mPolicy.notifyWindowResponsive(token, pid);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006438 };
6439 postCommandLocked(std::move(command));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006440}
6441
6442/**
6443 * Tell the policy that a connection has become unresponsive so that it can start ANR.
6444 * Check whether the connection of interest is a monitor or a window, and add the corresponding
6445 * command entry to the command queue.
6446 */
6447void InputDispatcher::processConnectionUnresponsiveLocked(const Connection& connection,
6448 std::string reason) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006449 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006450 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006451 if (connection.monitor) {
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006452 ALOGW("Monitor %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006453 reason.c_str());
Prabir Pradhanedd96402022-02-15 01:46:16 -08006454 pid = findMonitorPidByTokenLocked(connectionToken);
6455 } else {
6456 // The connection is a window
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006457 ALOGW("Window %s is unresponsive: %s", connection.getInputChannelName().c_str(),
Prabir Pradhanedd96402022-02-15 01:46:16 -08006458 reason.c_str());
6459 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6460 if (handle != nullptr) {
6461 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006462 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006463 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006464 sendWindowUnresponsiveCommandLocked(connectionToken, pid, std::move(reason));
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006465}
6466
6467/**
6468 * Tell the policy that a connection has become responsive so that it can stop ANR.
6469 */
6470void InputDispatcher::processConnectionResponsiveLocked(const Connection& connection) {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006471 const sp<IBinder>& connectionToken = connection.getToken();
Prabir Pradhanaeebeb42023-06-13 19:53:03 +00006472 std::optional<gui::Pid> pid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006473 if (connection.monitor) {
Prabir Pradhanedd96402022-02-15 01:46:16 -08006474 pid = findMonitorPidByTokenLocked(connectionToken);
6475 } else {
6476 // The connection is a window
6477 const sp<WindowInfoHandle> handle = getWindowHandleLocked(connectionToken);
6478 if (handle != nullptr) {
6479 pid = handle->getInfo()->ownerPid;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006480 }
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006481 }
Prabir Pradhanedd96402022-02-15 01:46:16 -08006482 sendWindowResponsiveCommandLocked(connectionToken, pid);
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +00006483}
6484
Prabir Pradhan24047542023-11-02 17:14:59 +00006485std::unique_ptr<const KeyEntry> InputDispatcher::afterKeyEventLockedInterruptable(
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006486 const std::shared_ptr<Connection>& connection, DispatchEntry& dispatchEntry,
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006487 const KeyEntry& keyEntry, bool handled) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006488 if (keyEntry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006489 if (!handled) {
6490 // Report the key as unhandled, since the fallback was not handled.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006491 mReporter->reportUnhandledKey(keyEntry.id);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006492 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006493 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006494 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006496 // Get the fallback key state.
6497 // Clear it out after dispatching the UP.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006498 int32_t originalKeyCode = keyEntry.keyCode;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006499 std::optional<int32_t> fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006500 if (keyEntry.action == AKEY_EVENT_ACTION_UP) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006501 connection->inputState.removeFallbackKey(originalKeyCode);
6502 }
6503
Prabir Pradhan8c90d782023-09-15 21:16:44 +00006504 if (handled || !dispatchEntry.hasForegroundTarget()) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006505 // If the application handles the original key for which we previously
6506 // generated a fallback or if the window is not a foreground window,
6507 // then cancel the associated fallback key, if any.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006508 if (fallbackKeyCode) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006509 // Dispatch the unhandled key to the policy with the cancel flag.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006510 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6511 ALOGD("Unhandled key event: Asking policy to cancel fallback action. "
6512 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6513 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount,
6514 keyEntry.policyFlags);
6515 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006516 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006517 event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518
6519 mLock.unlock();
6520
Prabir Pradhana41d2442023-04-20 21:30:40 +00006521 if (const auto unhandledKeyFallback =
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006522 mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6523 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006524 unhandledKeyFallback) {
6525 event = *unhandledKeyFallback;
6526 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
6528 mLock.lock();
6529
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006530 // Cancel the fallback key, but only if we still have a window for the channel.
6531 // It could have been removed during the policy call.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006532 if (*fallbackKeyCode != AKEYCODE_UNKNOWN) {
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006533 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6534 if (windowHandle != nullptr) {
6535 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
6536 "application handled the original non-fallback key "
6537 "or is no longer a foreground target, "
6538 "canceling previously dispatched fallback key");
6539 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006540 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006541 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006543 connection->inputState.removeFallbackKey(originalKeyCode);
6544 }
6545 } else {
6546 // If the application did not handle a non-fallback key, first check
6547 // that we are in a good state to perform unhandled key event processing
6548 // Then ask the policy what to do with it.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006549 bool initialDown = keyEntry.action == AKEY_EVENT_ACTION_DOWN && keyEntry.repeatCount == 0;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006550 if (!fallbackKeyCode && !initialDown) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006551 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6552 ALOGD("Unhandled key event: Skipping unhandled key event processing "
6553 "since this is not an initial down. "
6554 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6555 originalKeyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6556 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006557 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006560 // Dispatch the unhandled key to the policy.
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006561 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6562 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
6563 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
6564 keyEntry.keyCode, keyEntry.action, keyEntry.repeatCount, keyEntry.policyFlags);
6565 }
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006566 KeyEvent event = createKeyEvent(keyEntry);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006567
6568 mLock.unlock();
6569
Prabir Pradhana41d2442023-04-20 21:30:40 +00006570 bool fallback = false;
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -08006571 if (auto fb = mPolicy.dispatchUnhandledKey(connection->getToken(), event,
6572 keyEntry.policyFlags);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006573 fb) {
6574 fallback = true;
6575 event = *fb;
6576 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006577
6578 mLock.lock();
6579
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -08006580 if (connection->status != Connection::Status::NORMAL) {
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006581 connection->inputState.removeFallbackKey(originalKeyCode);
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006582 return {};
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006583 }
6584
6585 // Latch the fallback keycode for this key on an initial down.
6586 // The fallback keycode cannot change at any other point in the lifecycle.
6587 if (initialDown) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588 if (fallback) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006589 *fallbackKeyCode = event.getKeyCode();
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006590 } else {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006591 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006592 }
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006593 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006594 }
6595
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006596 ALOG_ASSERT(fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006597
6598 // Cancel the fallback key if the policy decides not to send it anymore.
6599 // We will continue to dispatch the key to the policy but we will no
6600 // longer dispatch a fallback key to the application.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006601 if (*fallbackKeyCode != AKEYCODE_UNKNOWN &&
6602 (!fallback || *fallbackKeyCode != event.getKeyCode())) {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006603 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6604 if (fallback) {
6605 ALOGD("Unhandled key event: Policy requested to send key %d"
6606 "as a fallback for %d, but on the DOWN it had requested "
6607 "to send %d instead. Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006608 event.getKeyCode(), originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006609 } else {
6610 ALOGD("Unhandled key event: Policy did not request fallback for %d, "
6611 "but on the DOWN it had requested to send %d. "
6612 "Fallback canceled.",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006613 originalKeyCode, *fallbackKeyCode);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006614 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006615 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006616
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006617 const auto windowHandle = getWindowHandleLocked(connection->getToken());
6618 if (windowHandle != nullptr) {
6619 CancelationOptions options(CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS,
6620 "canceling fallback, policy no longer desires it");
6621 options.keyCode = *fallbackKeyCode;
Prabir Pradhanbb3f1c02024-01-04 20:17:14 +00006622 synthesizeCancelationEventsForWindowLocked(windowHandle, options, connection);
Prabir Pradhanfa2c69f2024-02-01 20:31:34 +00006623 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006624
6625 fallback = false;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006626 *fallbackKeyCode = AKEYCODE_UNKNOWN;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006627 if (keyEntry.action != AKEY_EVENT_ACTION_UP) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006628 connection->inputState.setFallbackKey(originalKeyCode, *fallbackKeyCode);
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006629 }
6630 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006632 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6633 {
6634 std::string msg;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006635 const std::map<int32_t, int32_t>& fallbackKeys =
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006636 connection->inputState.getFallbackKeys();
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006637 for (const auto& [key, value] : fallbackKeys) {
6638 msg += StringPrintf(", %d->%d", key, value);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006639 }
6640 ALOGD("Unhandled key event: %zu currently tracked fallback keys%s.",
6641 fallbackKeys.size(), msg.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006642 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006643 }
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006644
6645 if (fallback) {
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006646 // Return the fallback key that we want dispatched to the channel.
6647 std::unique_ptr<KeyEntry> newEntry =
6648 std::make_unique<KeyEntry>(mIdGenerator.nextId(), keyEntry.injectionState,
6649 event.getEventTime(), event.getDeviceId(),
6650 event.getSource(), event.getDisplayId(),
6651 keyEntry.policyFlags, keyEntry.action,
6652 event.getFlags() | AKEY_EVENT_FLAG_FALLBACK,
6653 *fallbackKeyCode, event.getScanCode(),
6654 event.getMetaState(), event.getRepeatCount(),
6655 event.getDownTime());
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006656 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6657 ALOGD("Unhandled key event: Dispatching fallback key. "
6658 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -07006659 originalKeyCode, *fallbackKeyCode, keyEntry.metaState);
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006660 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006661 return newEntry;
Prabir Pradhanf557dcf2018-12-18 16:38:14 -08006662 } else {
Prabir Pradhan61a5d242021-07-26 16:41:09 +00006663 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
6664 ALOGD("Unhandled key event: No fallback key.");
6665 }
Prabir Pradhanf93562f2018-11-29 12:13:37 -08006666
6667 // Report the key as unhandled, since there is no fallback key.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -07006668 mReporter->reportUnhandledKey(keyEntry.id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669 }
6670 }
Prabir Pradhanb9dd1642023-11-02 18:05:36 +00006671 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08006672}
6673
Michael Wrightd02c5b62014-02-10 15:10:22 -08006674void InputDispatcher::traceInboundQueueLengthLocked() {
6675 if (ATRACE_ENABLED()) {
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -07006676 ATRACE_INT("iq", mInboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006677 }
6678}
6679
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006680void InputDispatcher::traceOutboundQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681 if (ATRACE_ENABLED()) {
6682 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006683 snprintf(counterName, sizeof(counterName), "oq:%s",
6684 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006685 ATRACE_INT(counterName, connection.outboundQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686 }
6687}
6688
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006689void InputDispatcher::traceWaitQueueLength(const Connection& connection) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006690 if (ATRACE_ENABLED()) {
6691 char counterName[40];
Siarhei Vishniakou10bfdc92024-02-01 11:12:57 -08006692 snprintf(counterName, sizeof(counterName), "wq:%s",
6693 connection.getInputChannelName().c_str());
Siarhei Vishniakou060a7272021-02-03 19:40:10 +00006694 ATRACE_INT(counterName, connection.waitQueue.size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006695 }
6696}
6697
Siarhei Vishniakou5e20f272023-06-08 17:24:44 -07006698void InputDispatcher::dump(std::string& dump) const {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006699 std::scoped_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006701 dump += "Input Dispatcher State:\n";
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702 dumpDispatchStateLocked(dump);
6703
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006704 if (!mLastAnrState.empty()) {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08006705 dump += "\nInput Dispatcher State at time of last ANR:\n";
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -07006706 dump += mLastAnrState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707 }
6708}
6709
6710void InputDispatcher::monitor() {
6711 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006712 std::unique_lock _l(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713 mLooper->wake();
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08006714 mDispatcherIsAlive.wait(_l);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715}
6716
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006717/**
6718 * Wake up the dispatcher and wait until it processes all events and commands.
6719 * The notification of mDispatcherEnteredIdle is guaranteed to happen after wake(), so
6720 * this method can be safely called from any thread, as long as you've ensured that
6721 * the work you are interested in completing has already been queued.
6722 */
Siarhei Vishniakoua66d65e2023-06-16 10:32:51 -07006723bool InputDispatcher::waitForIdle() const {
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -08006724 /**
6725 * Timeout should represent the longest possible time that a device might spend processing
6726 * events and commands.
6727 */
6728 constexpr std::chrono::duration TIMEOUT = 100ms;
6729 std::unique_lock lock(mLock);
6730 mLooper->wake();
6731 std::cv_status result = mDispatcherEnteredIdle.wait_for(lock, TIMEOUT);
6732 return result == std::cv_status::no_timeout;
6733}
6734
Vishnu Naire798b472020-07-23 13:52:21 -07006735/**
6736 * Sets focus to the window identified by the token. This must be called
6737 * after updating any input window handles.
6738 *
6739 * Params:
6740 * request.token - input channel token used to identify the window that should gain focus.
6741 * request.focusedToken - the token that the caller expects currently to be focused. If the
6742 * specified token does not match the currently focused window, this request will be dropped.
6743 * If the specified focused token matches the currently focused window, the call will succeed.
6744 * Set this to "null" if this call should succeed no matter what the currently focused token is.
6745 * request.timestamp - SYSTEM_TIME_MONOTONIC timestamp in nanos set by the client (wm)
6746 * when requesting the focus change. This determines which request gets
6747 * precedence if there is a focus change request from another source such as pointer down.
6748 */
Vishnu Nair958da932020-08-21 17:12:37 -07006749void InputDispatcher::setFocusedWindow(const FocusRequest& request) {
6750 { // acquire lock
6751 std::scoped_lock _l(mLock);
Vishnu Nairc519ff72021-01-21 08:23:08 -08006752 std::optional<FocusResolver::FocusChanges> changes =
6753 mFocusResolver.setFocusedWindow(request, getWindowHandlesLocked(request.displayId));
6754 if (changes) {
6755 onFocusChangedLocked(*changes);
Vishnu Nair958da932020-08-21 17:12:37 -07006756 }
6757 } // release lock
6758 // Wake up poll loop since it may need to make new input dispatching choices.
6759 mLooper->wake();
6760}
6761
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006762void InputDispatcher::onFocusChangedLocked(const FocusResolver::FocusChanges& changes,
6763 const sp<WindowInfoHandle> removedFocusedWindowHandle) {
Vishnu Nairc519ff72021-01-21 08:23:08 -08006764 if (changes.oldFocus) {
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006765 const auto resolvedWindow = removedFocusedWindowHandle != nullptr
6766 ? removedFocusedWindowHandle
6767 : getWindowHandleLocked(changes.oldFocus, changes.displayId);
6768 if (resolvedWindow == nullptr) {
6769 LOG(FATAL) << __func__ << ": Previously focused token did not have a window";
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006770 }
Prabir Pradhan4b9b1a12024-02-02 00:16:17 +00006771 CancelationOptions options(CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS,
6772 "focus left window");
6773 synthesizeCancelationEventsForWindowLocked(resolvedWindow, options);
6774 enqueueFocusEventLocked(changes.oldFocus, /*hasFocus=*/false, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006775 }
Vishnu Nairc519ff72021-01-21 08:23:08 -08006776 if (changes.newFocus) {
Siarhei Vishniakouc033dfb2023-10-03 10:45:16 -07006777 resetNoFocusedWindowTimeoutLocked();
Harry Cutts33476232023-01-30 19:57:29 +00006778 enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006779 }
6780
Prabir Pradhan99987712020-11-10 18:43:05 -08006781 // If a window has pointer capture, then it must have focus. We need to ensure that this
6782 // contract is upheld when pointer capture is being disabled due to a loss of window focus.
6783 // If the window loses focus before it loses pointer capture, then the window can be in a state
6784 // where it has pointer capture but not focus, violating the contract. Therefore we must
6785 // dispatch the pointer capture event before the focus event. Since focus events are added to
6786 // the front of the queue (above), we add the pointer capture event to the front of the queue
6787 // after the focus events are added. This ensures the pointer capture event ends up at the
6788 // front.
6789 disablePointerCaptureForcedLocked();
6790
Vishnu Nairc519ff72021-01-21 08:23:08 -08006791 if (mFocusedDisplayId == changes.displayId) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006792 sendFocusChangedCommandLocked(changes.oldFocus, changes.newFocus);
Vishnu Nair7d3d00d2020-08-03 11:20:42 -07006793 }
6794}
Vishnu Nair958da932020-08-21 17:12:37 -07006795
Prabir Pradhan99987712020-11-10 18:43:05 -08006796void InputDispatcher::disablePointerCaptureForcedLocked() {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006797 if (!mCurrentPointerCaptureRequest.enable && !mWindowTokenWithPointerCapture) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006798 return;
6799 }
6800
6801 ALOGD_IF(DEBUG_FOCUS, "Disabling Pointer Capture because the window lost focus.");
6802
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006803 if (mCurrentPointerCaptureRequest.enable) {
Prabir Pradhan99987712020-11-10 18:43:05 -08006804 setPointerCaptureLocked(false);
6805 }
6806
6807 if (!mWindowTokenWithPointerCapture) {
6808 // No need to send capture changes because no window has capture.
6809 return;
6810 }
6811
6812 if (mPendingEvent != nullptr) {
6813 // Move the pending event to the front of the queue. This will give the chance
6814 // for the pending event to be dropped if it is a captured event.
6815 mInboundQueue.push_front(mPendingEvent);
6816 mPendingEvent = nullptr;
6817 }
6818
6819 auto entry = std::make_unique<PointerCaptureChangedEntry>(mIdGenerator.nextId(), now(),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006820 mCurrentPointerCaptureRequest);
Prabir Pradhan99987712020-11-10 18:43:05 -08006821 mInboundQueue.push_front(std::move(entry));
6822}
6823
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00006824void InputDispatcher::setPointerCaptureLocked(bool enable) {
6825 mCurrentPointerCaptureRequest.enable = enable;
6826 mCurrentPointerCaptureRequest.seq++;
6827 auto command = [this, request = mCurrentPointerCaptureRequest]() REQUIRES(mLock) {
Prabir Pradhancef936d2021-07-21 16:17:52 +00006828 scoped_unlock unlock(mLock);
Prabir Pradhana41d2442023-04-20 21:30:40 +00006829 mPolicy.setPointerCapture(request);
Prabir Pradhancef936d2021-07-21 16:17:52 +00006830 };
6831 postCommandLocked(std::move(command));
Prabir Pradhan99987712020-11-10 18:43:05 -08006832}
6833
Vishnu Nair599f1412021-06-21 10:39:58 -07006834void InputDispatcher::displayRemoved(int32_t displayId) {
6835 { // acquire lock
6836 std::scoped_lock _l(mLock);
6837 // Set an empty list to remove all handles from the specific display.
Harry Cutts101ee9b2023-07-06 18:04:14 +00006838 setInputWindowsLocked(/*windowInfoHandles=*/{}, displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006839 setFocusedApplicationLocked(displayId, nullptr);
6840 // Call focus resolver to clean up stale requests. This must be called after input windows
6841 // have been removed for the removed display.
6842 mFocusResolver.displayRemoved(displayId);
Christine Franksb768bb42021-11-29 12:11:31 -08006843 // Reset pointer capture eligibility, regardless of previous state.
6844 std::erase(mIneligibleDisplaysForPointerCapture, displayId);
Antonio Kantek15beb512022-06-13 22:35:41 +00006845 // Remove the associated touch mode state.
6846 mTouchModePerDisplay.erase(displayId);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -07006847 mVerifiersByDisplay.erase(displayId);
Vishnu Nair599f1412021-06-21 10:39:58 -07006848 } // release lock
6849
6850 // Wake up poll loop since it may need to make new input dispatching choices.
6851 mLooper->wake();
6852}
6853
Patrick Williamsd828f302023-04-28 17:52:08 -05006854void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) {
Siarhei Vishniakouaeed0da2024-01-09 08:57:13 -08006855 if (auto result = validateWindowInfosUpdate(update); !result.ok()) {
6856 {
6857 // acquire lock
6858 std::scoped_lock _l(mLock);
6859 logDispatchStateLocked();
6860 }
6861 LOG_ALWAYS_FATAL("Incorrect WindowInfosUpdate provided: %s",
6862 result.error().message().c_str());
6863 };
chaviw15fab6f2021-06-07 14:15:52 -05006864 // The listener sends the windows as a flattened array. Separate the windows by display for
6865 // more convenient parsing.
6866 std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
Patrick Williamsd828f302023-04-28 17:52:08 -05006867 for (const auto& info : update.windowInfos) {
chaviw15fab6f2021-06-07 14:15:52 -05006868 handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07006869 handlesPerDisplay[info.displayId].push_back(sp<WindowInfoHandle>::make(info));
chaviw15fab6f2021-06-07 14:15:52 -05006870 }
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006871
6872 { // acquire lock
6873 std::scoped_lock _l(mLock);
Prabir Pradhan814fe082022-07-22 20:22:18 +00006874
6875 // Ensure that we have an entry created for all existing displays so that if a displayId has
6876 // no windows, we can tell that the windows were removed from the display.
6877 for (const auto& [displayId, _] : mWindowHandlesByDisplay) {
6878 handlesPerDisplay[displayId];
6879 }
6880
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006881 mDisplayInfos.clear();
Patrick Williamsd828f302023-04-28 17:52:08 -05006882 for (const auto& displayInfo : update.displayInfos) {
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006883 mDisplayInfos.emplace(displayInfo.displayId, displayInfo);
6884 }
6885
6886 for (const auto& [displayId, handles] : handlesPerDisplay) {
6887 setInputWindowsLocked(handles, displayId);
6888 }
Patrick Williams9464b2c2023-05-23 11:22:04 -05006889
6890 if (update.vsyncId < mWindowInfosVsyncId) {
6891 ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64
6892 ", current update vsync id: %" PRId64,
6893 mWindowInfosVsyncId, update.vsyncId);
6894 }
6895 mWindowInfosVsyncId = update.vsyncId;
Prabir Pradhan48f8cb92021-08-26 14:05:36 -07006896 }
6897 // Wake up poll loop since it may need to make new input dispatching choices.
6898 mLooper->wake();
chaviw15fab6f2021-06-07 14:15:52 -05006899}
6900
Vishnu Nair062a8672021-09-03 16:07:44 -07006901bool InputDispatcher::shouldDropInput(
6902 const EventEntry& entry, const sp<android::gui::WindowInfoHandle>& windowHandle) const {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006903 if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) ||
6904 (windowHandle->getInfo()->inputConfig.test(
6905 WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) &&
Vishnu Nair062a8672021-09-03 16:07:44 -07006906 isWindowObscuredLocked(windowHandle))) {
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006907 ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on "
6908 "display %" PRId32 ".",
Vishnu Nair062a8672021-09-03 16:07:44 -07006909 ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(),
Prabir Pradhan51e7db02022-02-07 06:02:57 -08006910 windowHandle->getInfo()->inputConfig.string().c_str(),
Vishnu Nair062a8672021-09-03 16:07:44 -07006911 windowHandle->getInfo()->displayId);
6912 return true;
6913 }
6914 return false;
6915}
6916
Siarhei Vishniakou18050092021-09-01 13:32:49 -07006917void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -05006918 const gui::WindowInfosUpdate& update) {
6919 mDispatcher.onWindowInfosChanged(update);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07006920}
6921
Arthur Hungdfd528e2021-12-08 13:23:04 +00006922void InputDispatcher::cancelCurrentTouch() {
6923 {
6924 std::scoped_lock _l(mLock);
6925 ALOGD("Canceling all ongoing pointer gestures on all displays.");
Michael Wrightfb04fd52022-11-24 22:31:11 +00006926 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
Arthur Hungdfd528e2021-12-08 13:23:04 +00006927 "cancel current touch");
6928 synthesizeCancelationEventsForAllConnectionsLocked(options);
6929
6930 mTouchStatesByDisplay.clear();
Arthur Hungdfd528e2021-12-08 13:23:04 +00006931 }
6932 // Wake up poll loop since there might be work to do.
6933 mLooper->wake();
6934}
6935
Prabir Pradhan1376fcd2022-01-21 09:56:35 -08006936void InputDispatcher::setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout) {
6937 std::scoped_lock _l(mLock);
6938 mMonitorDispatchingTimeout = timeout;
6939}
6940
Arthur Hungc539dbb2022-12-08 07:45:36 +00006941void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags,
6942 const sp<WindowInfoHandle>& oldWindowHandle,
6943 const sp<WindowInfoHandle>& newWindowHandle,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006944 TouchState& state, int32_t deviceId,
6945 const PointerProperties& pointerProperties,
Siarhei Vishniakoubf880522023-05-01 11:03:22 -07006946 std::vector<InputTarget>& targets) const {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006947 std::vector<PointerProperties> pointers{pointerProperties};
Arthur Hungc539dbb2022-12-08 07:45:36 +00006948 const bool oldHasWallpaper = oldWindowHandle->getInfo()->inputConfig.test(
6949 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
6950 const bool newHasWallpaper = targetFlags.test(InputTarget::Flags::FOREGROUND) &&
6951 newWindowHandle->getInfo()->inputConfig.test(
6952 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
6953 const sp<WindowInfoHandle> oldWallpaper =
6954 oldHasWallpaper ? state.getWallpaperWindow() : nullptr;
6955 const sp<WindowInfoHandle> newWallpaper =
6956 newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr;
6957 if (oldWallpaper == newWallpaper) {
6958 return;
6959 }
6960
6961 if (oldWallpaper != nullptr) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -08006962 const TouchedWindow& oldTouchedWindow = state.getTouchedWindow(oldWallpaper);
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00006963 addPointerWindowTargetLocked(oldWallpaper, InputTarget::DispatchMode::SLIPPERY_EXIT,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006964 oldTouchedWindow.targetFlags, getPointerIds(pointers),
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00006965 oldTouchedWindow.getDownTimeInTarget(deviceId), targets);
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006966 state.removeTouchingPointerFromWindow(deviceId, pointerProperties.id, oldWallpaper);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006967 }
6968
6969 if (newWallpaper != nullptr) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00006970 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::SLIPPERY_ENTER,
6971 InputTarget::Flags::WINDOW_IS_OBSCURED |
Arthur Hungc539dbb2022-12-08 07:45:36 +00006972 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006973 deviceId, pointers);
Arthur Hungc539dbb2022-12-08 07:45:36 +00006974 }
6975}
6976
6977void InputDispatcher::transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags,
6978 ftl::Flags<InputTarget::Flags> newTargetFlags,
6979 const sp<WindowInfoHandle> fromWindowHandle,
6980 const sp<WindowInfoHandle> toWindowHandle,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -07006981 TouchState& state, int32_t deviceId,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08006982 const std::vector<PointerProperties>& pointers) {
Arthur Hungc539dbb2022-12-08 07:45:36 +00006983 const bool oldHasWallpaper = oldTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
6984 fromWindowHandle->getInfo()->inputConfig.test(
6985 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
6986 const bool newHasWallpaper = newTargetFlags.test(InputTarget::Flags::FOREGROUND) &&
6987 toWindowHandle->getInfo()->inputConfig.test(
6988 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER);
6989
6990 const sp<WindowInfoHandle> oldWallpaper =
6991 oldHasWallpaper ? state.getWallpaperWindow() : nullptr;
6992 const sp<WindowInfoHandle> newWallpaper =
6993 newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr;
6994 if (oldWallpaper == newWallpaper) {
6995 return;
6996 }
6997
6998 if (oldWallpaper != nullptr) {
6999 CancelationOptions options(CancelationOptions::Mode::CANCEL_POINTER_EVENTS,
7000 "transferring touch focus to another window");
7001 state.removeWindowByToken(oldWallpaper->getToken());
7002 synthesizeCancelationEventsForWindowLocked(oldWallpaper, options);
7003 }
7004
7005 if (newWallpaper != nullptr) {
7006 nsecs_t downTimeInTarget = now();
Prabir Pradhan65455c72024-02-13 21:46:41 +00007007 ftl::Flags<InputTarget::Flags> wallpaperFlags = newTargetFlags;
7008 wallpaperFlags |= oldTargetFlags & InputTarget::Flags::SPLIT;
Arthur Hungc539dbb2022-12-08 07:45:36 +00007009 wallpaperFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED |
7010 InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +00007011 state.addOrUpdateWindow(newWallpaper, InputTarget::DispatchMode::AS_IS, wallpaperFlags,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08007012 deviceId, pointers, downTimeInTarget);
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007013 std::shared_ptr<Connection> wallpaperConnection =
7014 getConnectionLocked(newWallpaper->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007015 if (wallpaperConnection != nullptr) {
Siarhei Vishniakou1069fa82023-04-19 12:14:39 -07007016 std::shared_ptr<Connection> toConnection =
7017 getConnectionLocked(toWindowHandle->getToken());
Arthur Hungc539dbb2022-12-08 07:45:36 +00007018 toConnection->inputState.mergePointerStateTo(wallpaperConnection->inputState);
7019 synthesizePointerDownEventsForConnectionLocked(downTimeInTarget, wallpaperConnection,
7020 wallpaperFlags);
7021 }
7022 }
7023}
7024
7025sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow(
7026 const sp<WindowInfoHandle>& windowHandle) const {
7027 const std::vector<sp<WindowInfoHandle>>& windowHandles =
7028 getWindowHandlesLocked(windowHandle->getInfo()->displayId);
7029 bool foundWindow = false;
7030 for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
7031 if (!foundWindow && otherHandle != windowHandle) {
7032 continue;
7033 }
7034 if (windowHandle == otherHandle) {
7035 foundWindow = true;
7036 continue;
7037 }
7038
7039 if (otherHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::IS_WALLPAPER)) {
7040 return otherHandle;
7041 }
7042 }
7043 return nullptr;
7044}
7045
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007046void InputDispatcher::setKeyRepeatConfiguration(std::chrono::nanoseconds timeout,
7047 std::chrono::nanoseconds delay) {
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007048 std::scoped_lock _l(mLock);
7049
Siarhei Vishniakoufa2a0492023-11-14 13:13:18 -08007050 mConfig.keyRepeatTimeout = timeout.count();
7051 mConfig.keyRepeatDelay = delay.count();
Nergi Rahardi730cf3c2023-04-13 12:41:17 +09007052}
7053
Prabir Pradhan64f21d22023-11-28 21:19:42 +00007054bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token, int32_t displayId,
7055 DeviceId deviceId, int32_t pointerId) {
7056 std::scoped_lock _l(mLock);
7057 auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7058 if (touchStateIt == mTouchStatesByDisplay.end()) {
7059 return false;
7060 }
7061 for (const TouchedWindow& window : touchStateIt->second.windows) {
7062 if (window.windowHandle->getToken() == token &&
7063 (window.hasTouchingPointer(deviceId, pointerId) ||
7064 window.hasHoveringPointer(deviceId, pointerId))) {
7065 return true;
7066 }
7067 }
7068 return false;
7069}
7070
Garfield Tane84e6f92019-08-29 17:28:41 -07007071} // namespace android::inputdispatcher