blob: 4652c2dd128c99039ed18d862dbdac864de2af4b [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
Garfield Tane84e6f92019-08-29 17:28:41 -070031bool InputState::isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const {
32 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 Vishniakoud2770042019-10-29 11:08:14 -070086bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070087 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
88 switch (actionMasked) {
89 case AMOTION_EVENT_ACTION_UP:
90 case AMOTION_EVENT_ACTION_CANCEL: {
Harry Cutts33476232023-01-30 19:57:29 +000091 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -070092 if (index >= 0) {
93 mMotionMementos.erase(mMotionMementos.begin() + index);
94 return true;
95 }
Arthur Hung1a1007b2022-05-11 07:15:01 +000096 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
97 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
98 "displayId=%" PRId32 ", actionMasked=%d",
99 entry.deviceId, entry.source, entry.displayId, actionMasked);
100 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700101 return false;
102 }
103
104 case AMOTION_EVENT_ACTION_DOWN: {
Harry Cutts33476232023-01-30 19:57:29 +0000105 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700106 if (index >= 0) {
107 mMotionMementos.erase(mMotionMementos.begin() + index);
108 }
Harry Cutts33476232023-01-30 19:57:29 +0000109 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700110 return true;
111 }
112
113 case AMOTION_EVENT_ACTION_POINTER_UP:
114 case AMOTION_EVENT_ACTION_POINTER_DOWN:
115 case AMOTION_EVENT_ACTION_MOVE: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700116 if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700117 // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need
118 // to generate cancellation events for these since they're based in relative rather
119 // than absolute units.
120 return true;
121 }
122
Harry Cutts33476232023-01-30 19:57:29 +0000123 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700124
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700125 if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700126 // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all
127 // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral.
128 // Any other value and we need to track the motion so we can send cancellation
129 // events for anything generating fallback events (e.g. DPad keys for joystick
130 // movements).
131 if (index >= 0) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700132 if (entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700133 mMotionMementos.erase(mMotionMementos.begin() + index);
134 } else {
135 MotionMemento& memento = mMotionMementos[index];
136 memento.setPointers(entry);
137 }
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700138 } else if (!entry.pointerCoords[0].isEmpty()) {
Harry Cutts33476232023-01-30 19:57:29 +0000139 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700140 }
141
142 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
143 return true;
144 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800145
Garfield Tane84e6f92019-08-29 17:28:41 -0700146 if (index >= 0) {
147 MotionMemento& memento = mMotionMementos[index];
Svet Ganov5d3bc372020-01-26 23:11:07 -0800148 if (memento.firstNewPointerIdx < 0) {
149 memento.setPointers(entry);
150 return true;
151 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700152 }
Arthur Hung1a1007b2022-05-11 07:15:01 +0000153 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
154 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
155 "deviceId=%d, source=%08x, displayId=%" PRId32 ", actionMasked=%d",
156 entry.deviceId, entry.source, entry.displayId, actionMasked);
157 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700158 return false;
159 }
160
161 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
Harry Cutts33476232023-01-30 19:57:29 +0000162 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700163 if (index >= 0) {
164 mMotionMementos.erase(mMotionMementos.begin() + index);
165 return true;
166 }
Arthur Hung1a1007b2022-05-11 07:15:01 +0000167 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
168 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, "
169 "displayId=%" PRId32,
170 entry.deviceId, entry.source, entry.displayId);
171 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700172 return false;
173 }
174
175 case AMOTION_EVENT_ACTION_HOVER_ENTER:
176 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
Harry Cutts33476232023-01-30 19:57:29 +0000177 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700178 if (index >= 0) {
179 mMotionMementos.erase(mMotionMementos.begin() + index);
180 }
Harry Cutts33476232023-01-30 19:57:29 +0000181 addMotionMemento(entry, flags, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700182 return true;
183 }
184
185 default:
186 return true;
187 }
188}
189
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700190ssize_t InputState::findKeyMemento(const KeyEntry& entry) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700191 for (size_t i = 0; i < mKeyMementos.size(); i++) {
192 const KeyMemento& memento = mKeyMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700193 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
194 memento.displayId == entry.displayId && memento.keyCode == entry.keyCode &&
195 memento.scanCode == entry.scanCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700196 return i;
197 }
198 }
199 return -1;
200}
201
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700202ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700203 for (size_t i = 0; i < mMotionMementos.size(); i++) {
204 const MotionMemento& memento = mMotionMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700205 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
206 memento.displayId == entry.displayId && memento.hovering == hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700207 return i;
208 }
209 }
210 return -1;
211}
212
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700213void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700214 KeyMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700215 memento.deviceId = entry.deviceId;
216 memento.source = entry.source;
217 memento.displayId = entry.displayId;
218 memento.keyCode = entry.keyCode;
219 memento.scanCode = entry.scanCode;
220 memento.metaState = entry.metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700221 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700222 memento.downTime = entry.downTime;
223 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700224 mKeyMementos.push_back(memento);
225}
226
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700227void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700228 MotionMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700229 memento.deviceId = entry.deviceId;
230 memento.source = entry.source;
231 memento.displayId = entry.displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700232 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700233 memento.xPrecision = entry.xPrecision;
234 memento.yPrecision = entry.yPrecision;
235 memento.xCursorPosition = entry.xCursorPosition;
236 memento.yCursorPosition = entry.yCursorPosition;
237 memento.downTime = entry.downTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700238 memento.setPointers(entry);
239 memento.hovering = hovering;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700240 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700241 mMotionMementos.push_back(memento);
242}
243
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700244void InputState::MotionMemento::setPointers(const MotionEntry& entry) {
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800245 pointerCount = 0;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700246 for (uint32_t i = 0; i < entry.pointerCount; i++) {
Siarhei Vishniakou82dc0422023-02-17 23:12:52 -0800247 if (MotionEvent::getActionMasked(entry.action) == AMOTION_EVENT_ACTION_POINTER_UP) {
248 // In POINTER_UP events, the pointer is leaving. Since the action is not stored,
249 // this departing pointer should not be recorded.
250 const uint8_t actionIndex = MotionEvent::getActionIndex(entry.action);
251 if (i == actionIndex) {
252 continue;
253 }
254 }
255 pointerProperties[pointerCount].copyFrom(entry.pointerProperties[i]);
256 pointerCoords[pointerCount].copyFrom(entry.pointerCoords[i]);
257 pointerCount++;
Garfield Tane84e6f92019-08-29 17:28:41 -0700258 }
259}
260
Svet Ganov5d3bc372020-01-26 23:11:07 -0800261void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const {
262 for (uint32_t i = 0; i < pointerCount; i++) {
263 if (other.firstNewPointerIdx < 0) {
264 other.firstNewPointerIdx = other.pointerCount;
265 }
266 other.pointerProperties[other.pointerCount].copyFrom(pointerProperties[i]);
267 other.pointerCoords[other.pointerCount].copyFrom(pointerCoords[i]);
268 other.pointerCount++;
269 }
270}
271
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700272std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents(
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700273 nsecs_t currentTime, const CancelationOptions& options) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700274 std::vector<std::unique_ptr<EventEntry>> events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700275 for (KeyMemento& memento : mKeyMementos) {
276 if (shouldCancelKey(memento, options)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700277 events.push_back(
278 std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId,
279 memento.source, memento.displayId,
280 memento.policyFlags, AKEY_EVENT_ACTION_UP,
281 memento.flags | AKEY_EVENT_FLAG_CANCELED,
282 memento.keyCode, memento.scanCode, memento.metaState,
Harry Cutts33476232023-01-30 19:57:29 +0000283 /*repeatCount=*/0, memento.downTime));
Garfield Tane84e6f92019-08-29 17:28:41 -0700284 }
285 }
286
287 for (const MotionMemento& memento : mMotionMementos) {
288 if (shouldCancelMotion(memento, options)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000289 if (options.pointerIds == std::nullopt) {
290 const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
291 : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800292 int32_t flags = memento.flags;
293 if (action == AMOTION_EVENT_ACTION_CANCEL) {
294 flags |= AMOTION_EVENT_FLAG_CANCELED;
295 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000296 events.push_back(
297 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
298 memento.deviceId, memento.source,
299 memento.displayId, memento.policyFlags,
Harry Cutts33476232023-01-30 19:57:29 +0000300 action, /*actionButton=*/0, flags, AMETA_NONE,
301 /*buttonState=*/0, MotionClassification::NONE,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000302 AMOTION_EVENT_EDGE_FLAG_NONE,
303 memento.xPrecision, memento.yPrecision,
304 memento.xCursorPosition,
305 memento.yCursorPosition, memento.downTime,
306 memento.pointerCount,
307 memento.pointerProperties,
308 memento.pointerCoords));
309 } else {
310 std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents =
311 synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(),
312 currentTime);
313 events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()),
314 std::make_move_iterator(pointerCancelEvents.end()));
315 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700316 }
317 }
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700318 return events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700319}
320
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700321std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents(
322 nsecs_t currentTime) {
323 std::vector<std::unique_ptr<EventEntry>> events;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800324 for (MotionMemento& memento : mMotionMementos) {
325 if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) {
326 continue;
327 }
328
329 if (memento.firstNewPointerIdx < 0) {
330 continue;
331 }
332
333 uint32_t pointerCount = 0;
334 PointerProperties pointerProperties[MAX_POINTERS];
335 PointerCoords pointerCoords[MAX_POINTERS];
336
337 // We will deliver all pointers the target already knows about
338 for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) {
339 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
340 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
341 pointerCount++;
342 }
343
344 // We will send explicit events for all pointers the target doesn't know about
345 for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx);
346 i < memento.pointerCount; i++) {
347
348 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
349 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
350 pointerCount++;
351
352 // Down only if the first pointer, pointer down otherwise
353 const int32_t action = (pointerCount <= 1)
354 ? AMOTION_EVENT_ACTION_DOWN
355 : AMOTION_EVENT_ACTION_POINTER_DOWN
356 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
357
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700358 events.push_back(
359 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
360 memento.deviceId, memento.source,
361 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000362 /*actionButton=*/0, memento.flags, AMETA_NONE,
363 /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700364 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
365 memento.yPrecision, memento.xCursorPosition,
366 memento.yCursorPosition, memento.downTime,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000367 pointerCount, pointerProperties, pointerCoords));
Svet Ganov5d3bc372020-01-26 23:11:07 -0800368 }
369
370 memento.firstNewPointerIdx = INVALID_POINTER_INDEX;
371 }
372
373 return events;
374}
375
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000376std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800377 const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds,
378 nsecs_t currentTime) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000379 std::vector<std::unique_ptr<MotionEntry>> events;
380 std::vector<uint32_t> canceledPointerIndices;
381 std::vector<PointerProperties> pointerProperties(MAX_POINTERS);
382 std::vector<PointerCoords> pointerCoords(MAX_POINTERS);
383 for (uint32_t pointerIdx = 0; pointerIdx < memento.pointerCount; pointerIdx++) {
384 uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id);
385 pointerProperties[pointerIdx].copyFrom(memento.pointerProperties[pointerIdx]);
386 pointerCoords[pointerIdx].copyFrom(memento.pointerCoords[pointerIdx]);
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800387 if (pointerIds.test(pointerId)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000388 canceledPointerIndices.push_back(pointerIdx);
389 }
390 }
391
392 if (canceledPointerIndices.size() == memento.pointerCount) {
393 const int32_t action =
394 memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800395 int32_t flags = memento.flags;
396 if (action == AMOTION_EVENT_ACTION_CANCEL) {
397 flags |= AMOTION_EVENT_FLAG_CANCELED;
398 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000399 events.push_back(
400 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId,
401 memento.source, memento.displayId,
Harry Cutts33476232023-01-30 19:57:29 +0000402 memento.policyFlags, action, /*actionButton=*/0,
403 flags, AMETA_NONE, /*buttonState=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000404 MotionClassification::NONE,
405 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
406 memento.yPrecision, memento.xCursorPosition,
407 memento.yCursorPosition, memento.downTime,
408 memento.pointerCount, memento.pointerProperties,
409 memento.pointerCoords));
410 } else {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800411 // If we aren't canceling all pointers, we need to generate ACTION_POINTER_UP with
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000412 // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the
413 // previously canceled pointers from PointerProperties and PointerCoords, and update
414 // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we
415 // can just slide the remaining pointers to the beginning of the array when a pointer is
416 // canceled.
417 std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(),
418 std::greater<uint32_t>());
419
420 uint32_t pointerCount = memento.pointerCount;
421 for (const uint32_t pointerIdx : canceledPointerIndices) {
422 const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL
423 : AMOTION_EVENT_ACTION_POINTER_UP |
424 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
425 events.push_back(
426 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
427 memento.deviceId, memento.source,
428 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*actionButton=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000430 memento.flags | AMOTION_EVENT_FLAG_CANCELED,
Harry Cutts33476232023-01-30 19:57:29 +0000431 AMETA_NONE, /*buttonState=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000432 MotionClassification::NONE,
433 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
434 memento.yPrecision, memento.xCursorPosition,
435 memento.yCursorPosition, memento.downTime,
436 pointerCount, pointerProperties.data(),
437 pointerCoords.data()));
438
439 // Cleanup pointer information
440 pointerProperties.erase(pointerProperties.begin() + pointerIdx);
441 pointerCoords.erase(pointerCoords.begin() + pointerIdx);
442 pointerCount--;
443 }
444 }
445 return events;
446}
447
Garfield Tane84e6f92019-08-29 17:28:41 -0700448void InputState::clear() {
449 mKeyMementos.clear();
450 mMotionMementos.clear();
451 mFallbackKeys.clear();
452}
453
Svet Ganov5d3bc372020-01-26 23:11:07 -0800454void InputState::mergePointerStateTo(InputState& other) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700455 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800456 MotionMemento& memento = mMotionMementos[i];
457 // Since we support split pointers we need to merge touch events
458 // from the same source + device + screen.
Garfield Tane84e6f92019-08-29 17:28:41 -0700459 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800460 bool merged = false;
461 for (size_t j = 0; j < other.mMotionMementos.size(); j++) {
462 MotionMemento& otherMemento = other.mMotionMementos[j];
Garfield Tane84e6f92019-08-29 17:28:41 -0700463 if (memento.deviceId == otherMemento.deviceId &&
464 memento.source == otherMemento.source &&
465 memento.displayId == otherMemento.displayId) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800466 memento.mergePointerStateTo(otherMemento);
467 merged = true;
468 break;
Garfield Tane84e6f92019-08-29 17:28:41 -0700469 }
470 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800471 if (!merged) {
472 memento.firstNewPointerIdx = 0;
473 other.mMotionMementos.push_back(memento);
474 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700475 }
476 }
477}
478
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700479std::optional<int32_t> InputState::getFallbackKey(int32_t originalKeyCode) {
480 auto it = mFallbackKeys.find(originalKeyCode);
481 if (it == mFallbackKeys.end()) {
482 return {};
483 }
484 return it->second;
Garfield Tane84e6f92019-08-29 17:28:41 -0700485}
486
487void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700488 mFallbackKeys.insert_or_assign(originalKeyCode, fallbackKeyCode);
Garfield Tane84e6f92019-08-29 17:28:41 -0700489}
490
491void InputState::removeFallbackKey(int32_t originalKeyCode) {
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700492 mFallbackKeys.erase(originalKeyCode);
Garfield Tane84e6f92019-08-29 17:28:41 -0700493}
494
495bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) {
496 if (options.keyCode && memento.keyCode != options.keyCode.value()) {
497 return false;
498 }
499
500 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
501 return false;
502 }
503
504 if (options.displayId && memento.displayId != options.displayId.value()) {
505 return false;
506 }
507
508 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000509 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
510 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700511 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000512 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700513 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
514 default:
515 return false;
516 }
517}
518
519bool InputState::shouldCancelMotion(const MotionMemento& memento,
520 const CancelationOptions& options) {
521 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
522 return false;
523 }
524
525 if (options.displayId && memento.displayId != options.displayId.value()) {
526 return false;
527 }
528
529 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000530 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700531 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000532 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700533 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000534 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700535 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
536 default:
537 return false;
538 }
539}
540
541} // namespace android::inputdispatcher