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 "cj_display_manager.h"
17 #include <securec.h>
18 #include "cj_display_impl.h"
19 #include "display_manager.h"
20 #include "dm_common.h"
21 #include "singleton_container.h"
22 #include "window_manager_hilog.h"
23 
24 namespace OHOS {
25 namespace Rosen {
SetDisplayObject(sptr<Display> & obj,RetStruct & ret)26 static void SetDisplayObject(sptr<Display> &obj, RetStruct &ret)
27 {
28     auto result = DisplayImpl::CreateDisplayImpl(obj);
29     if (result == nullptr || ret.data == nullptr) {
30         TLOGE(WmsLogTag::DMS, "[GetDefaultDisplaySync] ERROR Failed to create DisplayImpl.");
31         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
32         return;
33     }
34     int64_t *dataPtr = static_cast<int64_t*>(ret.data);
35     if (dataPtr == nullptr) {
36         TLOGE(WmsLogTag::DMS, "[SetDisplayObject] ERROR Failed to create dataPtr.");
37         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
38         return;
39     }
40     dataPtr[ret.len] = result->GetID();
41     ret.len += 1;
42     ret.code = static_cast<int32_t>(DmErrorCode::DM_OK);
43 }
44 
SetDisplaysArrayObject(std::vector<sptr<Display>> & list,RetStruct & ret)45 static void SetDisplaysArrayObject(std::vector<sptr<Display>> &list, RetStruct &ret)
46 {
47     int64_t *displayImplIdList = static_cast<int64_t*>(malloc(sizeof(int64_t) * list.size()));
48     if (displayImplIdList == nullptr) {
49         TLOGE(WmsLogTag::DMS, "[SetDisplaysArrayObject] ERROR Failed to create displayImplIdList.");
50         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
51         return;
52     }
53     ret.data = displayImplIdList;
54     ret.len = 0;
55     for (auto& display : list) {
56         if (display == nullptr) {
57             continue;
58         }
59         SetDisplayObject(display, ret);
60         if (ret.code != 0) {
61             TLOGE(WmsLogTag::DMS,
62                 "[SetDisplaysArrayObject] ERROR Create display failed in %{public}" PRId64" obj", ret.len);
63             free(displayImplIdList);
64             ret.data = nullptr;
65             return;
66         }
67     }
68 }
69 
SetCRect(const DMRect & row,CRect * ptr)70 static void SetCRect(const DMRect &row, CRect *ptr)
71 {
72     ptr->left = row.posX_;
73     ptr->top = row.posY_;
74     ptr->width = row.width_;
75     ptr->height = row.height_;
76 }
77 
CreateCreaseRects(std::vector<DMRect> & list)78 static CRect* CreateCreaseRects(std::vector<DMRect> &list)
79 {
80     int32_t number = static_cast<int32_t>(list.size());
81     CRect *result = static_cast<CRect*>(malloc(sizeof(CRect) * number));
82     if (result == nullptr) {
83         return nullptr;
84     }
85     for (int i = 0; i < number; i++) {
86         SetCRect(list[i], (result + i));
87     }
88     return result;
89 }
90 
CreateCFoldCreaseRegionObject(sptr<FoldCreaseRegion> & foldCreaseRegion)91 static CFoldCreaseRegion* CreateCFoldCreaseRegionObject(sptr<FoldCreaseRegion> &foldCreaseRegion)
92 {
93     CFoldCreaseRegion *region = static_cast<CFoldCreaseRegion*>(malloc(sizeof(CFoldCreaseRegion)));
94     if (region == nullptr) {
95         return nullptr;
96     }
97     int ret = memset_s(region, sizeof(CFoldCreaseRegion), 0, sizeof(CFoldCreaseRegion));
98     if (ret != 0) {
99         free(region);
100         return nullptr;
101     }
102     region->displayId = static_cast<uint32_t>(foldCreaseRegion->GetDisplayId());
103     std::vector<DMRect> creaseRects = foldCreaseRegion->GetCreaseRects();
104 
105     region->number = static_cast<int64_t>(creaseRects.size());
106     region->creaseRects = CreateCreaseRects(creaseRects);
107     if (region->creaseRects == nullptr) {
108         TLOGE(WmsLogTag::DMS, "[CreateCreaseRects] ERROR Failed to create creaseRects.");
109         free(region);
110         return nullptr;
111     }
112     return region;
113 }
114 
GetDefaultDisplaySync()115 RetStruct CJDisplayManager::GetDefaultDisplaySync()
116 {
117     RetStruct ret = {.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL), .len = 0, .data = nullptr};
118     sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplaySync();
119     if (display == nullptr) {
120         TLOGE(WmsLogTag::DMS, "[DisplayManager] Get default display is nullptr");
121         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN);
122         return ret;
123     }
124     int64_t *displayImplId = static_cast<int64_t*>(malloc(sizeof(int64_t)));
125     if (displayImplId == nullptr) {
126         TLOGE(WmsLogTag::DMS, "[GetDefaultDisplaySync] ERROR Failed to create displayImplId.");
127         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN);
128         return ret;
129     }
130     ret.data = displayImplId;
131     ret.len = 0;
132     SetDisplayObject(display, ret);
133     return ret;
134 }
135 
GetAllDisplays()136 RetStruct CJDisplayManager::GetAllDisplays()
137 {
138     RetStruct ret = {.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL), .len = 0, .data = nullptr};
139     std::vector<sptr<Display>> displays = SingletonContainer::Get<DisplayManager>().GetAllDisplays();
140     if (displays.empty()) {
141         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN);
142         TLOGE(WmsLogTag::DMS, "[GetAllDisplays] ERROR Failed to get all displays.");
143         return ret;
144     }
145     SetDisplaysArrayObject(displays, ret);
146     return ret;
147 }
148 
HasPrivateWindow(uint32_t displayId)149 RetStruct CJDisplayManager::HasPrivateWindow(uint32_t displayId)
150 {
151     RetStruct ret = {.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL), .len = 0, .data = nullptr};
152     int64_t displayId_ = static_cast<int64_t>(displayId);
153     bool *hasPrivateWindow = static_cast<bool*>(malloc(sizeof(bool)));
154     if (hasPrivateWindow == nullptr) {
155         ret.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
156         return ret;
157     }
158     *hasPrivateWindow = false;
159     DmErrorCode errorCode = DM_JS_TO_ERROR_CODE_MAP.at(
160         SingletonContainer::Get<DisplayManager>().HasPrivateWindow(displayId_, *hasPrivateWindow));
161     TLOGI(WmsLogTag::DMS,
162         "[DisplayManager] Display id = %{public}" PRIu64", hasPrivateWindow = %{public}u err = %{public}d",
163          static_cast<uint64_t>(displayId_), *hasPrivateWindow, errorCode);
164     if (errorCode != DmErrorCode::DM_OK) {
165         ret.code = static_cast<int32_t>(errorCode);
166         free(hasPrivateWindow);
167         return ret;
168     }
169     ret.code = static_cast<int32_t>(DmErrorCode::DM_OK);
170     ret.data = static_cast<void*>(hasPrivateWindow);
171     return ret;
172 }
173 
IsFoldable()174 bool CJDisplayManager::IsFoldable()
175 {
176     return SingletonContainer::Get<DisplayManager>().IsFoldable();
177 }
178 
SetFoldDisplayMode(uint32_t mode)179 void CJDisplayManager::SetFoldDisplayMode(uint32_t mode)
180 {
181     FoldDisplayMode innerMode = static_cast<FoldDisplayMode>(mode);
182     SingletonContainer::Get<DisplayManager>().SetFoldDisplayMode(innerMode);
183 }
184 
GetFoldStatus()185 uint32_t CJDisplayManager::GetFoldStatus()
186 {
187     return static_cast<uint32_t>(SingletonContainer::Get<DisplayManager>().GetFoldStatus());
188 }
189 
GetFoldDisplayMode()190 uint32_t CJDisplayManager::GetFoldDisplayMode()
191 {
192     return static_cast<uint32_t>(SingletonContainer::Get<DisplayManager>().GetFoldDisplayMode());
193 }
194 
GetCurrentFoldCreaseRegion()195 RetStruct CJDisplayManager::GetCurrentFoldCreaseRegion()
196 {
197     RetStruct result = {.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL), .len = 0, .data = nullptr};
198     sptr<FoldCreaseRegion> region = SingletonContainer::Get<DisplayManager>().GetCurrentFoldCreaseRegion();
199     if (region == nullptr) {
200         result.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
201         return result;
202     }
203     result.data = CreateCFoldCreaseRegionObject(region);
204     result.code = static_cast<int32_t>(DmErrorCode::DM_OK);
205     if (result.data == nullptr) {
206         result.code = static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL);
207     }
208     return result;
209 }
210 }
211 }
212