blob: 425847183efda4b3ec55bccfc32a54533b565be9 [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
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 Vishniakou6e1e9872022-11-08 17:51:35 -080017#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050018#include <gui/WindowInfo.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070019
20#include "InputTarget.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070021#include "TouchState.h"
22
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080023using namespace android::ftl::flag_operators;
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080024using android::base::StringPrintf;
chaviw98318de2021-05-19 16:45:23 -050025using android::gui::WindowInfo;
26using android::gui::WindowInfoHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -070027
28namespace android::inputdispatcher {
29
Garfield Tane84e6f92019-08-29 17:28:41 -070030void TouchState::reset() {
Prabir Pradhane680f9b2022-02-04 04:24:00 -080031 *this = TouchState();
Garfield Tane84e6f92019-08-29 17:28:41 -070032}
33
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000034void TouchState::removeTouchedPointer(int32_t pointerId) {
35 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -080036 touchedWindow.removeTouchingPointer(pointerId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000037 }
38}
39
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080040void TouchState::removeTouchedPointerFromWindow(
41 int32_t pointerId, const sp<android::gui::WindowInfoHandle>& windowHandle) {
42 for (TouchedWindow& touchedWindow : windows) {
43 if (touchedWindow.windowHandle == windowHandle) {
Siarhei Vishniakou6464e462023-02-06 18:57:59 -080044 touchedWindow.removeTouchingPointer(pointerId);
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080045 return;
46 }
47 }
48}
49
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000050void TouchState::clearHoveringPointers() {
51 for (TouchedWindow& touchedWindow : windows) {
52 touchedWindow.clearHoveringPointers();
53 }
54}
55
56void TouchState::clearWindowsWithoutPointers() {
57 std::erase_if(windows, [](const TouchedWindow& w) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080058 return w.pointerIds.none() && !w.hasHoveringPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000059 });
60}
61
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080062void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle,
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080063 ftl::Flags<InputTarget::Flags> targetFlags,
64 std::bitset<MAX_POINTER_ID + 1> pointerIds,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -080065 std::optional<nsecs_t> firstDownTimeInTarget) {
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -080066 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000067 // We do not compare windows by token here because two windows that share the same token
68 // may have a different transform
Garfield Tane84e6f92019-08-29 17:28:41 -070069 if (touchedWindow.windowHandle == windowHandle) {
70 touchedWindow.targetFlags |= targetFlags;
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080071 if (targetFlags.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) {
72 touchedWindow.targetFlags.clear(InputTarget::Flags::DISPATCH_AS_IS);
Garfield Tane84e6f92019-08-29 17:28:41 -070073 }
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000074 // For cases like hover enter/exit or DISPATCH_AS_OUTSIDE a touch window might not have
75 // downTime set initially. Need to update existing window when an pointer is down for
76 // the window.
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080077 touchedWindow.pointerIds |= pointerIds;
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000078 if (!touchedWindow.firstDownTimeInTarget.has_value()) {
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -080079 touchedWindow.firstDownTimeInTarget = firstDownTimeInTarget;
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000080 }
Garfield Tane84e6f92019-08-29 17:28:41 -070081 return;
82 }
83 }
Garfield Tane84e6f92019-08-29 17:28:41 -070084 TouchedWindow touchedWindow;
85 touchedWindow.windowHandle = windowHandle;
86 touchedWindow.targetFlags = targetFlags;
87 touchedWindow.pointerIds = pointerIds;
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -080088 touchedWindow.firstDownTimeInTarget = firstDownTimeInTarget;
Garfield Tane84e6f92019-08-29 17:28:41 -070089 windows.push_back(touchedWindow);
90}
91
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000092void TouchState::addHoveringPointerToWindow(const sp<WindowInfoHandle>& windowHandle,
93 int32_t hoveringDeviceId, int32_t hoveringPointerId) {
94 for (TouchedWindow& touchedWindow : windows) {
95 if (touchedWindow.windowHandle == windowHandle) {
96 touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
97 return;
98 }
99 }
100
101 TouchedWindow touchedWindow;
102 touchedWindow.windowHandle = windowHandle;
103 touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
104 windows.push_back(touchedWindow);
105}
106
Garfield Tane84e6f92019-08-29 17:28:41 -0700107void TouchState::removeWindowByToken(const sp<IBinder>& token) {
108 for (size_t i = 0; i < windows.size(); i++) {
109 if (windows[i].windowHandle->getToken() == token) {
110 windows.erase(windows.begin() + i);
111 return;
112 }
113 }
114}
115
Sam Dubeyf886dec2023-01-27 13:28:19 +0000116void TouchState::filterNonAsIsTouchWindows() {
117 for (size_t i = 0; i < windows.size();) {
118 TouchedWindow& window = windows[i];
119 if (window.targetFlags.any(InputTarget::Flags::DISPATCH_AS_IS |
120 InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) {
121 window.targetFlags.clear(InputTarget::DISPATCH_MASK);
122 window.targetFlags |= InputTarget::Flags::DISPATCH_AS_IS;
123 i += 1;
124 } else {
125 windows.erase(windows.begin() + i);
126 }
127 }
128}
129
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800130void TouchState::cancelPointersForWindowsExcept(std::bitset<MAX_POINTER_ID + 1> pointerIds,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000131 const sp<IBinder>& token) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800132 if (pointerIds.none()) return;
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000133 std::for_each(windows.begin(), windows.end(), [&pointerIds, &token](TouchedWindow& w) {
134 if (w.windowHandle->getToken() != token) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800135 w.pointerIds &= ~pointerIds;
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000136 }
137 });
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800138 std::erase_if(windows, [](const TouchedWindow& w) { return w.pointerIds.none(); });
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000139}
140
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800141/**
142 * For any pointer that's being pilfered, remove it from all of the other windows that currently
143 * aren't pilfering it. For example, if we determined that pointer 1 is going to both window A and
144 * window B, but window A is currently pilfering pointer 1, then pointer 1 should not go to window
145 * B.
146 */
147void TouchState::cancelPointersForNonPilferingWindows() {
148 // First, find all pointers that are being pilfered, across all windows
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800149 std::bitset<MAX_POINTER_ID + 1> allPilferedPointerIds;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800150 std::for_each(windows.begin(), windows.end(), [&allPilferedPointerIds](const TouchedWindow& w) {
151 allPilferedPointerIds |= w.pilferedPointerIds;
152 });
153
154 // Optimization: most of the time, pilfering does not occur
155 if (allPilferedPointerIds.none()) return;
156
157 // Now, remove all pointers from every window that's being pilfered by other windows.
158 // For example, if window A is pilfering pointer 1 (only), and window B is pilfering pointer 2
159 // (only), the remove pointer 2 from window A and pointer 1 from window B. Usually, the set of
160 // pilfered pointers will be disjoint across all windows, but there's no reason to cause that
161 // limitation here.
162 std::for_each(windows.begin(), windows.end(), [&allPilferedPointerIds](TouchedWindow& w) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800163 std::bitset<MAX_POINTER_ID + 1> pilferedByOtherWindows =
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800164 w.pilferedPointerIds ^ allPilferedPointerIds;
Siarhei Vishniakou9af4e112023-02-06 10:49:07 -0800165 w.pointerIds &= ~pilferedByOtherWindows;
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000166 });
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800167 std::erase_if(windows, [](const TouchedWindow& w) { return w.pointerIds.none(); });
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000168}
169
chaviw98318de2021-05-19 16:45:23 -0500170sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700171 for (size_t i = 0; i < windows.size(); i++) {
172 const TouchedWindow& window = windows[i];
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800173 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700174 return window.windowHandle;
175 }
176 }
177 return nullptr;
178}
179
180bool TouchState::isSlippery() const {
181 // Must have exactly one foreground window.
182 bool haveSlipperyForegroundWindow = false;
183 for (const TouchedWindow& window : windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800184 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700185 if (haveSlipperyForegroundWindow ||
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800186 !window.windowHandle->getInfo()->inputConfig.test(
187 WindowInfo::InputConfig::SLIPPERY)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700188 return false;
189 }
190 haveSlipperyForegroundWindow = true;
191 }
192 }
193 return haveSlipperyForegroundWindow;
194}
195
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000196sp<WindowInfoHandle> TouchState::getWallpaperWindow() const {
197 for (size_t i = 0; i < windows.size(); i++) {
198 const TouchedWindow& window = windows[i];
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800199 if (window.windowHandle->getInfo()->inputConfig.test(
200 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000201 return window.windowHandle;
202 }
203 }
204 return nullptr;
205}
206
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800207const TouchedWindow& TouchState::getTouchedWindow(const sp<WindowInfoHandle>& windowHandle) const {
208 auto it = std::find_if(windows.begin(), windows.end(),
209 [&](const TouchedWindow& w) { return w.windowHandle == windowHandle; });
210 LOG_ALWAYS_FATAL_IF(it == windows.end(), "Could not find %s", windowHandle->getName().c_str());
211 return *it;
212}
213
Siarhei Vishniakou3ad385b2022-11-04 10:09:53 -0700214bool TouchState::isDown() const {
215 return std::any_of(windows.begin(), windows.end(),
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800216 [](const TouchedWindow& window) { return window.pointerIds.any(); });
Siarhei Vishniakou3ad385b2022-11-04 10:09:53 -0700217}
218
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000219std::set<sp<WindowInfoHandle>> TouchState::getWindowsWithHoveringPointer(int32_t hoveringDeviceId,
220 int32_t pointerId) const {
221 std::set<sp<WindowInfoHandle>> out;
222 for (const TouchedWindow& window : windows) {
223 if (window.hasHoveringPointer(hoveringDeviceId, pointerId)) {
224 out.insert(window.windowHandle);
225 }
226 }
227 return out;
228}
229
230void TouchState::removeHoveringPointer(int32_t hoveringDeviceId, int32_t hoveringPointerId) {
231 for (TouchedWindow& window : windows) {
232 window.removeHoveringPointer(hoveringDeviceId, hoveringPointerId);
233 }
234 std::erase_if(windows, [](const TouchedWindow& w) {
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800235 return w.pointerIds.none() && !w.hasHoveringPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000236 });
237}
238
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -0800239std::string TouchState::dump() const {
240 std::string out;
Siarhei Vishniakou0b0374d2022-11-17 17:40:53 -0800241 out += StringPrintf("deviceId=%d, source=%s\n", deviceId,
242 inputEventSourceToString(source).c_str());
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -0800243 if (!windows.empty()) {
244 out += " Windows:\n";
245 for (size_t i = 0; i < windows.size(); i++) {
246 const TouchedWindow& touchedWindow = windows[i];
247 out += StringPrintf(" %zu : ", i) + touchedWindow.dump();
248 }
249 } else {
250 out += " Windows: <none>\n";
251 }
252 return out;
253}
254
Garfield Tane84e6f92019-08-29 17:28:41 -0700255} // namespace android::inputdispatcher