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 #include <cinttypes>
18 #include <string>
19 #include <uv.h>
20 #include "define_multimodal.h"
21 #include "js_input_monitor_manager.h"
22
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsInputMonitorModule" };
28 constexpr size_t MAX_STRING_LEN = 1024;
29 }
30
JsOn(napi_env env,napi_callback_info info)31 static napi_value JsOn(napi_env env, napi_callback_info info)
32 {
33 MMI_LOGD("Enter");
34 size_t argc = 2;
35 size_t requireArgc = 2;
36 napi_value argv[requireArgc];
37 napi_status status = napi_generic_failure;
38
39 status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
40 if (status != napi_ok) {
41 MMI_LOGE("Register js monitor failed, get cb info failed");
42 return nullptr;
43 }
44 if (argc < requireArgc) {
45 MMI_LOGE("Register js monitor failed, the number of parameter is error");
46 return nullptr;
47 }
48
49 napi_valuetype valueType = napi_undefined;
50 status = napi_typeof(env, argv[0], &valueType);
51 if (status != napi_ok) {
52 MMI_LOGE("Register js monitor failed, typeof failed");
53 return nullptr;
54 }
55 if (valueType != napi_string) {
56 MMI_LOGE("Register js monitor failed, value type is not napi_string");
57 return nullptr;
58 }
59
60 char typeName[MAX_STRING_LEN] = {0};
61 size_t len = 0;
62 status = napi_get_value_string_utf8(env, argv[0], typeName, MAX_STRING_LEN - 1, &len);
63 if (status != napi_ok) {
64 MMI_LOGE("Register js monitor failed, napi_get_value_string_utf8 failed");
65 return nullptr;
66 }
67 if (std::strcmp(typeName, "touch") != 0) {
68 MMI_LOGD("Register js monitor failed, the first parameter is error");
69 return nullptr;
70 }
71
72 status = napi_typeof(env, argv[1], &valueType);
73 if (status != napi_ok) {
74 MMI_LOGE("Register js monitor failed, typeof failed");
75 return nullptr;
76 }
77 if (valueType != napi_function) {
78 MMI_LOGE("Register js monitor failed, is not napi_function");
79 return nullptr;
80 }
81 if (!JSIMM.AddEnv(env, info)) {
82 MMI_LOGE("AddEnv failed, register js monitor failed");
83 return nullptr;
84 }
85 JSIMM.AddMonitor(env, argv[1]);
86 MMI_LOGD("Leave");
87 return nullptr;
88 }
89
JsOff(napi_env env,napi_callback_info info)90 static napi_value JsOff(napi_env env, napi_callback_info info)
91 {
92 MMI_LOGD("Enter");
93 size_t argc = 2;
94 napi_value argv[argc];
95 argv[0] = nullptr;
96 argv[1] = nullptr;
97 napi_status status = napi_generic_failure;
98
99 status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
100 if (status != napi_ok) {
101 MMI_LOGE("Unregister js monitor failed, get cb info failed");
102 return nullptr;
103 }
104 size_t minArgc = 1;
105 if (argc < minArgc) {
106 MMI_LOGE("Unregister js monitor failed, the number of parameter is error");
107 return nullptr;
108 }
109 if (argv[0] == nullptr) {
110 MMI_LOGE("Unregister js monitor failed, the first parameter is null");
111 return nullptr;
112 }
113 napi_valuetype valueType = napi_undefined;
114 status = napi_typeof(env, argv[0], &valueType);
115 if (status != napi_ok) {
116 MMI_LOGE("Unregister js monitor failed, typeof failed");
117 return nullptr;
118 }
119 if (valueType != napi_string) {
120 MMI_LOGE("Unregister js monitor failed, value type is not napi_string");
121 return nullptr;
122 }
123
124 char typeName[MAX_STRING_LEN] = {0};
125 size_t len = 0;
126 status = napi_get_value_string_utf8(env, argv[0], typeName, MAX_STRING_LEN - 1, &len);
127 if (status != napi_ok) {
128 MMI_LOGE("Unregister js monitor failed, napi_get_value_string_utf8 failed");
129 return nullptr;
130 }
131 if (std::strcmp(typeName, "touch") != 0) {
132 MMI_LOGE("Unregister js monitor failed, the first parameter is error");
133 return nullptr;
134 }
135 if (argv[1] == nullptr) {
136 JSIMM.RemoveMonitor(env);
137 MMI_LOGD("remove all monitor");
138 return nullptr;
139 }
140
141 status = napi_typeof(env, argv[1], &valueType);
142 if (status != napi_ok) {
143 MMI_LOGE("Unregister js monitor failed, typeof failed");
144 return nullptr;
145 }
146 if (valueType != napi_function) {
147 JSIMM.RemoveMonitor(env);
148 MMI_LOGD("remove all monitor");
149 return nullptr;
150 }
151
152 JSIMM.RemoveMonitor(env, argv[1]);
153 MMI_LOGD("Leave");
154 return nullptr;
155 }
156
157 EXTERN_C_START
MmiInputMonitorInit(napi_env env,napi_value exports)158 static napi_value MmiInputMonitorInit(napi_env env, napi_value exports)
159 {
160 MMI_LOGD("Enter");
161 napi_property_descriptor desc[] = {
162 DECLARE_NAPI_FUNCTION("on", JsOn),
163 DECLARE_NAPI_FUNCTION("off", JsOff),
164 };
165 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
166 MMI_LOGD("Leave");
167 return exports;
168 }
169 EXTERN_C_END
170
171 static napi_module mmiInputMonitorModule = {
172 .nm_version = 1,
173 .nm_flags = 0,
174 .nm_filename = nullptr,
175 .nm_register_func = MmiInputMonitorInit,
176 .nm_modname = "inputMonitor",
177 .nm_priv = ((void*)0),
178 .reserved = { 0 },
179 };
180
RegisterModule(void)181 extern "C" __attribute__((constructor)) void RegisterModule(void)
182 {
183 napi_module_register(&mmiInputMonitorModule);
184 }
185 } // namespace MMI
186 } // namespace OHOS
187