blob: 1656a21b410df76317b47bf0b971bd3abd48240c [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
17#include "InputState.h"
18
Garfield Tanff1f1bb2020-01-28 13:24:04 -080019#include "InputDispatcher.h"
20
Garfield Tane84e6f92019-08-29 17:28:41 -070021namespace android::inputdispatcher {
22
Garfield Tanff1f1bb2020-01-28 13:24:04 -080023InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070024
25InputState::~InputState() {}
26
27bool InputState::isNeutral() const {
28 return mKeyMementos.empty() && mMotionMementos.empty();
29}
30
31bool 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: {
96 ssize_t index = findMotionMemento(entry, false /*hovering*/);
97 if (index >= 0) {
98 mMotionMementos.erase(mMotionMementos.begin() + index);
99 return true;
100 }
101#if DEBUG_OUTBOUND_EVENT_DETAILS
102 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
103 "displayId=%" PRId32 ", actionMasked=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700104 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700105#endif
106 return false;
107 }
108
109 case AMOTION_EVENT_ACTION_DOWN: {
110 ssize_t index = findMotionMemento(entry, false /*hovering*/);
111 if (index >= 0) {
112 mMotionMementos.erase(mMotionMementos.begin() + index);
113 }
114 addMotionMemento(entry, flags, false /*hovering*/);
115 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
128 ssize_t index = findMotionMemento(entry, false /*hovering*/);
129
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()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700144 addMotionMemento(entry, flags, false /*hovering*/);
145 }
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 }
158#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",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700161 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700162#endif
163 return false;
164 }
165
166 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
167 ssize_t index = findMotionMemento(entry, true /*hovering*/);
168 if (index >= 0) {
169 mMotionMementos.erase(mMotionMementos.begin() + index);
170 return true;
171 }
172#if DEBUG_OUTBOUND_EVENT_DETAILS
173 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, "
174 "displayId=%" PRId32,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700175 entry.deviceId, entry.source, entry.displayId);
Garfield Tane84e6f92019-08-29 17:28:41 -0700176#endif
177 return false;
178 }
179
180 case AMOTION_EVENT_ACTION_HOVER_ENTER:
181 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
182 ssize_t index = findMotionMemento(entry, true /*hovering*/);
183 if (index >= 0) {
184 mMotionMementos.erase(mMotionMementos.begin() + index);
185 }
186 addMotionMemento(entry, flags, true /*hovering*/);
187 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,
279 0 /*repeatCount*/, memento.downTime));
Garfield Tane84e6f92019-08-29 17:28:41 -0700280 }
281 }
282
283 for (const MotionMemento& memento : mMotionMementos) {
284 if (shouldCancelMotion(memento, options)) {
285 const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
286 : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700287 events.push_back(
288 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
289 memento.deviceId, memento.source,
290 memento.displayId, memento.policyFlags, action,
291 0 /*actionButton*/, memento.flags, AMETA_NONE,
292 0 /*buttonState*/, MotionClassification::NONE,
293 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
294 memento.yPrecision, memento.xCursorPosition,
295 memento.yCursorPosition, memento.downTime,
296 memento.pointerCount, memento.pointerProperties,
297 memento.pointerCoords, 0 /*xOffset*/,
298 0 /*yOffset*/));
Garfield Tane84e6f92019-08-29 17:28:41 -0700299 }
300 }
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700301 return events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700302}
303
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700304std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents(
305 nsecs_t currentTime) {
306 std::vector<std::unique_ptr<EventEntry>> events;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800307 for (MotionMemento& memento : mMotionMementos) {
308 if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) {
309 continue;
310 }
311
312 if (memento.firstNewPointerIdx < 0) {
313 continue;
314 }
315
316 uint32_t pointerCount = 0;
317 PointerProperties pointerProperties[MAX_POINTERS];
318 PointerCoords pointerCoords[MAX_POINTERS];
319
320 // We will deliver all pointers the target already knows about
321 for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) {
322 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
323 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
324 pointerCount++;
325 }
326
327 // We will send explicit events for all pointers the target doesn't know about
328 for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx);
329 i < memento.pointerCount; i++) {
330
331 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
332 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
333 pointerCount++;
334
335 // Down only if the first pointer, pointer down otherwise
336 const int32_t action = (pointerCount <= 1)
337 ? AMOTION_EVENT_ACTION_DOWN
338 : AMOTION_EVENT_ACTION_POINTER_DOWN
339 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
340
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700341 events.push_back(
342 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
343 memento.deviceId, memento.source,
344 memento.displayId, memento.policyFlags, action,
345 0 /*actionButton*/, memento.flags, AMETA_NONE,
346 0 /*buttonState*/, MotionClassification::NONE,
347 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
348 memento.yPrecision, memento.xCursorPosition,
349 memento.yCursorPosition, memento.downTime,
350 pointerCount, pointerProperties, pointerCoords,
351 0 /*xOffset*/, 0 /*yOffset*/));
Svet Ganov5d3bc372020-01-26 23:11:07 -0800352 }
353
354 memento.firstNewPointerIdx = INVALID_POINTER_INDEX;
355 }
356
357 return events;
358}
359
Garfield Tane84e6f92019-08-29 17:28:41 -0700360void InputState::clear() {
361 mKeyMementos.clear();
362 mMotionMementos.clear();
363 mFallbackKeys.clear();
364}
365
Svet Ganov5d3bc372020-01-26 23:11:07 -0800366void InputState::mergePointerStateTo(InputState& other) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700367 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800368 MotionMemento& memento = mMotionMementos[i];
369 // Since we support split pointers we need to merge touch events
370 // from the same source + device + screen.
Garfield Tane84e6f92019-08-29 17:28:41 -0700371 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800372 bool merged = false;
373 for (size_t j = 0; j < other.mMotionMementos.size(); j++) {
374 MotionMemento& otherMemento = other.mMotionMementos[j];
Garfield Tane84e6f92019-08-29 17:28:41 -0700375 if (memento.deviceId == otherMemento.deviceId &&
376 memento.source == otherMemento.source &&
377 memento.displayId == otherMemento.displayId) {
Svet Ganov5d3bc372020-01-26 23:11:07 -0800378 memento.mergePointerStateTo(otherMemento);
379 merged = true;
380 break;
Garfield Tane84e6f92019-08-29 17:28:41 -0700381 }
382 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800383 if (!merged) {
384 memento.firstNewPointerIdx = 0;
385 other.mMotionMementos.push_back(memento);
386 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700387 }
388 }
389}
390
391int32_t InputState::getFallbackKey(int32_t originalKeyCode) {
392 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
393 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
394}
395
396void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) {
397 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
398 if (index >= 0) {
399 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
400 } else {
401 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
402 }
403}
404
405void InputState::removeFallbackKey(int32_t originalKeyCode) {
406 mFallbackKeys.removeItem(originalKeyCode);
407}
408
409bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) {
410 if (options.keyCode && memento.keyCode != options.keyCode.value()) {
411 return false;
412 }
413
414 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
415 return false;
416 }
417
418 if (options.displayId && memento.displayId != options.displayId.value()) {
419 return false;
420 }
421
422 switch (options.mode) {
423 case CancelationOptions::CANCEL_ALL_EVENTS:
424 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
425 return true;
426 case CancelationOptions::CANCEL_FALLBACK_EVENTS:
427 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
428 default:
429 return false;
430 }
431}
432
433bool InputState::shouldCancelMotion(const MotionMemento& memento,
434 const CancelationOptions& options) {
435 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
436 return false;
437 }
438
439 if (options.displayId && memento.displayId != options.displayId.value()) {
440 return false;
441 }
442
443 switch (options.mode) {
444 case CancelationOptions::CANCEL_ALL_EVENTS:
445 return true;
446 case CancelationOptions::CANCEL_POINTER_EVENTS:
447 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
448 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
449 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
450 default:
451 return false;
452 }
453}
454
455} // namespace android::inputdispatcher