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 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 31 | bool InputState::isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const { |
| 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 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 41 | bool InputState::trackKey(const KeyEntry& entry, int32_t action, int32_t flags) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 42 | switch (action) { |
| 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) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | for (size_t i = 0; i < mFallbackKeys.size();) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 46 | if (mFallbackKeys.valueAt(i) == entry.keyCode) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 47 | mFallbackKeys.removeItemsAt(i); |
| 48 | } else { |
| 49 | i += 1; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | ssize_t index = findKeyMemento(entry); |
| 54 | if (index >= 0) { |
| 55 | mKeyMementos.erase(mKeyMementos.begin() + index); |
| 56 | return true; |
| 57 | } |
| 58 | /* FIXME: We can't just drop the key up event because that prevents creating |
| 59 | * popup windows that are automatically shown when a key is held and then |
| 60 | * dismissed when the key is released. The problem is that the popup will |
| 61 | * not have received the original key down, so the key up will be considered |
| 62 | * to be inconsistent with its observed state. We could perhaps handle this |
| 63 | * by synthesizing a key down but that will cause other problems. |
| 64 | * |
| 65 | * So for now, allow inconsistent key up events to be dispatched. |
| 66 | * |
| 67 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 68 | ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, " |
| 69 | "keyCode=%d, scanCode=%d", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 70 | entry.deviceId, entry.source, entry.keyCode, entry.scanCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 71 | #endif |
| 72 | return false; |
| 73 | */ |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | case AKEY_EVENT_ACTION_DOWN: { |
| 78 | ssize_t index = findKeyMemento(entry); |
| 79 | if (index >= 0) { |
| 80 | mKeyMementos.erase(mKeyMementos.begin() + index); |
| 81 | } |
| 82 | addKeyMemento(entry, flags); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | default: |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 91 | bool InputState::trackMotion(const MotionEntry& entry, int32_t action, int32_t flags) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 92 | int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK; |
| 93 | switch (actionMasked) { |
| 94 | case AMOTION_EVENT_ACTION_UP: |
| 95 | case AMOTION_EVENT_ACTION_CANCEL: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 96 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 97 | if (index >= 0) { |
| 98 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 99 | return true; |
| 100 | } |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 101 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 102 | ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, " |
| 103 | "displayId=%" PRId32 ", actionMasked=%d", |
| 104 | entry.deviceId, entry.source, entry.displayId, actionMasked); |
| 105 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | case AMOTION_EVENT_ACTION_DOWN: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 110 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 111 | if (index >= 0) { |
| 112 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 113 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 114 | addMotionMemento(entry, flags, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 119 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 120 | case AMOTION_EVENT_ACTION_MOVE: { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 121 | if (entry.source & AINPUT_SOURCE_CLASS_NAVIGATION) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 122 | // Trackballs can send MOVE events with a corresponding DOWN or UP. There's no need |
| 123 | // to generate cancellation events for these since they're based in relative rather |
| 124 | // than absolute units. |
| 125 | return true; |
| 126 | } |
| 127 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 128 | ssize_t index = findMotionMemento(entry, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 129 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 130 | if (entry.source & AINPUT_SOURCE_CLASS_JOYSTICK) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 131 | // Joysticks can send MOVE events without a corresponding DOWN or UP. Since all |
| 132 | // joystick axes are normalized to [-1, 1] we can trust that 0 means it's neutral. |
| 133 | // Any other value and we need to track the motion so we can send cancellation |
| 134 | // events for anything generating fallback events (e.g. DPad keys for joystick |
| 135 | // movements). |
| 136 | if (index >= 0) { |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 137 | if (entry.pointerCoords[0].isEmpty()) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 138 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 139 | } else { |
| 140 | MotionMemento& memento = mMotionMementos[index]; |
| 141 | memento.setPointers(entry); |
| 142 | } |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 143 | } else if (!entry.pointerCoords[0].isEmpty()) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 144 | addMotionMemento(entry, flags, /*hovering=*/false); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP. |
| 148 | return true; |
| 149 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 150 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 151 | if (index >= 0) { |
| 152 | MotionMemento& memento = mMotionMementos[index]; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 153 | if (memento.firstNewPointerIdx < 0) { |
| 154 | memento.setPointers(entry); |
| 155 | return true; |
| 156 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 157 | } |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 158 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 159 | ALOGD("Dropping inconsistent motion pointer up/down or move event: " |
| 160 | "deviceId=%d, source=%08x, displayId=%" PRId32 ", actionMasked=%d", |
| 161 | entry.deviceId, entry.source, entry.displayId, actionMasked); |
| 162 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 167 | ssize_t index = findMotionMemento(entry, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 168 | if (index >= 0) { |
| 169 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 170 | return true; |
| 171 | } |
Arthur Hung | 1a1007b | 2022-05-11 07:15:01 +0000 | [diff] [blame] | 172 | if (DEBUG_OUTBOUND_EVENT_DETAILS) { |
| 173 | ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, " |
| 174 | "displayId=%" PRId32, |
| 175 | entry.deviceId, entry.source, entry.displayId); |
| 176 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 181 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 182 | ssize_t index = findMotionMemento(entry, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 183 | if (index >= 0) { |
| 184 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 185 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 186 | addMotionMemento(entry, flags, /*hovering=*/true); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
| 190 | default: |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 195 | ssize_t InputState::findKeyMemento(const KeyEntry& entry) const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 196 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
| 197 | const KeyMemento& memento = mKeyMementos[i]; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 198 | if (memento.deviceId == entry.deviceId && memento.source == entry.source && |
| 199 | memento.displayId == entry.displayId && memento.keyCode == entry.keyCode && |
| 200 | memento.scanCode == entry.scanCode) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 201 | return i; |
| 202 | } |
| 203 | } |
| 204 | return -1; |
| 205 | } |
| 206 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 207 | ssize_t InputState::findMotionMemento(const MotionEntry& entry, bool hovering) const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 208 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 209 | const MotionMemento& memento = mMotionMementos[i]; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 210 | if (memento.deviceId == entry.deviceId && memento.source == entry.source && |
| 211 | memento.displayId == entry.displayId && memento.hovering == hovering) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 212 | return i; |
| 213 | } |
| 214 | } |
| 215 | return -1; |
| 216 | } |
| 217 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 218 | void InputState::addKeyMemento(const KeyEntry& entry, int32_t flags) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 219 | KeyMemento memento; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 220 | memento.deviceId = entry.deviceId; |
| 221 | memento.source = entry.source; |
| 222 | memento.displayId = entry.displayId; |
| 223 | memento.keyCode = entry.keyCode; |
| 224 | memento.scanCode = entry.scanCode; |
| 225 | memento.metaState = entry.metaState; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 226 | memento.flags = flags; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 227 | memento.downTime = entry.downTime; |
| 228 | memento.policyFlags = entry.policyFlags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 229 | mKeyMementos.push_back(memento); |
| 230 | } |
| 231 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 232 | void InputState::addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 233 | MotionMemento memento; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 234 | memento.deviceId = entry.deviceId; |
| 235 | memento.source = entry.source; |
| 236 | memento.displayId = entry.displayId; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 237 | memento.flags = flags; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 238 | memento.xPrecision = entry.xPrecision; |
| 239 | memento.yPrecision = entry.yPrecision; |
| 240 | memento.xCursorPosition = entry.xCursorPosition; |
| 241 | memento.yCursorPosition = entry.yCursorPosition; |
| 242 | memento.downTime = entry.downTime; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 243 | memento.setPointers(entry); |
| 244 | memento.hovering = hovering; |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 245 | memento.policyFlags = entry.policyFlags; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 246 | mMotionMementos.push_back(memento); |
| 247 | } |
| 248 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 249 | void InputState::MotionMemento::setPointers(const MotionEntry& entry) { |
| 250 | pointerCount = entry.pointerCount; |
| 251 | for (uint32_t i = 0; i < entry.pointerCount; i++) { |
| 252 | pointerProperties[i].copyFrom(entry.pointerProperties[i]); |
| 253 | pointerCoords[i].copyFrom(entry.pointerCoords[i]); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 257 | void InputState::MotionMemento::mergePointerStateTo(MotionMemento& other) const { |
| 258 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 259 | if (other.firstNewPointerIdx < 0) { |
| 260 | other.firstNewPointerIdx = other.pointerCount; |
| 261 | } |
| 262 | other.pointerProperties[other.pointerCount].copyFrom(pointerProperties[i]); |
| 263 | other.pointerCoords[other.pointerCount].copyFrom(pointerCoords[i]); |
| 264 | other.pointerCount++; |
| 265 | } |
| 266 | } |
| 267 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 268 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizeCancelationEvents( |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 269 | nsecs_t currentTime, const CancelationOptions& options) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 270 | std::vector<std::unique_ptr<EventEntry>> events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 271 | for (KeyMemento& memento : mKeyMementos) { |
| 272 | if (shouldCancelKey(memento, options)) { |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 273 | events.push_back( |
| 274 | std::make_unique<KeyEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId, |
| 275 | memento.source, memento.displayId, |
| 276 | memento.policyFlags, AKEY_EVENT_ACTION_UP, |
| 277 | memento.flags | AKEY_EVENT_FLAG_CANCELED, |
| 278 | memento.keyCode, memento.scanCode, memento.metaState, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 279 | /*repeatCount=*/0, memento.downTime)); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | for (const MotionMemento& memento : mMotionMementos) { |
| 284 | if (shouldCancelMotion(memento, options)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 285 | if (options.pointerIds == std::nullopt) { |
| 286 | const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT |
| 287 | : AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 288 | int32_t flags = memento.flags; |
| 289 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 290 | flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 291 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 292 | events.push_back( |
| 293 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 294 | memento.deviceId, memento.source, |
| 295 | memento.displayId, memento.policyFlags, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 296 | action, /*actionButton=*/0, flags, AMETA_NONE, |
| 297 | /*buttonState=*/0, MotionClassification::NONE, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 298 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 299 | memento.xPrecision, memento.yPrecision, |
| 300 | memento.xCursorPosition, |
| 301 | memento.yCursorPosition, memento.downTime, |
| 302 | memento.pointerCount, |
| 303 | memento.pointerProperties, |
| 304 | memento.pointerCoords)); |
| 305 | } else { |
| 306 | std::vector<std::unique_ptr<MotionEntry>> pointerCancelEvents = |
| 307 | synthesizeCancelationEventsForPointers(memento, options.pointerIds.value(), |
| 308 | currentTime); |
| 309 | events.insert(events.end(), std::make_move_iterator(pointerCancelEvents.begin()), |
| 310 | std::make_move_iterator(pointerCancelEvents.end())); |
| 311 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 314 | return events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 317 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents( |
| 318 | nsecs_t currentTime) { |
| 319 | std::vector<std::unique_ptr<EventEntry>> events; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 320 | for (MotionMemento& memento : mMotionMementos) { |
| 321 | if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | if (memento.firstNewPointerIdx < 0) { |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | uint32_t pointerCount = 0; |
| 330 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 331 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 332 | |
| 333 | // We will deliver all pointers the target already knows about |
| 334 | for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) { |
| 335 | pointerProperties[i].copyFrom(memento.pointerProperties[i]); |
| 336 | pointerCoords[i].copyFrom(memento.pointerCoords[i]); |
| 337 | pointerCount++; |
| 338 | } |
| 339 | |
| 340 | // We will send explicit events for all pointers the target doesn't know about |
| 341 | for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx); |
| 342 | i < memento.pointerCount; i++) { |
| 343 | |
| 344 | pointerProperties[i].copyFrom(memento.pointerProperties[i]); |
| 345 | pointerCoords[i].copyFrom(memento.pointerCoords[i]); |
| 346 | pointerCount++; |
| 347 | |
| 348 | // Down only if the first pointer, pointer down otherwise |
| 349 | const int32_t action = (pointerCount <= 1) |
| 350 | ? AMOTION_EVENT_ACTION_DOWN |
| 351 | : AMOTION_EVENT_ACTION_POINTER_DOWN |
| 352 | | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 353 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 354 | events.push_back( |
| 355 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 356 | memento.deviceId, memento.source, |
| 357 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 358 | /*actionButton=*/0, memento.flags, AMETA_NONE, |
| 359 | /*buttonState=*/0, MotionClassification::NONE, |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 360 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 361 | memento.yPrecision, memento.xCursorPosition, |
| 362 | memento.yCursorPosition, memento.downTime, |
Prabir Pradhan | 5beda76 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 363 | pointerCount, pointerProperties, pointerCoords)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | memento.firstNewPointerIdx = INVALID_POINTER_INDEX; |
| 367 | } |
| 368 | |
| 369 | return events; |
| 370 | } |
| 371 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 372 | std::vector<std::unique_ptr<MotionEntry>> InputState::synthesizeCancelationEventsForPointers( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 373 | const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 374 | nsecs_t currentTime) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 375 | std::vector<std::unique_ptr<MotionEntry>> events; |
| 376 | std::vector<uint32_t> canceledPointerIndices; |
| 377 | std::vector<PointerProperties> pointerProperties(MAX_POINTERS); |
| 378 | std::vector<PointerCoords> pointerCoords(MAX_POINTERS); |
| 379 | for (uint32_t pointerIdx = 0; pointerIdx < memento.pointerCount; pointerIdx++) { |
| 380 | uint32_t pointerId = uint32_t(memento.pointerProperties[pointerIdx].id); |
| 381 | pointerProperties[pointerIdx].copyFrom(memento.pointerProperties[pointerIdx]); |
| 382 | pointerCoords[pointerIdx].copyFrom(memento.pointerCoords[pointerIdx]); |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 383 | if (pointerIds.test(pointerId)) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 384 | canceledPointerIndices.push_back(pointerIdx); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (canceledPointerIndices.size() == memento.pointerCount) { |
| 389 | const int32_t action = |
| 390 | memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT : AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 391 | int32_t flags = memento.flags; |
| 392 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 393 | flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 394 | } |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 395 | events.push_back( |
| 396 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, memento.deviceId, |
| 397 | memento.source, memento.displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 398 | memento.policyFlags, action, /*actionButton=*/0, |
| 399 | flags, AMETA_NONE, /*buttonState=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 400 | MotionClassification::NONE, |
| 401 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 402 | memento.yPrecision, memento.xCursorPosition, |
| 403 | memento.yCursorPosition, memento.downTime, |
| 404 | memento.pointerCount, memento.pointerProperties, |
| 405 | memento.pointerCoords)); |
| 406 | } else { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 407 | // 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] | 408 | // FLAG_CANCELED for each of the canceled pointers. For each event, we must remove the |
| 409 | // previously canceled pointers from PointerProperties and PointerCoords, and update |
| 410 | // pointerCount appropriately. For convenience, sort the canceled pointer indices so that we |
| 411 | // can just slide the remaining pointers to the beginning of the array when a pointer is |
| 412 | // canceled. |
| 413 | std::sort(canceledPointerIndices.begin(), canceledPointerIndices.end(), |
| 414 | std::greater<uint32_t>()); |
| 415 | |
| 416 | uint32_t pointerCount = memento.pointerCount; |
| 417 | for (const uint32_t pointerIdx : canceledPointerIndices) { |
| 418 | const int32_t action = pointerCount == 1 ? AMOTION_EVENT_ACTION_CANCEL |
| 419 | : AMOTION_EVENT_ACTION_POINTER_UP | |
| 420 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 421 | events.push_back( |
| 422 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 423 | memento.deviceId, memento.source, |
| 424 | memento.displayId, memento.policyFlags, action, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 425 | /*actionButton=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 426 | memento.flags | AMOTION_EVENT_FLAG_CANCELED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 427 | AMETA_NONE, /*buttonState=*/0, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 428 | MotionClassification::NONE, |
| 429 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 430 | memento.yPrecision, memento.xCursorPosition, |
| 431 | memento.yCursorPosition, memento.downTime, |
| 432 | pointerCount, pointerProperties.data(), |
| 433 | pointerCoords.data())); |
| 434 | |
| 435 | // Cleanup pointer information |
| 436 | pointerProperties.erase(pointerProperties.begin() + pointerIdx); |
| 437 | pointerCoords.erase(pointerCoords.begin() + pointerIdx); |
| 438 | pointerCount--; |
| 439 | } |
| 440 | } |
| 441 | return events; |
| 442 | } |
| 443 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 444 | void InputState::clear() { |
| 445 | mKeyMementos.clear(); |
| 446 | mMotionMementos.clear(); |
| 447 | mFallbackKeys.clear(); |
| 448 | } |
| 449 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 450 | void InputState::mergePointerStateTo(InputState& other) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 451 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 452 | MotionMemento& memento = mMotionMementos[i]; |
| 453 | // Since we support split pointers we need to merge touch events |
| 454 | // from the same source + device + screen. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 455 | if (memento.source & AINPUT_SOURCE_CLASS_POINTER) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 456 | bool merged = false; |
| 457 | for (size_t j = 0; j < other.mMotionMementos.size(); j++) { |
| 458 | MotionMemento& otherMemento = other.mMotionMementos[j]; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 459 | if (memento.deviceId == otherMemento.deviceId && |
| 460 | memento.source == otherMemento.source && |
| 461 | memento.displayId == otherMemento.displayId) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 462 | memento.mergePointerStateTo(otherMemento); |
| 463 | merged = true; |
| 464 | break; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 467 | if (!merged) { |
| 468 | memento.firstNewPointerIdx = 0; |
| 469 | other.mMotionMementos.push_back(memento); |
| 470 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | int32_t InputState::getFallbackKey(int32_t originalKeyCode) { |
| 476 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 477 | return index >= 0 ? mFallbackKeys.valueAt(index) : -1; |
| 478 | } |
| 479 | |
| 480 | void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) { |
| 481 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 482 | if (index >= 0) { |
| 483 | mFallbackKeys.replaceValueAt(index, fallbackKeyCode); |
| 484 | } else { |
| 485 | mFallbackKeys.add(originalKeyCode, fallbackKeyCode); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void InputState::removeFallbackKey(int32_t originalKeyCode) { |
| 490 | mFallbackKeys.removeItem(originalKeyCode); |
| 491 | } |
| 492 | |
| 493 | bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) { |
| 494 | if (options.keyCode && memento.keyCode != options.keyCode.value()) { |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 507 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
| 508 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 509 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 510 | case CancelationOptions::Mode::CANCEL_FALLBACK_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 511 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
| 512 | default: |
| 513 | return false; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | bool InputState::shouldCancelMotion(const MotionMemento& memento, |
| 518 | const CancelationOptions& options) { |
| 519 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | switch (options.mode) { |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 528 | case CancelationOptions::Mode::CANCEL_ALL_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 529 | return true; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 530 | case CancelationOptions::Mode::CANCEL_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 531 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
Michael Wright | fb04fd5 | 2022-11-24 22:31:11 +0000 | [diff] [blame] | 532 | case CancelationOptions::Mode::CANCEL_NON_POINTER_EVENTS: |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 533 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
| 534 | default: |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | } // namespace android::inputdispatcher |