blob: 20b6eadf5bd74abc456975546aa6bec2d0c09e50 [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
chaviw3277faf2021-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
chaviw3277faf2021-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
28TouchState::TouchState()
29 : down(false), split(false), deviceId(-1), source(0), displayId(ADISPLAY_ID_NONE) {}
30
31TouchState::~TouchState() {}
32
33void TouchState::reset() {
34 down = false;
35 split = false;
36 deviceId = -1;
37 source = 0;
38 displayId = ADISPLAY_ID_NONE;
39 windows.clear();
40 portalWindows.clear();
41 gestureMonitors.clear();
42}
43
44void TouchState::copyFrom(const TouchState& other) {
45 down = other.down;
46 split = other.split;
47 deviceId = other.deviceId;
48 source = other.source;
49 displayId = other.displayId;
50 windows = other.windows;
51 portalWindows = other.portalWindows;
52 gestureMonitors = other.gestureMonitors;
53}
54
chaviw3277faf2021-05-19 16:45:23 -050055void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle, int32_t targetFlags,
Garfield Tane84e6f92019-08-29 17:28:41 -070056 BitSet32 pointerIds) {
57 if (targetFlags & InputTarget::FLAG_SPLIT) {
58 split = true;
59 }
60
61 for (size_t i = 0; i < windows.size(); i++) {
62 TouchedWindow& touchedWindow = windows[i];
63 if (touchedWindow.windowHandle == windowHandle) {
64 touchedWindow.targetFlags |= targetFlags;
65 if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
66 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS;
67 }
68 touchedWindow.pointerIds.value |= pointerIds.value;
69 return;
70 }
71 }
72
73 TouchedWindow touchedWindow;
74 touchedWindow.windowHandle = windowHandle;
75 touchedWindow.targetFlags = targetFlags;
76 touchedWindow.pointerIds = pointerIds;
77 windows.push_back(touchedWindow);
78}
79
chaviw3277faf2021-05-19 16:45:23 -050080void TouchState::addPortalWindow(const sp<android::gui::WindowInfoHandle>& windowHandle) {
Garfield Tane84e6f92019-08-29 17:28:41 -070081 size_t numWindows = portalWindows.size();
82 for (size_t i = 0; i < numWindows; i++) {
83 if (portalWindows[i] == windowHandle) {
84 return;
85 }
86 }
87 portalWindows.push_back(windowHandle);
88}
89
90void TouchState::addGestureMonitors(const std::vector<TouchedMonitor>& newMonitors) {
91 const size_t newSize = gestureMonitors.size() + newMonitors.size();
92 gestureMonitors.reserve(newSize);
93 gestureMonitors.insert(std::end(gestureMonitors), std::begin(newMonitors),
94 std::end(newMonitors));
95}
96
Garfield Tane84e6f92019-08-29 17:28:41 -070097void TouchState::removeWindowByToken(const sp<IBinder>& token) {
98 for (size_t i = 0; i < windows.size(); i++) {
99 if (windows[i].windowHandle->getToken() == token) {
100 windows.erase(windows.begin() + i);
101 return;
102 }
103 }
104}
105
106void TouchState::filterNonAsIsTouchWindows() {
107 for (size_t i = 0; i < windows.size();) {
108 TouchedWindow& window = windows[i];
109 if (window.targetFlags &
110 (InputTarget::FLAG_DISPATCH_AS_IS | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) {
111 window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK;
112 window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS;
113 i += 1;
114 } else {
115 windows.erase(windows.begin() + i);
116 }
117 }
118}
119
120void TouchState::filterNonMonitors() {
121 windows.clear();
122 portalWindows.clear();
123}
124
chaviw3277faf2021-05-19 16:45:23 -0500125sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const {
Garfield Tane84e6f92019-08-29 17:28:41 -0700126 for (size_t i = 0; i < windows.size(); i++) {
127 const TouchedWindow& window = windows[i];
128 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
129 return window.windowHandle;
130 }
131 }
132 return nullptr;
133}
134
135bool TouchState::isSlippery() const {
136 // Must have exactly one foreground window.
137 bool haveSlipperyForegroundWindow = false;
138 for (const TouchedWindow& window : windows) {
139 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
140 if (haveSlipperyForegroundWindow ||
chaviw3277faf2021-05-19 16:45:23 -0500141 !window.windowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700142 return false;
143 }
144 haveSlipperyForegroundWindow = true;
145 }
146 }
147 return haveSlipperyForegroundWindow;
148}
149
150} // namespace android::inputdispatcher