• 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         MMI_HILOGE("Failed to wrap native instance");
102         return nullptr;
103     }
104     return thisVar;
105 }
106 
GetInstance(napi_env env)107 JsInputDeviceContext* JsInputDeviceContext::GetInstance(napi_env env)
108 {
109     CALL_DEBUG_ENTER;
110     napi_value global = nullptr;
111     CHKRP(napi_get_global(env, &global), GET_GLOBAL);
112 
113     bool result = false;
114     CHKRP(napi_has_named_property(env, global, "multimodal_input_device", &result), HAS_NAMED_PROPERTY);
115     if (!result) {
116         MMI_HILOGE("multimodal_input_device was not found");
117         return nullptr;
118     }
119 
120     napi_value object = nullptr;
121     napi_handle_scope scope = nullptr;
122     napi_open_handle_scope(env, &scope);
123     CHKRP(napi_get_named_property(env, global, "multimodal_input_device", &object), GET_NAMED_PROPERTY);
124     CHKPP(object);
125 
126     JsInputDeviceContext *instance = nullptr;
127     CHKRP(napi_unwrap(env, object, (void**)&instance), UNWRAP);
128     CHKPP(instance);
129     napi_close_handle_scope(env, scope);
130     return instance;
131 }
132 
GetJsInputDeviceMgr() const133 std::shared_ptr<JsInputDeviceManager> JsInputDeviceContext::GetJsInputDeviceMgr() const
134 {
135     return mgr_;
136 }
137 
On(napi_env env,napi_callback_info info)138 napi_value JsInputDeviceContext::On(napi_env env, napi_callback_info info)
139 {
140     CALL_DEBUG_ENTER;
141     size_t argc = 2;
142     napi_value argv[2] = { 0 };
143     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
144     if (argc < 1) {
145         MMI_HILOGE("Require two parameters");
146         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
147         return nullptr;
148     }
149     if (!JsUtil::TypeOf(env, argv[0], napi_string)) {
150         MMI_HILOGE("First parameter type error");
151         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string");
152         return nullptr;
153     }
154 
155     char eventType[MAX_STRING_LEN] = { 0 };
156     size_t ret = 0;
157     CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, MAX_STRING_LEN - 1, &ret), GET_VALUE_STRING_UTF8);
158     std::string type = eventType;
159     if (type != CHANGED_TYPE) {
160         MMI_HILOGE("Type is not change");
161         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be change");
162         return nullptr;
163     }
164     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
165         MMI_HILOGE("Second parameter type error");
166         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "listener", "function");
167         return nullptr;
168     }
169 
170     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
171     CHKPP(jsIds);
172     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
173     jsInputDeviceMgr->RegisterDevListener(env, type, argv[1]);
174     return nullptr;
175 }
176 
Off(napi_env env,napi_callback_info info)177 napi_value JsInputDeviceContext::Off(napi_env env, napi_callback_info info)
178 {
179     CALL_DEBUG_ENTER;
180     size_t argc = 2;
181     napi_value argv[2] = { 0 };
182     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
183     if (argc < 1) {
184         MMI_HILOGE("Require two parameters");
185         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
186         return nullptr;
187     }
188     if (!JsUtil::TypeOf(env, argv[0], napi_string)) {
189         MMI_HILOGE("First parameter type error");
190         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string");
191         return nullptr;
192     }
193 
194     char eventType[MAX_STRING_LEN] = { 0 };
195     size_t ret = 0;
196     CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, MAX_STRING_LEN - 1, &ret), GET_VALUE_STRING_UTF8);
197     std::string type = eventType;
198     if (type != CHANGED_TYPE) {
199         MMI_HILOGE("Type is not change");
200         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be change");
201         return nullptr;
202     }
203 
204     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
205     CHKPP(jsIds);
206     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
207     if (argc == 1) {
208         jsInputDeviceMgr->UnregisterDevListener(env, type);
209         return nullptr;
210     }
211     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
212         MMI_HILOGE("Second parameter type error");
213         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "listener", "function");
214         return nullptr;
215     }
216     jsInputDeviceMgr->UnregisterDevListener(env, type, argv[1]);
217     return nullptr;
218 }
219 
GetDeviceIds(napi_env env,napi_callback_info info)220 napi_value JsInputDeviceContext::GetDeviceIds(napi_env env, napi_callback_info info)
221 {
222     CALL_DEBUG_ENTER;
223     size_t argc = 1;
224     napi_value argv[1] = { 0 };
225     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
226     if (argc > 1) {
227         THROWERR(env, "too many parameters");
228         return nullptr;
229     }
230 
231     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
232     CHKPP(jsIds);
233     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
234     if (argc < 1) {
235         return jsInputDeviceMgr->GetDeviceIds(env);
236     }
237     if (!JsUtil::TypeOf(env, argv[0], napi_function)) {
238         THROWERR(env, "The first parameter type is wrong");
239         return nullptr;
240     }
241     return jsInputDeviceMgr->GetDeviceIds(env, argv[0]);
242 }
243 
GetDevice(napi_env env,napi_callback_info info)244 napi_value JsInputDeviceContext::GetDevice(napi_env env, napi_callback_info info)
245 {
246     CALL_DEBUG_ENTER;
247     size_t argc = 2;
248     napi_value argv[2] = { 0 };
249     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
250     if (argc < 1 || argc > INPUT_PARAMETER) {
251         THROWERR(env, "the number of parameters is not as expected");
252         return nullptr;
253     }
254     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
255         THROWERR(env, "The first parameter type is wrong");
256         return nullptr;
257     }
258     int32_t id = 0;
259     CHKRP(napi_get_value_int32(env, argv[0], &id), GET_VALUE_INT32);
260     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
261     CHKPP(jsDev);
262     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
263     if (argc == 1) {
264         return jsInputDeviceMgr->GetDevice(env, id);
265     }
266     if (!JsUtil::TypeOf(env, argv[1], napi_function)) {
267         THROWERR(env, "Second parameter type is wrong");
268         return nullptr;
269     }
270     return jsInputDeviceMgr->GetDevice(env, id, argv[1]);
271 }
272 
SupportKeys(napi_env env,napi_callback_info info)273 napi_value JsInputDeviceContext::SupportKeys(napi_env env, napi_callback_info info)
274 {
275     CALL_DEBUG_ENTER;
276     size_t argc = 3;
277     napi_value argv[3] = { 0 };
278     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
279     if (argc < INPUT_PARAMETER) {
280         MMI_HILOGE("Require three parameters");
281         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
282         return nullptr;
283     }
284 
285     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
286         MMI_HILOGE("First parameter type error");
287         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
288         return nullptr;
289     }
290     int32_t deviceId = 0;
291     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
292 
293     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
294         MMI_HILOGE("Second parameter type error");
295         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
296         return nullptr;
297     }
298     uint32_t size = 0;
299     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
300     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
301         MMI_HILOGE("Size range error");
302         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
303         return nullptr;
304     }
305 
306     int32_t data = 0;
307     std::vector<int32_t> keyCodes;
308     for (uint32_t i = 0; i < size; ++i) {
309         napi_value keyValue = nullptr;
310         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
311         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
312             MMI_HILOGE("Second parameter type error");
313             THROWERR_API9(env, COMMON_PARAMETER_ERROR, "KeyCode", "number");
314             return nullptr;
315         }
316         CHKRP(napi_get_value_int32(env, keyValue, &data), GET_VALUE_INT32);
317         keyCodes.push_back(data);
318     }
319 
320     JsInputDeviceContext *jsContext = JsInputDeviceContext::GetInstance(env);
321     CHKPP(jsContext);
322     auto jsInputDeviceMgr = jsContext->GetJsInputDeviceMgr();
323     if (argc == INPUT_PARAMETER) {
324         return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes);
325     }
326     if (!JsUtil::TypeOf(env, argv[2], napi_function)) {
327         MMI_HILOGE("Third parameter type error");
328         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function");
329         return nullptr;
330     }
331     return jsInputDeviceMgr->SupportKeys(env, deviceId, keyCodes, argv[2]);
332 }
333 
SupportKeysSync(napi_env env,napi_callback_info info)334 napi_value JsInputDeviceContext::SupportKeysSync(napi_env env, napi_callback_info info)
335 {
336     CALL_DEBUG_ENTER;
337     size_t argc = ARGC_NUM;
338     napi_value argv[ARGC_NUM] = { 0 };
339     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
340     if (argc != ARGC_NUM) {
341         MMI_HILOGE("Require two parameters");
342         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
343         return nullptr;
344     }
345 
346     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
347         MMI_HILOGE("First parameter type error");
348         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
349         return nullptr;
350     }
351     int32_t deviceId = 0;
352     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
353 
354     if (!JsUtil::TypeOf(env, argv[1], napi_object)) {
355         MMI_HILOGE("Second parameter type error");
356         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keys", "array");
357         return nullptr;
358     }
359     uint32_t size = 0;
360     CHKRP(napi_get_array_length(env, argv[1], &size), GET_ARRAY_LENGTH);
361     if (size < MIN_N_SIZE || size > MAX_N_SIZE) {
362         MMI_HILOGE("Size range error");
363         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "size range error");
364         return nullptr;
365     }
366 
367     int32_t data = 0;
368     std::vector<int32_t> keyCodes;
369     for (uint32_t i = 0; i < size; ++i) {
370         napi_value keyValue = nullptr;
371         CHKRP(napi_get_element(env, argv[1], i, &keyValue), GET_ELEMENT);
372         if (!JsUtil::TypeOf(env, keyValue, napi_number)) {
373             MMI_HILOGE("Second parameter type error");
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 < 1) {
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 < 1) {
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 < 1) {
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 < 1) {
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("The 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 < 1) {
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("The 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 < 1) {
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 < 1) {
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 
SetInputDeviceEnabled(napi_env env,napi_callback_info info)654 napi_value JsInputDeviceContext::SetInputDeviceEnabled(napi_env env, napi_callback_info info)
655 {
656     CALL_DEBUG_ENTER;
657     size_t argc = 2;
658     napi_value argv[2] = { 0 };
659     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
660     if (argc < INPUT_PARAMETER) {
661         MMI_HILOGE("At least 2 parameter is required");
662         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
663         return nullptr;
664     }
665 
666     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
667         MMI_HILOGE("Rows parameter type is invalid");
668         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "deviceId", "number");
669         return nullptr;
670     }
671     int32_t deviceId = -1;
672     CHKRP(napi_get_value_int32(env, argv[0], &deviceId), GET_VALUE_INT32);
673     if (deviceId < 0) {
674         MMI_HILOGE("Invalid deviceId");
675         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "deviceId is invalid");
676         return nullptr;
677     }
678 
679     if (!JsUtil::TypeOf(env, argv[1], napi_boolean)) {
680         MMI_HILOGE("enable parameter type is invalid");
681         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "enable", "boolean");
682         return nullptr;
683     }
684     bool enable = true;
685     CHKRP(napi_get_value_bool(env, argv[1], &enable), GET_VALUE_BOOL);
686 
687     JsInputDeviceContext *jsIds = JsInputDeviceContext::GetInstance(env);
688     CHKPP(jsIds);
689     auto jsInputDeviceMgr = jsIds->GetJsInputDeviceMgr();
690     return jsInputDeviceMgr->SetInputDeviceEnabled(env, deviceId, enable);
691 }
692 
SetFunctionKeyEnabled(napi_env env,napi_callback_info info)693 napi_value JsInputDeviceContext::SetFunctionKeyEnabled(napi_env env, napi_callback_info info)
694 {
695     CALL_DEBUG_ENTER;
696     size_t argc = 2;
697     size_t count = 2;
698     napi_value argv[2] = { 0 };
699     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
700     if (argc < count) {
701         MMI_HILOGE("At least 2 parameter is required");
702         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
703         return nullptr;
704     }
705     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
706         MMI_HILOGE("First parameter type error");
707         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
708         return nullptr;
709     }
710     int32_t funcKey = -1;
711     CHKRP(napi_get_value_int32(env, argv[0], &funcKey), GET_VALUE_INT32);
712     if (funcKey != FunctionKey::FUNCTION_KEY_CAPSLOCK) {
713         MMI_HILOGE("First parameter value error");
714         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
715         return nullptr;
716     }
717     if (!JsUtil::TypeOf(env, argv[1], napi_boolean)) {
718         MMI_HILOGE("Second parameter type error");
719         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "state", "boolean");
720         return nullptr;
721     }
722     bool state = false;
723     CHKRP(napi_get_value_bool(env, argv[1], &state), GET_VALUE_BOOL);
724     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
725     CHKPP(jsDev);
726     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
727     CHKPP(jsInputDeviceMgr);
728     return jsInputDeviceMgr->SetFunctionKeyEnabled(env, funcKey, state);
729 }
730 
IsFunctionKeyEnabled(napi_env env,napi_callback_info info)731 napi_value JsInputDeviceContext::IsFunctionKeyEnabled(napi_env env, napi_callback_info info)
732 {
733     CALL_DEBUG_ENTER;
734     size_t argc = 1;
735     napi_value argv[1] = { 0 };
736     CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO);
737     if (argc < 1) {
738         MMI_HILOGE("At least 1 parameter is required");
739         THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error");
740         return nullptr;
741     }
742     if (!JsUtil::TypeOf(env, argv[0], napi_number)) {
743         MMI_HILOGE("First parameter type error");
744         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
745         return nullptr;
746     }
747     int32_t funcKey = -1;
748     CHKRP(napi_get_value_int32(env, argv[0], &funcKey), GET_VALUE_INT32);
749     if (funcKey != FunctionKey::FUNCTION_KEY_CAPSLOCK) {
750         MMI_HILOGE("First parameter value error");
751         THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funckey", "FunctionKey");
752         return nullptr;
753     }
754     JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env);
755     CHKPP(jsDev);
756     auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr();
757     CHKPP(jsInputDeviceMgr);
758     return jsInputDeviceMgr->IsFunctionKeyEnabled(env, funcKey);
759 }
760 
EnumClassConstructor(napi_env env,napi_callback_info info)761 napi_value JsInputDeviceContext::EnumClassConstructor(napi_env env, napi_callback_info info)
762 {
763     CALL_DEBUG_ENTER;
764     size_t argc = 0;
765     napi_value args[1] = { 0 };
766     napi_value ret = nullptr;
767     void *data = nullptr;
768     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
769     return ret;
770 }
771 
CreateEnumKeyboardType(napi_env env,napi_value exports)772 napi_value JsInputDeviceContext::CreateEnumKeyboardType(napi_env env, napi_value exports)
773 {
774     CALL_DEBUG_ENTER;
775     napi_value none = nullptr;
776     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_NONE, &none), CREATE_INT32);
777     napi_value unknown = nullptr;
778     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_UNKNOWN, &unknown), CREATE_INT32);
779     napi_value alphabeticKeyboard = nullptr;
780     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_ALPHABETICKEYBOARD, &alphabeticKeyboard), CREATE_INT32);
781     napi_value digitalKeyboard = nullptr;
782     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_DIGITALKEYBOARD, &digitalKeyboard), CREATE_INT32);
783     napi_value handwritingPen = nullptr;
784     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_HANDWRITINGPEN, &handwritingPen), CREATE_INT32);
785     napi_value remoteControl = nullptr;
786     CHKRP(napi_create_int32(env, KeyboardType::KEYBOARD_TYPE_REMOTECONTROL, &remoteControl), CREATE_INT32);
787     napi_property_descriptor desc[] = {
788         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
789         DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN", unknown),
790         DECLARE_NAPI_STATIC_PROPERTY("ALPHABETIC_KEYBOARD", alphabeticKeyboard),
791         DECLARE_NAPI_STATIC_PROPERTY("DIGITAL_KEYBOARD", digitalKeyboard),
792         DECLARE_NAPI_STATIC_PROPERTY("HANDWRITING_PEN", handwritingPen),
793         DECLARE_NAPI_STATIC_PROPERTY("REMOTE_CONTROL", remoteControl),
794     };
795     napi_value result = nullptr;
796     CHKRP(napi_define_class(env, "KeyboardType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
797         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
798     CHKRP(napi_set_named_property(env, exports, "KeyboardType", result), SET_NAMED_PROPERTY);
799     return exports;
800 }
801 
CreateEnumFunctionKey(napi_env env,napi_value exports)802 napi_value JsInputDeviceContext::CreateEnumFunctionKey(napi_env env, napi_value exports)
803 {
804     CALL_DEBUG_ENTER;
805     napi_value capsLock = nullptr;
806     CHKRP(napi_create_int32(env, FunctionKey::FUNCTION_KEY_CAPSLOCK, &capsLock), CREATE_INT32);
807     napi_property_descriptor desc[] = {
808         DECLARE_NAPI_STATIC_PROPERTY("CAPS_LOCK", capsLock),
809     };
810     napi_value result = nullptr;
811     CHKRP(napi_define_class(env, "FunctionKey", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
812         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
813     CHKRP(napi_set_named_property(env, exports, "FunctionKey", result), SET_NAMED_PROPERTY);
814     return exports;
815 }
816 
Export(napi_env env,napi_value exports)817 napi_value JsInputDeviceContext::Export(napi_env env, napi_value exports)
818 {
819     CALL_DEBUG_ENTER;
820     CHKPP(CreateInstance(env));
821     napi_property_descriptor desc[] = {
822         DECLARE_NAPI_STATIC_FUNCTION("on", On),
823         DECLARE_NAPI_STATIC_FUNCTION("off", Off),
824         DECLARE_NAPI_STATIC_FUNCTION("getDevice", GetDevice),
825         DECLARE_NAPI_STATIC_FUNCTION("getDeviceIds", GetDeviceIds),
826         DECLARE_NAPI_STATIC_FUNCTION("supportKeys", SupportKeys),
827         DECLARE_NAPI_STATIC_FUNCTION("supportKeysSync", SupportKeysSync),
828         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardType", GetKeyboardType),
829         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardTypeSync", GetKeyboardTypeSync),
830         DECLARE_NAPI_STATIC_FUNCTION("getDeviceList", GetDeviceList),
831         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfo", GetDeviceInfo),
832         DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfoSync", GetDeviceInfoSync),
833         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatDelay", SetKeyboardRepeatDelay),
834         DECLARE_NAPI_STATIC_FUNCTION("setKeyboardRepeatRate", SetKeyboardRepeatRate),
835         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatDelay", GetKeyboardRepeatDelay),
836         DECLARE_NAPI_STATIC_FUNCTION("getKeyboardRepeatRate", GetKeyboardRepeatRate),
837         DECLARE_NAPI_STATIC_FUNCTION("getIntervalSinceLastInput", GetIntervalSinceLastInput),
838         DECLARE_NAPI_STATIC_FUNCTION("setInputDeviceEnabled", SetInputDeviceEnabled),
839         DECLARE_NAPI_STATIC_FUNCTION("setFunctionKeyEnabled", SetFunctionKeyEnabled),
840         DECLARE_NAPI_STATIC_FUNCTION("isFunctionKeyEnabled", IsFunctionKeyEnabled),
841     };
842     CHKRP(napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc), DEFINE_PROPERTIES);
843     CHKPP(CreateEnumKeyboardType(env, exports));
844     CHKPP(CreateEnumFunctionKey(env, exports));
845     return exports;
846 }
847 } // namespace MMI
848 } // namespace OHOS
849