• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_input_monitor_module.h"
17 
18 #include <cinttypes>
19 #include <string>
20 #include <uv.h>
21 
22 #include "define_multimodal.h"
23 #include "napi_constants.h"
24 #include "js_input_monitor_manager.h"
25 #include "proto.h"
26 #include "util_napi_error.h"
27 
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsInputMonitorModule" };
32 } // namespace
33 
JsOnApi9(napi_env env,napi_callback_info info)34 static napi_value JsOnApi9(napi_env env, napi_callback_info info)
35 {
36     CALL_DEBUG_ENTER;
37     size_t argc = 2;
38     napi_value argv[2];
39 
40     CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
41     napi_valuetype valueType = napi_undefined;
42     CHKRP(env, napi_typeof(env, argv[0], &valueType), TYPEOF);
43     if (valueType != napi_string) {
44         MMI_HILOGE("First Parameter type error");
45         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "EventType", "string");
46         return nullptr;
47     }
48     char typeName[MAX_STRING_LEN] = { 0 };
49     size_t len = 0;
50     CHKRP(env, napi_get_value_string_utf8(env, argv[0], typeName, MAX_STRING_LEN - 1, &len), GET_STRING_UTF8);
51     if (std::strcmp(typeName, "touch") != 0 && std::strcmp(typeName, "mouse") != 0) {
52         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "EventType must be mouse or touch");
53         return nullptr;
54     }
55     CHKRP(env, napi_typeof(env, argv[1], &valueType), TYPEOF);
56     if (valueType != napi_function) {
57         MMI_HILOGE("Second Parameter type error");
58         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Second Parameter type error");
59         return nullptr;
60     }
61     if (!JsInputMonMgr.AddEnv(env, info)) {
62         MMI_HILOGE("AddEnv failed");
63         return nullptr;
64     }
65     JsInputMonMgr.AddMonitor(env, typeName, argv[1]);
66     return nullptr;
67 }
68 
JsOn(napi_env env,napi_callback_info info)69 static napi_value JsOn(napi_env env, napi_callback_info info)
70 {
71     CALL_DEBUG_ENTER;
72     size_t argc = 2;
73     napi_value argv[2];
74     CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
75     if (argc < 2) {
76         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error");
77         return nullptr;
78     }
79     JsOnApi9(env, info);
80     return nullptr;
81 }
82 
JsOffApi9(napi_env env,napi_callback_info info)83 static napi_value JsOffApi9(napi_env env, napi_callback_info info)
84 {
85     CALL_DEBUG_ENTER;
86     size_t argc = 2;
87     napi_value argv[2];
88     CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
89     napi_valuetype valueType = napi_undefined;
90     CHKRP(env, napi_typeof(env, argv[0], &valueType), TYPEOF);
91     if (valueType != napi_string) {
92         MMI_HILOGE("First Parameter type error");
93         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "EventType", "string");
94         return nullptr;
95     }
96     char typeName[MAX_STRING_LEN] = { 0 };
97     size_t len = 0;
98     CHKRP(env, napi_get_value_string_utf8(env, argv[0], typeName, MAX_STRING_LEN - 1, &len), GET_STRING_UTF8);
99     if (std::strcmp(typeName, "touch") != 0 && std::strcmp(typeName, "mouse") != 0) {
100         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "EventType must be mouse or touch");
101         return nullptr;
102     }
103     if (argv[1] == nullptr) {
104         JsInputMonMgr.RemoveMonitor(env, typeName);
105         MMI_HILOGD("Remove all monitor");
106         return nullptr;
107     }
108 
109     CHKRP(env, napi_typeof(env, argv[1], &valueType), TYPEOF);
110     if (valueType != napi_function) {
111         MMI_HILOGE("Second Parameter type error");
112         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Second Parameter type error");
113         return nullptr;
114     }
115 
116     if (!JsInputMonMgr.AddEnv(env, info)) {
117         JsInputMonMgr.RemoveMonitor(env, typeName);
118         return nullptr;
119     }
120 
121     JsInputMonMgr.RemoveMonitor(env, typeName, argv[1]);
122     return nullptr;
123 }
124 
JsOff(napi_env env,napi_callback_info info)125 static napi_value JsOff(napi_env env, napi_callback_info info)
126 {
127     CALL_DEBUG_ENTER;
128     size_t argc = 2;
129     napi_value argv[2];
130 
131     CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
132     if (argc < 1) {
133         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error");
134         return nullptr;
135     }
136     JsOffApi9(env, info);
137     return nullptr;
138 }
139 
140 EXTERN_C_START
MmiInputMonitorInit(napi_env env,napi_value exports)141 static napi_value MmiInputMonitorInit(napi_env env, napi_value exports)
142 {
143     CALL_DEBUG_ENTER;
144     napi_property_descriptor desc[] = {
145         DECLARE_NAPI_FUNCTION("on", JsOn),
146         DECLARE_NAPI_FUNCTION("off", JsOff),
147     };
148     CHKRP(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc), DEFINE_PROPERTIES);
149     return exports;
150 }
151 EXTERN_C_END
152 
153 static napi_module mmiInputMonitorModule = {
154     .nm_version = 1,
155     .nm_flags = 0,
156     .nm_filename = nullptr,
157     .nm_register_func = MmiInputMonitorInit,
158     .nm_modname = "multimodalInput.inputMonitor",
159     .nm_priv = ((void*)0),
160     .reserved = { 0 },
161 };
162 
RegisterModule(void)163 extern "C" __attribute__((constructor)) void RegisterModule(void)
164 {
165     napi_module_register(&mmiInputMonitorModule);
166 }
167 } // namespace MMI
168 } // namespace OHOS
169