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