1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "TouchedWindow.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <input/PrintTools.h>
22*38e8c45fSAndroid Build Coastguard Worker
23*38e8c45fSAndroid Build Coastguard Worker using android::base::Result;
24*38e8c45fSAndroid Build Coastguard Worker using android::base::StringPrintf;
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker namespace inputdispatcher {
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker namespace {
31*38e8c45fSAndroid Build Coastguard Worker
hasPointerId(const std::vector<PointerProperties> & pointers,int32_t pointerId)32*38e8c45fSAndroid Build Coastguard Worker bool hasPointerId(const std::vector<PointerProperties>& pointers, int32_t pointerId) {
33*38e8c45fSAndroid Build Coastguard Worker return std::find_if(pointers.begin(), pointers.end(),
34*38e8c45fSAndroid Build Coastguard Worker [&pointerId](const PointerProperties& properties) {
35*38e8c45fSAndroid Build Coastguard Worker return properties.id == pointerId;
36*38e8c45fSAndroid Build Coastguard Worker }) != pointers.end();
37*38e8c45fSAndroid Build Coastguard Worker }
38*38e8c45fSAndroid Build Coastguard Worker
hasPointerId(const std::vector<TouchedWindow::HoveringPointer> & pointers,int32_t pointerId)39*38e8c45fSAndroid Build Coastguard Worker bool hasPointerId(const std::vector<TouchedWindow::HoveringPointer>& pointers, int32_t pointerId) {
40*38e8c45fSAndroid Build Coastguard Worker return std::find_if(pointers.begin(), pointers.end(),
41*38e8c45fSAndroid Build Coastguard Worker [&pointerId](const TouchedWindow::HoveringPointer& pointer) {
42*38e8c45fSAndroid Build Coastguard Worker return pointer.properties.id == pointerId;
43*38e8c45fSAndroid Build Coastguard Worker }) != pointers.end();
44*38e8c45fSAndroid Build Coastguard Worker }
45*38e8c45fSAndroid Build Coastguard Worker
46*38e8c45fSAndroid Build Coastguard Worker } // namespace
47*38e8c45fSAndroid Build Coastguard Worker
hasHoveringPointers() const48*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasHoveringPointers() const {
49*38e8c45fSAndroid Build Coastguard Worker for (const auto& [_, state] : mDeviceStates) {
50*38e8c45fSAndroid Build Coastguard Worker if (!state.hoveringPointers.empty()) {
51*38e8c45fSAndroid Build Coastguard Worker return true;
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker }
54*38e8c45fSAndroid Build Coastguard Worker return false;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker
hasHoveringPointers(DeviceId deviceId) const57*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasHoveringPointers(DeviceId deviceId) const {
58*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
59*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
60*38e8c45fSAndroid Build Coastguard Worker return false;
61*38e8c45fSAndroid Build Coastguard Worker }
62*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
63*38e8c45fSAndroid Build Coastguard Worker
64*38e8c45fSAndroid Build Coastguard Worker return !state.hoveringPointers.empty();
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker
clearHoveringPointers(DeviceId deviceId)67*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::clearHoveringPointers(DeviceId deviceId) {
68*38e8c45fSAndroid Build Coastguard Worker auto stateIt = mDeviceStates.find(deviceId);
69*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
70*38e8c45fSAndroid Build Coastguard Worker return;
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
73*38e8c45fSAndroid Build Coastguard Worker state.hoveringPointers.clear();
74*38e8c45fSAndroid Build Coastguard Worker if (!state.hasPointers()) {
75*38e8c45fSAndroid Build Coastguard Worker mDeviceStates.erase(stateIt);
76*38e8c45fSAndroid Build Coastguard Worker }
77*38e8c45fSAndroid Build Coastguard Worker }
78*38e8c45fSAndroid Build Coastguard Worker
hasHoveringPointer(DeviceId deviceId,int32_t pointerId) const79*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const {
80*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
81*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
82*38e8c45fSAndroid Build Coastguard Worker return false;
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
85*38e8c45fSAndroid Build Coastguard Worker return hasPointerId(state.hoveringPointers, pointerId);
86*38e8c45fSAndroid Build Coastguard Worker }
87*38e8c45fSAndroid Build Coastguard Worker
addHoveringPointer(DeviceId deviceId,const PointerProperties & properties,float x,float y)88*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::addHoveringPointer(DeviceId deviceId, const PointerProperties& properties,
89*38e8c45fSAndroid Build Coastguard Worker float x, float y) {
90*38e8c45fSAndroid Build Coastguard Worker std::vector<HoveringPointer>& hoveringPointers = mDeviceStates[deviceId].hoveringPointers;
91*38e8c45fSAndroid Build Coastguard Worker const size_t initialSize = hoveringPointers.size();
92*38e8c45fSAndroid Build Coastguard Worker std::erase_if(hoveringPointers, [&properties](const HoveringPointer& pointer) {
93*38e8c45fSAndroid Build Coastguard Worker return pointer.properties.id == properties.id;
94*38e8c45fSAndroid Build Coastguard Worker });
95*38e8c45fSAndroid Build Coastguard Worker if (hoveringPointers.size() != initialSize) {
96*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << __func__ << ": " << properties << ", device " << deviceId << " was in "
97*38e8c45fSAndroid Build Coastguard Worker << *this;
98*38e8c45fSAndroid Build Coastguard Worker }
99*38e8c45fSAndroid Build Coastguard Worker hoveringPointers.push_back({properties, x, y});
100*38e8c45fSAndroid Build Coastguard Worker }
101*38e8c45fSAndroid Build Coastguard Worker
addTouchingPointers(DeviceId deviceId,const std::vector<PointerProperties> & pointers)102*38e8c45fSAndroid Build Coastguard Worker Result<void> TouchedWindow::addTouchingPointers(DeviceId deviceId,
103*38e8c45fSAndroid Build Coastguard Worker const std::vector<PointerProperties>& pointers) {
104*38e8c45fSAndroid Build Coastguard Worker std::vector<PointerProperties>& touchingPointers = mDeviceStates[deviceId].touchingPointers;
105*38e8c45fSAndroid Build Coastguard Worker const size_t initialSize = touchingPointers.size();
106*38e8c45fSAndroid Build Coastguard Worker for (const PointerProperties& pointer : pointers) {
107*38e8c45fSAndroid Build Coastguard Worker std::erase_if(touchingPointers, [&pointer](const PointerProperties& properties) {
108*38e8c45fSAndroid Build Coastguard Worker return properties.id == pointer.id;
109*38e8c45fSAndroid Build Coastguard Worker });
110*38e8c45fSAndroid Build Coastguard Worker }
111*38e8c45fSAndroid Build Coastguard Worker const bool foundInconsistentState = touchingPointers.size() != initialSize;
112*38e8c45fSAndroid Build Coastguard Worker touchingPointers.insert(touchingPointers.end(), pointers.begin(), pointers.end());
113*38e8c45fSAndroid Build Coastguard Worker if (foundInconsistentState) {
114*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << __func__ << ": " << dumpVector(pointers, streamableToString) << ", device "
115*38e8c45fSAndroid Build Coastguard Worker << deviceId << " already in " << *this;
116*38e8c45fSAndroid Build Coastguard Worker return android::base::Error();
117*38e8c45fSAndroid Build Coastguard Worker }
118*38e8c45fSAndroid Build Coastguard Worker return {};
119*38e8c45fSAndroid Build Coastguard Worker }
120*38e8c45fSAndroid Build Coastguard Worker
hasTouchingPointers() const121*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasTouchingPointers() const {
122*38e8c45fSAndroid Build Coastguard Worker for (const auto& [_, state] : mDeviceStates) {
123*38e8c45fSAndroid Build Coastguard Worker if (!state.touchingPointers.empty()) {
124*38e8c45fSAndroid Build Coastguard Worker return true;
125*38e8c45fSAndroid Build Coastguard Worker }
126*38e8c45fSAndroid Build Coastguard Worker }
127*38e8c45fSAndroid Build Coastguard Worker return false;
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker
hasTouchingPointers(DeviceId deviceId) const130*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasTouchingPointers(DeviceId deviceId) const {
131*38e8c45fSAndroid Build Coastguard Worker return !getTouchingPointers(deviceId).empty();
132*38e8c45fSAndroid Build Coastguard Worker }
133*38e8c45fSAndroid Build Coastguard Worker
hasTouchingPointer(DeviceId deviceId,int32_t pointerId) const134*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const {
135*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
136*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
137*38e8c45fSAndroid Build Coastguard Worker return false;
138*38e8c45fSAndroid Build Coastguard Worker }
139*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
140*38e8c45fSAndroid Build Coastguard Worker return hasPointerId(state.touchingPointers, pointerId);
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker
getTouchingPointers(DeviceId deviceId) const143*38e8c45fSAndroid Build Coastguard Worker std::vector<PointerProperties> TouchedWindow::getTouchingPointers(DeviceId deviceId) const {
144*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
145*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
146*38e8c45fSAndroid Build Coastguard Worker return {};
147*38e8c45fSAndroid Build Coastguard Worker }
148*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
149*38e8c45fSAndroid Build Coastguard Worker return state.touchingPointers;
150*38e8c45fSAndroid Build Coastguard Worker }
151*38e8c45fSAndroid Build Coastguard Worker
removeTouchingPointer(DeviceId deviceId,int32_t pointerId)152*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::removeTouchingPointer(DeviceId deviceId, int32_t pointerId) {
153*38e8c45fSAndroid Build Coastguard Worker std::bitset<MAX_POINTER_ID + 1> pointerIds;
154*38e8c45fSAndroid Build Coastguard Worker pointerIds.set(pointerId, true);
155*38e8c45fSAndroid Build Coastguard Worker
156*38e8c45fSAndroid Build Coastguard Worker removeTouchingPointers(deviceId, pointerIds);
157*38e8c45fSAndroid Build Coastguard Worker }
158*38e8c45fSAndroid Build Coastguard Worker
removeTouchingPointers(DeviceId deviceId,std::bitset<MAX_POINTER_ID+1> pointers)159*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::removeTouchingPointers(DeviceId deviceId,
160*38e8c45fSAndroid Build Coastguard Worker std::bitset<MAX_POINTER_ID + 1> pointers) {
161*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
162*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
163*38e8c45fSAndroid Build Coastguard Worker return;
164*38e8c45fSAndroid Build Coastguard Worker }
165*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
166*38e8c45fSAndroid Build Coastguard Worker
167*38e8c45fSAndroid Build Coastguard Worker std::erase_if(state.touchingPointers, [&pointers](const PointerProperties& properties) {
168*38e8c45fSAndroid Build Coastguard Worker return pointers.test(properties.id);
169*38e8c45fSAndroid Build Coastguard Worker });
170*38e8c45fSAndroid Build Coastguard Worker
171*38e8c45fSAndroid Build Coastguard Worker state.pilferingPointerIds &= ~pointers;
172*38e8c45fSAndroid Build Coastguard Worker
173*38e8c45fSAndroid Build Coastguard Worker if (!state.hasPointers()) {
174*38e8c45fSAndroid Build Coastguard Worker mDeviceStates.erase(stateIt);
175*38e8c45fSAndroid Build Coastguard Worker }
176*38e8c45fSAndroid Build Coastguard Worker }
177*38e8c45fSAndroid Build Coastguard Worker
hasActiveStylus() const178*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasActiveStylus() const {
179*38e8c45fSAndroid Build Coastguard Worker for (const auto& [_, state] : mDeviceStates) {
180*38e8c45fSAndroid Build Coastguard Worker for (const PointerProperties& properties : state.touchingPointers) {
181*38e8c45fSAndroid Build Coastguard Worker if (properties.toolType == ToolType::STYLUS) {
182*38e8c45fSAndroid Build Coastguard Worker return true;
183*38e8c45fSAndroid Build Coastguard Worker }
184*38e8c45fSAndroid Build Coastguard Worker }
185*38e8c45fSAndroid Build Coastguard Worker for (const HoveringPointer& pointer : state.hoveringPointers) {
186*38e8c45fSAndroid Build Coastguard Worker if (pointer.properties.toolType == ToolType::STYLUS) {
187*38e8c45fSAndroid Build Coastguard Worker return true;
188*38e8c45fSAndroid Build Coastguard Worker }
189*38e8c45fSAndroid Build Coastguard Worker }
190*38e8c45fSAndroid Build Coastguard Worker }
191*38e8c45fSAndroid Build Coastguard Worker return false;
192*38e8c45fSAndroid Build Coastguard Worker }
193*38e8c45fSAndroid Build Coastguard Worker
getTouchingDeviceIds() const194*38e8c45fSAndroid Build Coastguard Worker std::set<DeviceId> TouchedWindow::getTouchingDeviceIds() const {
195*38e8c45fSAndroid Build Coastguard Worker std::set<DeviceId> deviceIds;
196*38e8c45fSAndroid Build Coastguard Worker for (const auto& [deviceId, deviceState] : mDeviceStates) {
197*38e8c45fSAndroid Build Coastguard Worker if (!deviceState.touchingPointers.empty()) {
198*38e8c45fSAndroid Build Coastguard Worker deviceIds.insert(deviceId);
199*38e8c45fSAndroid Build Coastguard Worker }
200*38e8c45fSAndroid Build Coastguard Worker }
201*38e8c45fSAndroid Build Coastguard Worker return deviceIds;
202*38e8c45fSAndroid Build Coastguard Worker }
203*38e8c45fSAndroid Build Coastguard Worker
hasPilferingPointers(DeviceId deviceId) const204*38e8c45fSAndroid Build Coastguard Worker bool TouchedWindow::hasPilferingPointers(DeviceId deviceId) const {
205*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
206*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
207*38e8c45fSAndroid Build Coastguard Worker return false;
208*38e8c45fSAndroid Build Coastguard Worker }
209*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
210*38e8c45fSAndroid Build Coastguard Worker
211*38e8c45fSAndroid Build Coastguard Worker return state.pilferingPointerIds.any();
212*38e8c45fSAndroid Build Coastguard Worker }
213*38e8c45fSAndroid Build Coastguard Worker
addPilferingPointers(DeviceId deviceId,std::bitset<MAX_POINTER_ID+1> pointerIds)214*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::addPilferingPointers(DeviceId deviceId,
215*38e8c45fSAndroid Build Coastguard Worker std::bitset<MAX_POINTER_ID + 1> pointerIds) {
216*38e8c45fSAndroid Build Coastguard Worker mDeviceStates[deviceId].pilferingPointerIds |= pointerIds;
217*38e8c45fSAndroid Build Coastguard Worker }
218*38e8c45fSAndroid Build Coastguard Worker
addPilferingPointer(DeviceId deviceId,int32_t pointerId)219*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::addPilferingPointer(DeviceId deviceId, int32_t pointerId) {
220*38e8c45fSAndroid Build Coastguard Worker mDeviceStates[deviceId].pilferingPointerIds.set(pointerId);
221*38e8c45fSAndroid Build Coastguard Worker }
222*38e8c45fSAndroid Build Coastguard Worker
getPilferingPointers(DeviceId deviceId) const223*38e8c45fSAndroid Build Coastguard Worker std::bitset<MAX_POINTER_ID + 1> TouchedWindow::getPilferingPointers(DeviceId deviceId) const {
224*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
225*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
226*38e8c45fSAndroid Build Coastguard Worker return {};
227*38e8c45fSAndroid Build Coastguard Worker }
228*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
229*38e8c45fSAndroid Build Coastguard Worker
230*38e8c45fSAndroid Build Coastguard Worker return state.pilferingPointerIds;
231*38e8c45fSAndroid Build Coastguard Worker }
232*38e8c45fSAndroid Build Coastguard Worker
getPilferingPointers() const233*38e8c45fSAndroid Build Coastguard Worker std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> TouchedWindow::getPilferingPointers() const {
234*38e8c45fSAndroid Build Coastguard Worker std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> out;
235*38e8c45fSAndroid Build Coastguard Worker for (const auto& [deviceId, state] : mDeviceStates) {
236*38e8c45fSAndroid Build Coastguard Worker out.emplace(deviceId, state.pilferingPointerIds);
237*38e8c45fSAndroid Build Coastguard Worker }
238*38e8c45fSAndroid Build Coastguard Worker return out;
239*38e8c45fSAndroid Build Coastguard Worker }
240*38e8c45fSAndroid Build Coastguard Worker
getDownTimeInTarget(DeviceId deviceId) const241*38e8c45fSAndroid Build Coastguard Worker std::optional<nsecs_t> TouchedWindow::getDownTimeInTarget(DeviceId deviceId) const {
242*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
243*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
244*38e8c45fSAndroid Build Coastguard Worker return {};
245*38e8c45fSAndroid Build Coastguard Worker }
246*38e8c45fSAndroid Build Coastguard Worker const DeviceState& state = stateIt->second;
247*38e8c45fSAndroid Build Coastguard Worker return state.downTimeInTarget;
248*38e8c45fSAndroid Build Coastguard Worker }
249*38e8c45fSAndroid Build Coastguard Worker
trySetDownTimeInTarget(DeviceId deviceId,nsecs_t downTime)250*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime) {
251*38e8c45fSAndroid Build Coastguard Worker auto [stateIt, _] = mDeviceStates.try_emplace(deviceId);
252*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
253*38e8c45fSAndroid Build Coastguard Worker
254*38e8c45fSAndroid Build Coastguard Worker if (!state.downTimeInTarget) {
255*38e8c45fSAndroid Build Coastguard Worker state.downTimeInTarget = downTime;
256*38e8c45fSAndroid Build Coastguard Worker }
257*38e8c45fSAndroid Build Coastguard Worker }
258*38e8c45fSAndroid Build Coastguard Worker
removeAllTouchingPointersForDevice(DeviceId deviceId)259*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::removeAllTouchingPointersForDevice(DeviceId deviceId) {
260*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
261*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
262*38e8c45fSAndroid Build Coastguard Worker return;
263*38e8c45fSAndroid Build Coastguard Worker }
264*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
265*38e8c45fSAndroid Build Coastguard Worker
266*38e8c45fSAndroid Build Coastguard Worker state.touchingPointers.clear();
267*38e8c45fSAndroid Build Coastguard Worker state.pilferingPointerIds.reset();
268*38e8c45fSAndroid Build Coastguard Worker state.downTimeInTarget.reset();
269*38e8c45fSAndroid Build Coastguard Worker
270*38e8c45fSAndroid Build Coastguard Worker if (!state.hasPointers()) {
271*38e8c45fSAndroid Build Coastguard Worker mDeviceStates.erase(stateIt);
272*38e8c45fSAndroid Build Coastguard Worker }
273*38e8c45fSAndroid Build Coastguard Worker }
274*38e8c45fSAndroid Build Coastguard Worker
removeHoveringPointer(DeviceId deviceId,int32_t pointerId)275*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::removeHoveringPointer(DeviceId deviceId, int32_t pointerId) {
276*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
277*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
278*38e8c45fSAndroid Build Coastguard Worker return;
279*38e8c45fSAndroid Build Coastguard Worker }
280*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
281*38e8c45fSAndroid Build Coastguard Worker
282*38e8c45fSAndroid Build Coastguard Worker std::erase_if(state.hoveringPointers, [&pointerId](const HoveringPointer& pointer) {
283*38e8c45fSAndroid Build Coastguard Worker return pointer.properties.id == pointerId;
284*38e8c45fSAndroid Build Coastguard Worker });
285*38e8c45fSAndroid Build Coastguard Worker
286*38e8c45fSAndroid Build Coastguard Worker if (!state.hasPointers()) {
287*38e8c45fSAndroid Build Coastguard Worker mDeviceStates.erase(stateIt);
288*38e8c45fSAndroid Build Coastguard Worker }
289*38e8c45fSAndroid Build Coastguard Worker }
290*38e8c45fSAndroid Build Coastguard Worker
eraseHoveringPointersIf(std::function<bool (const PointerProperties &,float,float)> condition)291*38e8c45fSAndroid Build Coastguard Worker std::vector<DeviceId> TouchedWindow::eraseHoveringPointersIf(
292*38e8c45fSAndroid Build Coastguard Worker std::function<bool(const PointerProperties&, float /*x*/, float /*y*/)> condition) {
293*38e8c45fSAndroid Build Coastguard Worker std::vector<DeviceId> erasedDevices;
294*38e8c45fSAndroid Build Coastguard Worker for (auto& [deviceId, state] : mDeviceStates) {
295*38e8c45fSAndroid Build Coastguard Worker std::erase_if(state.hoveringPointers, [&](const HoveringPointer& pointer) {
296*38e8c45fSAndroid Build Coastguard Worker if (condition(pointer.properties, pointer.x, pointer.y)) {
297*38e8c45fSAndroid Build Coastguard Worker erasedDevices.push_back(deviceId);
298*38e8c45fSAndroid Build Coastguard Worker return true;
299*38e8c45fSAndroid Build Coastguard Worker }
300*38e8c45fSAndroid Build Coastguard Worker return false;
301*38e8c45fSAndroid Build Coastguard Worker });
302*38e8c45fSAndroid Build Coastguard Worker }
303*38e8c45fSAndroid Build Coastguard Worker
304*38e8c45fSAndroid Build Coastguard Worker return erasedDevices;
305*38e8c45fSAndroid Build Coastguard Worker }
306*38e8c45fSAndroid Build Coastguard Worker
removeAllHoveringPointersForDevice(DeviceId deviceId)307*38e8c45fSAndroid Build Coastguard Worker void TouchedWindow::removeAllHoveringPointersForDevice(DeviceId deviceId) {
308*38e8c45fSAndroid Build Coastguard Worker const auto stateIt = mDeviceStates.find(deviceId);
309*38e8c45fSAndroid Build Coastguard Worker if (stateIt == mDeviceStates.end()) {
310*38e8c45fSAndroid Build Coastguard Worker return;
311*38e8c45fSAndroid Build Coastguard Worker }
312*38e8c45fSAndroid Build Coastguard Worker DeviceState& state = stateIt->second;
313*38e8c45fSAndroid Build Coastguard Worker
314*38e8c45fSAndroid Build Coastguard Worker state.hoveringPointers.clear();
315*38e8c45fSAndroid Build Coastguard Worker
316*38e8c45fSAndroid Build Coastguard Worker if (!state.hasPointers()) {
317*38e8c45fSAndroid Build Coastguard Worker mDeviceStates.erase(stateIt);
318*38e8c45fSAndroid Build Coastguard Worker }
319*38e8c45fSAndroid Build Coastguard Worker }
320*38e8c45fSAndroid Build Coastguard Worker
deviceStateToString(const TouchedWindow::DeviceState & state)321*38e8c45fSAndroid Build Coastguard Worker std::string TouchedWindow::deviceStateToString(const TouchedWindow::DeviceState& state) {
322*38e8c45fSAndroid Build Coastguard Worker return StringPrintf("[touchingPointers=%s, "
323*38e8c45fSAndroid Build Coastguard Worker "downTimeInTarget=%s, hoveringPointers=%s, pilferingPointerIds=%s]",
324*38e8c45fSAndroid Build Coastguard Worker dumpVector(state.touchingPointers, streamableToString).c_str(),
325*38e8c45fSAndroid Build Coastguard Worker toString(state.downTimeInTarget).c_str(),
326*38e8c45fSAndroid Build Coastguard Worker dumpVector(state.hoveringPointers, streamableToString).c_str(),
327*38e8c45fSAndroid Build Coastguard Worker bitsetToString(state.pilferingPointerIds).c_str());
328*38e8c45fSAndroid Build Coastguard Worker }
329*38e8c45fSAndroid Build Coastguard Worker
dump() const330*38e8c45fSAndroid Build Coastguard Worker std::string TouchedWindow::dump() const {
331*38e8c45fSAndroid Build Coastguard Worker std::string out;
332*38e8c45fSAndroid Build Coastguard Worker std::string deviceStates =
333*38e8c45fSAndroid Build Coastguard Worker dumpMap(mDeviceStates, constToString, TouchedWindow::deviceStateToString);
334*38e8c45fSAndroid Build Coastguard Worker out += StringPrintf("name='%s', targetFlags=%s, mDeviceStates=%s\n",
335*38e8c45fSAndroid Build Coastguard Worker windowHandle->getName().c_str(), targetFlags.string().c_str(),
336*38e8c45fSAndroid Build Coastguard Worker deviceStates.c_str());
337*38e8c45fSAndroid Build Coastguard Worker return out;
338*38e8c45fSAndroid Build Coastguard Worker }
339*38e8c45fSAndroid Build Coastguard Worker
operator <<(std::ostream & out,const TouchedWindow::HoveringPointer & pointer)340*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const TouchedWindow::HoveringPointer& pointer) {
341*38e8c45fSAndroid Build Coastguard Worker out << pointer.properties << " at (" << pointer.x << ", " << pointer.y << ")";
342*38e8c45fSAndroid Build Coastguard Worker return out;
343*38e8c45fSAndroid Build Coastguard Worker }
344*38e8c45fSAndroid Build Coastguard Worker
operator <<(std::ostream & out,const TouchedWindow & window)345*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const TouchedWindow& window) {
346*38e8c45fSAndroid Build Coastguard Worker out << window.dump();
347*38e8c45fSAndroid Build Coastguard Worker return out;
348*38e8c45fSAndroid Build Coastguard Worker }
349*38e8c45fSAndroid Build Coastguard Worker
350*38e8c45fSAndroid Build Coastguard Worker } // namespace inputdispatcher
351*38e8c45fSAndroid Build Coastguard Worker } // namespace android
352