offer display change js interface

Signed-off-by: zengqingyu <zengqingyu3@huawei.com>
Change-Id: I46d60b1387bdf6814d1537f7a8d647c63f54c84d
This commit is contained in:
zengqingyu
2022-02-17 21:08:39 +08:00
parent 97aa599ef7
commit 56b10f345d
16 changed files with 768 additions and 170 deletions
+19
View File
@@ -13,8 +13,11 @@
* limitations under the License.
*/
import { AsyncCallback, Callback } from './basic';
/**
* interface of display manager
* @syscap SystemCapability.WindowManager.WindowManager.Core
* @devices tv, phone, tablet, wearable
*/
declare namespace display {
@@ -24,9 +27,24 @@ declare namespace display {
*/
function getDefaultDisplay(): Promise<Display>;
/**
* register the callback of display change
* @param type: type of callback
* @devices tv, phone, tablet, wearable, car
*/
function on(type: 'add' | 'remove' | 'change', callback: Callback<number>): void;
/**
* unregister the callback of display change
* @param type: type of callback
* #devices tv, phone, tablet, wearable, car
*/
function off(type: 'add' | 'remove' | 'change', callback: Callback<number>): void;
/**
/**
* the state of display
* @syscap SystemCapability.WindowManager.WindowManager.Core
* @devices tv, phone, tablet, wearable
*/
enum DisplayState {
@@ -62,6 +80,7 @@ declare namespace display {
/**
* Properties of display, it couldn't update automatically
* @syscap SystemCapability.WindowManager.WindowManager.Core
* @devices tv, phone, tablet, wearable
*/
interface Display {
+1 -1
View File
@@ -16,7 +16,7 @@ import("//build/ohos.gni")
group("napi_packages") {
deps = [
"display:display",
"display:display_napi",
"display_runtime:screen_napi",
"screenshot:screenshot",
"window_runtime:window_napi",
+32 -7
View File
@@ -13,22 +13,47 @@
import("//build/ohos.gni")
## Build display.so {{{
ohos_shared_library("display") {
sources = [ "native_display_module.cpp" ]
config("display_config") {
visibility = [ ":*" ]
include_dirs = [
"//foundation/windowmanager/interfaces/kits/napi/display",
"//foundation/windowmanager/utils/include",
]
}
## Build display_napi.so {{{
ohos_shared_library("display_napi") {
sources = [
"js_display.cpp",
"js_display_listener.cpp",
"js_display_manager.cpp",
"js_display_module.cpp",
]
configs = [ ":display_config" ]
deps = [
"../common:wm_napi_common",
"//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native",
"//foundation/aafwk/standard/frameworks/kits/appkit:app_context",
"//foundation/aafwk/standard/frameworks/kits/appkit:appkit_native",
"//foundation/aafwk/standard/interfaces/innerkits/want:want",
"//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client",
"//foundation/windowmanager/dm:libdm",
"//foundation/windowmanager/utils:libwmutil",
"//foundation/windowmanager/wm:libwm",
"//foundation/windowmanager/wmserver:libwms",
]
external_deps = [ "multimedia_image_standard:image_native" ]
external_deps = [
"ability_runtime:ability_manager",
"ability_runtime:runtime",
"hiviewdfx_hilog_native:libhilog",
"multimedia_image_standard:image_native",
"napi:ace_napi",
]
relative_install_dir = "module"
part_name = "window_manager"
subsystem_name = "window"
}
## Build display.so }}}
## Build display_napi.so }}}
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_display.h"
#include <cinttypes>
#include <map>
#include "display.h"
#include "window_manager_hilog.h"
namespace OHOS {
namespace Rosen {
using namespace AbilityRuntime;
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "JsDisplay"};
}
static std::map<DisplayId, std::shared_ptr<NativeReference>> g_JsDisplayMap;
std::recursive_mutex g_mutex;
JsDisplay::JsDisplay(const sptr<Display>& display) : display_(display)
{
}
JsDisplay::~JsDisplay()
{
WLOGFI("JsDisplay::~JsDisplay is called");
}
void JsDisplay::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("JsDisplay::Finalizer is called");
auto jsDisplay = std::unique_ptr<JsDisplay>(static_cast<JsDisplay*>(data));
if (jsDisplay == nullptr) {
WLOGFE("JsDisplay::Finalizer jsDisplay is null");
return;
}
sptr<Display> display = jsDisplay->display_;
if (display == nullptr) {
WLOGFE("JsDisplay::Finalizer display is null");
return;
}
DisplayId displayId = display->GetId();
WLOGFI("JsDisplay::Finalizer displayId : %{public}" PRIu64"", displayId);
std::lock_guard<std::recursive_mutex> lock(g_mutex);
if (g_JsDisplayMap.find(displayId) != g_JsDisplayMap.end()) {
WLOGFI("JsDisplay::Finalizer Display is destroyed: %{public}" PRIu64"", displayId);
g_JsDisplayMap.erase(displayId);
}
}
NativeValue* CreateJsDisplayObject(NativeEngine& engine, sptr<Display>& display)
{
WLOGFI("JsDisplay::CreateJsDisplay is called");
NativeValue* objValue = engine.CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return engine.CreateUndefined();
}
std::unique_ptr<JsDisplay> jsDisplay = std::make_unique<JsDisplay>(display);
object->SetNativePointer(jsDisplay.release(), JsDisplay::Finalizer, nullptr);
object->SetProperty("id", CreateJsValue(engine, static_cast<uint32_t>(display->GetId())));
object->SetProperty("width", CreateJsValue(engine, display->GetWidth()));
object->SetProperty("height", CreateJsValue(engine, display->GetHeight()));
object->SetProperty("refreshRate", CreateJsValue(engine, display->GetFreshRate()));
object->SetProperty("name", engine.CreateUndefined());
object->SetProperty("alive", engine.CreateUndefined());
object->SetProperty("state", engine.CreateUndefined());
object->SetProperty("rotation", engine.CreateUndefined());
object->SetProperty("densityDPI", engine.CreateUndefined());
object->SetProperty("densityPixels", engine.CreateUndefined());
object->SetProperty("scaledDensity", engine.CreateUndefined());
object->SetProperty("xDPI", engine.CreateUndefined());
object->SetProperty("yDPI", engine.CreateUndefined());
std::shared_ptr<NativeReference> jsDisplayRef;
jsDisplayRef.reset(engine.CreateReference(objValue, 1));
DisplayId displayId = display->GetId();
std::lock_guard<std::recursive_mutex> lock(g_mutex);
g_JsDisplayMap[displayId] = jsDisplayRef;
return objValue;
}
} // namespace Rosen
} // namespace OHOS
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_JS_DISPLAY_H
#define OHOS_JS_DISPLAY_H
#include "js_runtime_utils.h"
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "display.h"
namespace OHOS {
namespace Rosen {
NativeValue* CreateJsDisplayObject(NativeEngine& engine, sptr<Display>& Display);
class JsDisplay final {
public:
explicit JsDisplay(const sptr<Display>& Display);
~JsDisplay();
static void Finalizer(NativeEngine* engine, void* data, void* hint);
private:
sptr<Display> display_ = nullptr;
};
} // namespace Rosen
} // namespace OHOS
#endif
@@ -0,0 +1,143 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_display_listener.h"
#include "js_runtime_utils.h"
#include "window_manager_hilog.h"
namespace OHOS {
namespace Rosen {
using namespace AbilityRuntime;
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "JsDisplayListener"};
}
void JsDisplayListener::AddCallback(NativeValue* jsListenerObject)
{
WLOGFI("JsDisplayListener::AddCallback is called");
std::unique_ptr<NativeReference> callbackRef;
if (engine_ == nullptr) {
WLOGFE("engine_ nullptr");
return;
}
callbackRef.reset(engine_->CreateReference(jsListenerObject, 1));
std::lock_guard<std::mutex> lock(mtx_);
jsCallBack_.push_back(std::move(callbackRef));
WLOGFI("JsDisplayListener::AddCallback success jsCallBack_ size: %{public}u!",
static_cast<uint32_t>(jsCallBack_.size()));
}
void JsDisplayListener::RemoveAllCallback()
{
std::lock_guard<std::mutex> lock(mtx_);
jsCallBack_.clear();
}
void JsDisplayListener::RemoveCallback(NativeValue* jsListenerObject)
{
std::lock_guard<std::mutex> lock(mtx_);
for (auto iter = jsCallBack_.begin(); iter != jsCallBack_.end();) {
if (jsListenerObject->StrictEquals((*iter)->Get())) {
jsCallBack_.erase(iter);
} else {
iter++;
}
}
WLOGFI("JsDisplayListener::RemoveCallback success jsCallBack_ size: %{public}u!",
static_cast<uint32_t>(jsCallBack_.size()));
}
void JsDisplayListener::CallJsMethod(const char* methodName, NativeValue* const* argv, size_t argc)
{
WLOGFI("CallJsMethod methodName = %{public}s", methodName);
if (engine_ == nullptr) {
WLOGFE("engine_ nullptr");
return;
}
for (auto& callback : jsCallBack_) {
NativeValue* method = callback->Get();
if (method == nullptr) {
WLOGFE("Failed to get method callback from object");
continue;
}
engine_->CallFunction(engine_->CreateUndefined(), method, argv, argc);
}
}
void JsDisplayListener::OnCreate(DisplayId id)
{
std::lock_guard<std::mutex> lock(mtx_);
WLOGFI("JsDisplayListener::OnCreate is called, displayId: %{public}d", static_cast<uint32_t>(id));
if (jsCallBack_.empty()) {
WLOGFE("JsDisplayListener::OnCreate not register!");
return;
}
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* argv[] = {CreateJsValue(*engine_, static_cast<uint32_t>(id))};
CallJsMethod("add", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
void JsDisplayListener::OnDestroy(DisplayId id)
{
std::lock_guard<std::mutex> lock(mtx_);
WLOGFI("JsDisplayListener::OnDestroy is called, displayId: %{public}d", static_cast<uint32_t>(id));
if (jsCallBack_.empty()) {
WLOGFE("JsDisplayListener::OnDestroy not register!");
return;
}
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* argv[] = {CreateJsValue(*engine_, static_cast<uint32_t>(id))};
CallJsMethod("remove", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
void JsDisplayListener::OnChange(DisplayId id, DisplayChangeEvent event)
{
std::lock_guard<std::mutex> lock(mtx_);
WLOGFI("JsDisplayListener::OnChange is called, displayId: %{public}d", static_cast<uint32_t>(id));
if (jsCallBack_.empty()) {
WLOGFE("JsDisplayListener::OnChange not register!");
return;
}
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* argv[] = {CreateJsValue(*engine_, static_cast<uint32_t>(id))};
CallJsMethod("change", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
} // namespace Rosen
} // namespace OHOS
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_JS_DISPLAY_LISTENER_H
#define OHOS_JS_DISPLAY_LISTENER_H
#include <mutex>
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "refbase.h"
#include "display_manager.h"
namespace OHOS {
namespace Rosen {
class JsDisplayListener : public DisplayManager::IDisplayListener {
public:
explicit JsDisplayListener(NativeEngine* engine) : engine_(engine) {}
~JsDisplayListener() override = default;
void AddCallback(NativeValue* jsListenerObject);
void RemoveAllCallback();
void RemoveCallback(NativeValue* jsListenerObject);
void OnCreate(DisplayId id) override;
void OnDestroy(DisplayId id) override;
void OnChange(DisplayId id, DisplayChangeEvent event) override;
private:
void CallJsMethod(const char* methodName, NativeValue* const* argv = nullptr, size_t argc = 0);
NativeEngine* engine_ = nullptr;
std::mutex mtx_;
std::vector<std::unique_ptr<NativeReference>> jsCallBack_;
NativeValue* CreateDisplayIdArray(NativeEngine& engine, const std::vector<DisplayId>& data);
};
} // namespace Rosen
} // namespace OHOS
#endif /* OHOS_JS_DISPLAY_LISTENER_H */
@@ -0,0 +1,256 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vector>
#include "js_runtime_utils.h"
#include "native_engine/native_reference.h"
#include "display_manager.h"
#include "window_manager_hilog.h"
#include "singleton_container.h"
#include "js_display_listener.h"
#include "js_display.h"
#include "js_display_manager.h"
namespace OHOS {
namespace Rosen {
using namespace AbilityRuntime;
constexpr size_t ARGC_ONE = 1;
constexpr size_t ARGC_TWO = 2;
constexpr int32_t INDEX_ONE = 1;
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "JsDisplayManager"};
}
class JsDisplayManager {
public:
explicit JsDisplayManager(NativeEngine* engine) {
}
~JsDisplayManager() = default;
static void Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("JsDisplayManager::Finalizer is called");
std::unique_ptr<JsDisplayManager>(static_cast<JsDisplayManager*>(data));
}
static NativeValue* GetDefaultDisplay(NativeEngine* engine, NativeCallbackInfo* info)
{
JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
return (me != nullptr) ? me->OnGetDefaultDisplay(*engine, *info) : nullptr;
}
static NativeValue* RegisterDisplayManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
{
JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
return (me != nullptr) ? me->OnRegisterDisplayManagerCallback(*engine, *info) : nullptr;
}
static NativeValue* UnregisterDisplayManagerCallback(NativeEngine* engine, NativeCallbackInfo* info)
{
JsDisplayManager* me = CheckParamsAndGetThis<JsDisplayManager>(engine, info);
return (me != nullptr) ? me->OnUnregisterDisplayManagerCallback(*engine, *info) : nullptr;
}
private:
std::map<std::string, std::map<std::unique_ptr<NativeReference>, sptr<JsDisplayListener>>> jsCbMap_;
std::mutex mtx_;
NativeValue* OnGetDefaultDisplay(NativeEngine& engine, NativeCallbackInfo& info)
{
WLOGFI("JsDisplayManager::OnGetDefaultDisplay is called");
AsyncTask::CompleteCallback complete =
[](NativeEngine& engine, AsyncTask& task, int32_t status) {
sptr<Display> display = SingletonContainer::Get<DisplayManager>().GetDefaultDisplay();
if (display != nullptr) {
task.Resolve(engine, CreateJsDisplayObject(engine, display));
WLOGFI("JsDisplayManager::OnGetDefaultDisplay success");
} else {
task.Reject(engine, CreateJsError(engine,
static_cast<int32_t>(DMError::DM_ERROR_NULLPTR), "JsDisplayManager::OnGetDefaultDisplay failed."));
}
};
NativeValue* lastParam = nullptr;
NativeValue* result = nullptr;
AsyncTask::Schedule(
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
void RegisterDisplayListenerWithType(NativeEngine& engine, const std::string& type, NativeValue* value)
{
if (IfCallbackRegistered(type, value)) {
WLOGFE("JsDisplayManager::RegisterDisplayListenerWithType callback already registered!");
return;
}
std::unique_ptr<NativeReference> callbackRef;
callbackRef.reset(engine.CreateReference(value, 1));
sptr<JsDisplayListener> displayListener = new JsDisplayListener(&engine);
if (type == "add" || type == "remove" || type == "change") {
SingletonContainer::Get<DisplayManager>().RegisterDisplayListener(displayListener);
WLOGFI("JsDisplayManager::RegisterDisplayListenerWithType success");
} else {
WLOGFE("JsDisplayManager::RegisterDisplayListenerWithType failed method: %{public}s not support!",
type.c_str());
return;
}
displayListener->AddCallback(value);
jsCbMap_[type][std::move(callbackRef)] = displayListener;
}
bool IfCallbackRegistered(const std::string& type, NativeValue* jsListenerObject)
{
if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
WLOGFI("JsDisplayManager::IfCallbackRegistered methodName %{public}s not registered!", type.c_str());
return false;
}
for (auto& iter : jsCbMap_[type]) {
if (jsListenerObject->StrictEquals(iter.first->Get())) {
WLOGFE("JsDisplayManager::IfCallbackRegistered callback already registered!");
return true;
}
}
return false;
}
void UnregisterAllDisplayListenerWithType(const std::string& type)
{
if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
WLOGFI("JsDisplayManager::UnregisterAllDisplayListenerWithType methodName %{public}s not registered!",
type.c_str());
return;
}
for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
it->second->RemoveAllCallback();
if (type == "add" || type == "remove" || type == "change") {
sptr<DisplayManager::IDisplayListener> thisListener(it->second);
SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
WLOGFI("JsDisplayManager::UnregisterAllDisplayListenerWithType success");
}
jsCbMap_[type].erase(it++);
}
jsCbMap_.erase(type);
}
void UnRegisterDisplayListenerWithType(const std::string& type, NativeValue* value)
{
if (jsCbMap_.empty() || jsCbMap_.find(type) == jsCbMap_.end()) {
WLOGFI("JsDisplayManager::UnRegisterDisplayListenerWithType methodName %{public}s not registered!",
type.c_str());
return;
}
for (auto it = jsCbMap_[type].begin(); it != jsCbMap_[type].end();) {
if (value->StrictEquals(it->first->Get())) {
it->second->RemoveCallback(value);
if (type == "add" || type == "remove" || type == "change") {
sptr<DisplayManager::IDisplayListener> thisListener(it->second);
SingletonContainer::Get<DisplayManager>().UnregisterDisplayListener(thisListener);
WLOGFI("JsDisplayManager::UnRegisterDisplayListenerWithType success");
}
jsCbMap_[type].erase(it++);
break;
} else {
it++;
}
}
if (jsCbMap_[type].empty()) {
jsCbMap_.erase(type);
}
}
NativeValue* OnRegisterDisplayManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
{
WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback is called");
if (info.argc != ARGC_TWO) {
WLOGFE("JsDisplayManager Params not match: %{public}zu", info.argc);
return engine.CreateUndefined();
}
std::string cbType;
if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
WLOGFE("Failed to convert parameter to callbackType");
return engine.CreateUndefined();
}
NativeValue* value = info.argv[INDEX_ONE];
if (value == nullptr) {
WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback info->argv[1] is nullptr");
return engine.CreateUndefined();
}
if (!value->IsCallable()) {
WLOGFI("JsDisplayManager::OnRegisterDisplayManagerCallback info->argv[1] is not callable");
return engine.CreateUndefined();
}
std::lock_guard<std::mutex> lock(mtx_);
RegisterDisplayListenerWithType(engine, cbType, value);
return engine.CreateUndefined();
}
NativeValue* OnUnregisterDisplayManagerCallback(NativeEngine& engine, NativeCallbackInfo& info)
{
WLOGFI("JsDisplayManager::OnUnregisterDisplayCallback is called");
if (info.argc == 0) {
WLOGFE("JsDisplayManager Params not match %{public}zu", info.argc);
return engine.CreateUndefined();
}
std::string cbType;
if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
WLOGFE("Failed to convert parameter to callbackType");
return engine.CreateUndefined();
}
std::lock_guard<std::mutex> lock(mtx_);
if (info.argc == ARGC_ONE) {
UnregisterAllDisplayListenerWithType(cbType);
} else {
NativeValue* value = info.argv[INDEX_ONE];
if (value == nullptr) {
WLOGFI("JsDisplayManager::OnUnregisterDisplayManagerCallback info->argv[1] is nullptr");
return engine.CreateUndefined();
}
if (!value->IsCallable()) {
WLOGFI("JsDisplayManager::OnUnregisterDisplayManagerCallback info->argv[1] is not callable");
return engine.CreateUndefined();
}
UnRegisterDisplayListenerWithType(cbType, value);
}
return engine.CreateUndefined();
}
};
NativeValue* JsDisplayManagerInit(NativeEngine* engine, NativeValue* exportObj)
{
WLOGFI("JsDisplayManagerInit is called");
if (engine == nullptr || exportObj == nullptr) {
WLOGFE("JsDisplayManagerInit engine or exportObj is nullptr");
return nullptr;
}
NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
if (object == nullptr) {
WLOGFE("JsDisplayManagerInit object is nullptr");
return nullptr;
}
std::unique_ptr<JsDisplayManager> jsDisplayManager = std::make_unique<JsDisplayManager>(engine);
object->SetNativePointer(jsDisplayManager.release(), JsDisplayManager::Finalizer, nullptr);
BindNativeFunction(*engine, *object, "getDefaultDisplay", JsDisplayManager::GetDefaultDisplay);
BindNativeFunction(*engine, *object, "on", JsDisplayManager::RegisterDisplayManagerCallback);
BindNativeFunction(*engine, *object, "off", JsDisplayManager::UnregisterDisplayManagerCallback);
return engine->CreateUndefined();
}
} // namespace Rosen
} // namespace OHOS
@@ -1,19 +1,28 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERFACES_KITS_NAPI_GRAPHIC_DISPLAY_NATIVE_MODULE_H
#define INTERFACES_KITS_NAPI_GRAPHIC_DISPLAY_NATIVE_MODULE_H
#endif // INTERFACES_KITS_NAPI_GRAPHIC_DISPLAY_NATIVE_MODULE_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_JS_DISPLAY_MANAGER_H
#define OHOS_JS_DISPLAY_MANAGER_H
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
namespace OHOS {
namespace Rosen {
NativeValue* JsDisplayManagerInit(NativeEngine* engine, NativeValue* exportObj);
} // namespace Rosen
} // namespace OHOS
#endif
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_display_manager.h"
#include "native_engine/native_engine.h"
extern "C" __attribute__((constructor)) void NAPI_application_displaymanager_AutoRegister()
{
auto moduleManager = NativeModuleManager::GetInstance();
NativeModule newModuleInfo = {
.name = "display",
.fileName = "module/libdisplay_napi.so/display.js",
.registerCallback = OHOS::Rosen::JsDisplayManagerInit,
};
moduleManager->Register(&newModuleInfo);
}
@@ -1,113 +0,0 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "display_manager.h"
#include <cinttypes>
#include "native_display_module.h"
#include "wm_common.h"
#include "wm_napi_common.h"
namespace OHOS {
namespace Rosen {
namespace getDefaultDisplay {
struct Param {
WMError wret;
sptr<Display> display;
};
void Async(napi_env env, std::unique_ptr<Param> &param)
{
param->display = DisplayManager::GetInstance().GetDefaultDisplay();
if (param->display == nullptr) {
GNAPI_LOG("Get display failed!");
param->wret = WMError::WM_ERROR_NULLPTR;
return;
}
GNAPI_LOG("GetDefaultDisplay: id %{public}" PRIu64", w %{public}d, h %{public}d",
param->display->GetId(), param->display->GetWidth(), param->display->GetHeight());
param->wret = WMError::WM_OK;
}
napi_value Resolve(napi_env env, std::unique_ptr<Param> &param)
{
napi_value result;
if (param->wret != WMError::WM_OK) {
NAPI_CALL(env, napi_get_undefined(env, &result));
return result;
}
DisplayId id = param->display->GetId();
int32_t width = param->display->GetWidth();
int32_t height = param->display->GetHeight();
GNAPI_LOG("id : %{public}" PRIu64"", id);
GNAPI_LOG("width : %{public}d", width);
GNAPI_LOG("height : %{public}d", height);
NAPI_CALL(env, napi_create_object(env, &result));
NAPI_CALL(env, SetMemberInt32(env, result, "id", id));
NAPI_CALL(env, SetMemberUndefined(env, result, "name"));
NAPI_CALL(env, SetMemberUndefined(env, result, "alive"));
NAPI_CALL(env, SetMemberUndefined(env, result, "state"));
NAPI_CALL(env, SetMemberUndefined(env, result, "refreshRate"));
NAPI_CALL(env, SetMemberUndefined(env, result, "rotation"));
NAPI_CALL(env, SetMemberUint32(env, result, "width", width));
NAPI_CALL(env, SetMemberUint32(env, result, "height", height));
NAPI_CALL(env, SetMemberUndefined(env, result, "densityDPI"));
NAPI_CALL(env, SetMemberUndefined(env, result, "densityPixels"));
NAPI_CALL(env, SetMemberUndefined(env, result, "scaledDensity"));
NAPI_CALL(env, SetMemberUndefined(env, result, "xDPI"));
NAPI_CALL(env, SetMemberUndefined(env, result, "yDPI"));
return result;
}
napi_value MainFunc(napi_env env, napi_callback_info info)
{
GNAPI_LOG("Display Interface: getDefaultDisplay()");
GNAPI_LOG("%{public}s called", __PRETTY_FUNCTION__);
auto param = std::make_unique<Param>();
return CreatePromise<Param>(env, __PRETTY_FUNCTION__, Async, Resolve, param);
}
} // namespace getDefaultDisplay
napi_value DisplayModuleInit(napi_env env, napi_value exports)
{
GNAPI_LOG("%{public}s called", __PRETTY_FUNCTION__);
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("getDefaultDisplay", getDefaultDisplay::MainFunc),
};
NAPI_CALL(env, napi_define_properties(env,
exports, sizeof(properties) / sizeof(properties[0]), properties));
return exports;
}
} // namespace Rosen
} // namespace OHOS
extern "C" __attribute__((constructor)) void RegisterModule(void)
{
napi_module displayModule = {
.nm_version = 1, // NAPI v1
.nm_flags = 0, // normal
.nm_filename = nullptr,
.nm_register_func = OHOS::Rosen::DisplayModuleInit,
.nm_modname = "display",
.nm_priv = nullptr,
};
napi_module_register(&displayModule);
}
@@ -150,6 +150,8 @@ declare namespace screen {
ADD_TO_GROUP = 0,
REMOVE_FROM_GROUP = 1,
CHANGE_GROUP = 2,
UPDATE_ROTATION = 3,
CHANGE_MODE = 4,
}
/**
@@ -43,6 +43,10 @@ void JsScreen::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("JsScreen::Finalizer is called");
auto jsScreen = std::unique_ptr<JsScreen>(static_cast<JsScreen*>(data));
if (jsScreen == nullptr) {
WLOGFE("jsScreen::Finalizer jsScreen is null");
return;
}
sptr<Screen> screen = jsScreen->screen_;
if (screen == nullptr) {
WLOGFE("JsScreen::Finalizer screen is null");
@@ -80,16 +80,26 @@ void JsScreenListener::OnConnect(ScreenId id)
WLOGFE("JsScreenListener::OnConnect not register!");
return;
}
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateJsValue(*engine_, static_cast<uint32_t>(id)));
object->SetProperty("type", CreateJsValue(*engine_, SCREEN_CONNECT_TYPE));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenConnectEvent", argv, ArraySize(argv));
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateJsValue(*engine_, static_cast<uint32_t>(id)));
object->SetProperty("type", CreateJsValue(*engine_, SCREEN_CONNECT_TYPE));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenConnectEvent", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
void JsScreenListener::OnDisconnect(ScreenId id)
@@ -100,16 +110,26 @@ void JsScreenListener::OnDisconnect(ScreenId id)
WLOGFE("JsScreenListener::OnDisconnect not register!");
return;
}
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateJsValue(*engine_, static_cast<uint32_t>(id)));
object->SetProperty("type", CreateJsValue(*engine_, SCREEN_DISCONNECT_TYPE));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenConnectEvent", argv, ArraySize(argv));
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateJsValue(*engine_, static_cast<uint32_t>(id)));
object->SetProperty("type", CreateJsValue(*engine_, SCREEN_DISCONNECT_TYPE));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenConnectEvent", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
void JsScreenListener::OnChange(const std::vector<ScreenId> &vector, ScreenChangeEvent event)
@@ -120,16 +140,26 @@ void JsScreenListener::OnChange(const std::vector<ScreenId> &vector, ScreenChang
WLOGFE("JsScreenListener::OnChange not register!");
return;
}
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateScreenIdArray(*engine_, vector));
object->SetProperty("type", CreateJsValue(*engine_, event));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenChangeEvent", argv, ArraySize(argv));
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[=] (NativeEngine &engine, AsyncTask &task, int32_t status) {
NativeValue* propertyValue = engine_->CreateObject();
NativeObject* object = ConvertNativeValueTo<NativeObject>(propertyValue);
if (object == nullptr) {
WLOGFE("Failed to convert prop to jsObject");
return;
}
object->SetProperty("screenId", CreateScreenIdArray(*engine_, vector));
object->SetProperty("type", CreateJsValue(*engine_, event));
NativeValue* argv[] = {propertyValue};
CallJsMethod("screenChangeEvent", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule(*engine_, std::make_unique<AsyncTask>(
callback, std::move(execute), std::move(complete)));
}
NativeValue* JsScreenListener::CreateScreenIdArray(NativeEngine& engine, const std::vector<ScreenId>& data)
@@ -209,6 +209,10 @@ NativeValue* OnRegisterScreenMangerCallback(NativeEngine& engine, NativeCallback
return engine.CreateUndefined();
}
NativeValue* value = info.argv[INDEX_ONE];
if (value == nullptr) {
WLOGFI("JsScreenManager::OnRegisterScreenMangerCallback info->argv[1] is nullptr");
return engine.CreateUndefined();
}
if (!value->IsCallable()) {
WLOGFI("JsScreenManager::OnRegisterScreenMangerCallback info->argv[1] is not callable");
return engine.CreateUndefined();
@@ -235,6 +239,10 @@ NativeValue* OnUnregisterScreenManagerCallback(NativeEngine& engine, NativeCallb
UnregisterAllScreenListenerWithType(cbType);
} else {
NativeValue* value = info.argv[INDEX_ONE];
if (value == nullptr) {
WLOGFI("JsScreenManager::OnUnregisterScreenManagerCallback info->argv[1] is nullptr");
return engine.CreateUndefined();
}
if (!value->IsCallable()) {
WLOGFI("JsScreenManager::OnUnregisterScreenManagerCallback info->argv[1] is not callable");
return engine.CreateUndefined();
@@ -48,6 +48,10 @@ void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("JsWindow::Finalizer is called");
auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
if (jsWin == nullptr) {
WLOGFE("JsWindow::Finalizer JsWindow is null");
return;
}
std::string windowName = jsWin->GetWindowName();
WLOGFI("JsWindow::Finalizer windowName : %{public}s", windowName.c_str());
std::lock_guard<std::recursive_mutex> lock(g_mutex);