blob: 603c257c4dc0b7ef498eea82c259aff8f97c2f87 [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
19namespace android::inputdispatcher {
20
21InputState::InputState() {}
22
23InputState::~InputState() {}
24
25bool InputState::isNeutral() const {
26 return mKeyMementos.empty() && mMotionMementos.empty();
27}
28
29bool InputState::isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const {
30 for (const MotionMemento& memento : mMotionMementos) {
31 if (memento.deviceId == deviceId && memento.source == source &&
32 memento.displayId == displayId && memento.hovering) {
33 return true;
34 }
35 }
36 return false;
37}
38
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070039bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070040 switch (action) {
41 case AKEY_EVENT_ACTION_UP: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070042 if (entry.flags & AKEY_EVENT_FLAG_FALLBACK) {
Garfield Tane84e6f92019-08-29 17:28:41 -070043 for (size_t i = 0; i < mFallbackKeys.size();) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070044 if (mFallbackKeys.valueAt(i) == entry.keyCode) {
Garfield Tane84e6f92019-08-29 17:28:41 -070045 mFallbackKeys.removeItemsAt(i);
46 } else {
47 i += 1;
48 }
49 }
50 }
51 ssize_t index = findKeyMemento(entry);
52 if (index >= 0) {
53 mKeyMementos.erase(mKeyMementos.begin() + index);
54 return true;
55 }
56 /* FIXME: We can't just drop the key up event because that prevents creating
57 * popup windows that are automatically shown when a key is held and then
58 * dismissed when the key is released. The problem is that the popup will
59 * not have received the original key down, so the key up will be considered
60 * to be inconsistent with its observed state. We could perhaps handle this
61 * by synthesizing a key down but that will cause other problems.
62 *
63 * So for now, allow inconsistent key up events to be dispatched.
64 *
65 #if DEBUG_OUTBOUND_EVENT_DETAILS
66 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
67 "keyCode=%d, scanCode=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070068 entry.deviceId, entry.source, entry.keyCode, entry.scanCode);
Garfield Tane84e6f92019-08-29 17:28:41 -070069 #endif
70 return false;
71 */
72 return true;
73 }
74
75 case AKEY_EVENT_ACTION_DOWN: {
76 ssize_t index = findKeyMemento(entry);
77 if (index >= 0) {
78 mKeyMementos.erase(mKeyMementos.begin() + index);
79 }
80 addKeyMemento(entry, flags);
81 return true;
82 }
83
84 default:
85 return true;
86 }
87}
88
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070089bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) {
Garfield Tane84e6f92019-08-29 17:28:41 -070090 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
91 switch (actionMasked) {
92 case AMOTION_EVENT_ACTION_UP:
93 case AMOTION_EVENT_ACTION_CANCEL: {
94 ssize_t index = findMotionMemento(entry, false /*hovering*/);
95 if (index >= 0) {
96 mMotionMementos.erase(mMotionMementos.begin() + index);
97 return true;
98 }
99#if DEBUG_OUTBOUND_EVENT_DETAILS
100 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
101 "displayId=%" PRId32 ", actionMasked=%d",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700102 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700103#endif
104 return false;
105 }
106
107 case AMOTION_EVENT_ACTION_DOWN: {
108 ssize_t index = findMotionMemento(entry, false /*hovering*/);
109 if (index >= 0) {
110 mMotionMementos.erase(mMotionMementos.begin() + index);
111 }
112 addMotionMemento(entry, flags, false /*hovering*/);
113 return true;
114 }
115
116 case AMOTION_EVENT_ACTION_POINTER_UP:
117 case AMOTION_EVENT_ACTION_POINTER_DOWN:
118 case AMOTION_EVENT_ACTION_MOVE: {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700119 if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700120 // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need
121 // to generate cancellation events for these since they're based in relative rather
122 // than absolute units.
123 return true;
124 }
125
126 ssize_t index = findMotionMemento(entry, false /*hovering*/);
127
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700128 if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700129 // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all
130 // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral.
131 // Any other value and we need to track the motion so we can send cancellation
132 // events for anything generating fallback events (e.g. DPad keys for joystick
133 // movements).
134 if (index >= 0) {
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700135 if (entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700136 mMotionMementos.erase(mMotionMementos.begin() + index);
137 } else {
138 MotionMemento& memento = mMotionMementos[index];
139 memento.setPointers(entry);
140 }
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700141 } else if (!entry.pointerCoords[0].isEmpty()) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700142 addMotionMemento(entry, flags, false /*hovering*/);
143 }
144
145 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
146 return true;
147 }
148 if (index >= 0) {
149 MotionMemento& memento = mMotionMementos[index];
150 memento.setPointers(entry);
151 return true;
152 }
153#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",
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700156 entry.deviceId, entry.source, entry.displayId, actionMasked);
Garfield Tane84e6f92019-08-29 17:28:41 -0700157#endif
158 return false;
159 }
160
161 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
162 ssize_t index = findMotionMemento(entry, true /*hovering*/);
163 if (index >= 0) {
164 mMotionMementos.erase(mMotionMementos.begin() + index);
165 return true;
166 }
167#if DEBUG_OUTBOUND_EVENT_DETAILS
168 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, "
169 "displayId=%" PRId32,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700170 entry.deviceId, entry.source, entry.displayId);
Garfield Tane84e6f92019-08-29 17:28:41 -0700171#endif
172 return false;
173 }
174
175 case AMOTION_EVENT_ACTION_HOVER_ENTER:
176 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
177 ssize_t index = findMotionMemento(entry, true /*hovering*/);
178 if (index >= 0) {
179 mMotionMementos.erase(mMotionMementos.begin() + index);
180 }
181 addMotionMemento(entry, flags, true /*hovering*/);
182 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) {
245 pointerCount = entry.pointerCount;
246 for (uint32_t i = 0; i < entry.pointerCount; i++) {
247 pointerProperties[i].copyFrom(entry.pointerProperties[i]);
248 pointerCoords[i].copyFrom(entry.pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700249 }
250}
251
252void InputState::synthesizeCancelationEvents(nsecs_t currentTime,
253 std::vector<EventEntry*>& outEvents,
254 const CancelationOptions& options) {
255 for (KeyMemento& memento : mKeyMementos) {
256 if (shouldCancelKey(memento, options)) {
257 outEvents.push_back(new KeyEntry(SYNTHESIZED_EVENT_SEQUENCE_NUM, currentTime,
258 memento.deviceId, memento.source, memento.displayId,
259 memento.policyFlags, AKEY_EVENT_ACTION_UP,
260 memento.flags | AKEY_EVENT_FLAG_CANCELED,
261 memento.keyCode, memento.scanCode, memento.metaState,
262 0, memento.downTime));
263 }
264 }
265
266 for (const MotionMemento& memento : mMotionMementos) {
267 if (shouldCancelMotion(memento, options)) {
268 const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
269 : AMOTION_EVENT_ACTION_CANCEL;
270 outEvents.push_back(
271 new MotionEntry(SYNTHESIZED_EVENT_SEQUENCE_NUM, currentTime, memento.deviceId,
272 memento.source, memento.displayId, memento.policyFlags, action,
273 0 /*actionButton*/, memento.flags, AMETA_NONE,
274 0 /*buttonState*/, MotionClassification::NONE,
275 AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision,
276 memento.yPrecision, memento.xCursorPosition,
277 memento.yCursorPosition, memento.downTime, memento.pointerCount,
278 memento.pointerProperties, memento.pointerCoords, 0 /*xOffset*/,
279 0 /*yOffset*/));
280 }
281 }
282}
283
284void InputState::clear() {
285 mKeyMementos.clear();
286 mMotionMementos.clear();
287 mFallbackKeys.clear();
288}
289
290void InputState::copyPointerStateTo(InputState& other) const {
291 for (size_t i = 0; i < mMotionMementos.size(); i++) {
292 const MotionMemento& memento = mMotionMementos[i];
293 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
294 for (size_t j = 0; j < other.mMotionMementos.size();) {
295 const MotionMemento& otherMemento = other.mMotionMementos[j];
296 if (memento.deviceId == otherMemento.deviceId &&
297 memento.source == otherMemento.source &&
298 memento.displayId == otherMemento.displayId) {
299 other.mMotionMementos.erase(other.mMotionMementos.begin() + j);
300 } else {
301 j += 1;
302 }
303 }
304 other.mMotionMementos.push_back(memento);
305 }
306 }
307}
308
309int32_t InputState::getFallbackKey(int32_t originalKeyCode) {
310 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
311 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
312}
313
314void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) {
315 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
316 if (index >= 0) {
317 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
318 } else {
319 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
320 }
321}
322
323void InputState::removeFallbackKey(int32_t originalKeyCode) {
324 mFallbackKeys.removeItem(originalKeyCode);
325}
326
327bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) {
328 if (options.keyCode && memento.keyCode != options.keyCode.value()) {
329 return false;
330 }
331
332 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
333 return false;
334 }
335
336 if (options.displayId && memento.displayId != options.displayId.value()) {
337 return false;
338 }
339
340 switch (options.mode) {
341 case CancelationOptions::CANCEL_ALL_EVENTS:
342 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
343 return true;
344 case CancelationOptions::CANCEL_FALLBACK_EVENTS:
345 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
346 default:
347 return false;
348 }
349}
350
351bool InputState::shouldCancelMotion(const MotionMemento& memento,
352 const CancelationOptions& options) {
353 if (options.deviceId && memento.deviceId != options.deviceId.value()) {
354 return false;
355 }
356
357 if (options.displayId && memento.displayId != options.displayId.value()) {
358 return false;
359 }
360
361 switch (options.mode) {
362 case CancelationOptions::CANCEL_ALL_EVENTS:
363 return true;
364 case CancelationOptions::CANCEL_POINTER_EVENTS:
365 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
366 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
367 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
368 default:
369 return false;
370 }
371}
372
373} // namespace android::inputdispatcher