• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_constants.h"
19 #include "util_napi_error.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "JsInputDeviceContext"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr uint32_t MIN_N_SIZE { 1 };
28 constexpr uint32_t MAX_N_SIZE { 5 };
29 constexpr int32_t STANDARD_KEY_REPEAT_DELAY { 500 };
30 constexpr int32_t MIN_KEY_REPEAT_DELAY { 300 };
31 constexpr int32_t MAX_KEY_REPEAT_DELAY { 1000 };
32 constexpr int32_t STANDARD_KEY_REPEAT_RATE { 50 };
33 constexpr int32_t MIN_KEY_REPEAT_RATE { 36 };
34 constexpr int32_t MAX_KEY_REPEAT_RATE { 100 };
35 constexpr int32_t ARGC_NUM { 2 };
36 constexpr size_t INPUT_PARAMETER { 2 };
37 } // namespace
38 
JsInputDeviceContext()39 JsInputDeviceContext::JsInputDeviceContext()
40 {
41     mgr_ = std::make_shared<JsInputDeviceManager>();
42 }
43 
~JsInputDeviceContext()44 JsInputDeviceContext::~JsInputDeviceContext()
45 {
46     std::lock_guard<std::mutex> guard(mtx_);
47     auto jsInputDeviceMgr = mgr_;
48     mgr_.reset();
49     if (jsInputDeviceMgr) {
50         jsInputDeviceMgr->ResetEnv();
51     }
52 }
53 
CreateInstance(napi_env env)54 napi_value JsInputDeviceContext::CreateInstance(napi_env env)
55 {
56     CALL_DEBUG_ENTER;
57     napi_value global = nullptr;
58     CHKRP(napi_get_global(env, &global), GET_GLOBAL);
59 
60     constexpr char className[] = "JsInputDeviceContext";
61     napi_value jsClass = nullptr;
62     napi_property_descriptor desc[] = {};
63     napi_status status = napi_define_class(env, className, sizeof(className), JsInputDeviceContext::JsConstructor,
64                                            nullptr, sizeof(desc) / sizeof(desc[0]), nullptr, &jsClass);
65     CHKRP(status, DEFINE_CLASS);
66 
67     status = napi_set_named_property(env, global, "multimodalinput_input_device_class", jsClass);
68     CHKRP(status, SET_NAMED_PROPERTY);
69 
70     napi_value jsInstance = nullptr;
71     CHKRP(napi_new_instance(env, jsClass, 0, nullptr, &jsInstance), NEW_INSTANCE);
72     CHKRP(napi_set_named_property(env, global, "multimodal_input_device", jsInstance), SET_NAMED_PROPERTY);
73 
74     JsInputDeviceContext *jsContext = nullptr;
75     CHKRP(napi_unwrap(env, jsInstance, (void**)&jsContext), UNWRAP);
76     CHKPP(jsContext);
77     CHKRP(napi_create_reference(env, jsInstance, 1, &(jsContext->contextRef_)), CREATE_REFERENCE);
78 
79     uint32_t refCount = 0;
80     CHKRP(napi_reference_ref(env, jsContext->contextRef_, &refCount), REFERENCE_REF);
81     return jsInstance;
82 }
83 
JsConstructor(napi_env env,napi_callback_info info)84 napi_value JsInputDeviceContext::JsConstructor(napi_env env, napi_callback_info info)
85 {
86     CALL_DEBUG_ENTER;
87     napi_value thisVar = nullptr;
88     void *data = nullptr;
89     CHKRP(napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data), GET_CB_INFO);
90 
91     JsInputDeviceContext *jsContext = new (std::nothrow) JsInputDeviceContext();
92     CHKPP(jsContext);
93     napi_status status = napi_wrap(env, thisVar, jsContext, [](napi_env env, void* data, void* hin) {
94         MMI_HILOGI("jsvm ends");
95         JsInputDeviceContext *context = static_cast<JsInputDeviceContext*>(data);
96         delete context;
97         context = nullptr;
98     }, nullptr, nullptr);
99     if (status != napi_ok) {
100         delete jsContext;
101         jsContext = nullptr;
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 < 1) {
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     CHKPP(jsInputDeviceMgr);
175     jsInputDeviceMgr->RegisterDevListener(env, type, argv[1]);
176     return nullptr;
177 }
178 
Off(napi_env env,napi_callback_info info)179 napi_value JsInputDeviceContext::Off(napi_env env, napi_callback_info info)
180 {
181     CALL_DEBUG_ENTER;
182     size_t argc = 2;
183     napi_value argv[2] = { 0 };
184     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
185     if (argc < 1) {
186         MMI_HILOGE("Require two parameters");
187         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
188         return nullptr;
189     }
190     if (!JsUtil::TypeOf(env, argv[0], napi_string)) {
191         MMI_HILOGE("First parameter type error");
192         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string");
193         return nullptr;
194     }
195 
196     char eventType[MAX_STRING_LEN] = { 0 };
197     size_t ret = 0;
198     CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, MAX_STRING_LEN - 1, &ret), GET_VALUE_STRING_UTF8);
199     std::string type = eventType;
200     if (type != CHANGED_TYPE) {
201         MMI_HILOGE("Type is not change");
202         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be change");
203         return nullptr;
204     }
205 
206     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
207     CHKPP(jsIds);
208     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
209     CHKPP(jsInputDeviceMgr);
210     if (argc == 1) {
211         jsInputDeviceMgr->UnregisterDevListener(env, type);
212         return nullptr;
213     }
214     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
215         MMI_HILOGE("Second parameter type error");
216         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "listener", "function");
217         return nullptr;
218     }
219     jsInputDeviceMgr->UnregisterDevListener(env, type, argv[1]);
220     return nullptr;
221 }
222 
GetDeviceIds(napi_env env,napi_callback_info info)223 napi_value JsInputDeviceContext::GetDeviceIds(napi_env env, napi_callback_info info)
224 {
225     CALL_DEBUG_ENTER;
226     size_t argc = 1;
227     napi_value argv[1] = { 0 };
228     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
229     if (argc > 1) {
230         THROWERR(env, "too many parameters");
231         return nullptr;
232     }
233 
234     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
235     CHKPP(jsIds);
236     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
237     CHKPP(jsInputDeviceMgr);
238     if (argc < 1) {
239         return jsInputDeviceMgr->GetDeviceIds(env);
240     }
241     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
242         THROWERR(env, "The first parameter type is wrong");
243         return nullptr;
244     }
245     return jsInputDeviceMgr->GetDeviceIds(env, argv[0]);
246 }
247 
GetDevice(napi_env env,napi_callback_info info)248 napi_value JsInputDeviceContext::GetDevice(napi_env env, napi_callback_info info)
249 {
250     CALL_DEBUG_ENTER;
251     size_t argc = 2;
252     napi_value argv[2] = { 0 };
253     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
254     if (argc < 1 || argc > INPUT_PARAMETER) {
255         THROWERR(env, "the number of parameters is not as expected");
256         return nullptr;
257     }
258     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
259         THROWERR(env, "The first parameter type is wrong");
260         return nullptr;
261     }
262     int32_t id = 0;
263     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
264     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
265     CHKPP(jsDev);
266     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
267     CHKPP(jsInputDeviceMgr);
268     if (argc == 1) {
269         return jsInputDeviceMgr->GetDevice(env, id);
270     }
271     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
272         THROWERR(env, "Second parameter type is wrong");
273         return nullptr;
274     }
275     return jsInputDeviceMgr->GetDevice(env, id, argv[1]);
276 }
277 
SupportKeys(napi_env env,napi_callback_info info)278 napi_value JsInputDeviceContext::SupportKeys(napi_env env, napi_callback_info info)
279 {
280     CALL_DEBUG_ENTER;
281     size_t argc = 3;
282     napi_value argv[3] = { 0 };
283     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
284     if (argc < INPUT_PARAMETER) {
285         MMI_HILOGE("Require three parameters");
286         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
287         return nullptr;
288     }
289 
290     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
291         MMI_HILOGE("First parameter type error");
292         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
293         return nullptr;
294     }
295     int32_t deviceId = 0;
296     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
297 
298     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
299         MMI_HILOGE("Second parameter type error");
300         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
301         return nullptr;
302     }
303     uint32_t size = 0;
304     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
305     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
306         MMI_HILOGE("Size range error");
307         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
308         return nullptr;
309     }
310 
311     int32_t data = 0;
312     std::vector<int32_t> keyCodes;
313     for (uint32_t i = 0; i < size; ++i) {
314         napi_value keyValue = nullptr;
315         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
316         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
317             MMI_HILOGE("Second parameter type error");
318             THROWERR_API9(env, COMMON_PARAMETER_ERROR, "KeyCode", "number");
319             return nullptr;
320         }
321         CHKRP(napi_get_value_int32(env, keyValue, &data), GET_VALUE_INT32);
322         keyCodes.push_back(data);
323     }
324 
325     JsInputDeviceContext *jsContext = JsInputDeviceContext::GetInstance(env);
326     CHKPP(jsContext);
327     auto jsInputDeviceMgr = jsContext->GetJsInputDeviceMgr();
328     if (argc == INPUT_PARAMETER) {
329         return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes);
330     }
331     if (!JsUtil::TypeOf(env, argv[2], napi_function)) {
332         MMI_HILOGE("Third parameter type error");
333         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
334         return nullptr;
335     }
336     return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes, argv[2]);
337 }
338 
SupportKeysSync(napi_env env,napi_callback_info info)339 napi_value JsInputDeviceContext::SupportKeysSync(napi_env env, napi_callback_info info)
340 {
341     CALL_DEBUG_ENTER;
342     size_t argc = ARGC_NUM;
343     napi_value argv[ARGC_NUM] = { 0 };
344     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
345     if (argc != ARGC_NUM) {
346         MMI_HILOGE("Require two parameters");
347         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
348         return nullptr;
349     }
350 
351     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
352         MMI_HILOGE("First parameter type error");
353         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
354         return nullptr;
355     }
356     int32_t deviceId = 0;
357     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
358 
359     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
360         MMI_HILOGE("Second parameter type error");
361         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
362         return nullptr;
363     }
364     uint32_t size = 0;
365     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
366     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
367         MMI_HILOGE("Size range error");
368         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
369         return nullptr;
370     }
371 
372     int32_t data = 0;
373     std::vector<int32_t> keyCodes;
374     for (uint32_t i = 0; i < size; ++i) {
375         napi_value keyValue = nullptr;
376         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
377         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
378             MMI_HILOGE("Second parameter type error");
379             THROWERR_API9(env, COMMON_PARAMETER_ERROR, "KeyCode", "number");
380             return nullptr;
381         }
382         CHKRP(napi_get_value_int32(env, keyValue, &data), GET_VALUE_INT32);
383         keyCodes.push_back(data);
384     }
385 
386     JsInputDeviceContext *jsContext = JsInputDeviceContext::GetInstance(env);
387     CHKPP(jsContext);
388     auto jsInputDeviceMgr = jsContext->GetJsInputDeviceMgr();
389     CHKPP(jsInputDeviceMgr);
390     return jsInputDeviceMgr->SupportKeysSync(env, deviceId, keyCodes);
391 }
392 
GetKeyboardType(napi_env env,napi_callback_info info)393 napi_value JsInputDeviceContext::GetKeyboardType(napi_env env, napi_callback_info info)
394 {
395     CALL_DEBUG_ENTER;
396     size_t argc = 2;
397     napi_value argv[2] = { 0 };
398     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
399     if (argc < 1) {
400         MMI_HILOGE("Require two parameters");
401         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
402         return nullptr;
403     }
404 
405     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
406         MMI_HILOGE("First parameter type error");
407         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
408         return nullptr;
409     }
410     int32_t id = 0;
411     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
412 
413     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
414     CHKPP(jsDev);
415     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
416     CHKPP(jsInputDeviceMgr);
417     if (argc == 1) {
418         return jsInputDeviceMgr->GetKeyboardType(env, id);
419     }
420     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
421         MMI_HILOGE("Second parameter type error");
422         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
423         return nullptr;
424     }
425     return jsInputDeviceMgr->GetKeyboardType(env, id, argv[1]);
426 }
427 
GetKeyboardTypeSync(napi_env env,napi_callback_info info)428 napi_value JsInputDeviceContext::GetKeyboardTypeSync(napi_env env, napi_callback_info info)
429 {
430     CALL_DEBUG_ENTER;
431     size_t argc = 1;
432     napi_value argv[1] = { 0 };
433     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
434     if (argc != 1) {
435         MMI_HILOGE("Require one parameters");
436         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
437         return nullptr;
438     }
439 
440     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
441         MMI_HILOGE("First parameter type error");
442         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
443         return nullptr;
444     }
445     int32_t id = 0;
446     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
447 
448     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
449     CHKPP(jsDev);
450     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
451     CHKPP(jsInputDeviceMgr);
452 
453     return jsInputDeviceMgr->GetKeyboardTypeSync(env, id);
454 }
455 
GetDeviceList(napi_env env,napi_callback_info info)456 napi_value JsInputDeviceContext::GetDeviceList(napi_env env, napi_callback_info info)
457 {
458     CALL_DEBUG_ENTER;
459     size_t argc = 1;
460     napi_value argv[1] = { 0 };
461     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
462 
463     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
464     CHKPP(jsIds);
465     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
466     CHKPP(jsInputDeviceMgr);
467     if (argc < 1) {
468         return jsInputDeviceMgr->GetDeviceList(env);
469     }
470     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
471         MMI_HILOGE("First parameter type error");
472         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
473         return nullptr;
474     }
475     return jsInputDeviceMgr->GetDeviceList(env, argv[0]);
476 }
477 
GetDeviceInfo(napi_env env,napi_callback_info info)478 napi_value JsInputDeviceContext::GetDeviceInfo(napi_env env, napi_callback_info info)
479 {
480     CALL_DEBUG_ENTER;
481     size_t argc = 2;
482     napi_value argv[2] = { 0 };
483     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
484     if (argc < 1) {
485         MMI_HILOGE("Require two parameters");
486         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
487         return nullptr;
488     }
489     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
490         MMI_HILOGE("First parameter type error");
491         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
492         return nullptr;
493     }
494     int32_t id = 0;
495     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
496 
497     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
498     CHKPP(jsDev);
499     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
500     CHKPP(jsInputDeviceMgr);
501     if (argc == 1) {
502         return jsInputDeviceMgr->GetDeviceInfo(env, id);
503     }
504     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
505         MMI_HILOGE("Second parameter type error");
506         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
507         return nullptr;
508     }
509     return jsInputDeviceMgr->GetDeviceInfo(env, id, argv[1]);
510 }
511 
GetDeviceInfoSync(napi_env env,napi_callback_info info)512 napi_value JsInputDeviceContext::GetDeviceInfoSync(napi_env env, napi_callback_info info)
513 {
514     CALL_DEBUG_ENTER;
515     size_t argc = 1;
516     napi_value argv[1] = { 0 };
517     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
518     if (argc != 1) {
519         MMI_HILOGE("Require one parameters");
520         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
521         return nullptr;
522     }
523     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
524         MMI_HILOGE("First parameter type error");
525         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
526         return nullptr;
527     }
528     int32_t id = 0;
529     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
530 
531     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
532     CHKPP(jsDev);
533     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
534     CHKPP(jsInputDeviceMgr);
535     return jsInputDeviceMgr->GetDeviceInfoSync(env, id);
536 }
537 
SetKeyboardRepeatDelay(napi_env env,napi_callback_info info)538 napi_value JsInputDeviceContext::SetKeyboardRepeatDelay(napi_env env, napi_callback_info info)
539 {
540     CALL_DEBUG_ENTER;
541     size_t argc = 2;
542     napi_value argv[2] = { 0 };
543     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
544     if (argc < 1) {
545         MMI_HILOGE("At least 1 parameter is required");
546         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
547         return nullptr;
548     }
549     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
550         MMI_HILOGE("The delay parameter type is invalid");
551         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "delay", "number");
552         return nullptr;
553     }
554     int32_t repeatDelay = STANDARD_KEY_REPEAT_DELAY;
555     CHKRP(napi_get_value_int32(env, argv[0], &repeatDelay), GET_VALUE_INT32);
556     if (repeatDelay < MIN_KEY_REPEAT_DELAY) {
557         repeatDelay = MIN_KEY_REPEAT_DELAY;
558     } else if (repeatDelay > MAX_KEY_REPEAT_DELAY) {
559         repeatDelay = MAX_KEY_REPEAT_DELAY;
560     }
561     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
562     CHKPP(jsDev);
563     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
564     CHKPP(jsInputDeviceMgr);
565     if (argc == 1) {
566         return jsInputDeviceMgr->SetKeyboardRepeatDelay(env, repeatDelay);
567     }
568     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
569         MMI_HILOGE("Callback parameter type is invalid");
570         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
571         return nullptr;
572     }
573     return jsInputDeviceMgr->SetKeyboardRepeatDelay(env, repeatDelay, argv[1]);
574 }
575 
SetKeyboardRepeatRate(napi_env env,napi_callback_info info)576 napi_value JsInputDeviceContext::SetKeyboardRepeatRate(napi_env env, napi_callback_info info)
577 {
578     CALL_DEBUG_ENTER;
579     size_t argc = 2;
580     napi_value argv[2] = { 0 };
581     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
582     if (argc < 1) {
583         MMI_HILOGE("At least 1 parameter is required");
584         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
585         return nullptr;
586     }
587     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
588         MMI_HILOGE("The rate parameter type is invalid");
589         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "rate", "number");
590         return nullptr;
591     }
592     int32_t repeatRate = STANDARD_KEY_REPEAT_RATE;
593     CHKRP(napi_get_value_int32(env, argv[0], &repeatRate), GET_VALUE_INT32);
594     if (repeatRate < MIN_KEY_REPEAT_RATE) {
595         repeatRate = MIN_KEY_REPEAT_RATE;
596     } else if (repeatRate > MAX_KEY_REPEAT_RATE) {
597         repeatRate = MAX_KEY_REPEAT_RATE;
598     }
599     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
600     CHKPP(jsDev);
601     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
602     CHKPP(jsInputDeviceMgr);
603     if (argc == 1) {
604         return jsInputDeviceMgr->SetKeyboardRepeatRate(env, repeatRate);
605     }
606     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
607         MMI_HILOGE("Callback parameter type is invalid");
608         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
609         return nullptr;
610     }
611     return jsInputDeviceMgr->SetKeyboardRepeatRate(env, repeatRate, argv[1]);
612 }
613 
GetKeyboardRepeatDelay(napi_env env,napi_callback_info info)614 napi_value JsInputDeviceContext::GetKeyboardRepeatDelay(napi_env env, napi_callback_info info)
615 {
616     CALL_DEBUG_ENTER;
617     size_t argc = 1;
618     napi_value argv[1] = { 0 };
619     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
620     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
621     CHKPP(jsDev);
622     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
623     CHKPP(jsInputDeviceMgr);
624     if (argc < 1) {
625         return jsInputDeviceMgr->GetKeyboardRepeatDelay(env);
626     }
627     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
628         MMI_HILOGE("Callback parameter type is invalid");
629         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
630         return nullptr;
631     }
632     return jsInputDeviceMgr->GetKeyboardRepeatDelay(env, argv[0]);
633 }
634 
GetKeyboardRepeatRate(napi_env env,napi_callback_info info)635 napi_value JsInputDeviceContext::GetKeyboardRepeatRate(napi_env env, napi_callback_info info)
636 {
637     CALL_DEBUG_ENTER;
638     size_t argc = 1;
639     napi_value argv[1] = { 0 };
640     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
641     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
642     CHKPP(jsDev);
643     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
644     CHKPP(jsInputDeviceMgr);
645     if (argc < 1) {
646         return jsInputDeviceMgr->GetKeyboardRepeatRate(env);
647     }
648     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
649         MMI_HILOGE("Callback parameter type is invalid");
650         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
651         return nullptr;
652     }
653     return jsInputDeviceMgr->GetKeyboardRepeatRate(env, argv[0]);
654 }
655 
GetIntervalSinceLastInput(napi_env env,napi_callback_info info)656 napi_value JsInputDeviceContext::GetIntervalSinceLastInput(napi_env env, napi_callback_info info)
657 {
658     CALL_DEBUG_ENTER;
659     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
660     CHKPP(jsDev);
661     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
662     CHKPP(jsInputDeviceMgr);
663     return jsInputDeviceMgr->GetIntervalSinceLastInput(env);
664 }
665 
SetInputDeviceEnabled(napi_env env,napi_callback_info info)666 napi_value JsInputDeviceContext::SetInputDeviceEnabled(napi_env env, napi_callback_info info)
667 {
668     CALL_DEBUG_ENTER;
669     size_t argc = 2;
670     napi_value argv[2] = { 0 };
671     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
672     if (argc < INPUT_PARAMETER) {
673         MMI_HILOGE("At least 2 parameter is required");
674         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
675         return nullptr;
676     }
677 
678     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
679         MMI_HILOGE("Rows parameter type is invalid");
680         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
681         return nullptr;
682     }
683     int32_t deviceId = -1;
684     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
685     if (deviceId < 0) {
686         MMI_HILOGE("Invalid deviceId");
687         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "deviceId is invalid");
688         return nullptr;
689     }
690 
691     if (!JsUtil::TypeOf(env, argv[1], napi_boolean)) {
692         MMI_HILOGE("enable parameter type is invalid");
693         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "enable", "boolean");
694         return nullptr;
695     }
696     bool enable = true;
697     CHKRP(napi_get_value_bool(env, argv[1], &enable), GET_VALUE_BOOL);
698 
699     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
700     CHKPP(jsIds);
701     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
702     CHKPP(jsInputDeviceMgr);
703     return jsInputDeviceMgr->SetInputDeviceEnabled(env, deviceId, enable);
704 }
705 
SetFunctionKeyEnabled(napi_env env,napi_callback_info info)706 napi_value JsInputDeviceContext::SetFunctionKeyEnabled(napi_env env, napi_callback_info info)
707 {
708     CALL_DEBUG_ENTER;
709     size_t argc = 2;
710     size_t count = 2;
711     napi_value argv[2] = { 0 };
712     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
713     if (argc < count) {
714         MMI_HILOGE("At least 2 parameter is required");
715         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
716         return nullptr;
717     }
718     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
719         MMI_HILOGE("First parameter type error");
720         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
721         return nullptr;
722     }
723     int32_t funcKey = -1;
724     CHKRP(napi_get_value_int32(env, argv[0], &funcKey), GET_VALUE_INT32);
725     if (funcKey != FunctionKey::FUNCTION_KEY_CAPSLOCK) {
726         MMI_HILOGE("First parameter value error");
727         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
728         return nullptr;
729     }
730     if (!JsUtil::TypeOf(env, argv[1], napi_boolean)) {
731         MMI_HILOGE("Second parameter type error");
732         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "state", "boolean");
733         return nullptr;
734     }
735     bool state = false;
736     CHKRP(napi_get_value_bool(env, argv[1], &state), GET_VALUE_BOOL);
737     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
738     CHKPP(jsDev);
739     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
740     CHKPP(jsInputDeviceMgr);
741     return jsInputDeviceMgr->SetFunctionKeyEnabled(env, funcKey, state);
742 }
743 
IsFunctionKeyEnabled(napi_env env,napi_callback_info info)744 napi_value JsInputDeviceContext::IsFunctionKeyEnabled(napi_env env, napi_callback_info info)
745 {
746     CALL_DEBUG_ENTER;
747     size_t argc = 1;
748     napi_value argv[1] = { 0 };
749     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
750     if (argc < 1) {
751         MMI_HILOGE("At least 1 parameter is required");
752         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
753         return nullptr;
754     }
755     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
756         MMI_HILOGE("First parameter type error");
757         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
758         return nullptr;
759     }
760     int32_t funcKey = -1;
761     CHKRP(napi_get_value_int32(env, argv[0], &funcKey), GET_VALUE_INT32);
762     if (funcKey != FunctionKey::FUNCTION_KEY_CAPSLOCK) {
763         MMI_HILOGE("First parameter value error");
764         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
765         return nullptr;
766     }
767     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
768     CHKPP(jsDev);
769     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
770     CHKPP(jsInputDeviceMgr);
771     return jsInputDeviceMgr->IsFunctionKeyEnabled(env, funcKey);
772 }
773 
EnumClassConstructor(napi_env env,napi_callback_info info)774 napi_value JsInputDeviceContext::EnumClassConstructor(napi_env env, napi_callback_info info)
775 {
776     CALL_DEBUG_ENTER;
777     size_t argc = 0;
778     napi_value args[1] = { 0 };
779     napi_value ret = nullptr;
780     void *data = nullptr;
781     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
782     return ret;
783 }
784 
CreateEnumKeyboardType(napi_env env,napi_value exports)785 napi_value JsInputDeviceContext::CreateEnumKeyboardType(napi_env env, napi_value exports)
786 {
787     CALL_DEBUG_ENTER;
788     napi_value none = nullptr;
789     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_NONE, &none), CREATE_INT32);
790     napi_value unknown = nullptr;
791     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_UNKNOWN, &unknown), CREATE_INT32);
792     napi_value alphabeticKeyboard = nullptr;
793     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD, &alphabeticKeyboard), CREATE_INT32);
794     napi_value digitalKeyboard = nullptr;
795     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD, &digitalKeyboard), CREATE_INT32);
796     napi_value handwritingPen = nullptr;
797     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN, &handwritingPen), CREATE_INT32);
798     napi_value remoteControl = nullptr;
799     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_REMOTECONTROL, &remoteControl), CREATE_INT32);
800     napi_property_descriptor desc[] = {
801         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
802         DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN", unknown),
803         DECLARE_NAPI_STATIC_PROPERTY("ALPHABETIC_KEYBOARD", alphabeticKeyboard),
804         DECLARE_NAPI_STATIC_PROPERTY("DIGITAL_KEYBOARD", digitalKeyboard),
805         DECLARE_NAPI_STATIC_PROPERTY("HANDWRITING_PEN", handwritingPen),
806         DECLARE_NAPI_STATIC_PROPERTY("REMOTE_CONTROL", remoteControl),
807     };
808     napi_value result = nullptr;
809     CHKRP(napi_define_class(env, "KeyboardType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
810         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
811     CHKRP(napi_set_named_property(env, exports, "KeyboardType", result), SET_NAMED_PROPERTY);
812     return exports;
813 }
814 
CreateEnumFunctionKey(napi_env env,napi_value exports)815 napi_value JsInputDeviceContext::CreateEnumFunctionKey(napi_env env, napi_value exports)
816 {
817     CALL_DEBUG_ENTER;
818     napi_value capsLock = nullptr;
819     CHKRP(napi_create_int32(env, FunctionKey::FUNCTION_KEY_CAPSLOCK, &capsLock), CREATE_INT32);
820     napi_property_descriptor desc[] = {
821         DECLARE_NAPI_STATIC_PROPERTY("CAPS_LOCK", capsLock),
822     };
823     napi_value result = nullptr;
824     CHKRP(napi_define_class(env, "FunctionKey", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
825         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
826     CHKRP(napi_set_named_property(env, exports, "FunctionKey", result), SET_NAMED_PROPERTY);
827     return exports;
828 }
829 
Export(napi_env env,napi_value exports)830 napi_value JsInputDeviceContext::Export(napi_env env, napi_value exports)
831 {
832     CALL_DEBUG_ENTER;
833     CHKPP(CreateInstance(env));
834     napi_property_descriptor desc[] = {
835         DECLARE_NAPI_STATIC_FUNCTION("on", On),
836         DECLARE_NAPI_STATIC_FUNCTION("off", Off),
837         DECLARE_NAPI_STATIC_FUNCTION("getDevice", GetDevice),
838         DECLARE_NAPI_STATIC_FUNCTION("getDeviceIds", GetDeviceIds),
839         DECLARE_NAPI_STATIC_FUNCTION("supportKeys", SupportKeys),
840         DECLARE_NAPI_STATIC_FUNCTION("supportKeysSync", SupportKeysSync),
841         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardType", GetKeyboardType),
842         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardTypeSync", GetKeyboardTypeSync),
843         DECLARE_NAPI_STATIC_FUNCTION("getDeviceList", GetDeviceList),
844         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfo", GetDeviceInfo),
845         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfoSync", GetDeviceInfoSync),
846         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatDelay", SetKeyboardRepeatDelay),
847         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatRate", SetKeyboardRepeatRate),
848         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatDelay", GetKeyboardRepeatDelay),
849         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatRate", GetKeyboardRepeatRate),
850         DECLARE_NAPI_STATIC_FUNCTION("getIntervalSinceLastInput", GetIntervalSinceLastInput),
851         DECLARE_NAPI_STATIC_FUNCTION("setInputDeviceEnabled", SetInputDeviceEnabled),
852         DECLARE_NAPI_STATIC_FUNCTION("setFunctionKeyEnabled", SetFunctionKeyEnabled),
853         DECLARE_NAPI_STATIC_FUNCTION("isFunctionKeyEnabled", IsFunctionKeyEnabled),
854     };
855     CHKRP(napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc), DEFINE_PROPERTIES);
856     CHKPP(CreateEnumKeyboardType(env, exports));
857     CHKPP(CreateEnumFunctionKey(env, exports));
858     return exports;
859 }
860 } // namespace MMI
861 } // namespace OHOS
862