1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_input_device_manager.h"
17
18 #include "input_device_impl.h"
19 #include "util_napi_error.h"
20
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsInputDeviceManager" };
25 std::mutex mutex_;
26 } // namespace
RegisterDevListener(napi_env env,const std::string & type,napi_value handle)27 void JsInputDeviceManager::RegisterDevListener(napi_env env, const std::string &type, napi_value handle)
28 {
29 CALL_DEBUG_ENTER;
30 AddListener(env, type, handle);
31 }
32
UnregisterDevListener(napi_env env,const std::string & type,napi_value handle)33 void JsInputDeviceManager::UnregisterDevListener(napi_env env, const std::string &type, napi_value handle)
34 {
35 CALL_DEBUG_ENTER;
36 RemoveListener(env, type, handle);
37 }
38
GetDeviceIds(napi_env env,napi_value handle)39 napi_value JsInputDeviceManager::GetDeviceIds(napi_env env, napi_value handle)
40 {
41 CALL_DEBUG_ENTER;
42 std::lock_guard<std::mutex> guard(mutex_);
43 int32_t userData = InputDevImpl.GetUserData();
44 napi_value ret = CreateCallbackInfo(env, handle, userData);
45 auto callback = std::bind(EmitJsIds, userData, std::placeholders::_1);
46 InputMgr->GetDeviceIds(callback);
47 return ret;
48 }
49
GetDevice(napi_env env,int32_t id,napi_value handle)50 napi_value JsInputDeviceManager::GetDevice(napi_env env, int32_t id, napi_value handle)
51 {
52 CALL_DEBUG_ENTER;
53 std::lock_guard<std::mutex> guard(mutex_);
54 int32_t userData = InputDevImpl.GetUserData();
55 napi_value ret = CreateCallbackInfo(env, handle, userData);
56 auto callback = std::bind(EmitJsDev, userData, std::placeholders::_1);
57 InputMgr->GetDevice(id, callback);
58 return ret;
59 }
60
SupportKeys(napi_env env,int32_t id,std::vector<int32_t> & keyCodes,napi_value handle)61 napi_value JsInputDeviceManager::SupportKeys(napi_env env, int32_t id, std::vector<int32_t> &keyCodes,
62 napi_value handle)
63 {
64 CALL_DEBUG_ENTER;
65 std::lock_guard<std::mutex> guard(mutex_);
66 int32_t userData = InputDevImpl.GetUserData();
67 napi_value ret = CreateCallbackInfo(env, handle, userData);
68 auto callback = std::bind(EmitSupportKeys, userData, std::placeholders::_1);
69 int32_t napiCode = InputMgr->SupportKeys(id, keyCodes, callback);
70 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
71 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
72 }
73 if (napiCode != RET_OK) {
74 JsEventTarget::RemoveCallbackInfo(env, handle, userData);
75 }
76 return ret;
77 }
78
GetKeyboardType(napi_env env,int32_t id,napi_value handle)79 napi_value JsInputDeviceManager::GetKeyboardType(napi_env env, int32_t id, napi_value handle)
80 {
81 CALL_DEBUG_ENTER;
82 std::lock_guard<std::mutex> guard(mutex_);
83 int32_t userData = InputDevImpl.GetUserData();
84 napi_value ret = CreateCallbackInfo(env, handle, userData);
85 auto callback = std::bind(EmitJsKeyboardType, userData, std::placeholders::_1);
86 int32_t napiCode = InputMgr->GetKeyboardType(id, callback);
87 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
88 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
89 }
90 if (napiCode != RET_OK) {
91 JsEventTarget::RemoveCallbackInfo(env, handle, userData);
92 }
93 return ret;
94 }
95
GetDeviceList(napi_env env,napi_value handle)96 napi_value JsInputDeviceManager::GetDeviceList(napi_env env, napi_value handle)
97 {
98 CALL_DEBUG_ENTER;
99 std::lock_guard<std::mutex> guard(mutex_);
100 int32_t userData = InputDevImpl.GetUserData();
101 napi_value ret = CreateCallbackInfo(env, handle, userData, true);
102 auto callback = std::bind(EmitJsIds, userData, std::placeholders::_1);
103 int32_t napiCode = InputMgr->GetDeviceIds(callback);
104 if (napiCode != RET_OK) {
105 JsEventTarget::RemoveCallbackInfo(env, handle, userData);
106 }
107 return ret;
108 }
109
GetDeviceInfo(napi_env env,int32_t id,napi_value handle)110 napi_value JsInputDeviceManager::GetDeviceInfo(napi_env env, int32_t id, napi_value handle)
111 {
112 CALL_DEBUG_ENTER;
113 std::lock_guard<std::mutex> guard(mutex_);
114 int32_t userData = InputDevImpl.GetUserData();
115 napi_value ret = CreateCallbackInfo(env, handle, userData, true);
116 auto callback = std::bind(EmitJsDev, userData, std::placeholders::_1);
117 int32_t napiCode = InputMgr->GetDevice(id, callback);
118 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
119 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
120 }
121 if (napiCode != RET_OK) {
122 JsEventTarget::RemoveCallbackInfo(env, handle, userData);
123 }
124 return ret;
125 }
126
ResetEnv()127 void JsInputDeviceManager::ResetEnv()
128 {
129 CALL_DEBUG_ENTER;
130 JsEventTarget::ResetEnv();
131 }
132 } // namespace MMI
133 } // namespace OHOS