Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "TouchedWindow.h" |
| 18 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
| 21 | #include <input/PrintTools.h> |
| 22 | |
Siarhei Vishniakou | e535f97 | 2024-03-29 14:55:21 -0700 | [diff] [blame] | 23 | using android::base::Result; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 24 | using android::base::StringPrintf; |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | namespace inputdispatcher { |
| 29 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | bool hasPointerId(const std::vector<PointerProperties>& pointers, int32_t pointerId) { |
| 33 | return std::find_if(pointers.begin(), pointers.end(), |
| 34 | [&pointerId](const PointerProperties& properties) { |
| 35 | return properties.id == pointerId; |
| 36 | }) != pointers.end(); |
| 37 | } |
| 38 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 39 | bool hasPointerId(const std::vector<TouchedWindow::HoveringPointer>& pointers, int32_t pointerId) { |
| 40 | return std::find_if(pointers.begin(), pointers.end(), |
| 41 | [&pointerId](const TouchedWindow::HoveringPointer& pointer) { |
| 42 | return pointer.properties.id == pointerId; |
| 43 | }) != pointers.end(); |
| 44 | } |
| 45 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 46 | } // namespace |
| 47 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 48 | bool TouchedWindow::hasHoveringPointers() const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 49 | for (const auto& [_, state] : mDeviceStates) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 50 | if (!state.hoveringPointers.empty()) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | return false; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 57 | bool TouchedWindow::hasHoveringPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 58 | const auto stateIt = mDeviceStates.find(deviceId); |
| 59 | if (stateIt == mDeviceStates.end()) { |
| 60 | return false; |
| 61 | } |
| 62 | const DeviceState& state = stateIt->second; |
| 63 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 64 | return !state.hoveringPointers.empty(); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 67 | void TouchedWindow::clearHoveringPointers(DeviceId deviceId) { |
| 68 | auto stateIt = mDeviceStates.find(deviceId); |
| 69 | if (stateIt == mDeviceStates.end()) { |
| 70 | return; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 71 | } |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 72 | DeviceState& state = stateIt->second; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 73 | state.hoveringPointers.clear(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 74 | if (!state.hasPointers()) { |
| 75 | mDeviceStates.erase(stateIt); |
| 76 | } |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 79 | bool TouchedWindow::hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 80 | const auto stateIt = mDeviceStates.find(deviceId); |
| 81 | if (stateIt == mDeviceStates.end()) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 84 | const DeviceState& state = stateIt->second; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 85 | return hasPointerId(state.hoveringPointers, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 88 | void TouchedWindow::addHoveringPointer(DeviceId deviceId, const PointerProperties& properties, |
| 89 | float x, float y) { |
| 90 | std::vector<HoveringPointer>& hoveringPointers = mDeviceStates[deviceId].hoveringPointers; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 91 | const size_t initialSize = hoveringPointers.size(); |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 92 | std::erase_if(hoveringPointers, [&properties](const HoveringPointer& pointer) { |
| 93 | return pointer.properties.id == properties.id; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 94 | }); |
| 95 | if (hoveringPointers.size() != initialSize) { |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 96 | LOG(ERROR) << __func__ << ": " << properties << ", device " << deviceId << " was in " |
| 97 | << *this; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 98 | } |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 99 | hoveringPointers.push_back({properties, x, y}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Siarhei Vishniakou | e535f97 | 2024-03-29 14:55:21 -0700 | [diff] [blame] | 102 | Result<void> TouchedWindow::addTouchingPointers(DeviceId deviceId, |
| 103 | const std::vector<PointerProperties>& pointers) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 104 | std::vector<PointerProperties>& touchingPointers = mDeviceStates[deviceId].touchingPointers; |
| 105 | const size_t initialSize = touchingPointers.size(); |
| 106 | for (const PointerProperties& pointer : pointers) { |
| 107 | std::erase_if(touchingPointers, [&pointer](const PointerProperties& properties) { |
| 108 | return properties.id == pointer.id; |
| 109 | }); |
| 110 | } |
Siarhei Vishniakou | e535f97 | 2024-03-29 14:55:21 -0700 | [diff] [blame] | 111 | const bool foundInconsistentState = touchingPointers.size() != initialSize; |
| 112 | touchingPointers.insert(touchingPointers.end(), pointers.begin(), pointers.end()); |
| 113 | if (foundInconsistentState) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 114 | LOG(ERROR) << __func__ << ": " << dumpVector(pointers, streamableToString) << ", device " |
| 115 | << deviceId << " already in " << *this; |
Siarhei Vishniakou | e535f97 | 2024-03-29 14:55:21 -0700 | [diff] [blame] | 116 | return android::base::Error(); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 117 | } |
Siarhei Vishniakou | e535f97 | 2024-03-29 14:55:21 -0700 | [diff] [blame] | 118 | return {}; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool TouchedWindow::hasTouchingPointers() const { |
| 122 | for (const auto& [_, state] : mDeviceStates) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 123 | if (!state.touchingPointers.empty()) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 130 | bool TouchedWindow::hasTouchingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 131 | return !getTouchingPointers(deviceId).empty(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 134 | bool TouchedWindow::hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 135 | const auto stateIt = mDeviceStates.find(deviceId); |
| 136 | if (stateIt == mDeviceStates.end()) { |
| 137 | return false; |
| 138 | } |
| 139 | const DeviceState& state = stateIt->second; |
| 140 | return hasPointerId(state.touchingPointers, pointerId); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 143 | std::vector<PointerProperties> TouchedWindow::getTouchingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 144 | const auto stateIt = mDeviceStates.find(deviceId); |
| 145 | if (stateIt == mDeviceStates.end()) { |
| 146 | return {}; |
| 147 | } |
| 148 | const DeviceState& state = stateIt->second; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 149 | return state.touchingPointers; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 152 | void TouchedWindow::removeTouchingPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 153 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 154 | pointerIds.set(pointerId, true); |
| 155 | |
| 156 | removeTouchingPointers(deviceId, pointerIds); |
| 157 | } |
| 158 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 159 | void TouchedWindow::removeTouchingPointers(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 160 | std::bitset<MAX_POINTER_ID + 1> pointers) { |
| 161 | const auto stateIt = mDeviceStates.find(deviceId); |
| 162 | if (stateIt == mDeviceStates.end()) { |
| 163 | return; |
| 164 | } |
| 165 | DeviceState& state = stateIt->second; |
| 166 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 167 | std::erase_if(state.touchingPointers, [&pointers](const PointerProperties& properties) { |
| 168 | return pointers.test(properties.id); |
| 169 | }); |
| 170 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 171 | state.pilferingPointerIds &= ~pointers; |
| 172 | |
| 173 | if (!state.hasPointers()) { |
| 174 | mDeviceStates.erase(stateIt); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 178 | bool TouchedWindow::hasActiveStylus() const { |
| 179 | for (const auto& [_, state] : mDeviceStates) { |
| 180 | for (const PointerProperties& properties : state.touchingPointers) { |
| 181 | if (properties.toolType == ToolType::STYLUS) { |
| 182 | return true; |
| 183 | } |
| 184 | } |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 185 | for (const HoveringPointer& pointer : state.hoveringPointers) { |
| 186 | if (pointer.properties.toolType == ToolType::STYLUS) { |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 194 | std::set<DeviceId> TouchedWindow::getTouchingDeviceIds() const { |
| 195 | std::set<DeviceId> deviceIds; |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 196 | for (const auto& [deviceId, deviceState] : mDeviceStates) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 197 | if (!deviceState.touchingPointers.empty()) { |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 198 | deviceIds.insert(deviceId); |
| 199 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 200 | } |
| 201 | return deviceIds; |
| 202 | } |
| 203 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 204 | bool TouchedWindow::hasPilferingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 205 | const auto stateIt = mDeviceStates.find(deviceId); |
| 206 | if (stateIt == mDeviceStates.end()) { |
| 207 | return false; |
| 208 | } |
| 209 | const DeviceState& state = stateIt->second; |
| 210 | |
| 211 | return state.pilferingPointerIds.any(); |
| 212 | } |
| 213 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 214 | void TouchedWindow::addPilferingPointers(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 215 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
| 216 | mDeviceStates[deviceId].pilferingPointerIds |= pointerIds; |
| 217 | } |
| 218 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 219 | void TouchedWindow::addPilferingPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 220 | mDeviceStates[deviceId].pilferingPointerIds.set(pointerId); |
| 221 | } |
| 222 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 223 | std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getPilferingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 224 | const auto stateIt = mDeviceStates.find(deviceId); |
| 225 | if (stateIt == mDeviceStates.end()) { |
| 226 | return {}; |
| 227 | } |
| 228 | const DeviceState& state = stateIt->second; |
| 229 | |
| 230 | return state.pilferingPointerIds; |
| 231 | } |
| 232 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 233 | std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> TouchedWindow::getPilferingPointers() const { |
| 234 | std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> out; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 235 | for (const auto& [deviceId, state] : mDeviceStates) { |
| 236 | out.emplace(deviceId, state.pilferingPointerIds); |
| 237 | } |
| 238 | return out; |
| 239 | } |
| 240 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 241 | std::optional<nsecs_t> TouchedWindow::getDownTimeInTarget(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 242 | const auto stateIt = mDeviceStates.find(deviceId); |
| 243 | if (stateIt == mDeviceStates.end()) { |
| 244 | return {}; |
| 245 | } |
| 246 | const DeviceState& state = stateIt->second; |
| 247 | return state.downTimeInTarget; |
| 248 | } |
| 249 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 250 | void TouchedWindow::trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 251 | auto [stateIt, _] = mDeviceStates.try_emplace(deviceId); |
| 252 | DeviceState& state = stateIt->second; |
| 253 | |
| 254 | if (!state.downTimeInTarget) { |
| 255 | state.downTimeInTarget = downTime; |
| 256 | } |
| 257 | } |
| 258 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 259 | void TouchedWindow::removeAllTouchingPointersForDevice(DeviceId deviceId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 260 | const auto stateIt = mDeviceStates.find(deviceId); |
| 261 | if (stateIt == mDeviceStates.end()) { |
| 262 | return; |
| 263 | } |
| 264 | DeviceState& state = stateIt->second; |
| 265 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 266 | state.touchingPointers.clear(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 267 | state.pilferingPointerIds.reset(); |
| 268 | state.downTimeInTarget.reset(); |
| 269 | |
| 270 | if (!state.hasPointers()) { |
| 271 | mDeviceStates.erase(stateIt); |
| 272 | } |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 275 | void TouchedWindow::removeHoveringPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 276 | const auto stateIt = mDeviceStates.find(deviceId); |
| 277 | if (stateIt == mDeviceStates.end()) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 280 | DeviceState& state = stateIt->second; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 281 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 282 | std::erase_if(state.hoveringPointers, [&pointerId](const HoveringPointer& pointer) { |
| 283 | return pointer.properties.id == pointerId; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 284 | }); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 285 | |
| 286 | if (!state.hasPointers()) { |
| 287 | mDeviceStates.erase(stateIt); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 291 | std::vector<DeviceId> TouchedWindow::eraseHoveringPointersIf( |
| 292 | std::function<bool(const PointerProperties&, float /*x*/, float /*y*/)> condition) { |
| 293 | std::vector<DeviceId> erasedDevices; |
| 294 | for (auto& [deviceId, state] : mDeviceStates) { |
| 295 | std::erase_if(state.hoveringPointers, [&](const HoveringPointer& pointer) { |
| 296 | if (condition(pointer.properties, pointer.x, pointer.y)) { |
| 297 | erasedDevices.push_back(deviceId); |
| 298 | return true; |
| 299 | } |
| 300 | return false; |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | return erasedDevices; |
| 305 | } |
| 306 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 307 | void TouchedWindow::removeAllHoveringPointersForDevice(DeviceId deviceId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 308 | const auto stateIt = mDeviceStates.find(deviceId); |
| 309 | if (stateIt == mDeviceStates.end()) { |
| 310 | return; |
| 311 | } |
| 312 | DeviceState& state = stateIt->second; |
| 313 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 314 | state.hoveringPointers.clear(); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 315 | |
| 316 | if (!state.hasPointers()) { |
| 317 | mDeviceStates.erase(stateIt); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | std::string TouchedWindow::deviceStateToString(const TouchedWindow::DeviceState& state) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 322 | return StringPrintf("[touchingPointers=%s, " |
| 323 | "downTimeInTarget=%s, hoveringPointers=%s, pilferingPointerIds=%s]", |
| 324 | dumpVector(state.touchingPointers, streamableToString).c_str(), |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 325 | toString(state.downTimeInTarget).c_str(), |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 326 | dumpVector(state.hoveringPointers, streamableToString).c_str(), |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 327 | bitsetToString(state.pilferingPointerIds).c_str()); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 330 | std::string TouchedWindow::dump() const { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 331 | std::string out; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 332 | std::string deviceStates = |
| 333 | dumpMap(mDeviceStates, constToString, TouchedWindow::deviceStateToString); |
| 334 | out += StringPrintf("name='%s', targetFlags=%s, mDeviceStates=%s\n", |
| 335 | windowHandle->getName().c_str(), targetFlags.string().c_str(), |
| 336 | deviceStates.c_str()); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 337 | return out; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame^] | 340 | std::ostream& operator<<(std::ostream& out, const TouchedWindow::HoveringPointer& pointer) { |
| 341 | out << pointer.properties << " at (" << pointer.x << ", " << pointer.y << ")"; |
| 342 | return out; |
| 343 | } |
| 344 | |
Siarhei Vishniakou | 72945a0 | 2023-09-18 18:30:25 -0700 | [diff] [blame] | 345 | std::ostream& operator<<(std::ostream& out, const TouchedWindow& window) { |
| 346 | out << window.dump(); |
| 347 | return out; |
| 348 | } |
| 349 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 350 | } // namespace inputdispatcher |
| 351 | } // namespace android |