1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ROSEN_DM_COMMON_H
17 #define OHOS_ROSEN_DM_COMMON_H
18 
19 #include <refbase.h>
20 #include <string>
21 #include <map>
22 
23 #ifdef _WIN32
24 #define WINDOW_EXPORT __attribute__((dllexport))
25 #else
26 #define WINDOW_EXPORT __attribute__((visibility("default")))
27 #endif
28 
29 namespace OHOS {
30 namespace Rosen {
31 using DisplayId = uint64_t;
32 using ScreenId = uint64_t;
33 namespace {
34 constexpr DisplayId DISPLAY_ID_INVALID = -1ULL;
35 constexpr ScreenId SCREEN_ID_INVALID = -1ULL;
36 constexpr int DOT_PER_INCH = 160;
37 const static std::string DEFAULT_SCREEN_NAME = "buildIn";
38 constexpr int DOT_PER_INCH_MAXIMUM_VALUE = 640;
39 constexpr int DOT_PER_INCH_MINIMUM_VALUE = 80;
40 constexpr uint32_t BASELINE_DENSITY = 160;
41 }
42 
43 /**
44  * @brief Power state change reason.
45  */
46 enum class PowerStateChangeReason : uint32_t {
47     POWER_BUTTON = 0,
48     STATE_CHANGE_REASON_INIT = 0,
49     STATE_CHANGE_REASON_TIMEOUT = 1,
50     STATE_CHANGE_REASON_RUNNING_LOCK = 2,
51     STATE_CHANGE_REASON_BATTERY = 3,
52     STATE_CHANGE_REASON_THERMAL = 4,
53     STATE_CHANGE_REASON_WORK = 5,
54     STATE_CHANGE_REASON_SYSTEM = 6,
55     STATE_CHANGE_REASON_APPLICATION = 10,
56     STATE_CHANGE_REASON_SETTINGS = 11,
57     STATE_CHANGE_REASON_HARD_KEY = 12,
58     STATE_CHANGE_REASON_TOUCH = 13,
59     STATE_CHANGE_REASON_CABLE = 14,
60     STATE_CHANGE_REASON_SENSOR = 15,
61     STATE_CHANGE_REASON_LID = 16,
62     STATE_CHANGE_REASON_CAMERA = 17,
63     STATE_CHANGE_REASON_ACCESS = 18,
64     STATE_CHANGE_REASON_RESET = 19,
65     STATE_CHANGE_REASON_POWER_KEY = 20,
66     STATE_CHANGE_REASON_KEYBOARD = 21,
67     STATE_CHANGE_REASON_MOUSE = 22,
68     STATE_CHANGE_REASON_DOUBLE_CLICK = 23,
69     STATE_CHANGE_REASON_SWITCH = 25,
70     STATE_CHANGE_REASON_PRE_BRIGHT = 26,
71     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS = 27,
72     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON = 28,
73     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF = 29,
74     STATE_CHANGE_REASON_DISPLAY_SWITCH = 30,
75     STATE_CHANGE_REASON_PROXIMITY = 32,
76     STATE_CHANGE_REASON_AOD_SLIDING = 40,
77     STATE_CHANGE_REASON_PEN = 41,
78     STATE_CHANGE_REASON_SHUT_DOWN = 42,
79     STATE_CHANGE_REASON_REMOTE = 100,
80     STATE_CHANGE_REASON_UNKNOWN = 1000,
81 };
82 
83 /**
84  * @brief Enumerates the state of the screen power.
85  */
86 enum class ScreenPowerState : uint32_t {
87     POWER_ON,
88     POWER_STAND_BY,
89     POWER_SUSPEND,
90     POWER_OFF,
91     POWER_BUTT,
92     INVALID_STATE,
93 };
94 
95 /**
96  * @brief Enumerates the state of the display.
97  */
98 enum class DisplayState : uint32_t {
99     UNKNOWN,
100     OFF,
101     ON,
102     DOZE,
103     DOZE_SUSPEND,
104     VR,
105     ON_SUSPEND,
106 };
107 
108 /**
109  * @brief Enumerates display events.
110  */
111 enum class DisplayEvent : uint32_t {
112     UNLOCK,
113     KEYGUARD_DRAWN,
114 };
115 
116 /**
117  * @brief Enumerates DMError.
118  */
119 enum class DMError : int32_t {
120     DM_OK = 0,
121     DM_ERROR_INIT_DMS_PROXY_LOCKED = 100,
122     DM_ERROR_IPC_FAILED = 101,
123     DM_ERROR_REMOTE_CREATE_FAILED = 110,
124     DM_ERROR_NULLPTR = 120,
125     DM_ERROR_INVALID_PARAM = 130,
126     DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED = 140,
127     DM_ERROR_DEATH_RECIPIENT = 150,
128     DM_ERROR_INVALID_MODE_ID = 160,
129     DM_ERROR_WRITE_DATA_FAILED = 170,
130     DM_ERROR_RENDER_SERVICE_FAILED = 180,
131     DM_ERROR_UNREGISTER_AGENT_FAILED = 190,
132     DM_ERROR_INVALID_CALLING = 200,
133     DM_ERROR_INVALID_PERMISSION = 201,
134     DM_ERROR_NOT_SYSTEM_APP = 202,
135     DM_ERROR_DEVICE_NOT_SUPPORT = 801,
136     DM_ERROR_UNKNOWN = -1,
137 };
138 
139 /**
140  * @brief Enumerates DM error codes.
141  */
142 enum class DmErrorCode : int32_t {
143     DM_OK = 0,
144     DM_ERROR_NO_PERMISSION = 201,
145     DM_ERROR_NOT_SYSTEM_APP = 202,
146     DM_ERROR_INVALID_PARAM = 401,
147     DM_ERROR_DEVICE_NOT_SUPPORT = 801,
148     DM_ERROR_INVALID_SCREEN = 1400001,
149     DM_ERROR_INVALID_CALLING = 1400002,
150     DM_ERROR_SYSTEM_INNORMAL = 1400003,
151 };
152 
153 /**
154  * @brief Constructs the mapping of the DM errors to the DM error codes.
155  */
156 const std::map<DMError, DmErrorCode> DM_JS_TO_ERROR_CODE_MAP {
157     {DMError::DM_OK,                                    DmErrorCode::DM_OK                          },
158     {DMError::DM_ERROR_INVALID_PERMISSION,              DmErrorCode::DM_ERROR_NO_PERMISSION         },
159     {DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED,           DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
160     {DMError::DM_ERROR_IPC_FAILED,                      DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
161     {DMError::DM_ERROR_REMOTE_CREATE_FAILED,            DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
162     {DMError::DM_ERROR_NULLPTR,                         DmErrorCode::DM_ERROR_INVALID_SCREEN        },
163     {DMError::DM_ERROR_INVALID_PARAM,                   DmErrorCode::DM_ERROR_INVALID_PARAM         },
164     {DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED,    DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
165     {DMError::DM_ERROR_DEATH_RECIPIENT,                 DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
166     {DMError::DM_ERROR_INVALID_MODE_ID,                 DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
167     {DMError::DM_ERROR_WRITE_DATA_FAILED,               DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
168     {DMError::DM_ERROR_RENDER_SERVICE_FAILED,           DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
169     {DMError::DM_ERROR_UNREGISTER_AGENT_FAILED,         DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
170     {DMError::DM_ERROR_INVALID_CALLING,                 DmErrorCode::DM_ERROR_INVALID_CALLING       },
171     {DMError::DM_ERROR_NOT_SYSTEM_APP,                  DmErrorCode::DM_ERROR_NOT_SYSTEM_APP        },
172     {DMError::DM_ERROR_UNKNOWN,                         DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
173 };
174 
175 using DisplayStateCallback = std::function<void(DisplayState)>;
176 
177 /**
178  * @brief Enumerates display power events.
179  */
180 enum class DisplayPowerEvent : uint32_t {
181     WAKE_UP,
182     SLEEP,
183     DISPLAY_ON,
184     DISPLAY_OFF,
185     DESKTOP_READY,
186     DOZE,
187     DOZE_SUSPEND,
188 };
189 
190 /**
191  * @brief Enumerates event status.
192  */
193 enum class EventStatus : uint32_t {
194     BEGIN,
195     END,
196 };
197 
198 class IDisplayPowerEventListener : public RefBase {
199 public:
200     /**
201      * @brief Notify when display power event status changed.
202      */
203     virtual void OnDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) = 0;
204 };
205 
206 /**
207  * @brief Enumerates screen change events.
208  */
209 enum class ScreenChangeEvent : uint32_t {
210     UPDATE_ORIENTATION,
211     UPDATE_ROTATION,
212     CHANGE_MODE,
213     VIRTUAL_PIXEL_RATIO_CHANGED,
214 };
215 
216 /**
217  * @brief Enumerates screen group change events.
218  */
219 enum class ScreenGroupChangeEvent : uint32_t {
220     ADD_TO_GROUP,
221     REMOVE_FROM_GROUP,
222     CHANGE_GROUP,
223 };
224 
225 /**
226  * @brief Enumerates rotations.
227  */
228 enum class Rotation : uint32_t {
229     ROTATION_0,
230     ROTATION_90,
231     ROTATION_180,
232     ROTATION_270,
233 };
234 
235 /**
236  * @brief Enumerates orientations.
237  */
238 enum class Orientation : uint32_t {
239     BEGIN = 0,
240     UNSPECIFIED = BEGIN,
241     VERTICAL = 1,
242     HORIZONTAL = 2,
243     REVERSE_VERTICAL = 3,
244     REVERSE_HORIZONTAL = 4,
245     SENSOR = 5,
246     SENSOR_VERTICAL = 6,
247     SENSOR_HORIZONTAL = 7,
248     AUTO_ROTATION_RESTRICTED = 8,
249     AUTO_ROTATION_PORTRAIT_RESTRICTED = 9,
250     AUTO_ROTATION_LANDSCAPE_RESTRICTED = 10,
251     LOCKED = 11,
252     FOLLOW_RECENT = 12,
253     AUTO_ROTATION_UNSPECIFIED = 13,
254     USER_ROTATION_PORTRAIT = 14,
255     USER_ROTATION_LANDSCAPE = 15,
256     USER_ROTATION_PORTRAIT_INVERTED = 16,
257     USER_ROTATION_LANDSCAPE_INVERTED = 17,
258     FOLLOW_DESKTOP = 18,
259     END = FOLLOW_DESKTOP,
260 };
261 
262 /**
263  * @brief Enumerates display orientations.
264  */
265 enum class DisplayOrientation : uint32_t {
266     PORTRAIT = 0,
267     LANDSCAPE,
268     PORTRAIT_INVERTED,
269     LANDSCAPE_INVERTED,
270     UNKNOWN,
271 };
272 
273 /**
274  * @brief Enumerates display change events.
275  */
276 enum class DisplayChangeEvent : uint32_t {
277     UPDATE_ORIENTATION,
278     UPDATE_ROTATION,
279     DISPLAY_SIZE_CHANGED,
280     DISPLAY_FREEZED,
281     DISPLAY_UNFREEZED,
282     DISPLAY_VIRTUAL_PIXEL_RATIO_CHANGED,
283     UPDATE_ORIENTATION_FROM_WINDOW,
284     UPDATE_ROTATION_FROM_WINDOW,
285     UPDATE_REFRESHRATE,
286     UNKNOWN,
287 };
288 
289 /**
290  * @brief Enumerates screen source mode.
291  */
292 enum class ScreenSourceMode: uint32_t {
293     SCREEN_MAIN = 0,
294     SCREEN_MIRROR = 1,
295     SCREEN_EXTEND = 2,
296     SCREEN_ALONE = 3,
297 };
298 
299 /**
300  * @brief Enumerates the fold status.
301  */
302 enum class FoldStatus: uint32_t {
303     UNKNOWN = 0,
304     EXPAND = 1,
305     FOLDED = 2,
306     HALF_FOLD = 3,
307 };
308 
309 /**
310  * @brief Enumerates the fold display mode.
311  */
312 enum class FoldDisplayMode: uint32_t {
313     UNKNOWN = 0,
314     FULL = 1,
315     MAIN = 2,
316     SUB = 3,
317     COORDINATION = 4,
318 };
319 
320 /**
321  * @brief displayRect
322  */
323 struct DMRect {
324     int32_t posX_;
325     int32_t posY_;
326     uint32_t width_;
327     uint32_t height_;
328 
329     bool operator==(const DMRect& a) const
330     {
331         return (posX_ == a.posX_ && posY_ == a.posY_ && width_ == a.width_ && height_ == a.height_);
332     }
333 
334     bool operator!=(const DMRect& a) const
335     {
336         return !this->operator==(a);
337     }
338 
IsUninitializedRectDMRect339     bool IsUninitializedRect() const
340     {
341         return (posX_ == 0 && posY_ == 0 && width_ == 0 && height_ == 0);
342     }
343 
IsInsideOfDMRect344     bool IsInsideOf(const DMRect& a) const
345     {
346         return (posX_ >= a.posX_ && posY_ >= a.posY_ &&
347             posX_ + width_ <= a.posX_ + a.width_ && posY_ + height_ <= a.posY_ + a.height_);
348     }
NONEDMRect349     static DMRect NONE()
350     {
351         return {0, 0, 0, 0};
352     }
353 };
354 }
355 }
356 #endif // OHOS_ROSEN_DM_COMMON_H
357