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 "window_manager/oh_window_comm.h"
17#include "window_manager/oh_window_event_filter.h"
18#include "multimodalinput/oh_input_manager.h"
19#include "multimodalinput/oh_key_code.h"
20#include <bits/alltypes.h>
21#include <hilog/log.h>
22
23static int keyCode1 = -1;
24static bool filterESC(Input_KeyEvent *event){
25    auto keyCode = OH_Input_GetKeyEventKeyCode(event);
26    auto actionTime = OH_Input_GetKeyEventActionTime(event);
27    auto action = OH_Input_GetKeyEventAction(event);
28    return keyCode == keyCode1;
29}
30
31static napi_value filterKeyCode(napi_env env, napi_callback_info info){
32    size_t argc = 2;
33    napi_value args[2] = {nullptr};
34    napi_get_cb_info(env, info, &argc,  args,nullptr, nullptr);
35
36    napi_valuetype valuetype0;
37    napi_typeof(env, args[0], &valuetype0);
38
39    int32_t windowId;
40    napi_get_value_int32(env, args[0], &windowId);
41    int32_t keyCode;
42    napi_get_value_int32(env, args[1], &keyCode);
43    keyCode1 = keyCode;
44    auto result = OH_NativeWindowManager_RegisterKeyEventFilter(windowId, filterESC);
45    napi_value res;
46    napi_create_int32(env, result, &res);
47    return res;
48}
49static napi_value unFilterKeyCode(napi_env env, napi_callback_info info) {
50    size_t argc = 1;
51    napi_value args[1] = {nullptr};
52    napi_get_cb_info(env, info, &argc,  args,nullptr,nullptr);
53    napi_valuetype valuetype0;
54    napi_typeof(env, args[0], &valuetype0);
55
56    int32_t windowId;
57    napi_get_value_int32(env, args[0], &windowId);
58
59    auto result = OH_NativeWindowManager_UnregisterKeyEventFilter(windowId);
60    napi_value res;
61    napi_create_int32(env, result, &res);
62    return res;
63}
64
65
66
67
68EXTERN_C_START
69static napi_value Init(napi_env env, napi_value exports)
70{
71    napi_property_descriptor desc[] = {
72        {"filterKeyCode", nullptr, filterKeyCode, nullptr, nullptr, nullptr, napi_default, nullptr},
73        {"unFilterKeyCode", nullptr, unFilterKeyCode, nullptr, nullptr, nullptr, napi_default, nullptr}
74    };
75    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
76    return exports;
77}
78EXTERN_C_END
79
80static napi_module demoModule = {
81    .nm_version = 1,
82    .nm_flags = 0,
83    .nm_filename = nullptr,
84    .nm_register_func = Init,
85    .nm_modname = "entry",
86    .nm_priv = ((void*)0),
87    .reserved = { 0 },
88};
89
90extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
91{
92    napi_module_register(&demoModule);
93}
94