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 "js_screen_manager.h"
17
18 #include <cinttypes>
19 #include <vector>
20 #include <new>
21 #include "js_runtime_utils.h"
22 #include "js_screen.h"
23 #include "js_screen_listener.h"
24 #include "native_engine/native_reference.h"
25 #include "screen_manager.h"
26 #include "singleton_container.h"
27 #include "surface_utils.h"
28 #include "window_manager_hilog.h"
29
30 namespace OHOS {
31 namespace Rosen {
32 using namespace AbilityRuntime;
33 constexpr size_t ARGC_ONE = 1;
34 constexpr size_t ARGC_TWO = 2;
35 constexpr size_t ARGC_THREE = 3;
36 constexpr int32_t INDEX_ONE = 1;
37 constexpr uint32_t MAX_SCREENS_NUM = 1000;
38 namespace {
39 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "JsScreenManager"};
40 }
41
42 class JsScreenManager {
43 public:
JsScreenManager(NativeEngine * engine)44 explicit JsScreenManager(NativeEngine* engine) {
45 }
46
47 ~JsScreenManager() = default;
48
Finalizer(NativeEngine * engine,void * data,void * hint)49 static void Finalizer(NativeEngine* engine, void* data, void* hint)
50 {
51 WLOGI("JsScreenManager::Finalizer is called");
52 std::unique_ptr<JsScreenManager>(static_cast<JsScreenManager*>(data));
53 }
54
GetAllScreens(NativeEngine * engine,NativeCallbackInfo * info)55 static NativeValue* GetAllScreens(NativeEngine* engine, NativeCallbackInfo* info)
56 {
57 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
58 return (me != nullptr) ? me->OnGetAllScreens(*engine, *info) : nullptr;
59 }
60
RegisterScreenManagerCallback(NativeEngine * engine,NativeCallbackInfo * info)61 static NativeValue* RegisterScreenManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
62 {
63 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
64 return (me != nullptr) ? me->OnRegisterScreenManagerCallback(*engine, *info) : nullptr;
65 }
66
UnregisterScreenMangerCallback(NativeEngine * engine,NativeCallbackInfo * info)67 static NativeValue* UnregisterScreenMangerCallback(NativeEngine* engine, NativeCallbackInfo* info)
68 {
69 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
70 return (me != nullptr) ? me->OnUnregisterScreenManagerCallback(*engine, *info) : nullptr;
71 }
72
MakeMirror(NativeEngine * engine,NativeCallbackInfo * info)73 static NativeValue* MakeMirror(NativeEngine* engine, NativeCallbackInfo* info)
74 {
75 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
76 return (me != nullptr) ? me->OnMakeMirror(*engine, *info) : nullptr;
77 }
78
MakeExpand(NativeEngine * engine,NativeCallbackInfo * info)79 static NativeValue* MakeExpand(NativeEngine* engine, NativeCallbackInfo* info)
80 {
81 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
82 return (me != nullptr) ? me->OnMakeExpand(*engine, *info) : nullptr;
83 }
84
StopMirror(NativeEngine * engine,NativeCallbackInfo * info)85 static NativeValue* StopMirror(NativeEngine* engine, NativeCallbackInfo* info)
86 {
87 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
88 return (me != nullptr) ? me->OnStopMirror(*engine, *info) : nullptr;
89 }
90
StopExpand(NativeEngine * engine,NativeCallbackInfo * info)91 static NativeValue* StopExpand(NativeEngine* engine, NativeCallbackInfo* info)
92 {
93 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
94 return (me != nullptr) ? me->OnStopExpand(*engine, *info) : nullptr;
95 }
96
CreateVirtualScreen(NativeEngine * engine,NativeCallbackInfo * info)97 static NativeValue* CreateVirtualScreen(NativeEngine* engine, NativeCallbackInfo* info)
98 {
99 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
100 return (me != nullptr) ? me->OnCreateVirtualScreen(*engine, *info) : nullptr;
101 }
102
DestroyVirtualScreen(NativeEngine * engine,NativeCallbackInfo * info)103 static NativeValue* DestroyVirtualScreen(NativeEngine* engine, NativeCallbackInfo* info)
104 {
105 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
106 return (me != nullptr) ? me->OnDestroyVirtualScreen(*engine, *info) : nullptr;
107 }
108
SetVirtualScreenSurface(NativeEngine * engine,NativeCallbackInfo * info)109 static NativeValue* SetVirtualScreenSurface(NativeEngine* engine, NativeCallbackInfo* info)
110 {
111 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
112 return (me != nullptr) ? me->OnSetVirtualScreenSurface(*engine, *info) : nullptr;
113 }
114
IsScreenRotationLocked(NativeEngine * engine,NativeCallbackInfo * info)115 static NativeValue* IsScreenRotationLocked(NativeEngine* engine, NativeCallbackInfo* info)
116 {
117 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
118 return (me != nullptr) ? me->OnIsScreenRotationLocked(*engine, *info) : nullptr;
119 }
120
SetScreenRotationLocked(NativeEngine * engine,NativeCallbackInfo * info)121 static NativeValue* SetScreenRotationLocked(NativeEngine* engine, NativeCallbackInfo* info)
122 {
123 JsScreenManager* me = CheckParamsAndGetThis<JsScreenManager>(engine, info);
124 return (me != nullptr) ? me->OnSetScreenRotationLocked(*engine, *info) : nullptr;
125 }
126 private:
127 std::map<std::string, std::map<std::unique_ptr<NativeReference>, sptr<JsScreenListener>>> jsCbMap_;
128 std::mutex mtx_;
129
OnGetAllScreens(NativeEngine & engine,NativeCallbackInfo & info)130 NativeValue* OnGetAllScreens(NativeEngine& engine, NativeCallbackInfo& info)
131 {
132 WLOGI("OnGetAllScreens is called");
133 AsyncTask::CompleteCallback complete =
134 [this](NativeEngine& engine, AsyncTask& task, int32_t status) {
135 std::vector<sptr<Screen>> screens;
136 auto res = DM_JS_TO_ERROR_CODE_MAP.at(SingletonContainer::Get<ScreenManager>().GetAllScreens(screens));
137 if (res != DmErrorCode::DM_OK) {
138 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(res),
139 "JsScreenManager::OnGetAllScreens failed."));
140 } else if (!screens.empty()) {
141 task.Resolve(engine, CreateJsScreenVectorObject(engine, screens));
142 WLOGI("JsScreenManager::OnGetAllScreens success");
143 } else {
144 task.Reject(engine, CreateJsError(engine,
145 static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN),
146 "JsScreenManager::OnGetAllScreens failed."));
147 }
148 };
149 NativeValue* lastParam = nullptr;
150 if (info.argc >= ARGC_ONE && info.argv[ARGC_ONE - 1] != nullptr &&
151 info.argv[ARGC_ONE - 1]->TypeOf() == NATIVE_FUNCTION) {
152 lastParam = info.argv[ARGC_ONE - 1];
153 }
154 NativeValue* result = nullptr;
155 AsyncTask::Schedule("JsScreenManager::OnGetAllScreens",
156 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
157 return result;
158 }
159
CreateJsScreenVectorObject(NativeEngine & engine,std::vector<sptr<Screen>> & screens)160 NativeValue* CreateJsScreenVectorObject(NativeEngine& engine, std::vector<sptr<Screen>>& screens)
161 {
162 NativeValue* arrayValue = engine.CreateArray(screens.size());
163 NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
164 if (array == nullptr) {
165 WLOGFE("Failed to get screens");
166 return engine.CreateUndefined();
167 }
168 size_t i = 0;
169 for (auto& screen : screens) {
170 if (screen == nullptr) {
171 continue;
172 }
173 array->SetElement(i++, CreateJsScreenObject(engine, screen));
174 }
175 return arrayValue;
176 }
177
IfCallbackRegistered(const std::string & type,NativeValue * jsListenerObject)178 bool IfCallbackRegistered(const std::string& type, NativeValue* jsListenerObject)
179 {
180 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
181 WLOGI("JsScreenManager::IfCallbackRegistered methodName %{public}s not registered!", type.c_str());
182 return false;
183 }
184
185 for (auto& iter : jsCbMap_[type]) {
186 if (jsListenerObject->StrictEquals(iter.first->Get())) {
187 WLOGFE("JsScreenManager::IfCallbackRegistered callback already registered!");
188 return true;
189 }
190 }
191 return false;
192 }
193
RegisterScreenListenerWithType(NativeEngine & engine,const std::string & type,NativeValue * value)194 DmErrorCode RegisterScreenListenerWithType(NativeEngine& engine, const std::string& type, NativeValue* value)
195 {
196 if (IfCallbackRegistered(type, value)) {
197 WLOGFE("JsScreenManager::RegisterScreenListenerWithType callback already registered!");
198 return DmErrorCode::DM_ERROR_INVALID_CALLING;
199 }
200 std::unique_ptr<NativeReference> callbackRef;
201 callbackRef.reset(engine.CreateReference(value, 1));
202 sptr<JsScreenListener> screenListener = new(std::nothrow) JsScreenListener(&engine);
203 if (screenListener == nullptr) {
204 WLOGFE("screenListener is nullptr");
205 return DmErrorCode::DM_ERROR_INVALID_SCREEN;
206 }
207 if (type == EVENT_CONNECT || type == EVENT_DISCONNECT || type == EVENT_CHANGE) {
208 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
209 SingletonContainer::Get<ScreenManager>().RegisterScreenListener(screenListener));
210 if (ret != DmErrorCode::DM_OK) {
211 return ret;
212 }
213 WLOGI("JsScreenManager::RegisterScreenListenerWithType success");
214 } else {
215 WLOGFE("JsScreenManager::RegisterScreenListenerWithType failed method: %{public}s not support!",
216 type.c_str());
217 return DmErrorCode::DM_ERROR_INVALID_CALLING;
218 }
219 screenListener->AddCallback(type, value);
220 jsCbMap_[type][std::move(callbackRef)] = screenListener;
221 return DmErrorCode::DM_OK;
222 }
223
UnregisterAllScreenListenerWithType(const std::string & type)224 DmErrorCode UnregisterAllScreenListenerWithType(const std::string& type)
225 {
226 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
227 WLOGFE("JsScreenManager::UnregisterAllScreenListenerWithType methodName %{public}s not registered!",
228 type.c_str());
229 return DmErrorCode::DM_ERROR_INVALID_CALLING;
230 }
231 for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
232 it->second->RemoveAllCallback();
233 if (type == EVENT_CONNECT || type == EVENT_DISCONNECT || type == EVENT_CHANGE) {
234 sptr<ScreenManager::IScreenListener> thisListener(it->second);
235 SingletonContainer::Get<ScreenManager>().UnregisterScreenListener(thisListener);
236 WLOGI("JsScreenManager::UnregisterAllScreenListenerWithType success");
237 }
238 jsCbMap_[type].erase(it++);
239 }
240 jsCbMap_.erase(type);
241 return DmErrorCode::DM_OK;
242 }
243
UnRegisterScreenListenerWithType(const std::string & type,NativeValue * value)244 DmErrorCode UnRegisterScreenListenerWithType(const std::string& type, NativeValue* value)
245 {
246 if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
247 WLOGI("JsScreenManager::UnRegisterScreenListenerWithType methodName %{public}s not registered!",
248 type.c_str());
249 return DmErrorCode::DM_ERROR_INVALID_CALLING;
250 }
251 if (value == nullptr) {
252 WLOGFE("JsScreenManager::UnRegisterScreenListenerWithType value is nullptr");
253 return DmErrorCode::DM_ERROR_INVALID_SCREEN;
254 }
255 DmErrorCode ret = DmErrorCode::DM_OK;
256 for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
257 if (value->StrictEquals(it->first->Get())) {
258 it->second->RemoveCallback(type, value);
259 if (type == EVENT_CONNECT || type == EVENT_DISCONNECT || type == EVENT_CHANGE) {
260 sptr<ScreenManager::IScreenListener> thisListener(it->second);
261 ret = DM_JS_TO_ERROR_CODE_MAP.at(
262 SingletonContainer::Get<ScreenManager>().UnregisterScreenListener(thisListener));
263 }
264 jsCbMap_[type].erase(it++);
265 break;
266 } else {
267 it++;
268 }
269 }
270 if (jsCbMap_[type].empty()) {
271 jsCbMap_.erase(type);
272 }
273 if (ret == DmErrorCode::DM_OK) {
274 WLOGFI("JsScreenManager::UnRegisterScreenListenerWithType success");
275 }
276 return ret;
277 }
278
OnRegisterScreenManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)279 NativeValue* OnRegisterScreenManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
280 {
281 WLOGI("JsScreenManager::OnRegisterScreenManagerCallback is called");
282 if (info.argc < ARGC_TWO) {
283 WLOGFE("Params not match");
284 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
285 return engine.CreateUndefined();
286 }
287 std::string cbType;
288 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
289 WLOGFE("Failed to convert parameter to callbackType");
290 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
291 return engine.CreateUndefined();
292 }
293 NativeValue* value = info.argv[INDEX_ONE];
294 if (value == nullptr) {
295 WLOGI("JsScreenManager::OnRegisterScreenManagerCallback info->argv[1] is nullptr");
296 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
297 return engine.CreateUndefined();
298 }
299 if (!value->IsCallable()) {
300 WLOGI("JsScreenManager::OnRegisterScreenManagerCallback info->argv[1] is not callable");
301 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
302 return engine.CreateUndefined();
303 }
304 std::lock_guard<std::mutex> lock(mtx_);
305 DmErrorCode ret = RegisterScreenListenerWithType(engine, cbType, value);
306 if (ret != DmErrorCode::DM_OK) {
307 WLOGFE("JsScreenManager::OnRegisterScreenManagerCallback failed");
308 engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
309 }
310 return engine.CreateUndefined();
311 }
312
OnUnregisterScreenManagerCallback(NativeEngine & engine,NativeCallbackInfo & info)313 NativeValue* OnUnregisterScreenManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
314 {
315 WLOGI("JsScreenManager::OnUnregisterScreenCallback is called");
316 if (info.argc < ARGC_ONE) {
317 WLOGFE("Params not match");
318 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
319 return engine.CreateUndefined();
320 }
321 std::string cbType;
322 if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
323 WLOGFE("Failed to convert parameter to callbackType");
324 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
325 return engine.CreateUndefined();
326 }
327 std::lock_guard<std::mutex> lock(mtx_);
328 if (info.argc == ARGC_ONE) {
329 UnregisterAllScreenListenerWithType(cbType);
330 } else {
331 NativeValue* value = info.argv[INDEX_ONE];
332 if ((value == nullptr) || (!value->IsCallable())) {
333 DmErrorCode ret = UnregisterAllScreenListenerWithType(cbType);
334 if (ret != DmErrorCode::DM_OK) {
335 WLOGFE("JsScreenManager::OnUnRegisterAllScreenManagerCallback failed");
336 engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
337 }
338 } else {
339 DmErrorCode ret = UnRegisterScreenListenerWithType(cbType, value);
340 if (ret != DmErrorCode::DM_OK) {
341 WLOGFE("JsScreenManager::OnUnRegisterScreenManagerCallback failed");
342 engine.Throw(CreateJsError(engine, static_cast<int32_t>(ret)));
343 }
344 }
345 }
346 return engine.CreateUndefined();
347 }
348
OnMakeMirror(NativeEngine & engine,NativeCallbackInfo & info)349 NativeValue* OnMakeMirror(NativeEngine& engine, NativeCallbackInfo& info)
350 {
351 WLOGI("OnMakeMirror is called");
352 if (info.argc < ARGC_TWO) {
353 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
354 return engine.CreateUndefined();
355 }
356
357 int64_t mainScreenId;
358 if (!ConvertFromJsValue(engine, info.argv[0], mainScreenId)) {
359 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
360 return engine.CreateUndefined();
361 }
362 NativeArray* array = ConvertNativeValueTo<NativeArray>(info.argv[INDEX_ONE]);
363 if (array == nullptr) {
364 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
365 return engine.CreateUndefined();
366 }
367 uint32_t size = array->GetLength();
368 std::vector<ScreenId> screenIds;
369 for (uint32_t i = 0; i < size; i++) {
370 uint32_t screenId;
371 NativeValue* value = array->GetElement(i);
372 if (!ConvertFromJsValue(engine, value, screenId)) {
373 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
374 return engine.CreateUndefined();
375 }
376 screenIds.emplace_back(static_cast<ScreenId>(screenId));
377 }
378
379 AsyncTask::CompleteCallback complete =
380 [mainScreenId, screenIds](NativeEngine& engine, AsyncTask& task, int32_t status) {
381 ScreenId screenGroupId = INVALID_SCREEN_ID;
382 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
383 SingletonContainer::Get<ScreenManager>().MakeMirror(mainScreenId, screenIds, screenGroupId));
384 if (ret == DmErrorCode::DM_OK) {
385 task.Resolve(engine, CreateJsValue(engine, static_cast<uint32_t>(screenGroupId)));
386 } else {
387 task.Reject(engine, CreateJsError(engine,
388 static_cast<int32_t>(ret),
389 "JsScreenManager::OnMakeMirror failed."));
390 }
391 };
392 NativeValue* lastParam = nullptr;
393 if (info.argc >= ARGC_THREE && info.argv[ARGC_THREE - 1] != nullptr &&
394 info.argv[ARGC_THREE - 1]->TypeOf() == NATIVE_FUNCTION) {
395 lastParam = info.argv[ARGC_THREE - 1];
396 }
397 NativeValue* result = nullptr;
398 AsyncTask::Schedule("JsScreenManager::OnMakeMirror",
399 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
400 return result;
401 }
402
OnMakeExpand(NativeEngine & engine,NativeCallbackInfo & info)403 NativeValue* OnMakeExpand(NativeEngine& engine, NativeCallbackInfo& info)
404 {
405 WLOGI("OnMakeExpand is called");
406 if (info.argc < ARGC_ONE) {
407 WLOGFE("Params not match");
408 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
409 return engine.CreateUndefined();
410 }
411
412 NativeArray* array = ConvertNativeValueTo<NativeArray>(info.argv[0]);
413 if (array == nullptr) {
414 WLOGFE("Failed to get options");
415 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
416 return engine.CreateUndefined();
417 }
418 uint32_t size = array->GetLength();
419 std::vector<ExpandOption> options;
420 for (uint32_t i = 0; i < size; ++i) {
421 NativeObject* object = ConvertNativeValueTo<NativeObject>(array->GetElement(i));
422 ExpandOption expandOption;
423 int32_t res = GetExpandOptionFromJs(engine, object, expandOption);
424 if (res == -1) {
425 WLOGE("expandoption param %{public}d error!", i);
426 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
427 return engine.CreateUndefined();
428 }
429 options.emplace_back(expandOption);
430 }
431
432 AsyncTask::CompleteCallback complete =
433 [options](NativeEngine& engine, AsyncTask& task, int32_t status) {
434 ScreenId screenGroupId = INVALID_SCREEN_ID;
435 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
436 SingletonContainer::Get<ScreenManager>().MakeExpand(options, screenGroupId));
437 if (ret == DmErrorCode::DM_OK) {
438 task.Resolve(engine, CreateJsValue(engine, static_cast<uint32_t>(screenGroupId)));
439 WLOGI("MakeExpand success");
440 } else {
441 task.Reject(engine, CreateJsError(engine,
442 static_cast<int32_t>(ret),
443 "JsScreenManager::OnMakeExpand failed."));
444 WLOGFE("MakeExpand failed");
445 }
446 };
447 NativeValue* lastParam = nullptr;
448 if (info.argc >= ARGC_TWO && info.argv[ARGC_TWO - 1] != nullptr &&
449 info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) {
450 lastParam = info.argv[ARGC_TWO - 1];
451 }
452 NativeValue* result = nullptr;
453 AsyncTask::Schedule("JsScreenManager::OnMakeExpand",
454 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
455 return result;
456 }
457
OnStopMirror(NativeEngine & engine,NativeCallbackInfo & info)458 NativeValue* OnStopMirror(NativeEngine& engine, NativeCallbackInfo& info)
459 {
460 WLOGI("OnStopMirror is called");
461 if (info.argc < ARGC_ONE) {
462 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
463 return engine.CreateUndefined();
464 }
465
466 NativeArray* array = ConvertNativeValueTo<NativeArray>(info.argv[0]);
467 if (array == nullptr) {
468 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
469 return engine.CreateUndefined();
470 }
471 uint32_t size = array->GetLength();
472 if (size > MAX_SCREENS_NUM) {
473 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
474 return engine.CreateUndefined();
475 }
476 std::vector<ScreenId> screenIds;
477 for (uint32_t i = 0; i < size; i++) {
478 uint32_t screenId;
479 NativeValue* value = array->GetElement(i);
480 if (!ConvertFromJsValue(engine, value, screenId)) {
481 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
482 return engine.CreateUndefined();
483 }
484 screenIds.emplace_back(static_cast<ScreenId>(screenId));
485 }
486
487 AsyncTask::CompleteCallback complete =
488 [ screenIds](NativeEngine& engine, AsyncTask& task, int32_t status) {
489 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
490 SingletonContainer::Get<ScreenManager>().StopMirror(screenIds));
491 if (ret == DmErrorCode::DM_OK) {
492 task.Resolve(engine, engine.CreateUndefined());
493 } else {
494 task.Reject(engine, CreateJsError(engine,
495 static_cast<int32_t>(ret),
496 "JsScreenManager::OnStopMirror failed."));
497 }
498 };
499 NativeValue* lastParam = nullptr;
500 if (info.argc >= ARGC_TWO && info.argv[ARGC_TWO - 1] != nullptr &&
501 info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) {
502 lastParam = info.argv[ARGC_TWO - 1];
503 }
504 NativeValue* result = nullptr;
505 AsyncTask::Schedule("JsScreenManager::OnStopMirror",
506 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
507 return result;
508 }
509
OnStopExpand(NativeEngine & engine,NativeCallbackInfo & info)510 NativeValue* OnStopExpand(NativeEngine& engine, NativeCallbackInfo& info)
511 {
512 WLOGI("OnStopExpand is called");
513 if (info.argc < ARGC_ONE) {
514 WLOGFE("Params not match");
515 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
516 return engine.CreateUndefined();
517 }
518
519 NativeArray* array = ConvertNativeValueTo<NativeArray>(info.argv[0]);
520 if (array == nullptr) {
521 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
522 return engine.CreateUndefined();
523 }
524 uint32_t size = array->GetLength();
525 if (size > MAX_SCREENS_NUM) {
526 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
527 return engine.CreateUndefined();
528 }
529 std::vector<ScreenId> screenIds;
530 for (uint32_t i = 0; i < size; i++) {
531 uint32_t screenId;
532 NativeValue* value = array->GetElement(i);
533 if (!ConvertFromJsValue(engine, value, screenId)) {
534 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
535 return engine.CreateUndefined();
536 }
537 screenIds.emplace_back(static_cast<ScreenId>(screenId));
538 }
539
540 AsyncTask::CompleteCallback complete =
541 [screenIds](NativeEngine& engine, AsyncTask& task, int32_t status) {
542 DmErrorCode ret = DM_JS_TO_ERROR_CODE_MAP.at(
543 SingletonContainer::Get<ScreenManager>().StopExpand(screenIds));
544 if (ret == DmErrorCode::DM_OK) {
545 task.Resolve(engine, engine.CreateUndefined());
546 WLOGI("MakeExpand success");
547 } else {
548 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret),
549 "JsScreenManager::OnStopExpand failed."));
550 WLOGFE("MakeExpand failed");
551 }
552 };
553 NativeValue* lastParam = nullptr;
554 if (info.argc >= ARGC_TWO && info.argv[ARGC_TWO - 1] != nullptr &&
555 info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) {
556 lastParam = info.argv[ARGC_TWO - 1];
557 }
558 NativeValue* result = nullptr;
559 AsyncTask::Schedule("JsScreenManager::OnStopExpand",
560 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
561 return result;
562 }
563
GetExpandOptionFromJs(NativeEngine & engine,NativeObject * optionObject,ExpandOption & option)564 static int32_t GetExpandOptionFromJs(NativeEngine& engine, NativeObject* optionObject, ExpandOption& option)
565 {
566 NativeValue* screedIdValue = optionObject->GetProperty("screenId");
567 NativeValue* startXValue = optionObject->GetProperty("startX");
568 NativeValue* startYValue = optionObject->GetProperty("startY");
569 uint32_t screenId;
570 uint32_t startX;
571 uint32_t startY;
572 if (!ConvertFromJsValue(engine, screedIdValue, screenId)) {
573 WLOGFE("Failed to convert screedIdValue to callbackType");
574 return -1;
575 }
576 if (!ConvertFromJsValue(engine, startXValue, startX)) {
577 WLOGFE("Failed to convert startXValue to callbackType");
578 return -1;
579 }
580 if (!ConvertFromJsValue(engine, startYValue, startY)) {
581 WLOGFE("Failed to convert startYValue to callbackType");
582 return -1;
583 }
584 option = {screenId, startX, startY};
585 return 0;
586 }
587
OnCreateVirtualScreen(NativeEngine & engine,NativeCallbackInfo & info)588 NativeValue* OnCreateVirtualScreen(NativeEngine& engine, NativeCallbackInfo& info)
589 {
590 WLOGI("JsScreenManager::OnCreateVirtualScreen is called");
591 DmErrorCode errCode = DmErrorCode::DM_OK;
592 VirtualScreenOption option;
593 if (info.argc < ARGC_ONE) {
594 WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
595 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
596 } else {
597 NativeObject* object = ConvertNativeValueTo<NativeObject>(info.argv[0]);
598 if (object == nullptr) {
599 WLOGFE("Failed to convert parameter to VirtualScreenOption.");
600 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
601 } else {
602 errCode = GetVirtualScreenOptionFromJs(engine, object, option);
603 }
604 }
605 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM) {
606 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
607 return engine.CreateUndefined();
608 }
609 AsyncTask::CompleteCallback complete =
610 [option](NativeEngine& engine, AsyncTask& task, int32_t status) {
611 auto screenId = SingletonContainer::Get<ScreenManager>().CreateVirtualScreen(option);
612 auto screen = SingletonContainer::Get<ScreenManager>().GetScreenById(screenId);
613 if (screen == nullptr) {
614 task.Reject(engine, CreateJsError(engine,
615 static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN),
616 "ScreenManager::CreateVirtualScreen failed."));
617 WLOGFE("ScreenManager::CreateVirtualScreen failed.");
618 return;
619 }
620 task.Resolve(engine, CreateJsScreenObject(engine, screen));
621 WLOGI("JsScreenManager::OnCreateVirtualScreen success");
622 };
623 NativeValue* lastParam = nullptr;
624 if (info.argc >= ARGC_TWO && info.argv[ARGC_TWO - 1] != nullptr &&
625 info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) {
626 lastParam = info.argv[ARGC_TWO - 1];
627 }
628 NativeValue* result = nullptr;
629 AsyncTask::Schedule("JsScreenManager::OnCreateVirtualScreen",
630 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
631 return result;
632 }
633
GetVirtualScreenOptionFromJs(NativeEngine & engine,NativeObject * optionObject,VirtualScreenOption & option)634 DmErrorCode GetVirtualScreenOptionFromJs(NativeEngine& engine, NativeObject* optionObject, VirtualScreenOption& option)
635 {
636 NativeValue* name = optionObject->GetProperty("name");
637 if (!ConvertFromJsValue(engine, name, option.name_)) {
638 WLOGFE("Failed to convert parameter to name.");
639 return DmErrorCode::DM_ERROR_INVALID_PARAM;
640 }
641 NativeValue* width = optionObject->GetProperty("width");
642 if (!ConvertFromJsValue(engine, width, option.width_)) {
643 WLOGFE("Failed to convert parameter to width.");
644 return DmErrorCode::DM_ERROR_INVALID_PARAM;
645 }
646 NativeValue* height = optionObject->GetProperty("height");
647 if (!ConvertFromJsValue(engine, height, option.height_)) {
648 WLOGFE("Failed to convert parameter to height.");
649 return DmErrorCode::DM_ERROR_INVALID_PARAM;
650 }
651 NativeValue* density = optionObject->GetProperty("density");
652 double densityValue;
653 if (!ConvertFromJsValue(engine, density, densityValue)) {
654 WLOGFE("Failed to convert parameter to density.");
655 return DmErrorCode::DM_ERROR_INVALID_PARAM;
656 }
657 option.density_ = static_cast<float>(densityValue);
658
659 NativeValue* surfaceIdNativeValue = optionObject->GetProperty("surfaceId");
660 if (!GetSurfaceFromJs(engine, surfaceIdNativeValue, option.surface_)) {
661 return DmErrorCode::DM_ERROR_INVALID_PARAM;
662 }
663 return DmErrorCode::DM_OK;
664 }
665
GetSurfaceFromJs(NativeEngine & engine,NativeValue * surfaceIdNativeValue,sptr<Surface> & surface)666 bool GetSurfaceFromJs(NativeEngine& engine, NativeValue* surfaceIdNativeValue, sptr<Surface>& surface)
667 {
668 if (surfaceIdNativeValue == nullptr || surfaceIdNativeValue->TypeOf() != NATIVE_STRING) {
669 WLOGFE("Failed to convert parameter to surface. Invalidate params.");
670 return false;
671 }
672 char buffer[PATH_MAX];
673 size_t length = 0;
674 uint64_t surfaceId = 0;
675 napi_env env = reinterpret_cast<napi_env>(&engine);
676 napi_value surfaceIdValue = reinterpret_cast<napi_value>(surfaceIdNativeValue);
677 if (napi_get_value_string_utf8(env, surfaceIdValue, buffer, PATH_MAX, &length) != napi_ok) {
678 WLOGFE("Failed to convert parameter to surface.");
679 return false;
680 }
681 std::istringstream inputStream(buffer);
682 inputStream >> surfaceId;
683 surface = SurfaceUtils::GetInstance()->GetSurface(surfaceId);
684 if (surface == nullptr) {
685 WLOGI("GetSurfaceFromJs failed, surfaceId:%{public}" PRIu64"", surfaceId);
686 }
687 return true;
688 }
689
OnDestroyVirtualScreen(NativeEngine & engine,NativeCallbackInfo & info)690 NativeValue* OnDestroyVirtualScreen(NativeEngine& engine, NativeCallbackInfo& info)
691 {
692 WLOGI("JsScreenManager::OnDestroyVirtualScreen is called");
693 DmErrorCode errCode = DmErrorCode::DM_OK;
694 int64_t screenId = -1LL;
695 if (info.argc < ARGC_ONE) {
696 WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
697 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
698 } else {
699 if (!ConvertFromJsValue(engine, info.argv[0], screenId)) {
700 WLOGFE("Failed to convert parameter to screen id.");
701 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
702 }
703 }
704 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM || screenId == -1LL) {
705 WLOGFE("JsScreenManager::OnDestroyVirtualScreen failed, Invalidate params.");
706 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
707 return engine.CreateUndefined();
708 }
709 AsyncTask::CompleteCallback complete =
710 [screenId](NativeEngine& engine, AsyncTask& task, int32_t status) {
711 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
712 SingletonContainer::Get<ScreenManager>().DestroyVirtualScreen(screenId));
713 if (res != DmErrorCode::DM_OK) {
714 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(res),
715 "ScreenManager::DestroyVirtualScreen failed."));
716 WLOGFE("ScreenManager::DestroyVirtualScreen failed.");
717 return;
718 }
719 task.Resolve(engine, engine.CreateUndefined());
720 WLOGI("JsScreenManager::OnDestroyVirtualScreen success");
721 };
722 NativeValue* lastParam = nullptr;
723 if (info.argc >= ARGC_TWO && info.argv[ARGC_TWO - 1] != nullptr &&
724 info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) {
725 lastParam = info.argv[ARGC_TWO - 1];
726 }
727 NativeValue* result = nullptr;
728 AsyncTask::Schedule("JsScreenManager::OnDestroyVirtualScreen",
729 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
730 return result;
731 }
732
OnSetVirtualScreenSurface(NativeEngine & engine,NativeCallbackInfo & info)733 NativeValue* OnSetVirtualScreenSurface(NativeEngine& engine, NativeCallbackInfo& info)
734 {
735 WLOGI("JsScreenManager::OnSetVirtualScreenSurface is called");
736 DmErrorCode errCode = DmErrorCode::DM_OK;
737 int64_t screenId = -1LL;
738 sptr<Surface> surface;
739 if (info.argc < ARGC_TWO) {
740 WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
741 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
742 } else {
743 if (!ConvertFromJsValue(engine, info.argv[0], screenId)) {
744 WLOGFE("Failed to convert parameter to screen id.");
745 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
746 }
747 if (!GetSurfaceFromJs(engine, info.argv[1], surface)) {
748 WLOGFE("Failed to convert parameter to surface");
749 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
750 }
751 }
752 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM || surface == nullptr) {
753 WLOGFE("JsScreenManager::OnSetVirtualScreenSurface failed, Invalidate params.");
754 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
755 return engine.CreateUndefined();
756 }
757 AsyncTask::CompleteCallback complete =
758 [screenId, surface](NativeEngine& engine, AsyncTask& task, int32_t status) {
759 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
760 SingletonContainer::Get<ScreenManager>().SetVirtualScreenSurface(screenId, surface));
761 if (res != DmErrorCode::DM_OK) {
762 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(res),
763 "ScreenManager::SetVirtualScreenSurface failed."));
764 WLOGFE("ScreenManager::SetVirtualScreenSurface failed.");
765 return;
766 }
767 task.Resolve(engine, engine.CreateUndefined());
768 WLOGI("JsScreenManager::OnSetVirtualScreenSurface success");
769 };
770 NativeValue* lastParam = nullptr;
771 if (info.argc >= ARGC_THREE && info.argv[ARGC_THREE - 1] != nullptr &&
772 info.argv[ARGC_THREE - 1]->TypeOf() == NATIVE_FUNCTION) {
773 lastParam = info.argv[ARGC_THREE - 1];
774 }
775 NativeValue* result = nullptr;
776 AsyncTask::Schedule("JsScreenManager::OnSetVirtualScreenSurface",
777 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
778 return result;
779 }
780
OnIsScreenRotationLocked(NativeEngine & engine,NativeCallbackInfo & info)781 NativeValue* OnIsScreenRotationLocked(NativeEngine& engine, NativeCallbackInfo& info)
782 {
783 WLOGI("OnIsScreenRotationLocked is called");
784 AsyncTask::CompleteCallback complete =
785 [](NativeEngine& engine, AsyncTask& task, int32_t status) {
786 bool isLocked = false;
787 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
788 SingletonContainer::Get<ScreenManager>().IsScreenRotationLocked(isLocked));
789 if (res == DmErrorCode::DM_OK) {
790 task.Resolve(engine, CreateJsValue(engine, isLocked));
791 WLOGFI("OnIsScreenRotationLocked success");
792 } else {
793 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(res),
794 "JsScreenManager::OnIsScreenRotationLocked failed."));
795 WLOGFE("OnIsScreenRotationLocked failed");
796 }
797 };
798 NativeValue* lastParam = nullptr;
799 if (info.argc >= ARGC_ONE && info.argv[ARGC_ONE - 1] != nullptr &&
800 info.argv[ARGC_ONE - 1]->TypeOf() == NATIVE_FUNCTION) {
801 lastParam = info.argv[ARGC_ONE - 1];
802 }
803 NativeValue* result = nullptr;
804 AsyncTask::Schedule("JsScreenManager::OnIsScreenRotationLocked",
805 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
806 return result;
807 }
808
OnSetScreenRotationLocked(NativeEngine & engine,NativeCallbackInfo & info)809 NativeValue* OnSetScreenRotationLocked(NativeEngine& engine, NativeCallbackInfo& info)
810 {
811 WLOGI("JsScreenManager::OnSetScreenRotationLocked is called");
812 DmErrorCode errCode = DmErrorCode::DM_OK;
813 if (info.argc < ARGC_ONE) {
814 WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
815 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
816 }
817 bool isLocked = false;
818 if (errCode == DmErrorCode::DM_OK) {
819 NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
820 if (nativeVal == nullptr) {
821 WLOGFE("[NAPI]Failed to convert parameter to isLocked");
822 errCode = DmErrorCode::DM_ERROR_INVALID_PARAM;
823 } else {
824 isLocked = static_cast<bool>(*nativeVal);
825 }
826 }
827 if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM) {
828 WLOGFE("JsScreenManager::OnSetScreenRotationLocked failed, Invalidate params.");
829 engine.Throw(CreateJsError(engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
830 return engine.CreateUndefined();
831 }
832
833 AsyncTask::CompleteCallback complete =
834 [isLocked](NativeEngine& engine, AsyncTask& task, int32_t status) {
835 auto res = DM_JS_TO_ERROR_CODE_MAP.at(
836 SingletonContainer::Get<ScreenManager>().SetScreenRotationLocked(isLocked));
837 if (res == DmErrorCode::DM_OK) {
838 task.Resolve(engine, engine.CreateUndefined());
839 WLOGFI("OnSetScreenRotationLocked success");
840 } else {
841 task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(res),
842 "JsScreenManager::OnSetScreenRotationLocked failed."));
843 WLOGFE("OnSetScreenRotationLocked failed");
844 }
845 };
846 NativeValue* lastParam = (info.argc <= ARGC_ONE) ? nullptr :
847 ((info.argv[ARGC_TWO - 1] != nullptr && info.argv[ARGC_TWO - 1]->TypeOf() == NATIVE_FUNCTION) ?
848 info.argv[1] : nullptr);
849 NativeValue* result = nullptr;
850 AsyncTask::Schedule("JsScreenManager::OnSetScreenRotationLocked",
851 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
852 return result;
853 }
854 };
855
InitScreenOrientation(NativeEngine * engine)856 NativeValue* InitScreenOrientation(NativeEngine* engine)
857 {
858 WLOGI("JsScreenManager::InitScreenOrientation called");
859
860 if (engine == nullptr) {
861 WLOGFE("engine is nullptr");
862 return nullptr;
863 }
864
865 NativeValue *objValue = engine->CreateObject();
866 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
867 if (object == nullptr) {
868 WLOGFE("Failed to get object");
869 return nullptr;
870 }
871
872 object->SetProperty("UNSPECIFIED", CreateJsValue(*engine, static_cast<int32_t>(Orientation::UNSPECIFIED)));
873 object->SetProperty("VERTICAL", CreateJsValue(*engine, static_cast<int32_t>(Orientation::VERTICAL)));
874 object->SetProperty("HORIZONTAL", CreateJsValue(*engine, static_cast<int32_t>(Orientation::HORIZONTAL)));
875 object->SetProperty("REVERSE_VERTICAL",
876 CreateJsValue(*engine, static_cast<int32_t>(Orientation::REVERSE_VERTICAL)));
877 object->SetProperty("REVERSE_HORIZONTAL",
878 CreateJsValue(*engine, static_cast<int32_t>(Orientation::REVERSE_HORIZONTAL)));
879 return objValue;
880 }
881
InitScreenSourceMode(NativeEngine * engine)882 NativeValue* InitScreenSourceMode(NativeEngine* engine)
883 {
884 WLOGI("JsScreenManager::InitScreenSourceMode called");
885
886 if (engine == nullptr) {
887 WLOGFE("engine is nullptr");
888 return nullptr;
889 }
890
891 NativeValue *objValue = engine->CreateObject();
892 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
893 if (object == nullptr) {
894 WLOGFE("Failed to get object");
895 return nullptr;
896 }
897
898 object->SetProperty("SCREEN_MAIN",
899 CreateJsValue(*engine, static_cast<uint32_t>(ScreenSourceMode::SCREEN_MAIN)));
900 object->SetProperty("SCREEN_MIRROR",
901 CreateJsValue(*engine, static_cast<uint32_t>(ScreenSourceMode::SCREEN_MIRROR)));
902 object->SetProperty("SCREEN_EXTEND",
903 CreateJsValue(*engine, static_cast<uint32_t>(ScreenSourceMode::SCREEN_EXTEND)));
904 object->SetProperty("SCREEN_ALONE",
905 CreateJsValue(*engine, static_cast<uint32_t>(ScreenSourceMode::SCREEN_ALONE)));
906 return objValue;
907 }
908
InitDisplayErrorCode(NativeEngine * engine)909 NativeValue* InitDisplayErrorCode(NativeEngine* engine)
910 {
911 WLOGI("JsDisplayManager::InitDisplayErrorCode called");
912
913 if (engine == nullptr) {
914 WLOGFE("engine is nullptr");
915 return nullptr;
916 }
917
918 NativeValue *objValue = engine->CreateObject();
919 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
920 if (object == nullptr) {
921 WLOGFE("Failed to get object");
922 return nullptr;
923 }
924
925 object->SetProperty("DM_ERROR_NO_PERMISSION",
926 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_NO_PERMISSION)));
927 object->SetProperty("DM_ERROR_NOT_SYSTEM_APP",
928 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_NOT_SYSTEM_APP)));
929 object->SetProperty("DM_ERROR_INVALID_PARAM",
930 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_PARAM)));
931 object->SetProperty("DM_ERROR_DEVICE_NOT_SUPPORT",
932 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT)));
933 object->SetProperty("DM_ERROR_INVALID_SCREEN",
934 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_SCREEN)));
935 object->SetProperty("DM_ERROR_INVALID_CALLING",
936 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_INVALID_CALLING)));
937 object->SetProperty("DM_ERROR_SYSTEM_INNORMAL",
938 CreateJsValue(*engine, static_cast<int32_t>(DmErrorCode::DM_ERROR_SYSTEM_INNORMAL)));
939 return objValue;
940 }
941
InitDisplayError(NativeEngine * engine)942 NativeValue* InitDisplayError(NativeEngine* engine)
943 {
944 WLOGI("JsDisplayManager::InitDisplayError called");
945
946 if (engine == nullptr) {
947 WLOGFE("engine is nullptr");
948 return nullptr;
949 }
950
951 NativeValue *objValue = engine->CreateObject();
952 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
953 if (object == nullptr) {
954 WLOGFE("Failed to get object");
955 return nullptr;
956 }
957
958 object->SetProperty("DM_ERROR_INIT_DMS_PROXY_LOCKED",
959 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED)));
960 object->SetProperty("DM_ERROR_IPC_FAILED",
961 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_IPC_FAILED)));
962 object->SetProperty("DM_ERROR_REMOTE_CREATE_FAILED",
963 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_REMOTE_CREATE_FAILED)));
964 object->SetProperty("DM_ERROR_NULLPTR",
965 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_NULLPTR)));
966 object->SetProperty("DM_ERROR_INVALID_PARAM",
967 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_PARAM)));
968 object->SetProperty("DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED",
969 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED)));
970 object->SetProperty("DM_ERROR_DEATH_RECIPIENT",
971 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_DEATH_RECIPIENT)));
972 object->SetProperty("DM_ERROR_INVALID_MODE_ID",
973 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_MODE_ID)));
974 object->SetProperty("DM_ERROR_WRITE_DATA_FAILED",
975 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_WRITE_DATA_FAILED)));
976 object->SetProperty("DM_ERROR_RENDER_SERVICE_FAILED",
977 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_RENDER_SERVICE_FAILED)));
978 object->SetProperty("DM_ERROR_UNREGISTER_AGENT_FAILED",
979 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_UNREGISTER_AGENT_FAILED)));
980 object->SetProperty("DM_ERROR_INVALID_CALLING",
981 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_INVALID_CALLING)));
982 object->SetProperty("DM_ERROR_UNKNOWN",
983 CreateJsValue(*engine, static_cast<int32_t>(DMError::DM_ERROR_UNKNOWN)));
984 return objValue;
985 }
986
JsScreenManagerInit(NativeEngine * engine,NativeValue * exportObj)987 NativeValue* JsScreenManagerInit(NativeEngine* engine, NativeValue* exportObj)
988 {
989 WLOGI("JsScreenManagerInit is called");
990
991 if (engine == nullptr || exportObj == nullptr) {
992 WLOGFE("JsScreenManagerInit engine or exportObj is nullptr");
993 return nullptr;
994 }
995
996 NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
997 if (object == nullptr) {
998 WLOGFE("JsScreenManagerInit object is nullptr");
999 return nullptr;
1000 }
1001
1002 std::unique_ptr<JsScreenManager> jsScreenManager = std::make_unique<JsScreenManager>(engine);
1003 object->SetNativePointer(jsScreenManager.release(), JsScreenManager::Finalizer, nullptr);
1004
1005 object->SetProperty("Orientation", InitScreenOrientation(engine));
1006 object->SetProperty("ScreenSourceMode", InitScreenSourceMode(engine));
1007 object->SetProperty("DmErrorCode", InitDisplayErrorCode(engine));
1008 object->SetProperty("DMError", InitDisplayError(engine));
1009
1010 const char *moduleName = "JsScreenManager";
1011 BindNativeFunction(*engine, *object, "getAllScreens", moduleName, JsScreenManager::GetAllScreens);
1012 BindNativeFunction(*engine, *object, "on", moduleName, JsScreenManager::RegisterScreenManagerCallback);
1013 BindNativeFunction(*engine, *object, "off", moduleName, JsScreenManager::UnregisterScreenMangerCallback);
1014 BindNativeFunction(*engine, *object, "makeMirror", moduleName, JsScreenManager::MakeMirror);
1015 BindNativeFunction(*engine, *object, "makeExpand", moduleName, JsScreenManager::MakeExpand);
1016 BindNativeFunction(*engine, *object, "stopMirror", moduleName, JsScreenManager::StopMirror);
1017 BindNativeFunction(*engine, *object, "stopExpand", moduleName, JsScreenManager::StopExpand);
1018 BindNativeFunction(*engine, *object, "createVirtualScreen", moduleName, JsScreenManager::CreateVirtualScreen);
1019 BindNativeFunction(*engine, *object, "destroyVirtualScreen", moduleName, JsScreenManager::DestroyVirtualScreen);
1020 BindNativeFunction(*engine, *object, "setVirtualScreenSurface", moduleName,
1021 JsScreenManager::SetVirtualScreenSurface);
1022 BindNativeFunction(*engine, *object, "setScreenRotationLocked", moduleName,
1023 JsScreenManager::SetScreenRotationLocked);
1024 BindNativeFunction(*engine, *object, "isScreenRotationLocked", moduleName,
1025 JsScreenManager::IsScreenRotationLocked);
1026 return engine->CreateUndefined();
1027 }
1028 } // namespace Rosen
1029 } // namespace OHOS