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 | |
| 23 | using android::base::StringPrintf; |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace inputdispatcher { |
| 28 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 29 | bool TouchedWindow::hasHoveringPointers() const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 30 | for (const auto& [_, state] : mDeviceStates) { |
| 31 | if (state.hoveringPointerIds.any()) { |
| 32 | return true; |
| 33 | } |
| 34 | } |
| 35 | return false; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 38 | bool TouchedWindow::hasHoveringPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 39 | const auto stateIt = mDeviceStates.find(deviceId); |
| 40 | if (stateIt == mDeviceStates.end()) { |
| 41 | return false; |
| 42 | } |
| 43 | const DeviceState& state = stateIt->second; |
| 44 | |
| 45 | return state.hoveringPointerIds.any(); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 48 | void TouchedWindow::clearHoveringPointers() { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 49 | for (auto& [_, state] : mDeviceStates) { |
| 50 | state.hoveringPointerIds.reset(); |
| 51 | } |
| 52 | |
| 53 | std::erase_if(mDeviceStates, [](const auto& pair) { return !pair.second.hasPointers(); }); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 56 | bool TouchedWindow::hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 57 | const auto stateIt = mDeviceStates.find(deviceId); |
| 58 | if (stateIt == mDeviceStates.end()) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 61 | const DeviceState& state = stateIt->second; |
| 62 | |
| 63 | return state.hoveringPointerIds.test(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 66 | void TouchedWindow::addHoveringPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 67 | mDeviceStates[deviceId].hoveringPointerIds.set(pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 70 | void TouchedWindow::addTouchingPointers(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 71 | std::bitset<MAX_POINTER_ID + 1> pointers) { |
| 72 | mDeviceStates[deviceId].touchingPointerIds |= pointers; |
| 73 | } |
| 74 | |
| 75 | bool TouchedWindow::hasTouchingPointers() const { |
| 76 | for (const auto& [_, state] : mDeviceStates) { |
| 77 | if (state.touchingPointerIds.any()) { |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 84 | bool TouchedWindow::hasTouchingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 85 | return getTouchingPointers(deviceId).any(); |
| 86 | } |
| 87 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 88 | bool TouchedWindow::hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 89 | return getTouchingPointers(deviceId).test(pointerId); |
| 90 | } |
| 91 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 92 | std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getTouchingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 93 | const auto stateIt = mDeviceStates.find(deviceId); |
| 94 | if (stateIt == mDeviceStates.end()) { |
| 95 | return {}; |
| 96 | } |
| 97 | const DeviceState& state = stateIt->second; |
| 98 | |
| 99 | return state.touchingPointerIds; |
| 100 | } |
| 101 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 102 | void TouchedWindow::removeTouchingPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 103 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 104 | pointerIds.set(pointerId, true); |
| 105 | |
| 106 | removeTouchingPointers(deviceId, pointerIds); |
| 107 | } |
| 108 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 109 | void TouchedWindow::removeTouchingPointers(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 110 | std::bitset<MAX_POINTER_ID + 1> pointers) { |
| 111 | const auto stateIt = mDeviceStates.find(deviceId); |
| 112 | if (stateIt == mDeviceStates.end()) { |
| 113 | return; |
| 114 | } |
| 115 | DeviceState& state = stateIt->second; |
| 116 | |
| 117 | state.touchingPointerIds &= ~pointers; |
| 118 | state.pilferingPointerIds &= ~pointers; |
| 119 | |
| 120 | if (!state.hasPointers()) { |
| 121 | mDeviceStates.erase(stateIt); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 125 | std::set<DeviceId> TouchedWindow::getTouchingDeviceIds() const { |
| 126 | std::set<DeviceId> deviceIds; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 127 | for (const auto& [deviceId, _] : mDeviceStates) { |
| 128 | deviceIds.insert(deviceId); |
| 129 | } |
| 130 | return deviceIds; |
| 131 | } |
| 132 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 133 | std::set<DeviceId> TouchedWindow::getActiveDeviceIds() const { |
| 134 | std::set<DeviceId> out; |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 135 | for (const auto& [deviceId, _] : mDeviceStates) { |
| 136 | out.emplace(deviceId); |
| 137 | } |
| 138 | return out; |
| 139 | } |
| 140 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 141 | bool TouchedWindow::hasPilferingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 142 | const auto stateIt = mDeviceStates.find(deviceId); |
| 143 | if (stateIt == mDeviceStates.end()) { |
| 144 | return false; |
| 145 | } |
| 146 | const DeviceState& state = stateIt->second; |
| 147 | |
| 148 | return state.pilferingPointerIds.any(); |
| 149 | } |
| 150 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 151 | void TouchedWindow::addPilferingPointers(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 152 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
| 153 | mDeviceStates[deviceId].pilferingPointerIds |= pointerIds; |
| 154 | } |
| 155 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 156 | void TouchedWindow::addPilferingPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 157 | mDeviceStates[deviceId].pilferingPointerIds.set(pointerId); |
| 158 | } |
| 159 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 160 | std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getPilferingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 161 | const auto stateIt = mDeviceStates.find(deviceId); |
| 162 | if (stateIt == mDeviceStates.end()) { |
| 163 | return {}; |
| 164 | } |
| 165 | const DeviceState& state = stateIt->second; |
| 166 | |
| 167 | return state.pilferingPointerIds; |
| 168 | } |
| 169 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 170 | std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> TouchedWindow::getPilferingPointers() const { |
| 171 | std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> out; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 172 | for (const auto& [deviceId, state] : mDeviceStates) { |
| 173 | out.emplace(deviceId, state.pilferingPointerIds); |
| 174 | } |
| 175 | return out; |
| 176 | } |
| 177 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 178 | std::optional<nsecs_t> TouchedWindow::getDownTimeInTarget(DeviceId deviceId) const { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 179 | const auto stateIt = mDeviceStates.find(deviceId); |
| 180 | if (stateIt == mDeviceStates.end()) { |
| 181 | return {}; |
| 182 | } |
| 183 | const DeviceState& state = stateIt->second; |
| 184 | return state.downTimeInTarget; |
| 185 | } |
| 186 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 187 | void TouchedWindow::trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 188 | auto [stateIt, _] = mDeviceStates.try_emplace(deviceId); |
| 189 | DeviceState& state = stateIt->second; |
| 190 | |
| 191 | if (!state.downTimeInTarget) { |
| 192 | state.downTimeInTarget = downTime; |
| 193 | } |
| 194 | } |
| 195 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 196 | void TouchedWindow::removeAllTouchingPointersForDevice(DeviceId deviceId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 197 | const auto stateIt = mDeviceStates.find(deviceId); |
| 198 | if (stateIt == mDeviceStates.end()) { |
| 199 | return; |
| 200 | } |
| 201 | DeviceState& state = stateIt->second; |
| 202 | |
| 203 | state.touchingPointerIds.reset(); |
| 204 | state.pilferingPointerIds.reset(); |
| 205 | state.downTimeInTarget.reset(); |
| 206 | |
| 207 | if (!state.hasPointers()) { |
| 208 | mDeviceStates.erase(stateIt); |
| 209 | } |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 212 | void TouchedWindow::removeHoveringPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 213 | const auto stateIt = mDeviceStates.find(deviceId); |
| 214 | if (stateIt == mDeviceStates.end()) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 217 | DeviceState& state = stateIt->second; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 218 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 219 | state.hoveringPointerIds.set(pointerId, false); |
| 220 | |
| 221 | if (!state.hasPointers()) { |
| 222 | mDeviceStates.erase(stateIt); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 226 | void TouchedWindow::removeAllHoveringPointersForDevice(DeviceId deviceId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 227 | const auto stateIt = mDeviceStates.find(deviceId); |
| 228 | if (stateIt == mDeviceStates.end()) { |
| 229 | return; |
| 230 | } |
| 231 | DeviceState& state = stateIt->second; |
| 232 | |
| 233 | state.hoveringPointerIds.reset(); |
| 234 | |
| 235 | if (!state.hasPointers()) { |
| 236 | mDeviceStates.erase(stateIt); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | std::string TouchedWindow::deviceStateToString(const TouchedWindow::DeviceState& state) { |
| 241 | return StringPrintf("[touchingPointerIds=%s, " |
| 242 | "downTimeInTarget=%s, hoveringPointerIds=%s, pilferingPointerIds=%s]", |
| 243 | bitsetToString(state.touchingPointerIds).c_str(), |
| 244 | toString(state.downTimeInTarget).c_str(), |
| 245 | bitsetToString(state.hoveringPointerIds).c_str(), |
| 246 | bitsetToString(state.pilferingPointerIds).c_str()); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 249 | std::string TouchedWindow::dump() const { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 250 | std::string out; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 251 | std::string deviceStates = |
| 252 | dumpMap(mDeviceStates, constToString, TouchedWindow::deviceStateToString); |
| 253 | out += StringPrintf("name='%s', targetFlags=%s, mDeviceStates=%s\n", |
| 254 | windowHandle->getName().c_str(), targetFlags.string().c_str(), |
| 255 | deviceStates.c_str()); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 256 | return out; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Siarhei Vishniakou | 72945a0 | 2023-09-18 18:30:25 -0700 | [diff] [blame] | 259 | std::ostream& operator<<(std::ostream& out, const TouchedWindow& window) { |
| 260 | out << window.dump(); |
| 261 | return out; |
| 262 | } |
| 263 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 264 | } // namespace inputdispatcher |
| 265 | } // namespace android |