• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include <securec.h>
17 
18 #include <shared_mutex>
19 
20 #include "display.h"
21 #include "display_info.h"
22 #include "display_manager.h"
23 #include "dm_common.h"
24 #include "oh_display_info.h"
25 #include "oh_display_manager.h"
26 #include "window_manager_hilog.h"
27 
28 using namespace OHOS;
29 using namespace Rosen;
30 
31 class OH_DisplayModeChangeListener : public DisplayManager::IDisplayModeListener {
32 private:
33     OH_NativeDisplayManager_FoldDisplayModeChangeCallback innerDisplayModeChangeFunc_;
34 public:
OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)35     explicit OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)
36     {
37         innerDisplayModeChangeFunc_ = displayModeChangeFunc;
38     }
OnDisplayModeChanged(FoldDisplayMode displayMode)39     void OnDisplayModeChanged(FoldDisplayMode displayMode)
40     {
41         if (innerDisplayModeChangeFunc_ == NULL) {
42             TLOGI(WmsLogTag::DMS, "[DMNDK] OnDisplayModeChanged callback is null");
43             return;
44         }
45         TLOGI(WmsLogTag::DMS, "[DMNDK] displayMode callback displayMode=%{public}d", displayMode);
46         innerDisplayModeChangeFunc_(static_cast<NativeDisplayManager_FoldDisplayMode>(displayMode));
47     }
48 };
49 
50 class OH_DisplayChangeListener : public DisplayManager::IDisplayListener {
51 private:
52     OH_NativeDisplayManager_DisplayChangeCallback innerDisplayChangeFunc_;
53 public:
OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)54     explicit OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)
55     {
56         innerDisplayChangeFunc_ = displayChangeFunc;
57     }
OnCreate(DisplayId)58     void OnCreate(DisplayId)
59     {
60         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support create callback.");
61     }
OnDestroy(DisplayId)62     void OnDestroy(DisplayId)
63     {
64         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support delete callback.");
65     }
OnChange(DisplayId displayId)66     void OnChange(DisplayId displayId)
67     {
68         if (innerDisplayChangeFunc_ == NULL) {
69             TLOGI(WmsLogTag::DMS, "[DMNDK] OnChange callback is null");
70             return;
71         }
72         TLOGI(WmsLogTag::DMS, "[DMNDK] OnChange callback displayId=%{public}" PRIu64, displayId);
73         innerDisplayChangeFunc_(static_cast<uint64_t>(displayId));
74         sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplaySync();
75         if (display != nullptr) {
76             TLOGI(WmsLogTag::DMS, "[DMNDK] OnChange callback rotation=%{public}d orientation=%{public}d",
77                 display->GetRotation(), display->GetOrientation());
78         }
79     }
80 };
81 
OH_GetDefaultDisplayInfo()82 static sptr<DisplayInfo> OH_GetDefaultDisplayInfo()
83 {
84     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
85     if (defaultDisplay == nullptr) {
86         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display null.");
87         return nullptr;
88     }
89     auto info = defaultDisplay->GetDisplayInfo();
90     if (info == nullptr) {
91         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
92         return nullptr;
93     }
94     return info;
95 }
96 
OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t * displayId)97 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t *displayId)
98 {
99     if (displayId == nullptr) {
100         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayId null.");
101         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
102     }
103     auto displayInfo = OH_GetDefaultDisplayInfo();
104     if (displayInfo == nullptr) {
105         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
106         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
107     }
108     *displayId = displayInfo->GetDisplayId();
109     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
110 }
111 
OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t * displayWidth)112 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t *displayWidth)
113 {
114     if (displayWidth == nullptr) {
115         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayWidth null.");
116         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
117     }
118     auto displayInfo = OH_GetDefaultDisplayInfo();
119     if (displayInfo == nullptr) {
120         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
121         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
122     }
123     *displayWidth = displayInfo->GetWidth();
124     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
125 }
126 
OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t * displayHeight)127 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t *displayHeight)
128 {
129     if (displayHeight == nullptr) {
130         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayHeight null.");
131         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
132     }
133     auto displayInfo = OH_GetDefaultDisplayInfo();
134     if (displayInfo == nullptr) {
135         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
136         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
137     }
138     *displayHeight = displayInfo->GetHeight();
139     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
140 }
141 
OH_NativeDisplayManager_GetDefaultDisplayRotation(NativeDisplayManager_Rotation * displayRotation)142 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRotation(
143     NativeDisplayManager_Rotation *displayRotation)
144 {
145     if (displayRotation == nullptr) {
146         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayRotation null.");
147         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
148     }
149     auto displayInfo = OH_GetDefaultDisplayInfo();
150     if (displayInfo == nullptr) {
151         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
152         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
153     }
154     *displayRotation = static_cast<NativeDisplayManager_Rotation>(displayInfo->GetRotation());
155     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
156 }
157 
OH_NativeDisplayManager_GetDefaultDisplayOrientation(NativeDisplayManager_Orientation * displayOrientation)158 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayOrientation(
159     NativeDisplayManager_Orientation *displayOrientation)
160 {
161     if (displayOrientation == nullptr) {
162         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayOrientation null.");
163         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
164     }
165     auto displayInfo = OH_GetDefaultDisplayInfo();
166     if (displayInfo == nullptr) {
167         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
168         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
169     }
170     *displayOrientation = static_cast<NativeDisplayManager_Orientation>(displayInfo->GetDisplayOrientation());
171     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
172 }
173 
OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float * virtualPixel)174 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float *virtualPixel)
175 {
176     if (virtualPixel == nullptr) {
177         TLOGE(WmsLogTag::DMS, "[DMNDK] input virtualPixel null.");
178         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
179     }
180     auto displayInfo = OH_GetDefaultDisplayInfo();
181     if (displayInfo == nullptr) {
182         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
183         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
184     }
185     *virtualPixel = displayInfo->GetVirtualPixelRatio();
186     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
187 }
188 
OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t * refreshRate)189 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t *refreshRate)
190 {
191     if (refreshRate == nullptr) {
192         TLOGE(WmsLogTag::DMS, "[DMNDK] input refreshRate null.");
193         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
194     }
195     auto displayInfo = OH_GetDefaultDisplayInfo();
196     if (displayInfo == nullptr) {
197         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
198         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
199     }
200     *refreshRate = displayInfo->GetRefreshRate();
201     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
202 }
203 
OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t * densityDpi)204 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t *densityDpi)
205 {
206     if (densityDpi == nullptr) {
207         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityDpi null.");
208         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
209     }
210     auto displayInfo = OH_GetDefaultDisplayInfo();
211     if (displayInfo == nullptr) {
212         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
213         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
214     }
215     *densityDpi = displayInfo->GetVirtualPixelRatio() * DOT_PER_INCH;
216     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
217 }
218 
OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float * densityPixels)219 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float *densityPixels)
220 {
221     if (densityPixels == nullptr) {
222         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityPixels null.");
223         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
224     }
225     auto displayInfo = OH_GetDefaultDisplayInfo();
226     if (displayInfo == nullptr) {
227         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
228         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
229     }
230     *densityPixels = displayInfo->GetVirtualPixelRatio();
231     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
232 }
233 
OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float * scaledDensity)234 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float *scaledDensity)
235 {
236     if (scaledDensity == nullptr) {
237         TLOGE(WmsLogTag::DMS, "[DMNDK] input scaledDensity null.");
238         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
239     }
240     auto displayInfo = OH_GetDefaultDisplayInfo();
241     if (displayInfo == nullptr) {
242         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
243         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
244     }
245     *scaledDensity = displayInfo->GetVirtualPixelRatio();
246     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
247 }
248 
OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float * xDpi)249 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float *xDpi)
250 {
251     if (xDpi == nullptr) {
252         TLOGE(WmsLogTag::DMS, "[DMNDK] input xDpi null.");
253         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
254     }
255     auto displayInfo = OH_GetDefaultDisplayInfo();
256     if (displayInfo == nullptr) {
257         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
258         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
259     }
260     *xDpi = displayInfo->GetXDpi();
261     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
262 }
263 
OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float * yDpi)264 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float *yDpi)
265 {
266     if (yDpi == nullptr) {
267         TLOGE(WmsLogTag::DMS, "[DMNDK] input yDpi null.");
268         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
269     }
270     auto displayInfo = OH_GetDefaultDisplayInfo();
271     if (displayInfo == nullptr) {
272         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
273         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
274     }
275     *yDpi = displayInfo->GetYDpi();
276     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
277 }
278 
OH_SetDisplayRect(const DMRect & dmRect,NativeDisplayManager_Rect * displayRectItem)279 static void OH_SetDisplayRect(const DMRect &dmRect, NativeDisplayManager_Rect *displayRectItem)
280 {
281     displayRectItem->left = dmRect.posX_;
282     displayRectItem->top = dmRect.posY_;
283     displayRectItem->width = dmRect.width_;
284     displayRectItem->height = dmRect.height_;
285 }
286 
OH_CreateBoundingRects(const std::vector<DMRect> & boundingRects)287 static NativeDisplayManager_Rect* OH_CreateBoundingRects(const std::vector<DMRect> &boundingRects)
288 {
289     int32_t boundSize = static_cast<int32_t>(boundingRects.size());
290     NativeDisplayManager_Rect *ohBoundingRects =
291         static_cast<NativeDisplayManager_Rect*>(malloc(sizeof(NativeDisplayManager_Rect) * boundSize));
292     if (ohBoundingRects == NULL) {
293         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
294         return NULL;
295     }
296     auto retMemset = memset_s(ohBoundingRects, sizeof(NativeDisplayManager_Rect) * boundSize, 0,
297         sizeof(NativeDisplayManager_Rect) * boundSize);
298     if (retMemset != 0) {
299         free(ohBoundingRects);
300         ohBoundingRects = NULL;
301         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
302         return NULL;
303     }
304     TLOGI(WmsLogTag::DMS, "[DMNDK] bounding size:%{public}d.", boundSize);
305     for (int i = 0; i < boundSize; i++) {
306         OH_SetDisplayRect(boundingRects[i], (ohBoundingRects + i));
307     }
308     return ohBoundingRects;
309 }
310 
OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects & waterArea,NativeDisplayManager_CutoutInfo * ohCutoutInfo)311 static void OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects &waterArea,
312     NativeDisplayManager_CutoutInfo *ohCutoutInfo)
313 {
314     if (ohCutoutInfo == NULL) {
315         TLOGE(WmsLogTag::DMS, "[DMNDK] ohCutoutInfo is null.");
316         return;
317     }
318     TLOGI(WmsLogTag::DMS, "[DMNDK] set waterfall Area.");
319     OH_SetDisplayRect(waterArea.left, &(ohCutoutInfo->waterfallDisplayAreaRects.left));
320     OH_SetDisplayRect(waterArea.top, &(ohCutoutInfo->waterfallDisplayAreaRects.top));
321     OH_SetDisplayRect(waterArea.right, &(ohCutoutInfo->waterfallDisplayAreaRects.right));
322     OH_SetDisplayRect(waterArea.bottom, &(ohCutoutInfo->waterfallDisplayAreaRects.bottom));
323 }
324 
OH_CreateCutoutInfoObject(const sptr<CutoutInfo> & cutoutInfo)325 static NativeDisplayManager_CutoutInfo* OH_CreateCutoutInfoObject(const sptr<CutoutInfo> &cutoutInfo)
326 {
327     NativeDisplayManager_CutoutInfo *ohCutoutInfo =
328         static_cast<NativeDisplayManager_CutoutInfo*>(malloc(sizeof(NativeDisplayManager_CutoutInfo)));
329     if (ohCutoutInfo == NULL) {
330         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
331         return NULL;
332     }
333     auto retMemset = memset_s(ohCutoutInfo, sizeof(NativeDisplayManager_CutoutInfo), 0,
334         sizeof(NativeDisplayManager_CutoutInfo));
335     if (retMemset != 0) {
336         free(ohCutoutInfo);
337         ohCutoutInfo = NULL;
338         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
339         return NULL;
340     }
341     std::vector<DMRect> boundingRects = cutoutInfo->GetBoundingRects();
342     WaterfallDisplayAreaRects waterRects = cutoutInfo->GetWaterfallDisplayAreaRects();
343     ohCutoutInfo->boundingRectsLength = static_cast<int32_t>(boundingRects.size());
344     TLOGI(WmsLogTag::DMS, "[DMNDK] boundingRectsLength=%{public}d.", ohCutoutInfo->boundingRectsLength);
345     ohCutoutInfo->boundingRects = OH_CreateBoundingRects(boundingRects);
346     if (ohCutoutInfo->boundingRects == NULL) {
347         TLOGE(WmsLogTag::DMS, "[DMNDK] create bounding rects failed.");
348         free(ohCutoutInfo);
349         ohCutoutInfo = NULL;
350         return NULL;
351     }
352     OH_SetWaterfallDisplayAreaRects(waterRects, ohCutoutInfo);
353     return ohCutoutInfo;
354 }
355 
OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo ** cutoutInfo)356 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(
357     NativeDisplayManager_CutoutInfo **cutoutInfo)
358 {
359     if (cutoutInfo == NULL) {
360         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null.");
361         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
362     }
363     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info.");
364     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
365     if (defaultDisplay == nullptr) {
366         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (display) null.");
367         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
368     }
369     sptr<CutoutInfo> cutoutInfoInner = defaultDisplay->GetCutoutInfo();
370     if (cutoutInfoInner == nullptr) {
371         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (from display) null.");
372         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
373     }
374     *cutoutInfo = OH_CreateCutoutInfoObject(cutoutInfoInner);
375     if (*cutoutInfo == NULL) {
376         TLOGE(WmsLogTag::DMS, "[DMNDK] convert cutout info null.");
377         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
378     }
379     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info success.");
380     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
381 }
382 
OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo * cutoutInfo)383 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(
384     NativeDisplayManager_CutoutInfo *cutoutInfo)
385 {
386     if (cutoutInfo == NULL) {
387         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null pointer.");
388         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
389     }
390     if (cutoutInfo->boundingRects != NULL) {
391         free(cutoutInfo->boundingRects);
392         cutoutInfo->boundingRects = NULL;
393     }
394     free(cutoutInfo);
395     cutoutInfo = NULL;
396     TLOGI(WmsLogTag::DMS, "[DMNDK] destroy cutoutInfo end.");
397     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
398 }
399 
OH_NativeDisplayManager_IsFoldable()400 bool OH_NativeDisplayManager_IsFoldable()
401 {
402     bool isFoldable = DisplayManager::GetInstance().IsFoldable();
403     TLOGI(WmsLogTag::DMS, "[DMNDK] get display isFoldable=%{public}d.", isFoldable);
404     return isFoldable;
405 }
406 
OH_NativeDisplayManager_GetFoldDisplayMode(NativeDisplayManager_FoldDisplayMode * foldDisplayMode)407 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetFoldDisplayMode(
408     NativeDisplayManager_FoldDisplayMode *foldDisplayMode)
409 {
410     TLOGI(WmsLogTag::DMS, "[DMNDK] get fold display mode.");
411     if (foldDisplayMode == NULL) {
412         TLOGE(WmsLogTag::DMS, "[DMNDK] input foldDisplayMode null.");
413         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
414     }
415     if (!DisplayManager::GetInstance().IsFoldable()) {
416         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
417         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
418     }
419     FoldDisplayMode foldMode = DisplayManager::GetInstance().GetFoldDisplayMode();
420     switch (foldMode) {
421         case FoldDisplayMode::SUB:
422             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_SUB;
423             break;
424         case FoldDisplayMode::MAIN:
425             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_MAIN;
426             break;
427         case FoldDisplayMode::FULL:
428             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_FULL;
429             break;
430         case FoldDisplayMode::COORDINATION:
431             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_COORDINATION;
432             break;
433         default:
434             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_UNKNOWN;
435             break;
436     }
437     TLOGI(WmsLogTag::DMS, "[DMNDK] current fold display mode: %{public}d.", *foldDisplayMode);
438     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
439 }
440 
441 std::shared_mutex foldChangeMutex;
442 std::map<uint32_t, OH_NativeDisplayManager_FoldDisplayModeChangeCallback> g_foldChangeCallbackMap;
443 std::map<uint32_t, sptr<DisplayManager::IDisplayModeListener>> g_foldDisplayModeChangeListenerMap;
444 
CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)445 bool CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)
446 {
447     if (g_foldChangeCallbackMap.empty()) {
448         return false;
449     }
450     for (auto iter : g_foldChangeCallbackMap) {
451         if (iter.second == displayModeChangeCallback) {
452             return true;
453         }
454     }
455     return false;
456 }
457 
OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback,uint32_t * listenerIndex)458 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(
459     OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback, uint32_t *listenerIndex)
460 {
461     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold display mode change listener.");
462     if (displayModeChangeCallback == NULL || listenerIndex == NULL) {
463         TLOGE(WmsLogTag::DMS, "[DMNDK] input params null.");
464         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
465     }
466     if (!DisplayManager::GetInstance().IsFoldable()) {
467         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
468         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
469     }
470     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
471     if (CheckFoldChangeHasRegistered(displayModeChangeCallback)) {
472         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
473         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
474     }
475     sptr<DisplayManager::IDisplayModeListener> displayModeListener =
476         sptr<OH_DisplayModeChangeListener>::MakeSptr(displayModeChangeCallback);
477     if (displayModeListener == nullptr) {
478         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener MakeSptr fail.");
479         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
480     }
481     static std::atomic<uint32_t> registerCount = 1;
482     DMError ret = DisplayManager::GetInstance().RegisterDisplayModeListener(displayModeListener);
483     if (ret != DMError::DM_OK) {
484         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener register failed ret=%{public}d.", ret);
485         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
486     }
487     *listenerIndex = registerCount++;
488     g_foldChangeCallbackMap.emplace(*listenerIndex, displayModeChangeCallback);
489     g_foldDisplayModeChangeListenerMap.emplace(*listenerIndex, displayModeListener);
490     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold change success and listenerIndex= %{public}d.", *listenerIndex);
491     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
492 }
493 
OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)494 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)
495 {
496     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister fold display mode change listener %{public}d.", listenerIndex);
497     if (!DisplayManager::GetInstance().IsFoldable()) {
498         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(device is not foldable).");
499         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
500     }
501     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
502     auto iter = g_foldDisplayModeChangeListenerMap.find(listenerIndex);
503     if (iter == g_foldDisplayModeChangeListenerMap.end()) {
504         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fold change listener fail(not find register info).");
505         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
506     }
507     DMError ret = DMError::DM_OK;
508     if (iter->second != nullptr) {
509         ret = DisplayManager::GetInstance().UnregisterDisplayModeListener(iter->second);
510         g_foldDisplayModeChangeListenerMap.erase(listenerIndex);
511         g_foldChangeCallbackMap.erase(listenerIndex);
512         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister fold change listener ert=%{public}d", ret);
513     }
514     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
515         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
516 }
517 
518 std::shared_mutex displayChangeMutex;
519 std::map<uint32_t, OH_NativeDisplayManager_DisplayChangeCallback> g_displayChangeCallbackMap;
520 std::map<uint32_t, sptr<DisplayManager::IDisplayListener>> g_displayChangeListenerMap;
521 
CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)522 bool CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)
523 {
524     if (g_displayChangeCallbackMap.empty()) {
525         return false;
526     }
527     for (auto iter : g_displayChangeCallbackMap) {
528         if (iter.second == displayChangeCallback) {
529             return true;
530         }
531     }
532     return false;
533 }
534 
OH_NativeDisplayManager_RegisterDisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback,uint32_t * listenerIndex)535 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterDisplayChangeListener(
536     OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback, uint32_t *listenerIndex)
537 {
538     TLOGI(WmsLogTag::DMS, "[DMNDK] register display change listener.");
539     if (displayChangeCallback == NULL || listenerIndex == NULL) {
540         TLOGE(WmsLogTag::DMS, "[DMNDK] register display change listener fail(input params null).");
541         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
542     }
543     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
544     if (CheckDisplayChangeHasRegistered(displayChangeCallback)) {
545         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
546         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
547     }
548     sptr<DisplayManager::IDisplayListener> displayListener =
549         sptr<OH_DisplayChangeListener>::MakeSptr(displayChangeCallback);
550     if (displayListener == nullptr) {
551         TLOGE(WmsLogTag::DMS, "[DMNDK] register display change MakeSptr fail.");
552         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
553     }
554     static std::atomic<uint32_t> registerCount = 1;
555     DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(displayListener);
556     if (ret != DMError::DM_OK) {
557         TLOGE(WmsLogTag::DMS, "[DMNDK] display change listener register failed ret=%{public}d.", ret);
558         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
559     }
560     *listenerIndex = registerCount++;
561     g_displayChangeCallbackMap.emplace(*listenerIndex, displayChangeCallback);
562     g_displayChangeListenerMap.emplace(*listenerIndex, displayListener);
563     TLOGI(WmsLogTag::DMS, "[DMNDK] register display change success and listenerIndex= %{public}d.", *listenerIndex);
564     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
565 }
566 
OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)567 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)
568 {
569     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister display change listener %{public}d.", listenerIndex);
570     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
571     auto iter = g_displayChangeListenerMap.find(listenerIndex);
572     if (iter == g_displayChangeListenerMap.end()) {
573         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister display change listener fail(not find register info).");
574         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
575     }
576     DMError ret = DMError::DM_OK;
577     if (iter->second != nullptr) {
578         ret = DisplayManager::GetInstance().UnregisterDisplayListener(iter->second);
579         g_displayChangeListenerMap.erase(listenerIndex);
580         g_displayChangeCallbackMap.erase(listenerIndex);
581         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister display change listener ert=%{public}d", ret);
582     }
583     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
584         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
585 }
586