xref: /aosp_15_r20/frameworks/native/services/inputflinger/dispatcher/TouchedWindow.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2019 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 #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <gui/WindowInfo.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <utils/BitSet.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <bitset>
23*38e8c45fSAndroid Build Coastguard Worker #include <ostream>
24*38e8c45fSAndroid Build Coastguard Worker #include <set>
25*38e8c45fSAndroid Build Coastguard Worker #include "InputTarget.h"
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker namespace inputdispatcher {
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker // Focus tracking for touch.
32*38e8c45fSAndroid Build Coastguard Worker struct TouchedWindow {
33*38e8c45fSAndroid Build Coastguard Worker     sp<gui::WindowInfoHandle> windowHandle;
34*38e8c45fSAndroid Build Coastguard Worker     InputTarget::DispatchMode dispatchMode = InputTarget::DispatchMode::AS_IS;
35*38e8c45fSAndroid Build Coastguard Worker     ftl::Flags<InputTarget::Flags> targetFlags;
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker     // Hovering
38*38e8c45fSAndroid Build Coastguard Worker     bool hasHoveringPointers() const;
39*38e8c45fSAndroid Build Coastguard Worker     bool hasHoveringPointers(DeviceId deviceId) const;
40*38e8c45fSAndroid Build Coastguard Worker     bool hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const;
41*38e8c45fSAndroid Build Coastguard Worker     void addHoveringPointer(DeviceId deviceId, const PointerProperties& pointer, float x, float y);
42*38e8c45fSAndroid Build Coastguard Worker     void removeHoveringPointer(DeviceId deviceId, int32_t pointerId);
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker     // Touching
45*38e8c45fSAndroid Build Coastguard Worker     bool hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const;
46*38e8c45fSAndroid Build Coastguard Worker     bool hasTouchingPointers() const;
47*38e8c45fSAndroid Build Coastguard Worker     bool hasTouchingPointers(DeviceId deviceId) const;
48*38e8c45fSAndroid Build Coastguard Worker     std::vector<PointerProperties> getTouchingPointers(DeviceId deviceId) const;
49*38e8c45fSAndroid Build Coastguard Worker     android::base::Result<void> addTouchingPointers(DeviceId deviceId,
50*38e8c45fSAndroid Build Coastguard Worker                                                     const std::vector<PointerProperties>& pointers);
51*38e8c45fSAndroid Build Coastguard Worker     void removeTouchingPointer(DeviceId deviceId, int32_t pointerId);
52*38e8c45fSAndroid Build Coastguard Worker     void removeTouchingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointers);
53*38e8c45fSAndroid Build Coastguard Worker     bool hasActiveStylus() const;
54*38e8c45fSAndroid Build Coastguard Worker     std::set<DeviceId> getTouchingDeviceIds() const;
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker     // Pilfering pointers
57*38e8c45fSAndroid Build Coastguard Worker     bool hasPilferingPointers(DeviceId deviceId) const;
58*38e8c45fSAndroid Build Coastguard Worker     void addPilferingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointerIds);
59*38e8c45fSAndroid Build Coastguard Worker     void addPilferingPointer(DeviceId deviceId, int32_t pointerId);
60*38e8c45fSAndroid Build Coastguard Worker     std::bitset<MAX_POINTER_ID + 1> getPilferingPointers(DeviceId deviceId) const;
61*38e8c45fSAndroid Build Coastguard Worker     std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> getPilferingPointers() const;
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker     // Down time
64*38e8c45fSAndroid Build Coastguard Worker     std::optional<nsecs_t> getDownTimeInTarget(DeviceId deviceId) const;
65*38e8c45fSAndroid Build Coastguard Worker     void trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime);
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     void removeAllTouchingPointersForDevice(DeviceId deviceId);
68*38e8c45fSAndroid Build Coastguard Worker     void removeAllHoveringPointersForDevice(DeviceId deviceId);
69*38e8c45fSAndroid Build Coastguard Worker     void clearHoveringPointers(DeviceId deviceId);
70*38e8c45fSAndroid Build Coastguard Worker     std::string dump() const;
71*38e8c45fSAndroid Build Coastguard Worker 
72*38e8c45fSAndroid Build Coastguard Worker     struct HoveringPointer {
73*38e8c45fSAndroid Build Coastguard Worker         PointerProperties properties;
74*38e8c45fSAndroid Build Coastguard Worker         float x;
75*38e8c45fSAndroid Build Coastguard Worker         float y;
76*38e8c45fSAndroid Build Coastguard Worker     };
77*38e8c45fSAndroid Build Coastguard Worker 
78*38e8c45fSAndroid Build Coastguard Worker     std::vector<DeviceId> eraseHoveringPointersIf(
79*38e8c45fSAndroid Build Coastguard Worker             std::function<bool(const PointerProperties&, float /*x*/, float /*y*/)> condition);
80*38e8c45fSAndroid Build Coastguard Worker 
81*38e8c45fSAndroid Build Coastguard Worker private:
82*38e8c45fSAndroid Build Coastguard Worker     struct DeviceState {
83*38e8c45fSAndroid Build Coastguard Worker         std::vector<PointerProperties> touchingPointers;
84*38e8c45fSAndroid Build Coastguard Worker         // The pointer ids of the pointers that this window is currently pilfering, by device
85*38e8c45fSAndroid Build Coastguard Worker         std::bitset<MAX_POINTER_ID + 1> pilferingPointerIds;
86*38e8c45fSAndroid Build Coastguard Worker         // Time at which the first action down occurred on this window, for each device
87*38e8c45fSAndroid Build Coastguard Worker         // NOTE: This is not initialized in case of HOVER entry/exit and DISPATCH_AS_OUTSIDE
88*38e8c45fSAndroid Build Coastguard Worker         // scenario.
89*38e8c45fSAndroid Build Coastguard Worker         std::optional<nsecs_t> downTimeInTarget;
90*38e8c45fSAndroid Build Coastguard Worker         std::vector<HoveringPointer> hoveringPointers;
91*38e8c45fSAndroid Build Coastguard Worker 
hasPointersTouchedWindow::DeviceState92*38e8c45fSAndroid Build Coastguard Worker         bool hasPointers() const { return !touchingPointers.empty() || !hoveringPointers.empty(); };
93*38e8c45fSAndroid Build Coastguard Worker     };
94*38e8c45fSAndroid Build Coastguard Worker 
95*38e8c45fSAndroid Build Coastguard Worker     std::map<DeviceId, DeviceState> mDeviceStates;
96*38e8c45fSAndroid Build Coastguard Worker 
97*38e8c45fSAndroid Build Coastguard Worker     static std::string deviceStateToString(const TouchedWindow::DeviceState& state);
98*38e8c45fSAndroid Build Coastguard Worker };
99*38e8c45fSAndroid Build Coastguard Worker 
100*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const TouchedWindow& window);
101*38e8c45fSAndroid Build Coastguard Worker 
102*38e8c45fSAndroid Build Coastguard Worker } // namespace inputdispatcher
103*38e8c45fSAndroid Build Coastguard Worker } // namespace android
104