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