blob: 2bf63beb0526bf7ce1d77617cee4728a448baf7b [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 Vishniakou2899c552023-07-10 18:20:46 -070017#include <android-base/logging.h>
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080018#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050019#include <gui/WindowInfo.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070020
21#include "InputTarget.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070022#include "TouchState.h"
23
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080024using namespace android::ftl::flag_operators;
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080025using android::base::StringPrintf;
chaviw98318de2021-05-19 16:45:23 -050026using android::gui::WindowInfo;
27using android::gui::WindowInfoHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -070028
29namespace android::inputdispatcher {
30
Garfield Tane84e6f92019-08-29 17:28:41 -070031void TouchState::reset() {
Prabir Pradhane680f9b2022-02-04 04:24:00 -080032 *this = TouchState();
Garfield Tane84e6f92019-08-29 17:28:41 -070033}
34
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070035bool TouchState::hasTouchingPointers(DeviceId deviceId) const {
Siarhei Vishniakou45504fe2023-05-05 16:05:10 -070036 return std::any_of(windows.begin(), windows.end(), [&](const TouchedWindow& window) {
37 return window.hasTouchingPointers(deviceId);
38 });
39}
40
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070041void TouchState::removeTouchingPointer(DeviceId deviceId, int32_t pointerId) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000042 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070043 touchedWindow.removeTouchingPointer(deviceId, pointerId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000044 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080045 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000046}
47
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070048void TouchState::removeTouchingPointerFromWindow(
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070049 DeviceId deviceId, int32_t pointerId,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070050 const sp<android::gui::WindowInfoHandle>& windowHandle) {
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080051 for (TouchedWindow& touchedWindow : windows) {
52 if (touchedWindow.windowHandle == windowHandle) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070053 touchedWindow.removeTouchingPointer(deviceId, pointerId);
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080054 clearWindowsWithoutPointers();
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -080055 return;
56 }
57 }
58}
59
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070060void TouchState::clearHoveringPointers(DeviceId deviceId) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000061 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070062 touchedWindow.removeAllHoveringPointersForDevice(deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000063 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -080064 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000065}
66
67void TouchState::clearWindowsWithoutPointers() {
68 std::erase_if(windows, [](const TouchedWindow& w) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070069 return !w.hasTouchingPointers() && !w.hasHoveringPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000070 });
71}
72
Siarhei Vishniakoue535f972024-03-29 14:55:21 -070073android::base::Result<void> TouchState::addOrUpdateWindow(
74 const sp<WindowInfoHandle>& windowHandle, InputTarget::DispatchMode dispatchMode,
75 ftl::Flags<InputTarget::Flags> targetFlags, DeviceId deviceId,
76 const std::vector<PointerProperties>& touchingPointers,
77 std::optional<nsecs_t> firstDownTimeInTarget) {
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -080078 if (touchingPointers.empty()) {
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070079 LOG(FATAL) << __func__ << "No pointers specified for " << windowHandle->getName();
Siarhei Vishniakoue535f972024-03-29 14:55:21 -070080 return android::base::Error();
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070081 }
Siarhei Vishniakou5cee1e32022-11-29 12:35:39 -080082 for (TouchedWindow& touchedWindow : windows) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000083 // We do not compare windows by token here because two windows that share the same token
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070084 // 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 Tane84e6f92019-08-29 17:28:41 -070088 if (touchedWindow.windowHandle == windowHandle) {
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +000089 touchedWindow.dispatchMode = dispatchMode;
Garfield Tane84e6f92019-08-29 17:28:41 -070090 touchedWindow.targetFlags |= targetFlags;
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000091 // For cases like hover enter/exit or DISPATCH_AS_OUTSIDE a touch window might not have
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070092 // downTime set initially. Need to update existing window when a pointer is down for the
93 // window.
Siarhei Vishniakoue535f972024-03-29 14:55:21 -070094 android::base::Result<void> addResult =
95 touchedWindow.addTouchingPointers(deviceId, touchingPointers);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070096 if (firstDownTimeInTarget) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070097 touchedWindow.trySetDownTimeInTarget(deviceId, *firstDownTimeInTarget);
Vaibhav Devmurari882bd9b2022-06-23 14:54:54 +000098 }
Siarhei Vishniakoue535f972024-03-29 14:55:21 -070099 return addResult;
Garfield Tane84e6f92019-08-29 17:28:41 -0700100 }
101 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700102 TouchedWindow touchedWindow;
103 touchedWindow.windowHandle = windowHandle;
Prabir Pradhan4b09c1f2023-11-17 03:16:25 +0000104 touchedWindow.dispatchMode = dispatchMode;
Garfield Tane84e6f92019-08-29 17:28:41 -0700105 touchedWindow.targetFlags = targetFlags;
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800106 touchedWindow.addTouchingPointers(deviceId, touchingPointers);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700107 if (firstDownTimeInTarget) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700108 touchedWindow.trySetDownTimeInTarget(deviceId, *firstDownTimeInTarget);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700109 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700110 windows.push_back(touchedWindow);
Siarhei Vishniakoue535f972024-03-29 14:55:21 -0700111 return {};
Garfield Tane84e6f92019-08-29 17:28:41 -0700112}
113
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000114void TouchState::addHoveringPointerToWindow(const sp<WindowInfoHandle>& windowHandle,
Siarhei Vishniakoub711e652024-08-12 12:00:28 -0700115 DeviceId deviceId, const PointerProperties& pointer,
116 float x, float y) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000117 for (TouchedWindow& touchedWindow : windows) {
118 if (touchedWindow.windowHandle == windowHandle) {
Siarhei Vishniakoub711e652024-08-12 12:00:28 -0700119 touchedWindow.addHoveringPointer(deviceId, pointer, x, y);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000120 return;
121 }
122 }
123
124 TouchedWindow touchedWindow;
125 touchedWindow.windowHandle = windowHandle;
Siarhei Vishniakoub711e652024-08-12 12:00:28 -0700126 touchedWindow.addHoveringPointer(deviceId, pointer, x, y);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000127 windows.push_back(touchedWindow);
128}
129
Garfield Tane84e6f92019-08-29 17:28:41 -0700130void TouchState::removeWindowByToken(const sp<IBinder>& token) {
131 for (size_t i = 0; i < windows.size(); i++) {
132 if (windows[i].windowHandle->getToken() == token) {
133 windows.erase(windows.begin() + i);
134 return;
135 }
136 }
137}
138
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700139void TouchState::cancelPointersForWindowsExcept(DeviceId deviceId,
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700140 std::bitset<MAX_POINTER_ID + 1> pointerIds,
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000141 const sp<IBinder>& token) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700142 std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) {
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000143 if (w.windowHandle->getToken() != token) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700144 w.removeTouchingPointers(deviceId, pointerIds);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000145 }
146 });
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800147 clearWindowsWithoutPointers();
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000148}
149
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800150/**
151 * For any pointer that's being pilfered, remove it from all of the other windows that currently
152 * aren't pilfering it. For example, if we determined that pointer 1 is going to both window A and
153 * window B, but window A is currently pilfering pointer 1, then pointer 1 should not go to window
154 * B.
155 */
156void TouchState::cancelPointersForNonPilferingWindows() {
157 // First, find all pointers that are being pilfered, across all windows
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700158 std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> allPilferedPointerIdsByDevice;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700159 for (const TouchedWindow& w : windows) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700160 for (const auto& [deviceId, pilferedPointerIds] : w.getPilferingPointers()) {
161 allPilferedPointerIdsByDevice[deviceId] |= pilferedPointerIds;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700162 }
163 };
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800164
165 // Optimization: most of the time, pilfering does not occur
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700166 if (allPilferedPointerIdsByDevice.empty()) return;
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -0800167
168 // Now, remove all pointers from every window that's being pilfered by other windows.
169 // For example, if window A is pilfering pointer 1 (only), and window B is pilfering pointer 2
170 // (only), the remove pointer 2 from window A and pointer 1 from window B. Usually, the set of
171 // pilfered pointers will be disjoint across all windows, but there's no reason to cause that
172 // limitation here.
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700173 for (const auto& [deviceId, allPilferedPointerIds] : allPilferedPointerIdsByDevice) {
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700174 std::for_each(windows.begin(), windows.end(), [&](TouchedWindow& w) {
175 std::bitset<MAX_POINTER_ID + 1> pilferedByOtherWindows =
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700176 w.getPilferingPointers(deviceId) ^ allPilferedPointerIds;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700177 // Remove all pointers pilfered by other windows
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700178 w.removeTouchingPointers(deviceId, pilferedByOtherWindows);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700179 });
180 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800181 clearWindowsWithoutPointers();
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000182}
183
Linnan Liccf6ce32024-04-11 20:32:13 +0800184sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle(DeviceId deviceId) const {
185 for (const auto& window : windows) {
186 if (!window.hasTouchingPointers(deviceId)) {
187 continue;
188 }
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800189 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700190 return window.windowHandle;
191 }
192 }
193 return nullptr;
194}
195
Linnan Li49b2b202024-04-12 12:46:40 +0800196bool TouchState::isSlippery(DeviceId deviceId) const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700197 // Must have exactly one foreground window.
198 bool haveSlipperyForegroundWindow = false;
199 for (const TouchedWindow& window : windows) {
Linnan Li49b2b202024-04-12 12:46:40 +0800200 if (!window.hasTouchingPointers(deviceId)) {
201 continue;
202 }
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800203 if (window.targetFlags.test(InputTarget::Flags::FOREGROUND)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700204 if (haveSlipperyForegroundWindow ||
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800205 !window.windowHandle->getInfo()->inputConfig.test(
206 WindowInfo::InputConfig::SLIPPERY)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700207 return false;
208 }
209 haveSlipperyForegroundWindow = true;
210 }
211 }
212 return haveSlipperyForegroundWindow;
213}
214
Linnan Li72352222024-04-12 18:55:57 +0800215sp<WindowInfoHandle> TouchState::getWallpaperWindow(DeviceId deviceId) const {
216 for (const auto& window : windows) {
217 if (!window.hasTouchingPointers(deviceId)) {
218 continue;
219 }
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800220 if (window.windowHandle->getInfo()->inputConfig.test(
221 gui::WindowInfo::InputConfig::IS_WALLPAPER)) {
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000222 return window.windowHandle;
223 }
224 }
225 return nullptr;
226}
227
Siarhei Vishniakou0026b4c2022-11-10 19:33:29 -0800228const TouchedWindow& TouchState::getTouchedWindow(const sp<WindowInfoHandle>& windowHandle) const {
229 auto it = std::find_if(windows.begin(), windows.end(),
230 [&](const TouchedWindow& w) { return w.windowHandle == windowHandle; });
231 LOG_ALWAYS_FATAL_IF(it == windows.end(), "Could not find %s", windowHandle->getName().c_str());
232 return *it;
233}
234
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700235bool TouchState::isDown(DeviceId deviceId) const {
236 return std::any_of(windows.begin(), windows.end(), [&deviceId](const TouchedWindow& window) {
237 return window.hasTouchingPointers(deviceId);
238 });
Siarhei Vishniakou3ad385b2022-11-04 10:09:53 -0700239}
240
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700241bool TouchState::hasHoveringPointers(DeviceId deviceId) const {
242 return std::any_of(windows.begin(), windows.end(), [&deviceId](const TouchedWindow& window) {
243 return window.hasHoveringPointers(deviceId);
244 });
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800245}
246
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700247bool TouchState::hasActiveStylus() const {
248 return std::any_of(windows.begin(), windows.end(),
249 [](const TouchedWindow& window) { return window.hasActiveStylus(); });
250}
251
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700252std::set<sp<WindowInfoHandle>> TouchState::getWindowsWithHoveringPointer(DeviceId deviceId,
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000253 int32_t pointerId) const {
254 std::set<sp<WindowInfoHandle>> out;
255 for (const TouchedWindow& window : windows) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700256 if (window.hasHoveringPointer(deviceId, pointerId)) {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000257 out.insert(window.windowHandle);
258 }
259 }
260 return out;
261}
262
263void TouchState::removeHoveringPointer(int32_t hoveringDeviceId, int32_t hoveringPointerId) {
264 for (TouchedWindow& window : windows) {
265 window.removeHoveringPointer(hoveringDeviceId, hoveringPointerId);
266 }
Siarhei Vishniakouf372b812023-02-14 18:06:51 -0800267 clearWindowsWithoutPointers();
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +0000268}
269
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700270void TouchState::removeAllPointersForDevice(DeviceId deviceId) {
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700271 for (TouchedWindow& window : windows) {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -0700272 window.removeAllHoveringPointersForDevice(deviceId);
273 window.removeAllTouchingPointersForDevice(deviceId);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700274 }
Siarhei Vishniakou0836a302023-05-03 13:54:30 -0700275
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -0700276 clearWindowsWithoutPointers();
277}
278
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -0800279std::string TouchState::dump() const {
280 std::string out;
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
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700293std::ostream& operator<<(std::ostream& out, const TouchState& state) {
294 out << state.dump();
295 return out;
296}
297
Garfield Tane84e6f92019-08-29 17:28:41 -0700298} // namespace android::inputdispatcher