blob: cc9cc4efb28f3e5e38cc34d41209036fe0535c31 [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) {
Garfield Tane84e6f92019-08-29 17:28:41 -070045 for (size_t i = 0; i < mFallbackKeys.size();) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070046 if (mFallbackKeys.valueAt(i) == entry.keyCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -070047 mFallbackKeys.removeItemsAt(i);
48 } else {
49 i += 1;
50 }
51 }
52 }
53 ssize_t index = findKeyMemento(entry);
54 if (index >= 0) {
55 mKeyMementos.erase(mKeyMementos.begin() + index);
56 return true;
57 }
58 /* FIXME: We can't just drop the key up event because that prevents creating
59 * popup windows that are automatically shown when a key is held and then
60 * dismissed when the key is released. The problem is that the popup will
61 * not have received the original key down, so the key up will be considered
62 * to be inconsistent with its observed state. We could perhaps handle this
63 * by synthesizing a key down but that will cause other problems.
64 *
65 * So for now, allow inconsistent key up events to be dispatched.
66 *
67 #if DEBUG_OUTBOUND_EVENT_DETAILS
68 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
69 "keyCode=%d, scanCode=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070070 entry.deviceId, entry.source, entry.keyCode, entry.scanCode);
Garfield Tane84e6f92019-08-29 17:28:41 -070071 #endif
72 return false;
73 */
74 return true;
75 }
76
77 case AKEY_EVENT_ACTION_DOWN: {
78 ssize_t index = findKeyMemento(entry);
79 if (index >= 0) {
80 mKeyMementos.erase(mKeyMementos.begin() + index);
81 }
82 addKeyMemento(entry, flags);
83 return true;
84 }
85
86 default:
87 return true;
88 }
89}
90
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070091bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070092 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
93 switch (actionMasked) {
94 case AMOTION_EVENT_ACTION_UP:
95 case AMOTION_EVENT_ACTION_CANCEL: {
Harry Cutts33476232023-01-30 19:57:29 +000096 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -070097 if (index >= 0) {
98 mMotionMementos.erase(mMotionMementos.begin() + index);
99 return true;
100 }
Arthur Hung1a1007b2022-05-11 07:15:01 +0000101 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
102 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
103 "displayId=%" PRId32 ", actionMasked=%d",
104 entry.deviceId, entry.source, entry.displayId, actionMasked);
105 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700106 return false;
107 }
108
109 case AMOTION_EVENT_ACTION_DOWN: {
Harry Cutts33476232023-01-30 19:57:29 +0000110 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700111 if (index >= 0) {
112 mMotionMementos.erase(mMotionMementos.begin() + index);
113 }
Harry Cutts33476232023-01-30 19:57:29 +0000114 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700115 return true;
116 }
117
118 case AMOTION_EVENT_ACTION_POINTER_UP:
119 case AMOTION_EVENT_ACTION_POINTER_DOWN:
120 case AMOTION_EVENT_ACTION_MOVE: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700121 if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700122 // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need
123 // to generate cancellation events for these since they're based in relative rather
124 // than absolute units.
125 return true;
126 }
127
Harry Cutts33476232023-01-30 19:57:29 +0000128 ssize_t index = findMotionMemento(entry, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700129
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700130 if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700131 // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all
132 // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral.
133 // Any other value and we need to track the motion so we can send cancellation
134 // events for anything generating fallback events (e.g. DPad keys for joystick
135 // movements).
136 if (index >= 0) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700137 if (entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700138 mMotionMementos.erase(mMotionMementos.begin() + index);
139 } else {
140 MotionMemento& memento = mMotionMementos[index];
141 memento.setPointers(entry);
142 }
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700143 } else if (!entry.pointerCoords[0].isEmpty()) {
Harry Cutts33476232023-01-30 19:57:29 +0000144 addMotionMemento(entry, flags, /*hovering=*/false);
Garfield Tane84e6f92019-08-29 17:28:41 -0700145 }
146
147 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
148 return true;
149 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800150
Garfield Tane84e6f92019-08-29 17:28:41 -0700151 if (index >= 0) {
152 MotionMemento& memento = mMotionMementos[index];
Svet Ganov5d3bc372020-01-26 23:11:07 -0800153 if (memento.firstNewPointerIdx < 0) {
154 memento.setPointers(entry);
155 return true;
156 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700157 }
Arthur Hung1a1007b2022-05-11 07:15:01 +0000158 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
159 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
160 "deviceId=%d, source=%08x, displayId=%" PRId32 ", actionMasked=%d",
161 entry.deviceId, entry.source, entry.displayId, actionMasked);
162 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700163 return false;
164 }
165
166 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
Harry Cutts33476232023-01-30 19:57:29 +0000167 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700168 if (index >= 0) {
169 mMotionMementos.erase(mMotionMementos.begin() + index);
170 return true;
171 }
Arthur Hung1a1007b2022-05-11 07:15:01 +0000172 if (DEBUG_OUTBOUND_EVENT_DETAILS) {
173 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, "
174 "displayId=%" PRId32,
175 entry.deviceId, entry.source, entry.displayId);
176 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700177 return false;
178 }
179
180 case AMOTION_EVENT_ACTION_HOVER_ENTER:
181 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
Harry Cutts33476232023-01-30 19:57:29 +0000182 ssize_t index = findMotionMemento(entry, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700183 if (index >= 0) {
184 mMotionMementos.erase(mMotionMementos.begin() + index);
185 }
Harry Cutts33476232023-01-30 19:57:29 +0000186 addMotionMemento(entry, flags, /*hovering=*/true);
Garfield Tane84e6f92019-08-29 17:28:41 -0700187 return true;
188 }
189
190 default:
191 return true;
192 }
193}
194
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700195ssize_t InputState::findKeyMemento(const KeyEntry& entry) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700196 for (size_t i = 0; i < mKeyMementos.size(); i++) {
197 const KeyMemento& memento = mKeyMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700198 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
199 memento.displayId == entry.displayId && memento.keyCode == entry.keyCode &&
200 memento.scanCode == entry.scanCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700201 return i;
202 }
203 }
204 return -1;
205}
206
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700207ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700208 for (size_t i = 0; i < mMotionMementos.size(); i++) {
209 const MotionMemento& memento = mMotionMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700210 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
211 memento.displayId == entry.displayId && memento.hovering == hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700212 return i;
213 }
214 }
215 return -1;
216}
217
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700218void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700219 KeyMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700220 memento.deviceId = entry.deviceId;
221 memento.source = entry.source;
222 memento.displayId = entry.displayId;
223 memento.keyCode = entry.keyCode;
224 memento.scanCode = entry.scanCode;
225 memento.metaState = entry.metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700227 memento.downTime = entry.downTime;
228 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700229 mKeyMementos.push_back(memento);
230}
231
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700232void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700233 MotionMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700234 memento.deviceId = entry.deviceId;
235 memento.source = entry.source;
236 memento.displayId = entry.displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700237 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700238 memento.xPrecision = entry.xPrecision;
239 memento.yPrecision = entry.yPrecision;
240 memento.xCursorPosition = entry.xCursorPosition;
241 memento.yCursorPosition = entry.yCursorPosition;
242 memento.downTime = entry.downTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700243 memento.setPointers(entry);
244 memento.hovering = hovering;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700245 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700246 mMotionMementos.push_back(memento);
247}
248
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700249void InputState::MotionMemento::setPointers(const MotionEntry& entry) {
250 pointerCount = entry.pointerCount;
251 for (uint32_t i = 0; i < entry.pointerCount; i++) {
252 pointerProperties[i].copyFrom(entry.pointerProperties[i]);
253 pointerCoords[i].copyFrom(entry.pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700254 }
255}
256
Svet Ganov5d3bc372020-01-26 23:11:07 -0800257void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const {
258 for (uint32_t i = 0; i < pointerCount; i++) {
259 if (other.firstNewPointerIdx < 0) {
260 other.firstNewPointerIdx = other.pointerCount;
261 }
262 other.pointerProperties[other.pointerCount].copyFrom(pointerProperties[i]);
263 other.pointerCoords[other.pointerCount].copyFrom(pointerCoords[i]);
264 other.pointerCount++;
265 }
266}
267
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700268std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents(
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700269 nsecs_t currentTime, const CancelationOptions& options) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700270 std::vector<std::unique_ptr<EventEntry>> events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700271 for (KeyMemento& memento : mKeyMementos) {
272 if (shouldCancelKey(memento, options)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700273 events.push_back(
274 std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId,
275 memento.source, memento.displayId,
276 memento.policyFlags, AKEY_EVENT_ACTION_UP,
277 memento.flags | AKEY_EVENT_FLAG_CANCELED,
278 memento.keyCode, memento.scanCode, memento.metaState,
Harry Cutts33476232023-01-30 19:57:29 +0000279 /*repeatCount=*/0, memento.downTime));
Garfield Tane84e6f92019-08-29 17:28:41 -0700280 }
281 }
282
283 for (const MotionMemento& memento : mMotionMementos) {
284 if (shouldCancelMotion(memento, options)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000285 if (options.pointerIds == std::nullopt) {
286 const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
287 : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800288 int32_t flags = memento.flags;
289 if (action == AMOTION_EVENT_ACTION_CANCEL) {
290 flags |= AMOTION_EVENT_FLAG_CANCELED;
291 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000292 events.push_back(
293 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
294 memento.deviceId, memento.source,
295 memento.displayId, memento.policyFlags,
Harry Cutts33476232023-01-30 19:57:29 +0000296 action, /*actionButton=*/0, flags, AMETA_NONE,
297 /*buttonState=*/0, MotionClassification::NONE,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000298 AMOTION_EVENT_EDGE_FLAG_NONE,
299 memento.xPrecision, memento.yPrecision,
300 memento.xCursorPosition,
301 memento.yCursorPosition, memento.downTime,
302 memento.pointerCount,
303 memento.pointerProperties,
304 memento.pointerCoords));
305 } else {
306 std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents =
307 synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(),
308 currentTime);
309 events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()),
310 std::make_move_iterator(pointerCancelEvents.end()));
311 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700312 }
313 }
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700314 return events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700315}
316
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700317std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents(
318 nsecs_t currentTime) {
319 std::vector<std::unique_ptr<EventEntry>> events;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800320 for (MotionMemento& memento : mMotionMementos) {
321 if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) {
322 continue;
323 }
324
325 if (memento.firstNewPointerIdx < 0) {
326 continue;
327 }
328
329 uint32_t pointerCount = 0;
330 PointerProperties pointerProperties[MAX_POINTERS];
331 PointerCoords pointerCoords[MAX_POINTERS];
332
333 // We will deliver all pointers the target already knows about
334 for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) {
335 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
336 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
337 pointerCount++;
338 }
339
340 // We will send explicit events for all pointers the target doesn't know about
341 for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx);
342 i < memento.pointerCount; i++) {
343
344 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
345 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
346 pointerCount++;
347
348 // Down only if the first pointer, pointer down otherwise
349 const int32_t action = (pointerCount <= 1)
350 ? AMOTION_EVENT_ACTION_DOWN
351 : AMOTION_EVENT_ACTION_POINTER_DOWN
352 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
353
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700354 events.push_back(
355 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
356 memento.deviceId, memento.source,
357 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000358 /*actionButton=*/0, memento.flags, AMETA_NONE,
359 /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700360 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
361 memento.yPrecision, memento.xCursorPosition,
362 memento.yCursorPosition, memento.downTime,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000363 pointerCount, pointerProperties, pointerCoords));
Svet Ganov5d3bc372020-01-26 23:11:07 -0800364 }
365
366 memento.firstNewPointerIdx = INVALID_POINTER_INDEX;
367 }
368
369 return events;
370}
371
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000372std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800373 const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds,
374 nsecs_t currentTime) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000375 std::vector<std::unique_ptr<MotionEntry>> events;
376 std::vector<uint32_t> canceledPointerIndices;
377 std::vector<PointerProperties> pointerProperties(MAX_POINTERS);
378 std::vector<PointerCoords> pointerCoords(MAX_POINTERS);
379 for (uint32_t pointerIdx = 0; pointerIdx < memento.pointerCount; pointerIdx++) {
380 uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id);
381 pointerProperties[pointerIdx].copyFrom(memento.pointerProperties[pointerIdx]);
382 pointerCoords[pointerIdx].copyFrom(memento.pointerCoords[pointerIdx]);
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800383 if (pointerIds.test(pointerId)) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000384 canceledPointerIndices.push_back(pointerIdx);
385 }
386 }
387
388 if (canceledPointerIndices.size() == memento.pointerCount) {
389 const int32_t action =
390 memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800391 int32_t flags = memento.flags;
392 if (action == AMOTION_EVENT_ACTION_CANCEL) {
393 flags |= AMOTION_EVENT_FLAG_CANCELED;
394 }
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000395 events.push_back(
396 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId,
397 memento.source, memento.displayId,
Harry Cutts33476232023-01-30 19:57:29 +0000398 memento.policyFlags, action, /*actionButton=*/0,
399 flags, AMETA_NONE, /*buttonState=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000400 MotionClassification::NONE,
401 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
402 memento.yPrecision, memento.xCursorPosition,
403 memento.yCursorPosition, memento.downTime,
404 memento.pointerCount, memento.pointerProperties,
405 memento.pointerCoords));
406 } else {
Siarhei Vishniakou1ae72f12023-01-29 12:55:30 -0800407 // If we aren't canceling all pointers, we need to generate ACTION_POINTER_UP with
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000408 // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the
409 // previously canceled pointers from PointerProperties and PointerCoords, and update
410 // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we
411 // can just slide the remaining pointers to the beginning of the array when a pointer is
412 // canceled.
413 std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(),
414 std::greater<uint32_t>());
415
416 uint32_t pointerCount = memento.pointerCount;
417 for (const uint32_t pointerIdx : canceledPointerIndices) {
418 const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL
419 : AMOTION_EVENT_ACTION_POINTER_UP |
420 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
421 events.push_back(
422 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
423 memento.deviceId, memento.source,
424 memento.displayId, memento.policyFlags, action,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*actionButton=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000426 memento.flags | AMOTION_EVENT_FLAG_CANCELED,
Harry Cutts33476232023-01-30 19:57:29 +0000427 AMETA_NONE, /*buttonState=*/0,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000428 MotionClassification::NONE,
429 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
430 memento.yPrecision, memento.xCursorPosition,
431 memento.yCursorPosition, memento.downTime,
432 pointerCount, pointerProperties.data(),
433 pointerCoords.data()));
434
435 // Cleanup pointer information
436 pointerProperties.erase(pointerProperties.begin() + pointerIdx);
437 pointerCoords.erase(pointerCoords.begin() + pointerIdx);
438 pointerCount--;
439 }
440 }
441 return events;
442}
443
Garfield Tane84e6f92019-08-29 17:28:41 -0700444void InputState::clear() {
445 mKeyMementos.clear();
446 mMotionMementos.clear();
447 mFallbackKeys.clear();
448}
449
Svet Ganov5d3bc372020-01-26 23:11:07 -0800450void InputState::mergePointerStateTo(InputState& other) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700451 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800452 MotionMemento& memento = mMotionMementos[i];
453 // Since we support split pointers we need to merge touch events
454 // from the same source + device + screen.
Garfield Tane84e6f92019-08-29 17:28:41 -0700455 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800456 bool merged = false;
457 for (size_t j = 0; j < other.mMotionMementos.size(); j++) {
458 MotionMemento& otherMemento = other.mMotionMementos[j];
Garfield Tane84e6f92019-08-29 17:28:41 -0700459 if (memento.deviceId == otherMemento.deviceId &&
460 memento.source == otherMemento.source &&
461 memento.displayId == otherMemento.displayId) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800462 memento.mergePointerStateTo(otherMemento);
463 merged = true;
464 break;
Garfield Tane84e6f92019-08-29 17:28:41 -0700465 }
466 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800467 if (!merged) {
468 memento.firstNewPointerIdx = 0;
469 other.mMotionMementos.push_back(memento);
470 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700471 }
472 }
473}
474
475int32_t InputState::getFallbackKey(int32_t originalKeyCode) {
476 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
477 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
478}
479
480void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) {
481 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
482 if (index >= 0) {
483 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
484 } else {
485 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
486 }
487}
488
489void InputState::removeFallbackKey(int32_t originalKeyCode) {
490 mFallbackKeys.removeItem(originalKeyCode);
491}
492
493bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) {
494 if (options.keyCode && memento.keyCode != options.keyCode.value()) {
495 return false;
496 }
497
498 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
499 return false;
500 }
501
502 if (options.displayId && memento.displayId != options.displayId.value()) {
503 return false;
504 }
505
506 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000507 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
508 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700509 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000510 case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700511 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
512 default:
513 return false;
514 }
515}
516
517bool InputState::shouldCancelMotion(const MotionMemento& memento,
518 const CancelationOptions& options) {
519 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
520 return false;
521 }
522
523 if (options.displayId && memento.displayId != options.displayId.value()) {
524 return false;
525 }
526
527 switch (options.mode) {
Michael Wrightfb04fd52022-11-24 22:31:11 +0000528 case CancelationOptions::Mode::CANCEL_ALL_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700529 return true;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000530 case CancelationOptions::Mode::CANCEL_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700531 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Michael Wrightfb04fd52022-11-24 22:31:11 +0000532 case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS:
Garfield Tane84e6f92019-08-29 17:28:41 -0700533 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
534 default:
535 return false;
536 }
537}
538
539} // namespace android::inputdispatcher