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 <unistd.h>
18 
19 #include <string>
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
InitWallpaperType(napi_env & env)30 static napi_value InitWallpaperType(napi_env &env)
31 {
32     napi_value wallpaperType = nullptr;
33     napi_value systemType = nullptr;
34     napi_value lockscreenType = nullptr;
35     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(WALLPAPER_SYSTEM), &systemType));
36     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(WALLPAPER_LOCKSCREEN), &lockscreenType));
37     NAPI_CALL(env, napi_create_object(env, &wallpaperType));
38     NAPI_CALL(env, napi_set_named_property(env, wallpaperType, "WALLPAPER_SYSTEM", systemType));
39     NAPI_CALL(env, napi_set_named_property(env, wallpaperType, "WALLPAPER_LOCKSCREEN", lockscreenType));
40     return wallpaperType;
41 }
42 
InitWallpaperResourceType(napi_env & env)43 static napi_value InitWallpaperResourceType(napi_env &env)
44 {
45     napi_value wallpaperResourceType = nullptr;
46     napi_value wallpaperResDefault = nullptr;
47     napi_value wallpaperResPicture = nullptr;
48     napi_value wallpaperResVideo = nullptr;
49     napi_value wallpaperResPackage = nullptr;
50     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(DEFAULT), &wallpaperResDefault));
51     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PICTURE), &wallpaperResPicture));
52     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(VIDEO), &wallpaperResVideo));
53     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PACKAGE), &wallpaperResPackage));
54     NAPI_CALL(env, napi_create_object(env, &wallpaperResourceType));
55     NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "DEFAULT", wallpaperResDefault));
56     NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "PICTURE", wallpaperResPicture));
57     NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "VIDEO", wallpaperResVideo));
58     NAPI_CALL(env, napi_set_named_property(env, wallpaperResourceType, "PACKAGE", wallpaperResPackage));
59     return wallpaperResourceType;
60 }
61 
InitRotateState(napi_env & env)62 static napi_value InitRotateState(napi_env &env)
63 {
64     napi_value rotateState = nullptr;
65     napi_value port = nullptr;
66     napi_value land = nullptr;
67     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(RotateState::PORT), &port));
68     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(RotateState::LAND), &land));
69     NAPI_CALL(env, napi_create_object(env, &rotateState));
70     NAPI_CALL(env, napi_set_named_property(env, rotateState, "PORT", port));
71     NAPI_CALL(env, napi_set_named_property(env, rotateState, "PORTRAIT", port));
72     NAPI_CALL(env, napi_set_named_property(env, rotateState, "LAND", land));
73     NAPI_CALL(env, napi_set_named_property(env, rotateState, "LANDSCAPE", land));
74     return rotateState;
75 }
76 
InitFoldState(napi_env & env)77 static napi_value InitFoldState(napi_env &env)
78 {
79     napi_value foldState = nullptr;
80     napi_value normal = nullptr;
81     napi_value unfold_1 = nullptr;
82     napi_value unfold_2 = nullptr;
83     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::NORMAL), &normal));
84     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::UNFOLD_1), &unfold_1));
85     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::UNFOLD_2), &unfold_2));
86     NAPI_CALL(env, napi_create_object(env, &foldState));
87     NAPI_CALL(env, napi_set_named_property(env, foldState, "NORMAL", normal));
88     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_1", unfold_1));
89     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_ONCE_STATE", unfold_1));
90     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_2", unfold_2));
91     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_TWICE_STATE", unfold_2));
92     return foldState;
93 }
94 
Init(napi_env env,napi_value exports)95 static napi_value Init(napi_env env, napi_value exports)
96 {
97     HILOG_DEBUG("napi_module Init start...");
98     napi_value wallpaperType = InitWallpaperType(env);
99     napi_value wallpaperResourceType = InitWallpaperResourceType(env);
100     napi_value rotateState = InitRotateState(env);
101     napi_value foldState = InitFoldState(env);
102 
103     napi_property_descriptor desc[] = {
104         DECLARE_NAPI_FUNCTION("getColors", NAPI_GetColors),
105         DECLARE_NAPI_FUNCTION("getColorsSync", NAPI_GetColorsSync),
106         DECLARE_NAPI_FUNCTION("getId", NAPI_GetId),
107         DECLARE_NAPI_FUNCTION("getFile", NAPI_GetFile),
108         DECLARE_NAPI_FUNCTION("getMinHeight", NAPI_GetMinHeight),
109         DECLARE_NAPI_FUNCTION("getMinHeightSync", NAPI_GetMinHeightSync),
110         DECLARE_NAPI_FUNCTION("getMinWidth", NAPI_GetMinWidth),
111         DECLARE_NAPI_FUNCTION("getMinWidthSync", NAPI_GetMinWidthSync),
112         DECLARE_NAPI_FUNCTION("isChangePermitted", NAPI_IsChangePermitted),
113         DECLARE_NAPI_FUNCTION("isOperationAllowed", NAPI_IsOperationAllowed),
114         DECLARE_NAPI_FUNCTION("reset", NAPI_Reset),
115         DECLARE_NAPI_FUNCTION("restore", NAPI_Restore),
116         DECLARE_NAPI_FUNCTION("setWallpaper", NAPI_SetWallpaper),
117         DECLARE_NAPI_FUNCTION("setImage", NAPI_SetImage),
118         DECLARE_NAPI_FUNCTION("getPixelMap", NAPI_GetPixelMap),
119         DECLARE_NAPI_FUNCTION("getImage", NAPI_GetImage),
120         DECLARE_NAPI_FUNCTION("getCorrespondWallpaper", NAPI_GetCorrespondWallpaper),
121         DECLARE_NAPI_FUNCTION("getWallpaperByState", NAPI_GetCorrespondWallpaper),
122         DECLARE_NAPI_FUNCTION("on", NAPI_On),
123         DECLARE_NAPI_FUNCTION("off", NAPI_Off),
124         DECLARE_NAPI_FUNCTION("setVideo", NAPI_SetVideo),
125         DECLARE_NAPI_FUNCTION("sendEvent", NAPI_SendEvent),
126         DECLARE_NAPI_FUNCTION("setCustomWallpaper", NAPI_SetCustomWallpaper),
127         DECLARE_NAPI_FUNCTION("setAllWallpapers", NAPI_SetAllWallpapers),
128         DECLARE_NAPI_STATIC_PROPERTY("WallpaperType", wallpaperType),
129         DECLARE_NAPI_STATIC_PROPERTY("WallpaperResourceType", wallpaperResourceType),
130         DECLARE_NAPI_STATIC_PROPERTY("RotateState", rotateState),
131         DECLARE_NAPI_STATIC_PROPERTY("FoldState", foldState),
132     };
133 
134     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
135     HILOG_DEBUG("napi_module Init end...");
136     return exports;
137 }
138 
139 EXTERN_C_END
140 
141 /*
142  * Module define
143  */
144 static napi_module g_wallpaperExtensionModule = {
145     .nm_version = 1,
146     .nm_flags = 0,
147     .nm_filename = nullptr,
148     .nm_register_func = Init,
149     .nm_modname = "wallpaper",
150     .nm_priv = ((void *)0),
151     .reserved = { 0 },
152 };
153 
RegisterModule(void)154 extern "C" __attribute__((constructor)) void RegisterModule(void)
155 {
156     napi_module_register(&g_wallpaperExtensionModule);
157 }
158 } // namespace WallpaperNAPI
159 } // namespace OHOS