1 /*
2  * Copyright (C) 2011 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 #pragma once
18 
19 #include <android/gui/TouchOcclusionMode.h>
20 #include <binder/Parcel.h>
21 #include <binder/Parcelable.h>
22 #include <ftl/Flags.h>
23 #include <gui/constants.h>
24 #include <ui/Rect.h>
25 #include <ui/Region.h>
26 #include <ui/Transform.h>
27 #include <utils/RefBase.h>
28 #include <utils/Timers.h>
29 
30 #include "InputApplication.h"
31 
32 namespace android::gui {
33 
34 /*
35  * Describes the properties of a window that can receive input.
36  */
37 struct WindowInfo : public Parcelable {
38     WindowInfo() = default;
39 
40     // Window flags from WindowManager.LayoutParams
41     enum class Flag : uint32_t {
42         ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
43         DIM_BEHIND = 0x00000002,
44         BLUR_BEHIND = 0x00000004,
45         NOT_FOCUSABLE = 0x00000008,
46         NOT_TOUCHABLE = 0x00000010,
47         NOT_TOUCH_MODAL = 0x00000020,
48         TOUCHABLE_WHEN_WAKING = 0x00000040,
49         KEEP_SCREEN_ON = 0x00000080,
50         LAYOUT_IN_SCREEN = 0x00000100,
51         LAYOUT_NO_LIMITS = 0x00000200,
52         FULLSCREEN = 0x00000400,
53         FORCE_NOT_FULLSCREEN = 0x00000800,
54         DITHER = 0x00001000,
55         SECURE = 0x00002000,
56         SCALED = 0x00004000,
57         IGNORE_CHEEK_PRESSES = 0x00008000,
58         LAYOUT_INSET_DECOR = 0x00010000,
59         ALT_FOCUSABLE_IM = 0x00020000,
60         WATCH_OUTSIDE_TOUCH = 0x00040000,
61         SHOW_WHEN_LOCKED = 0x00080000,
62         SHOW_WALLPAPER = 0x00100000,
63         TURN_SCREEN_ON = 0x00200000,
64         DISMISS_KEYGUARD = 0x00400000,
65         SPLIT_TOUCH = 0x00800000,
66         HARDWARE_ACCELERATED = 0x01000000,
67         LAYOUT_IN_OVERSCAN = 0x02000000,
68         TRANSLUCENT_STATUS = 0x04000000,
69         TRANSLUCENT_NAVIGATION = 0x08000000,
70         LOCAL_FOCUS_MODE = 0x10000000,
71         SLIPPERY = 0x20000000,
72         LAYOUT_ATTACHED_IN_DECOR = 0x40000000,
73         DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000,
74     }; // Window types from WindowManager.LayoutParams
75 
76     enum class Type : int32_t {
77         UNKNOWN = 0,
78         FIRST_APPLICATION_WINDOW = 1,
79         BASE_APPLICATION = 1,
80         APPLICATION = 2,
81         APPLICATION_STARTING = 3,
82         LAST_APPLICATION_WINDOW = 99,
83         FIRST_SUB_WINDOW = 1000,
84         APPLICATION_PANEL = FIRST_SUB_WINDOW,
85         APPLICATION_MEDIA = FIRST_SUB_WINDOW + 1,
86         APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW + 2,
87         APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW + 3,
88         APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW + 4,
89         LAST_SUB_WINDOW = 1999,
90         FIRST_SYSTEM_WINDOW = 2000,
91         STATUS_BAR = FIRST_SYSTEM_WINDOW,
92         SEARCH_BAR = FIRST_SYSTEM_WINDOW + 1,
93         PHONE = FIRST_SYSTEM_WINDOW + 2,
94         SYSTEM_ALERT = FIRST_SYSTEM_WINDOW + 3,
95         KEYGUARD = FIRST_SYSTEM_WINDOW + 4,
96         TOAST = FIRST_SYSTEM_WINDOW + 5,
97         SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW + 6,
98         PRIORITY_PHONE = FIRST_SYSTEM_WINDOW + 7,
99         SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW + 8,
100         KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW + 9,
101         SYSTEM_ERROR = FIRST_SYSTEM_WINDOW + 10,
102         INPUT_METHOD = FIRST_SYSTEM_WINDOW + 11,
103         INPUT_METHOD_DIALOG = FIRST_SYSTEM_WINDOW + 12,
104         WALLPAPER = FIRST_SYSTEM_WINDOW + 13,
105         STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW + 14,
106         SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW + 15,
107         DRAG = FIRST_SYSTEM_WINDOW + 16,
108         STATUS_BAR_SUB_PANEL = FIRST_SYSTEM_WINDOW + 17,
109         POINTER = FIRST_SYSTEM_WINDOW + 18,
110         NAVIGATION_BAR = FIRST_SYSTEM_WINDOW + 19,
111         VOLUME_OVERLAY = FIRST_SYSTEM_WINDOW + 20,
112         BOOT_PROGRESS = FIRST_SYSTEM_WINDOW + 21,
113         INPUT_CONSUMER = FIRST_SYSTEM_WINDOW + 22,
114         NAVIGATION_BAR_PANEL = FIRST_SYSTEM_WINDOW + 24,
115         MAGNIFICATION_OVERLAY = FIRST_SYSTEM_WINDOW + 27,
116         ACCESSIBILITY_OVERLAY = FIRST_SYSTEM_WINDOW + 32,
117         DOCK_DIVIDER = FIRST_SYSTEM_WINDOW + 34,
118         ACCESSIBILITY_MAGNIFICATION_OVERLAY = FIRST_SYSTEM_WINDOW + 39,
119         NOTIFICATION_SHADE = FIRST_SYSTEM_WINDOW + 40,
120         LAST_SYSTEM_WINDOW = 2999,
121     };
122 
123     enum class Feature {
124         DISABLE_TOUCH_PAD_GESTURES = 1u << 0,
125         NO_INPUT_CHANNEL = 1u << 1,
126         DISABLE_USER_ACTIVITY = 1u << 2,
127         DROP_INPUT = 1u << 3,
128         DROP_INPUT_IF_OBSCURED = 1u << 4,
129     };
130 
131     /* These values are filled in by the WM and passed through SurfaceFlinger
132      * unless specified otherwise.
133      */
134     // This value should NOT be used to uniquely identify the window. There may be different
135     // input windows that have the same token.
136     sp<IBinder> token;
137 
138     // The token that identifies which client window this WindowInfo was created for.
139     sp<IBinder> windowToken;
140 
141     // This uniquely identifies the input window.
142     int32_t id = -1;
143     std::string name;
144     Flags<Flag> flags;
145     Type type = Type::UNKNOWN;
146     std::chrono::nanoseconds dispatchingTimeout = std::chrono::seconds(5);
147 
148     /* These values are filled in by SurfaceFlinger. */
149     int32_t frameLeft = -1;
150     int32_t frameTop = -1;
151     int32_t frameRight = -1;
152     int32_t frameBottom = -1;
153 
154     /*
155      * SurfaceFlinger consumes this value to shrink the computed frame. This is
156      * different from shrinking the touchable region in that it DOES shift the coordinate
157      * space where-as the touchable region does not and is more like "cropping". This
158      * is used for window shadows.
159      */
160     int32_t surfaceInset = 0;
161 
162     // A global scaling factor for all windows. Unlike windowScaleX/Y this results
163     // in scaling of the TOUCH_MAJOR/TOUCH_MINOR axis.
164     float globalScaleFactor = 1.0f;
165 
166     // The opacity of this window, from 0.0 to 1.0 (inclusive).
167     // An alpha of 1.0 means fully opaque and 0.0 means fully transparent.
168     float alpha;
169 
170     // Transform applied to individual windows.
171     ui::Transform transform;
172 
173     // Display orientation as ui::Transform::RotationFlags. Used for compatibility raw coordinates.
174     uint32_t displayOrientation = ui::Transform::ROT_0;
175 
176     // Display size in its natural rotation. Used to rotate raw coordinates for compatibility.
177     int32_t displayWidth = 0;
178     int32_t displayHeight = 0;
179 
180     /*
181      * This is filled in by the WM relative to the frame and then translated
182      * to absolute coordinates by SurfaceFlinger once the frame is computed.
183      */
184     Region touchableRegion;
185     bool visible = false;
186     bool focusable = false;
187     bool hasWallpaper = false;
188     bool paused = false;
189     /* This flag is set when the window is of a trusted type that is allowed to silently
190      * overlay other windows for the purpose of implementing the secure views feature.
191      * Trusted overlays, such as IME windows, can partly obscure other windows without causing
192      * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
193      */
194     bool trustedOverlay = false;
195     TouchOcclusionMode touchOcclusionMode = TouchOcclusionMode::BLOCK_UNTRUSTED;
196     int32_t ownerPid = -1;
197     int32_t ownerUid = -1;
198     std::string packageName;
199     Flags<Feature> inputFeatures;
200     int32_t displayId = ADISPLAY_ID_NONE;
201     int32_t portalToDisplayId = ADISPLAY_ID_NONE;
202     InputApplicationInfo applicationInfo;
203     bool replaceTouchableRegionWithCrop = false;
204     wp<IBinder> touchableRegionCropHandle;
205 
206     void addTouchableRegion(const Rect& region);
207 
208     bool touchableRegionContainsPoint(int32_t x, int32_t y) const;
209 
210     bool frameContainsPoint(int32_t x, int32_t y) const;
211 
212     bool supportsSplitTouch() const;
213 
214     bool overlaps(const WindowInfo* other) const;
215 
216     bool operator==(const WindowInfo& inputChannel) const;
217 
218     status_t writeToParcel(android::Parcel* parcel) const override;
219 
220     status_t readFromParcel(const android::Parcel* parcel) override;
221 };
222 
223 /*
224  * Handle for a window that can receive input.
225  *
226  * Used by the native input dispatcher to indirectly refer to the window manager objects
227  * that describe a window.
228  */
229 class WindowInfoHandle : public RefBase {
230 public:
231     explicit WindowInfoHandle();
232     WindowInfoHandle(const WindowInfoHandle& other);
233     WindowInfoHandle(const WindowInfo& other);
234 
getInfo()235     inline const WindowInfo* getInfo() const { return &mInfo; }
236 
237     sp<IBinder> getToken() const;
238 
getId()239     int32_t getId() const { return mInfo.id; }
240 
getApplicationToken()241     sp<IBinder> getApplicationToken() { return mInfo.applicationInfo.token; }
242 
getName()243     inline std::string getName() const { return !mInfo.name.empty() ? mInfo.name : "<invalid>"; }
244 
getDispatchingTimeout(std::chrono::nanoseconds defaultValue)245     inline std::chrono::nanoseconds getDispatchingTimeout(
246             std::chrono::nanoseconds defaultValue) const {
247         return mInfo.token ? std::chrono::nanoseconds(mInfo.dispatchingTimeout) : defaultValue;
248     }
249 
250     /**
251      * Updates from another input window handle.
252      */
253     void updateFrom(const sp<WindowInfoHandle> handle);
254 
255     /**
256      * Releases the channel used by the associated information when it is
257      * no longer needed.
258      */
259     void releaseChannel();
260 
261     // Not override since this class is not derrived from Parcelable.
262     status_t readFromParcel(const android::Parcel* parcel);
263     status_t writeToParcel(android::Parcel* parcel) const;
264 
265 protected:
266     virtual ~WindowInfoHandle();
267 
268     WindowInfo mInfo;
269 };
270 } // namespace android::gui