1 /*
2 * Copyright (c) 2022 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 <vector>
17 #include <new>
18
19 #include <hitrace_meter.h>
20 #include <js_err_utils.h>
21 #include "js_runtime_utils.h"
22 #include "native_engine/native_reference.h"
23 #include "display_manager.h"
24 #include "window_manager_hilog.h"
25 #include "singleton_container.h"
26 #include "js_display_listener.h"
27 #include "js_display.h"
28 #include "js_display_manager.h"
29 #include "scene_board_judgement.h"
30 #include "screen.h"
31 #include "screen_manager.h"
32 #include "surface_utils.h"
33
34 namespace OHOS {
35 namespace Rosen {
36 using namespace AbilityRuntime;
37 constexpr size_t ARGC_ONE = 1;
38 constexpr size_t ARGC_TWO = 2;
39 constexpr size_t ARGC_THREE = 3;
40 constexpr int32_t INDEX_ONE = 1;
41 class JsDisplayManager {
42 public:
JsDisplayManager(napi_env env)43 explicit JsDisplayManager(napi_env env) {
44 }
45
46 ~JsDisplayManager() = default;
47
Finalizer(napi_env env,void * data,void * hint)48 static void Finalizer(napi_env env, void* data, void* hint)
49 {
50 TLOGD(WmsLogTag::DMS, "Finalizer is called");
51 std::unique_ptr<JsDisplayManager>(static_cast<JsDisplayManager*>(data));
52 }
53
GetDefaultDisplay(napi_env env,napi_callback_info info)54 static napi_value GetDefaultDisplay(napi_env env, napi_callback_info info)
55 {
56 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
57 return (me != nullptr) ? me->OnGetDefaultDisplay(env, info) : nullptr;
58 }
59
GetDefaultDisplaySync(napi_env env,napi_callback_info info)60 static napi_value GetDefaultDisplaySync(napi_env env, napi_callback_info info)
61 {
62 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
63 return (me != nullptr) ? me->OnGetDefaultDisplaySync(env, info) : nullptr;
64 }
65
GetPrimaryDisplaySync(napi_env env,napi_callback_info info)66 static napi_value GetPrimaryDisplaySync(napi_env env, napi_callback_info info)
67 {
68 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
69 return (me != nullptr) ? me->OnGetPrimaryDisplaySync(env, info) : nullptr;
70 }
71
GetDisplayByIdSync(napi_env env,napi_callback_info info)72 static napi_value GetDisplayByIdSync(napi_env env, napi_callback_info info)
73 {
74 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
75 return (me != nullptr) ? me->OnGetDisplayByIdSync(env, info) : nullptr;
76 }
77
GetAllDisplay(napi_env env,napi_callback_info info)78 static napi_value GetAllDisplay(napi_env env, napi_callback_info info)
79 {
80 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
81 return (me != nullptr) ? me->OnGetAllDisplay(env, info) : nullptr;
82 }
83
GetAllDisplayPhysicalResolution(napi_env env,napi_callback_info info)84 static napi_value GetAllDisplayPhysicalResolution(napi_env env, napi_callback_info info)
85 {
86 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
87 return (me != nullptr) ? me->OnGetAllDisplayPhysicalResolution(env, info) : nullptr;
88 }
89
GetAllDisplays(napi_env env,napi_callback_info info)90 static napi_value GetAllDisplays(napi_env env, napi_callback_info info)
91 {
92 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
93 return (me != nullptr) ? me->OnGetAllDisplays(env, info) : nullptr;
94 }
95
RegisterDisplayManagerCallback(napi_env env,napi_callback_info info)96 static napi_value RegisterDisplayManagerCallback(napi_env env, napi_callback_info info)
97 {
98 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
99 return (me != nullptr) ? me->OnRegisterDisplayManagerCallback(env, info) : nullptr;
100 }
101
UnregisterDisplayManagerCallback(napi_env env,napi_callback_info info)102 static napi_value UnregisterDisplayManagerCallback(napi_env env, napi_callback_info info)
103 {
104 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
105 return (me != nullptr) ? me->OnUnregisterDisplayManagerCallback(env, info) : nullptr;
106 }
107
HasPrivateWindow(napi_env env,napi_callback_info info)108 static napi_value HasPrivateWindow(napi_env env, napi_callback_info info)
109 {
110 JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
111 return (me != nullptr) ? me->OnHasPrivateWindow(env, info) : nullptr;
112 }
113
IsFoldable(napi_env env,napi_callback_info info)114 static napi_value IsFoldable(napi_env env, napi_callback_info info)
115 {
116 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
117 return (me != nullptr) ? me->OnIsFoldable(env, info) : nullptr;
118 }
119
IsCaptured(napi_env env,napi_callback_info info)120 static napi_value IsCaptured(napi_env env, napi_callback_info info)
121 {
122 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
123 return (me != nullptr) ? me->OnIsCaptured(env, info) : nullptr;
124 }
125
GetFoldStatus(napi_env env,napi_callback_info info)126 static napi_value GetFoldStatus(napi_env env, napi_callback_info info)
127 {
128 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
129 return (me != nullptr) ? me->OnGetFoldStatus(env, info) : nullptr;
130 }
131
GetFoldDisplayMode(napi_env env,napi_callback_info info)132 static napi_value GetFoldDisplayMode(napi_env env, napi_callback_info info)
133 {
134 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
135 return (me != nullptr) ? me->OnGetFoldDisplayMode(env, info) : nullptr;
136 }
137
SetFoldDisplayMode(napi_env env,napi_callback_info info)138 static napi_value SetFoldDisplayMode(napi_env env, napi_callback_info info)
139 {
140 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
141 return (me != nullptr) ? me->OnSetFoldDisplayMode(env, info) : nullptr;
142 }
143
SetFoldStatusLocked(napi_env env,napi_callback_info info)144 static napi_value SetFoldStatusLocked(napi_env env, napi_callback_info info)
145 {
146 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
147 return (me != nullptr) ? me->OnSetFoldStatusLocked(env, info) : nullptr;
148 }
149
GetCurrentFoldCreaseRegion(napi_env env,napi_callback_info info)150 static napi_value GetCurrentFoldCreaseRegion(napi_env env, napi_callback_info info)
151 {
152 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
153 return (me != nullptr) ? me->OnGetCurrentFoldCreaseRegion(env, info) : nullptr;
154 }
155
CreateVirtualScreen(napi_env env,napi_callback_info info)156 static napi_value CreateVirtualScreen(napi_env env, napi_callback_info info)
157 {
158 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
159 return (me != nullptr) ? me->OnCreateVirtualScreen(env, info) : nullptr;
160 }
161
MakeUnique(napi_env env,napi_callback_info info)162 static napi_value MakeUnique(napi_env env, napi_callback_info info)
163 {
164 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
165 return (me != nullptr) ? me->OnMakeUnique(env, info) : nullptr;
166 }
167
DestroyVirtualScreen(napi_env env,napi_callback_info info)168 static napi_value DestroyVirtualScreen(napi_env env, napi_callback_info info)
169 {
170 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
171 return (me != nullptr) ? me->OnDestroyVirtualScreen(env, info) : nullptr;
172 }
173
SetVirtualScreenSurface(napi_env env,napi_callback_info info)174 static napi_value SetVirtualScreenSurface(napi_env env, napi_callback_info info)
175 {
176 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
177 return (me != nullptr) ? me->OnSetVirtualScreenSurface(env, info) : nullptr;
178 }
179
AddVirtualScreenBlockList(napi_env env,napi_callback_info info)180 static napi_value AddVirtualScreenBlockList(napi_env env, napi_callback_info info)
181 {
182 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
183 return (me != nullptr) ? me->OnAddVirtualScreenBlockList(env, info) : nullptr;
184 }
185
RemoveVirtualScreenBlockList(napi_env env,napi_callback_info info)186 static napi_value RemoveVirtualScreenBlockList(napi_env env, napi_callback_info info)
187 {
188 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
189 return (me != nullptr) ? me->OnRemoveVirtualScreenBlockList(env, info) : nullptr;
190 }
191
ConvertRelativeCoordinateToGlobal(napi_env env,napi_callback_info info)192 static napi_value ConvertRelativeCoordinateToGlobal(napi_env env, napi_callback_info info)
193 {
194 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
195 return (me != nullptr) ? me->OnConvertRelativeCoordinateToGlobal(env, info) : nullptr;
196 }
197
ConvertGlobalCoordinateToRelative(napi_env env,napi_callback_info info)198 static napi_value ConvertGlobalCoordinateToRelative(napi_env env, napi_callback_info info)
199 {
200 auto* me = CheckParamsAndGetThis<JsDisplayManager>(env, info);
201 return (me != nullptr) ? me->OnConvertGlobalCoordinateToRelative(env, info) : nullptr;
202 }
203
204 private:
205 std::map<std::string, std::map<std::unique_ptr<NativeReference>, sptr<JsDisplayListener>>> jsCbMap_;
206 std::mutex mtx_;
207
OnGetDefaultDisplay(napi_env env,napi_callback_info info)208 napi_value OnGetDefaultDisplay(napi_env env, napi_callback_info info)
209 {
210 TLOGI(WmsLogTag::DMS, "GetDefaultDisplay called");
211 DMError errCode = DMError::DM_OK;
212 size_t argc = 4;
213 napi_value argv[4] = {nullptr};
214 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
215 if (argc != 0 && argc != ARGC_ONE) {
216 TLOGE(WmsLogTag::DMS, "params not match");
217 errCode = DMError::DM_ERROR_INVALID_PARAM;
218 }
219
220 napi_value lastParam = nullptr;
221 if (argc == ARGC_ONE && GetType(env, argv[0]) == napi_function) {
222 lastParam = argv[0];
223 }
224 napi_value result = nullptr;
225 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
226 auto asyncTask = [this, env, errCode, task = napiAsyncTask.get()] {
227 if (errCode != DMError::DM_OK) {
228 task->Reject(env, CreateJsError(env,
229 static_cast<int32_t>(errCode), "JsDisplayManager::OnGetDefaultDisplay failed."));
230 delete task;
231 return;
232 }
233 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "Async:GetDefaultDisplay");
234 sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplay();
235 if (display != nullptr) {
236 task->Resolve(env, CreateJsDisplayObject(env, display));
237 TLOGNI(WmsLogTag::DMS, "OnGetDefaultDisplay success");
238 } else {
239 task->Reject(env, CreateJsError(env,
240 static_cast<int32_t>(DMError::DM_ERROR_NULLPTR), "JsDisplayManager::OnGetDefaultDisplay failed."));
241 }
242 delete task;
243 };
244 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnGetDefaultDisplay");
245 return result;
246 }
247
OnGetPrimaryDisplaySync(napi_env env,napi_callback_info info)248 napi_value OnGetPrimaryDisplaySync(napi_env env, napi_callback_info info)
249 {
250 TLOGD(WmsLogTag::DMS, "OnGetPrimaryDisplaySync called");
251 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "Sync:OnGetPrimaryDisplaySync");
252 sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetPrimaryDisplaySync();
253 if (display == nullptr) {
254 TLOGE(WmsLogTag::DMS, "[NAPI]Display info is nullptr, js error will be happen");
255 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN),
256 "invalid screen id"));
257 return NapiGetUndefined(env);
258 }
259 return CreateJsDisplayObject(env, display);
260 }
261
OnGetDefaultDisplaySync(napi_env env,napi_callback_info info)262 napi_value OnGetDefaultDisplaySync(napi_env env, napi_callback_info info)
263 {
264 TLOGD(WmsLogTag::DMS, "GetDefaultDisplaySync called");
265 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "Sync:GetDefaultDisplay");
266 sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplaySync(true);
267 if (display == nullptr) {
268 TLOGE(WmsLogTag::DMS, "[NAPI]Display info is nullptr, js error will be happen");
269 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN)));
270 return NapiGetUndefined(env);
271 }
272 return CreateJsDisplayObject(env, display);
273 }
274
OnGetDisplayByIdSync(napi_env env,napi_callback_info info)275 napi_value OnGetDisplayByIdSync(napi_env env, napi_callback_info info)
276 {
277 TLOGD(WmsLogTag::DMS, "OnGetDisplayByIdSync called");
278 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "Sync:OnGetDisplayByIdSync");
279 size_t argc = 4;
280 napi_value argv[4] = {nullptr};
281 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
282 if (argc < ARGC_ONE) {
283 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
284 std::string errMsg = "Invalid args count, need one arg";
285 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
286 return NapiGetUndefined(env);
287 }
288 int64_t displayId = static_cast<int64_t>(DISPLAY_ID_INVALID);
289 if (!ConvertFromJsValue(env, argv[0], displayId)) {
290 TLOGE(WmsLogTag::DMS, "[NAPI]Failed to convert parameter to displayId");
291 std::string errMsg = "Failed to convert parameter to displayId";
292 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
293 return NapiGetUndefined(env);
294 }
295 if (displayId < 0) {
296 std::string errMsg = "displayid is invalid, less than 0";
297 TLOGE(WmsLogTag::DMS, "[NAPI]Invalid displayId: %{public}" PRId64", less than 0", displayId);
298 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
299 return NapiGetUndefined(env);
300 }
301 sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDisplayById(static_cast<DisplayId>(displayId));
302 if (display == nullptr) {
303 TLOGE(WmsLogTag::DMS, "[NAPI]Display info is nullptr, js error will be happen");
304 std::ostringstream oss;
305 oss << "[getDisplayByIdSync]message: display is null, possible causes: display id " << displayId << " ";
306 oss << "corresponding display does not exist.";
307 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL), oss.str()));
308 return NapiGetUndefined(env);
309 }
310 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "Sync:OnGetDisplayByIdSync end");
311 return CreateJsDisplayObject(env, display);
312 }
313
OnGetAllDisplay(napi_env env,napi_callback_info info)314 napi_value OnGetAllDisplay(napi_env env, napi_callback_info info)
315 {
316 TLOGD(WmsLogTag::DMS, "GetAllDisplay called");
317 DMError errCode = DMError::DM_OK;
318 size_t argc = 4;
319 napi_value argv[4] = {nullptr};
320 std::string taskName = "OnGetAllDisplay";
321 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
322 if (argc != 0 && argc != ARGC_ONE) {
323 TLOGE(WmsLogTag::DMS, "OnGetAllDisplay params not match");
324 errCode = DMError::DM_ERROR_INVALID_PARAM;
325 return NapiGetUndefined(env);
326 }
327 napi_value lastParam = nullptr;
328 if (argc == ARGC_ONE && GetType(env, argv[0]) == napi_function) {
329 lastParam = argv[0];
330 }
331 napi_value result = nullptr;
332 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
333 auto asyncTask = [this, env, task = napiAsyncTask.get()]() {
334 std::vector<sptr<Display>> displays = SingletonContainer::Get<DisplayManager>().GetAllDisplays();
335 if (!displays.empty()) {
336 task->Resolve(env, CreateJsDisplayArrayObject(env, displays));
337 TLOGNI(WmsLogTag::DMS, "GetAllDisplays success");
338 } else {
339 task->Reject(env, CreateJsError(env,
340 static_cast<int32_t>(DMError::DM_ERROR_NULLPTR), "JsDisplayManager::OnGetAllDisplay failed."));
341 }
342 delete task;
343 };
344 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, taskName);
345 return result;
346 }
347
NapiSendDmsEvent(napi_env env,std::function<void ()> asyncTask,std::unique_ptr<AbilityRuntime::NapiAsyncTask> & napiAsyncTask,std::string taskName)348 void NapiSendDmsEvent(napi_env env, std::function<void()> asyncTask,
349 std::unique_ptr<AbilityRuntime::NapiAsyncTask>& napiAsyncTask, std::string taskName)
350 {
351 if (!env) {
352 TLOGE(WmsLogTag::DMS, "env is null");
353 return;
354 }
355 if (napi_send_event(env, asyncTask, napi_eprio_immediate, taskName.c_str()) != napi_status::napi_ok) {
356 napiAsyncTask->Reject(env, CreateJsError(env,
357 static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN), "Send event failed!"));
358 } else {
359 napiAsyncTask.release();
360 TLOGI(WmsLogTag::DMS, "%{public}s:send event success", taskName.c_str());
361 }
362 }
363
CreateEmptyAsyncTask(napi_env env,napi_value lastParam,napi_value * result)364 std::unique_ptr<NapiAsyncTask> CreateEmptyAsyncTask(napi_env env, napi_value lastParam, napi_value* result)
365 {
366 napi_valuetype type = napi_undefined;
367 napi_typeof(env, lastParam, &type);
368 if (lastParam == nullptr || type != napi_function) {
369 napi_deferred nativeDeferred = nullptr;
370 napi_create_promise(env, &nativeDeferred, result);
371 return std::make_unique<NapiAsyncTask>(nativeDeferred, std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
372 std::unique_ptr<NapiAsyncTask::CompleteCallback>());
373 } else {
374 napi_get_undefined(env, result);
375 napi_ref callbackRef = nullptr;
376 napi_create_reference(env, lastParam, 1, &callbackRef);
377 return std::make_unique<NapiAsyncTask>(callbackRef, std::unique_ptr<NapiAsyncTask::ExecuteCallback>(),
378 std::unique_ptr<NapiAsyncTask::CompleteCallback>());
379 }
380 }
381
CreateJsDisplayPhysicalArrayObject(napi_env env,const std::vector<DisplayPhysicalResolution> & physicalArray)382 napi_value CreateJsDisplayPhysicalArrayObject(napi_env env,
383 const std::vector<DisplayPhysicalResolution>& physicalArray)
384 {
385 TLOGD(WmsLogTag::DMS, "CreateJsDisplayPhysicalArrayObject is called");
386 napi_value arrayValue = nullptr;
387 napi_create_array_with_length(env, physicalArray.size(), &arrayValue);
388 if (arrayValue == nullptr) {
389 TLOGE(WmsLogTag::DMS, "Failed to create display array");
390 return NapiGetUndefined(env);
391 }
392 int32_t i = 0;
393 for (const auto& displayItem : physicalArray) {
394 napi_set_element(env, arrayValue, i++, CreateJsDisplayPhysicalInfoObject(env, displayItem));
395 }
396 return arrayValue;
397 }
398
OnGetAllDisplayPhysicalResolution(napi_env env,napi_callback_info info)399 napi_value OnGetAllDisplayPhysicalResolution(napi_env env, napi_callback_info info)
400 {
401 TLOGD(WmsLogTag::DMS, "called");
402 DMError errCode = DMError::DM_OK;
403 size_t argc = 4;
404 napi_value argv[4] = {nullptr};
405 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
406 if (argc != 0 && argc != ARGC_ONE) {
407 TLOGE(WmsLogTag::DMS, "params not match");
408 errCode = DMError::DM_ERROR_INVALID_PARAM;
409 }
410
411 napi_value lastParam = nullptr;
412 if (argc == ARGC_ONE && GetType(env, argv[0]) == napi_function) {
413 lastParam = argv[0];
414 }
415 napi_value result = nullptr;
416 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
417 auto asyncTask = [this, env, errCode, task = napiAsyncTask.get()] {
418 if (errCode != DMError::DM_OK) {
419 task->Reject(env, CreateJsError(env,
420 static_cast<int32_t>(errCode), "JsDisplayManager::OnGetAllDisplayPhysicalResolution failed."));
421 delete task;
422 return;
423 }
424 std::vector<DisplayPhysicalResolution> displayPhysicalArray =
425 SingletonContainer::Get<DisplayManager>().GetAllDisplayPhysicalResolution();
426 if (!displayPhysicalArray.empty()) {
427 task->Resolve(env, CreateJsDisplayPhysicalArrayObject(env, displayPhysicalArray));
428 TLOGNI(WmsLogTag::DMS, "OnGetAllDisplayPhysicalResolution success");
429 } else {
430 task->Reject(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL),
431 "JsDisplayManager::OnGetAllDisplayPhysicalResolution failed."));
432 }
433 delete task;
434 };
435 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnGetAllDisplayPhysicalResolution");
436 return result;
437 }
438
OnGetAllDisplays(napi_env env,napi_callback_info info)439 napi_value OnGetAllDisplays(napi_env env, napi_callback_info info)
440 {
441 TLOGD(WmsLogTag::DMS, "called");
442 size_t argc = 4;
443 napi_value argv[4] = {nullptr};
444 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
445 napi_value lastParam = nullptr;
446 if (argc >= ARGC_ONE && argv[ARGC_ONE - 1] != nullptr &&
447 GetType(env, argv[ARGC_ONE - 1]) == napi_function) {
448 lastParam = argv[0];
449 }
450 napi_value result = nullptr;
451 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
452 auto asyncTask = [this, env, task = napiAsyncTask.get()] {
453 std::vector<sptr<Display>> displays = SingletonContainer::Get<DisplayManager>().GetAllDisplays();
454 if (!displays.empty()) {
455 task->Resolve(env, CreateJsDisplayArrayObject(env, displays));
456 TLOGND(WmsLogTag::DMS, "GetAllDisplays success");
457 } else {
458 auto errorPending = false;
459 napi_is_exception_pending(env, &errorPending);
460 if (errorPending) {
461 napi_value exception = nullptr;
462 napi_get_and_clear_last_exception(env, &exception);
463 }
464 task->Reject(env, CreateJsError(env,
465 static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN),
466 "JsDisplayManager::OnGetAllDisplays failed."));
467 }
468 delete task;
469 };
470 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnGetDefaultDisplay");
471 return result;
472 }
473
RegisterDisplayListenerWithType(napi_env env,const std::string & type,napi_value value)474 DMError RegisterDisplayListenerWithType(napi_env env, const std::string& type, napi_value value)
475 {
476 if (IfCallbackRegistered(env, type, value)) {
477 TLOGE(WmsLogTag::DMS, "callback already registered!");
478 return DMError::DM_OK;
479 }
480 std::unique_ptr<NativeReference> callbackRef;
481 napi_ref result = nullptr;
482 napi_create_reference(env, value, 1, &result);
483 callbackRef.reset(reinterpret_cast<NativeReference*>(result));
484 sptr<JsDisplayListener> displayListener = new(std::nothrow) JsDisplayListener(env);
485 DMError ret = DMError::DM_OK;
486 if (displayListener == nullptr) {
487 TLOGE(WmsLogTag::DMS, "displayListener is nullptr");
488 return DMError::DM_ERROR_INVALID_PARAM;
489 }
490 if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
491 ret = SingletonContainer::Get<DisplayManager>().RegisterDisplayListener(displayListener);
492 } else if (type == EVENT_PRIVATE_MODE_CHANGE) {
493 ret = SingletonContainer::Get<DisplayManager>().RegisterPrivateWindowListener(displayListener);
494 } else if (type == EVENT_FOLD_STATUS_CHANGED) {
495 ret = SingletonContainer::Get<DisplayManager>().RegisterFoldStatusListener(displayListener);
496 } else if (type == EVENT_DISPLAY_MODE_CHANGED) {
497 ret = SingletonContainer::Get<DisplayManager>().RegisterDisplayModeListener(displayListener);
498 } else if (type == EVENT_AVAILABLE_AREA_CHANGED) {
499 ret = SingletonContainer::Get<DisplayManager>().RegisterAvailableAreaListener(displayListener);
500 } else if (type == EVENT_FOLD_ANGLE_CHANGED) {
501 ret = SingletonContainer::Get<DisplayManager>().RegisterFoldAngleListener(displayListener);
502 } else if (type == EVENT_CAPTURE_STATUS_CHANGED) {
503 ret = SingletonContainer::Get<DisplayManager>().RegisterCaptureStatusListener(displayListener);
504 } else {
505 TLOGE(WmsLogTag::DMS, "RegisterDisplayListenerWithType failed, %{public}s not support", type.c_str());
506 return DMError::DM_ERROR_INVALID_PARAM;
507 }
508 if (ret != DMError::DM_OK) {
509 TLOGE(WmsLogTag::DMS, "RegisterDisplayListenerWithType failed, ret: %{public}u", ret);
510 return ret;
511 }
512 displayListener->AddCallback(type, value);
513 jsCbMap_[type][std::move(callbackRef)] = displayListener;
514 return DMError::DM_OK;
515 }
516
IfCallbackRegistered(napi_env env,const std::string & type,napi_value jsListenerObject)517 bool IfCallbackRegistered(napi_env env, const std::string& type, napi_value jsListenerObject)
518 {
519 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
520 TLOGI(WmsLogTag::DMS, "method %{public}s not registered!", type.c_str());
521 return false;
522 }
523
524 for (auto& iter : jsCbMap_[type]) {
525 bool isEquals = false;
526 napi_strict_equals(env, jsListenerObject, iter.first->GetNapiValue(), &isEquals);
527 if (isEquals) {
528 TLOGE(WmsLogTag::DMS, "callback already registered!");
529 return true;
530 }
531 }
532 return false;
533 }
534
UnregisterAllDisplayListenerWithType(const std::string & type)535 DMError UnregisterAllDisplayListenerWithType(const std::string& type)
536 {
537 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
538 TLOGI(WmsLogTag::DMS, "methodName %{public}s not registered!", type.c_str());
539 return DMError::DM_OK;
540 }
541 DMError ret = DMError::DM_OK;
542 for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
543 it->second->RemoveAllCallback();
544 if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
545 sptr<DisplayManager::IDisplayListener> thisListener(it->second);
546 ret = SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
547 } else if (type == EVENT_PRIVATE_MODE_CHANGE) {
548 sptr<DisplayManager::IPrivateWindowListener> thisListener(it->second);
549 ret = SingletonContainer::Get<DisplayManager>().UnregisterPrivateWindowListener(thisListener);
550 } else if (type == EVENT_AVAILABLE_AREA_CHANGED) {
551 sptr<DisplayManager::IAvailableAreaListener> thisListener(it->second);
552 ret = SingletonContainer::Get<DisplayManager>().UnregisterAvailableAreaListener(thisListener);
553 } else if (type == EVENT_FOLD_STATUS_CHANGED) {
554 sptr<DisplayManager::IFoldStatusListener> thisListener(it->second);
555 ret = SingletonContainer::Get<DisplayManager>().UnregisterFoldStatusListener(thisListener);
556 } else if (type == EVENT_DISPLAY_MODE_CHANGED) {
557 sptr<DisplayManager::IDisplayModeListener> thisListener(it->second);
558 ret = SingletonContainer::Get<DisplayManager>().UnregisterDisplayModeListener(thisListener);
559 } else if (type == EVENT_FOLD_ANGLE_CHANGED) {
560 sptr<DisplayManager::IFoldAngleListener> thisListener(it->second);
561 ret = SingletonContainer::Get<DisplayManager>().UnregisterFoldAngleListener(thisListener);
562 } else if (type == EVENT_CAPTURE_STATUS_CHANGED) {
563 sptr<DisplayManager::ICaptureStatusListener> thisListener(it->second);
564 ret = SingletonContainer::Get<DisplayManager>().UnregisterCaptureStatusListener(thisListener);
565 } else {
566 ret = DMError::DM_ERROR_INVALID_PARAM;
567 }
568 jsCbMap_[type].erase(it++);
569 TLOGI(WmsLogTag::DMS, "type %{public}s ret: %{public}u", type.c_str(), ret);
570 }
571 jsCbMap_.erase(type);
572 return ret;
573 }
574
UnRegisterDisplayListenerWithType(napi_env env,const std::string & type,napi_value value)575 DMError UnRegisterDisplayListenerWithType(napi_env env, const std::string& type, napi_value value)
576 {
577 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
578 TLOGI(WmsLogTag::DMS, "methodName %{public}s not registered!", type.c_str());
579 return DMError::DM_OK;
580 }
581 DMError ret = DMError::DM_OK;
582 for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
583 bool isEquals = false;
584 napi_strict_equals(env, value, it->first->GetNapiValue(), &isEquals);
585 if (isEquals) {
586 it->second->RemoveCallback(env, type, value);
587 if (type == EVENT_ADD || type == EVENT_REMOVE || type == EVENT_CHANGE) {
588 sptr<DisplayManager::IDisplayListener> thisListener(it->second);
589 ret = SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
590 } else if (type == EVENT_PRIVATE_MODE_CHANGE) {
591 sptr<DisplayManager::IPrivateWindowListener> thisListener(it->second);
592 ret = SingletonContainer::Get<DisplayManager>().UnregisterPrivateWindowListener(thisListener);
593 } else if (type == EVENT_AVAILABLE_AREA_CHANGED) {
594 sptr<DisplayManager::IAvailableAreaListener> thisListener(it->second);
595 ret = SingletonContainer::Get<DisplayManager>().UnregisterAvailableAreaListener(thisListener);
596 } else if (type == EVENT_FOLD_STATUS_CHANGED) {
597 sptr<DisplayManager::IFoldStatusListener> thisListener(it->second);
598 ret = SingletonContainer::Get<DisplayManager>().UnregisterFoldStatusListener(thisListener);
599 } else if (type == EVENT_DISPLAY_MODE_CHANGED) {
600 sptr<DisplayManager::IDisplayModeListener> thisListener(it->second);
601 ret = SingletonContainer::Get<DisplayManager>().UnregisterDisplayModeListener(thisListener);
602 } else if (type == EVENT_FOLD_ANGLE_CHANGED) {
603 sptr<DisplayManager::IFoldAngleListener> thisListener(it->second);
604 ret = SingletonContainer::Get<DisplayManager>().UnregisterFoldAngleListener(thisListener);
605 } else if (type == EVENT_CAPTURE_STATUS_CHANGED) {
606 sptr<DisplayManager::ICaptureStatusListener> thisListener(it->second);
607 ret = SingletonContainer::Get<DisplayManager>().UnregisterCaptureStatusListener(thisListener);
608 } else {
609 ret = DMError::DM_ERROR_INVALID_PARAM;
610 }
611 jsCbMap_[type].erase(it++);
612 TLOGI(WmsLogTag::DMS, "type %{public}s ret: %{public}u", type.c_str(), ret);
613 break;
614 } else {
615 it++;
616 }
617 }
618 if (jsCbMap_[type].empty()) {
619 jsCbMap_.erase(type);
620 }
621 return ret;
622 }
623
NapiIsCallable(napi_env env,napi_value value)624 bool NapiIsCallable(napi_env env, napi_value value)
625 {
626 bool result = false;
627 napi_is_callable(env, value, &result);
628 return result;
629 }
630
OnRegisterDisplayManagerCallback(napi_env env,napi_callback_info info)631 napi_value OnRegisterDisplayManagerCallback(napi_env env, napi_callback_info info)
632 {
633 TLOGD(WmsLogTag::DMS, "OnRegisterDisplayManagerCallback is called");
634 size_t argc = 4;
635 napi_value argv[4] = {nullptr};
636 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
637 if (argc < ARGC_TWO) {
638 TLOGE(WmsLogTag::DMS, "JsDisplayManager Params not match: %{public}zu", argc);
639 std::string errMsg = "Invalid args count, need 2 args";
640 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
641 return NapiGetUndefined(env);
642 }
643 std::string cbType;
644 if (!ConvertFromJsValue(env, argv[0], cbType)) {
645 std::string errMsg = "Failed to convert parameter to callbackType";
646 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
647 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to callbackType");
648 return NapiGetUndefined(env);
649 }
650 napi_value value = argv[INDEX_ONE];
651 if (value == nullptr) {
652 TLOGI(WmsLogTag::DMS, "OnRegisterDisplayManagerCallback info->argv[1] is nullptr");
653 std::string errMsg = "OnRegisterDisplayManagerCallback is nullptr";
654 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
655 return NapiGetUndefined(env);
656 }
657 if (!NapiIsCallable(env, value)) {
658 TLOGI(WmsLogTag::DMS, "OnRegisterDisplayManagerCallback info->argv[1] is not callable");
659 std::string errMsg = "OnRegisterDisplayManagerCallback is not callable";
660 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
661 return NapiGetUndefined(env);
662 }
663 std::lock_guard<std::mutex> lock(mtx_);
664 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(RegisterDisplayListenerWithType(env, cbType, value));
665 if (ret != DmErrorCode::DM_OK) {
666 DmErrorCode errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
667 if (ret == DmErrorCode::DM_ERROR_NOT_SYSTEM_APP) {
668 errCode = ret;
669 }
670 TLOGE(WmsLogTag::DMS, "Failed to register display listener with type");
671 std::string errMsg = "Failed to register display listener with type";
672 napi_throw(env, CreateJsError(env, static_cast<int32_t>(errCode), errMsg));
673 return NapiGetUndefined(env);
674 }
675 return NapiGetUndefined(env);
676 }
677
OnUnregisterDisplayManagerCallback(napi_env env,napi_callback_info info)678 napi_value OnUnregisterDisplayManagerCallback(napi_env env, napi_callback_info info)
679 {
680 TLOGD(WmsLogTag::DMS, "called");
681 size_t argc = 4;
682 napi_value argv[4] = {nullptr};
683 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
684 if (argc < ARGC_ONE) {
685 TLOGE(WmsLogTag::DMS, "JsDisplayManager Params not match %{public}zu", argc);
686 std::string errMsg = "Invalid args count, need one arg at least!";
687 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
688 return NapiGetUndefined(env);
689 }
690 std::string cbType;
691 if (!ConvertFromJsValue(env, argv[0], cbType)) {
692 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to callbackType");
693 std::string errMsg = "Failed to convert parameter to string";
694 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
695 return NapiGetUndefined(env);
696 }
697 std::lock_guard<std::mutex> lock(mtx_);
698 DmErrorCode ret;
699 if (argc == ARGC_ONE) {
700 ret = DM_JS_TO_ERROR_CODE_MAP.at(UnregisterAllDisplayListenerWithType(cbType));
701 } else {
702 napi_value value = argv[INDEX_ONE];
703 if ((value == nullptr) || (!NapiIsCallable(env, value))) {
704 ret = DM_JS_TO_ERROR_CODE_MAP.at(UnregisterAllDisplayListenerWithType(cbType));
705 } else {
706 ret = DM_JS_TO_ERROR_CODE_MAP.at(UnRegisterDisplayListenerWithType(env, cbType, value));
707 }
708 }
709 if (ret != DmErrorCode::DM_OK) {
710 DmErrorCode errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
711 if (ret == DmErrorCode::DM_ERROR_NOT_SYSTEM_APP) {
712 errCode = ret;
713 }
714 TLOGW(WmsLogTag::DMS, "failed to unregister display listener with type");
715 std::string errMsg = "failed to unregister display listener with type";
716 napi_throw(env, CreateJsError(env, static_cast<int32_t>(errCode), errMsg));
717 return NapiGetUndefined(env);
718 }
719 return NapiGetUndefined(env);
720 }
721
OnHasPrivateWindow(napi_env env,napi_callback_info info)722 napi_value OnHasPrivateWindow(napi_env env, napi_callback_info info)
723 {
724 bool hasPrivateWindow = false;
725 size_t argc = 4;
726 napi_value argv[4] = {nullptr};
727 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
728 if (argc < ARGC_ONE) {
729 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
730 std::string errMsg = "Invalid args count, need one arg";
731 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
732 return NapiGetUndefined(env);
733 }
734 int64_t displayId = static_cast<int64_t>(DISPLAY_ID_INVALID);
735 if (!ConvertFromJsValue(env, argv[0], displayId)) {
736 TLOGE(WmsLogTag::DMS, "[NAPI]Failed to convert parameter to displayId");
737 std::string errMsg = "Failed to convert parameter to displayId";
738 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
739 return NapiGetUndefined(env);
740 }
741 if (displayId < 0) {
742 TLOGE(WmsLogTag::DMS, "[NAPI]Invalid displayId: %{public}" PRId64", less than 0", displayId);
743 std::string errMsg = "displayid is invalid, less than 0";
744 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
745 return NapiGetUndefined(env);
746 }
747 DmErrorCode errCode = DM_JS_TO_ERROR_CODE_MAP.at(
748 SingletonContainer::Get<DisplayManager>().HasPrivateWindow(displayId, hasPrivateWindow));
749 TLOGI(WmsLogTag::DMS, "[NAPI]Display id = %{public}" PRIu64", hasPrivateWindow = %{public}u err = %{public}d",
750 static_cast<uint64_t>(displayId), hasPrivateWindow, errCode);
751 if (errCode != DmErrorCode::DM_OK) {
752 napi_throw(env, JsErrUtils::CreateJsError(env, errCode));
753 return NapiGetUndefined(env);
754 }
755 napi_value result;
756 napi_get_boolean(env, hasPrivateWindow, &result);
757 return result;
758 }
759
CreateJsDisplayArrayObject(napi_env env,std::vector<sptr<Display>> & displays)760 napi_value CreateJsDisplayArrayObject(napi_env env, std::vector<sptr<Display>>& displays)
761 {
762 TLOGD(WmsLogTag::DMS, "CreateJsDisplayArrayObject is called");
763 napi_value arrayValue = nullptr;
764 napi_create_array_with_length(env, displays.size(), &arrayValue);
765 if (arrayValue == nullptr) {
766 TLOGE(WmsLogTag::DMS, "Failed to create display array");
767 return NapiGetUndefined(env);
768 }
769 int32_t i = 0;
770 for (auto& display : displays) {
771 if (display == nullptr) {
772 continue;
773 }
774 napi_set_element(env, arrayValue, i++, CreateJsDisplayObject(env, display));
775 }
776 return arrayValue;
777 }
778
OnIsFoldable(napi_env env,napi_callback_info info)779 napi_value OnIsFoldable(napi_env env, napi_callback_info info)
780 {
781 size_t argc = 4;
782 napi_value argv[4] = {nullptr};
783 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
784 if (argc >= ARGC_ONE) {
785 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
786 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
787 return NapiGetUndefined(env);
788 }
789 bool foldable = SingletonContainer::Get<DisplayManager>().IsFoldable();
790 TLOGD(WmsLogTag::DMS, "[NAPI]foldable = %{public}u", foldable);
791 napi_value result;
792 napi_get_boolean(env, foldable, &result);
793 return result;
794 }
795
OnIsCaptured(napi_env env,napi_callback_info info)796 napi_value OnIsCaptured(napi_env env, napi_callback_info info)
797 {
798 size_t argc = 4; // default arg length
799 napi_value argv[4] = { nullptr }; // default arg length
800 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
801 if (argc >= ARGC_ONE) {
802 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
803 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
804 return NapiGetUndefined(env);
805 }
806 bool isCapture = SingletonContainer::Get<DisplayManager>().IsCaptured();
807 TLOGD(WmsLogTag::DMS, "[NAPI]IsCaptured = %{public}u", isCapture);
808 napi_value result;
809 napi_get_boolean(env, isCapture, &result);
810 return result;
811 }
812
OnGetFoldStatus(napi_env env,napi_callback_info info)813 napi_value OnGetFoldStatus(napi_env env, napi_callback_info info)
814 {
815 size_t argc = 4;
816 napi_value argv[4] = {nullptr};
817 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
818 if (argc >= ARGC_ONE) {
819 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
820 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
821 return NapiGetUndefined(env);
822 }
823 FoldStatus status = SingletonContainer::Get<DisplayManager>().GetFoldStatus();
824 TLOGD(WmsLogTag::DMS, "[NAPI]status = %{public}u", status);
825 return CreateJsValue(env, status);
826 }
827
OnGetFoldDisplayMode(napi_env env,napi_callback_info info)828 napi_value OnGetFoldDisplayMode(napi_env env, napi_callback_info info)
829 {
830 size_t argc = 4;
831 napi_value argv[4] = {nullptr};
832 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
833 if (argc >= ARGC_ONE) {
834 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
835 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
836 return NapiGetUndefined(env);
837 }
838 FoldDisplayMode mode = SingletonContainer::Get<DisplayManager>().GetFoldDisplayModeForExternal();
839 TLOGD(WmsLogTag::DMS, "[NAPI]mode = %{public}u", mode);
840 return CreateJsValue(env, mode);
841 }
842
OnSetFoldDisplayMode(napi_env env,napi_callback_info info)843 napi_value OnSetFoldDisplayMode(napi_env env, napi_callback_info info)
844 {
845 size_t argc = 4;
846 napi_value argv[4] = {nullptr};
847 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
848 if (argc < ARGC_ONE) {
849 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
850 std::string errMsg = "Invalid args count, need one arg";
851 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
852 return NapiGetUndefined(env);
853 }
854 FoldDisplayMode mode = FoldDisplayMode::UNKNOWN;
855 if (!ConvertFromJsValue(env, argv[0], mode)) {
856 TLOGE(WmsLogTag::DMS, "[NAPI]Failed to convert parameter to FoldDisplayMode");
857 std::string errMsg = "Failed to convert parameter to FoldDisplayMode";
858 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
859 return NapiGetUndefined(env);
860 }
861 std::string reason = "";
862 if (argc == ARGC_TWO) {
863 if (!ConvertFromJsValue(env, argv[INDEX_ONE], reason)) {
864 TLOGE(WmsLogTag::DMS, "[NAPI]Failed to convert parameter to reason");
865 std::string errMsg = "Failed to convert parameter to reason";
866 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
867 return NapiGetUndefined(env);
868 }
869 }
870 DmErrorCode errCode = DM_JS_TO_ERROR_CODE_MAP.at(
871 SingletonContainer::Get<DisplayManager>().SetFoldDisplayModeFromJs(mode, reason));
872 TLOGI(WmsLogTag::DMS, "[NAPI]%{public}d", static_cast<int32_t>(errCode));
873 if (errCode != DmErrorCode::DM_OK) {
874 napi_throw(env, JsErrUtils::CreateJsError(env, errCode));
875 return NapiGetUndefined(env);
876 }
877 return NapiGetUndefined(env);
878 }
879
OnSetFoldStatusLocked(napi_env env,napi_callback_info info)880 napi_value OnSetFoldStatusLocked(napi_env env, napi_callback_info info)
881 {
882 size_t argc = 4;
883 napi_value argv[4] = {nullptr};
884 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
885 if (argc < ARGC_ONE) {
886 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
887 std::string errMsg = "Invalid args count, need one arg";
888 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
889 return NapiGetUndefined(env);
890 }
891 bool locked = false;
892 if (!ConvertFromJsValue(env, argv[0], locked)) {
893 TLOGE(WmsLogTag::DMS, "[NAPI]Failed to convert parameter to SetFoldStatusLocked");
894 std::string errMsg = "Failed to convert parameter to SetFoldStatusLocked";
895 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM), errMsg));
896 return NapiGetUndefined(env);
897 }
898 DmErrorCode errCode = DM_JS_TO_ERROR_CODE_MAP.at(
899 SingletonContainer::Get<DisplayManager>().SetFoldStatusLockedFromJs(locked));
900 if (errCode != DmErrorCode::DM_OK) {
901 napi_throw(env, JsErrUtils::CreateJsError(env, errCode));
902 return NapiGetUndefined(env);
903 }
904 TLOGI(WmsLogTag::DMS, "[NAPI]locked: %{public}d", locked);
905 return NapiGetUndefined(env);
906 }
907
OnGetCurrentFoldCreaseRegion(napi_env env,napi_callback_info info)908 napi_value OnGetCurrentFoldCreaseRegion(napi_env env, napi_callback_info info)
909 {
910 size_t argc = 4;
911 napi_value argv[4] = {nullptr};
912 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
913 if (argc >= ARGC_ONE) {
914 TLOGE(WmsLogTag::DMS, "Params not match %{public}zu", argc);
915 napi_throw(env, CreateJsError(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
916 return NapiGetUndefined(env);
917 }
918 sptr<FoldCreaseRegion> region = SingletonContainer::Get<DisplayManager>().GetCurrentFoldCreaseRegion();
919 return CreateJsFoldCreaseRegionObject(env, region);
920 }
921
CreateJsFoldCreaseRegionObject(napi_env env,sptr<FoldCreaseRegion> region)922 napi_value CreateJsFoldCreaseRegionObject(napi_env env, sptr<FoldCreaseRegion> region)
923 {
924 TLOGI(WmsLogTag::DMS, "called");
925 napi_value objValue = nullptr;
926 napi_create_object(env, &objValue);
927 if (objValue == nullptr) {
928 TLOGE(WmsLogTag::DMS, "Failed to convert prop to jsObject");
929 return NapiGetUndefined(env);
930 }
931 if (region == nullptr) {
932 TLOGW(WmsLogTag::DMS, "Get null fold crease region");
933 return NapiGetUndefined(env);
934 }
935 DisplayId displayId = region->GetDisplayId();
936 std::vector<DMRect> creaseRects = region->GetCreaseRects();
937 napi_set_named_property(env, objValue, "displayId", CreateJsValue(env, static_cast<uint32_t>(displayId)));
938 napi_set_named_property(env, objValue, "creaseRects", CreateJsCreaseRectsArrayObject(env, creaseRects));
939 return objValue;
940 }
941
CreateJsCreaseRectsArrayObject(napi_env env,std::vector<DMRect> creaseRects)942 napi_value CreateJsCreaseRectsArrayObject(napi_env env, std::vector<DMRect> creaseRects)
943 {
944 napi_value arrayValue = nullptr;
945 napi_create_array_with_length(env, creaseRects.size(), &arrayValue);
946 size_t i = 0;
947 for (const auto& rect : creaseRects) {
948 napi_set_element(env, arrayValue, i++, CreateJsRectObject(env, rect));
949 }
950 return arrayValue;
951 }
952
OnCreateVirtualScreen(napi_env env,napi_callback_info info)953 napi_value OnCreateVirtualScreen(napi_env env, napi_callback_info info)
954 {
955 TLOGI(WmsLogTag::DMS, "called");
956 DmErrorCode errCode = DmErrorCode::DM_OK;
957 VirtualScreenOption option;
958 size_t argc = 4;
959 std::string errMsg = "";
960 napi_value argv[4] = {nullptr};
961 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
962 if (argc < ARGC_ONE) {
963 errMsg = "Invalid args count, need one arg at least!";
964 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
965 } else {
966 napi_value object = argv[0];
967 if (object == nullptr) {
968 errMsg = "Failed to get options, options is nullptr";
969 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
970 } else {
971 errCode = GetVirtualScreenOptionFromJs(env, object, option);
972 }
973 }
974 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM) {
975 return NapiThrowError(env, DmErrorCode::DM_ERROR_INVALID_PARAM, errMsg);
976 }
977 napi_value lastParam = nullptr;
978 if (argc >= ARGC_TWO && argv[ARGC_TWO - 1] != nullptr &&
979 GetType(env, argv[ARGC_TWO - 1]) == napi_function) {
980 lastParam = argv[ARGC_TWO - 1];
981 }
982 napi_value result = nullptr;
983 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
984 auto asyncTask = [option, env, task = napiAsyncTask.get()]() {
985 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "JsDisplayManager::OnCreateVirtualScreen");
986 auto screenId = SingletonContainer::Get<ScreenManager>().CreateVirtualScreen(option);
987 auto screen = SingletonContainer::Get<ScreenManager>().GetScreenById(screenId);
988 if (screen == nullptr) {
989 DmErrorCode ret = DmErrorCode::DM_ERROR_INVALID_SCREEN;
990 if (screenId == ERROR_ID_NOT_SYSTEM_APP) {
991 ret = DmErrorCode::DM_ERROR_NO_PERMISSION;
992 }
993 task->Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "CreateVirtualScreen failed."));
994 TLOGNE(WmsLogTag::DMS, "JsDisplayManager::CreateVirtualScreen failed.");
995 } else {
996 task->Resolve(env, CreateJsValue(env, static_cast<int64_t>(screenId)));
997 TLOGNI(WmsLogTag::DMS, "JsDisplayManager::OnCreateVirtualScreen success");
998 }
999 delete task;
1000 };
1001 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnCreateVirtualScreen");
1002 return result;
1003 }
1004
OnMakeUnique(napi_env env,napi_callback_info info)1005 napi_value OnMakeUnique(napi_env env, napi_callback_info info)
1006 {
1007 TLOGI(WmsLogTag::DMS, "called");
1008 size_t argc = 4;
1009 int64_t screenId = -1LL;
1010 DmErrorCode errCode = DmErrorCode::DM_OK;
1011 std::string errMsg = "";
1012 napi_value argv[4] = {nullptr};
1013 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1014 if (argc < ARGC_ONE) {
1015 TLOGE(WmsLogTag::DMS, "[NAPI]Argc is invalid: %{public}zu", argc);
1016 errMsg = "Invalid args count, need one arg at least!";
1017 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1018 } else {
1019 if (!ConvertFromJsValue(env, argv[0], screenId) || static_cast<int32_t>(screenId) < 0) {
1020 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to screen id.");
1021 errMsg = "Failed to convert parameter to screen id.";
1022 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1023 }
1024 }
1025 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM || screenId == -1LL) {
1026 TLOGE(WmsLogTag::DMS, "JsDisplayManager::OnMakeUnique failed, Invalidate params.");
1027 return NapiThrowError(env, DmErrorCode::DM_ERROR_INVALID_PARAM, errMsg);
1028 }
1029 std::vector<ScreenId> screenIds;
1030 screenIds.emplace_back(static_cast<ScreenId>(screenId));
1031 napi_value lastParam = nullptr;
1032 napi_value result = nullptr;
1033 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1034 auto asyncTask = [this, screenIds, env, task = napiAsyncTask.get()]() {
1035 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "JsDisplayManager::OnMakeUnique");
1036 std::vector<DisplayId> displayIds;
1037 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
1038 SingletonContainer::Get<ScreenManager>().MakeUniqueScreen(screenIds, displayIds));
1039 ret = (ret == DmErrorCode::DM_ERROR_NOT_SYSTEM_APP) ? DmErrorCode::DM_ERROR_NO_PERMISSION : ret;
1040 if (ret == DmErrorCode::DM_OK) {
1041 task->Resolve(env, NapiGetUndefined(env));
1042 TLOGNI(WmsLogTag::DMS, "makeUnique success");
1043 } else {
1044 task->Reject(env,
1045 CreateJsError(env, static_cast<int32_t>(ret), "JsDisplayManager::OnMakeUnique failed."));
1046 }
1047 delete task;
1048 };
1049 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnMakeUnique");
1050 return result;
1051 }
1052
OnDestroyVirtualScreen(napi_env env,napi_callback_info info)1053 napi_value OnDestroyVirtualScreen(napi_env env, napi_callback_info info)
1054 {
1055 TLOGI(WmsLogTag::DMS, "called");
1056 DmErrorCode errCode = DmErrorCode::DM_OK;
1057 int64_t screenId = -1LL;
1058 std::string errMsg = "";
1059 size_t argc = 4;
1060 napi_value argv[4] = {nullptr};
1061 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1062 if (argc < ARGC_ONE) {
1063 TLOGE(WmsLogTag::DMS, "[NAPI]Argc is invalid: %{public}zu", argc);
1064 errMsg = "Invalid args count, need one arg at least!";
1065 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1066 } else {
1067 if (!ConvertFromJsValue(env, argv[0], screenId)) {
1068 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to screen id.");
1069 errMsg = "Failed to convert parameter to screen id.";
1070 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1071 }
1072 }
1073 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM || screenId == -1LL) {
1074 TLOGE(WmsLogTag::DMS, "JsDisplayManager::OnDestroyVirtualScreen failed, Invalidate params.");
1075 return NapiThrowError(env, DmErrorCode::DM_ERROR_INVALID_PARAM, errMsg);
1076 }
1077 napi_value lastParam = nullptr;
1078 if (argc >= ARGC_TWO && argv[ARGC_TWO - 1] != nullptr &&
1079 GetType(env, argv[ARGC_TWO - 1]) == napi_function) {
1080 lastParam = argv[ARGC_TWO - 1];
1081 }
1082 napi_value result = nullptr;
1083 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1084 auto asyncTask = [screenId, env, task = napiAsyncTask.get()]() {
1085 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "JsDisplayManager::OnDestroyVirtualScreen");
1086 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
1087 SingletonContainer::Get<ScreenManager>().DestroyVirtualScreen(screenId));
1088 res = (res == DmErrorCode::DM_ERROR_NOT_SYSTEM_APP) ? DmErrorCode::DM_ERROR_NO_PERMISSION : res;
1089 if (res != DmErrorCode::DM_OK) {
1090 task->Reject(env, CreateJsError(env, static_cast<int32_t>(res),
1091 "JsDisplayManager::DestroyVirtualScreen failed."));
1092 TLOGNE(WmsLogTag::DMS, "JsDisplayManager::DestroyVirtualScreen failed.");
1093 delete task;
1094 return;
1095 }
1096 task->Resolve(env, NapiGetUndefined(env));
1097 TLOGNI(WmsLogTag::DMS, "JsDisplayManager::OnDestroyVirtualScreen success");
1098 delete task;
1099 };
1100 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnDestroyVirtualScreen");
1101 return result;
1102 }
1103
OnSetVirtualScreenSurface(napi_env env,napi_callback_info info)1104 napi_value OnSetVirtualScreenSurface(napi_env env, napi_callback_info info)
1105 {
1106 TLOGI(WmsLogTag::DMS, "called");
1107 DmErrorCode errCode = DmErrorCode::DM_OK;
1108 int64_t screenId = -1LL;
1109 sptr<Surface> surface;
1110 size_t argc = 4;
1111 std::string errMsg = "";
1112 napi_value argv[4] = {nullptr};
1113 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1114 if (argc < ARGC_TWO) {
1115 TLOGE(WmsLogTag::DMS, "[NAPI]Argc is invalid: %{public}zu", argc);
1116 errMsg = "Invalid args count, need 2 args at least!";
1117 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1118 } else {
1119 if (!ConvertFromJsValue(env, argv[0], screenId)) {
1120 errMsg = "Failed to convert parameter to screen id.";
1121 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1122 }
1123 if (!GetSurfaceFromJs(env, argv[1], surface)) {
1124 errMsg = "Failed to convert parameter.";
1125 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
1126 }
1127 }
1128 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM || surface == nullptr) {
1129 return NapiThrowError(env, DmErrorCode::DM_ERROR_INVALID_PARAM, errMsg);
1130 }
1131 napi_value lastParam = nullptr;
1132 if (argc >= ARGC_THREE && argv[ARGC_THREE - 1] != nullptr &&
1133 GetType(env, argv[ARGC_THREE - 1]) == napi_function) {
1134 lastParam = argv[ARGC_THREE - 1];
1135 }
1136 napi_value result = nullptr;
1137 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1138 auto asyncTask = [screenId, surface, env, task = napiAsyncTask.get()]() {
1139 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "JsDisplayManager::OnSetVirtualScreenSurface");
1140 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
1141 SingletonContainer::Get<ScreenManager>().SetVirtualScreenSurface(screenId, surface));
1142 res = (res == DmErrorCode::DM_ERROR_NOT_SYSTEM_APP) ? DmErrorCode::DM_ERROR_NO_PERMISSION : res;
1143 if (res != DmErrorCode::DM_OK) {
1144 task->Reject(env, CreateJsError(env, static_cast<int32_t>(res),
1145 "JsDisplayManager::SetVirtualScreenSurface failed."));
1146 TLOGNE(WmsLogTag::DMS, "JsDisplayManager::SetVirtualScreenSurface failed.");
1147 } else {
1148 task->Resolve(env, NapiGetUndefined(env));
1149 }
1150 delete task;
1151 };
1152 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "OnSetVirtualScreenSurface");
1153 return result;
1154 }
1155
OnAddVirtualScreenBlockList(napi_env env,napi_callback_info info)1156 napi_value OnAddVirtualScreenBlockList(napi_env env, napi_callback_info info)
1157 {
1158 TLOGD(WmsLogTag::DMS, "in");
1159 size_t argc = ARGC_THREE;
1160 napi_value argv[ARGC_THREE] = {nullptr};
1161 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1162 if (argc < ARGC_ONE) {
1163 TLOGE(WmsLogTag::DMS, "[NAPI]Argc is invalid: %{public}zu", argc);
1164 return NapiThrowError(
1165 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Invalid parameter count");
1166 }
1167 napi_value nativeArray = argv[0];
1168 uint32_t size = 0;
1169 if (GetType(env, nativeArray) != napi_object ||
1170 napi_get_array_length(env, nativeArray, &size) == napi_invalid_arg) {
1171 return NapiThrowError(
1172 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Failed to convert parameter to black list array");
1173 }
1174 std::vector<int32_t> persistentIds;
1175 for (uint32_t i = 0; i < size; i++) {
1176 int32_t persistentId = 0;
1177 napi_value element = nullptr;
1178 napi_get_element(env, nativeArray, i, &element);
1179 if (!ConvertFromJsValue(env, element, persistentId)) {
1180 return NapiThrowError(
1181 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Failed to convert parameter to persistent id");
1182 }
1183 persistentIds.push_back(persistentId);
1184 }
1185 napi_value result = nullptr;
1186 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, nullptr, &result);
1187 auto asyncTask = [persistentIds, env, task = napiAsyncTask.get()] {
1188 if (persistentIds.size() == 0) {
1189 TLOGND(WmsLogTag::DMS, "RemoveVirtualScreenBlockList: persistentIds size is 0, no need update");
1190 task->Resolve(env, NapiGetUndefined(env));
1191 return;
1192 }
1193 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
1194 SingletonContainer::Get<ScreenManager>().AddVirtualScreenBlockList(persistentIds));
1195 if (res != DmErrorCode::DM_OK) {
1196 TLOGNE(WmsLogTag::DMS, "failed");
1197 task->Reject(env, CreateJsError(env, static_cast<int32_t>(res), "add black list failed"));
1198 } else {
1199 task->Resolve(env, NapiGetUndefined(env));
1200 }
1201 delete task;
1202 };
1203 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "AddVirtualScreenBlockList");
1204 return result;
1205 }
1206
OnRemoveVirtualScreenBlockList(napi_env env,napi_callback_info info)1207 napi_value OnRemoveVirtualScreenBlockList(napi_env env, napi_callback_info info)
1208 {
1209 TLOGD(WmsLogTag::DMS, "in");
1210 size_t argc = ARGC_THREE;
1211 napi_value argv[ARGC_THREE] = {nullptr};
1212 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1213 if (argc < ARGC_ONE) {
1214 TLOGE(WmsLogTag::DMS, "[NAPI]Argc is invalid: %{public}zu", argc);
1215 return NapiThrowError(
1216 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Invalid parameter count");
1217 }
1218 napi_value nativeArray = argv[0];
1219 uint32_t size = 0;
1220 if (GetType(env, nativeArray) != napi_object ||
1221 napi_get_array_length(env, nativeArray, &size) == napi_invalid_arg) {
1222 return NapiThrowError(
1223 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Failed to convert parameter to black list array");
1224 }
1225 std::vector<int32_t> persistentIds;
1226 for (uint32_t i = 0; i < size; i++) {
1227 int32_t persistentId = 0;
1228 napi_value element = nullptr;
1229 napi_get_element(env, nativeArray, i, &element);
1230 if (!ConvertFromJsValue(env, element, persistentId)) {
1231 return NapiThrowError(
1232 env, DmErrorCode::DM_ERROR_INVALID_PARAM, "Failed to convert parameter to persistent id");
1233 }
1234 persistentIds.push_back(persistentId);
1235 }
1236 napi_value result = nullptr;
1237 std::unique_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, nullptr, &result);
1238 auto asyncTask = [persistentIds, env, task = napiAsyncTask.get()] {
1239 if (persistentIds.size() == 0) {
1240 TLOGND(WmsLogTag::DMS, "RemoveVirtualScreenBlockList: persistentIds size is 0, no need update");
1241 task->Resolve(env, NapiGetUndefined(env));
1242 return;
1243 }
1244 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
1245 SingletonContainer::Get<ScreenManager>().RemoveVirtualScreenBlockList(persistentIds));
1246 if (res != DmErrorCode::DM_OK) {
1247 TLOGNE(WmsLogTag::DMS, "failed");
1248 task->Reject(env, CreateJsError(env, static_cast<int32_t>(res), "remove black list failed"));
1249 } else {
1250 task->Resolve(env, NapiGetUndefined(env));
1251 }
1252 delete task;
1253 };
1254 NapiSendDmsEvent(env, asyncTask, napiAsyncTask, "RemoveVirtualScreenBlockList");
1255 return result;
1256 }
1257
OnConvertGlobalCoordinateToRelative(napi_env env,napi_callback_info info)1258 napi_value OnConvertGlobalCoordinateToRelative(napi_env env, napi_callback_info info)
1259 {
1260 TLOGD(WmsLogTag::DMS, "in");
1261 Position globalPosition;
1262 int64_t displayIdTemp = 0;
1263 size_t argc = ARGC_TWO;
1264 napi_value argv[ARGC_TWO] = {nullptr};
1265 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1266 if (argc < ARGC_ONE) {
1267 return NapiThrowError(env, DmErrorCode::DM_ERROR_ILLEGAL_PARAM, "Invalid args count, need one arg at least!");
1268 }
1269 if (argv[0] == nullptr) {
1270 return NapiThrowError(env, DmErrorCode::DM_ERROR_ILLEGAL_PARAM,
1271 "Failed to get globalPosition, globalPosition is nullptr");
1272 }
1273 if (argc == ARGC_TWO) {
1274 if (!ConvertFromJsValue(env, argv[1], displayIdTemp)) {
1275 return NapiThrowError(env, DmErrorCode::DM_ERROR_ILLEGAL_PARAM,
1276 "Failed to convert displayIdObject to displayId.");
1277 }
1278 if (displayIdTemp < 0) {
1279 return NapiThrowError(env, DmErrorCode::DM_ERROR_ILLEGAL_PARAM, "displayId less than 0.");
1280 }
1281 }
1282 DmErrorCode errCode = DmErrorCode::DM_OK;
1283 errCode = GetPositionFromJs(env, argv[0], globalPosition);
1284 if (errCode != DmErrorCode::DM_OK) {
1285 return NapiThrowError(env, errCode, "Get position from js failed.");
1286 }
1287 RelativePosition relativePosition;
1288 if (argc == ARGC_TWO) {
1289 errCode = DM_JS_TO_ERROR_CODE_MAP.at(
1290 SingletonContainer::Get<DisplayManager>().ConvertGlobalCoordinateToRelativeWithDisplayId(globalPosition,
1291 static_cast<DisplayId>(displayIdTemp), relativePosition));
1292 } else {
1293 errCode = DM_JS_TO_ERROR_CODE_MAP.at(
1294 SingletonContainer::Get<DisplayManager>().ConvertGlobalCoordinateToRelative(globalPosition,
1295 relativePosition));
1296 }
1297 if (errCode != DmErrorCode::DM_OK) {
1298 return NapiThrowError(env, errCode, "Convert global coordinate to relative failed");
1299 }
1300 return CreateJsRelativePositionObject(env, relativePosition);
1301 }
1302
CreateJsRelativePositionObject(napi_env env,RelativePosition & relativePosition)1303 napi_value CreateJsRelativePositionObject(napi_env env, RelativePosition& relativePosition)
1304 {
1305 TLOGD(WmsLogTag::DMS, "called");
1306 napi_value objValue = nullptr;
1307 napi_create_object(env, &objValue);
1308 if (objValue == nullptr) {
1309 TLOGE(WmsLogTag::DMS, "Failed to get object");
1310 return NapiGetUndefined(env);
1311 }
1312 napi_set_named_property(env, objValue, "displayId",
1313 CreateJsValue(env, static_cast<int64_t>(relativePosition.displayId)));
1314 napi_set_named_property(env, objValue, "position",
1315 CreateJsGlobalPositionObject(env, relativePosition.position));
1316 return objValue;
1317 }
1318
OnConvertRelativeCoordinateToGlobal(napi_env env,napi_callback_info info)1319 napi_value OnConvertRelativeCoordinateToGlobal(napi_env env, napi_callback_info info)
1320 {
1321 TLOGD(WmsLogTag::DMS, "in");
1322 DmErrorCode errCode = DmErrorCode::DM_OK;
1323 RelativePosition relativePosition;
1324 std::string errMsg;
1325 size_t argc = ARGC_ONE;
1326 napi_value argv[ARGC_ONE] = {nullptr};
1327 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1328 if (argc < ARGC_ONE) {
1329 return NapiThrowError(env, DmErrorCode::DM_ERROR_ILLEGAL_PARAM, "Invalid args count, need one arg at least!");
1330 }
1331
1332 errCode = GetRelativePositionFromJs(env, argv[0], relativePosition);
1333 if (errCode != DmErrorCode::DM_OK) {
1334 return NapiThrowError(env, errCode, "Failed to get relativePosition!");
1335 }
1336 Position globalPosition;
1337 errCode = DM_JS_TO_ERROR_CODE_MAP.at(
1338 SingletonContainer::Get<DisplayManager>().ConvertRelativeCoordinateToGlobal(relativePosition, globalPosition));
1339 if (errCode != DmErrorCode::DM_OK) {
1340 return NapiThrowError(env, errCode, "Convert relative coordinate to global failed");
1341 }
1342 return CreateJsGlobalPositionObject(env, globalPosition);
1343 }
1344
CreateJsGlobalPositionObject(napi_env env,Position & globalPosition)1345 napi_value CreateJsGlobalPositionObject(napi_env env, Position& globalPosition)
1346 {
1347 TLOGD(WmsLogTag::DMS, "called");
1348 napi_value objValue = nullptr;
1349 napi_create_object(env, &objValue);
1350 if (objValue == nullptr) {
1351 TLOGE(WmsLogTag::DMS, "Failed to get object");
1352 return NapiGetUndefined(env);
1353 }
1354 napi_set_named_property(env, objValue, "x", CreateJsValue(env, globalPosition.x));
1355 napi_set_named_property(env, objValue, "y", CreateJsValue(env, globalPosition.y));
1356 return objValue;
1357 }
1358
GetRelativePositionFromJs(napi_env env,napi_value relativePositionObject,RelativePosition & relativePosition)1359 DmErrorCode GetRelativePositionFromJs(napi_env env, napi_value relativePositionObject,
1360 RelativePosition& relativePosition)
1361 {
1362 TLOGD(WmsLogTag::DMS, "called");
1363 if (relativePositionObject == nullptr) {
1364 TLOGE(WmsLogTag::DMS, "Failed to get relativePosition, relativePosition is nullptr.");
1365 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1366 }
1367 napi_value displayId = nullptr;
1368 napi_get_named_property(env, relativePositionObject, "displayId", &displayId);
1369 int64_t displayIdTemp = 0;
1370 if (!ConvertFromJsValue(env, displayId, displayIdTemp)) {
1371 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to displayId.");
1372 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1373 }
1374 if (displayIdTemp < 0) {
1375 TLOGE(WmsLogTag::DMS, "DisplayId is invalid, less than 0.");
1376 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1377 }
1378 relativePosition.displayId = static_cast<uint64_t>(displayIdTemp);
1379 napi_value position = nullptr;
1380 napi_get_named_property(env, relativePositionObject, "position", &position);
1381 return GetPositionFromJs(env, position, relativePosition.position);
1382 }
1383
GetPositionFromJs(napi_env env,napi_value positionObject,Position & position)1384 DmErrorCode GetPositionFromJs(napi_env env, napi_value positionObject, Position& position)
1385 {
1386 TLOGD(WmsLogTag::DMS, "called");
1387 napi_value positionX = nullptr;
1388 double positionXTemp = 0;
1389 napi_get_named_property(env, positionObject, "x", &positionX);
1390 if (!ConvertFromJsValue(env, positionX, positionXTemp)) {
1391 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to positionX.");
1392 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1393 }
1394 if (positionXTemp < INT32_MIN || positionXTemp > INT32_MAX) {
1395 TLOGE(WmsLogTag::DMS, "Coordinate x exceeded the range!");
1396 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1397 }
1398
1399 napi_value positionY = nullptr;
1400 double positionYTemp = 0;
1401 napi_get_named_property(env, positionObject, "y", &positionY);
1402 if (!ConvertFromJsValue(env, positionY, positionYTemp)) {
1403 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to positionY.");
1404 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1405 }
1406 if (positionYTemp < INT32_MIN || positionYTemp > INT32_MAX) {
1407 TLOGE(WmsLogTag::DMS, "Coordinate y exceeded the range!");
1408 return DmErrorCode::DM_ERROR_ILLEGAL_PARAM;
1409 }
1410
1411 position.x = static_cast<int32_t>(positionXTemp);
1412 position.y = static_cast<int32_t>(positionYTemp);
1413 return DmErrorCode::DM_OK;
1414 }
1415
GetVirtualScreenOptionFromJs(napi_env env,napi_value optionObject,VirtualScreenOption & option)1416 DmErrorCode GetVirtualScreenOptionFromJs(napi_env env, napi_value optionObject, VirtualScreenOption& option)
1417 {
1418 napi_value name = nullptr;
1419 napi_get_named_property(env, optionObject, "name", &name);
1420 if (!ConvertFromJsValue(env, name, option.name_)) {
1421 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to name.");
1422 return DmErrorCode::DM_ERROR_INVALID_PARAM;
1423 }
1424 napi_value width = nullptr;
1425 napi_get_named_property(env, optionObject, "width", &width);
1426 if (!ConvertFromJsValue(env, width, option.width_)) {
1427 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to width.");
1428 return DmErrorCode::DM_ERROR_INVALID_PARAM;
1429 }
1430 napi_value height = nullptr;
1431 napi_get_named_property(env, optionObject, "height", &height);
1432 if (!ConvertFromJsValue(env, height, option.height_)) {
1433 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to height.");
1434 return DmErrorCode::DM_ERROR_INVALID_PARAM;
1435 }
1436 napi_value density = nullptr;
1437 napi_get_named_property(env, optionObject, "density", &density);
1438 double densityValue;
1439 if (!ConvertFromJsValue(env, density, densityValue)) {
1440 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to density.");
1441 return DmErrorCode::DM_ERROR_INVALID_PARAM;
1442 }
1443 option.density_ = static_cast<float>(densityValue);
1444
1445 napi_value surfaceIdNapiValue = nullptr;
1446 napi_get_named_property(env, optionObject, "surfaceId", &surfaceIdNapiValue);
1447 if (!GetSurfaceFromJs(env, surfaceIdNapiValue, option.surface_)) {
1448 return DmErrorCode::DM_ERROR_INVALID_PARAM;
1449 }
1450 return DmErrorCode::DM_OK;
1451 }
1452
GetSurfaceFromJs(napi_env env,napi_value surfaceIdNapiValue,sptr<Surface> & surface)1453 bool GetSurfaceFromJs(napi_env env, napi_value surfaceIdNapiValue, sptr<Surface>& surface)
1454 {
1455 if (surfaceIdNapiValue == nullptr || GetType(env, surfaceIdNapiValue) != napi_string) {
1456 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to surface. Invalidate params.");
1457 return false;
1458 }
1459
1460 char buffer[PATH_MAX];
1461 size_t length = 0;
1462 uint64_t surfaceId = 0;
1463 if (napi_get_value_string_utf8(env, surfaceIdNapiValue, buffer, PATH_MAX, &length) != napi_ok) {
1464 TLOGE(WmsLogTag::DMS, "Failed to convert parameter to surface.");
1465 return false;
1466 }
1467 std::istringstream inputStream(buffer);
1468 inputStream >> surfaceId;
1469 surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
1470 if (surface == nullptr) {
1471 TLOGI(WmsLogTag::DMS, "GetSurfaceFromJs failed, surfaceId:%{public}" PRIu64"", surfaceId);
1472 }
1473 return true;
1474 }
1475
NapiThrowError(napi_env env,DmErrorCode errCode,std::string msg="")1476 napi_value NapiThrowError(napi_env env, DmErrorCode errCode, std::string msg = "")
1477 {
1478 napi_throw(env, CreateJsError(env, static_cast<int32_t>(errCode), msg));
1479 return NapiGetUndefined(env);
1480 }
1481 };
1482
InitDisplayState(napi_env env)1483 napi_value InitDisplayState(napi_env env)
1484 {
1485 TLOGD(WmsLogTag::DMS, "InitDisplayState called");
1486
1487 if (env == nullptr) {
1488 TLOGE(WmsLogTag::DMS, "env is nullptr");
1489 return nullptr;
1490 }
1491
1492 napi_value objValue = nullptr;
1493 napi_create_object(env, &objValue);
1494 if (objValue == nullptr) {
1495 TLOGE(WmsLogTag::DMS, "Failed to get object");
1496 return nullptr;
1497 }
1498 napi_set_named_property(env, objValue, "STATE_UNKNOWN",
1499 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_UNKNOWN)));
1500 napi_set_named_property(env, objValue, "STATE_OFF",
1501 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_OFF)));
1502 napi_set_named_property(env, objValue, "STATE_ON",
1503 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_ON)));
1504 napi_set_named_property(env, objValue, "STATE_DOZE",
1505 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_DOZE)));
1506 napi_set_named_property(env, objValue, "STATE_DOZE_SUSPEND",
1507 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_DOZE_SUSPEND)));
1508 napi_set_named_property(env, objValue, "STATE_VR",
1509 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_VR)));
1510 napi_set_named_property(env, objValue, "STATE_ON_SUSPEND",
1511 CreateJsValue(env, static_cast<int32_t>(DisplayStateMode::STATE_ON_SUSPEND)));
1512 return objValue;
1513 }
1514
InitOrientation(napi_env env)1515 napi_value InitOrientation(napi_env env)
1516 {
1517 TLOGD(WmsLogTag::DMS, "InitOrientation called");
1518
1519 if (env == nullptr) {
1520 TLOGE(WmsLogTag::DMS, "env is nullptr");
1521 return nullptr;
1522 }
1523
1524 napi_value objValue = nullptr;
1525 napi_create_object(env, &objValue);
1526 if (objValue == nullptr) {
1527 TLOGE(WmsLogTag::DMS, "Failed to get object");
1528 return nullptr;
1529 }
1530
1531 napi_set_named_property(env, objValue, "PORTRAIT",
1532 CreateJsValue(env, static_cast<uint32_t>(DisplayOrientation::PORTRAIT)));
1533 napi_set_named_property(env, objValue, "LANDSCAPE",
1534 CreateJsValue(env, static_cast<uint32_t>(DisplayOrientation::LANDSCAPE)));
1535 napi_set_named_property(env, objValue, "PORTRAIT_INVERTED",
1536 CreateJsValue(env, static_cast<uint32_t>(DisplayOrientation::PORTRAIT_INVERTED)));
1537 napi_set_named_property(env, objValue, "LANDSCAPE_INVERTED",
1538 CreateJsValue(env, static_cast<uint32_t>(DisplayOrientation::LANDSCAPE_INVERTED)));
1539 return objValue;
1540 }
1541
InitDisplayErrorCode(napi_env env)1542 napi_value InitDisplayErrorCode(napi_env env)
1543 {
1544 TLOGD(WmsLogTag::DMS, "InitDisplayErrorCode called");
1545
1546 if (env == nullptr) {
1547 TLOGE(WmsLogTag::DMS, "env is nullptr");
1548 return nullptr;
1549 }
1550
1551 napi_value objValue = nullptr;
1552 napi_create_object(env, &objValue);
1553 if (objValue == nullptr) {
1554 TLOGE(WmsLogTag::DMS, "Failed to get object");
1555 return nullptr;
1556 }
1557
1558 napi_set_named_property(env, objValue, "DM_ERROR_NO_PERMISSION",
1559 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_NO_PERMISSION)));
1560 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_PARAM",
1561 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
1562 napi_set_named_property(env, objValue, "DM_ERROR_DEVICE_NOT_SUPPORT",
1563 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT)));
1564 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_SCREEN",
1565 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN)));
1566 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_CALLING",
1567 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_CALLING)));
1568 napi_set_named_property(env, objValue, "DM_ERROR_SYSTEM_INNORMAL",
1569 CreateJsValue(env, static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL)));
1570
1571 return objValue;
1572 }
1573
InitDisplayError(napi_env env)1574 napi_value InitDisplayError(napi_env env)
1575 {
1576 TLOGD(WmsLogTag::DMS, "InitDisplayError called");
1577
1578 if (env == nullptr) {
1579 TLOGE(WmsLogTag::DMS, "env is nullptr");
1580 return nullptr;
1581 }
1582
1583 napi_value objValue = nullptr;
1584 napi_create_object(env, &objValue);
1585 if (objValue == nullptr) {
1586 TLOGE(WmsLogTag::DMS, "Failed to get object");
1587 return nullptr;
1588 }
1589
1590 napi_set_named_property(env, objValue, "DM_ERROR_INIT_DMS_PROXY_LOCKED",
1591 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED)));
1592 napi_set_named_property(env, objValue, "DM_ERROR_IPC_FAILED",
1593 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_IPC_FAILED)));
1594 napi_set_named_property(env, objValue, "DM_ERROR_REMOTE_CREATE_FAILED",
1595 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_REMOTE_CREATE_FAILED)));
1596 napi_set_named_property(env, objValue, "DM_ERROR_NULLPTR",
1597 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_NULLPTR)));
1598 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_PARAM",
1599 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_INVALID_PARAM)));
1600 napi_set_named_property(env, objValue, "DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED",
1601 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED)));
1602 napi_set_named_property(env, objValue, "DM_ERROR_DEATH_RECIPIENT",
1603 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_DEATH_RECIPIENT)));
1604 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_MODE_ID",
1605 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_INVALID_MODE_ID)));
1606 napi_set_named_property(env, objValue, "DM_ERROR_WRITE_DATA_FAILED",
1607 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_WRITE_DATA_FAILED)));
1608 napi_set_named_property(env, objValue, "DM_ERROR_RENDER_SERVICE_FAILED",
1609 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_RENDER_SERVICE_FAILED)));
1610 napi_set_named_property(env, objValue, "DM_ERROR_UNREGISTER_AGENT_FAILED",
1611 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_UNREGISTER_AGENT_FAILED)));
1612 napi_set_named_property(env, objValue, "DM_ERROR_INVALID_CALLING",
1613 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_INVALID_CALLING)));
1614 napi_set_named_property(env, objValue, "DM_ERROR_UNKNOWN",
1615 CreateJsValue(env, static_cast<int32_t>(DMError::DM_ERROR_UNKNOWN)));
1616
1617 return objValue;
1618 }
1619
InitFoldStatus(napi_env env)1620 napi_value InitFoldStatus(napi_env env)
1621 {
1622 TLOGD(WmsLogTag::DMS, "InitFoldStatus called");
1623
1624 if (env == nullptr) {
1625 TLOGE(WmsLogTag::DMS, "env is nullptr");
1626 return nullptr;
1627 }
1628
1629 napi_value objValue = nullptr;
1630 napi_create_object(env, &objValue);
1631 if (objValue == nullptr) {
1632 TLOGE(WmsLogTag::DMS, "Failed to get object");
1633 return nullptr;
1634 }
1635 napi_set_named_property(env, objValue, "FOLD_STATUS_UNKNOWN",
1636 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::UNKNOWN)));
1637 napi_set_named_property(env, objValue, "FOLD_STATUS_EXPANDED",
1638 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::EXPAND)));
1639 napi_set_named_property(env, objValue, "FOLD_STATUS_FOLDED",
1640 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLDED)));
1641 napi_set_named_property(env, objValue, "FOLD_STATUS_HALF_FOLDED",
1642 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::HALF_FOLD)));
1643 napi_set_named_property(env, objValue, "FOLD_STATUS_EXPANDED_WITH_SECOND_EXPANDED",
1644 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_EXPAND)));
1645 napi_set_named_property(env, objValue, "FOLD_STATUS_EXPANDED_WITH_SECOND_HALF_FOLDED",
1646 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_HALF_FOLDED)));
1647 napi_set_named_property(env, objValue, "FOLD_STATUS_FOLDED_WITH_SECOND_EXPANDED",
1648 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_EXPAND)));
1649 napi_set_named_property(env, objValue, "FOLD_STATUS_FOLDED_WITH_SECOND_HALF_FOLDED",
1650 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_HALF_FOLDED)));
1651 napi_set_named_property(env, objValue, "FOLD_STATUS_HALF_FOLDED_WITH_SECOND_EXPANDED",
1652 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_EXPAND)));
1653 napi_set_named_property(env, objValue, "FOLD_STATUS_HALF_FOLDED_WITH_SECOND_HALF_FOLDED",
1654 CreateJsValue(env, static_cast<uint32_t>(FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_HALF_FOLDED)));
1655 return objValue;
1656 }
1657
InitFoldDisplayMode(napi_env env)1658 napi_value InitFoldDisplayMode(napi_env env)
1659 {
1660 TLOGD(WmsLogTag::DMS, "IniFoldDisplayMode called");
1661
1662 if (env == nullptr) {
1663 TLOGE(WmsLogTag::DMS, "env is nullptr");
1664 return nullptr;
1665 }
1666
1667 napi_value objValue = nullptr;
1668 napi_create_object(env, &objValue);
1669 if (objValue == nullptr) {
1670 TLOGE(WmsLogTag::DMS, "Failed to get object");
1671 return nullptr;
1672 }
1673
1674 napi_set_named_property(env, objValue, "FOLD_DISPLAY_MODE_UNKNOWN",
1675 CreateJsValue(env, static_cast<uint32_t>(FoldDisplayMode::UNKNOWN)));
1676 napi_set_named_property(env, objValue, "FOLD_DISPLAY_MODE_FULL",
1677 CreateJsValue(env, static_cast<uint32_t>(FoldDisplayMode::FULL)));
1678 napi_set_named_property(env, objValue, "FOLD_DISPLAY_MODE_MAIN",
1679 CreateJsValue(env, static_cast<uint32_t>(FoldDisplayMode::MAIN)));
1680 napi_set_named_property(env, objValue, "FOLD_DISPLAY_MODE_SUB",
1681 CreateJsValue(env, static_cast<uint32_t>(FoldDisplayMode::SUB)));
1682 napi_set_named_property(env, objValue, "FOLD_DISPLAY_MODE_COORDINATION",
1683 CreateJsValue(env, static_cast<uint32_t>(FoldDisplayMode::COORDINATION)));
1684 return objValue;
1685 }
1686
InitColorSpace(napi_env env)1687 napi_value InitColorSpace(napi_env env)
1688 {
1689 TLOGD(WmsLogTag::DMS, "InitColorSpace called");
1690
1691 if (env == nullptr) {
1692 TLOGE(WmsLogTag::DMS, "env is nullptr");
1693 return nullptr;
1694 }
1695
1696 napi_value objValue = nullptr;
1697 napi_create_object(env, &objValue);
1698 if (objValue == nullptr) {
1699 TLOGE(WmsLogTag::DMS, "Failed to get object");
1700 return nullptr;
1701 }
1702
1703 napi_set_named_property(env, objValue, "UNKNOWN",
1704 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::UNKNOWN)));
1705 napi_set_named_property(env, objValue, "ADOBE_RGB",
1706 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::ADOBE_RGB)));
1707 napi_set_named_property(env, objValue, "BT2020_HLG",
1708 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::BT2020_HLG)));
1709 napi_set_named_property(env, objValue, "BT2020_PQ",
1710 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::BT2020_PQ)));
1711 napi_set_named_property(env, objValue, "BT601_EBU",
1712 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::BT601_EBU)));
1713 napi_set_named_property(env, objValue, "BT601_SMPTE_C",
1714 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::BT601_SMPTE_C)));
1715 napi_set_named_property(env, objValue, "BT709",
1716 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::BT709)));
1717 napi_set_named_property(env, objValue, "P3_HLG",
1718 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::P3_HLG)));
1719 napi_set_named_property(env, objValue, "P3_PQ",
1720 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::P3_PQ)));
1721 napi_set_named_property(env, objValue, "DISPLAY_P3",
1722 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::DISPLAY_P3)));
1723 napi_set_named_property(env, objValue, "SRGB",
1724 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::SRGB)));
1725 napi_set_named_property(env, objValue, "LINEAR_SRGB",
1726 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::LINEAR_SRGB)));
1727 napi_set_named_property(env, objValue, "LINEAR_P3",
1728 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::LINEAR_P3)));
1729 napi_set_named_property(env, objValue, "LINEAR_BT2020",
1730 CreateJsValue(env, static_cast<uint32_t>(DmsColorSpace::LINEAR_BT2020)));
1731 return objValue;
1732 }
1733
InitHDRFormat(napi_env env)1734 napi_value InitHDRFormat(napi_env env)
1735 {
1736 TLOGD(WmsLogTag::DMS, "InitHDRFormat called");
1737
1738 if (env == nullptr) {
1739 TLOGE(WmsLogTag::DMS, "env is nullptr");
1740 return nullptr;
1741 }
1742
1743 napi_value objValue = nullptr;
1744 napi_create_object(env, &objValue);
1745 if (objValue == nullptr) {
1746 TLOGE(WmsLogTag::DMS, "Failed to get object");
1747 return nullptr;
1748 }
1749
1750 napi_set_named_property(env, objValue, "NONE",
1751 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::NONE)));
1752 napi_set_named_property(env, objValue, "VIDEO_HLG",
1753 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::VIDEO_HLG)));
1754 napi_set_named_property(env, objValue, "VIDEO_HDR10",
1755 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::VIDEO_HDR10)));
1756 napi_set_named_property(env, objValue, "VIDEO_HDR_VIVID",
1757 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::VIDEO_HDR_VIVID)));
1758 napi_set_named_property(env, objValue, "IMAGE_HDR_VIVID_DUAL",
1759 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::IMAGE_HDR_VIVID_DUAL)));
1760 napi_set_named_property(env, objValue, "IMAGE_HDR_VIVID_SINGLE",
1761 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::IMAGE_HDR_VIVID_SINGLE)));
1762 napi_set_named_property(env, objValue, "IMAGE_HDR_ISO_DUAL",
1763 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::IMAGE_HDR_ISO_DUAL)));
1764 napi_set_named_property(env, objValue, "IMAGE_HDR_ISO_SINGLE",
1765 CreateJsValue(env, static_cast<uint32_t>(HDRFormat::IMAGE_HDR_ISO_SINGLE)));
1766 return objValue;
1767 }
1768
InitScreenShape(napi_env env)1769 napi_value InitScreenShape(napi_env env)
1770 {
1771 TLOGD(WmsLogTag::DMS, "InitScreenShape called");
1772
1773 if (env == nullptr) {
1774 TLOGE(WmsLogTag::DMS, "env is nullptr");
1775 return nullptr;
1776 }
1777
1778 napi_value objValue = nullptr;
1779 napi_create_object(env, &objValue);
1780 if (objValue == nullptr) {
1781 TLOGE(WmsLogTag::DMS, "Failed to get object");
1782 return nullptr;
1783 }
1784
1785 napi_set_named_property(env, objValue, "RECTANGLE",
1786 CreateJsValue(env, static_cast<uint32_t>(ScreenShape::RECTANGLE)));
1787 napi_set_named_property(env, objValue, "ROUND",
1788 CreateJsValue(env, static_cast<uint32_t>(ScreenShape::ROUND)));
1789 return objValue;
1790 }
1791
InitDisplaySourceMode(napi_env env)1792 napi_value InitDisplaySourceMode(napi_env env)
1793 {
1794 TLOGD(WmsLogTag::DMS, "InitDisplaySourceMode called");
1795
1796 if (env == nullptr) {
1797 TLOGE(WmsLogTag::DMS, "env is nullptr");
1798 return nullptr;
1799 }
1800
1801 napi_value objValue = nullptr;
1802 napi_create_object(env, &objValue);
1803 if (objValue == nullptr) {
1804 TLOGE(WmsLogTag::DMS, "Failed to get object");
1805 return nullptr;
1806 }
1807
1808 napi_set_named_property(env, objValue, "NONE",
1809 CreateJsValue(env, static_cast<uint32_t>(DisplaySourceMode::NONE)));
1810 napi_set_named_property(env, objValue, "MAIN",
1811 CreateJsValue(env, static_cast<uint32_t>(DisplaySourceMode::MAIN)));
1812 napi_set_named_property(env, objValue, "MIRROR",
1813 CreateJsValue(env, static_cast<uint32_t>(DisplaySourceMode::MIRROR)));
1814 napi_set_named_property(env, objValue, "EXTEND",
1815 CreateJsValue(env, static_cast<uint32_t>(DisplaySourceMode::EXTEND)));
1816 napi_set_named_property(env, objValue, "ALONE",
1817 CreateJsValue(env, static_cast<uint32_t>(DisplaySourceMode::ALONE)));
1818 return objValue;
1819 }
1820
BindCoordinateConvertNativeFunction(napi_env env,napi_value exportObj,const char * moduleName)1821 static void BindCoordinateConvertNativeFunction(napi_env env, napi_value exportObj, const char* moduleName)
1822 {
1823 BindNativeFunction(env, exportObj, "convertRelativeToGlobalCoordinate", moduleName,
1824 JsDisplayManager::ConvertRelativeCoordinateToGlobal);
1825 BindNativeFunction(env, exportObj, "convertGlobalToRelativeCoordinate", moduleName,
1826 JsDisplayManager::ConvertGlobalCoordinateToRelative);
1827 }
1828
JsDisplayManagerInit(napi_env env,napi_value exportObj)1829 napi_value JsDisplayManagerInit(napi_env env, napi_value exportObj)
1830 {
1831 TLOGD(WmsLogTag::DMS, "JsDisplayManagerInit is called");
1832
1833 if (env == nullptr || exportObj == nullptr) {
1834 TLOGE(WmsLogTag::DMS, "JsDisplayManagerInit env or exportObj is nullptr");
1835 return nullptr;
1836 }
1837
1838 std::unique_ptr<JsDisplayManager> jsDisplayManager = std::make_unique<JsDisplayManager>(env);
1839 napi_wrap(env, exportObj, jsDisplayManager.release(), JsDisplayManager::Finalizer, nullptr, nullptr);
1840
1841 napi_set_named_property(env, exportObj, "DisplayState", InitDisplayState(env));
1842 napi_set_named_property(env, exportObj, "Orientation", InitOrientation(env));
1843 napi_set_named_property(env, exportObj, "DmErrorCode", InitDisplayErrorCode(env));
1844 napi_set_named_property(env, exportObj, "DMError", InitDisplayError(env));
1845 napi_set_named_property(env, exportObj, "FoldStatus", InitFoldStatus(env));
1846 napi_set_named_property(env, exportObj, "FoldDisplayMode", InitFoldDisplayMode(env));
1847 napi_set_named_property(env, exportObj, "ColorSpace", InitColorSpace(env));
1848 napi_set_named_property(env, exportObj, "HDRFormat", InitHDRFormat(env));
1849 napi_set_named_property(env, exportObj, "ScreenShape", InitScreenShape(env));
1850 napi_set_named_property(env, exportObj, "DisplaySourceMode", InitDisplaySourceMode(env));
1851
1852 const char *moduleName = "JsDisplayManager";
1853 BindNativeFunction(env, exportObj, "getDefaultDisplay", moduleName, JsDisplayManager::GetDefaultDisplay);
1854 BindNativeFunction(env, exportObj, "getDefaultDisplaySync", moduleName, JsDisplayManager::GetDefaultDisplaySync);
1855 BindNativeFunction(env, exportObj, "getPrimaryDisplaySync", moduleName, JsDisplayManager::GetPrimaryDisplaySync);
1856 BindNativeFunction(env, exportObj, "getDisplayByIdSync", moduleName, JsDisplayManager::GetDisplayByIdSync);
1857 BindNativeFunction(env, exportObj, "getAllDisplay", moduleName, JsDisplayManager::GetAllDisplay);
1858 BindNativeFunction(env, exportObj, "getAllDisplays", moduleName, JsDisplayManager::GetAllDisplays);
1859 BindNativeFunction(env, exportObj, "hasPrivateWindow", moduleName, JsDisplayManager::HasPrivateWindow);
1860 BindNativeFunction(env, exportObj, "isFoldable", moduleName, JsDisplayManager::IsFoldable);
1861 BindNativeFunction(env, exportObj, "isCaptured", moduleName, JsDisplayManager::IsCaptured);
1862 BindNativeFunction(env, exportObj, "getFoldStatus", moduleName, JsDisplayManager::GetFoldStatus);
1863 BindNativeFunction(env, exportObj, "getFoldDisplayMode", moduleName, JsDisplayManager::GetFoldDisplayMode);
1864 BindNativeFunction(env, exportObj, "setFoldDisplayMode", moduleName, JsDisplayManager::SetFoldDisplayMode);
1865 BindNativeFunction(env, exportObj, "setFoldStatusLocked", moduleName, JsDisplayManager::SetFoldStatusLocked);
1866 BindNativeFunction(env, exportObj, "getCurrentFoldCreaseRegion", moduleName,
1867 JsDisplayManager::GetCurrentFoldCreaseRegion);
1868 BindNativeFunction(env, exportObj, "on", moduleName, JsDisplayManager::RegisterDisplayManagerCallback);
1869 BindNativeFunction(env, exportObj, "off", moduleName, JsDisplayManager::UnregisterDisplayManagerCallback);
1870 BindNativeFunction(env, exportObj, "getAllDisplayPhysicalResolution", moduleName,
1871 JsDisplayManager::GetAllDisplayPhysicalResolution);
1872 BindNativeFunction(env, exportObj, "createVirtualScreen", moduleName, JsDisplayManager::CreateVirtualScreen);
1873 BindNativeFunction(env, exportObj, "makeUnique", moduleName, JsDisplayManager::MakeUnique);
1874 BindNativeFunction(env, exportObj, "destroyVirtualScreen", moduleName, JsDisplayManager::DestroyVirtualScreen);
1875 BindNativeFunction(env, exportObj, "setVirtualScreenSurface", moduleName,
1876 JsDisplayManager::SetVirtualScreenSurface);
1877 BindNativeFunction(env, exportObj, "addVirtualScreenBlockList", moduleName,
1878 JsDisplayManager::AddVirtualScreenBlockList);
1879 BindNativeFunction(env, exportObj, "removeVirtualScreenBlockList", moduleName,
1880 JsDisplayManager::RemoveVirtualScreenBlockList);
1881 BindCoordinateConvertNativeFunction(env, exportObj, moduleName);
1882 return NapiGetUndefined(env);
1883 }
1884 } // namespace Rosen
1885 } // namespace OHOS
1886