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