• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "napi/native_api.h"
16 #include <cstddef>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 #define LOG_TAG "DisplayManager_NDK"
21 #include <hilog/log.h>
22 #include <window_manager/oh_display_info.h>
23 #include <window_manager/oh_display_manager.h>
24 #include <window_manager/oh_display_capture.h>
25 #include <multimedia/image_framework/image/pixelmap_native.h>
26 
27 
Add(napi_env env,napi_callback_info info)28 static napi_value Add(napi_env env, napi_callback_info info)
29 {
30     size_t argc = 2;
31     napi_value args[2] = {nullptr};
32 
33     napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
34 
35     napi_valuetype valuetype0;
36     napi_typeof(env, args[0], &valuetype0);
37 
38     napi_valuetype valuetype1;
39     napi_typeof(env, args[1], &valuetype1);
40 
41     double value0;
42     napi_get_value_double(env, args[0], &value0);
43 
44     double value1;
45     napi_get_value_double(env, args[1], &value1);
46 
47     napi_value sum;
48     napi_create_double(env, value0 + value1, &sum);
49 
50     return sum;
51 
52 }
CreateJsValue(napi_env env,int32_t value)53 static napi_value CreateJsValue(napi_env env, int32_t value)
54 {
55     napi_value result = nullptr;
56     napi_create_int32(env,value,&result);
57     return result;
58 }
59 
CreateJsValue(napi_env env,uint32_t value)60 static napi_value CreateJsValue(napi_env env, uint32_t value)
61 {
62     napi_value result = nullptr;
63     napi_create_uint32(env,value,&result);
64     return result;
65 }
66 
CreateJsValue(napi_env env,double value)67 static napi_value CreateJsValue(napi_env env, double value)
68 {
69     napi_value result = nullptr;
70     napi_create_double(env,value,&result);
71     return result;
72 }
73 
CreateJsString(napi_env env,char * value)74 static napi_value CreateJsString(napi_env env, char* value)
75 {
76     napi_value result = nullptr;
77     napi_create_string_utf8(env,value,strlen(value),&result);
78     return result;
79 }
80 
CreateJsColorSpaceArray(napi_env env,NativeDisplayManager_DisplayColorSpace * colorSpaces)81 static napi_value CreateJsColorSpaceArray(napi_env env, NativeDisplayManager_DisplayColorSpace *colorSpaces)
82 {
83     napi_value arrayValue = nullptr;
84     if(colorSpaces != nullptr){
85         napi_create_array_with_length(env,colorSpaces->colorSpaceLength,&arrayValue);
86         for(int index = 0;index < colorSpaces->colorSpaceLength;index++){
87             napi_set_element(env,arrayValue,index,CreateJsValue(env,colorSpaces->colorSpaces[index]));
88         }
89     }else{
90         napi_create_array_with_length(env,0,&arrayValue);
91     }
92     return arrayValue;
93 }
94 
CreateJsHDRFormatArray(napi_env env,NativeDisplayManager_DisplayHdrFormat * hdrFormats)95 static napi_value CreateJsHDRFormatArray(napi_env env, NativeDisplayManager_DisplayHdrFormat *hdrFormats)
96 {
97     napi_value arrayValue = nullptr;
98     if(hdrFormats != nullptr){
99         napi_create_array_with_length(env,hdrFormats->hdrFormatLength,&arrayValue);
100         for(int index = 0;index < hdrFormats->hdrFormatLength;index++){
101             napi_set_element(env,arrayValue,index,CreateJsValue(env,hdrFormats->hdrFormats[index]));
102         }
103     }else{
104         napi_create_array_with_length(env,0,&arrayValue);
105     }
106     return arrayValue;
107 }
108 
NapiSetNameProperty(napi_env env,napi_value objValue,NativeDisplayManager_DisplayInfo * info)109 static void NapiSetNameProperty(napi_env env,napi_value objValue,NativeDisplayManager_DisplayInfo *info)
110 {
111     if(objValue == nullptr || info == nullptr){
112         OH_LOG_INFO(LOG_APP, "objValue or DisplayInfo is null");
113         return;
114     }
115     napi_set_named_property(env,objValue,"id",CreateJsValue(env,info->id));
116     napi_set_named_property(env,objValue,"name",CreateJsString(env,info->name));
117     napi_set_named_property(env,objValue,"alive",CreateJsValue(env,info->isAlive));
118     napi_set_named_property(env,objValue,"width",CreateJsValue(env,info->width));
119     napi_set_named_property(env,objValue,"height",CreateJsValue(env,info->height));
120     napi_set_named_property(env,objValue,"physicalWidth",CreateJsValue(env,info->physicalWidth));
121     napi_set_named_property(env,objValue,"physicalHeight",CreateJsValue(env,info->physicalHeight));
122     napi_set_named_property(env,objValue,"refreshRate",CreateJsValue(env,info->refreshRate));
123     napi_set_named_property(env,objValue,"availableWidth",CreateJsValue(env,info->availableWidth));
124     napi_set_named_property(env,objValue,"availableHeight",CreateJsValue(env,info->availableHeight));
125     napi_set_named_property(env,objValue,"densityDPI",CreateJsValue(env,info->densityDPI));
126     napi_set_named_property(env,objValue,"densityPixels",CreateJsValue(env,info->densityPixels));
127     napi_set_named_property(env,objValue,"scaledDensity",CreateJsValue(env,info->scaledDensity));
128     napi_set_named_property(env,objValue,"xDPI",CreateJsValue(env,info->xDPI));
129     napi_set_named_property(env,objValue,"yDPI",CreateJsValue(env,info->yDPI));
130     napi_set_named_property(env,objValue,"rotation",CreateJsValue(env,info->rotation));
131     napi_set_named_property(env,objValue,"state",CreateJsValue(env,info->state));
132     napi_set_named_property(env,objValue,"orientation",CreateJsValue(env,info->orientation));
133     napi_set_named_property(env,objValue,"hdrFormat",CreateJsHDRFormatArray(env,info->hdrFormat));
134     napi_set_named_property(env,objValue,"colorSpace",CreateJsColorSpaceArray(env,info->colorSpace));
135 }
136 
GetPrimaryDisplay(napi_env env,napi_callback_info info)137 static napi_value GetPrimaryDisplay(napi_env env, napi_callback_info info)
138 {
139     OH_LOG_INFO(LOG_APP, "get primary display");
140     NativeDisplayManager_DisplayInfo *displayInfo = nullptr;
141     napi_value objValue = nullptr;
142 
143     NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_CreatePrimaryDisplay(&displayInfo);
144     OH_LOG_INFO(LOG_APP, "get primary diaplay errCode =%{public}d.",errCode);
145     if(errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK){
146         napi_create_object(env,&objValue);
147         NapiSetNameProperty(env,objValue,displayInfo);
148         OH_NativeDisplayManager_DestroyDisplay(displayInfo);
149     }else{
150         napi_create_uint32(env,errCode,&objValue);
151     }
152     return objValue;
153 }
154 
DestroyDisplayWithoutCreate(napi_env env,napi_callback_info info)155 static napi_value DestroyDisplayWithoutCreate(napi_env env, napi_callback_info info)
156 {
157     OH_LOG_INFO(LOG_APP, "destroy display without create.");
158     NativeDisplayManager_DisplayInfo *displayInfo = nullptr;
159     napi_value objValue = nullptr;
160 
161     OH_NativeDisplayManager_DestroyDisplay(displayInfo);
162     napi_create_uint32(env,NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK,&objValue);
163     return objValue;
164 }
165 
NapiCreateAllDisplay(napi_env env,NativeDisplayManager_DisplaysInfo * infos)166 static napi_value NapiCreateAllDisplay(napi_env env,NativeDisplayManager_DisplaysInfo *infos)
167 {
168     napi_value arrayValue;
169     if(infos == nullptr){
170         napi_create_array_with_length(env,0,&arrayValue);
171     }else{
172         napi_create_array_with_length(env,infos->displaysLength,&arrayValue);
173         for(int32_t i = 0;i<infos->displaysLength;i++){
174             napi_value objValue = nullptr;
175             napi_create_object(env,&objValue);
176             NativeDisplayManager_DisplayInfo displayItem = infos->displaysInfo[i];
177             NapiSetNameProperty(env,objValue,&(displayItem));
178             napi_set_element(env,arrayValue,i,objValue);
179         }
180     }
181     return arrayValue;
182 }
183 
GetAllDisplay(napi_env env,napi_callback_info info)184 static napi_value GetAllDisplay(napi_env env, napi_callback_info info)
185 {
186     OH_LOG_INFO(LOG_APP, "get all display");
187     NativeDisplayManager_DisplaysInfo *displaysInfo = nullptr;
188     napi_value arrayValue = nullptr;
189 
190     NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_CreateAllDisplays(&displaysInfo);
191     OH_LOG_INFO(LOG_APP, "get all diaplay errCode =%{public}d.",errCode);
192     if(errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK){
193         arrayValue = NapiCreateAllDisplay(env,displaysInfo);
194         OH_NativeDisplayManager_DestroyAllDisplays(displaysInfo);
195     }else{
196         napi_create_uint32(env,errCode,&arrayValue);
197     }
198     return arrayValue;
199 }
200 
GetDisplayById(napi_env env,napi_callback_info info)201 static napi_value GetDisplayById(napi_env env, napi_callback_info info)
202 {
203     size_t argc = 1;
204     napi_value args[1] = {nullptr};
205     napi_get_cb_info(env,info,&argc,args,nullptr,nullptr);
206     napi_valuetype valuetype0;
207     napi_typeof(env,args[0],&valuetype0);
208     int32_t displayId;
209     napi_get_value_int32(env,args[0],&displayId);
210     OH_LOG_INFO(LOG_APP, "get display by id=%{public}d.",displayId);
211 
212     NativeDisplayManager_DisplayInfo *displayInfo = nullptr;
213     napi_value objValue = nullptr;
214 
215     NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_CreateDisplayById(displayId,&displayInfo);
216     OH_LOG_INFO(LOG_APP, "get display by id errCode=%{public}d.",errCode);
217     if(errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK){
218         napi_create_object(env,&objValue);
219         NapiSetNameProperty(env,objValue,displayInfo);
220         OH_NativeDisplayManager_DestroyDisplay(displayInfo);
221     }else{
222         napi_create_uint32(env,errCode,&objValue);
223     }
224     return objValue;
225 }
226 
GetDisplayState_UNKNOWN(napi_env env,napi_callback_info info)227 static napi_value GetDisplayState_UNKNOWN(napi_env env, napi_callback_info info)
228 {
229     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_UNKNOWN;
230     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_UNKNOWN =%{public}d.",DisplayState);
231     napi_value result;
232     napi_create_uint32(env,DisplayState,&result);
233     return result;
234 }
235 
GetDisplayState_OFF(napi_env env,napi_callback_info info)236 static napi_value GetDisplayState_OFF(napi_env env, napi_callback_info info)
237 {
238     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_OFF;
239     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_OFF =%{public}d.",DisplayState);
240     napi_value result;
241     napi_create_uint32(env,DisplayState,&result);
242     return result;
243 }
244 
GetDisplayState_ON(napi_env env,napi_callback_info info)245 static napi_value GetDisplayState_ON(napi_env env, napi_callback_info info)
246 {
247     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_ON;
248     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_ON =%{public}d.",DisplayState);
249     napi_value result;
250     napi_create_uint32(env,DisplayState,&result);
251     return result;
252 }
253 
GetDisplayState_DOZE(napi_env env,napi_callback_info info)254 static napi_value GetDisplayState_DOZE(napi_env env, napi_callback_info info)
255 {
256     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_DOZE;
257     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_DOZE =%{public}d.",DisplayState);
258     napi_value result;
259     napi_create_uint32(env,DisplayState,&result);
260     return result;
261 }
262 
GetDisplayState_DOZE_SUSPEND(napi_env env,napi_callback_info info)263 static napi_value GetDisplayState_DOZE_SUSPEND(napi_env env, napi_callback_info info)
264 {
265     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_DOZE_SUSPEND;
266     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_DOZE_SUSPEND =%{public}d.",DisplayState);
267     napi_value result;
268     napi_create_uint32(env,DisplayState,&result);
269     return result;
270 }
271 
GetDisplayState_VR(napi_env env,napi_callback_info info)272 static napi_value GetDisplayState_VR(napi_env env, napi_callback_info info)
273 {
274     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_VR;
275     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_VR =%{public}d.",DisplayState);
276     napi_value result;
277     napi_create_uint32(env,DisplayState,&result);
278     return result;
279 }
280 
GetDisplayState_ON_SUSPEND(napi_env env,napi_callback_info info)281 static napi_value GetDisplayState_ON_SUSPEND(napi_env env, napi_callback_info info)
282 {
283     NativeDisplayManager_DisplayState DisplayState = DISPLAY_MANAGER_DISPLAY_STATE_ON_SUSPEND;
284     OH_LOG_INFO(LOG_APP, "DISPLAY_MANAGER_DISPLAY_STATE_ON_SUSPEND =%{public}d.",DisplayState);
285     napi_value result;
286     napi_create_uint32(env,DisplayState,&result);
287     return result;
288 }
GetDisplayCapture(napi_env env,napi_callback_info info)289 static napi_value GetDisplayCapture(napi_env env, napi_callback_info info)
290 {
291     size_t argc = 1;
292     napi_value args[1] = {nullptr};
293     napi_get_cb_info(env,info,&argc,args,nullptr,nullptr);
294     napi_valuetype valuetype0;
295     napi_typeof(env,args[0],&valuetype0);
296     int32_t displayId;
297     napi_get_value_int32(env,args[0],&displayId);
298     OH_LOG_INFO(LOG_APP, "get capture displayId=%{public}d.",displayId);
299 
300     OH_PixelmapNative *imagePixelMap = nullptr;
301     napi_value objValue = nullptr;
302     NativeDisplayManager_ErrorCode errCode = OH_NativeDisplayManager_CaptureScreenPixelmap(displayId,&imagePixelMap);
303     OH_LOG_INFO(LOG_APP, "get capture errCode=%{public}d.",errCode);
304     if(errCode == NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK){
305         if(imagePixelMap != nullptr){
306             napi_value pixelValue;
307             OH_PixelmapNative_ConvertPixelmapNativeToNapi(env,imagePixelMap,&pixelValue);
308             OH_PixelmapNative_Release(imagePixelMap);
309             return pixelValue;
310         }
311     }
312     napi_create_uint32(env,errCode,&objValue);
313     return objValue;
314 }
315 
316 EXTERN_C_START
Init(napi_env env,napi_value exports)317 static napi_value Init(napi_env env, napi_value exports)
318 {
319     napi_property_descriptor desc[] = {
320         { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
321         { "getPrimaryDisplay", nullptr, GetPrimaryDisplay, nullptr, nullptr, nullptr, napi_default, nullptr },
322         { "getAllDisplay", nullptr, GetAllDisplay, nullptr, nullptr, nullptr, napi_default, nullptr },
323         { "getDisplayById", nullptr, GetDisplayById, nullptr, nullptr, nullptr, napi_default, nullptr },
324         { "destroyDisplayWithoutCreate", nullptr, DestroyDisplayWithoutCreate, nullptr, nullptr, nullptr, napi_default, nullptr },
325         { "getDisplayState_UNKNOWN", nullptr, GetDisplayState_UNKNOWN, nullptr, nullptr, nullptr, napi_default, nullptr },
326         { "getDisplayState_OFF", nullptr, GetDisplayState_OFF, nullptr, nullptr, nullptr, napi_default, nullptr },
327         { "getDisplayState_ON", nullptr, GetDisplayState_ON, nullptr, nullptr, nullptr, napi_default, nullptr },
328         { "getDisplayState_DOZE_SUSPEND", nullptr, GetDisplayState_DOZE_SUSPEND, nullptr, nullptr, nullptr, napi_default, nullptr },
329         { "getDisplayState_DOZE", nullptr, GetDisplayState_DOZE, nullptr, nullptr, nullptr, napi_default, nullptr },
330         { "getDisplayState_VR", nullptr, GetDisplayState_VR, nullptr, nullptr, nullptr, napi_default, nullptr },
331         { "getDisplayState_ON_SUSPEND", nullptr, GetDisplayState_ON_SUSPEND, nullptr, nullptr, nullptr, napi_default, nullptr },
332         { "getDisplayCapture", nullptr, GetDisplayCapture, nullptr, nullptr, nullptr, napi_default, nullptr },
333     };
334     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
335     return exports;
336 }
337 EXTERN_C_END
338 
339 static napi_module demoModule = {
340     .nm_version = 1,
341     .nm_flags = 0,
342     .nm_filename = nullptr,
343     .nm_register_func = Init,
344     .nm_modname = "entry",
345     .nm_priv = ((void*)0),
346     .reserved = { 0 },
347 };
348 
RegisterEntryModule(void)349 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
350 {
351     napi_module_register(&demoModule);
352 }
353