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 "js_key_event.h"
17 
18 #include "mmi_log.h"
19 #include "napi_constants.h"
20 #include "util_napi.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "JsKeyEvent"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 enum class Action : int32_t {
29     CANCEL = 0,
30     DOWN = 1,
31     UP = 2,
32 };
33 enum VKeyboardAction : int32_t {
34     UNKNOWN = 0,
35     ACTIVATE_KEYBOARD = 1,
36     VKEY_DOWN = 2,
37     VKEY_UP = 3,
38     RESET_BUTTON_COLOR = 4,
39     TWO_FINGERS_IN = 5,
40     TWO_FINGERS_OUT = 6,
41     TWO_HANDS_UP = 7,
42     TWO_HANDS_DOWN = 8,
43 };
44 } // namespace
45 
GetNapiInt32(napi_env env, int32_t code)46 napi_value JsKeyEvent::GetNapiInt32(napi_env env, int32_t code)
47 {
48     CALL_DEBUG_ENTER;
49     napi_value ret = nullptr;
50     CHKRP(napi_create_int32(env, code, &ret), CREATE_INT32);
51     return ret;
52 }
53 
EnumClassConstructor(napi_env env, napi_callback_info info)54 napi_value JsKeyEvent::EnumClassConstructor(napi_env env, napi_callback_info info)
55 {
56     CALL_DEBUG_ENTER;
57     size_t argc = 0;
58     napi_value args[1] = { 0 };
59     napi_value ret = nullptr;
60     void *data = nullptr;
61     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
62     return ret;
63 }
64 
ExportVKeyboardAction(napi_env env, napi_value exports)65 napi_value JsKeyEvent::ExportVKeyboardAction(napi_env env, napi_value exports)
66 {
67     CALL_DEBUG_ENTER;
68     napi_property_descriptor desc[] = {
69         DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN", GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::UNKNOWN))),
70         DECLARE_NAPI_STATIC_PROPERTY("ACTIVATE_KEYBOARD",
71             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::ACTIVATE_KEYBOARD))),
72         DECLARE_NAPI_STATIC_PROPERTY("VKEY_DOWN",
73             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::VKEY_DOWN))),
74         DECLARE_NAPI_STATIC_PROPERTY("VKEY_UP",
75             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::VKEY_UP))),
76         DECLARE_NAPI_STATIC_PROPERTY("RESET_BUTTON_COLOR",
77             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::RESET_BUTTON_COLOR))),
78         DECLARE_NAPI_STATIC_PROPERTY("TWO_FINGERS_IN",
79             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::TWO_FINGERS_IN))),
80         DECLARE_NAPI_STATIC_PROPERTY("TWO_FINGERS_OUT",
81             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::TWO_FINGERS_OUT))),
82         DECLARE_NAPI_STATIC_PROPERTY("TWO_HANDS_UP",
83             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::TWO_HANDS_UP))),
84         DECLARE_NAPI_STATIC_PROPERTY("TWO_HANDS_DOWN",
85             GetNapiInt32(env, static_cast<int32_t>(VKeyboardAction::TWO_HANDS_DOWN))),
86     };
87 
88     napi_value vkAction = nullptr;
89     CHKRP(napi_define_class(env, "VKeyboardAction", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
90         sizeof(desc) / sizeof(*desc), desc, &vkAction), DEFINE_CLASS);
91     CHKRP(napi_set_named_property(env, exports, "VKeyboardAction", vkAction), SET_NAMED_PROPERTY);
92     return exports;
93 }
94 
Export(napi_env env, napi_value exports)95 napi_value JsKeyEvent::Export(napi_env env, napi_value exports)
96 {
97     CALL_DEBUG_ENTER;
98     napi_property_descriptor desc[] = {
99         DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(Action::CANCEL))),
100         DECLARE_NAPI_STATIC_PROPERTY("DOWN", GetNapiInt32(env, static_cast<int32_t>(Action::DOWN))),
101         DECLARE_NAPI_STATIC_PROPERTY("UP", GetNapiInt32(env, static_cast<int32_t>(Action::UP))),
102     };
103 
104     napi_value action = nullptr;
105     CHKRP(napi_define_class(env, "Action", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
106         sizeof(desc) / sizeof(*desc), desc, &action), DEFINE_CLASS);
107     CHKRP(napi_set_named_property(env, exports, "Action", action), SET_NAMED_PROPERTY);
108     CHKPP(ExportVKeyboardAction(env, exports));
109     return exports;
110 }
111 } // namespace MMI
112 } // namespace OHOS