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