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 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "JsInputDeviceManager"
23
24 namespace OHOS {
25 namespace MMI {
26
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 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
43 CHKPP(cb);
44 napi_value ret = CreateCallbackInfo(env, handle, cb);
45 auto callback = [cb] (std::vector<int32_t> &ids) { return EmitJsIds(cb, ids); };
46 InputManager::GetInstance()->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 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
54 CHKPP(cb);
55 napi_value ret = CreateCallbackInfo(env, handle, cb);
56 auto callback = [cb] (std::shared_ptr<InputDevice> device) { return EmitJsDev(cb, device); };
57 InputManager::GetInstance()->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 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
66 CHKPP(cb);
67 napi_value ret = CreateCallbackInfo(env, handle, cb);
68 auto callback = [cb] (std::vector<bool> &keystrokeAbility) { return EmitSupportKeys(cb, keystrokeAbility); };
69 int32_t napiCode = InputManager::GetInstance()->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 return ret;
74 }
75
SupportKeysSyncCallback(napi_env env,napi_value * result,std::vector<bool> & isSupported)76 void JsInputDeviceManager::SupportKeysSyncCallback(napi_env env, napi_value* result, std::vector<bool> &isSupported)
77 {
78 CALL_DEBUG_ENTER;
79 napi_create_array(env, &(*result));
80 for (uint i = 0; i < isSupported.size(); i++) {
81 napi_value value;
82 napi_get_boolean(env, isSupported[i], &value);
83 napi_set_element(env, *result, i, value);
84 }
85 }
86
SupportKeysSync(napi_env env,int32_t id,std::vector<int32_t> & keyCodes)87 napi_value JsInputDeviceManager::SupportKeysSync(napi_env env, int32_t id, std::vector<int32_t> &keyCodes)
88 {
89 CALL_DEBUG_ENTER;
90 napi_value result = nullptr;
91 auto callback = [env, &result] (std::vector<bool> &isSupported) {
92 return SupportKeysSyncCallback(env, &result, isSupported);
93 };
94 int32_t napiCode = InputManager::GetInstance()->SupportKeys(id, keyCodes, callback);
95 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
96 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
97 }
98 return result;
99 }
100
GetKeyboardType(napi_env env,int32_t id,napi_value handle)101 napi_value JsInputDeviceManager::GetKeyboardType(napi_env env, int32_t id, napi_value handle)
102 {
103 CALL_DEBUG_ENTER;
104 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
105 CHKPP(cb);
106 napi_value ret = CreateCallbackInfo(env, handle, cb);
107 auto callback = [cb] (int32_t keyboardType) { return EmitJsKeyboardType(cb, keyboardType); };
108 int32_t napiCode = InputManager::GetInstance()->GetKeyboardType(id, callback);
109 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
110 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
111 }
112 return ret;
113 }
114
GetKeyboardTypeSyncCallback(napi_env env,napi_value * result,int32_t keyboardType)115 void JsInputDeviceManager::GetKeyboardTypeSyncCallback(napi_env env, napi_value* result, int32_t keyboardType)
116 {
117 CALL_DEBUG_ENTER;
118 auto status = napi_create_int32(env, keyboardType, &(*result));
119 if (status != napi_ok) {
120 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Get keyboard type fail");
121 }
122 }
123
GetKeyboardTypeSync(napi_env env,int32_t id)124 napi_value JsInputDeviceManager::GetKeyboardTypeSync(napi_env env, int32_t id)
125 {
126 CALL_DEBUG_ENTER;
127 napi_value result = nullptr;
128 auto callback = [env, &result] (int32_t keyboardType) {
129 return GetKeyboardTypeSyncCallback(env, &result, keyboardType);
130 };
131 int32_t napiCode = InputManager::GetInstance()->GetKeyboardType(id, callback);
132 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
133 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
134 }
135 return result;
136 }
137
GetDeviceList(napi_env env,napi_value handle)138 napi_value JsInputDeviceManager::GetDeviceList(napi_env env, napi_value handle)
139 {
140 CALL_DEBUG_ENTER;
141 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
142 CHKPP(cb);
143 napi_value ret = CreateCallbackInfo(env, handle, cb);
144 auto callback = [cb] (std::vector<int32_t> &ids) { return EmitJsIds(cb, ids); };
145 InputManager::GetInstance()->GetDeviceIds(callback);
146 return ret;
147 }
148
GetDeviceInfo(napi_env env,int32_t id,napi_value handle)149 napi_value JsInputDeviceManager::GetDeviceInfo(napi_env env, int32_t id, napi_value handle)
150 {
151 CALL_DEBUG_ENTER;
152 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
153 CHKPP(cb);
154 napi_value ret = CreateCallbackInfo(env, handle, cb);
155 auto callback = [cb] (std::shared_ptr<InputDevice> inputDevice) { return EmitJsDev(cb, inputDevice); };
156 int32_t napiCode = InputManager::GetInstance()->GetDevice(id, callback);
157 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
158 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
159 }
160 return ret;
161 }
162
GetDeviceInfoSyncCallback(napi_env env,napi_value * result,sptr<JsUtil::CallbackInfo> cb,std::shared_ptr<InputDevice> inputDevice)163 void JsInputDeviceManager::GetDeviceInfoSyncCallback(napi_env env, napi_value* result, sptr<JsUtil::CallbackInfo> cb,
164 std::shared_ptr<InputDevice> inputDevice)
165 {
166 CALL_DEBUG_ENTER;
167 auto status = napi_create_object(env, &(*result));
168 if (status != napi_ok) {
169 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "create object fail");
170 }
171
172 cb->env = env;
173 cb->data.device = inputDevice;
174 *result = JsUtil::GetDeviceInfo(cb);
175 if (*result == nullptr) {
176 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "GetDeviceInfo fail");
177 }
178 }
179
GetDeviceInfoSync(napi_env env,int32_t id,napi_value handle)180 napi_value JsInputDeviceManager::GetDeviceInfoSync(napi_env env, int32_t id, napi_value handle)
181 {
182 CALL_DEBUG_ENTER;
183 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
184 CHKPP(cb);
185 CreateCallbackInfo(env, handle, cb);
186 napi_value result = nullptr;
187 auto callback = [env, &result, cb] (std::shared_ptr<InputDevice> inputDevice) {
188 return GetDeviceInfoSyncCallback(env, &result, cb, inputDevice);
189 };
190 int32_t napiCode = InputManager::GetInstance()->GetDevice(id, callback);
191 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
192 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
193 }
194 return result;
195 }
196
SetKeyboardRepeatDelay(napi_env env,int32_t delay,napi_value handle)197 napi_value JsInputDeviceManager::SetKeyboardRepeatDelay(napi_env env, int32_t delay, napi_value handle)
198 {
199 CALL_DEBUG_ENTER;
200 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
201 CHKPP(cb);
202 napi_value ret = CreateCallbackInfo(env, handle, cb);
203 int32_t napiCode = InputManager::GetInstance()->SetKeyboardRepeatDelay(delay);
204 EmitJsSetKeyboardRepeatDelay(cb, napiCode);
205 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
206 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
207 }
208 return ret;
209 }
210
SetKeyboardRepeatRate(napi_env env,int32_t rate,napi_value handle)211 napi_value JsInputDeviceManager::SetKeyboardRepeatRate(napi_env env, int32_t rate, napi_value handle)
212 {
213 CALL_DEBUG_ENTER;
214 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
215 CHKPP(cb);
216 napi_value ret = CreateCallbackInfo(env, handle, cb);
217 int32_t napiCode = InputManager::GetInstance()->SetKeyboardRepeatRate(rate);
218 EmitJsSetKeyboardRepeatRate(cb, napiCode);
219 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
220 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
221 }
222 return ret;
223 }
224
GetKeyboardRepeatDelay(napi_env env,napi_value handle)225 napi_value JsInputDeviceManager::GetKeyboardRepeatDelay(napi_env env, napi_value handle)
226 {
227 CALL_DEBUG_ENTER;
228 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
229 CHKPP(cb);
230 napi_value ret = CreateCallbackInfo(env, handle, cb);
231 auto callback = [cb] (int32_t delay) { return EmitJsKeyboardRepeatDelay(cb, delay); };
232 int32_t napiCode = InputManager::GetInstance()->GetKeyboardRepeatDelay(callback);
233 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
234 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
235 }
236 return ret;
237 }
238
GetKeyboardRepeatRate(napi_env env,napi_value handle)239 napi_value JsInputDeviceManager::GetKeyboardRepeatRate(napi_env env, napi_value handle)
240 {
241 CALL_DEBUG_ENTER;
242 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
243 CHKPP(cb);
244 napi_value ret = CreateCallbackInfo(env, handle, cb);
245 auto callback = [cb] (int32_t rate) { return EmitJsKeyboardRepeatRate(cb, rate); };
246 int32_t napiCode = InputManager::GetInstance()->GetKeyboardRepeatRate(callback);
247 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
248 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
249 }
250 return ret;
251 }
252
GetIntervalSinceLastInput(napi_env env)253 napi_value JsInputDeviceManager::GetIntervalSinceLastInput(napi_env env)
254 {
255 CALL_DEBUG_ENTER;
256 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
257 CHKPP(cb);
258 napi_value ret = CreateCallbackInfo(env, nullptr, cb);
259 int64_t timeInterval = -1;
260 int32_t napiCode = InputManager::GetInstance()->GetIntervalSinceLastInput(timeInterval);
261 EmitJsGetIntervalSinceLastInput(cb, timeInterval);
262 if (napiCode != RET_OK) {
263 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid get interval since last input");
264 }
265 return ret;
266 }
267
ResetEnv()268 void JsInputDeviceManager::ResetEnv()
269 {
270 CALL_DEBUG_ENTER;
271 JsEventTarget::ResetEnv();
272 }
273 } // namespace MMI
274 } // namespace OHOS