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