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