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