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 "util_napi_error.h"
19
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "JsInputDeviceManager"
22
23 namespace OHOS {
24 namespace MMI {
25
RegisterDevListener(napi_env env,const std::string & type,napi_value handle)26 void JsInputDeviceManager::RegisterDevListener(napi_env env, const std::string &type, napi_value handle)
27 {
28 CALL_DEBUG_ENTER;
29 AddListener(env, type, handle);
30 }
31
UnregisterDevListener(napi_env env,const std::string & type,napi_value handle)32 void JsInputDeviceManager::UnregisterDevListener(napi_env env, const std::string &type, napi_value handle)
33 {
34 CALL_DEBUG_ENTER;
35 RemoveListener(env, type, handle);
36 }
37
GetDeviceIds(napi_env env,napi_value handle)38 napi_value JsInputDeviceManager::GetDeviceIds(napi_env env, napi_value handle)
39 {
40 CALL_DEBUG_ENTER;
41 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
42 CHKPP(cb);
43 napi_value ret = CreateCallbackInfo(env, handle, cb);
44 EmitJsIds(cb, cb->data.ids);
45 return ret;
46 }
47
GetDevice(napi_env env,int32_t id,napi_value handle)48 napi_value JsInputDeviceManager::GetDevice(napi_env env, int32_t id, napi_value handle)
49 {
50 CALL_DEBUG_ENTER;
51 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
52 CHKPP(cb);
53 napi_value ret = CreateCallbackInfo(env, handle, cb);
54 EmitJsDev(cb, id);
55 return ret;
56 }
57
SupportKeys(napi_env env,int32_t id,std::vector<int32_t> & keyCodes,napi_value handle)58 napi_value JsInputDeviceManager::SupportKeys(napi_env env, int32_t id, std::vector<int32_t> &keyCodes,
59 napi_value handle)
60 {
61 CALL_DEBUG_ENTER;
62 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
63 CHKPP(cb);
64 napi_value ret = CreateCallbackInfo(env, handle, cb);
65 EmitSupportKeys(cb, keyCodes, id);
66 return ret;
67 }
68
SupportKeysSyncCallback(napi_env env,napi_value * result,std::vector<bool> & isSupported)69 void JsInputDeviceManager::SupportKeysSyncCallback(napi_env env, napi_value* result, std::vector<bool> &isSupported)
70 {
71 CALL_DEBUG_ENTER;
72 napi_create_array(env, &(*result));
73 for (uint i = 0; i < isSupported.size(); i++) {
74 napi_value value;
75 napi_get_boolean(env, isSupported[i], &value);
76 napi_set_element(env, *result, i, value);
77 }
78 }
79
SupportKeysSync(napi_env env,int32_t id,std::vector<int32_t> & keyCodes)80 napi_value JsInputDeviceManager::SupportKeysSync(napi_env env, int32_t id, std::vector<int32_t> &keyCodes)
81 {
82 CALL_DEBUG_ENTER;
83 napi_value result = nullptr;
84 auto callback = [env, &result] (std::vector<bool> &isSupported) {
85 return SupportKeysSyncCallback(env, &result, isSupported);
86 };
87 int32_t napiCode = InputManager::GetInstance()->SupportKeys(id, keyCodes, callback);
88 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
89 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
90 }
91 return result;
92 }
93
GetKeyboardType(napi_env env,int32_t id,napi_value handle)94 napi_value JsInputDeviceManager::GetKeyboardType(napi_env env, int32_t id, napi_value handle)
95 {
96 CALL_DEBUG_ENTER;
97 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
98 CHKPP(cb);
99 napi_value ret = CreateCallbackInfo(env, handle, cb);
100 EmitJsKeyboardType(cb, id);
101 return ret;
102 }
103
GetKeyboardTypeSyncCallback(napi_env env,napi_value * result,int32_t keyboardType)104 void JsInputDeviceManager::GetKeyboardTypeSyncCallback(napi_env env, napi_value* result, int32_t keyboardType)
105 {
106 CALL_DEBUG_ENTER;
107 auto status = napi_create_int32(env, keyboardType, &(*result));
108 if (status != napi_ok) {
109 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Get keyboard type fail");
110 }
111 }
112
GetKeyboardTypeSync(napi_env env,int32_t id)113 napi_value JsInputDeviceManager::GetKeyboardTypeSync(napi_env env, int32_t id)
114 {
115 CALL_DEBUG_ENTER;
116 napi_value result = nullptr;
117 auto callback = [env, &result] (int32_t keyboardType) {
118 return GetKeyboardTypeSyncCallback(env, &result, keyboardType);
119 };
120 int32_t napiCode = InputManager::GetInstance()->GetKeyboardType(id, callback);
121 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
122 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
123 }
124 return result;
125 }
126
GetDeviceList(napi_env env,napi_value handle)127 napi_value JsInputDeviceManager::GetDeviceList(napi_env env, napi_value handle)
128 {
129 CALL_DEBUG_ENTER;
130 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
131 CHKPP(cb);
132 napi_value ret = CreateCallbackInfo(env, handle, cb);
133 EmitJsIds(cb, cb->data.ids);
134 return ret;
135 }
136
GetDeviceInfo(napi_env env,int32_t id,napi_value handle)137 napi_value JsInputDeviceManager::GetDeviceInfo(napi_env env, int32_t id, napi_value handle)
138 {
139 CALL_DEBUG_ENTER;
140 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
141 CHKPP(cb);
142 napi_value ret = CreateCallbackInfo(env, handle, cb);
143 EmitJsDev(cb, id);
144 return ret;
145 }
146
GetDeviceInfoSyncCallback(napi_env env,napi_value * result,sptr<JsUtil::CallbackInfo> cb,std::shared_ptr<InputDevice> inputDevice)147 void JsInputDeviceManager::GetDeviceInfoSyncCallback(napi_env env, napi_value* result, sptr<JsUtil::CallbackInfo> cb,
148 std::shared_ptr<InputDevice> inputDevice)
149 {
150 CALL_DEBUG_ENTER;
151 auto status = napi_create_object(env, &(*result));
152 if (status != napi_ok) {
153 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "create object fail");
154 }
155 CHKPV(cb);
156 cb->env = env;
157 cb->data.device = inputDevice;
158 *result = JsUtil::GetDeviceInfo(cb);
159 if (*result == nullptr) {
160 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "GetDeviceInfo fail");
161 }
162 }
163
GetDeviceInfoSync(napi_env env,int32_t id,napi_value handle)164 napi_value JsInputDeviceManager::GetDeviceInfoSync(napi_env env, int32_t id, napi_value handle)
165 {
166 CALL_DEBUG_ENTER;
167 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
168 CHKPP(cb);
169 CreateCallbackInfo(env, handle, cb);
170 napi_value result = nullptr;
171 auto callback = [env, &result, cb] (std::shared_ptr<InputDevice> inputDevice) {
172 return GetDeviceInfoSyncCallback(env, &result, cb, inputDevice);
173 };
174 int32_t napiCode = InputManager::GetInstance()->GetDevice(id, callback);
175 if (napiCode != OTHER_ERROR && napiCode != RET_OK) {
176 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Invalid input device id");
177 }
178 return result;
179 }
180
SetKeyboardRepeatDelay(napi_env env,int32_t delay,napi_value handle)181 napi_value JsInputDeviceManager::SetKeyboardRepeatDelay(napi_env env, int32_t delay, napi_value handle)
182 {
183 CALL_DEBUG_ENTER;
184 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
185 CHKPP(cb);
186 napi_value ret = CreateCallbackInfo(env, handle, cb);
187 EmitJsSetKeyboardRepeatDelay(cb, delay);
188 return ret;
189 }
190
SetKeyboardRepeatRate(napi_env env,int32_t rate,napi_value handle)191 napi_value JsInputDeviceManager::SetKeyboardRepeatRate(napi_env env, int32_t rate, napi_value handle)
192 {
193 CALL_DEBUG_ENTER;
194 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
195 CHKPP(cb);
196 napi_value ret = CreateCallbackInfo(env, handle, cb);
197 EmitJsSetKeyboardRepeatRate(cb, rate);
198 return ret;
199 }
200
GetKeyboardRepeatDelay(napi_env env,napi_value handle)201 napi_value JsInputDeviceManager::GetKeyboardRepeatDelay(napi_env env, napi_value handle)
202 {
203 CALL_DEBUG_ENTER;
204 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
205 CHKPP(cb);
206 napi_value ret = CreateCallbackInfo(env, handle, cb);
207 EmitJsKeyboardRepeatDelay(cb, cb->data.keyboardRepeatDelay);
208 return ret;
209 }
210
GetKeyboardRepeatRate(napi_env env,napi_value handle)211 napi_value JsInputDeviceManager::GetKeyboardRepeatRate(napi_env env, 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 EmitJsKeyboardRepeatRate(cb, cb->data.keyboardRepeatRate);
218 return ret;
219 }
220
GetIntervalSinceLastInput(napi_env env)221 napi_value JsInputDeviceManager::GetIntervalSinceLastInput(napi_env env)
222 {
223 CALL_DEBUG_ENTER;
224 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
225 CHKPP(cb);
226 napi_value ret = CreateCallbackInfo(env, nullptr, cb);
227 EmitJsGetIntervalSinceLastInput(cb);
228 return ret;
229 }
230
ResetEnv()231 void JsInputDeviceManager::ResetEnv()
232 {
233 CALL_DEBUG_ENTER;
234 JsEventTarget::ResetEnv();
235 }
236
SetInputDeviceEnabled(napi_env env,int32_t deviceId,bool enable,napi_value handle)237 napi_value JsInputDeviceManager::SetInputDeviceEnabled(napi_env env, int32_t deviceId, bool enable, napi_value handle)
238 {
239 CALL_DEBUG_ENTER;
240 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
241 CHKPP(cb);
242 napi_value ret = CreateCallbackInfo(env, nullptr, cb);
243 auto callback = [cb] (int32_t errcode) { return EmitJsSetInputDeviceEnabled(cb, errcode); };
244 int32_t napiCode = InputManager::GetInstance()->SetInputDeviceEnabled(deviceId, enable, callback);
245 if (napiCode == ERROR_NOT_SYSAPI) {
246 MMI_HILOGE("System applications use only");
247 THROWERR_CUSTOM(env, ERROR_NOT_SYSAPI,
248 "System applications use only");
249 }
250 if (napiCode == ERROR_NO_PERMISSION) {
251 MMI_HILOGE("Permission denied");
252 THROWERR_CUSTOM(env, ERROR_NO_PERMISSION,
253 "Permission denied");
254 }
255 return ret;
256 }
257
SetFunctionKeyEnabled(napi_env env,int32_t funcKey,bool state,napi_value handle)258 napi_value JsInputDeviceManager::SetFunctionKeyEnabled(napi_env env, int32_t funcKey, bool state, napi_value handle)
259 {
260 CALL_DEBUG_ENTER;
261 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
262 CHKPP(cb);
263 napi_value ret = CreateCallbackInfo(env, handle, cb);
264 EmitJsSetFunctionKeyState(cb, funcKey, state);
265 return ret;
266 }
267
IsFunctionKeyEnabled(napi_env env,int32_t funcKey,napi_value handle)268 napi_value JsInputDeviceManager::IsFunctionKeyEnabled(napi_env env, int32_t funcKey, napi_value handle)
269 {
270 CALL_DEBUG_ENTER;
271 sptr<JsUtil::CallbackInfo> cb = new (std::nothrow) JsUtil::CallbackInfo();
272 CHKPP(cb);
273 napi_value ret = CreateCallbackInfo(env, handle, cb);
274 EmitJsGetFunctionKeyState(cb, funcKey);
275 return ret;
276 }
277 } // namespace MMI
278 } // namespace OHOS