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 | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 38 | bool TouchedWindow::hasHoveringPointers(int32_t 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 | |
| 56 | bool TouchedWindow::hasHoveringPointer(int32_t 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 | |
| 66 | void TouchedWindow::addHoveringPointer(int32_t 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 | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 70 | void TouchedWindow::addTouchingPointer(int32_t deviceId, int32_t pointerId) { |
| 71 | mDeviceStates[deviceId].touchingPointerIds.set(pointerId); |
| 72 | } |
| 73 | |
| 74 | void TouchedWindow::addTouchingPointers(int32_t deviceId, |
| 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 | |
| 88 | bool TouchedWindow::hasTouchingPointers(int32_t deviceId) const { |
| 89 | return getTouchingPointers(deviceId).any(); |
| 90 | } |
| 91 | |
| 92 | bool TouchedWindow::hasTouchingPointer(int32_t deviceId, int32_t pointerId) const { |
| 93 | return getTouchingPointers(deviceId).test(pointerId); |
| 94 | } |
| 95 | |
| 96 | std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getTouchingPointers(int32_t deviceId) const { |
| 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 | |
| 106 | void TouchedWindow::removeTouchingPointer(int32_t deviceId, int32_t pointerId) { |
| 107 | std::bitset<MAX_POINTER_ID + 1> pointerIds; |
| 108 | pointerIds.set(pointerId, true); |
| 109 | |
| 110 | removeTouchingPointers(deviceId, pointerIds); |
| 111 | } |
| 112 | |
| 113 | void TouchedWindow::removeTouchingPointers(int32_t deviceId, |
| 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 | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 129 | std::set<int32_t> TouchedWindow::getTouchingDeviceIds() const { |
| 130 | std::set<int32_t> deviceIds; |
| 131 | for (const auto& [deviceId, _] : mDeviceStates) { |
| 132 | deviceIds.insert(deviceId); |
| 133 | } |
| 134 | return deviceIds; |
| 135 | } |
| 136 | |
| 137 | bool TouchedWindow::hasPilferingPointers(int32_t deviceId) const { |
| 138 | const auto stateIt = mDeviceStates.find(deviceId); |
| 139 | if (stateIt == mDeviceStates.end()) { |
| 140 | return false; |
| 141 | } |
| 142 | const DeviceState& state = stateIt->second; |
| 143 | |
| 144 | return state.pilferingPointerIds.any(); |
| 145 | } |
| 146 | |
| 147 | void TouchedWindow::addPilferingPointers(int32_t deviceId, |
| 148 | std::bitset<MAX_POINTER_ID + 1> pointerIds) { |
| 149 | mDeviceStates[deviceId].pilferingPointerIds |= pointerIds; |
| 150 | } |
| 151 | |
| 152 | void TouchedWindow::addPilferingPointer(int32_t deviceId, int32_t pointerId) { |
| 153 | mDeviceStates[deviceId].pilferingPointerIds.set(pointerId); |
| 154 | } |
| 155 | |
| 156 | std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getPilferingPointers(int32_t deviceId) const { |
| 157 | const auto stateIt = mDeviceStates.find(deviceId); |
| 158 | if (stateIt == mDeviceStates.end()) { |
| 159 | return {}; |
| 160 | } |
| 161 | const DeviceState& state = stateIt->second; |
| 162 | |
| 163 | return state.pilferingPointerIds; |
| 164 | } |
| 165 | |
| 166 | std::map<int32_t, std::bitset<MAX_POINTER_ID + 1>> TouchedWindow::getPilferingPointers() const { |
| 167 | std::map<int32_t, std::bitset<MAX_POINTER_ID + 1>> out; |
| 168 | for (const auto& [deviceId, state] : mDeviceStates) { |
| 169 | out.emplace(deviceId, state.pilferingPointerIds); |
| 170 | } |
| 171 | return out; |
| 172 | } |
| 173 | |
| 174 | std::optional<nsecs_t> TouchedWindow::getDownTimeInTarget(int32_t deviceId) const { |
| 175 | const auto stateIt = mDeviceStates.find(deviceId); |
| 176 | if (stateIt == mDeviceStates.end()) { |
| 177 | return {}; |
| 178 | } |
| 179 | const DeviceState& state = stateIt->second; |
| 180 | return state.downTimeInTarget; |
| 181 | } |
| 182 | |
| 183 | void TouchedWindow::trySetDownTimeInTarget(int32_t deviceId, nsecs_t downTime) { |
| 184 | auto [stateIt, _] = mDeviceStates.try_emplace(deviceId); |
| 185 | DeviceState& state = stateIt->second; |
| 186 | |
| 187 | if (!state.downTimeInTarget) { |
| 188 | state.downTimeInTarget = downTime; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void TouchedWindow::removeAllTouchingPointersForDevice(int32_t deviceId) { |
| 193 | const auto stateIt = mDeviceStates.find(deviceId); |
| 194 | if (stateIt == mDeviceStates.end()) { |
| 195 | return; |
| 196 | } |
| 197 | DeviceState& state = stateIt->second; |
| 198 | |
| 199 | state.touchingPointerIds.reset(); |
| 200 | state.pilferingPointerIds.reset(); |
| 201 | state.downTimeInTarget.reset(); |
| 202 | |
| 203 | if (!state.hasPointers()) { |
| 204 | mDeviceStates.erase(stateIt); |
| 205 | } |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 208 | void TouchedWindow::removeHoveringPointer(int32_t deviceId, int32_t pointerId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 209 | const auto stateIt = mDeviceStates.find(deviceId); |
| 210 | if (stateIt == mDeviceStates.end()) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 213 | DeviceState& state = stateIt->second; |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 214 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 215 | state.hoveringPointerIds.set(pointerId, false); |
| 216 | |
| 217 | if (!state.hasPointers()) { |
| 218 | mDeviceStates.erase(stateIt); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 222 | void TouchedWindow::removeAllHoveringPointersForDevice(int32_t deviceId) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 223 | const auto stateIt = mDeviceStates.find(deviceId); |
| 224 | if (stateIt == mDeviceStates.end()) { |
| 225 | return; |
| 226 | } |
| 227 | DeviceState& state = stateIt->second; |
| 228 | |
| 229 | state.hoveringPointerIds.reset(); |
| 230 | |
| 231 | if (!state.hasPointers()) { |
| 232 | mDeviceStates.erase(stateIt); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | std::string TouchedWindow::deviceStateToString(const TouchedWindow::DeviceState& state) { |
| 237 | return StringPrintf("[touchingPointerIds=%s, " |
| 238 | "downTimeInTarget=%s, hoveringPointerIds=%s, pilferingPointerIds=%s]", |
| 239 | bitsetToString(state.touchingPointerIds).c_str(), |
| 240 | toString(state.downTimeInTarget).c_str(), |
| 241 | bitsetToString(state.hoveringPointerIds).c_str(), |
| 242 | bitsetToString(state.pilferingPointerIds).c_str()); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 245 | std::string TouchedWindow::dump() const { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 246 | std::string out; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame^] | 247 | std::string deviceStates = |
| 248 | dumpMap(mDeviceStates, constToString, TouchedWindow::deviceStateToString); |
| 249 | out += StringPrintf("name='%s', targetFlags=%s, mDeviceStates=%s\n", |
| 250 | windowHandle->getName().c_str(), targetFlags.string().c_str(), |
| 251 | deviceStates.c_str()); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 252 | return out; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } // namespace inputdispatcher |
| 256 | } // namespace android |