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 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 17 | #include <android-base/logging.h> |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 18 | #include <android-base/stringprintf.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 19 | #include <gui/WindowInfo.h> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 20 | |
| 21 | #include "InputTarget.h" |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include "TouchState.h" |
| 23 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 24 | using namespace android::ftl::flag_operators; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 25 | using android::base::StringPrintf; |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 26 | using android::gui::WindowInfo; |
| 27 | using android::gui::WindowInfoHandle; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android::inputdispatcher { |
| 30 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 31 | void TouchState::reset() { |
Prabir Pradhan | e680f9b | 2022-02-04 04:24:00 -0800 | [diff] [blame] | 32 | *this = TouchState(); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 35 | bool TouchState::hasTouchingPointers(DeviceId deviceId) const { |
Siarhei Vishniakou | 45504fe | 2023-05-05 16:05:10 -0700 | [diff] [blame] | 36 | return std::any_of(windows.begin(), windows.end(), [&](const TouchedWindow& window) { |
| 37 | return window.hasTouchingPointers(deviceId); |
| 38 | }); |
| 39 | } |
| 40 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 41 | void TouchState::removeTouchingPointer(DeviceId deviceId, int32_t pointerId) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 42 | for (TouchedWindow& touchedWindow : windows) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 43 | touchedWindow.removeTouchingPointer(deviceId, pointerId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 44 | } |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 45 | clearWindowsWithoutPointers(); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 48 | void TouchState::removeTouchingPointerFromWindow( |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 49 | DeviceId deviceId, int32_t pointerId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 50 | const sp<android::gui::WindowInfoHandle>& windowHandle) { |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 51 | for (TouchedWindow& touchedWindow : windows) { |
| 52 | if (touchedWindow.windowHandle == windowHandle) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 53 | touchedWindow.removeTouchingPointer(deviceId, pointerId); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 54 | clearWindowsWithoutPointers(); |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 60 | void TouchState::clearHoveringPointers(DeviceId deviceId) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 61 | for (TouchedWindow& touchedWindow : windows) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 62 | touchedWindow.removeAllHoveringPointersForDevice(deviceId); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 63 | } |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 64 | clearWindowsWithoutPointers(); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void TouchState::clearWindowsWithoutPointers() { |
| 68 | std::erase_if(windows, [](const TouchedWindow& w) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 69 | return !w.hasTouchingPointers() && !w.hasHoveringPointers(); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 70 | }); |
| 71 | } |
| 72 | |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 73 | void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle, |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 74 | InputTarget::DispatchMode dispatchMode, |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 75 | ftl::Flags<InputTarget::Flags> targetFlags, DeviceId deviceId, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 76 | const std::vector<PointerProperties>& touchingPointers, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 77 | std::optional<nsecs_t> firstDownTimeInTarget) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 78 | if (touchingPointers.empty()) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 79 | LOG(FATAL) << __func__ << "No pointers specified for " << windowHandle->getName(); |
| 80 | return; |
| 81 | } |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 82 | for (TouchedWindow& touchedWindow : windows) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 83 | // We do not compare windows by token here because two windows that share the same token |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 84 | // may have a different transform. They will be combined later when we create InputTargets. |
| 85 | // At that point, per-pointer window transform will be considered. |
| 86 | // An alternative design choice here would have been to compare here by token, but to |
| 87 | // store per-pointer transform. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 88 | if (touchedWindow.windowHandle == windowHandle) { |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 89 | touchedWindow.dispatchMode = dispatchMode; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 90 | touchedWindow.targetFlags |= targetFlags; |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 91 | // For cases like hover enter/exit or DISPATCH_AS_OUTSIDE a touch window might not have |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 92 | // downTime set initially. Need to update existing window when a pointer is down for the |
| 93 | // window. |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 94 | touchedWindow.addTouchingPointers(deviceId, touchingPointers); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 95 | if (firstDownTimeInTarget) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 96 | touchedWindow.trySetDownTimeInTarget(deviceId, *firstDownTimeInTarget); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 97 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 101 | TouchedWindow touchedWindow; |
| 102 | touchedWindow.windowHandle = windowHandle; |
Prabir Pradhan | 4b09c1f | 2023-11-17 03:16:25 +0000 | [diff] [blame] | 103 | touchedWindow.dispatchMode = dispatchMode; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 104 | touchedWindow.targetFlags = targetFlags; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 105 | touchedWindow.addTouchingPointers(deviceId, touchingPointers); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 106 | if (firstDownTimeInTarget) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 107 | touchedWindow.trySetDownTimeInTarget(deviceId, *firstDownTimeInTarget); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 108 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 109 | windows.push_back(touchedWindow); |
| 110 | } |
| 111 | |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 112 | void TouchState::addHoveringPointerToWindow(const sp<WindowInfoHandle>& windowHandle, |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 113 | DeviceId deviceId, const PointerProperties& pointer) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 114 | for (TouchedWindow& touchedWindow : windows) { |
| 115 | if (touchedWindow.windowHandle == windowHandle) { |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 116 | touchedWindow.addHoveringPointer(deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | TouchedWindow touchedWindow; |
| 122 | touchedWindow.windowHandle = windowHandle; |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 123 | touchedWindow.addHoveringPointer(deviceId, pointer); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 124 | windows.push_back(touchedWindow); |
| 125 | } |
| 126 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 127 | void TouchState::removeWindowByToken(const sp<IBinder>& token) { |
| 128 | for (size_t i = 0; i < windows.size(); i++) { |
| 129 | if (windows[i].windowHandle->getToken() == token) { |
| 130 | windows.erase(windows.begin() + i); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 136 | void TouchState::cancelPointersForWindowsExcept(DeviceId deviceId, |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 137 | std::bitset<MAX_POINTER_ID + 1> pointerIds, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 138 | const sp<IBinder>& token) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 139 | std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) { |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 140 | if (w.windowHandle->getToken() != token) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 141 | w.removeTouchingPointers(deviceId, pointerIds); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 142 | } |
| 143 | }); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 144 | clearWindowsWithoutPointers(); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 147 | /** |
| 148 | * For any pointer that's being pilfered, remove it from all of the other windows that currently |
| 149 | * aren't pilfering it. For example, if we determined that pointer 1 is going to both window A and |
| 150 | * window B, but window A is currently pilfering pointer 1, then pointer 1 should not go to window |
| 151 | * B. |
| 152 | */ |
| 153 | void TouchState::cancelPointersForNonPilferingWindows() { |
| 154 | // First, find all pointers that are being pilfered, across all windows |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 155 | std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> allPilferedPointerIdsByDevice; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 156 | for (const TouchedWindow& w : windows) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 157 | for (const auto& [deviceId, pilferedPointerIds] : w.getPilferingPointers()) { |
| 158 | allPilferedPointerIdsByDevice[deviceId] |= pilferedPointerIds; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 159 | } |
| 160 | }; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 161 | |
| 162 | // Optimization: most of the time, pilfering does not occur |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 163 | if (allPilferedPointerIdsByDevice.empty()) return; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 164 | |
| 165 | // Now, remove all pointers from every window that's being pilfered by other windows. |
| 166 | // For example, if window A is pilfering pointer 1 (only), and window B is pilfering pointer 2 |
| 167 | // (only), the remove pointer 2 from window A and pointer 1 from window B. Usually, the set of |
| 168 | // pilfered pointers will be disjoint across all windows, but there's no reason to cause that |
| 169 | // limitation here. |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 170 | for (const auto& [deviceId, allPilferedPointerIds] : allPilferedPointerIdsByDevice) { |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 171 | std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) { |
| 172 | std::bitset<MAX_POINTER_ID + 1> pilferedByOtherWindows = |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 173 | w.getPilferingPointers(deviceId) ^ allPilferedPointerIds; |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 174 | // Remove all pointers pilfered by other windows |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 175 | w.removeTouchingPointers(deviceId, pilferedByOtherWindows); |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 176 | }); |
| 177 | } |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 178 | clearWindowsWithoutPointers(); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 179 | } |
| 180 | |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 181 | sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 182 | for (size_t i = 0; i < windows.size(); i++) { |
| 183 | const TouchedWindow& window = windows[i]; |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 184 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 185 | return window.windowHandle; |
| 186 | } |
| 187 | } |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | bool TouchState::isSlippery() const { |
| 192 | // Must have exactly one foreground window. |
| 193 | bool haveSlipperyForegroundWindow = false; |
| 194 | for (const TouchedWindow& window : windows) { |
Siarhei Vishniakou | 253f464 | 2022-11-09 13:42:06 -0800 | [diff] [blame] | 195 | if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 196 | if (haveSlipperyForegroundWindow || |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 197 | !window.windowHandle->getInfo()->inputConfig.test( |
| 198 | WindowInfo::InputConfig::SLIPPERY)) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | haveSlipperyForegroundWindow = true; |
| 202 | } |
| 203 | } |
| 204 | return haveSlipperyForegroundWindow; |
| 205 | } |
| 206 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 207 | sp<WindowInfoHandle> TouchState::getWallpaperWindow() const { |
| 208 | for (size_t i = 0; i < windows.size(); i++) { |
| 209 | const TouchedWindow& window = windows[i]; |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 210 | if (window.windowHandle->getInfo()->inputConfig.test( |
| 211 | gui::WindowInfo::InputConfig::IS_WALLPAPER)) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 212 | return window.windowHandle; |
| 213 | } |
| 214 | } |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
Siarhei Vishniakou | 0026b4c | 2022-11-10 19:33:29 -0800 | [diff] [blame] | 218 | const TouchedWindow& TouchState::getTouchedWindow(const sp<WindowInfoHandle>& windowHandle) const { |
| 219 | auto it = std::find_if(windows.begin(), windows.end(), |
| 220 | [&](const TouchedWindow& w) { return w.windowHandle == windowHandle; }); |
| 221 | LOG_ALWAYS_FATAL_IF(it == windows.end(), "Could not find %s", windowHandle->getName().c_str()); |
| 222 | return *it; |
| 223 | } |
| 224 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 225 | bool TouchState::isDown(DeviceId deviceId) const { |
| 226 | return std::any_of(windows.begin(), windows.end(), [&deviceId](const TouchedWindow& window) { |
| 227 | return window.hasTouchingPointers(deviceId); |
| 228 | }); |
Siarhei Vishniakou | 3ad385b | 2022-11-04 10:09:53 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 231 | bool TouchState::hasHoveringPointers(DeviceId deviceId) const { |
| 232 | return std::any_of(windows.begin(), windows.end(), [&deviceId](const TouchedWindow& window) { |
| 233 | return window.hasHoveringPointers(deviceId); |
| 234 | }); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 237 | bool TouchState::hasActiveStylus() const { |
| 238 | return std::any_of(windows.begin(), windows.end(), |
| 239 | [](const TouchedWindow& window) { return window.hasActiveStylus(); }); |
| 240 | } |
| 241 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 242 | std::set<sp<WindowInfoHandle>> TouchState::getWindowsWithHoveringPointer(DeviceId deviceId, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 243 | int32_t pointerId) const { |
| 244 | std::set<sp<WindowInfoHandle>> out; |
| 245 | for (const TouchedWindow& window : windows) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 246 | if (window.hasHoveringPointer(deviceId, pointerId)) { |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 247 | out.insert(window.windowHandle); |
| 248 | } |
| 249 | } |
| 250 | return out; |
| 251 | } |
| 252 | |
| 253 | void TouchState::removeHoveringPointer(int32_t hoveringDeviceId, int32_t hoveringPointerId) { |
| 254 | for (TouchedWindow& window : windows) { |
| 255 | window.removeHoveringPointer(hoveringDeviceId, hoveringPointerId); |
| 256 | } |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 257 | clearWindowsWithoutPointers(); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 260 | void TouchState::removeAllPointersForDevice(DeviceId deviceId) { |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 261 | for (TouchedWindow& window : windows) { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 262 | window.removeAllHoveringPointersForDevice(deviceId); |
| 263 | window.removeAllTouchingPointersForDevice(deviceId); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 264 | } |
Siarhei Vishniakou | 0836a30 | 2023-05-03 13:54:30 -0700 | [diff] [blame] | 265 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 266 | clearWindowsWithoutPointers(); |
| 267 | } |
| 268 | |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 269 | std::string TouchState::dump() const { |
| 270 | std::string out; |
Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 271 | if (!windows.empty()) { |
| 272 | out += " Windows:\n"; |
| 273 | for (size_t i = 0; i < windows.size(); i++) { |
| 274 | const TouchedWindow& touchedWindow = windows[i]; |
| 275 | out += StringPrintf(" %zu : ", i) + touchedWindow.dump(); |
| 276 | } |
| 277 | } else { |
| 278 | out += " Windows: <none>\n"; |
| 279 | } |
| 280 | return out; |
| 281 | } |
| 282 | |
Siarhei Vishniakou | 72945a0 | 2023-09-18 18:30:25 -0700 | [diff] [blame] | 283 | std::ostream& operator<<(std::ostream& out, const TouchState& state) { |
| 284 | out << state.dump(); |
| 285 | return out; |
| 286 | } |
| 287 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 288 | } // namespace android::inputdispatcher |