diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 325093f0def890efe269902a105708f694a1b8c2..734260169f2311be9e83165c46e5786f50a53e7a 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -53,6 +53,7 @@ ohos_shared_library("windowstage") { group("napi_packages") { deps = [ "display:display", + "screenshot:screenshot", "window:window", "window_runtime:windowmanager_napi", ] diff --git a/interfaces/kits/napi/common/BUILD.gn b/interfaces/kits/napi/common/BUILD.gn index 081352bd7b0e40125324ae1282ee56dea13bf13e..83d4c34ad9555f910783343cd5c5fa804437af8e 100644 --- a/interfaces/kits/napi/common/BUILD.gn +++ b/interfaces/kits/napi/common/BUILD.gn @@ -41,6 +41,7 @@ ohos_static_library("wm_napi_common") { public_deps = [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", "//foundation/ace/napi:ace_napi", + "//foundation/windowmanager/wm:libwm", ] } ## Build wm_napi_common.a }}} diff --git a/interfaces/kits/napi/common/wm_napi_common.h b/interfaces/kits/napi/common/wm_napi_common.h index 480c511de12f56c776c31b05345df24facf900a8..f80accb8051526a7c60474a95647e4fbced3cdf5 100644 --- a/interfaces/kits/napi/common/wm_napi_common.h +++ b/interfaces/kits/napi/common/wm_napi_common.h @@ -23,6 +23,7 @@ #include #include #include +#include "wm_common.h" constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0, "NapiWindowManagerCommonLayer" }; @@ -95,7 +96,15 @@ napi_value CreatePromise(napi_env env, napi_get_undefined(env, &resolveValue); } - napi_resolve_deferred(env, info->deferred, resolveValue); + if (info->deferred) { + if (info->param->wret == OHOS::Rosen::WMError::WM_OK) { + GNAPI_LOG("CreatePromise, resolve"); + napi_resolve_deferred(env, info->deferred, resolveValue); + } else { + GNAPI_LOG("CreatePromise, reject"); + napi_reject_deferred(env, info->deferred, resolveValue); + } + } napi_delete_async_work(env, info->asyncWork); delete info; }; diff --git a/interfaces/kits/napi/screenshot/BUILD.gn b/interfaces/kits/napi/screenshot/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9b7cbb6805ae41c27b75d1a62637630cedd35f90 --- /dev/null +++ b/interfaces/kits/napi/screenshot/BUILD.gn @@ -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. + +import("//build/ohos.gni") + +## Build screenshot.so {{{ +ohos_shared_library("screenshot") { + sources = [ "native_screenshot_module.cpp" ] + + deps = [ + "../common:wm_napi_common", + "//foundation/multimedia/image_standard/interfaces/innerkits:image", + "//foundation/windowmanager/wm:libwm", + "//foundation/windowmanager/wm:libwmutil", + ] + + external_deps = [ + "aafwk_standard:runtime", + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module" + part_name = "window_manager" + subsystem_name = "window" +} +## Build screenshot.so }}} diff --git a/interfaces/kits/napi/screenshot/api/@ohos.sceenshot.d.ts b/interfaces/kits/napi/screenshot/api/@ohos.sceenshot.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..c0337cb9bd76787fc7e50d27b6776fbe9a0821c2 --- /dev/null +++ b/interfaces/kits/napi/screenshot/api/@ohos.sceenshot.d.ts @@ -0,0 +1,101 @@ +/* + * 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. + */ + +import { AsyncCallback, ErrorCallback } from './basic'; +import image from './@ohos.multimedia.image'; + +/** + * interface for screenshot + * @devices tv, phone, tablet, wearable, car + * @systemapi Hide this for inner system use. + * @since 7 + */ +declare namespace screenshot { + /** + * Takes a screenshot and saves it as a PixelMap object + * @param options Screenshot options, which consist of screenRect, imageSize, and rotation. You need to set these parameters + * @devices tv, phone, tablet, wearable, car + * @permission ohos.permission.CAPTURE_SCREEN + * @since 7 + */ + function save(options: ScreenshotOptions, callback: AsyncCallback): void; + + /** + * Takes a screenshot and saves it as a PixelMap object + * @param options Screenshot options, which consist of screenRect, imageSize, and rotation. You need to set these parameters + * @devices tv, phone, tablet, wearable, car + * @permission ohos.permission.CAPTURE_SCREEN + * @since 7 + */ + function save(callback: AsyncCallback): void; + + /** + * Takes a screenshot and saves it as a PixelMap object + * @param options Screenshot options, which consist of screenRect, imageSize, and rotation. You need to set these parameters + * @devices tv, phone, tablet, wearable, car + * @permission ohos.permission.CAPTURE_SCREEN + * @since 7 + */ + function save(options?: ScreenshotOptions): Promise; + + /** + * Describes the region of the screen to capture + * @devices tv, phone, tablet, wearable, car + * @since 7 + */ + interface Rect { + left: number; + top: number; + width: number; + height: number; + } + + /** + * Describes the size of the screen region to capture + * @devices tv, phone, tablet, wearable, car + * @since 7 + */ + interface Size { + width: number; + height: number; + } + + /** + * Describes screenshot options + * @devices tv, phone, tablet, wearable, car + * @since 7 + */ + interface ScreenshotOptions { + /** + * Region of the screen to capture. If this parameter is null, the full screen will be captured + */ + screenRect?: Rect; + /** + * Region of the screen to capture. If this parameter is null, the full screen will be captured + */ + imageSize?: Size; + /** + * Rotation angle of the screenshot. The value can be 0, 90, 180, or 270. The default value is 0 + */ + rotation?: number; + /** + * The specify screen to be captured. + * @since 8 + */ + displayId?: number; + } +} + +export default screenshot; diff --git a/interfaces/kits/napi/screenshot/native_screenshot_module.cpp b/interfaces/kits/napi/screenshot/native_screenshot_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85a84bf80753ceda042ada66bf759f99cd1f419e --- /dev/null +++ b/interfaces/kits/napi/screenshot/native_screenshot_module.cpp @@ -0,0 +1,113 @@ +/* + * 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 "native_screenshot_module.h" +#include +#include "display_manager.h" +#include "display_property.h" +#include "pixel_map.h" +#include "pixel_map_napi.h" +#include "wm_napi_common.h" + +namespace OHOS { +namespace Rosen { +namespace save { +struct Rect { + int32_t left = 0; + int32_t top = 0; + int32_t width = 0; + int32_t height = 0; +}; + +struct Size { + int32_t width = 0; + int32_t height = 0; +}; + +struct Option { + Rect rect; + Size size; + int rotation = 0; + DisplayId displayId = 0; +}; + +struct Param { + WMError wret; + Option screenshotOptions; + std::shared_ptr image; +}; + +void Async(napi_env env, std::unique_ptr ¶m) +{ + param->screenshotOptions.displayId = DisplayManager::GetInstance().GetDefaultDisplayId(); + param->image = DisplayManager::GetInstance().GetScreenshot(param->screenshotOptions.displayId); + if (param->image == nullptr) { + GNAPI_LOG("Get Screenshot failed!"); + param->wret = WMError::WM_ERROR_NULLPTR; + return; + } + param->wret = WMError::WM_OK; +} + +napi_value Resolve(napi_env env, std::unique_ptr ¶m) +{ + napi_value result; + if (param->wret != WMError::WM_OK) { + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; + } + + GNAPI_LOG("Screenshot image Width %{public}d, Height %{public}d", + param->image->GetWidth(), param->image->GetHeight()); + napi_value jsImage = OHOS::Media::PixelMapNapi::CreatePixelMap(env, param->image); + return jsImage; +} + +napi_value MainFunc(napi_env env, napi_callback_info info) +{ + GNAPI_LOG("%{public}s called", __PRETTY_FUNCTION__); + + auto param = std::make_unique(); + return CreatePromise(env, __PRETTY_FUNCTION__, Async, Resolve, param); +} +} // namespace save + +napi_value ScreenshotModuleInit(napi_env env, napi_value exports) +{ + GNAPI_LOG("%{public}s called", __PRETTY_FUNCTION__); + + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("save", save::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 screenshotModule = { + .nm_version = 1, // NAPI v1 + .nm_flags = 0, // normal + .nm_filename = nullptr, + .nm_register_func = OHOS::Rosen::ScreenshotModuleInit, + .nm_modname = "screenshot", + .nm_priv = nullptr, + }; + napi_module_register(&screenshotModule); +} diff --git a/interfaces/kits/napi/screenshot/native_screenshot_module.h b/interfaces/kits/napi/screenshot/native_screenshot_module.h new file mode 100644 index 0000000000000000000000000000000000000000..4e8b2b1a492bb76c285c8fa252e8031637f6f046 --- /dev/null +++ b/interfaces/kits/napi/screenshot/native_screenshot_module.h @@ -0,0 +1,19 @@ +/* + * 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 NATIVE_SCREENSHOT_MODULE_H +#define NATIVE_SCREENSHOT_MODULE_H + +#endif // NATIVE_SCREENSHOT_MODULE_H diff --git a/interfaces/kits/napi/window/native_window_module.cpp b/interfaces/kits/napi/window/native_window_module.cpp index 288fe190e51c088c864d19107d0fcfacffd03324..28f0e1cd7bd3a4862dbac2c897161de47e619ef9 100644 --- a/interfaces/kits/napi/window/native_window_module.cpp +++ b/interfaces/kits/napi/window/native_window_module.cpp @@ -51,6 +51,7 @@ napi_value WindowConstructor(napi_env env, napi_callback_info info) namespace ResetSize { struct Param { ::OHOS::AppExecFwk::Ability *ability; + WMError wret; int width; int height; }; @@ -59,9 +60,10 @@ void Async(napi_env env, std::unique_ptr& param) { if (param == nullptr || param->ability == nullptr || param->ability->GetWindow() == nullptr) { GNAPI_LOG("Error, use nullptr!)"); + param->wret = WMError::WM_ERROR_NULLPTR; return; } - param->ability->GetWindow()->Resize(param->width, param->height); + param->wret = param->ability->GetWindow()->Resize(param->width, param->height); } napi_value MainFunc(napi_env env, napi_callback_info info) @@ -88,6 +90,7 @@ napi_value MainFunc(napi_env env, napi_callback_info info) namespace MoveTo { struct Param { ::OHOS::AppExecFwk::Ability *ability; + WMError wret; int x; int y; }; @@ -96,9 +99,10 @@ void Async(napi_env env, std::unique_ptr& param) { if (param == nullptr || param->ability == nullptr || param->ability->GetWindow() == nullptr) { GNAPI_LOG("Error, use nullptr!)"); + param->wret = WMError::WM_ERROR_NULLPTR; return; } - param->ability->GetWindow()->MoveTo(param->x, param->y); + param->wret = param->ability->GetWindow()->MoveTo(param->x, param->y); } napi_value MainFunc(napi_env env, napi_callback_info info) @@ -125,16 +129,29 @@ napi_value MainFunc(napi_env env, napi_callback_info info) namespace SetWindowType { struct Param { ::OHOS::AppExecFwk::Ability *ability; + WMError wret; int windowType; }; +std::map windowTypeMap { + { 0, WindowType::WINDOW_TYPE_APP_MAIN_WINDOW }, // 0 is TYPE_APP + { 30, WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW }, // 30 is TYPE_SYSTEM_ALERT + { 70, WindowType::WINDOW_TYPE_VOLUME_OVERLAY }, // 70 is TYPE_SYSTEM_VOLUME + { 90, WindowType::WINDOW_TYPE_PANEL }, // 90 is TYPE_SYSTEM_PANEL +}; + void Async(napi_env env, std::unique_ptr& param) { if (param == nullptr || param->ability == nullptr || param->ability->GetWindow() == nullptr) { GNAPI_LOG("Error, use nullptr!)"); return; } - param->ability->GetWindow()->SetWindowType(static_cast(param->windowType)); + if (windowTypeMap.count(param->windowType) == 0) { + GNAPI_LOG("invalid window type"); + param->wret = WMError::WM_ERROR_INVALID_PARAM; + return; + } + param->wret = param->ability->GetWindow()->SetWindowType(windowTypeMap.at(param->windowType)); } void CreateWindowTypeObject(napi_env env, napi_value value)