blob: 17f0b8737b6b839c788b6d885755d9c3c12c4f87 [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
2 * Copyright (C) 2019 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
Arthur Hung1a1007b2022-05-11 07:15:01 +000017#include "DebugConfig.h"
Chris Yef59a2f42020-10-16 12:55:26 -070018#include "input/InputDevice.h"
19
Garfield Tane84e6f92019-08-29 17:28:41 -070020#include "InputState.h"
21
Arthur Hung1a1007b2022-05-11 07:15:01 +000022#include <cinttypes>
Garfield Tanff1f1bb2020-01-28 13:24:04 -080023#include "InputDispatcher.h"
24
Garfield Tane84e6f92019-08-29 17:28:41 -070025namespace android::inputdispatcher {
26
Garfield Tanff1f1bb2020-01-28 13:24:04 -080027InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070028
29InputState::~InputState() {}
30
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070031bool InputState::isHovering(DeviceId deviceId, uint32_t source, int32_t displayId) const {
Garfield Tane84e6f92019-08-29 17:28:41 -070032 for (const MotionMemento& memento : mMotionMementos) {
33 if (memento.deviceId == deviceId && memento.source == source &&
34 memento.displayId == displayId && memento.hovering) {
35 return true;
36 }
37 }
38 return false;
39}
40
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070041bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070042 switch (action) {
43 case AKEY_EVENT_ACTION_UP: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070044 if (entry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -070045 std::erase_if(mFallbackKeys,
46 [&entry](const auto& item) { return item.second == entry.keyCode; });
Garfield Tane84e6f92019-08-29 17:28:41 -070047 }
48 ssize_t index = findKeyMemento(entry);
49 if (index >= 0) {
50 mKeyMementos.erase(mKeyMementos.begin() + index);
51 return true;
52 }
53 /* FIXME: We can't just drop the key up event because that prevents creating
54 * popup windows that are automatically shown when a key is held and then
55 * dismissed when the key is released. The problem is that the popup will
56 * not have received the original key down, so the key up will be considered
57 * to be inconsistent with its observed state. We could perhaps handle this
58 * by synthesizing a key down but that will cause other problems.
59 *
60 * So for now, allow inconsistent key up events to be dispatched.
61 *
62 #if DEBUG_OUTBOUND_EVENT_DETAILS
63 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
64 "keyCode=%d, scanCode=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070065 entry.deviceId, entry.source, entry.keyCode, entry.scanCode);
Garfield Tane84e6f92019-08-29 17:28:41 -070066 #endif
67 return false;
68 */
69 return true;
70 }
71
72 case AKEY_EVENT_ACTION_DOWN: {
73 ssize_t index = findKeyMemento(entry);
74 if (index >= 0) {
75 mKeyMementos.erase(mKeyMementos.begin() + index);
76 }
77 addKeyMemento(entry, flags);
78 return true;
79 }
80
81 default:
82 return true;
83 }
84}
85
Siarhei Vishniakouf4043212023-09-18 19:33:03 -070086/**
87 * Return:
88 * true if the incoming event was correctly tracked,
89 * false if the incoming event should be dropped.
90 */
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070091bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070092 // Don't track non-pointer events
93 if (!isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) {
94 // This is a focus-dispatched event; we don't track its state.
95 return true;
96 }
97
98 if (!mMotionMementos.empty()) {
99 const MotionMemento& lastMemento = mMotionMementos.back();
100 if (isStylusEvent(lastMemento.source, lastMemento.pointerProperties) &&
101 !isStylusEvent(entry.source, entry.pointerProperties)) {
102 // We already have a stylus stream, and the new event is not from stylus.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700103 return false;
104 }
105 }
106
Garfield Tane84e6f92019-08-29 17:28:41 -0700107 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
108 switch (actionMasked) {
109 case AMOTION_EVENT_ACTION_UP:
110 case AMOTION_EVENT_ACTION_CANCEL: {
Harry Cutts33476232023-01-30 19:57:29 +0000111 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700112 if (index >= 0) {
113 mMotionMementos.erase(mMotionMementos.begin() + index);
114 return true;
115 }
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -0700116
Garfield Tane84e6f92019-08-29 17:28:41 -0700117 return false;
118 }
119
120 case AMOTION_EVENT_ACTION_DOWN: {
Harry Cutts33476232023-01-30 19:57:29 +0000121 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700122 if (index >= 0) {
123 mMotionMementos.erase(mMotionMementos.begin() + index);
124 }
Harry Cutts33476232023-01-30 19:57:29 +0000125 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700126 return true;
127 }
128
129 case AMOTION_EVENT_ACTION_POINTER_UP:
130 case AMOTION_EVENT_ACTION_POINTER_DOWN:
131 case AMOTION_EVENT_ACTION_MOVE: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700132 if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700133 // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need
134 // to generate cancellation events for these since they're based in relative rather
135 // than absolute units.
136 return true;
137 }
138
Harry Cutts33476232023-01-30 19:57:29 +0000139 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700140
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700141 if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700142 // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all
143 // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral.
144 // Any other value and we need to track the motion so we can send cancellation
145 // events for anything generating fallback events (e.g. DPad keys for joystick
146 // movements).
147 if (index >= 0) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700148 if (entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700149 mMotionMementos.erase(mMotionMementos.begin() + index);
150 } else {
151 MotionMemento& memento = mMotionMementos[index];
152 memento.setPointers(entry);
153 }
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700154 } else if (!entry.pointerCoords[0].isEmpty()) {
Harry Cutts33476232023-01-30 19:57:29 +0000155 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700156 }
157
158 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
159 return true;
160 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800161
Garfield Tane84e6f92019-08-29 17:28:41 -0700162 if (index >= 0) {
163 MotionMemento& memento = mMotionMementos[index];
Svet Ganov5d3bc372020-01-26 23:11:07 -0800164 if (memento.firstNewPointerIdx < 0) {
165 memento.setPointers(entry);
166 return true;
167 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700168 }
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -0700169
Garfield Tane84e6f92019-08-29 17:28:41 -0700170 return false;
171 }
172
173 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
Harry Cutts33476232023-01-30 19:57:29 +0000174 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700175 if (index >= 0) {
176 mMotionMementos.erase(mMotionMementos.begin() + index);
177 return true;
178 }
Siarhei Vishniakouc94dafe2023-05-26 10:24:19 -0700179
Garfield Tane84e6f92019-08-29 17:28:41 -0700180 return false;
181 }
182
183 case AMOTION_EVENT_ACTION_HOVER_ENTER:
184 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
Harry Cutts33476232023-01-30 19:57:29 +0000185 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700186 if (index >= 0) {
187 mMotionMementos.erase(mMotionMementos.begin() + index);
188 }
Harry Cutts33476232023-01-30 19:57:29 +0000189 addMotionMemento(entry, flags, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700190 return true;
191 }
192
193 default:
194 return true;
195 }
196}
197
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700198ssize_t InputState::findKeyMemento(const KeyEntry& entry) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700199 for (size_t i = 0; i < mKeyMementos.size(); i++) {
200 const KeyMemento& memento = mKeyMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700201 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
202 memento.displayId == entry.displayId && memento.keyCode == entry.keyCode &&
203 memento.scanCode == entry.scanCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700204 return i;
205 }
206 }
207 return -1;
208}
209
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700210ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700211 for (size_t i = 0; i < mMotionMementos.size(); i++) {
212 const MotionMemento& memento = mMotionMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700213 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
214 memento.displayId == entry.displayId && memento.hovering == hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700215 return i;
216 }
217 }
218 return -1;
219}
220
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700221void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700222 KeyMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700223 memento.deviceId = entry.deviceId;
224 memento.source = entry.source;
225 memento.displayId = entry.displayId;
226 memento.keyCode = entry.keyCode;
227 memento.scanCode = entry.scanCode;
228 memento.metaState = entry.metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700229 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700230 memento.downTime = entry.downTime;
231 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700232 mKeyMementos.push_back(memento);
233}
234
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700235void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700236 MotionMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700237 memento.deviceId = entry.deviceId;
238 memento.source = entry.source;
239 memento.displayId = entry.displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700240 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700241 memento.xPrecision = entry.xPrecision;
242 memento.yPrecision = entry.yPrecision;
243 memento.xCursorPosition = entry.xCursorPosition;
244 memento.yCursorPosition = entry.yCursorPosition;
245 memento.downTime = entry.downTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700246 memento.setPointers(entry);
247 memento.hovering = hovering;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700248 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700249 mMotionMementos.push_back(memento);
250}
251
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700252void InputState::MotionMemento::setPointers(const MotionEntry& entry) {
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700253 pointerProperties.clear();
254 pointerCoords.clear();
255
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700256 for (uint32_t i = 0; i < entry.getPointerCount(); i++) {
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800257 if (MotionEvent::getActionMasked(entry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
258 // In POINTER_UP events, the pointer is leaving. Since the action is not stored,
259 // this departing pointer should not be recorded.
260 const uint8_t actionIndex = MotionEvent::getActionIndex(entry.action);
261 if (i == actionIndex) {
262 continue;
263 }
264 }
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700265 pointerProperties.push_back(entry.pointerProperties[i]);
266 pointerCoords.push_back(entry.pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700267 }
268}
269
Svet Ganov5d3bc372020-01-26 23:11:07 -0800270void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const {
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700271 for (uint32_t i = 0; i < getPointerCount(); i++) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800272 if (other.firstNewPointerIdx < 0) {
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700273 other.firstNewPointerIdx = other.getPointerCount();
Svet Ganov5d3bc372020-01-26 23:11:07 -0800274 }
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700275 other.pointerProperties.push_back(pointerProperties[i]);
276 other.pointerCoords.push_back(pointerCoords[i]);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800277 }
278}
279
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700280size_t InputState::MotionMemento::getPointerCount() const {
281 return pointerProperties.size();
282}
283
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700284bool InputState::shouldCancelPreviousStream(const MotionEntry& motionEntry,
285 int32_t resolvedAction) const {
286 if (!isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER)) {
287 // This is a focus-dispatched event that should not affect the previous stream.
288 return false;
289 }
290
291 // New MotionEntry pointer event is coming in.
292
293 // If this is a new gesture, and it's from a different device, then, in general, we will cancel
294 // the current gesture.
295 // However, because stylus should be preferred over touch, we need to treat some cases in a
296 // special way.
297 if (mMotionMementos.empty()) {
298 // There is no ongoing pointer gesture, so there is nothing to cancel
299 return false;
300 }
301
302 const MotionMemento& lastMemento = mMotionMementos.back();
303 const int32_t actionMasked = MotionEvent::getActionMasked(resolvedAction);
304
305 // For compatibility, only one input device can be active at a time in the same window.
306 if (lastMemento.deviceId == motionEntry.deviceId) {
307 // In general, the same device should produce self-consistent streams so nothing needs to
308 // be canceled. But there is one exception:
309 // Sometimes ACTION_DOWN is received without a corresponding HOVER_EXIT. To account for
310 // that, cancel the previous hovering stream
311 if (actionMasked == AMOTION_EVENT_ACTION_DOWN && lastMemento.hovering) {
312 return true;
313 }
314
315 // Use the previous stream cancellation logic to generate all HOVER_EXIT events.
316 // If this hover event was generated as a result of the pointer leaving the window,
317 // the HOVER_EXIT event should have the same coordinates as the previous
318 // HOVER_MOVE event in this stream. Ensure that all HOVER_EXITs have the same
319 // coordinates as the previous event by cancelling the stream here. With this approach, the
320 // HOVER_EXIT event is generated from the previous event.
321 if (actionMasked == AMOTION_EVENT_ACTION_HOVER_EXIT && lastMemento.hovering) {
322 return true;
323 }
324
325 // If the stream changes its source, just cancel the current gesture to be safe. It's
326 // possible that the app isn't handling source changes properly
327 if (motionEntry.source != lastMemento.source) {
328 LOG(INFO) << "Canceling stream: last source was "
329 << inputEventSourceToString(lastMemento.source) << " and new event is "
330 << motionEntry;
331 return true;
332 }
333
334 // If the injection is happening into two different displays, the same injected device id
335 // could be going into both. And at this time, if mirroring is active, the same connection
336 // would receive different events from each display. Since the TouchStates are per-display,
337 // it's unlikely that those two streams would be consistent with each other. Therefore,
338 // cancel the previous gesture if the display id changes.
339 if (motionEntry.displayId != lastMemento.displayId) {
340 LOG(INFO) << "Canceling stream: last displayId was "
341 << inputEventSourceToString(lastMemento.displayId) << " and new event is "
342 << motionEntry;
343 return true;
344 }
345
346 return false;
347 }
348
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -0700349 if (isStylusEvent(lastMemento.source, lastMemento.pointerProperties)) {
350 // A stylus is already active.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700351 if (isStylusEvent(motionEntry.source, motionEntry.pointerProperties) &&
352 actionMasked == AMOTION_EVENT_ACTION_DOWN) {
Siarhei Vishniakou3ad54f52023-11-02 16:54:40 -0700353 // If this new event is from a different device, then cancel the old
354 // stylus and allow the new stylus to take over, but only if it's going down.
355 // Otherwise, they will start to race each other.
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700356 return true;
357 }
358
359 // Keep the current stylus gesture.
360 return false;
361 }
362
363 // Cancel the current gesture if this is a start of a new gesture from a new device.
364 if (actionMasked == AMOTION_EVENT_ACTION_DOWN ||
365 actionMasked == AMOTION_EVENT_ACTION_HOVER_ENTER) {
366 return true;
367 }
368 // By default, don't cancel any events.
369 return false;
370}
371
372std::unique_ptr<EventEntry> InputState::cancelConflictingInputStream(const MotionEntry& motionEntry,
373 int32_t resolvedAction) {
374 if (!shouldCancelPreviousStream(motionEntry, resolvedAction)) {
375 return {};
376 }
377
378 const MotionMemento& memento = mMotionMementos.back();
379
380 // Cancel the last device stream
381 std::unique_ptr<MotionEntry> cancelEntry =
382 createCancelEntryForMemento(memento, motionEntry.eventTime);
383
384 if (!trackMotion(*cancelEntry, cancelEntry->action, cancelEntry->flags)) {
385 LOG(FATAL) << "Generated inconsistent cancel event!";
386 }
387 return cancelEntry;
388}
389
390std::unique_ptr<MotionEntry> InputState::createCancelEntryForMemento(const MotionMemento& memento,
391 nsecs_t eventTime) const {
392 const int32_t action =
393 memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL;
394 int32_t flags = memento.flags;
395 if (action == AMOTION_EVENT_ACTION_CANCEL) {
396 flags |= AMOTION_EVENT_FLAG_CANCELED;
397 }
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000398 return std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
399 eventTime, memento.deviceId, memento.source,
400 memento.displayId, memento.policyFlags, action,
401 /*actionButton=*/0, flags, AMETA_NONE,
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700402 /*buttonState=*/0, MotionClassification::NONE,
403 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
404 memento.yPrecision, memento.xCursorPosition,
405 memento.yCursorPosition, memento.downTime,
406 memento.pointerProperties, memento.pointerCoords);
407}
408
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700409std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents(
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700410 nsecs_t currentTime, const CancelationOptions& options) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700411 std::vector<std::unique_ptr<EventEntry>> events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700412 for (KeyMemento& memento : mKeyMementos) {
413 if (shouldCancelKey(memento, options)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700414 events.push_back(
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000415 std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
416 currentTime, memento.deviceId, memento.source,
417 memento.displayId, memento.policyFlags,
418 AKEY_EVENT_ACTION_UP,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700419 memento.flags | AKEY_EVENT_FLAG_CANCELED,
420 memento.keyCode, memento.scanCode, memento.metaState,
Harry Cutts33476232023-01-30 19:57:29 +0000421 /*repeatCount=*/0, memento.downTime));
Garfield Tane84e6f92019-08-29 17:28:41 -0700422 }
423 }
424
425 for (const MotionMemento& memento : mMotionMementos) {
426 if (shouldCancelMotion(memento, options)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000427 if (options.pointerIds == std::nullopt) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700428 events.push_back(createCancelEntryForMemento(memento, currentTime));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000429 } else {
430 std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents =
431 synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(),
432 currentTime);
433 events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()),
434 std::make_move_iterator(pointerCancelEvents.end()));
435 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700436 }
437 }
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700438 return events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700439}
440
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700441std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents(
442 nsecs_t currentTime) {
443 std::vector<std::unique_ptr<EventEntry>> events;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800444 for (MotionMemento& memento : mMotionMementos) {
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700445 if (!isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800446 continue;
447 }
448
449 if (memento.firstNewPointerIdx < 0) {
450 continue;
451 }
452
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700453 std::vector<PointerProperties> pointerProperties;
454 std::vector<PointerCoords> pointerCoords;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800455
456 // We will deliver all pointers the target already knows about
457 for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700458 pointerProperties.push_back(memento.pointerProperties[i]);
459 pointerCoords.push_back(memento.pointerCoords[i]);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800460 }
461
462 // We will send explicit events for all pointers the target doesn't know about
463 for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx);
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700464 i < memento.getPointerCount(); i++) {
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700465 pointerProperties.push_back(memento.pointerProperties[i]);
466 pointerCoords.push_back(memento.pointerCoords[i]);
467
468 const size_t pointerCount = pointerProperties.size();
Svet Ganov5d3bc372020-01-26 23:11:07 -0800469
470 // Down only if the first pointer, pointer down otherwise
471 const int32_t action = (pointerCount <= 1)
472 ? AMOTION_EVENT_ACTION_DOWN
473 : AMOTION_EVENT_ACTION_POINTER_DOWN
474 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
475
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700476 events.push_back(
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000477 std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
478 currentTime, memento.deviceId, memento.source,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700479 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000480 /*actionButton=*/0, memento.flags, AMETA_NONE,
481 /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700482 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
483 memento.yPrecision, memento.xCursorPosition,
484 memento.yCursorPosition, memento.downTime,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700485 pointerProperties, pointerCoords));
Svet Ganov5d3bc372020-01-26 23:11:07 -0800486 }
487
488 memento.firstNewPointerIdx = INVALID_POINTER_INDEX;
489 }
490
491 return events;
492}
493
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000494std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800495 const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds,
496 nsecs_t currentTime) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000497 std::vector<std::unique_ptr<MotionEntry>> events;
498 std::vector<uint32_t> canceledPointerIndices;
499 std::vector<PointerProperties> pointerProperties(MAX_POINTERS);
500 std::vector<PointerCoords> pointerCoords(MAX_POINTERS);
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700501 for (uint32_t pointerIdx = 0; pointerIdx < memento.getPointerCount(); pointerIdx++) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000502 uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id);
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -0700503 pointerProperties[pointerIdx] = memento.pointerProperties[pointerIdx];
504 pointerCoords[pointerIdx] = memento.pointerCoords[pointerIdx];
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800505 if (pointerIds.test(pointerId)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000506 canceledPointerIndices.push_back(pointerIdx);
507 }
508 }
509
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700510 if (canceledPointerIndices.size() == memento.getPointerCount()) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000511 const int32_t action =
512 memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800513 int32_t flags = memento.flags;
514 if (action == AMOTION_EVENT_ACTION_CANCEL) {
515 flags |= AMOTION_EVENT_FLAG_CANCELED;
516 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000517 events.push_back(
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000518 std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
519 currentTime, memento.deviceId, memento.source,
520 memento.displayId, memento.policyFlags, action,
521 /*actionButton=*/0, flags, AMETA_NONE,
522 /*buttonState=*/0, MotionClassification::NONE,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000523 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
524 memento.yPrecision, memento.xCursorPosition,
525 memento.yCursorPosition, memento.downTime,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700526 memento.pointerProperties, memento.pointerCoords));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000527 } else {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800528 // If we aren't canceling all pointers, we need to generate ACTION_POINTER_UP with
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000529 // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the
530 // previously canceled pointers from PointerProperties and PointerCoords, and update
531 // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we
532 // can just slide the remaining pointers to the beginning of the array when a pointer is
533 // canceled.
534 std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(),
535 std::greater<uint32_t>());
536
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700537 uint32_t pointerCount = memento.getPointerCount();
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000538 for (const uint32_t pointerIdx : canceledPointerIndices) {
539 const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL
540 : AMOTION_EVENT_ACTION_POINTER_UP |
541 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
542 events.push_back(
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000543 std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr,
544 currentTime, memento.deviceId, memento.source,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000545 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000546 /*actionButton=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000547 memento.flags | AMOTION_EVENT_FLAG_CANCELED,
Harry Cutts33476232023-01-30 19:57:29 +0000548 AMETA_NONE, /*buttonState=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000549 MotionClassification::NONE,
550 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
551 memento.yPrecision, memento.xCursorPosition,
552 memento.yCursorPosition, memento.downTime,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700553 pointerProperties, pointerCoords));
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000554
555 // Cleanup pointer information
556 pointerProperties.erase(pointerProperties.begin() + pointerIdx);
557 pointerCoords.erase(pointerCoords.begin() + pointerIdx);
558 pointerCount--;
559 }
560 }
561 return events;
562}
563
Garfield Tane84e6f92019-08-29 17:28:41 -0700564void InputState::clear() {
565 mKeyMementos.clear();
566 mMotionMementos.clear();
567 mFallbackKeys.clear();
568}
569
Svet Ganov5d3bc372020-01-26 23:11:07 -0800570void InputState::mergePointerStateTo(InputState& other) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700571 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800572 MotionMemento& memento = mMotionMementos[i];
573 // Since we support split pointers we need to merge touch events
574 // from the same source + device + screen.
Siarhei Vishniakouf4043212023-09-18 19:33:03 -0700575 if (isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800576 bool merged = false;
577 for (size_t j = 0; j < other.mMotionMementos.size(); j++) {
578 MotionMemento& otherMemento = other.mMotionMementos[j];
Garfield Tane84e6f92019-08-29 17:28:41 -0700579 if (memento.deviceId == otherMemento.deviceId &&
580 memento.source == otherMemento.source &&
581 memento.displayId == otherMemento.displayId) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800582 memento.mergePointerStateTo(otherMemento);
583 merged = true;
584 break;
Garfield Tane84e6f92019-08-29 17:28:41 -0700585 }
586 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800587 if (!merged) {
588 memento.firstNewPointerIdx = 0;
589 other.mMotionMementos.push_back(memento);
590 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700591 }
592 }
593}
594
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700595std::optional<int32_t> InputState::getFallbackKey(int32_t originalKeyCode) {
596 auto it = mFallbackKeys.find(originalKeyCode);
597 if (it == mFallbackKeys.end()) {
598 return {};
599 }
600 return it->second;
Garfield Tane84e6f92019-08-29 17:28:41 -0700601}
602
603void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700604 mFallbackKeys.insert_or_assign(originalKeyCode, fallbackKeyCode);
Garfield Tane84e6f92019-08-29 17:28:41 -0700605}
606
607void InputState::removeFallbackKey(int32_t originalKeyCode) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700608 mFallbackKeys.erase(originalKeyCode);
Garfield Tane84e6f92019-08-29 17:28:41 -0700609}
610
611bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) {
612 if (options.keyCode && memento.keyCode != options.keyCode.value()) {
613 return false;
614 }
615
616 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
617 return false;
618 }
619
620 if (options.displayId && memento.displayId != options.displayId.value()) {
621 return false;
622 }
623
624 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000625 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
626 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700627 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000628 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700629 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
630 default:
631 return false;
632 }
633}
634
635bool InputState::shouldCancelMotion(const MotionMemento& memento,
636 const CancelationOptions& options) {
637 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
638 return false;
639 }
640
641 if (options.displayId && memento.displayId != options.displayId.value()) {
642 return false;
643 }
644
645 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000646 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700647 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000648 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700649 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000650 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700651 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
652 default:
653 return false;
654 }
655}
656
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700657std::ostream& operator<<(std::ostream& out, const InputState& state) {
658 if (!state.mMotionMementos.empty()) {
659 out << "mMotionMementos: ";
660 for (const InputState::MotionMemento& memento : state.mMotionMementos) {
661 out << "{deviceId= " << memento.deviceId << ", hovering=" << memento.hovering << "}, ";
662 }
663 }
664 return out;
665}
666
Garfield Tane84e6f92019-08-29 17:28:41 -0700667} // namespace android::inputdispatcher