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