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 <napi/native_api.h>
17 #include <pthread.h>
18 #include <string>
19 #include <unistd.h>
20
21 #include "hilog_wrapper.h"
22 #include "napi/native_node_api.h"
23 #include "napi_wallpaper_ability.h"
24 #include "wallpaper_manager_common_info.h"
25
26 namespace OHOS {
27 namespace WallpaperNAPI {
28
29 EXTERN_C_START
Init(napi_env env,napi_value exports)30 static napi_value Init(napi_env env, napi_value exports)
31 {
32 HILOG_INFO("napi_module Init start...");
33 napi_value wallpaperType = nullptr;
34 napi_value systemType = nullptr;
35 napi_value lockscreenType = nullptr;
36 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(WALLPAPER_SYSTEM), &systemType));
37 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(WALLPAPER_LOCKSCREEN), &lockscreenType));
38 NAPI_CALL(env, napi_create_object(env, &wallpaperType));
39 NAPI_CALL(env, napi_set_named_property(env, wallpaperType, "WALLPAPER_SYSTEM", systemType));
40 NAPI_CALL(env, napi_set_named_property(env, wallpaperType, "WALLPAPER_LOCKSCREEN", lockscreenType));
41
42 // WallpaperResourceType
43 napi_value wallpaperResourceType = nullptr;
44 napi_value wallpaperResDefault = nullptr;
45 napi_value wallpaperResPicture = nullptr;
46 napi_value wallpaperResVideo = nullptr;
47 napi_value wallpaperResPackage = nullptr;
48 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(DEFAULT), &wallpaperResDefault));
49 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PICTURE), &wallpaperResPicture));
50 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(VIDEO), &wallpaperResVideo));
51 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PACKAGE), &wallpaperResPackage));
52 NAPI_CALL(env, napi_create_object(env, &wallpaperResourceType));
53 NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "DEFAULT", wallpaperResDefault));
54 NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "PICTURE", wallpaperResPicture));
55 NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "VIDEO", wallpaperResVideo));
56 NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "PACKAGE", wallpaperResPackage));
57
58 napi_property_descriptor desc[] = {
59 DECLARE_NAPI_FUNCTION("getColors", NAPI_GetColors),
60 DECLARE_NAPI_FUNCTION("getColorsSync", NAPI_GetColorsSync),
61 DECLARE_NAPI_FUNCTION("getId", NAPI_GetId),
62 DECLARE_NAPI_FUNCTION("getFile", NAPI_GetFile),
63 DECLARE_NAPI_FUNCTION("getMinHeight", NAPI_GetMinHeight),
64 DECLARE_NAPI_FUNCTION("getMinHeightSync", NAPI_GetMinHeightSync),
65 DECLARE_NAPI_FUNCTION("getMinWidth", NAPI_GetMinWidth),
66 DECLARE_NAPI_FUNCTION("getMinWidthSync", NAPI_GetMinWidthSync),
67 DECLARE_NAPI_FUNCTION("isChangePermitted", NAPI_IsChangePermitted),
68 DECLARE_NAPI_FUNCTION("isOperationAllowed", NAPI_IsOperationAllowed),
69 DECLARE_NAPI_FUNCTION("reset", NAPI_Reset),
70 DECLARE_NAPI_FUNCTION("restore", NAPI_Restore),
71 DECLARE_NAPI_FUNCTION("setWallpaper", NAPI_SetWallpaper),
72 DECLARE_NAPI_FUNCTION("setImage", NAPI_SetImage),
73 DECLARE_NAPI_FUNCTION("getPixelMap", NAPI_GetPixelMap),
74 DECLARE_NAPI_FUNCTION("getImage", NAPI_GetImage),
75 DECLARE_NAPI_FUNCTION("on", NAPI_On),
76 DECLARE_NAPI_FUNCTION("off", NAPI_Off),
77 DECLARE_NAPI_FUNCTION("setVideo", NAPI_SetVideo),
78 DECLARE_NAPI_FUNCTION("sendEvent", NAPI_SendEvent),
79 DECLARE_NAPI_FUNCTION("setCustomWallpaper", NAPI_SetCustomWallpaper),
80 DECLARE_NAPI_STATIC_PROPERTY("WallpaperType", wallpaperType),
81 DECLARE_NAPI_STATIC_PROPERTY("WallpaperResourceType", wallpaperResourceType),
82 };
83
84 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
85 HILOG_INFO("napi_module Init end...");
86 return exports;
87 }
88
89 EXTERN_C_END
90
91 /*
92 * Module define
93 */
94 static napi_module _module = {
95 .nm_version = 1,
96 .nm_flags = 0,
97 .nm_filename = nullptr,
98 .nm_register_func = Init,
99 .nm_modname = "wallpaper",
100 .nm_priv = ((void *)0),
101 .reserved = { 0 },
102 };
103
RegisterModule(void)104 extern "C" __attribute__((constructor)) void RegisterModule(void)
105 {
106 napi_module_register(&_module);
107 }
108 } // namespace WallpaperNAPI
109 } // namespace OHOS