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 | |
| 17 | #include "InputState.h" |
| 18 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include "InputDispatcher.h" |
| 20 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 21 | namespace android::inputdispatcher { |
| 22 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 23 | InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {} |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 24 | |
| 25 | InputState::~InputState() {} |
| 26 | |
| 27 | bool InputState::isNeutral() const { |
| 28 | return mKeyMementos.empty() && mMotionMementos.empty(); |
| 29 | } |
| 30 | |
| 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: { |
| 96 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 97 | if (index >= 0) { |
| 98 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 99 | return true; |
| 100 | } |
| 101 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 102 | ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, " |
| 103 | "displayId=%" PRId32 ", actionMasked=%d", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 104 | entry.deviceId, entry.source, entry.displayId, actionMasked); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 105 | #endif |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | case AMOTION_EVENT_ACTION_DOWN: { |
| 110 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 111 | if (index >= 0) { |
| 112 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 113 | } |
| 114 | addMotionMemento(entry, flags, false /*hovering*/); |
| 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 | |
| 128 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 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()) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 144 | addMotionMemento(entry, flags, false /*hovering*/); |
| 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 | } |
| 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", |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 161 | entry.deviceId, entry.source, entry.displayId, actionMasked); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 162 | #endif |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 167 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 168 | if (index >= 0) { |
| 169 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 170 | return true; |
| 171 | } |
| 172 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 173 | ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x, " |
| 174 | "displayId=%" PRId32, |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 175 | entry.deviceId, entry.source, entry.displayId); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 176 | #endif |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 181 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
| 182 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 183 | if (index >= 0) { |
| 184 | mMotionMementos.erase(mMotionMementos.begin() + index); |
| 185 | } |
| 186 | addMotionMemento(entry, flags, true /*hovering*/); |
| 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, |
| 279 | 0 /*repeatCount*/, 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)) { |
| 285 | const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT |
| 286 | : AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 287 | events.push_back( |
| 288 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 289 | memento.deviceId, memento.source, |
| 290 | memento.displayId, memento.policyFlags, action, |
| 291 | 0 /*actionButton*/, memento.flags, AMETA_NONE, |
| 292 | 0 /*buttonState*/, MotionClassification::NONE, |
| 293 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 294 | memento.yPrecision, memento.xCursorPosition, |
| 295 | memento.yCursorPosition, memento.downTime, |
| 296 | memento.pointerCount, memento.pointerProperties, |
| 297 | memento.pointerCoords, 0 /*xOffset*/, |
| 298 | 0 /*yOffset*/)); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 301 | return events; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 304 | std::vector<std::unique_ptr<EventEntry>> InputState::synthesizePointerDownEvents( |
| 305 | nsecs_t currentTime) { |
| 306 | std::vector<std::unique_ptr<EventEntry>> events; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 307 | for (MotionMemento& memento : mMotionMementos) { |
| 308 | if (!(memento.source & AINPUT_SOURCE_CLASS_POINTER)) { |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | if (memento.firstNewPointerIdx < 0) { |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | uint32_t pointerCount = 0; |
| 317 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 318 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 319 | |
| 320 | // We will deliver all pointers the target already knows about |
| 321 | for (uint32_t i = 0; i < static_cast<uint32_t>(memento.firstNewPointerIdx); i++) { |
| 322 | pointerProperties[i].copyFrom(memento.pointerProperties[i]); |
| 323 | pointerCoords[i].copyFrom(memento.pointerCoords[i]); |
| 324 | pointerCount++; |
| 325 | } |
| 326 | |
| 327 | // We will send explicit events for all pointers the target doesn't know about |
| 328 | for (uint32_t i = static_cast<uint32_t>(memento.firstNewPointerIdx); |
| 329 | i < memento.pointerCount; i++) { |
| 330 | |
| 331 | pointerProperties[i].copyFrom(memento.pointerProperties[i]); |
| 332 | pointerCoords[i].copyFrom(memento.pointerCoords[i]); |
| 333 | pointerCount++; |
| 334 | |
| 335 | // Down only if the first pointer, pointer down otherwise |
| 336 | const int32_t action = (pointerCount <= 1) |
| 337 | ? AMOTION_EVENT_ACTION_DOWN |
| 338 | : AMOTION_EVENT_ACTION_POINTER_DOWN |
| 339 | | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 340 | |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 341 | events.push_back( |
| 342 | std::make_unique<MotionEntry>(mIdGenerator.nextId(), currentTime, |
| 343 | memento.deviceId, memento.source, |
| 344 | memento.displayId, memento.policyFlags, action, |
| 345 | 0 /*actionButton*/, memento.flags, AMETA_NONE, |
| 346 | 0 /*buttonState*/, MotionClassification::NONE, |
| 347 | AMOTION_EVENT_EDGE_FLAG_NONE, memento.xPrecision, |
| 348 | memento.yPrecision, memento.xCursorPosition, |
| 349 | memento.yCursorPosition, memento.downTime, |
| 350 | pointerCount, pointerProperties, pointerCoords, |
| 351 | 0 /*xOffset*/, 0 /*yOffset*/)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | memento.firstNewPointerIdx = INVALID_POINTER_INDEX; |
| 355 | } |
| 356 | |
| 357 | return events; |
| 358 | } |
| 359 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 360 | void InputState::clear() { |
| 361 | mKeyMementos.clear(); |
| 362 | mMotionMementos.clear(); |
| 363 | mFallbackKeys.clear(); |
| 364 | } |
| 365 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 366 | void InputState::mergePointerStateTo(InputState& other) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 367 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 368 | MotionMemento& memento = mMotionMementos[i]; |
| 369 | // Since we support split pointers we need to merge touch events |
| 370 | // from the same source + device + screen. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 371 | if (memento.source & AINPUT_SOURCE_CLASS_POINTER) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 372 | bool merged = false; |
| 373 | for (size_t j = 0; j < other.mMotionMementos.size(); j++) { |
| 374 | MotionMemento& otherMemento = other.mMotionMementos[j]; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 375 | if (memento.deviceId == otherMemento.deviceId && |
| 376 | memento.source == otherMemento.source && |
| 377 | memento.displayId == otherMemento.displayId) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 378 | memento.mergePointerStateTo(otherMemento); |
| 379 | merged = true; |
| 380 | break; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 383 | if (!merged) { |
| 384 | memento.firstNewPointerIdx = 0; |
| 385 | other.mMotionMementos.push_back(memento); |
| 386 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | int32_t InputState::getFallbackKey(int32_t originalKeyCode) { |
| 392 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 393 | return index >= 0 ? mFallbackKeys.valueAt(index) : -1; |
| 394 | } |
| 395 | |
| 396 | void InputState::setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode) { |
| 397 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 398 | if (index >= 0) { |
| 399 | mFallbackKeys.replaceValueAt(index, fallbackKeyCode); |
| 400 | } else { |
| 401 | mFallbackKeys.add(originalKeyCode, fallbackKeyCode); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void InputState::removeFallbackKey(int32_t originalKeyCode) { |
| 406 | mFallbackKeys.removeItem(originalKeyCode); |
| 407 | } |
| 408 | |
| 409 | bool InputState::shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options) { |
| 410 | if (options.keyCode && memento.keyCode != options.keyCode.value()) { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | switch (options.mode) { |
| 423 | case CancelationOptions::CANCEL_ALL_EVENTS: |
| 424 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
| 425 | return true; |
| 426 | case CancelationOptions::CANCEL_FALLBACK_EVENTS: |
| 427 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
| 428 | default: |
| 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | bool InputState::shouldCancelMotion(const MotionMemento& memento, |
| 434 | const CancelationOptions& options) { |
| 435 | if (options.deviceId && memento.deviceId != options.deviceId.value()) { |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | if (options.displayId && memento.displayId != options.displayId.value()) { |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | switch (options.mode) { |
| 444 | case CancelationOptions::CANCEL_ALL_EVENTS: |
| 445 | return true; |
| 446 | case CancelationOptions::CANCEL_POINTER_EVENTS: |
| 447 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
| 448 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
| 449 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
| 450 | default: |
| 451 | return false; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | } // namespace android::inputdispatcher |