xref: /aosp_15_r20/frameworks/native/services/inputflinger/dispatcher/InputTarget.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 <ftl/flags.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <gui/WindowInfo.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <ui/Transform.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <utils/BitSet.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <bitset>
24*38e8c45fSAndroid Build Coastguard Worker #include "Connection.h"
25*38e8c45fSAndroid Build Coastguard Worker #include "InputTargetFlags.h"
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker /*
30*38e8c45fSAndroid Build Coastguard Worker  * An input target specifies how an input event is to be dispatched to a particular window
31*38e8c45fSAndroid Build Coastguard Worker  * including the window's input channel, control flags, a timeout, and an X / Y offset to
32*38e8c45fSAndroid Build Coastguard Worker  * be added to input event coordinates to compensate for the absolute position of the
33*38e8c45fSAndroid Build Coastguard Worker  * window area.
34*38e8c45fSAndroid Build Coastguard Worker  */
35*38e8c45fSAndroid Build Coastguard Worker class InputTarget {
36*38e8c45fSAndroid Build Coastguard Worker public:
37*38e8c45fSAndroid Build Coastguard Worker     using Flags = InputTargetFlags;
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker     enum class DispatchMode {
40*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that the event should be sent as is.
41*38e8c45fSAndroid Build Coastguard Worker          * Should always be set unless the event is to be transmuted. */
42*38e8c45fSAndroid Build Coastguard Worker         AS_IS,
43*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
44*38e8c45fSAndroid Build Coastguard Worker          * of the area of this target and so should instead be delivered as an
45*38e8c45fSAndroid Build Coastguard Worker          * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
46*38e8c45fSAndroid Build Coastguard Worker         OUTSIDE,
47*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that a hover sequence is starting in the given window.
48*38e8c45fSAndroid Build Coastguard Worker          * The event is transmuted into ACTION_HOVER_ENTER. */
49*38e8c45fSAndroid Build Coastguard Worker         HOVER_ENTER,
50*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that a hover event happened outside of a window which handled
51*38e8c45fSAndroid Build Coastguard Worker          * previous hover events, signifying the end of the current hover sequence for that
52*38e8c45fSAndroid Build Coastguard Worker          * window.
53*38e8c45fSAndroid Build Coastguard Worker          * The event is transmuted into ACTION_HOVER_ENTER. */
54*38e8c45fSAndroid Build Coastguard Worker         HOVER_EXIT,
55*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that the event should be canceled.
56*38e8c45fSAndroid Build Coastguard Worker          * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
57*38e8c45fSAndroid Build Coastguard Worker          * outside of a window. */
58*38e8c45fSAndroid Build Coastguard Worker         SLIPPERY_EXIT,
59*38e8c45fSAndroid Build Coastguard Worker         /* This flag indicates that the event should be dispatched as an initial down.
60*38e8c45fSAndroid Build Coastguard Worker          * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
61*38e8c45fSAndroid Build Coastguard Worker          * into a new window. */
62*38e8c45fSAndroid Build Coastguard Worker         SLIPPERY_ENTER,
63*38e8c45fSAndroid Build Coastguard Worker 
64*38e8c45fSAndroid Build Coastguard Worker         ftl_last = SLIPPERY_ENTER,
65*38e8c45fSAndroid Build Coastguard Worker     };
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     // The input connection to be targeted.
68*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<Connection> connection;
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker     // Flags for the input target.
71*38e8c45fSAndroid Build Coastguard Worker     ftl::Flags<Flags> flags;
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker     // The dispatch mode that should be used for this target.
74*38e8c45fSAndroid Build Coastguard Worker     DispatchMode dispatchMode = DispatchMode::AS_IS;
75*38e8c45fSAndroid Build Coastguard Worker 
76*38e8c45fSAndroid Build Coastguard Worker     // Scaling factor to apply to MotionEvent as it is delivered.
77*38e8c45fSAndroid Build Coastguard Worker     // (ignored for KeyEvents)
78*38e8c45fSAndroid Build Coastguard Worker     float globalScaleFactor = 1.0f;
79*38e8c45fSAndroid Build Coastguard Worker 
80*38e8c45fSAndroid Build Coastguard Worker     // Current display transform. Used for compatibility for raw coordinates.
81*38e8c45fSAndroid Build Coastguard Worker     ui::Transform displayTransform;
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     // Event time for the first motion event (ACTION_DOWN) dispatched to this input target if
84*38e8c45fSAndroid Build Coastguard Worker     // FLAG_SPLIT is set.
85*38e8c45fSAndroid Build Coastguard Worker     std::optional<nsecs_t> firstDownTimeInTarget;
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker     // The window that this input target is being dispatched to. It is possible for this to be
88*38e8c45fSAndroid Build Coastguard Worker     // null for cases like global monitors.
89*38e8c45fSAndroid Build Coastguard Worker     sp<gui::WindowInfoHandle> windowHandle;
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker     InputTarget() = default;
92*38e8c45fSAndroid Build Coastguard Worker     InputTarget(const std::shared_ptr<Connection>&, ftl::Flags<Flags> = {});
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker     android::base::Result<void> addPointers(std::bitset<MAX_POINTER_ID + 1> pointerIds,
95*38e8c45fSAndroid Build Coastguard Worker                                             const ui::Transform& transform);
96*38e8c45fSAndroid Build Coastguard Worker     void setDefaultPointerTransform(const ui::Transform& transform);
97*38e8c45fSAndroid Build Coastguard Worker 
98*38e8c45fSAndroid Build Coastguard Worker     /**
99*38e8c45fSAndroid Build Coastguard Worker      * Returns whether the default pointer information should be used. This will be true when the
100*38e8c45fSAndroid Build Coastguard Worker      * InputTarget doesn't have any bits set in the pointerIds bitset. This can happen for monitors
101*38e8c45fSAndroid Build Coastguard Worker      * and non splittable windows since we want all pointers for the EventEntry to go to this
102*38e8c45fSAndroid Build Coastguard Worker      * target.
103*38e8c45fSAndroid Build Coastguard Worker      */
104*38e8c45fSAndroid Build Coastguard Worker     bool useDefaultPointerTransform() const;
105*38e8c45fSAndroid Build Coastguard Worker 
106*38e8c45fSAndroid Build Coastguard Worker     /**
107*38e8c45fSAndroid Build Coastguard Worker      * Returns the default Transform object. This should be used when useDefaultPointerTransform is
108*38e8c45fSAndroid Build Coastguard Worker      * true.
109*38e8c45fSAndroid Build Coastguard Worker      */
110*38e8c45fSAndroid Build Coastguard Worker     const ui::Transform& getDefaultPointerTransform() const;
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker     const ui::Transform& getTransformForPointer(int32_t pointerId) const;
113*38e8c45fSAndroid Build Coastguard Worker 
114*38e8c45fSAndroid Build Coastguard Worker     std::bitset<MAX_POINTER_ID + 1> getPointerIds() const;
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker     std::string getPointerInfoString() const;
117*38e8c45fSAndroid Build Coastguard Worker 
118*38e8c45fSAndroid Build Coastguard Worker private:
119*38e8c45fSAndroid Build Coastguard Worker     template <typename K, typename V>
120*38e8c45fSAndroid Build Coastguard Worker     using ArrayMap = std::vector<std::pair<K, V>>;
121*38e8c45fSAndroid Build Coastguard Worker     using PointerIds = std::bitset<MAX_POINTER_ID + 1>;
122*38e8c45fSAndroid Build Coastguard Worker     // The mapping of pointer IDs to the transform that should be used for that collection of IDs.
123*38e8c45fSAndroid Build Coastguard Worker     // Each of the pointer IDs are mutually disjoint, and their union makes up pointer IDs to
124*38e8c45fSAndroid Build Coastguard Worker     // include in the motion events dispatched to this target. We use an ArrayMap to store this to
125*38e8c45fSAndroid Build Coastguard Worker     // avoid having to define hash or comparison functions for ui::Transform, which would be needed
126*38e8c45fSAndroid Build Coastguard Worker     // to use std::unordered_map or std::map respectively.
127*38e8c45fSAndroid Build Coastguard Worker     ArrayMap<ui::Transform, PointerIds> mPointerTransforms;
128*38e8c45fSAndroid Build Coastguard Worker };
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const InputTarget& target);
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher
133