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 
InitRotateState(napi_env &env)63 static napi_value InitRotateState(napi_env &env)
64 {
65     napi_value rotateState = nullptr;
66     napi_value port = nullptr;
67     napi_value land = nullptr;
68     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(RotateState::PORT), &port));
69     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(RotateState::LAND), &land));
70     NAPI_CALL(env, napi_create_object(env, &rotateState));
71     NAPI_CALL(env, napi_set_named_property(env, rotateState, "PORT", port));
72     NAPI_CALL(env, napi_set_named_property(env, rotateState, "LAND", land));
73     return rotateState;
74 }
75 
InitFoldState(napi_env &env)76 static napi_value InitFoldState(napi_env &env)
77 {
78     napi_value foldState = nullptr;
79     napi_value normal = nullptr;
80     napi_value unfold_1 = nullptr;
81     napi_value unfold_2 = nullptr;
82     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::NORMAL), &normal));
83     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::UNFOLD_1), &unfold_1));
84     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FoldState::UNFOLD_2), &unfold_2));
85     NAPI_CALL(env, napi_create_object(env, &foldState));
86     NAPI_CALL(env, napi_set_named_property(env, foldState, "NORMAL", normal));
87     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_1", unfold_1));
88     NAPI_CALL(env, napi_set_named_property(env, foldState, "UNFOLD_2", unfold_2));
89     return foldState;
90 }
91 
Init(napi_env env, napi_value exports)92 static napi_value Init(napi_env env, napi_value exports)
93 {
94     HILOG_DEBUG("napi_module Init start...");
95     napi_value wallpaperType = InitWallpaperType(env);
96     napi_value wallpaperResourceType = InitWallpaperResourceType(env);
97     napi_value rotateState = InitRotateState(env);
98     napi_value foldState = InitFoldState(env);
99 
100     napi_property_descriptor desc[] = {
101         DECLARE_NAPI_FUNCTION("getColors", NAPI_GetColors),
102         DECLARE_NAPI_FUNCTION("getColorsSync", NAPI_GetColorsSync),
103         DECLARE_NAPI_FUNCTION("getId", NAPI_GetId),
104         DECLARE_NAPI_FUNCTION("getFile", NAPI_GetFile),
105         DECLARE_NAPI_FUNCTION("getMinHeight", NAPI_GetMinHeight),
106         DECLARE_NAPI_FUNCTION("getMinHeightSync", NAPI_GetMinHeightSync),
107         DECLARE_NAPI_FUNCTION("getMinWidth", NAPI_GetMinWidth),
108         DECLARE_NAPI_FUNCTION("getMinWidthSync", NAPI_GetMinWidthSync),
109         DECLARE_NAPI_FUNCTION("isChangePermitted", NAPI_IsChangePermitted),
110         DECLARE_NAPI_FUNCTION("isOperationAllowed", NAPI_IsOperationAllowed),
111         DECLARE_NAPI_FUNCTION("reset", NAPI_Reset),
112         DECLARE_NAPI_FUNCTION("restore", NAPI_Restore),
113         DECLARE_NAPI_FUNCTION("setWallpaper", NAPI_SetWallpaper),
114         DECLARE_NAPI_FUNCTION("setImage", NAPI_SetImage),
115         DECLARE_NAPI_FUNCTION("getPixelMap", NAPI_GetPixelMap),
116         DECLARE_NAPI_FUNCTION("getImage", NAPI_GetImage),
117         DECLARE_NAPI_FUNCTION("getCorrespondWallpaper", NAPI_GetCorrespondWallpaper),
118         DECLARE_NAPI_FUNCTION("on", NAPI_On),
119         DECLARE_NAPI_FUNCTION("off", NAPI_Off),
120         DECLARE_NAPI_FUNCTION("setVideo", NAPI_SetVideo),
121         DECLARE_NAPI_FUNCTION("sendEvent", NAPI_SendEvent),
122         DECLARE_NAPI_FUNCTION("setCustomWallpaper", NAPI_SetCustomWallpaper),
123         DECLARE_NAPI_FUNCTION("setAllWallpapers", NAPI_SetAllWallpapers),
124         DECLARE_NAPI_STATIC_PROPERTY("WallpaperType", wallpaperType),
125         DECLARE_NAPI_STATIC_PROPERTY("WallpaperResourceType", wallpaperResourceType),
126         DECLARE_NAPI_STATIC_PROPERTY("RotateState", rotateState),
127         DECLARE_NAPI_STATIC_PROPERTY("FoldState", foldState),
128     };
129 
130     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
131     HILOG_DEBUG("napi_module Init end...");
132     return exports;
133 }
134 
135 EXTERN_C_END
136 
137 /*
138  * Module define
139  */
140 static napi_module g_wallpaperExtensionModule = {
141     .nm_version = 1,
142     .nm_flags = 0,
143     .nm_filename = nullptr,
144     .nm_register_func = Init,
145     .nm_modname = "wallpaper",
146     .nm_priv = ((void *)0),
147     .reserved = { 0 },
148 };
149 
RegisterModule(void)150 extern "C" __attribute__((constructor)) void RegisterModule(void)
151 {
152     napi_module_register(&g_wallpaperExtensionModule);
153 }
154 } // namespace WallpaperNAPI
155 } // namespace OHOS