1 /*
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 
17 #ifndef _UI_INPUT_INPUTDISPATCHER_INPUTTARGET_H
18 #define _UI_INPUT_INPUTDISPATCHER_INPUTTARGET_H
19 
20 #include <input/InputTransport.h>
21 #include <utils/BitSet.h>
22 #include <utils/RefBase.h>
23 
24 namespace android::inputdispatcher {
25 
26 /*
27  * Information about each pointer for an InputTarget. This includes offset and scale so
28  * all pointers can be normalized to a single offset and scale.
29  *
30  * These values are ignored for KeyEvents
31  */
32 struct PointerInfo {
33     // The x and y offset to add to a MotionEvent as it is delivered.
34     float xOffset = 0.0f;
35     float yOffset = 0.0f;
36 
37     // Scaling factor to apply to MotionEvent as it is delivered.
38     float windowXScale = 1.0f;
39     float windowYScale = 1.0f;
40 };
41 
42 /*
43  * An input target specifies how an input event is to be dispatched to a particular window
44  * including the window's input channel, control flags, a timeout, and an X / Y offset to
45  * be added to input event coordinates to compensate for the absolute position of the
46  * window area.
47  */
48 struct InputTarget {
49     enum {
50         /* This flag indicates that the event is being delivered to a foreground application. */
51         FLAG_FOREGROUND = 1 << 0,
52 
53         /* This flag indicates that the MotionEvent falls within the area of the target
54          * obscured by another visible window above it.  The motion event should be
55          * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
56         FLAG_WINDOW_IS_OBSCURED = 1 << 1,
57 
58         /* This flag indicates that a motion event is being split across multiple windows. */
59         FLAG_SPLIT = 1 << 2,
60 
61         /* This flag indicates that the pointer coordinates dispatched to the application
62          * will be zeroed out to avoid revealing information to an application. This is
63          * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
64          * the same UID from watching all touches. */
65         FLAG_ZERO_COORDS = 1 << 3,
66 
67         /* This flag indicates that the event should be sent as is.
68          * Should always be set unless the event is to be transmuted. */
69         FLAG_DISPATCH_AS_IS = 1 << 8,
70 
71         /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
72          * of the area of this target and so should instead be delivered as an
73          * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
74         FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
75 
76         /* This flag indicates that a hover sequence is starting in the given window.
77          * The event is transmuted into ACTION_HOVER_ENTER. */
78         FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
79 
80         /* This flag indicates that a hover event happened outside of a window which handled
81          * previous hover events, signifying the end of the current hover sequence for that
82          * window.
83          * The event is transmuted into ACTION_HOVER_ENTER. */
84         FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
85 
86         /* This flag indicates that the event should be canceled.
87          * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
88          * outside of a window. */
89         FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
90 
91         /* This flag indicates that the event should be dispatched as an initial down.
92          * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
93          * into a new window. */
94         FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
95 
96         /* Mask for all dispatch modes. */
97         FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS | FLAG_DISPATCH_AS_OUTSIDE |
98                 FLAG_DISPATCH_AS_HOVER_ENTER | FLAG_DISPATCH_AS_HOVER_EXIT |
99                 FLAG_DISPATCH_AS_SLIPPERY_EXIT | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
100 
101         /* This flag indicates that the target of a MotionEvent is partly or wholly
102          * obscured by another visible window above it.  The motion event should be
103          * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
104         FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
105 
106     };
107 
108     // The input channel to be targeted.
109     sp<InputChannel> inputChannel;
110 
111     // Flags for the input target.
112     int32_t flags = 0;
113 
114     // Scaling factor to apply to MotionEvent as it is delivered.
115     // (ignored for KeyEvents)
116     float globalScaleFactor = 1.0f;
117 
118     // The subset of pointer ids to include in motion events dispatched to this input target
119     // if FLAG_SPLIT is set.
120     BitSet32 pointerIds;
121     // The data is stored by the pointerId. Use the bit position of pointerIds to look up
122     // PointerInfo per pointerId.
123     PointerInfo pointerInfos[MAX_POINTERS];
124 
125     void addPointers(BitSet32 pointerIds, float xOffset, float yOffset, float windowXScale,
126                      float windowYScale);
127     void setDefaultPointerInfo(float xOffset, float yOffset, float windowXScale,
128                                float windowYScale);
129 
130     /**
131      * Returns whether the default pointer information should be used. This will be true when the
132      * InputTarget doesn't have any bits set in the pointerIds bitset. This can happen for monitors
133      * and non splittable windows since we want all pointers for the EventEntry to go to this
134      * target.
135      */
136     bool useDefaultPointerInfo() const;
137 
138     /**
139      * Returns the default PointerInfo object. This should be used when useDefaultPointerInfo is
140      * true.
141      */
142     const PointerInfo& getDefaultPointerInfo() const;
143 
144     std::string getPointerInfoString() const;
145 };
146 
147 std::string dispatchModeToString(int32_t dispatchMode);
148 
149 } // namespace android::inputdispatcher
150 
151 #endif // _UI_INPUT_INPUTDISPATCHER_INPUTTARGET_H
152