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