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 | } |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame^] | 424 | return std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 425 | eventTime, memento.deviceId, memento.source, |
| 426 | memento.displayId, memento.policyFlags, action, |
| 427 | /*actionButton=*/0, flags, AMETA_NONE, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 428 | /*buttonState=*/0, MotionClassification::NONE, |
| 429 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 430 | memento.yPrecision, memento.xCursorPosition, |
| 431 | memento.yCursorPosition, memento.downTime, |
| 432 | memento.pointerProperties, memento.pointerCoords); |
| 433 | } |
| 434 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 435 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents( |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 436 | nsecs_t currentTime, const CancelationOptions& options) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 437 | std::vector<std::unique_ptr<EventEntry>> events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 438 | for (KeyMemento& memento : mKeyMementos) { |
| 439 | if (shouldCancelKey(memento, options)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 440 | events.push_back( |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame^] | 441 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 442 | currentTime, memento.deviceId, memento.source, |
| 443 | memento.displayId, memento.policyFlags, |
| 444 | AKEY_EVENT_ACTION_UP, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 445 | memento.flags | AKEY_EVENT_FLAG_CANCELED, |
| 446 | memento.keyCode, memento.scanCode, memento.metaState, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 447 | /*repeatCount=*/0, memento.downTime)); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | for (const MotionMemento& memento : mMotionMementos) { |
| 452 | if (shouldCancelMotion(memento, options)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 453 | if (options.pointerIds == std::nullopt) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 454 | events.push_back(createCancelEntryForMemento(memento, currentTime)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 455 | } else { |
| 456 | std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents = |
| 457 | synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(), |
| 458 | currentTime); |
| 459 | events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()), |
| 460 | std::make_move_iterator(pointerCancelEvents.end())); |
| 461 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 464 | return events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 467 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents( |
| 468 | nsecs_t currentTime) { |
| 469 | std::vector<std::unique_ptr<EventEntry>> events; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 470 | for (MotionMemento& memento : mMotionMementos) { |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 471 | if (!isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 472 | continue; |
| 473 | } |
| 474 | |
| 475 | if (memento.firstNewPointerIdx < 0) { |
| 476 | continue; |
| 477 | } |
| 478 | |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 479 | std::vector<PointerProperties> pointerProperties; |
| 480 | std::vector<PointerCoords> pointerCoords; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 481 | |
| 482 | // We will deliver all pointers the target already knows about |
| 483 | 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] | 484 | pointerProperties.push_back(memento.pointerProperties[i]); |
| 485 | pointerCoords.push_back(memento.pointerCoords[i]); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // We will send explicit events for all pointers the target doesn't know about |
| 489 | for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx); |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 490 | i < memento.getPointerCount(); i++) { |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 491 | pointerProperties.push_back(memento.pointerProperties[i]); |
| 492 | pointerCoords.push_back(memento.pointerCoords[i]); |
| 493 | |
| 494 | const size_t pointerCount = pointerProperties.size(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 495 | |
| 496 | // Down only if the first pointer, pointer down otherwise |
| 497 | const int32_t action = (pointerCount <= 1) |
| 498 | ? AMOTION_EVENT_ACTION_DOWN |
| 499 | : AMOTION_EVENT_ACTION_POINTER_DOWN |
| 500 | | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 501 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 502 | events.push_back( |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame^] | 503 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 504 | currentTime, memento.deviceId, memento.source, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 505 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 506 | /*actionButton=*/0, memento.flags, AMETA_NONE, |
| 507 | /*buttonState=*/0, MotionClassification::NONE, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 508 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 509 | memento.yPrecision, memento.xCursorPosition, |
| 510 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 511 | pointerProperties, pointerCoords)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | memento.firstNewPointerIdx = INVALID_POINTER_INDEX; |
| 515 | } |
| 516 | |
| 517 | return events; |
| 518 | } |
| 519 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 520 | std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 521 | const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 522 | nsecs_t currentTime) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 523 | std::vector<std::unique_ptr<MotionEntry>> events; |
| 524 | std::vector<uint32_t> canceledPointerIndices; |
| 525 | std::vector<PointerProperties> pointerProperties(MAX_POINTERS); |
| 526 | std::vector<PointerCoords> pointerCoords(MAX_POINTERS); |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 527 | for (uint32_t pointerIdx = 0; pointerIdx < memento.getPointerCount(); pointerIdx++) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 528 | uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id); |
Siarhei Vishniakou | 73e6d37 | 2023-07-06 18:07:21 -0700 | [diff] [blame] | 529 | pointerProperties[pointerIdx] = memento.pointerProperties[pointerIdx]; |
| 530 | pointerCoords[pointerIdx] = memento.pointerCoords[pointerIdx]; |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 531 | if (pointerIds.test(pointerId)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 532 | canceledPointerIndices.push_back(pointerIdx); |
| 533 | } |
| 534 | } |
| 535 | |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 536 | if (canceledPointerIndices.size() == memento.getPointerCount()) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 537 | const int32_t action = |
| 538 | memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 539 | int32_t flags = memento.flags; |
| 540 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 541 | flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 542 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 543 | events.push_back( |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame^] | 544 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 545 | currentTime, memento.deviceId, memento.source, |
| 546 | memento.displayId, memento.policyFlags, action, |
| 547 | /*actionButton=*/0, flags, AMETA_NONE, |
| 548 | /*buttonState=*/0, MotionClassification::NONE, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 549 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 550 | memento.yPrecision, memento.xCursorPosition, |
| 551 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 552 | memento.pointerProperties, memento.pointerCoords)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 553 | } else { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 554 | // 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] | 555 | // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the |
| 556 | // previously canceled pointers from PointerProperties and PointerCoords, and update |
| 557 | // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we |
| 558 | // can just slide the remaining pointers to the beginning of the array when a pointer is |
| 559 | // canceled. |
| 560 | std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(), |
| 561 | std::greater<uint32_t>()); |
| 562 | |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 563 | uint32_t pointerCount = memento.getPointerCount(); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 564 | for (const uint32_t pointerIdx : canceledPointerIndices) { |
| 565 | const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL |
| 566 | : AMOTION_EVENT_ACTION_POINTER_UP | |
| 567 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 568 | events.push_back( |
Prabir Pradhan | a8cdbe1 | 2023-11-01 21:30:02 +0000 | [diff] [blame^] | 569 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), /*injectionState=*/nullptr, |
| 570 | currentTime, memento.deviceId, memento.source, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 571 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 572 | /*actionButton=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 573 | memento.flags | AMOTION_EVENT_FLAG_CANCELED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 574 | AMETA_NONE, /*buttonState=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 575 | MotionClassification::NONE, |
| 576 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 577 | memento.yPrecision, memento.xCursorPosition, |
| 578 | memento.yCursorPosition, memento.downTime, |
Siarhei Vishniakou | edd6120 | 2023-10-18 11:22:40 -0700 | [diff] [blame] | 579 | pointerProperties, pointerCoords)); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 580 | |
| 581 | // Cleanup pointer information |
| 582 | pointerProperties.erase(pointerProperties.begin() + pointerIdx); |
| 583 | pointerCoords.erase(pointerCoords.begin() + pointerIdx); |
| 584 | pointerCount--; |
| 585 | } |
| 586 | } |
| 587 | return events; |
| 588 | } |
| 589 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 590 | void InputState::clear() { |
| 591 | mKeyMementos.clear(); |
| 592 | mMotionMementos.clear(); |
| 593 | mFallbackKeys.clear(); |
| 594 | } |
| 595 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 596 | void InputState::mergePointerStateTo(InputState& other) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 597 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 598 | MotionMemento& memento = mMotionMementos[i]; |
| 599 | // Since we support split pointers we need to merge touch events |
| 600 | // from the same source + device + screen. |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 601 | if (isFromSource(memento.source, AINPUT_SOURCE_CLASS_POINTER)) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 602 | bool merged = false; |
| 603 | for (size_t j = 0; j < other.mMotionMementos.size(); j++) { |
| 604 | MotionMemento& otherMemento = other.mMotionMementos[j]; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 605 | if (memento.deviceId == otherMemento.deviceId && |
| 606 | memento.source == otherMemento.source && |
| 607 | memento.displayId == otherMemento.displayId) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 608 | memento.mergePointerStateTo(otherMemento); |
| 609 | merged = true; |
| 610 | break; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 613 | if (!merged) { |
| 614 | memento.firstNewPointerIdx = 0; |
| 615 | other.mMotionMementos.push_back(memento); |
| 616 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 621 | std::optional<int32_t> InputState::getFallbackKey(int32_t originalKeyCode) { |
| 622 | auto it = mFallbackKeys.find(originalKeyCode); |
| 623 | if (it == mFallbackKeys.end()) { |
| 624 | return {}; |
| 625 | } |
| 626 | return it->second; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 630 | mFallbackKeys.insert_or_assign(originalKeyCode, fallbackKeyCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void InputState::removeFallbackKey(int32_t originalKeyCode) { |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 634 | mFallbackKeys.erase(originalKeyCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) { |
| 638 | if (options.keyCode && memento.keyCode != options.keyCode.value()) { |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 651 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 652 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 653 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 654 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 655 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
| 656 | default: |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | bool InputState::shouldCancelMotion(const MotionMemento& memento, |
| 662 | const CancelationOptions& options) { |
| 663 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 672 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 673 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 674 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 675 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 676 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 677 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
| 678 | default: |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 683 | std::ostream& operator<<(std::ostream& out, const InputState& state) { |
| 684 | if (!state.mMotionMementos.empty()) { |
| 685 | out << "mMotionMementos: "; |
| 686 | for (const InputState::MotionMemento& memento : state.mMotionMementos) { |
| 687 | out << "{deviceId= " << memento.deviceId << ", hovering=" << memento.hovering << "}, "; |
| 688 | } |
| 689 | } |
| 690 | return out; |
| 691 | } |
| 692 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 693 | } // namespace android::inputdispatcher |