1 /*
2  * Copyright (c) 2021-2024 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_device_context.h"
17 
18 #include "mmi_log.h"
19 #include "napi_constants.h"
20 #include "util_napi_error.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "JsInputDeviceContext"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr uint32_t MIN_N_SIZE { 1 };
29 constexpr uint32_t MAX_N_SIZE { 5 };
30 constexpr int32_t STANDARD_KEY_REPEAT_DELAY { 500 };
31 constexpr int32_t MIN_KEY_REPEAT_DELAY { 300 };
32 constexpr int32_t MAX_KEY_REPEAT_DELAY { 1000 };
33 constexpr int32_t STANDARD_KEY_REPEAT_RATE { 50 };
34 constexpr int32_t MIN_KEY_REPEAT_RATE { 36 };
35 constexpr int32_t MAX_KEY_REPEAT_RATE { 100 };
36 constexpr int32_t ARGC_NUM { 2 };
37 constexpr size_t INPUT_PARAMETER { 2 };
38 } // namespace
39 
JsInputDeviceContext()40 JsInputDeviceContext::JsInputDeviceContext()
41 {
42     mgr_ = std::make_shared<JsInputDeviceManager>();
43 }
44 
~JsInputDeviceContext()45 JsInputDeviceContext::~JsInputDeviceContext()
46 {
47     std::lock_guard<std::mutex> guard(mtx_);
48     auto jsInputDeviceMgr = mgr_;
49     mgr_.reset();
50     if (jsInputDeviceMgr) {
51         jsInputDeviceMgr->ResetEnv();
52     }
53 }
54 
CreateInstance(napi_env env)55 napi_value JsInputDeviceContext::CreateInstance(napi_env env)
56 {
57     CALL_DEBUG_ENTER;
58     napi_value global = nullptr;
59     CHKRP(napi_get_global(env, &global), GET_GLOBAL);
60 
61     constexpr char className[] = "JsInputDeviceContext";
62     napi_value jsClass = nullptr;
63     napi_property_descriptor desc[] = {};
64     napi_status status = napi_define_class(env, className, sizeof(className), JsInputDeviceContext::JsConstructor,
65                                            nullptr, sizeof(desc) / sizeof(desc[0]), nullptr, &jsClass);
66     CHKRP(status, DEFINE_CLASS);
67 
68     status = napi_set_named_property(env, global, "multimodalinput_input_device_class", jsClass);
69     CHKRP(status, SET_NAMED_PROPERTY);
70 
71     napi_value jsInstance = nullptr;
72     CHKRP(napi_new_instance(env, jsClass, 0, nullptr, &jsInstance), NEW_INSTANCE);
73     CHKRP(napi_set_named_property(env, global, "multimodal_input_device", jsInstance), SET_NAMED_PROPERTY);
74 
75     JsInputDeviceContext *jsContext = nullptr;
76     CHKRP(napi_unwrap(env, jsInstance, (void**)&jsContext), UNWRAP);
77     CHKPP(jsContext);
78     CHKRP(napi_create_reference(env, jsInstance, 1, &(jsContext->contextRef_)), CREATE_REFERENCE);
79 
80     uint32_t refCount = 0;
81     CHKRP(napi_reference_ref(env, jsContext->contextRef_, &refCount), REFERENCE_REF);
82     return jsInstance;
83 }
84 
JsConstructor(napi_env env,napi_callback_info info)85 napi_value JsInputDeviceContext::JsConstructor(napi_env env, napi_callback_info info)
86 {
87     CALL_DEBUG_ENTER;
88     napi_value thisVar = nullptr;
89     void *data = nullptr;
90     CHKRP(napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data), GET_CB_INFO);
91 
92     JsInputDeviceContext *jsContext = new (std::nothrow) JsInputDeviceContext();
93     CHKPP(jsContext);
94     napi_status status = napi_wrap(env, thisVar, jsContext, [](napi_env env, void* data, void* hin) {
95         MMI_HILOGI("jsvm ends");
96         JsInputDeviceContext *context = static_cast<JsInputDeviceContext*>(data);
97         delete context;
98         context = nullptr;
99     }, nullptr, nullptr);
100     if (status != napi_ok) {
101         delete jsContext;
102         MMI_HILOGE("Failed to wrap native instance");
103         return nullptr;
104     }
105     return thisVar;
106 }
107 
GetInstance(napi_env env)108 JsInputDeviceContext* JsInputDeviceContext::GetInstance(napi_env env)
109 {
110     CALL_DEBUG_ENTER;
111     napi_value global = nullptr;
112     CHKRP(napi_get_global(env, &global), GET_GLOBAL);
113 
114     bool result = false;
115     CHKRP(napi_has_named_property(env, global, "multimodal_input_device", &result), HAS_NAMED_PROPERTY);
116     if (!result) {
117         MMI_HILOGE("multimodal_input_device was not found");
118         return nullptr;
119     }
120 
121     napi_value object = nullptr;
122     napi_handle_scope scope = nullptr;
123     napi_open_handle_scope(env, &scope);
124     CHKRP(napi_get_named_property(env, global, "multimodal_input_device", &object), GET_NAMED_PROPERTY);
125     CHKPP(object);
126 
127     JsInputDeviceContext *instance = nullptr;
128     CHKRP(napi_unwrap(env, object, (void**)&instance), UNWRAP);
129     CHKPP(instance);
130     napi_close_handle_scope(env, scope);
131     return instance;
132 }
133 
GetJsInputDeviceMgr() const134 std::shared_ptr<JsInputDeviceManager> JsInputDeviceContext::GetJsInputDeviceMgr() const
135 {
136     return mgr_;
137 }
138 
On(napi_env env,napi_callback_info info)139 napi_value JsInputDeviceContext::On(napi_env env, napi_callback_info info)
140 {
141     CALL_DEBUG_ENTER;
142     size_t argc = 2;
143     napi_value argv[2] = { 0 };
144     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
145     if (argc == 0) {
146         MMI_HILOGE("Require two parameters");
147         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
148         return nullptr;
149     }
150     if (!JsUtil::TypeOf(env, argv[0], napi_string)) {
151         MMI_HILOGE("First parameter type error");
152         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string");
153         return nullptr;
154     }
155 
156     char eventType[MAX_STRING_LEN] = { 0 };
157     size_t ret = 0;
158     CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, MAX_STRING_LEN - 1, &ret), GET_VALUE_STRING_UTF8);
159     std::string type = eventType;
160     if (type != CHANGED_TYPE) {
161         MMI_HILOGE("Type is not change");
162         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be change");
163         return nullptr;
164     }
165     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
166         MMI_HILOGE("Second parameter type error");
167         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "listener", "function");
168         return nullptr;
169     }
170 
171     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
172     CHKPP(jsIds);
173     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
174     jsInputDeviceMgr->RegisterDevListener(env, type, argv[1]);
175     return nullptr;
176 }
177 
Off(napi_env env,napi_callback_info info)178 napi_value JsInputDeviceContext::Off(napi_env env, napi_callback_info info)
179 {
180     CALL_DEBUG_ENTER;
181     size_t argc = 2;
182     napi_value argv[2] = { 0 };
183     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
184     if (argc == 0) {
185         MMI_HILOGE("Require two parameters");
186         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
187         return nullptr;
188     }
189     if (!JsUtil::TypeOf(env, argv[0], napi_string)) {
190         MMI_HILOGE("First parameter type error");
191         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string");
192         return nullptr;
193     }
194 
195     char eventType[MAX_STRING_LEN] = { 0 };
196     size_t ret = 0;
197     CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, MAX_STRING_LEN - 1, &ret), GET_VALUE_STRING_UTF8);
198     std::string type = eventType;
199     if (type != CHANGED_TYPE) {
200         MMI_HILOGE("Type is not change");
201         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be change");
202         return nullptr;
203     }
204 
205     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
206     CHKPP(jsIds);
207     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
208     if (argc == 1) {
209         jsInputDeviceMgr->UnregisterDevListener(env, type);
210         return nullptr;
211     }
212     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
213         MMI_HILOGE("Second parameter type error");
214         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "listener", "function");
215         return nullptr;
216     }
217     jsInputDeviceMgr->UnregisterDevListener(env, type, argv[1]);
218     return nullptr;
219 }
220 
GetDeviceIds(napi_env env,napi_callback_info info)221 napi_value JsInputDeviceContext::GetDeviceIds(napi_env env, napi_callback_info info)
222 {
223     CALL_DEBUG_ENTER;
224     size_t argc = 1;
225     napi_value argv[1] = { 0 };
226     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
227     if (argc > 1) {
228         THROWERR(env, "too many parameters");
229         return nullptr;
230     }
231 
232     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
233     CHKPP(jsIds);
234     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
235     if (argc == 0) {
236         return jsInputDeviceMgr->GetDeviceIds(env);
237     }
238     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
239         THROWERR(env, "The first parameter type is wrong");
240         return nullptr;
241     }
242     return jsInputDeviceMgr->GetDeviceIds(env, argv[0]);
243 }
244 
GetDevice(napi_env env,napi_callback_info info)245 napi_value JsInputDeviceContext::GetDevice(napi_env env, napi_callback_info info)
246 {
247     CALL_DEBUG_ENTER;
248     size_t argc = 2;
249     napi_value argv[2] = { 0 };
250     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
251     if (argc < 1 || argc > INPUT_PARAMETER) {
252         THROWERR(env, "the number of parameters is not as expected");
253         return nullptr;
254     }
255     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
256         THROWERR(env, "The first parameter type is wrong");
257         return nullptr;
258     }
259     int32_t id = 0;
260     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
261     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
262     CHKPP(jsDev);
263     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
264     if (argc == 1) {
265         return jsInputDeviceMgr->GetDevice(env, id);
266     }
267     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
268         THROWERR(env, "The second parameter type is wrong");
269         return nullptr;
270     }
271     return jsInputDeviceMgr->GetDevice(env, id, argv[1]);
272 }
273 
SupportKeys(napi_env env,napi_callback_info info)274 napi_value JsInputDeviceContext::SupportKeys(napi_env env, napi_callback_info info)
275 {
276     CALL_DEBUG_ENTER;
277     size_t argc = 3;
278     napi_value argv[3] = { 0 };
279     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
280     if (argc < INPUT_PARAMETER) {
281         MMI_HILOGE("Require three parameters");
282         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
283         return nullptr;
284     }
285 
286     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
287         MMI_HILOGE("First parameter type error");
288         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
289         return nullptr;
290     }
291     int32_t deviceId = 0;
292     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
293 
294     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
295         MMI_HILOGE("Second parameter type error");
296         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
297         return nullptr;
298     }
299     uint32_t size = 0;
300     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
301     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
302         MMI_HILOGE("Size range error");
303         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
304         return nullptr;
305     }
306 
307     int32_t data = 0;
308     std::vector<int32_t> keyCodes;
309     for (uint32_t i = 0; i < size; ++i) {
310         napi_value keyValue = nullptr;
311         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
312         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
313             MMI_HILOGE("Second parameter type error");
314             THROWERR_API9(env, COMMON_PARAMETER_ERROR, "KeyCode", "number");
315             return nullptr;
316         }
317         CHKRP(napi_get_value_int32(env, keyValue, &data), GET_VALUE_INT32);
318         keyCodes.push_back(data);
319     }
320 
321     JsInputDeviceContext *jsContext = JsInputDeviceContext::GetInstance(env);
322     CHKPP(jsContext);
323     auto jsInputDeviceMgr = jsContext->GetJsInputDeviceMgr();
324     if (argc == INPUT_PARAMETER) {
325         return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes);
326     }
327     if (!JsUtil::TypeOf(env, argv[2], napi_function)) {
328         MMI_HILOGE("Third parameter type error");
329         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
330         return nullptr;
331     }
332     return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes, argv[2]);
333 }
334 
SupportKeysSync(napi_env env,napi_callback_info info)335 napi_value JsInputDeviceContext::SupportKeysSync(napi_env env, napi_callback_info info)
336 {
337     CALL_DEBUG_ENTER;
338     size_t argc = ARGC_NUM;
339     napi_value argv[ARGC_NUM] = { 0 };
340     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
341     if (argc != ARGC_NUM) {
342         MMI_HILOGE("Require two parameters");
343         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
344         return nullptr;
345     }
346 
347     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
348         MMI_HILOGE("First parameter type error");
349         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
350         return nullptr;
351     }
352     int32_t deviceId = 0;
353     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
354 
355     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
356         MMI_HILOGE("Second parameter type error");
357         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
358         return nullptr;
359     }
360     uint32_t size = 0;
361     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
362     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
363         MMI_HILOGE("Size range error");
364         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
365         return nullptr;
366     }
367 
368     int32_t data = 0;
369     std::vector<int32_t> keyCodes;
370     for (uint32_t i = 0; i < size; ++i) {
371         napi_value keyValue = nullptr;
372         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
373         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
374             THROWERR_API9(env, COMMON_PARAMETER_ERROR, "KeyCode", "number");
375             return nullptr;
376         }
377         CHKRP(napi_get_value_int32(env, keyValue, &data), GET_VALUE_INT32);
378         keyCodes.push_back(data);
379     }
380 
381     JsInputDeviceContext *jsContext = JsInputDeviceContext::GetInstance(env);
382     CHKPP(jsContext);
383     auto jsInputDeviceMgr = jsContext->GetJsInputDeviceMgr();
384     CHKPP(jsInputDeviceMgr);
385     return jsInputDeviceMgr->SupportKeysSync(env, deviceId, keyCodes);
386 }
387 
GetKeyboardType(napi_env env,napi_callback_info info)388 napi_value JsInputDeviceContext::GetKeyboardType(napi_env env, napi_callback_info info)
389 {
390     CALL_DEBUG_ENTER;
391     size_t argc = 2;
392     napi_value argv[2] = { 0 };
393     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
394     if (argc == 0) {
395         MMI_HILOGE("Require two parameters");
396         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
397         return nullptr;
398     }
399 
400     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
401         MMI_HILOGE("First parameter type error");
402         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
403         return nullptr;
404     }
405     int32_t id = 0;
406     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
407 
408     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
409     CHKPP(jsDev);
410     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
411     CHKPP(jsInputDeviceMgr);
412     if (argc == 1) {
413         return jsInputDeviceMgr->GetKeyboardType(env, id);
414     }
415     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
416         MMI_HILOGE("Second parameter type error");
417         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
418         return nullptr;
419     }
420     return jsInputDeviceMgr->GetKeyboardType(env, id, argv[1]);
421 }
422 
GetKeyboardTypeSync(napi_env env,napi_callback_info info)423 napi_value JsInputDeviceContext::GetKeyboardTypeSync(napi_env env, napi_callback_info info)
424 {
425     CALL_DEBUG_ENTER;
426     size_t argc = 1;
427     napi_value argv[1] = { 0 };
428     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
429     if (argc != 1) {
430         MMI_HILOGE("Require one parameters");
431         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
432         return nullptr;
433     }
434 
435     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
436         MMI_HILOGE("First parameter type error");
437         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
438         return nullptr;
439     }
440     int32_t id = 0;
441     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
442 
443     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
444     CHKPP(jsDev);
445     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
446     CHKPP(jsInputDeviceMgr);
447 
448     return jsInputDeviceMgr->GetKeyboardTypeSync(env, id);
449 }
450 
GetDeviceList(napi_env env,napi_callback_info info)451 napi_value JsInputDeviceContext::GetDeviceList(napi_env env, napi_callback_info info)
452 {
453     CALL_DEBUG_ENTER;
454     size_t argc = 1;
455     napi_value argv[1] = { 0 };
456     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
457 
458     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
459     CHKPP(jsIds);
460     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
461     if (argc == 0) {
462         return jsInputDeviceMgr->GetDeviceList(env);
463     }
464     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
465         MMI_HILOGE("First parameter type error");
466         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
467         return nullptr;
468     }
469     return jsInputDeviceMgr->GetDeviceList(env, argv[0]);
470 }
471 
GetDeviceInfo(napi_env env,napi_callback_info info)472 napi_value JsInputDeviceContext::GetDeviceInfo(napi_env env, napi_callback_info info)
473 {
474     CALL_DEBUG_ENTER;
475     size_t argc = 2;
476     napi_value argv[2] = { 0 };
477     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
478     if (argc == 0) {
479         MMI_HILOGE("Require two parameters");
480         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
481         return nullptr;
482     }
483     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
484         MMI_HILOGE("First parameter type error");
485         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
486         return nullptr;
487     }
488     int32_t id = 0;
489     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
490 
491     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
492     CHKPP(jsDev);
493     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
494     if (argc == 1) {
495         return jsInputDeviceMgr->GetDeviceInfo(env, id);
496     }
497     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
498         MMI_HILOGE("Second parameter type error");
499         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
500         return nullptr;
501     }
502     return jsInputDeviceMgr->GetDeviceInfo(env, id, argv[1]);
503 }
504 
GetDeviceInfoSync(napi_env env,napi_callback_info info)505 napi_value JsInputDeviceContext::GetDeviceInfoSync(napi_env env, napi_callback_info info)
506 {
507     CALL_DEBUG_ENTER;
508     size_t argc = 1;
509     napi_value argv[1] = { 0 };
510     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
511     if (argc != 1) {
512         MMI_HILOGE("Require one parameters");
513         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
514         return nullptr;
515     }
516     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
517         MMI_HILOGE("First parameter type error");
518         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
519         return nullptr;
520     }
521     int32_t id = 0;
522     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
523 
524     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
525     CHKPP(jsDev);
526     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
527 
528     return jsInputDeviceMgr->GetDeviceInfoSync(env, id);
529 }
530 
SetKeyboardRepeatDelay(napi_env env,napi_callback_info info)531 napi_value JsInputDeviceContext::SetKeyboardRepeatDelay(napi_env env, napi_callback_info info)
532 {
533     CALL_DEBUG_ENTER;
534     size_t argc = 2;
535     napi_value argv[2] = { 0 };
536     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
537     if (argc == 0) {
538         MMI_HILOGE("At least 1 parameter is required");
539         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
540         return nullptr;
541     }
542     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
543         MMI_HILOGE("delay parameter type is invalid");
544         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "delay", "number");
545         return nullptr;
546     }
547     int32_t repeatDelay = STANDARD_KEY_REPEAT_DELAY;
548     CHKRP(napi_get_value_int32(env, argv[0], &repeatDelay), GET_VALUE_INT32);
549     if (repeatDelay < MIN_KEY_REPEAT_DELAY) {
550         repeatDelay = MIN_KEY_REPEAT_DELAY;
551     } else if (repeatDelay > MAX_KEY_REPEAT_DELAY) {
552         repeatDelay = MAX_KEY_REPEAT_DELAY;
553     }
554     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
555     CHKPP(jsDev);
556     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
557     if (argc == 1) {
558         return jsInputDeviceMgr->SetKeyboardRepeatDelay(env, repeatDelay);
559     }
560     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
561         MMI_HILOGE("callback parameter type is invalid");
562         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
563         return nullptr;
564     }
565     return jsInputDeviceMgr->SetKeyboardRepeatDelay(env, repeatDelay, argv[1]);
566 }
567 
SetKeyboardRepeatRate(napi_env env,napi_callback_info info)568 napi_value JsInputDeviceContext::SetKeyboardRepeatRate(napi_env env, napi_callback_info info)
569 {
570     CALL_DEBUG_ENTER;
571     size_t argc = 2;
572     napi_value argv[2] = { 0 };
573     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
574     if (argc == 0) {
575         MMI_HILOGE("At least 1 parameter is required");
576         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
577         return nullptr;
578     }
579     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
580         MMI_HILOGE("rate parameter type is invalid");
581         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "rate", "number");
582         return nullptr;
583     }
584     int32_t repeatRate = STANDARD_KEY_REPEAT_RATE;
585     CHKRP(napi_get_value_int32(env, argv[0], &repeatRate), GET_VALUE_INT32);
586     if (repeatRate < MIN_KEY_REPEAT_RATE) {
587         repeatRate = MIN_KEY_REPEAT_RATE;
588     } else if (repeatRate > MAX_KEY_REPEAT_RATE) {
589         repeatRate = MAX_KEY_REPEAT_RATE;
590     }
591     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
592     CHKPP(jsDev);
593     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
594     if (argc == 1) {
595         return jsInputDeviceMgr->SetKeyboardRepeatRate(env, repeatRate);
596     }
597     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
598         MMI_HILOGE("callback parameter type is invalid");
599         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
600         return nullptr;
601     }
602     return jsInputDeviceMgr->SetKeyboardRepeatRate(env, repeatRate, argv[1]);
603 }
604 
GetKeyboardRepeatDelay(napi_env env,napi_callback_info info)605 napi_value JsInputDeviceContext::GetKeyboardRepeatDelay(napi_env env, napi_callback_info info)
606 {
607     CALL_DEBUG_ENTER;
608     size_t argc = 1;
609     napi_value argv[1] = { 0 };
610     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
611     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
612     CHKPP(jsDev);
613     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
614     if (argc == 0) {
615         return jsInputDeviceMgr->GetKeyboardRepeatDelay(env);
616     }
617     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
618         MMI_HILOGE("callback parameter type is invalid");
619         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
620         return nullptr;
621     }
622     return jsInputDeviceMgr->GetKeyboardRepeatDelay(env, argv[0]);
623 }
624 
GetKeyboardRepeatRate(napi_env env,napi_callback_info info)625 napi_value JsInputDeviceContext::GetKeyboardRepeatRate(napi_env env, napi_callback_info info)
626 {
627     CALL_DEBUG_ENTER;
628     size_t argc = 1;
629     napi_value argv[1] = { 0 };
630     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
631     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
632     CHKPP(jsDev);
633     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
634     if (argc == 0) {
635         return jsInputDeviceMgr->GetKeyboardRepeatRate(env);
636     }
637     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
638         MMI_HILOGE("callback parameter type is invalid");
639         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
640         return nullptr;
641     }
642     return jsInputDeviceMgr->GetKeyboardRepeatRate(env, argv[0]);
643 }
644 
GetIntervalSinceLastInput(napi_env env,napi_callback_info info)645 napi_value JsInputDeviceContext::GetIntervalSinceLastInput(napi_env env, napi_callback_info info)
646 {
647     CALL_DEBUG_ENTER;
648     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
649     CHKPP(jsDev);
650     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
651     return jsInputDeviceMgr->GetIntervalSinceLastInput(env);
652 }
653 
EnumClassConstructor(napi_env env,napi_callback_info info)654 napi_value JsInputDeviceContext::EnumClassConstructor(napi_env env, napi_callback_info info)
655 {
656     CALL_DEBUG_ENTER;
657     size_t argc = 0;
658     napi_value args[1] = { 0 };
659     napi_value ret = nullptr;
660     void *data = nullptr;
661     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
662     return ret;
663 }
664 
CreateEnumKeyboardType(napi_env env,napi_value exports)665 napi_value JsInputDeviceContext::CreateEnumKeyboardType(napi_env env, napi_value exports)
666 {
667     CALL_DEBUG_ENTER;
668     napi_value none = nullptr;
669     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_NONE, &none), CREATE_INT32);
670     napi_value unknown = nullptr;
671     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_UNKNOWN, &unknown), CREATE_INT32);
672     napi_value alphabeticKeyboard = nullptr;
673     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD, &alphabeticKeyboard), CREATE_INT32);
674     napi_value digitalKeyboard = nullptr;
675     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD, &digitalKeyboard), CREATE_INT32);
676     napi_value handwritingPen = nullptr;
677     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN, &handwritingPen), CREATE_INT32);
678     napi_value remoteControl = nullptr;
679     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_REMOTECONTROL, &remoteControl), CREATE_INT32);
680     napi_property_descriptor desc[] = {
681         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
682         DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN", unknown),
683         DECLARE_NAPI_STATIC_PROPERTY("ALPHABETIC_KEYBOARD", alphabeticKeyboard),
684         DECLARE_NAPI_STATIC_PROPERTY("DIGITAL_KEYBOARD", digitalKeyboard),
685         DECLARE_NAPI_STATIC_PROPERTY("HANDWRITING_PEN", handwritingPen),
686         DECLARE_NAPI_STATIC_PROPERTY("REMOTE_CONTROL", remoteControl),
687     };
688     napi_value result = nullptr;
689     CHKRP(napi_define_class(env, "KeyboardType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
690         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
691     CHKRP(napi_set_named_property(env, exports, "KeyboardType", result), SET_NAMED_PROPERTY);
692     return exports;
693 }
694 
Export(napi_env env,napi_value exports)695 napi_value JsInputDeviceContext::Export(napi_env env, napi_value exports)
696 {
697     CALL_DEBUG_ENTER;
698     CHKPP(CreateInstance(env));
699     napi_property_descriptor desc[] = {
700         DECLARE_NAPI_STATIC_FUNCTION("on", On),
701         DECLARE_NAPI_STATIC_FUNCTION("off", Off),
702         DECLARE_NAPI_STATIC_FUNCTION("getDevice", GetDevice),
703         DECLARE_NAPI_STATIC_FUNCTION("getDeviceIds", GetDeviceIds),
704         DECLARE_NAPI_STATIC_FUNCTION("supportKeys", SupportKeys),
705         DECLARE_NAPI_STATIC_FUNCTION("supportKeysSync", SupportKeysSync),
706         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardType", GetKeyboardType),
707         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardTypeSync", GetKeyboardTypeSync),
708         DECLARE_NAPI_STATIC_FUNCTION("getDeviceList", GetDeviceList),
709         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfo", GetDeviceInfo),
710         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfoSync", GetDeviceInfoSync),
711         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatDelay", SetKeyboardRepeatDelay),
712         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatRate", SetKeyboardRepeatRate),
713         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatDelay", GetKeyboardRepeatDelay),
714         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatRate", GetKeyboardRepeatRate),
715         DECLARE_NAPI_STATIC_FUNCTION("getIntervalSinceLastInput", GetIntervalSinceLastInput),
716     };
717     CHKRP(napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc), DEFINE_PROPERTIES);
718     CHKPP(CreateEnumKeyboardType(env, exports));
719     return exports;
720 }
721 } // namespace MMI
722 } // namespace OHOS
723