blob: dcbb3665970c11d1a6238de44fccf85bda7a1bd3 [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 Vishniakou45504fe2023-05-05 16:05:10 -070034std::set<int32_t> TouchState::getActiveDeviceIds() const {
35 std::set<int32_t> out;
36 for (const TouchedWindow& w : windows) {
37 std::set<int32_t> deviceIds = w.getActiveDeviceIds();
38 out.insert(deviceIds.begin(), deviceIds.end());
39 }
40 return out;
41}
42
43bool TouchState::hasTouchingPointers(int32_t deviceId) const {
44 return std::any_of(windows.begin(), windows.end(), [&](const TouchedWindow& window) {
45 return window.hasTouchingPointers(deviceId);
46 });
47}
48
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070049void TouchState::removeTouchingPointer(int32_t removedDeviceId, int32_t pointerId) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000050 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070051 touchedWindow.removeTouchingPointer(removedDeviceId, pointerId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000052 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080053 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000054}
55
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070056void TouchState::removeTouchingPointerFromWindow(
57 int32_t removedDeviceId, int32_t pointerId,
58 const sp<android::gui::WindowInfoHandle>& windowHandle) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080059 for (TouchedWindow& touchedWindow : windows) {
60 if (touchedWindow.windowHandle == windowHandle) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070061 touchedWindow.removeTouchingPointer(removedDeviceId, pointerId);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080062 clearWindowsWithoutPointers();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080063 return;
64 }
65 }
66}
67
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000068void TouchState::clearHoveringPointers() {
69 for (TouchedWindow& touchedWindow : windows) {
70 touchedWindow.clearHoveringPointers();
71 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080072 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000073}
74
75void TouchState::clearWindowsWithoutPointers() {
76 std::erase_if(windows, [](const TouchedWindow& w) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070077 return !w.hasTouchingPointers() && !w.hasHoveringPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000078 });
79}
80
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080081void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle,
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080082 ftl::Flags<InputTarget::Flags> targetFlags,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070083 int32_t addedDeviceId,
84 std::bitset<MAX_POINTER_ID + 1> touchingPointerIds,
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -080085 std::optional<nsecs_t> firstDownTimeInTarget) {
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -080086 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000087 // We do not compare windows by token here because two windows that share the same token
88 // may have a different transform
Garfield Tane84e6f92019-08-29 17:28:41 -070089 if (touchedWindow.windowHandle == windowHandle) {
90 touchedWindow.targetFlags |= targetFlags;
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080091 if (targetFlags.test(InputTarget::Flags::DISPATCH_AS_SLIPPERY_EXIT)) {
92 touchedWindow.targetFlags.clear(InputTarget::Flags::DISPATCH_AS_IS);
Garfield Tane84e6f92019-08-29 17:28:41 -070093 }
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000094 // For cases like hover enter/exit or DISPATCH_AS_OUTSIDE a touch window might not have
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070095 // downTime set initially. Need to update existing window when a pointer is down for the
96 // window.
97 touchedWindow.addTouchingPointers(addedDeviceId, touchingPointerIds);
98 if (firstDownTimeInTarget) {
99 touchedWindow.trySetDownTimeInTarget(addedDeviceId, *firstDownTimeInTarget);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +0000100 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700101 return;
102 }
103 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700104 TouchedWindow touchedWindow;
105 touchedWindow.windowHandle = windowHandle;
106 touchedWindow.targetFlags = targetFlags;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700107 touchedWindow.addTouchingPointers(addedDeviceId, touchingPointerIds);
108 if (firstDownTimeInTarget) {
109 touchedWindow.trySetDownTimeInTarget(addedDeviceId, *firstDownTimeInTarget);
110 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700111 windows.push_back(touchedWindow);
112}
113
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000114void TouchState::addHoveringPointerToWindow(const sp<WindowInfoHandle>& windowHandle,
115 int32_t hoveringDeviceId, int32_t hoveringPointerId) {
116 for (TouchedWindow& touchedWindow : windows) {
117 if (touchedWindow.windowHandle == windowHandle) {
118 touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
119 return;
120 }
121 }
122
123 TouchedWindow touchedWindow;
124 touchedWindow.windowHandle = windowHandle;
125 touchedWindow.addHoveringPointer(hoveringDeviceId, hoveringPointerId);
126 windows.push_back(touchedWindow);
127}
128
Garfield Tane84e6f92019-08-29 17:28:41 -0700129void TouchState::removeWindowByToken(const sp<IBinder>& token) {
130 for (size_t i = 0; i < windows.size(); i++) {
131 if (windows[i].windowHandle->getToken() == token) {
132 windows.erase(windows.begin() + i);
133 return;
134 }
135 }
136}
137
Sam Dubeyf886dec2023-01-27 13:28:19 +0000138void TouchState::filterNonAsIsTouchWindows() {
139 for (size_t i = 0; i < windows.size();) {
140 TouchedWindow& window = windows[i];
141 if (window.targetFlags.any(InputTarget::Flags::DISPATCH_AS_IS |
142 InputTarget::Flags::DISPATCH_AS_SLIPPERY_ENTER)) {
143 window.targetFlags.clear(InputTarget::DISPATCH_MASK);
144 window.targetFlags |= InputTarget::Flags::DISPATCH_AS_IS;
145 i += 1;
146 } else {
147 windows.erase(windows.begin() + i);
148 }
149 }
150}
151
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700152void TouchState::cancelPointersForWindowsExcept(int32_t touchedDeviceId,
153 std::bitset<MAX_POINTER_ID + 1> pointerIds,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000154 const sp<IBinder>& token) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700155 std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000156 if (w.windowHandle->getToken() != token) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700157 w.removeTouchingPointers(touchedDeviceId, pointerIds);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000158 }
159 });
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800160 clearWindowsWithoutPointers();
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000161}
162
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800163/**
164 * For any pointer that's being pilfered, remove it from all of the other windows that currently
165 * aren't pilfering it. For example, if we determined that pointer 1 is going to both window A and
166 * window B, but window A is currently pilfering pointer 1, then pointer 1 should not go to window
167 * B.
168 */
169void TouchState::cancelPointersForNonPilferingWindows() {
170 // First, find all pointers that are being pilfered, across all windows
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700171 std::map<int32_t /*deviceId*/, std::bitset<MAX_POINTER_ID + 1>> allPilferedPointerIdsByDevice;
172 for (const TouchedWindow& w : windows) {
173 for (const auto& [iterDeviceId, pilferedPointerIds] : w.getPilferingPointers()) {
174 allPilferedPointerIdsByDevice[iterDeviceId] |= pilferedPointerIds;
175 }
176 };
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800177
178 // Optimization: most of the time, pilfering does not occur
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700179 if (allPilferedPointerIdsByDevice.empty()) return;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800180
181 // Now, remove all pointers from every window that's being pilfered by other windows.
182 // For example, if window A is pilfering pointer 1 (only), and window B is pilfering pointer 2
183 // (only), the remove pointer 2 from window A and pointer 1 from window B. Usually, the set of
184 // pilfered pointers will be disjoint across all windows, but there's no reason to cause that
185 // limitation here.
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700186 for (const auto& [iterDeviceId, allPilferedPointerIds] : allPilferedPointerIdsByDevice) {
187 std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) {
188 std::bitset<MAX_POINTER_ID + 1> pilferedByOtherWindows =
189 w.getPilferingPointers(iterDeviceId) ^ allPilferedPointerIds;
190 // Remove all pointers pilfered by other windows
191 w.removeTouchingPointers(iterDeviceId, pilferedByOtherWindows);
192 });
193 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800194 clearWindowsWithoutPointers();
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000195}
196
chaviw98318de2021-05-19 16:45:23 -0500197sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700198 for (size_t i = 0; i < windows.size(); i++) {
199 const TouchedWindow& window = windows[i];
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800200 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700201 return window.windowHandle;
202 }
203 }
204 return nullptr;
205}
206
207bool TouchState::isSlippery() const {
208 // Must have exactly one foreground window.
209 bool haveSlipperyForegroundWindow = false;
210 for (const TouchedWindow& window : windows) {
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800211 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700212 if (haveSlipperyForegroundWindow ||
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800213 !window.windowHandle->getInfo()->inputConfig.test(
214 WindowInfo::InputConfig::SLIPPERY)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700215 return false;
216 }
217 haveSlipperyForegroundWindow = true;
218 }
219 }
220 return haveSlipperyForegroundWindow;
221}
222
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000223sp<WindowInfoHandle> TouchState::getWallpaperWindow() const {
224 for (size_t i = 0; i < windows.size(); i++) {
225 const TouchedWindow& window = windows[i];
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800226 if (window.windowHandle->getInfo()->inputConfig.test(
227 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000228 return window.windowHandle;
229 }
230 }
231 return nullptr;
232}
233
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800234const TouchedWindow& TouchState::getTouchedWindow(const sp<WindowInfoHandle>& windowHandle) const {
235 auto it = std::find_if(windows.begin(), windows.end(),
236 [&](const TouchedWindow& w) { return w.windowHandle == windowHandle; });
237 LOG_ALWAYS_FATAL_IF(it == windows.end(), "Could not find %s", windowHandle->getName().c_str());
238 return *it;
239}
240
Siarhei Vishniakou3ad385b2022-11-04 10:09:53 -0700241bool TouchState::isDown() const {
242 return std::any_of(windows.begin(), windows.end(),
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700243 [](const TouchedWindow& window) { return window.hasTouchingPointers(); });
Siarhei Vishniakou3ad385b2022-11-04 10:09:53 -0700244}
245
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800246bool TouchState::hasHoveringPointers() const {
247 return std::any_of(windows.begin(), windows.end(),
248 [](const TouchedWindow& window) { return window.hasHoveringPointers(); });
249}
250
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000251std::set<sp<WindowInfoHandle>> TouchState::getWindowsWithHoveringPointer(int32_t hoveringDeviceId,
252 int32_t pointerId) const {
253 std::set<sp<WindowInfoHandle>> out;
254 for (const TouchedWindow& window : windows) {
255 if (window.hasHoveringPointer(hoveringDeviceId, pointerId)) {
256 out.insert(window.windowHandle);
257 }
258 }
259 return out;
260}
261
262void TouchState::removeHoveringPointer(int32_t hoveringDeviceId, int32_t hoveringPointerId) {
263 for (TouchedWindow& window : windows) {
264 window.removeHoveringPointer(hoveringDeviceId, hoveringPointerId);
265 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800266 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000267}
268
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700269void TouchState::removeAllPointersForDevice(int32_t removedDeviceId) {
270 for (TouchedWindow& window : windows) {
271 window.removeAllHoveringPointersForDevice(removedDeviceId);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700272 window.removeAllTouchingPointersForDevice(removedDeviceId);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700273 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700274
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700275 clearWindowsWithoutPointers();
276}
277
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -0800278std::string TouchState::dump() const {
279 std::string out;
Siarhei Vishniakou45504fe2023-05-05 16:05:10 -0700280 out += StringPrintf("source=%s\n", inputEventSourceToString(source).c_str());
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -0800281 if (!windows.empty()) {
282 out += " Windows:\n";
283 for (size_t i = 0; i < windows.size(); i++) {
284 const TouchedWindow& touchedWindow = windows[i];
285 out += StringPrintf(" %zu : ", i) + touchedWindow.dump();
286 }
287 } else {
288 out += " Windows: <none>\n";
289 }
290 return out;
291}
292
Garfield Tane84e6f92019-08-29 17:28:41 -0700293} // namespace android::inputdispatcher