• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sstream>
21 #include <string>
22 #include <map>
23 
24 #ifdef _WIN32
25 #define WINDOW_EXPORT __attribute__((dllexport))
26 #else
27 #define WINDOW_EXPORT __attribute__((visibility("default")))
28 #endif
29 
30 namespace OHOS {
31 namespace Rosen {
32 using DisplayId = uint64_t;
33 using ScreenId = uint64_t;
34 namespace {
35 constexpr DisplayId DISPLAY_ID_INVALID = -1ULL;
36 constexpr ScreenId SCREEN_ID_INVALID = -1ULL;
37 constexpr int DOT_PER_INCH = 160;
38 const static std::string DEFAULT_SCREEN_NAME = "buildIn";
39 constexpr int DOT_PER_INCH_MAXIMUM_VALUE = 1000;
40 constexpr int DOT_PER_INCH_MINIMUM_VALUE = 80;
41 constexpr uint32_t BASELINE_DENSITY = 160;
42 }
43 
44 /**
45  * @brief Power state change reason.
46  */
47 enum class PowerStateChangeReason : uint32_t {
48     POWER_BUTTON = 0,
49     STATE_CHANGE_REASON_INIT = 0,
50     STATE_CHANGE_REASON_TIMEOUT = 1,
51     STATE_CHANGE_REASON_RUNNING_LOCK = 2,
52     STATE_CHANGE_REASON_BATTERY = 3,
53     STATE_CHANGE_REASON_THERMAL = 4,
54     STATE_CHANGE_REASON_WORK = 5,
55     STATE_CHANGE_REASON_SYSTEM = 6,
56     STATE_CHANGE_REASON_APPLICATION = 10,
57     STATE_CHANGE_REASON_SETTINGS = 11,
58     STATE_CHANGE_REASON_HARD_KEY = 12,
59     STATE_CHANGE_REASON_TOUCH = 13,
60     STATE_CHANGE_REASON_CABLE = 14,
61     STATE_CHANGE_REASON_SENSOR = 15,
62     STATE_CHANGE_REASON_LID = 16,
63     STATE_CHANGE_REASON_CAMERA = 17,
64     STATE_CHANGE_REASON_ACCESS = 18,
65     STATE_CHANGE_REASON_RESET = 19,
66     STATE_CHANGE_REASON_POWER_KEY = 20,
67     STATE_CHANGE_REASON_KEYBOARD = 21,
68     STATE_CHANGE_REASON_MOUSE = 22,
69     STATE_CHANGE_REASON_DOUBLE_CLICK = 23,
70     STATE_CHANGE_REASON_SWITCH = 25,
71     STATE_CHANGE_REASON_PRE_BRIGHT = 26,
72     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS = 27,
73     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON = 28,
74     STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF = 29,
75     STATE_CHANGE_REASON_DISPLAY_SWITCH = 30,
76     STATE_CHANGE_REASON_PROXIMITY = 32,
77     STATE_CHANGE_REASON_AOD_SLIDING = 40,
78     STATE_CHANGE_REASON_PEN = 41,
79     STATE_CHANGE_REASON_SHUT_DOWN = 42,
80     STATE_CHANGE_REASON_HIBERNATE = 45,
81     STATE_CHANGE_REASON_REMOTE = 100,
82     STATE_CHANGE_REASON_UNKNOWN = 1000,
83 };
84 
85 /**
86  * @brief Enumerates the state of the screen power.
87  */
88 enum class ScreenPowerState : uint32_t {
89     POWER_ON,
90     POWER_STAND_BY,
91     POWER_SUSPEND,
92     POWER_OFF,
93     POWER_BUTT,
94     INVALID_STATE,
95 };
96 
97 /**
98  * @brief Enumerates the state of the display.
99  */
100 enum class DisplayState : uint32_t {
101     UNKNOWN,
102     OFF,
103     ON,
104     DOZE,
105     DOZE_SUSPEND,
106     VR,
107     ON_SUSPEND,
108 };
109 
110 /**
111  * @brief Enumerates display events.
112  */
113 enum class DisplayEvent : uint32_t {
114     UNLOCK,
115     KEYGUARD_DRAWN,
116 };
117 
118 /**
119  * @brief Enumerates DMError.
120  */
121 enum class DMError : int32_t {
122     DM_OK = 0,
123     DM_ERROR_INIT_DMS_PROXY_LOCKED = 100,
124     DM_ERROR_IPC_FAILED = 101,
125     DM_ERROR_REMOTE_CREATE_FAILED = 110,
126     DM_ERROR_NULLPTR = 120,
127     DM_ERROR_INVALID_PARAM = 130,
128     DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED = 140,
129     DM_ERROR_DEATH_RECIPIENT = 150,
130     DM_ERROR_INVALID_MODE_ID = 160,
131     DM_ERROR_WRITE_DATA_FAILED = 170,
132     DM_ERROR_RENDER_SERVICE_FAILED = 180,
133     DM_ERROR_UNREGISTER_AGENT_FAILED = 190,
134     DM_ERROR_INVALID_CALLING = 200,
135     DM_ERROR_INVALID_PERMISSION = 201,
136     DM_ERROR_NOT_SYSTEM_APP = 202,
137     DM_ERROR_DEVICE_NOT_SUPPORT = 801,
138     DM_ERROR_UNKNOWN = -1,
139 };
140 
141 /**
142  * @brief Enumerates DM error codes.
143  */
144 enum class DmErrorCode : int32_t {
145     DM_OK = 0,
146     DM_ERROR_NO_PERMISSION = 201,
147     DM_ERROR_NOT_SYSTEM_APP = 202,
148     DM_ERROR_INVALID_PARAM = 401,
149     DM_ERROR_DEVICE_NOT_SUPPORT = 801,
150     DM_ERROR_INVALID_SCREEN = 1400001,
151     DM_ERROR_INVALID_CALLING = 1400002,
152     DM_ERROR_SYSTEM_INNORMAL = 1400003,
153 };
154 
155 /**
156  * @brief Constructs the mapping of the DM errors to the DM error codes.
157  */
158 const std::map<DMError, DmErrorCode> DM_JS_TO_ERROR_CODE_MAP {
159     {DMError::DM_OK,                                    DmErrorCode::DM_OK                          },
160     {DMError::DM_ERROR_INVALID_PERMISSION,              DmErrorCode::DM_ERROR_NO_PERMISSION         },
161     {DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED,           DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
162     {DMError::DM_ERROR_IPC_FAILED,                      DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
163     {DMError::DM_ERROR_REMOTE_CREATE_FAILED,            DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
164     {DMError::DM_ERROR_NULLPTR,                         DmErrorCode::DM_ERROR_INVALID_SCREEN        },
165     {DMError::DM_ERROR_INVALID_PARAM,                   DmErrorCode::DM_ERROR_INVALID_PARAM         },
166     {DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED,    DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
167     {DMError::DM_ERROR_DEATH_RECIPIENT,                 DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
168     {DMError::DM_ERROR_INVALID_MODE_ID,                 DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
169     {DMError::DM_ERROR_WRITE_DATA_FAILED,               DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
170     {DMError::DM_ERROR_RENDER_SERVICE_FAILED,           DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
171     {DMError::DM_ERROR_UNREGISTER_AGENT_FAILED,         DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
172     {DMError::DM_ERROR_INVALID_CALLING,                 DmErrorCode::DM_ERROR_INVALID_CALLING       },
173     {DMError::DM_ERROR_NOT_SYSTEM_APP,                  DmErrorCode::DM_ERROR_NOT_SYSTEM_APP        },
174     {DMError::DM_ERROR_UNKNOWN,                         DmErrorCode::DM_ERROR_SYSTEM_INNORMAL       },
175 };
176 
177 using DisplayStateCallback = std::function<void(DisplayState)>;
178 
179 /**
180  * @brief Enumerates display power events.
181  */
182 enum class DisplayPowerEvent : uint32_t {
183     WAKE_UP,
184     SLEEP,
185     DISPLAY_ON,
186     DISPLAY_OFF,
187     DESKTOP_READY,
188     DOZE,
189     DOZE_SUSPEND,
190 };
191 
192 /**
193  * @brief Enumerates event status.
194  */
195 enum class EventStatus : uint32_t {
196     BEGIN,
197     END,
198 };
199 
200 class IDisplayPowerEventListener : public RefBase {
201 public:
202     /**
203      * @brief Notify when display power event status changed.
204      */
205     virtual void OnDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) = 0;
206 };
207 
208 /**
209  * @brief Enumerates screen change events.
210  */
211 enum class ScreenChangeEvent : uint32_t {
212     UPDATE_ORIENTATION,
213     UPDATE_ROTATION,
214     CHANGE_MODE,
215     VIRTUAL_PIXEL_RATIO_CHANGED,
216 };
217 
218 /**
219  * @brief Enumerates screen group change events.
220  */
221 enum class ScreenGroupChangeEvent : uint32_t {
222     ADD_TO_GROUP,
223     REMOVE_FROM_GROUP,
224     CHANGE_GROUP,
225 };
226 
227 /**
228  * @brief Enumerates rotations.
229  */
230 enum class Rotation : uint32_t {
231     ROTATION_0,
232     ROTATION_90,
233     ROTATION_180,
234     ROTATION_270,
235 };
236 
237 /**
238  * @brief Enumerates orientations.
239  */
240 enum class Orientation : uint32_t {
241     BEGIN = 0,
242     UNSPECIFIED = BEGIN,
243     VERTICAL = 1,
244     HORIZONTAL = 2,
245     REVERSE_VERTICAL = 3,
246     REVERSE_HORIZONTAL = 4,
247     SENSOR = 5,
248     SENSOR_VERTICAL = 6,
249     SENSOR_HORIZONTAL = 7,
250     AUTO_ROTATION_RESTRICTED = 8,
251     AUTO_ROTATION_PORTRAIT_RESTRICTED = 9,
252     AUTO_ROTATION_LANDSCAPE_RESTRICTED = 10,
253     LOCKED = 11,
254     FOLLOW_RECENT = 12,
255     AUTO_ROTATION_UNSPECIFIED = 13,
256     USER_ROTATION_PORTRAIT = 14,
257     USER_ROTATION_LANDSCAPE = 15,
258     USER_ROTATION_PORTRAIT_INVERTED = 16,
259     USER_ROTATION_LANDSCAPE_INVERTED = 17,
260     FOLLOW_DESKTOP = 18,
261     END = FOLLOW_DESKTOP,
262     USER_PAGE_ROTATION_PORTRAIT = 3000,
263     USER_PAGE_ROTATION_LANDSCAPE = 3001,
264     USER_PAGE_ROTATION_PORTRAIT_INVERTED = 3002,
265     USER_PAGE_ROTATION_LANDSCAPE_INVERTED = 3003,
266     INVALID = 3004,
267 };
268 
269 /**
270  * @brief Enumerates display orientations.
271  */
272 enum class DisplayOrientation : uint32_t {
273     PORTRAIT = 0,
274     LANDSCAPE,
275     PORTRAIT_INVERTED,
276     LANDSCAPE_INVERTED,
277     UNKNOWN,
278 };
279 
280 /**
281  * @brief Enumerates display change events.
282  */
283 enum class DisplayChangeEvent : uint32_t {
284     UPDATE_ORIENTATION,
285     UPDATE_ROTATION,
286     DISPLAY_SIZE_CHANGED,
287     DISPLAY_FREEZED,
288     DISPLAY_UNFREEZED,
289     DISPLAY_VIRTUAL_PIXEL_RATIO_CHANGED,
290     UPDATE_ORIENTATION_FROM_WINDOW,
291     UPDATE_ROTATION_FROM_WINDOW,
292     UPDATE_REFRESHRATE,
293     UNKNOWN,
294 };
295 
296 /**
297  * @brief Enumerates screen source mode.
298  */
299 enum class ScreenSourceMode: uint32_t {
300     SCREEN_MAIN = 0,
301     SCREEN_MIRROR = 1,
302     SCREEN_EXTEND = 2,
303     SCREEN_ALONE = 3,
304 };
305 
306 /**
307  * @brief Enumerates the fold status.
308  */
309 enum class FoldStatus: uint32_t {
310     UNKNOWN = 0,
311     EXPAND = 1,
312     FOLDED = 2,
313     HALF_FOLD = 3,
314 };
315 
316 /**
317  * @brief Enumerates the fold display mode.
318  */
319 enum class FoldDisplayMode: uint32_t {
320     UNKNOWN = 0,
321     FULL = 1,
322     MAIN = 2,
323     SUB = 3,
324     COORDINATION = 4,
325 };
326 
327 /**
328  * @brief displayRect
329  */
330 struct DMRect {
331     int32_t posX_;
332     int32_t posY_;
333     uint32_t width_;
334     uint32_t height_;
335 
336     bool operator==(const DMRect& a) const
337     {
338         return (posX_ == a.posX_ && posY_ == a.posY_ && width_ == a.width_ && height_ == a.height_);
339     }
340 
341     bool operator!=(const DMRect& a) const
342     {
343         return !this->operator==(a);
344     }
345 
IsUninitializedRectDMRect346     bool IsUninitializedRect() const
347     {
348         return (posX_ == 0 && posY_ == 0 && width_ == 0 && height_ == 0);
349     }
350 
IsInsideOfDMRect351     bool IsInsideOf(const DMRect& a) const
352     {
353         return (posX_ >= a.posX_ && posY_ >= a.posY_ &&
354             posX_ + width_ <= a.posX_ + a.width_ && posY_ + height_ <= a.posY_ + a.height_);
355     }
NONEDMRect356     static DMRect NONE()
357     {
358         return {0, 0, 0, 0};
359     }
360 };
361 
362 /**
363  * @struct Position
364  *
365  * @brief Coordinate of points on the screen
366  */
367 struct Position {
368     int32_t x = 0;
369     int32_t y = 0;
370 
371     bool operator==(const Position& other) const
372     {
373         return x == other.x && y == other.y;
374     }
375 
376     bool operator!=(const Position& other) const
377     {
378         return !(*this == other);
379     }
380 
SafeAddPosition381     bool SafeAdd(const Position& other, Position& result) const
382     {
383         return SafeOp(other, std::plus<int64_t>(), result);
384     }
385 
SafeSubPosition386     bool SafeSub(const Position& other, Position& result) const
387     {
388         return SafeOp(other, std::minus<int64_t>(), result);
389     }
390 
ToStringPosition391     inline std::string ToString() const
392     {
393         std::ostringstream oss;
394         oss << "[" << x << ", " << y << "]";
395         return oss.str();
396     }
397 
398 private:
399     template<typename Op>
SafeOpPosition400     bool SafeOp(const Position& other, Op op, Position& result) const
401     {
402         int64_t newX = op(static_cast<int64_t>(x), static_cast<int64_t>(other.x));
403         int64_t newY = op(static_cast<int64_t>(y), static_cast<int64_t>(other.y));
404         if (newX < INT32_MIN || newX > INT32_MAX || newY < INT32_MIN || newY > INT32_MAX) {
405             return false;
406         }
407         result.x = static_cast<int32_t>(newX);
408         result.y = static_cast<int32_t>(newY);
409         return true;
410     }
411 };
412 }
413 }
414 #endif // OHOS_ROSEN_DM_COMMON_H
415