blob: ad3c6159ef433c79a4175876d902d3cee2244ffe [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
Chris Yef59a2f42020-10-16 12:55:26 -070017#include "input/InputDevice.h"
18
Garfield Tane84e6f92019-08-29 17:28:41 -070019#include "InputState.h"
20
Garfield Tanff1f1bb2020-01-28 13:24:04 -080021#include "InputDispatcher.h"
22
Garfield Tane84e6f92019-08-29 17:28:41 -070023namespace android::inputdispatcher {
24
Garfield Tanff1f1bb2020-01-28 13:24:04 -080025InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070026
27InputState::~InputState() {}
28
29bool InputState::isNeutral() const {
30 return mKeyMementos.empty() && mMotionMementos.empty();
31}
32
33bool InputState::isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const {
34 for (const MotionMemento& memento : mMotionMementos) {
35 if (memento.deviceId == deviceId && memento.source == source &&
36 memento.displayId == displayId && memento.hovering) {
37 return true;
38 }
39 }
40 return false;
41}
42
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070043bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070044 switch (action) {
45 case AKEY_EVENT_ACTION_UP: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070046 if (entry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Garfield Tane84e6f92019-08-29 17:28:41 -070047 for (size_t i = 0; i < mFallbackKeys.size();) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070048 if (mFallbackKeys.valueAt(i) == entry.keyCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -070049 mFallbackKeys.removeItemsAt(i);
50 } else {
51 i += 1;
52 }
53 }
54 }
55 ssize_t index = findKeyMemento(entry);
56 if (index >= 0) {
57 mKeyMementos.erase(mKeyMementos.begin() + index);
58 return true;
59 }
60 /* FIXME: We can't just drop the key up event because that prevents creating
61 * popup windows that are automatically shown when a key is held and then
62 * dismissed when the key is released. The problem is that the popup will
63 * not have received the original key down, so the key up will be considered
64 * to be inconsistent with its observed state. We could perhaps handle this
65 * by synthesizing a key down but that will cause other problems.
66 *
67 * So for now, allow inconsistent key up events to be dispatched.
68 *
69 #if DEBUG_OUTBOUND_EVENT_DETAILS
70 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
71 "keyCode=%d, scanCode=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070072 entry.deviceId, entry.source, entry.keyCode, entry.scanCode);
Garfield Tane84e6f92019-08-29 17:28:41 -070073 #endif
74 return false;
75 */
76 return true;
77 }
78
79 case AKEY_EVENT_ACTION_DOWN: {
80 ssize_t index = findKeyMemento(entry);
81 if (index >= 0) {
82 mKeyMementos.erase(mKeyMementos.begin() + index);
83 }
84 addKeyMemento(entry, flags);
85 return true;
86 }
87
88 default:
89 return true;
90 }
91}
92
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070093bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070094 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
95 switch (actionMasked) {
96 case AMOTION_EVENT_ACTION_UP:
97 case AMOTION_EVENT_ACTION_CANCEL: {
98 ssize_t index = findMotionMemento(entry, false /*hovering*/);
99 if (index >= 0) {
100 mMotionMementos.erase(mMotionMementos.begin() + index);
101 return true;
102 }
103#if DEBUG_OUTBOUND_EVENT_DETAILS
104 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
105 "displayId=%" PRId32 ", actionMasked=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700106 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700107#endif
108 return false;
109 }
110
111 case AMOTION_EVENT_ACTION_DOWN: {
112 ssize_t index = findMotionMemento(entry, false /*hovering*/);
113 if (index >= 0) {
114 mMotionMementos.erase(mMotionMementos.begin() + index);
115 }
116 addMotionMemento(entry, flags, false /*hovering*/);
117 return true;
118 }
119
120 case AMOTION_EVENT_ACTION_POINTER_UP:
121 case AMOTION_EVENT_ACTION_POINTER_DOWN:
122 case AMOTION_EVENT_ACTION_MOVE: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700123 if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700124 // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need
125 // to generate cancellation events for these since they're based in relative rather
126 // than absolute units.
127 return true;
128 }
129
130 ssize_t index = findMotionMemento(entry, false /*hovering*/);
131
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700132 if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700133 // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all
134 // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral.
135 // Any other value and we need to track the motion so we can send cancellation
136 // events for anything generating fallback events (e.g. DPad keys for joystick
137 // movements).
138 if (index >= 0) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700139 if (entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700140 mMotionMementos.erase(mMotionMementos.begin() + index);
141 } else {
142 MotionMemento& memento = mMotionMementos[index];
143 memento.setPointers(entry);
144 }
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700145 } else if (!entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700146 addMotionMemento(entry, flags, false /*hovering*/);
147 }
148
149 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
150 return true;
151 }
Svet Ganov5d3bc372020-01-26 23:11:07 -0800152
Garfield Tane84e6f92019-08-29 17:28:41 -0700153 if (index >= 0) {
154 MotionMemento& memento = mMotionMementos[index];
Svet Ganov5d3bc372020-01-26 23:11:07 -0800155 if (memento.firstNewPointerIdx < 0) {
156 memento.setPointers(entry);
157 return true;
158 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700159 }
160#if DEBUG_OUTBOUND_EVENT_DETAILS
161 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
162 "deviceId=%d, source=%08x, displayId=%" PRId32 ", actionMasked=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700163 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700164#endif
165 return false;
166 }
167
168 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
169 ssize_t index = findMotionMemento(entry, true /*hovering*/);
170 if (index >= 0) {
171 mMotionMementos.erase(mMotionMementos.begin() + index);
172 return true;
173 }
174#if DEBUG_OUTBOUND_EVENT_DETAILS
175 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, "
176 "displayId=%" PRId32,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700177 entry.deviceId, entry.source, entry.displayId);
Garfield Tane84e6f92019-08-29 17:28:41 -0700178#endif
179 return false;
180 }
181
182 case AMOTION_EVENT_ACTION_HOVER_ENTER:
183 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
184 ssize_t index = findMotionMemento(entry, true /*hovering*/);
185 if (index >= 0) {
186 mMotionMementos.erase(mMotionMementos.begin() + index);
187 }
188 addMotionMemento(entry, flags, true /*hovering*/);
189 return true;
190 }
191
192 default:
193 return true;
194 }
195}
196
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700197ssize_t InputState::findKeyMemento(const KeyEntry& entry) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700198 for (size_t i = 0; i < mKeyMementos.size(); i++) {
199 const KeyMemento& memento = mKeyMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700200 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
201 memento.displayId == entry.displayId && memento.keyCode == entry.keyCode &&
202 memento.scanCode == entry.scanCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700203 return i;
204 }
205 }
206 return -1;
207}
208
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700209ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700210 for (size_t i = 0; i < mMotionMementos.size(); i++) {
211 const MotionMemento& memento = mMotionMementos[i];
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700212 if (memento.deviceId == entry.deviceId && memento.source == entry.source &&
213 memento.displayId == entry.displayId && memento.hovering == hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700214 return i;
215 }
216 }
217 return -1;
218}
219
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700220void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700221 KeyMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700222 memento.deviceId = entry.deviceId;
223 memento.source = entry.source;
224 memento.displayId = entry.displayId;
225 memento.keyCode = entry.keyCode;
226 memento.scanCode = entry.scanCode;
227 memento.metaState = entry.metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700228 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700229 memento.downTime = entry.downTime;
230 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700231 mKeyMementos.push_back(memento);
232}
233
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700234void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700235 MotionMemento memento;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700236 memento.deviceId = entry.deviceId;
237 memento.source = entry.source;
238 memento.displayId = entry.displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700239 memento.flags = flags;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700240 memento.xPrecision = entry.xPrecision;
241 memento.yPrecision = entry.yPrecision;
242 memento.xCursorPosition = entry.xCursorPosition;
243 memento.yCursorPosition = entry.yCursorPosition;
244 memento.downTime = entry.downTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700245 memento.setPointers(entry);
246 memento.hovering = hovering;
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700247 memento.policyFlags = entry.policyFlags;
Garfield Tane84e6f92019-08-29 17:28:41 -0700248 mMotionMementos.push_back(memento);
249}
250
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700251void InputState::MotionMemento::setPointers(const MotionEntry& entry) {
252 pointerCount = entry.pointerCount;
253 for (uint32_t i = 0; i < entry.pointerCount; i++) {
254 pointerProperties[i].copyFrom(entry.pointerProperties[i]);
255 pointerCoords[i].copyFrom(entry.pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700256 }
257}
258
Svet Ganov5d3bc372020-01-26 23:11:07 -0800259void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const {
260 for (uint32_t i = 0; i < pointerCount; i++) {
261 if (other.firstNewPointerIdx < 0) {
262 other.firstNewPointerIdx = other.pointerCount;
263 }
264 other.pointerProperties[other.pointerCount].copyFrom(pointerProperties[i]);
265 other.pointerCoords[other.pointerCount].copyFrom(pointerCoords[i]);
266 other.pointerCount++;
267 }
268}
269
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700270std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents(
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700271 nsecs_t currentTime, const CancelationOptions& options) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700272 std::vector<std::unique_ptr<EventEntry>> events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700273 for (KeyMemento& memento : mKeyMementos) {
274 if (shouldCancelKey(memento, options)) {
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700275 events.push_back(
276 std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId,
277 memento.source, memento.displayId,
278 memento.policyFlags, AKEY_EVENT_ACTION_UP,
279 memento.flags | AKEY_EVENT_FLAG_CANCELED,
280 memento.keyCode, memento.scanCode, memento.metaState,
281 0 /*repeatCount*/, memento.downTime));
Garfield Tane84e6f92019-08-29 17:28:41 -0700282 }
283 }
284
285 for (const MotionMemento& memento : mMotionMementos) {
286 if (shouldCancelMotion(memento, options)) {
287 const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
288 : AMOTION_EVENT_ACTION_CANCEL;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700289 events.push_back(
290 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
291 memento.deviceId, memento.source,
292 memento.displayId, memento.policyFlags, action,
293 0 /*actionButton*/, memento.flags, AMETA_NONE,
294 0 /*buttonState*/, MotionClassification::NONE,
295 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
296 memento.yPrecision, memento.xCursorPosition,
297 memento.yCursorPosition, memento.downTime,
298 memento.pointerCount, memento.pointerProperties,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000299 memento.pointerCoords));
Garfield Tane84e6f92019-08-29 17:28:41 -0700300 }
301 }
Siarhei Vishniakou00fca7c2019-10-29 13:05:57 -0700302 return events;
Garfield Tane84e6f92019-08-29 17:28:41 -0700303}
304
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700305std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents(
306 nsecs_t currentTime) {
307 std::vector<std::unique_ptr<EventEntry>> events;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800308 for (MotionMemento& memento : mMotionMementos) {
309 if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) {
310 continue;
311 }
312
313 if (memento.firstNewPointerIdx < 0) {
314 continue;
315 }
316
317 uint32_t pointerCount = 0;
318 PointerProperties pointerProperties[MAX_POINTERS];
319 PointerCoords pointerCoords[MAX_POINTERS];
320
321 // We will deliver all pointers the target already knows about
322 for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) {
323 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
324 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
325 pointerCount++;
326 }
327
328 // We will send explicit events for all pointers the target doesn't know about
329 for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx);
330 i < memento.pointerCount; i++) {
331
332 pointerProperties[i].copyFrom(memento.pointerProperties[i]);
333 pointerCoords[i].copyFrom(memento.pointerCoords[i]);
334 pointerCount++;
335
336 // Down only if the first pointer, pointer down otherwise
337 const int32_t action = (pointerCount <= 1)
338 ? AMOTION_EVENT_ACTION_DOWN
339 : AMOTION_EVENT_ACTION_POINTER_DOWN
340 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
341
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700342 events.push_back(
343 std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime,
344 memento.deviceId, memento.source,
345 memento.displayId, memento.policyFlags, action,
346 0 /*actionButton*/, memento.flags, AMETA_NONE,
347 0 /*buttonState*/, MotionClassification::NONE,
348 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
349 memento.yPrecision, memento.xCursorPosition,
350 memento.yCursorPosition, memento.downTime,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000351 pointerCount, pointerProperties, pointerCoords));
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