Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 17 | #include "DebugConfig.h" |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 18 | #include "input/InputDevice.h" |
| 19 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 20 | #include "InputState.h" |
| 21 | |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 22 | #include <cinttypes> |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 23 | #include "InputDispatcher.h" |
| 24 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 25 | namespace android::inputdispatcher { |
| 26 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 27 | namespace { |
| 28 | bool isHoverAction(int32_t action) { |
| 29 | switch (MotionEvent::getActionMasked(action)) { |
| 30 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 31 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 32 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | } // namespace |
| 39 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 40 | InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {} |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 41 | |
| 42 | InputState::~InputState() {} |
| 43 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 44 | bool InputState::isHovering(DeviceId deviceId, uint32_t source, int32_t displayId) const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | for (const MotionMemento& memento : mMotionMementos) { |
| 46 | if (memento.deviceId == deviceId && memento.source == source && |
| 47 | memento.displayId == displayId && memento.hovering) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 54 | bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 55 | switch (action) { |
| 56 | case AKEY_EVENT_ACTION_UP: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 57 | if (entry.flags & AKEY_EVENT_FLAG_FALLBACK) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 58 | std::erase_if(mFallbackKeys, |
| 59 | [&entry](const auto& item) { return item.second == entry.keyCode; }); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 60 | } |
| 61 | ssize_t index = findKeyMemento(entry); |
| 62 | if (index >= 0) { |
| 63 | mKeyMementos.erase(mKeyMementos.begin() + index); |
| 64 | return true; |
| 65 | } |
| 66 | /* FIXME: We can't just drop the key up event because that prevents creating |
| 67 | * popup windows that are automatically shown when a key is held and then |
| 68 | * dismissed when the key is released. The problem is that the popup will |
| 69 | * not have received the original key down, so the key up will be considered |
| 70 | * to be inconsistent with its observed state. We could perhaps handle this |
| 71 | * by synthesizing a key down but that will cause other problems. |
| 72 | * |
| 73 | * So for now, allow inconsistent key up events to be dispatched. |
| 74 | * |
| 75 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 76 | ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, " |
| 77 | "keyCode=%d, scanCode=%d", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 78 | entry.deviceId, entry.source, entry.keyCode, entry.scanCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 79 | #endif |
| 80 | return false; |
| 81 | */ |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | case AKEY_EVENT_ACTION_DOWN: { |
| 86 | ssize_t index = findKeyMemento(entry); |
| 87 | if (index >= 0) { |
| 88 | mKeyMementos.erase(mKeyMementos.begin() + index); |
| 89 | } |
| 90 | addKeyMemento(entry, flags); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | default: |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Return: |
| 101 | * true if the incoming event was correctly tracked, |
| 102 | * false if the incoming event should be dropped. |
| 103 | */ |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 104 | bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 105 | // Don't track non-pointer events |
| 106 | if (!isFromSource(entry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 107 | // This is a focus-dispatched event; we don't track its state. |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if (!mMotionMementos.empty()) { |
| 112 | const MotionMemento& lastMemento = mMotionMementos.back(); |
| 113 | if (isStylusEvent(lastMemento.source, lastMemento.pointerProperties) && |
| 114 | !isStylusEvent(entry.source, entry.pointerProperties)) { |
| 115 | // We already have a stylus stream, and the new event is not from stylus. |
| 116 | if (!lastMemento.hovering) { |
| 117 | // If stylus is currently down, reject the new event unconditionally. |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | if (!lastMemento.hovering && isHoverAction(action)) { |
| 122 | // Reject hovers if already down |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 127 | int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK; |
| 128 | switch (actionMasked) { |
| 129 | case AMOTION_EVENT_ACTION_UP: |
| 130 | case AMOTION_EVENT_ACTION_CANCEL: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 131 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 132 | if (index >= 0) { |
| 133 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 134 | return true; |
| 135 | } |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 136 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | case AMOTION_EVENT_ACTION_DOWN: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 141 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 142 | if (index >= 0) { |
| 143 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 144 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 145 | addMotionMemento(entry, flags, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 150 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 151 | case AMOTION_EVENT_ACTION_MOVE: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 152 | if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 153 | // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need |
| 154 | // to generate cancellation events for these since they're based in relative rather |
| 155 | // than absolute units. |
| 156 | return true; |
| 157 | } |
| 158 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 159 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 160 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 161 | if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 162 | // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all |
| 163 | // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral. |
| 164 | // Any other value and we need to track the motion so we can send cancellation |
| 165 | // events for anything generating fallback events (e.g. DPad keys for joystick |
| 166 | // movements). |
| 167 | if (index >= 0) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 168 | if (entry.pointerCoords[0].isEmpty()) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 169 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 170 | } else { |
| 171 | MotionMemento& memento = mMotionMementos[index]; |
| 172 | memento.setPointers(entry); |
| 173 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 174 | } else if (!entry.pointerCoords[0].isEmpty()) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 175 | addMotionMemento(entry, flags, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP. |
| 179 | return true; |
| 180 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 181 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 182 | if (index >= 0) { |
| 183 | MotionMemento& memento = mMotionMementos[index]; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 184 | if (memento.firstNewPointerIdx < 0) { |
| 185 | memento.setPointers(entry); |
| 186 | return true; |
| 187 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 188 | } |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 189 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
| 193 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 194 | ssize_t index = findMotionMemento(entry, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 195 | if (index >= 0) { |
| 196 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 197 | return true; |
| 198 | } |
Siarhei Vishniakou | c94dafe | 2023-05-26 10:24:19 -0700 | [diff] [blame] | 199 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
| 203 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 204 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 205 | ssize_t index = findMotionMemento(entry, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 206 | if (index >= 0) { |
| 207 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 208 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 209 | addMotionMemento(entry, flags, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
| 213 | default: |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 218 | ssize_t InputState::findKeyMemento(const KeyEntry& entry) const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 219 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
| 220 | const KeyMemento& memento = mKeyMementos[i]; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 221 | if (memento.deviceId == entry.deviceId && memento.source == entry.source && |
| 222 | memento.displayId == entry.displayId && memento.keyCode == entry.keyCode && |
| 223 | memento.scanCode == entry.scanCode) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 224 | return i; |
| 225 | } |
| 226 | } |
| 227 | return -1; |
| 228 | } |
| 229 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 230 | ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 231 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 232 | const MotionMemento& memento = mMotionMementos[i]; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 233 | if (memento.deviceId == entry.deviceId && memento.source == entry.source && |
| 234 | memento.displayId == entry.displayId && memento.hovering == hovering) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 235 | return i; |
| 236 | } |
| 237 | } |
| 238 | return -1; |
| 239 | } |
| 240 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 241 | void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 242 | KeyMemento memento; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 243 | memento.deviceId = entry.deviceId; |
| 244 | memento.source = entry.source; |
| 245 | memento.displayId = entry.displayId; |
| 246 | memento.keyCode = entry.keyCode; |
| 247 | memento.scanCode = entry.scanCode; |
| 248 | memento.metaState = entry.metaState; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 249 | memento.flags = flags; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 250 | memento.downTime = entry.downTime; |
| 251 | memento.policyFlags = entry.policyFlags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 252 | mKeyMementos.push_back(memento); |
| 253 | } |
| 254 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 255 | void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 256 | MotionMemento memento; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 257 | memento.deviceId = entry.deviceId; |
| 258 | memento.source = entry.source; |
| 259 | memento.displayId = entry.displayId; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 260 | memento.flags = flags; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 261 | memento.xPrecision = entry.xPrecision; |
| 262 | memento.yPrecision = entry.yPrecision; |
| 263 | memento.xCursorPosition = entry.xCursorPosition; |
| 264 | memento.yCursorPosition = entry.yCursorPosition; |
| 265 | memento.downTime = entry.downTime; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 266 | memento.setPointers(entry); |
| 267 | memento.hovering = hovering; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 268 | memento.policyFlags = entry.policyFlags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 269 | mMotionMementos.push_back(memento); |
| 270 | } |
| 271 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 272 | void InputState::MotionMemento::setPointers(const MotionEntry& entry) { |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 273 | pointerProperties.clear(); |
| 274 | pointerCoords.clear(); |
| 275 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 276 | for (uint32_t i = 0; i < entry.getPointerCount(); i++) { |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 277 | if (MotionEvent::getActionMasked(entry.action) == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 278 | // In POINTER_UP events, the pointer is leaving. Since the action is not stored, |
| 279 | // this departing pointer should not be recorded. |
| 280 | const uint8_t actionIndex = MotionEvent::getActionIndex(entry.action); |
| 281 | if (i == actionIndex) { |
| 282 | continue; |
| 283 | } |
| 284 | } |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 285 | pointerProperties.push_back(entry.pointerProperties[i]); |
| 286 | pointerCoords.push_back(entry.pointerCoords[i]); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 290 | void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const { |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 291 | for (uint32_t i = 0; i < getPointerCount(); i++) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 292 | if (other.firstNewPointerIdx < 0) { |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 293 | other.firstNewPointerIdx = other.getPointerCount(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 294 | } |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 295 | other.pointerProperties.push_back(pointerProperties[i]); |
| 296 | other.pointerCoords.push_back(pointerCoords[i]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 300 | size_t InputState::MotionMemento::getPointerCount() const { |
| 301 | return pointerProperties.size(); |
| 302 | } |
| 303 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 304 | bool InputState::shouldCancelPreviousStream(const MotionEntry& motionEntry, |
| 305 | int32_t resolvedAction) const { |
| 306 | if (!isFromSource(motionEntry.source, AINPUT_SOURCE_CLASS_POINTER)) { |
| 307 | // This is a focus-dispatched event that should not affect the previous stream. |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | // New MotionEntry pointer event is coming in. |
| 312 | |
| 313 | // If this is a new gesture, and it's from a different device, then, in general, we will cancel |
| 314 | // the current gesture. |
| 315 | // However, because stylus should be preferred over touch, we need to treat some cases in a |
| 316 | // special way. |
| 317 | if (mMotionMementos.empty()) { |
| 318 | // There is no ongoing pointer gesture, so there is nothing to cancel |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | const MotionMemento& lastMemento = mMotionMementos.back(); |
| 323 | const int32_t actionMasked = MotionEvent::getActionMasked(resolvedAction); |
| 324 | |
| 325 | // For compatibility, only one input device can be active at a time in the same window. |
| 326 | if (lastMemento.deviceId == motionEntry.deviceId) { |
| 327 | // In general, the same device should produce self-consistent streams so nothing needs to |
| 328 | // be canceled. But there is one exception: |
| 329 | // Sometimes ACTION_DOWN is received without a corresponding HOVER_EXIT. To account for |
| 330 | // that, cancel the previous hovering stream |
| 331 | if (actionMasked == AMOTION_EVENT_ACTION_DOWN && lastMemento.hovering) { |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | // Use the previous stream cancellation logic to generate all HOVER_EXIT events. |
| 336 | // If this hover event was generated as a result of the pointer leaving the window, |
| 337 | // the HOVER_EXIT event should have the same coordinates as the previous |
| 338 | // HOVER_MOVE event in this stream. Ensure that all HOVER_EXITs have the same |
| 339 | // coordinates as the previous event by cancelling the stream here. With this approach, the |
| 340 | // HOVER_EXIT event is generated from the previous event. |
| 341 | if (actionMasked == AMOTION_EVENT_ACTION_HOVER_EXIT && lastMemento.hovering) { |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | // If the stream changes its source, just cancel the current gesture to be safe. It's |
| 346 | // possible that the app isn't handling source changes properly |
| 347 | if (motionEntry.source != lastMemento.source) { |
| 348 | LOG(INFO) << "Canceling stream: last source was " |
| 349 | << inputEventSourceToString(lastMemento.source) << " and new event is " |
| 350 | << motionEntry; |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | // If the injection is happening into two different displays, the same injected device id |
| 355 | // could be going into both. And at this time, if mirroring is active, the same connection |
| 356 | // would receive different events from each display. Since the TouchStates are per-display, |
| 357 | // it's unlikely that those two streams would be consistent with each other. Therefore, |
| 358 | // cancel the previous gesture if the display id changes. |
| 359 | if (motionEntry.displayId != lastMemento.displayId) { |
| 360 | LOG(INFO) << "Canceling stream: last displayId was " |
| 361 | << inputEventSourceToString(lastMemento.displayId) << " and new event is " |
| 362 | << motionEntry; |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | // We want stylus down to block touch and other source types, but stylus hover should not |
| 370 | // have such an effect. |
| 371 | if (isHoverAction(motionEntry.action) && !lastMemento.hovering) { |
| 372 | // New event is a hover. Keep the current non-hovering gesture instead |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | if (isStylusEvent(lastMemento.source, lastMemento.pointerProperties) && !lastMemento.hovering) { |
| 377 | // We have non-hovering stylus already active. |
| 378 | if (isStylusEvent(motionEntry.source, motionEntry.pointerProperties) && |
| 379 | actionMasked == AMOTION_EVENT_ACTION_DOWN) { |
| 380 | // If this new event is a stylus from a different device going down, then cancel the old |
| 381 | // stylus and allow the new stylus to take over |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | // Keep the current stylus gesture. |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // Cancel the current gesture if this is a start of a new gesture from a new device. |
| 390 | if (actionMasked == AMOTION_EVENT_ACTION_DOWN || |
| 391 | actionMasked == AMOTION_EVENT_ACTION_HOVER_ENTER) { |
| 392 | return true; |
| 393 | } |
| 394 | // By default, don't cancel any events. |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | std::unique_ptr<EventEntry> InputState::cancelConflictingInputStream(const MotionEntry& motionEntry, |
| 399 | int32_t resolvedAction) { |
| 400 | if (!shouldCancelPreviousStream(motionEntry, resolvedAction)) { |
| 401 | return {}; |
| 402 | } |
| 403 | |
| 404 | const MotionMemento& memento = mMotionMementos.back(); |
| 405 | |
| 406 | // Cancel the last device stream |
| 407 | std::unique_ptr<MotionEntry> cancelEntry = |
| 408 | createCancelEntryForMemento(memento, motionEntry.eventTime); |
| 409 | |
| 410 | if (!trackMotion(*cancelEntry, cancelEntry->action, cancelEntry->flags)) { |
| 411 | LOG(FATAL) << "Generated inconsistent cancel event!"; |
| 412 | } |
| 413 | return cancelEntry; |
| 414 | } |
| 415 | |
| 416 | std::unique_ptr<MotionEntry> InputState::createCancelEntryForMemento(const MotionMemento& memento, |
| 417 | nsecs_t eventTime) const { |
| 418 | const int32_t action = |
| 419 | memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL; |
| 420 | int32_t flags = memento.flags; |
| 421 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 422 | flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 423 | } |
| 424 | return std::make_unique<MotionEntry>(mIdGenerator.nextId(), eventTime, memento.deviceId, |
| 425 | memento.source, memento.displayId, memento.policyFlags, |
| 426 | action, /*actionButton=*/0, flags, AMETA_NONE, |
| 427 | /*buttonState=*/0, MotionClassification::NONE, |
| 428 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 429 | memento.yPrecision, memento.xCursorPosition, |
| 430 | memento.yCursorPosition, memento.downTime, |
| 431 | memento.pointerProperties, memento.pointerCoords); |
| 432 | } |
| 433 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 434 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents( |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 435 | nsecs_t currentTime, const CancelationOptions& options) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 436 | std::vector<std::unique_ptr<EventEntry>> events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 437 | for (KeyMemento& memento : mKeyMementos) { |
| 438 | if (shouldCancelKey(memento, options)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 439 | events.push_back( |
| 440 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId, |
| 441 | memento.source, memento.displayId, |
| 442 | memento.policyFlags, AKEY_EVENT_ACTION_UP, |
| 443 | memento.flags | AKEY_EVENT_FLAG_CANCELED, |
| 444 | memento.keyCode, memento.scanCode, memento.metaState, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 445 | /*repeatCount=*/0, memento.downTime)); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | for (const MotionMemento& memento : mMotionMementos) { |
| 450 | if (shouldCancelMotion(memento, options)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 451 | if (options.pointerIds == std::nullopt) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 452 | events.push_back(createCancelEntryForMemento(memento, currentTime)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 453 | } else { |
| 454 | std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents = |
| 455 | synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(), |
| 456 | currentTime); |
| 457 | events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()), |
| 458 | std::make_move_iterator(pointerCancelEvents.end())); |
| 459 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 462 | return events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 465 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents( |
| 466 | nsecs_t currentTime) { |
| 467 | std::vector<std::unique_ptr<EventEntry>> events; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 468 | for (MotionMemento& memento : mMotionMementos) { |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 469 | if (!isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 470 | continue; |
| 471 | } |
| 472 | |
| 473 | if (memento.firstNewPointerIdx < 0) { |
| 474 | continue; |
| 475 | } |
| 476 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 477 | std::vector<PointerProperties> pointerProperties; |
| 478 | std::vector<PointerCoords> pointerCoords; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 479 | |
| 480 | // We will deliver all pointers the target already knows about |
| 481 | for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 482 | pointerProperties.push_back(memento.pointerProperties[i]); |
| 483 | pointerCoords.push_back(memento.pointerCoords[i]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // We will send explicit events for all pointers the target doesn't know about |
| 487 | for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx); |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 488 | i < memento.getPointerCount(); i++) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 489 | pointerProperties.push_back(memento.pointerProperties[i]); |
| 490 | pointerCoords.push_back(memento.pointerCoords[i]); |
| 491 | |
| 492 | const size_t pointerCount = pointerProperties.size(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 493 | |
| 494 | // Down only if the first pointer, pointer down otherwise |
| 495 | const int32_t action = (pointerCount <= 1) |
| 496 | ? AMOTION_EVENT_ACTION_DOWN |
| 497 | : AMOTION_EVENT_ACTION_POINTER_DOWN |
| 498 | | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 499 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 500 | events.push_back( |
| 501 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 502 | memento.deviceId, memento.source, |
| 503 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 504 | /*actionButton=*/0, memento.flags, AMETA_NONE, |
| 505 | /*buttonState=*/0, MotionClassification::NONE, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 506 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 507 | memento.yPrecision, memento.xCursorPosition, |
| 508 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 509 | pointerProperties, pointerCoords)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | memento.firstNewPointerIdx = INVALID_POINTER_INDEX; |
| 513 | } |
| 514 | |
| 515 | return events; |
| 516 | } |
| 517 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 518 | std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 519 | const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 520 | nsecs_t currentTime) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 521 | std::vector<std::unique_ptr<MotionEntry>> events; |
| 522 | std::vector<uint32_t> canceledPointerIndices; |
| 523 | std::vector<PointerProperties> pointerProperties(MAX_POINTERS); |
| 524 | std::vector<PointerCoords> pointerCoords(MAX_POINTERS); |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 525 | for (uint32_t pointerIdx = 0; pointerIdx < memento.getPointerCount(); pointerIdx++) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 526 | uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id); |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 527 | pointerProperties[pointerIdx] = memento.pointerProperties[pointerIdx]; |
| 528 | pointerCoords[pointerIdx] = memento.pointerCoords[pointerIdx]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 529 | if (pointerIds.test(pointerId)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 530 | canceledPointerIndices.push_back(pointerIdx); |
| 531 | } |
| 532 | } |
| 533 | |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 534 | if (canceledPointerIndices.size() == memento.getPointerCount()) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 535 | const int32_t action = |
| 536 | memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 537 | int32_t flags = memento.flags; |
| 538 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 539 | flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 540 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 541 | events.push_back( |
| 542 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId, |
| 543 | memento.source, memento.displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 544 | memento.policyFlags, action, /*actionButton=*/0, |
| 545 | flags, AMETA_NONE, /*buttonState=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 546 | MotionClassification::NONE, |
| 547 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 548 | memento.yPrecision, memento.xCursorPosition, |
| 549 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 550 | memento.pointerProperties, memento.pointerCoords)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 551 | } else { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 552 | // If we aren't canceling all pointers, we need to generate ACTION_POINTER_UP with |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 553 | // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the |
| 554 | // previously canceled pointers from PointerProperties and PointerCoords, and update |
| 555 | // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we |
| 556 | // can just slide the remaining pointers to the beginning of the array when a pointer is |
| 557 | // canceled. |
| 558 | std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(), |
| 559 | std::greater<uint32_t>()); |
| 560 | |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 561 | uint32_t pointerCount = memento.getPointerCount(); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 562 | for (const uint32_t pointerIdx : canceledPointerIndices) { |
| 563 | const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL |
| 564 | : AMOTION_EVENT_ACTION_POINTER_UP | |
| 565 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 566 | events.push_back( |
| 567 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 568 | memento.deviceId, memento.source, |
| 569 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 570 | /*actionButton=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 571 | memento.flags | AMOTION_EVENT_FLAG_CANCELED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 572 | AMETA_NONE, /*buttonState=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 573 | MotionClassification::NONE, |
| 574 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 575 | memento.yPrecision, memento.xCursorPosition, |
| 576 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 577 | pointerProperties, pointerCoords)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 578 | |
| 579 | // Cleanup pointer information |
| 580 | pointerProperties.erase(pointerProperties.begin() + pointerIdx); |
| 581 | pointerCoords.erase(pointerCoords.begin() + pointerIdx); |
| 582 | pointerCount--; |
| 583 | } |
| 584 | } |
| 585 | return events; |
| 586 | } |
| 587 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 588 | void InputState::clear() { |
| 589 | mKeyMementos.clear(); |
| 590 | mMotionMementos.clear(); |
| 591 | mFallbackKeys.clear(); |
| 592 | } |
| 593 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 594 | void InputState::mergePointerStateTo(InputState& other) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 595 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 596 | MotionMemento& memento = mMotionMementos[i]; |
| 597 | // Since we support split pointers we need to merge touch events |
| 598 | // from the same source + device + screen. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 599 | if (isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 600 | bool merged = false; |
| 601 | for (size_t j = 0; j < other.mMotionMementos.size(); j++) { |
| 602 | MotionMemento& otherMemento = other.mMotionMementos[j]; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 603 | if (memento.deviceId == otherMemento.deviceId && |
| 604 | memento.source == otherMemento.source && |
| 605 | memento.displayId == otherMemento.displayId) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 606 | memento.mergePointerStateTo(otherMemento); |
| 607 | merged = true; |
| 608 | break; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 611 | if (!merged) { |
| 612 | memento.firstNewPointerIdx = 0; |
| 613 | other.mMotionMementos.push_back(memento); |
| 614 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 619 | std::optional<int32_t> InputState::getFallbackKey(int32_t originalKeyCode) { |
| 620 | auto it = mFallbackKeys.find(originalKeyCode); |
| 621 | if (it == mFallbackKeys.end()) { |
| 622 | return {}; |
| 623 | } |
| 624 | return it->second; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 628 | mFallbackKeys.insert_or_assign(originalKeyCode, fallbackKeyCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void InputState::removeFallbackKey(int32_t originalKeyCode) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 632 | mFallbackKeys.erase(originalKeyCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) { |
| 636 | if (options.keyCode && memento.keyCode != options.keyCode.value()) { |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 649 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 650 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 651 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 652 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 653 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
| 654 | default: |
| 655 | return false; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | bool InputState::shouldCancelMotion(const MotionMemento& memento, |
| 660 | const CancelationOptions& options) { |
| 661 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 670 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 671 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 672 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 673 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 674 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 675 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
| 676 | default: |
| 677 | return false; |
| 678 | } |
| 679 | } |
| 680 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 681 | std::ostream& operator<<(std::ostream& out, const InputState& state) { |
| 682 | if (!state.mMotionMementos.empty()) { |
| 683 | out << "mMotionMementos: "; |
| 684 | for (const InputState::MotionMemento& memento : state.mMotionMementos) { |
| 685 | out << "{deviceId= " << memento.deviceId << ", hovering=" << memento.hovering << "}, "; |
| 686 | } |
| 687 | } |
| 688 | return out; |
| 689 | } |
| 690 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 691 | } // namespace android::inputdispatcher |