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_touch_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 "JsMouseEvent"
24 
25 namespace OHOS {
26 namespace MMI {
GetNapiInt32(napi_env env,int32_t code)27 napi_value JsTouchEvent::GetNapiInt32(napi_env env, int32_t code)
28 {
29     CALL_DEBUG_ENTER;
30     napi_value ret = nullptr;
31     CHKRP(napi_create_int32(env, code, &ret), CREATE_INT32);
32     return ret;
33 }
34 
EnumClassConstructor(napi_env env,napi_callback_info info)35 napi_value JsTouchEvent::EnumClassConstructor(napi_env env, napi_callback_info info)
36 {
37     CALL_DEBUG_ENTER;
38     size_t argc = 0;
39     napi_value args[1] = { 0 };
40     napi_value ret = nullptr;
41     void *data = nullptr;
42     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
43     return ret;
44 }
45 
Export(napi_env env,napi_value exports)46 napi_value JsTouchEvent::Export(napi_env env, napi_value exports)
47 {
48     CALL_DEBUG_ENTER;
49     napi_property_descriptor actionArr[] = {
50         DECLARE_NAPI_STATIC_PROPERTY("CANCEL", GetNapiInt32(env, static_cast<int32_t>(Action::CANCEL))),
51         DECLARE_NAPI_STATIC_PROPERTY("DOWN", GetNapiInt32(env, static_cast<int32_t>(Action::DOWN))),
52         DECLARE_NAPI_STATIC_PROPERTY("MOVE", GetNapiInt32(env, static_cast<int32_t>(Action::MOVE))),
53         DECLARE_NAPI_STATIC_PROPERTY("UP", GetNapiInt32(env, static_cast<int32_t>(Action::UP))),
54         DECLARE_NAPI_STATIC_PROPERTY("PULL_DOWN", GetNapiInt32(env, static_cast<int32_t>(Action::PULL_DOWN))),
55         DECLARE_NAPI_STATIC_PROPERTY("PULL_MOVE", GetNapiInt32(env, static_cast<int32_t>(Action::PULL_MOVE))),
56         DECLARE_NAPI_STATIC_PROPERTY("PULL_UP", GetNapiInt32(env, static_cast<int32_t>(Action::PULL_UP))),
57     };
58     napi_value action = nullptr;
59     CHKRP(napi_define_class(env, "Action", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
60         sizeof(actionArr) / sizeof(*actionArr), actionArr, &action), DEFINE_CLASS);
61     CHKRP(napi_set_named_property(env, exports, "Action", action), SET_NAMED_PROPERTY);
62 
63     napi_property_descriptor toolTypeArr[] = {
64         DECLARE_NAPI_STATIC_PROPERTY("FINGER", GetNapiInt32(env, static_cast<int32_t>(ToolType::FINGER))),
65         DECLARE_NAPI_STATIC_PROPERTY("PEN", GetNapiInt32(env, static_cast<int32_t>(ToolType::PEN))),
66         DECLARE_NAPI_STATIC_PROPERTY("RUBBER", GetNapiInt32(env, static_cast<int32_t>(ToolType::RUBBER))),
67         DECLARE_NAPI_STATIC_PROPERTY("BRUSH", GetNapiInt32(env, static_cast<int32_t>(ToolType::BRUSH))),
68         DECLARE_NAPI_STATIC_PROPERTY("PENCIL", GetNapiInt32(env, static_cast<int32_t>(ToolType::PENCIL))),
69         DECLARE_NAPI_STATIC_PROPERTY("AIRBRUSH", GetNapiInt32(env, static_cast<int32_t>(ToolType::AIRBRUSH))),
70         DECLARE_NAPI_STATIC_PROPERTY("MOUSE", GetNapiInt32(env, static_cast<int32_t>(ToolType::MOUSE))),
71         DECLARE_NAPI_STATIC_PROPERTY("LENS", GetNapiInt32(env, static_cast<int32_t>(ToolType::LENS))),
72     };
73     napi_value toolType = nullptr;
74     CHKRP(napi_define_class(env, "ToolType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
75         sizeof(toolTypeArr) / sizeof(*toolTypeArr), toolTypeArr, &toolType), DEFINE_CLASS);
76     CHKRP(napi_set_named_property(env, exports, "ToolType", toolType), SET_NAMED_PROPERTY);
77 
78     napi_property_descriptor sourceTypeArr[] = {
79         DECLARE_NAPI_STATIC_PROPERTY("TOUCH_SCREEN", GetNapiInt32(env, static_cast<int32_t>(SourceType::TOUCH_SCREEN))),
80         DECLARE_NAPI_STATIC_PROPERTY("PEN", GetNapiInt32(env, static_cast<int32_t>(SourceType::PEN))),
81         DECLARE_NAPI_STATIC_PROPERTY("TOUCH_PAD", GetNapiInt32(env, static_cast<int32_t>(SourceType::TOUCH_PAD))),
82     };
83     napi_value sourceType = nullptr;
84     CHKRP(napi_define_class(env, "SourceType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
85         sizeof(sourceTypeArr) / sizeof(*sourceTypeArr), sourceTypeArr, &sourceType), DEFINE_CLASS);
86     CHKRP(napi_set_named_property(env, exports, "SourceType", sourceType), SET_NAMED_PROPERTY);
87     return exports;
88 }
89 } // namespace MMI
90 } // namespace OHOS