blob: d55d6576671ee7e712fc6a7599980d617a13871c [file] [log] [blame]
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -08001/*
2 * Copyright (C) 2022 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
17#include "TouchedWindow.h"
18
19#include <android-base/stringprintf.h>
20#include <input/PrintTools.h>
21
22using android::base::StringPrintf;
23
24namespace android {
25
26namespace inputdispatcher {
27
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000028bool TouchedWindow::hasHoveringPointers() const {
29 return !mHoveringPointerIdsByDevice.empty();
30}
31
Siarhei Vishniakoue0431e42023-01-28 17:01:39 -080032bool TouchedWindow::hasHoveringPointers(int32_t deviceId) const {
33 return mHoveringPointerIdsByDevice.find(deviceId) != mHoveringPointerIdsByDevice.end();
34}
35
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000036void TouchedWindow::clearHoveringPointers() {
37 mHoveringPointerIdsByDevice.clear();
38}
39
40bool TouchedWindow::hasHoveringPointer(int32_t deviceId, int32_t pointerId) const {
41 auto it = mHoveringPointerIdsByDevice.find(deviceId);
42 if (it == mHoveringPointerIdsByDevice.end()) {
43 return false;
44 }
45 return it->second.test(pointerId);
46}
47
48void TouchedWindow::addHoveringPointer(int32_t deviceId, int32_t pointerId) {
49 const auto [it, _] = mHoveringPointerIdsByDevice.insert({deviceId, {}});
50 it->second.set(pointerId);
51}
52
Siarhei Vishniakou6464e462023-02-06 18:57:59 -080053void TouchedWindow::removeTouchingPointer(int32_t pointerId) {
54 pointerIds.reset(pointerId);
55 pilferedPointerIds.reset(pointerId);
56 if (pointerIds.none()) {
57 firstDownTimeInTarget.reset();
58 }
59}
60
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -070061void TouchedWindow::removeAllTouchingPointers() {
62 pointerIds.reset();
63}
64
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000065void TouchedWindow::removeHoveringPointer(int32_t deviceId, int32_t pointerId) {
66 const auto it = mHoveringPointerIdsByDevice.find(deviceId);
67 if (it == mHoveringPointerIdsByDevice.end()) {
68 return;
69 }
70 it->second.set(pointerId, false);
71
72 if (it->second.none()) {
73 mHoveringPointerIdsByDevice.erase(deviceId);
74 }
75}
76
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -070077void TouchedWindow::removeAllHoveringPointersForDevice(int32_t deviceId) {
78 mHoveringPointerIdsByDevice.erase(deviceId);
79}
80
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080081std::string TouchedWindow::dump() const {
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000082 std::string out;
83 std::string hoveringPointers =
84 dumpMap(mHoveringPointerIdsByDevice, constToString, bitsetToString);
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080085 out += StringPrintf("name='%s', pointerIds=%s, targetFlags=%s, firstDownTimeInTarget=%s, "
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080086 "mHoveringPointerIdsByDevice=%s, pilferedPointerIds=%s\n",
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080087 windowHandle->getName().c_str(), bitsetToString(pointerIds).c_str(),
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000088 targetFlags.string().c_str(), toString(firstDownTimeInTarget).c_str(),
Siarhei Vishniakou060f82b2023-01-27 06:39:14 -080089 hoveringPointers.c_str(), bitsetToString(pilferedPointerIds).c_str());
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000090 return out;
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080091}
92
93} // namespace inputdispatcher
94} // namespace android