blob: b63fe104fa726d28275e7bc8663e524c6872f392 [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
chaviw98318de2021-05-19 16:45:23 -050017#include <gui/WindowInfo.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070018
19#include "InputTarget.h"
20
21#include "TouchState.h"
22
chaviw98318de2021-05-19 16:45:23 -050023using android::gui::WindowInfo;
24using android::gui::WindowInfoHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -070025
26namespace android::inputdispatcher {
27
Garfield Tane84e6f92019-08-29 17:28:41 -070028void TouchState::reset() {
Prabir Pradhane680f9b2022-02-04 04:24:00 -080029 *this = TouchState();
Garfield Tane84e6f92019-08-29 17:28:41 -070030}
31
chaviw98318de2021-05-19 16:45:23 -050032void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle, int32_t targetFlags,
Garfield Tane84e6f92019-08-29 17:28:41 -070033 BitSet32 pointerIds) {
34 if (targetFlags & InputTarget::FLAG_SPLIT) {
35 split = true;
36 }
37
38 for (size_t i = 0; i < windows.size(); i++) {
39 TouchedWindow& touchedWindow = windows[i];
40 if (touchedWindow.windowHandle == windowHandle) {
41 touchedWindow.targetFlags |= targetFlags;
42 if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
43 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS;
44 }
45 touchedWindow.pointerIds.value |= pointerIds.value;
46 return;
47 }
48 }
49
Prabir Pradhane680f9b2022-02-04 04:24:00 -080050 if (preventNewTargets) return; // Don't add new TouchedWindows.
51
Garfield Tane84e6f92019-08-29 17:28:41 -070052 TouchedWindow touchedWindow;
53 touchedWindow.windowHandle = windowHandle;
54 touchedWindow.targetFlags = targetFlags;
55 touchedWindow.pointerIds = pointerIds;
56 windows.push_back(touchedWindow);
57}
58
Garfield Tane84e6f92019-08-29 17:28:41 -070059void TouchState::removeWindowByToken(const sp<IBinder>& token) {
60 for (size_t i = 0; i < windows.size(); i++) {
61 if (windows[i].windowHandle->getToken() == token) {
62 windows.erase(windows.begin() + i);
63 return;
64 }
65 }
66}
67
68void TouchState::filterNonAsIsTouchWindows() {
69 for (size_t i = 0; i < windows.size();) {
70 TouchedWindow& window = windows[i];
71 if (window.targetFlags &
72 (InputTarget::FLAG_DISPATCH_AS_IS | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) {
73 window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK;
74 window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS;
75 i += 1;
76 } else {
77 windows.erase(windows.begin() + i);
78 }
79 }
80}
81
Prabir Pradhan07e05b62021-11-19 03:57:24 -080082void TouchState::filterWindowsExcept(const sp<IBinder>& token) {
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -080083 std::erase_if(windows,
84 [&token](const TouchedWindow& w) { return w.windowHandle->getToken() != token; });
Garfield Tane84e6f92019-08-29 17:28:41 -070085}
86
chaviw98318de2021-05-19 16:45:23 -050087sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const {
Garfield Tane84e6f92019-08-29 17:28:41 -070088 for (size_t i = 0; i < windows.size(); i++) {
89 const TouchedWindow& window = windows[i];
90 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
91 return window.windowHandle;
92 }
93 }
94 return nullptr;
95}
96
97bool TouchState::isSlippery() const {
98 // Must have exactly one foreground window.
99 bool haveSlipperyForegroundWindow = false;
100 for (const TouchedWindow& window : windows) {
101 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
102 if (haveSlipperyForegroundWindow ||
chaviw98318de2021-05-19 16:45:23 -0500103 !window.windowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700104 return false;
105 }
106 haveSlipperyForegroundWindow = true;
107 }
108 }
109 return haveSlipperyForegroundWindow;
110}
111
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000112sp<WindowInfoHandle> TouchState::getWallpaperWindow() const {
113 for (size_t i = 0; i < windows.size(); i++) {
114 const TouchedWindow& window = windows[i];
115 if (window.windowHandle->getInfo()->type == WindowInfo::Type::WALLPAPER) {
116 return window.windowHandle;
117 }
118 }
119 return nullptr;
120}
121
Prabir Pradhan07e05b62021-11-19 03:57:24 -0800122sp<WindowInfoHandle> TouchState::getWindow(const sp<IBinder>& token) const {
123 for (const TouchedWindow& touchedWindow : windows) {
124 const auto& windowHandle = touchedWindow.windowHandle;
125 if (windowHandle->getToken() == token) {
126 return windowHandle;
127 }
128 }
129 return nullptr;
130}
131
Garfield Tane84e6f92019-08-29 17:28:41 -0700132} // namespace android::inputdispatcher