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 <cstdio>
16 #include "napi/native_api.h"
17 #include "window_manager/oh_window_comm.h"
18 #include "window_manager/oh_window.h"
19 #include "window_manager/oh_window_event_filter.h"
20 #include "multimodalinput/oh_input_manager.h"
21 #include "multimodalinput/oh_key_code.h"
22 #include <bits/alltypes.h>
23 #include <hilog/log.h>
24 #include <string>
25 #include <type_traits>
26 #include "multimedia/image_framework/image/pixelmap_native.h"
27 #include "multimedia/image_framework/image/image_packer_native.h"
28 #include "native_drawing/drawing_pixel_map.h"
29 #include <cstdint>
30 #include <memory>
31 #include <unistd.h>
32 #include <future>
33 #define Check_Napi_Create_Object_Return_If_Null(env,objValue)\
34 do{ \
35 napi_create_object((env),&(objValue)); \
36 if((objValue) == nullptr){ \
37 return nullptr; \
38 } \
39 }while (0)
40
41 static int keyCode1 = -1;
filterESC(Input_KeyEvent * event)42 static bool filterESC(Input_KeyEvent *event){
43 auto keyCode = OH_Input_GetKeyEventKeyCode(event);
44 auto actionTime = OH_Input_GetKeyEventActionTime(event);
45 auto action = OH_Input_GetKeyEventAction(event);
46 return keyCode == keyCode1;
47 }
48
filterKeyCode(napi_env env,napi_callback_info info)49 static napi_value filterKeyCode(napi_env env, napi_callback_info info){
50 size_t argc = 2;
51 napi_value args[2] = {nullptr};
52 napi_get_cb_info(env, info, &argc, args,nullptr, nullptr);
53
54 napi_valuetype valuetype0;
55 napi_typeof(env, args[0], &valuetype0);
56
57 int32_t windowId;
58 napi_get_value_int32(env, args[0], &windowId);
59 int32_t keyCode;
60 napi_get_value_int32(env, args[1], &keyCode);
61 keyCode1 = keyCode;
62 auto result = OH_NativeWindowManager_RegisterKeyEventFilter(windowId, filterESC);
63 napi_value res;
64 napi_create_int32(env, result, &res);
65 return res;
66 }
unFilterKeyCode(napi_env env,napi_callback_info info)67 static napi_value unFilterKeyCode(napi_env env, napi_callback_info info) {
68 size_t argc = 1;
69 napi_value args[1] = {nullptr};
70 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
71 napi_valuetype valuetype0;
72 napi_typeof(env, args[0], &valuetype0);
73
74 int32_t windowId;
75 napi_get_value_int32(env, args[0], &windowId);
76
77 auto result = OH_NativeWindowManager_UnregisterKeyEventFilter(windowId);
78 napi_value res;
79 napi_create_int32(env, result, &res);
80 return res;
81 }
82
IsWindowShown(napi_env env,napi_callback_info info)83 static napi_value IsWindowShown(napi_env env, napi_callback_info info) {
84 size_t argc = 1;
85 napi_value args[1] = {nullptr};
86 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
87 napi_valuetype valuetype0;
88 napi_typeof(env, args[0], &valuetype0);
89
90 int32_t windowId;
91 napi_get_value_int32(env, args[0], &windowId);
92
93 napi_value retValue;
94 napi_create_object(env,&retValue);
95 napi_value resultCode;
96 napi_value isShowWindow;
97
98 bool isShow = false;
99 int32_t resultInt32 = OH_WindowManager_IsWindowShown(static_cast<int>(windowId),&isShow);
100 napi_create_int32(env,resultInt32,&resultCode);
101 napi_create_int32(env,(int)isShow,&isShowWindow);
102 napi_set_named_property(env,retValue,"isShow",isShowWindow);
103 napi_set_named_property(env,retValue,"resultCode",resultCode);
104 return retValue;
105 }
106
ShowWindow(napi_env env,napi_callback_info info)107 static napi_value ShowWindow(napi_env env, napi_callback_info info) {
108 size_t argc = 1;
109 napi_value args[1] = {nullptr};
110 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
111 napi_valuetype valuetype0;
112 napi_typeof(env, args[0], &valuetype0);
113
114 int32_t windowId;
115 napi_get_value_int32(env, args[0], &windowId);
116
117 napi_value retValue;
118 napi_create_object(env,&retValue);
119 napi_value resultCode;
120
121 int32_t resultInt32 = OH_WindowManager_ShowWindow(static_cast<int>(windowId));
122 napi_create_int32(env,resultInt32,&resultCode);
123 napi_set_named_property(env,retValue,"resultCode",resultCode);
124 return retValue;
125 }
126
SetWindowStatusBarEnabled(napi_env env,napi_callback_info info)127 static napi_value SetWindowStatusBarEnabled(napi_env env, napi_callback_info info) {
128 size_t argc = 4;
129 napi_value args[4] = {nullptr};
130 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
131
132 int32_t windowId;
133 napi_get_value_int32(env, args[0], &windowId);
134
135 bool enabled = false;
136 napi_get_value_bool(env, args[1], &enabled);
137
138 bool enableAnimation = false;
139 napi_get_value_bool(env, args[2], &enableAnimation);
140
141 napi_value resultCode;
142
143 auto res = OH_WindowManager_SetWindowStatusBarEnabled(windowId,enabled,enableAnimation);
144 napi_create_int32(env,res,&resultCode);
145 return resultCode;
146 }
147
SetWindowNavigationBarEnabled(napi_env env,napi_callback_info info)148 static napi_value SetWindowNavigationBarEnabled(napi_env env, napi_callback_info info) {
149 size_t argc = 4;
150 napi_value args[4] = {nullptr};
151 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
152
153 int32_t windowId;
154 napi_get_value_int32(env, args[0], &windowId);
155
156 bool enabled = false;
157 napi_get_value_bool(env, args[1], &enabled);
158
159 bool enableAnimation = false;
160 napi_get_value_bool(env, args[2], &enableAnimation);
161
162 napi_value resultCode;
163
164 auto res = OH_WindowManager_SetWindowNavigationBarEnabled(windowId,enabled,enableAnimation);
165 napi_create_int32(env,res,&resultCode);
166 return resultCode;
167 }
168
SetWindowStatusBarColor(napi_env env,napi_callback_info info)169 static napi_value SetWindowStatusBarColor(napi_env env, napi_callback_info info) {
170 size_t argc = 4;
171 napi_value args[4] = {nullptr};
172 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
173
174 int32_t windowId;
175 napi_get_value_int32(env, args[0], &windowId);
176
177 int32_t color;
178 napi_get_value_int32(env, args[1], &color);
179
180 napi_value resultCode;
181
182 auto res = OH_WindowManager_SetWindowStatusBarColor(windowId,color);
183 napi_create_int32(env,res,&resultCode);
184 return resultCode;
185 }
186
CreateJsNumber(napi_env env,double value)187 inline napi_value CreateJsNumber(napi_env env,double value){
188 napi_value result = nullptr;
189 napi_create_double(env,value,&result);
190 return result;
191 }
192
193 template<class T>
ConvertFromJsValue(napi_env env,napi_value jsValue,T & value)194 bool ConvertFromJsValue(napi_env env,napi_value jsValue,T& value){
195 if(jsValue == nullptr){
196 return false;
197 }
198
199 using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
200 napi_value result = nullptr;
201 if constexpr (std::is_same_v<ValueType,bool>){
202 return napi_get_value_bool(env,jsValue,nullptr) == napi_ok;
203 }else if constexpr(std::is_arithmetic_v<ValueType>){
204 return ConvertFromJsNumber(env,jsValue,value);
205 }else if constexpr(std::is_same_v<ValueType,std::string>){
206 size_t len = 0;
207 if(napi_get_value_string_utf8(env,jsValue,nullptr,0,&len)!=napi_ok){
208 return false;
209 }
210 auto buffer = std::make_unique<char[]>(len + 1);
211 size_t strLength = 0;
212 if(napi_get_value_string_utf8(env,jsValue,buffer.get(),len+1,&strLength)==napi_ok){
213 value = buffer.get();
214 return true;
215 }
216 return false;
217 }else if constexpr(std::is_enum_v<ValueType>){
218 std::make_signed_t<ValueType> numberValue = 0;
219 if(!ConvertFromJsNumber(env,jsValue,numberValue)){
220 return true;
221 }
222 value = static_cast<ValueType>(numberValue);
223 return true;
224 }
225 return false;
226 }
227
228 template<class T>
CreateJsValue(napi_env env,const T & value)229 napi_value CreateJsValue(napi_env env,const T& value){
230 using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
231 napi_value result = nullptr;
232 if constexpr (std::is_same_v<ValueType,bool>){
233 napi_get_boolean(env,value,&result);
234 return result;
235 }else if constexpr(std::is_arithmetic_v<ValueType>){
236 return CreateJsNumber(env,value);
237 }else if constexpr(std::is_same_v<ValueType,std::string>){
238 napi_create_string_utf8(env,value.c_str(),value.length(),&result);
239 return result;
240 }else if constexpr(std::is_enum_v<ValueType>){
241 return CreateJsNumber(env,static_cast<std::make_signed_t<ValueType>>(value));
242 }else if constexpr(std::is_same_v<ValueType,const char*>){
243 (value != nullptr)?napi_create_string_utf8(env,value,strlen(value),&result):
244 napi_get_undefined(env,&result);
245 return result;
246 }else {
247 return result;
248 }
249 }
250
GetRectAndConvertToJsValue(napi_env env,const WindowManager_Rect & rect)251 napi_value GetRectAndConvertToJsValue(napi_env env, const WindowManager_Rect& rect){
252 napi_value objValue = nullptr;
253 Check_Napi_Create_Object_Return_If_Null(env,objValue);
254
255 napi_set_named_property(env,objValue,"left",CreateJsValue(env,rect.posX));
256 napi_set_named_property(env,objValue,"top",CreateJsValue(env,rect.posY));
257 napi_set_named_property(env,objValue,"width",CreateJsValue(env,rect.width));
258 napi_set_named_property(env,objValue,"height",CreateJsValue(env,rect.height));
259 return objValue;
260 }
261
ConvertAvoidAreaToJsValue(napi_env env,WindowManager_AvoidArea * avoidArea)262 napi_value ConvertAvoidAreaToJsValue(napi_env env, WindowManager_AvoidArea* avoidArea){
263 napi_value objValue = nullptr;
264 Check_Napi_Create_Object_Return_If_Null(env,objValue);
265
266 napi_set_named_property(env,objValue,"leftRect",GetRectAndConvertToJsValue(env,avoidArea->leftRect));
267 napi_set_named_property(env,objValue,"topRect",GetRectAndConvertToJsValue(env,avoidArea->topRect));
268 napi_set_named_property(env,objValue,"rightRect",GetRectAndConvertToJsValue(env,avoidArea->rightRect));
269 napi_set_named_property(env,objValue,"bottomRect",GetRectAndConvertToJsValue(env,avoidArea->bottomRect));
270 return objValue;
271 }
272
GetAvoidAreaByType(napi_env env,napi_callback_info info)273 static napi_value GetAvoidAreaByType(napi_env env, napi_callback_info info) {
274 size_t argc = 2;
275 napi_value args[2] = {nullptr};
276 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
277
278 int32_t windowId;
279 napi_get_value_int32(env, args[0], &windowId);
280
281 int32_t resultValue = 0;
282 napi_get_value_int32(env, args[1], &resultValue);
283
284 WindowManager_AvoidAreaType avoidAreaType = static_cast<WindowManager_AvoidAreaType>(resultValue);
285 WindowManager_AvoidArea* avoidArea = new WindowManager_AvoidArea();
286
287 auto res = OH_WindowManager_GetWindowAvoidArea(windowId,avoidAreaType,avoidArea);
288 if(res == 0){
289 napi_value avoidAreaObj = ConvertAvoidAreaToJsValue(env,avoidArea);
290 return avoidAreaObj;
291 }else{
292 napi_value resultValue;
293 napi_create_int32(env,res,&resultValue);
294 return resultValue;
295 }
296
297 }
298
SetBackgroundColor(napi_env env,napi_callback_info info)299 static napi_value SetBackgroundColor(napi_env env, napi_callback_info info) {
300 size_t argc = 4;
301 napi_value args[4] = {nullptr};
302 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
303
304 int32_t windowId;
305 napi_get_value_int32(env, args[0], &windowId);
306
307 std::string color;
308 ConvertFromJsValue(env,args[1],color);
309
310 auto res = OH_WindowManager_SetWindowBackgroundColor(windowId,color.c_str());
311 napi_value resultValue;
312 napi_create_int32(env,res,&resultValue);
313 return resultValue;
314 }
315
SetBrightness(napi_env env,napi_callback_info info)316 static napi_value SetBrightness(napi_env env, napi_callback_info info) {
317 size_t argc = 4;
318 napi_value args[4] = {nullptr};
319 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
320
321 int32_t windowId;
322 napi_get_value_int32(env, args[0], &windowId);
323
324 double brightness;
325 napi_get_value_double(env,args[1],&brightness);
326
327 auto res = OH_WindowManager_SetWindowBrightness(windowId,brightness);
328 napi_value resultValue;
329 napi_create_int32(env,res,&resultValue);
330 return resultValue;
331 }
332
SetKeepScreenOn(napi_env env,napi_callback_info info)333 static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info) {
334 size_t argc = 4;
335 napi_value args[4] = {nullptr};
336 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
337
338 int32_t windowId;
339 napi_get_value_int32(env, args[0], &windowId);
340
341 bool isKeepScreenOn;
342 napi_get_value_bool(env,args[1],&isKeepScreenOn);
343
344 auto res = OH_WindowManager_SetWindowKeepScreenOn(windowId,isKeepScreenOn);
345 napi_value resultValue;
346 napi_create_int32(env,res,&resultValue);
347 return resultValue;
348 }
349
SetWindowPrivacyMode(napi_env env,napi_callback_info info)350 static napi_value SetWindowPrivacyMode(napi_env env, napi_callback_info info) {
351 size_t argc = 4;
352 napi_value args[4] = {nullptr};
353 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
354
355 int32_t windowId;
356 napi_get_value_int32(env, args[0], &windowId);
357
358 bool isPrivacy;
359 napi_get_value_bool(env,args[1],&isPrivacy);
360
361 auto res = OH_WindowManager_SetWindowPrivacyMode(windowId,isPrivacy);
362 napi_value resultValue;
363 napi_create_int32(env,res,&resultValue);
364 return resultValue;
365 }
366
Snapshot(napi_env env,napi_callback_info info)367 static napi_value Snapshot(napi_env env, napi_callback_info info) {
368 size_t argc = 1;
369 napi_value args[1] = {nullptr};
370 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
371
372 int32_t windowId;
373 napi_get_value_int32(env, args[0], &windowId);
374 OH_PixelmapNative* pixelMap;
375
376 OH_Pixelmap_InitializationOptions* createOpts;
377 OH_PixelmapInitializationOptions_Create(&createOpts);
378 OH_PixelmapInitializationOptions_SetWidth(createOpts,6);
379 OH_PixelmapInitializationOptions_SetHeight(createOpts,4);
380 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts,PIXEL_FORMAT_RGBA_8888);
381 OH_PixelmapInitializationOptions_SetAlphaType(createOpts,PIXELMAP_ALPHA_TYPE_UNKNOWN);
382
383 auto d = OH_PixelmapNative_CreateEmptyPixelmap(createOpts,&pixelMap);
384 OH_LOG_INFO(LOG_APP,"OH_PixelmapNative_CreateEmptyPixelmap pixelMap %{public}d",pixelMap);
385
386 auto res = OH_WindowManager_Snapshot(windowId,pixelMap);
387 OH_LOG_INFO(LOG_APP,"OH_WindowManager_Snapshot res=%{public}d.",res);
388 if(res == 0){
389 napi_value pixelValue;
390 Image_ErrorCode ans = OH_PixelmapNative_ConvertPixelmapNativeToNapi(env,pixelMap,&pixelValue);
391 OH_PixelmapNative_Release(pixelMap);
392 return pixelValue;
393
394 }else{
395 napi_value resultValue;
396 napi_create_int32(env,res,&resultValue);
397 return resultValue;
398 }
399 }
400
SetWindowFocusable(napi_env env,napi_callback_info info)401 static napi_value SetWindowFocusable(napi_env env, napi_callback_info info) {
402 size_t argc = 4;
403 napi_value args[4] = {nullptr};
404 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
405
406 int32_t windowId;
407 napi_get_value_int32(env, args[0], &windowId);
408
409 bool isFocusable;
410 napi_get_value_bool(env,args[1],&isFocusable);
411
412 auto res = OH_WindowManager_SetWindowFocusable(windowId,isFocusable);
413 napi_value resultValue;
414 napi_create_int32(env,res,&resultValue);
415 return resultValue;
416 }
417
SetWindowTouchable(napi_env env,napi_callback_info info)418 static napi_value SetWindowTouchable(napi_env env, napi_callback_info info) {
419 size_t argc = 4;
420 napi_value args[4] = {nullptr};
421 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
422
423 int32_t windowId;
424 napi_get_value_int32(env, args[0], &windowId);
425
426 bool isTouchable;
427 napi_get_value_bool(env,args[1],&isTouchable);
428
429 auto res = OH_WindowManager_SetWindowTouchable(windowId,isTouchable);
430 napi_value resultValue;
431 napi_create_int32(env,res,&resultValue);
432 return resultValue;
433 }
434
CreateJsWindowPropertiesObject(napi_env env,WindowManager_WindowProperties * windowProperties)435 napi_value CreateJsWindowPropertiesObject(napi_env env, WindowManager_WindowProperties* windowProperties){
436 napi_value objValue = nullptr;
437 Check_Napi_Create_Object_Return_If_Null(env,objValue);
438
439 napi_value windowRectObj = GetRectAndConvertToJsValue(env,windowProperties->windowRect);
440 napi_set_named_property(env,objValue,"windowRect",windowRectObj);
441 napi_value drawableRectObj = GetRectAndConvertToJsValue(env,windowProperties->drawableRect);
442 napi_set_named_property(env,objValue,"drawableRect",drawableRectObj);
443 napi_set_named_property(env,objValue,"type",CreateJsValue(env,windowProperties->type));
444 napi_set_named_property(env,objValue,"isLayoutFullScreen",CreateJsValue(env,windowProperties->isLayoutFullScreen));
445 napi_set_named_property(env,objValue,"isFullScreen",CreateJsValue(env,windowProperties->isFullScreen));
446 napi_set_named_property(env,objValue,"touchable",CreateJsValue(env,windowProperties->touchable));
447 napi_set_named_property(env,objValue,"focusable",CreateJsValue(env,windowProperties->focusable));
448 napi_set_named_property(env,objValue,"isPrivacyMode",CreateJsValue(env,windowProperties->isPrivacyMode));
449 napi_set_named_property(env,objValue,"isKeepScreenOn",CreateJsValue(env,windowProperties->isKeepScreenOn));
450 napi_set_named_property(env,objValue,"brightness",CreateJsValue(env,windowProperties->brightness));
451 napi_set_named_property(env,objValue,"isTransparent",CreateJsValue(env,windowProperties->isTransparent));
452 napi_set_named_property(env,objValue,"id",CreateJsValue(env,windowProperties->id));
453 napi_set_named_property(env,objValue,"displayId",CreateJsValue(env,static_cast<int64_t>(windowProperties->displayId)));
454 return objValue;
455 }
456
GetWindowPorperties(napi_env env,napi_callback_info info)457 static napi_value GetWindowPorperties(napi_env env, napi_callback_info info) {
458 size_t argc = 4;
459 napi_value args[4] = {nullptr};
460 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
461
462 int32_t windowId;
463 napi_get_value_int32(env, args[0], &windowId);
464 WindowManager_WindowProperties* windowProperties = new WindowManager_WindowProperties();
465
466 auto res = OH_WindowManager_GetWindowProperties(windowId,windowProperties);
467 if(res == 0){
468 auto objValue = CreateJsWindowPropertiesObject(env,windowProperties);
469 return objValue;
470 }else{
471 napi_value resultValue;
472 napi_create_int32(env,res,&resultValue);
473 return resultValue;
474 }
475 }
476
477 //设置鼠标过滤函数
filterMouseEvent(Input_MouseEvent * mouseEvent)478 static bool filterMouseEvent(Input_MouseEvent* mouseEvent){
479 int32_t action = OH_Input_GetMouseEventAction(mouseEvent);
480 int32_t displayX = OH_Input_GetMouseEventDisplayX(mouseEvent);
481 int32_t displayY = OH_Input_GetMouseEventDisplayY(mouseEvent);
482 int32_t mouseButton = OH_Input_GetMouseEventButton(mouseEvent);
483 int32_t actionTime = OH_Input_GetMouseEventActionTime(mouseEvent);
484 static bool flag = false;
485 OH_LOG_INFO(LogType::LOG_APP,"testTag filterMouseEvent in action=%{public}d displayX=%{public}d "
486 "displayY=%{public}d mouseButton=%{public}d actionTime=%{public}ld ",action,displayX,displayY,mouseButton,actionTime);
487 //过滤鼠标右键按下
488 if(mouseButton == Input_MouseEventButton::MOUSE_BUTTON_RIGHT){
489 flag = true;
490
491 }else{
492 flag = false;
493 }
494 return flag;
495 }
496
registerMouseFilter(napi_env env,napi_callback_info info)497 static napi_value registerMouseFilter(napi_env env, napi_callback_info info){
498 size_t argc = 1;
499 napi_value args[1] = {nullptr};
500 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
501
502 int32_t windowId;
503 napi_get_value_int32(env, args[0], &windowId);
504
505 auto res = OH_NativeWindowManager_RegisterMouseEventFilter(windowId,filterMouseEvent);
506 napi_value resultValue;
507 napi_create_int32(env,res,&resultValue);
508 return resultValue;
509 }
510
clearMouseFilter(napi_env env,napi_callback_info info)511 static napi_value clearMouseFilter(napi_env env, napi_callback_info info){
512 size_t argc = 1;
513 napi_value args[1] = {nullptr};
514 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
515
516 int32_t windowId;
517 napi_get_value_int32(env, args[0], &windowId);
518
519 auto res = OH_NativeWindowManager_UnregisterMouseEventFilter(windowId);
520 napi_value resultValue;
521 napi_create_int32(env,res,&resultValue);
522 return resultValue;
523 }
524
525 //设置触摸过滤函数
filterTouchEvent(Input_TouchEvent * touchEvent)526 static bool filterTouchEvent(Input_TouchEvent* touchEvent){
527 int32_t action = OH_Input_GetTouchEventAction(touchEvent);
528 int32_t displayX = OH_Input_GetTouchEventDisplayX(touchEvent);
529 int32_t displayY = OH_Input_GetTouchEventDisplayY(touchEvent);
530 int32_t id = OH_Input_GetTouchEventFingerId(touchEvent);
531 int32_t actionTime = OH_Input_GetTouchEventActionTime(touchEvent);
532 static bool flag = false;
533 OH_LOG_INFO(LogType::LOG_APP,"testTag filterTouchEvent in action=%{public}d displayX=%{public}d "
534 "displayY=%{public}d mouseButton=%{public}d actionTime=%{public}ld ",action,displayX,displayY,id,actionTime);
535 //过滤触摸移动事件
536 if(action == Input_TouchEventAction::TOUCH_ACTION_MOVE){
537 flag = true;
538
539 }else{
540 flag = false;
541 }
542 return flag;
543 }
544
registerTouchFilter(napi_env env,napi_callback_info info)545 static napi_value registerTouchFilter(napi_env env, napi_callback_info info){
546 size_t argc = 1;
547 napi_value args[1] = {nullptr};
548 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
549
550 int32_t windowId;
551 napi_get_value_int32(env, args[0], &windowId);
552
553 auto res = OH_NativeWindowManager_RegisterTouchEventFilter(windowId,filterTouchEvent);
554 napi_value resultValue;
555 napi_create_int32(env,res,&resultValue);
556 return resultValue;
557 }
558
clearTouchFilter(napi_env env,napi_callback_info info)559 static napi_value clearTouchFilter(napi_env env, napi_callback_info info){
560 size_t argc = 1;
561 napi_value args[1] = {nullptr};
562 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
563
564 int32_t windowId;
565 napi_get_value_int32(env, args[0], &windowId);
566
567 auto res = OH_NativeWindowManager_UnregisterTouchEventFilter(windowId);
568 napi_value resultValue;
569 napi_create_int32(env,res,&resultValue);
570 return resultValue;
571 }
572
573
CreateJsWindowLayoutInfoObject(napi_env env,const WindowManager_Rect & rect)574 napi_value CreateJsWindowLayoutInfoObject(napi_env env,const WindowManager_Rect& rect){
575 napi_value objValue = nullptr;
576 Check_Napi_Create_Object_Return_If_Null(env,objValue);
577 napi_set_named_property(env,objValue,"rect",GetRectAndConvertToJsValue(env,rect));
578 return objValue;
579 }
580
getAllWIndowLayOutInfo(napi_env env,napi_callback_info info)581 static napi_value getAllWIndowLayOutInfo(napi_env env, napi_callback_info info){
582 size_t argc = 4;
583 napi_value args[4] = {nullptr};
584 napi_get_cb_info(env, info, &argc, args,nullptr,nullptr);
585
586 int32_t displayId;
587 napi_get_value_int32(env, args[0], &displayId);
588 WindowManager_Rect** windowLayoutInfo = (WindowManager_Rect**)malloc(sizeof(WindowManager_Rect));
589 size_t* windowLayoutInfoSize = (size_t*)malloc(sizeof(size_t));
590 auto res = OH_WindowManager_GetAllWindowLayoutInfoList(displayId,windowLayoutInfo,windowLayoutInfoSize);
591
592 if(res != 0){
593 napi_value resultValue;
594 napi_create_int32(env,res,&resultValue);
595 return resultValue;
596 }else{
597 napi_value arrayValue = nullptr;
598 napi_create_array_with_length(env,*windowLayoutInfoSize,&arrayValue);
599 for(size_t i = 0;i < *windowLayoutInfoSize;i++){
600 OH_LOG_INFO(LOG_APP,"*windowLayoutInfoSize : %{public}d",*windowLayoutInfoSize);
601 napi_set_element(env,arrayValue,i,CreateJsWindowLayoutInfoObject(env,*(windowLayoutInfo[0]+i)));
602 }
603 OH_WindowManager_ReleaseAllWindowLayoutInfoList(*windowLayoutInfo);
604 *windowLayoutInfo = NULL;
605 windowLayoutInfo = NULL;
606 return arrayValue;
607 }
608
609 }
610
611 EXTERN_C_START
Init(napi_env env,napi_value exports)612 static napi_value Init(napi_env env, napi_value exports)
613 {
614 napi_property_descriptor desc[] = {
615 {"filterKeyCode", nullptr, filterKeyCode, nullptr, nullptr, nullptr, napi_default, nullptr},
616 {"unFilterKeyCode", nullptr, unFilterKeyCode, nullptr, nullptr, nullptr, napi_default, nullptr},
617 {"isWindowShown", nullptr, IsWindowShown, nullptr, nullptr, nullptr, napi_default, nullptr},
618 {"showWindow", nullptr, ShowWindow, nullptr, nullptr, nullptr, napi_default, nullptr},
619 {"setWindowStatusBarEnabled", nullptr, SetWindowStatusBarEnabled, nullptr, nullptr, nullptr, napi_default, nullptr},
620 {"setWindowNavigationBarEnabled", nullptr, SetWindowNavigationBarEnabled, nullptr, nullptr, nullptr, napi_default, nullptr},
621 {"setWindowStatusBarColor", nullptr, SetWindowStatusBarColor, nullptr, nullptr, nullptr, napi_default, nullptr},
622 {"getAvoidAreaByType", nullptr, GetAvoidAreaByType, nullptr, nullptr, nullptr, napi_default, nullptr},
623 {"setBackgroundColor", nullptr, SetBackgroundColor, nullptr, nullptr, nullptr, napi_default, nullptr},
624 {"setBrightness", nullptr, SetBrightness, nullptr, nullptr, nullptr, napi_default, nullptr},
625 {"setKeepScreenOn", nullptr, SetKeepScreenOn, nullptr, nullptr, nullptr, napi_default, nullptr},
626 {"setWindowPrivacyMode", nullptr, SetWindowPrivacyMode, nullptr, nullptr, nullptr, napi_default, nullptr},
627 {"snapshot", nullptr, Snapshot, nullptr, nullptr, nullptr, napi_default, nullptr},
628 {"setWindowFocusable", nullptr, SetWindowFocusable, nullptr, nullptr, nullptr, napi_default, nullptr},
629 {"setWindowTouchable", nullptr, SetWindowTouchable, nullptr, nullptr, nullptr, napi_default, nullptr},
630 {"getWindowPorperties", nullptr, GetWindowPorperties, nullptr, nullptr, nullptr, napi_default, nullptr},
631 {"registerMouseFilter", nullptr, registerMouseFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
632 {"clearMouseFilter", nullptr, clearMouseFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
633 {"registerTouchFilter", nullptr, registerTouchFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
634 {"clearTouchFilter", nullptr, clearTouchFilter, nullptr, nullptr, nullptr, napi_default, nullptr},
635 {"getAllWIndowLayOutInfo", nullptr, getAllWIndowLayOutInfo, nullptr, nullptr, nullptr, napi_default, nullptr}
636 };
637 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
638 return exports;
639 }
640 EXTERN_C_END
641
642 static napi_module demoModule = {
643 .nm_version = 1,
644 .nm_flags = 0,
645 .nm_filename = nullptr,
646 .nm_register_func = Init,
647 .nm_modname = "entry",
648 .nm_priv = ((void*)0),
649 .reserved = { 0 },
650 };
651
RegisterEntryModule(void)652 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
653 {
654 napi_module_register(&demoModule);
655 }
656