diff --git a/frameworks/napi/input_device/include/js_event_target.h b/frameworks/napi/input_device/include/js_event_target.h index abd0a3cc08cb3c1b4ba186072ce292552b14cf2a..8a868b4d17e17b6189e826e1d62271054bf5b869 100644 --- a/frameworks/napi/input_device/include/js_event_target.h +++ b/frameworks/napi/input_device/include/js_event_target.h @@ -38,6 +38,8 @@ public: static void EmitJsDev(int32_t userData, std::shared_ptr device); static void EmitSupportKeys(int32_t userData, std::vector &keystrokeAbility); static void EmitJsKeyboardType(int32_t userData, int32_t keyboardType); + static void EmitJsGetFunctionKeyState(int32_t userData, int32_t errCode, bool state); + static void EmitJsSetFunctionKeyState(int32_t userData, int32_t errCode); void AddListener(napi_env env, const std::string &type, napi_value handle); void RemoveListener(napi_env env, const std::string &type, napi_value handle); void RemoveCallbackInfo(napi_env env, napi_value handle, int32_t userData); @@ -59,10 +61,12 @@ private: static void CallDevListAsyncWork(uv_work_t *work, int32_t status); static void CallDevInfoAsyncWork(uv_work_t *work, int32_t status); static void CallDevInfoPromiseWork(uv_work_t *work, int32_t status); + static void CallGetFunctionKeyState(uv_work_t *work, int32_t status); + static void CallSetFunctionKeyState(uv_work_t *work, int32_t status); static void EmitAddedDeviceEvent(uv_work_t *work, int32_t status); static void EmitRemoveDeviceEvent(uv_work_t *work, int32_t status); static std::unique_ptr GetCallbackInfo(uv_work_t *work); - static napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage); + static napi_value CreateBusinessError(napi_env env, int32_t errCode, std::string errMessage); private: inline static std::map> callback_ {}; inline static std::map>> devListener_ {}; diff --git a/frameworks/napi/input_device/include/js_input_device_context.h b/frameworks/napi/input_device/include/js_input_device_context.h index 9e78e180c2faced89ec0e879043dfd8abb7cb7e3..b27f23438ced631a39012536c76aebe626a42da8 100644 --- a/frameworks/napi/input_device/include/js_input_device_context.h +++ b/frameworks/napi/input_device/include/js_input_device_context.h @@ -34,6 +34,8 @@ public: static napi_value GetDeviceInfo(napi_env env, napi_callback_info info); static napi_value SupportKeys(napi_env env, napi_callback_info info); static napi_value GetKeyboardType(napi_env env, napi_callback_info info); + static napi_value GetFunctionKeyState(napi_env env, napi_callback_info info); + static napi_value SetFunctionKeyState(napi_env env, napi_callback_info info); std::shared_ptr GetJsInputDeviceMgr() const; private: @@ -42,6 +44,7 @@ private: static napi_value JsConstructor(napi_env env, napi_callback_info info); static napi_value EnumClassConstructor(napi_env env, napi_callback_info info); static napi_value CreateEnumKeyboardType(napi_env env, napi_value exports); + static napi_value CreateEnumFunctionKeyType(napi_env env, napi_value exports); std::shared_ptr mgr_ { nullptr }; napi_ref contextRef_ { nullptr }; std::mutex mtx_; diff --git a/frameworks/napi/input_device/include/js_input_device_manager.h b/frameworks/napi/input_device/include/js_input_device_manager.h index 80949a7c33873b14728e7cb00c9d0df76648c4fb..aa1a58c4c2a19d9d5bdee0e2d8f98b33cb75f555 100644 --- a/frameworks/napi/input_device/include/js_input_device_manager.h +++ b/frameworks/napi/input_device/include/js_input_device_manager.h @@ -33,6 +33,8 @@ public: napi_value SupportKeys(napi_env env, int32_t id, std::vector &keyCodes, napi_value handle = nullptr); napi_value GetKeyboardType(napi_env env, int32_t id, napi_value handle = nullptr); + napi_value GetFunctionKeyState(napi_env env, int32_t funcKey, napi_value handle = nullptr); + napi_value SetFunctionKeyState(napi_env env, int32_t funcKey, bool state, napi_value handle = nullptr); void RegisterDevListener(napi_env env, const std::string &type, napi_value handle); void UnregisterDevListener(napi_env env, const std::string &type, napi_value handle = nullptr); napi_value GetDeviceList(napi_env env, napi_value handle = nullptr); diff --git a/frameworks/napi/input_device/include/js_util.h b/frameworks/napi/input_device/include/js_util.h index 6cabb8afbb8f95e3ed6c1535b64d841ec3a3e7d0..58be0f1c3b0a16cd46382d08a2c243f6527b5f43 100644 --- a/frameworks/napi/input_device/include/js_util.h +++ b/frameworks/napi/input_device/include/js_util.h @@ -38,6 +38,7 @@ public: std::vector keystrokeAbility; int32_t deviceId { 0 }; int32_t keyboardType { 0 }; + bool funcKeyState { false }; }; struct CallbackInfo { napi_env env { nullptr }; diff --git a/frameworks/napi/input_device/src/js_event_target.cpp b/frameworks/napi/input_device/src/js_event_target.cpp old mode 100755 new mode 100644 index 3ddeae113ad7188000f58fe1c120d132147d80e6..ea3c7ea682557b66d05502c14895ae76a4954222 --- a/frameworks/napi/input_device/src/js_event_target.cpp +++ b/frameworks/napi/input_device/src/js_event_target.cpp @@ -451,7 +451,7 @@ void JsEventTarget::CallKeystrokeAbilityPromise(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, callResult), REJECT_DEFERRED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_array(cb->env, &callResult), CREATE_ARRAY, scope); @@ -499,7 +499,7 @@ void JsEventTarget::CallKeystrokeAbilityAsync(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult[0] = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult[0] = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &callResult[1]), GET_UNDEFINED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_array(cb->env, &callResult[1]), CREATE_ARRAY, scope); @@ -632,7 +632,7 @@ void JsEventTarget::CallKeyboardTypeAsync(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult[0] = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult[0] = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &callResult[1]), GET_UNDEFINED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_int32(cb->env, cb->data.keyboardType, &callResult[1]), CREATE_INT32, scope); @@ -677,7 +677,7 @@ void JsEventTarget::CallKeyboardTypePromise(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, callResult), REJECT_DEFERRED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_int32(cb->env, cb->data.keyboardType, &callResult), CREATE_INT32, scope); @@ -716,7 +716,7 @@ void JsEventTarget::CallDevListAsyncWork(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult[0] = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult[0] = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &callResult[1]), GET_UNDEFINED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_array(cb->env, &callResult[1]), CREATE_ARRAY, scope); @@ -766,7 +766,7 @@ void JsEventTarget::CallDevListPromiseWork(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, callResult), REJECT_DEFERRED, scope); } else { CHKRV_SCOPE(cb->env, napi_create_array(cb->env, &callResult), CREATE_ARRAY, scope); @@ -811,7 +811,7 @@ void JsEventTarget::CallDevInfoPromiseWork(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, callResult), REJECT_DEFERRED, scope); } else { callResult = JsUtil::GetDeviceInfo(cb); @@ -854,7 +854,7 @@ void JsEventTarget::CallDevInfoAsyncWork(uv_work_t *work, int32_t status) MMI_HILOGE("Error code %{public}d not found", cb->errCode); return; } - callResult[0] = GreateBusinessError(cb->env, cb->errCode, codeMsg.msg); + callResult[0] = CreateBusinessError(cb->env, cb->errCode, codeMsg.msg); CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &callResult[1]), GET_UNDEFINED, scope); } else { callResult[1] = JsUtil::GetDeviceInfo(cb); @@ -869,6 +869,172 @@ void JsEventTarget::CallDevInfoAsyncWork(uv_work_t *work, int32_t status) JsUtil::DeleteCallbackInfo(std::move(cb)); } +void JsEventTarget::EmitJsGetFunctionKeyState(int32_t userData, int32_t errCode, bool state) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(mutex_); + auto iter = callback_.find(userData); + if (iter == callback_.end()) { + MMI_HILOGE("Failed to search for userData"); + return; + } + CHKPV(iter->second); + if (iter->second->env == nullptr) { + callback_.erase(iter); + MMI_HILOGE("The env is nullptr"); + return; + } + iter->second->data.funcKeyState = state; + iter->second->errCode = errCode; + + uv_loop_s *loop = nullptr; + CHKRV(iter->second->env, napi_get_uv_event_loop(iter->second->env, &loop), GET_UV_LOOP); + + uv_work_t *work = new (std::nothrow) uv_work_t; + CHKPV(work); + int32_t *uData = new (std::nothrow) int32_t(userData); + if (uData == nullptr) { + MMI_HILOGE("uData is nullptr"); + JsUtil::DeletePtr(work); + return; + } + work->data = static_cast(uData); + int32_t ret = 0; + ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, CallGetFunctionKeyState); + if (ret != 0) { + MMI_HILOGE("uv_queue_work failed"); + JsUtil::DeletePtr(work); + JsUtil::DeletePtr(uData); + } +} + +void JsEventTarget::CallGetFunctionKeyState(uv_work_t *work, int32_t status) +{ + CALL_DEBUG_ENTER; + CHKPV(work); + if (work->data == nullptr) { + JsUtil::DeletePtr(work); + MMI_HILOGE("Check data is null"); + return; + } + std::unique_ptr cb = GetCallbackInfo(work); + CHKPV(cb); + CHKPV(cb->env); + + napi_handle_scope scope = nullptr; + napi_open_handle_scope(cb->env, &scope); + CHKPV(scope); + + napi_value results[2] = { 0 }; + if (cb->errCode == RET_OK) { + CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &results[0]), GET_UNDEFINED, scope); + CHKRV_SCOPE(cb->env, napi_get_boolean(cb->env, cb->data.funcKeyState, &results[1]), CREATE_INT32, scope); + } else { + CHKRV_SCOPE(cb->env, napi_create_object(cb->env, &results[0]), CREATE_OBJECT, scope); + napi_value errCode = nullptr; + CHKRV_SCOPE(cb->env, napi_create_int32(cb->env, cb->errCode, &errCode), CREATE_INT32, scope); + CHKRV_SCOPE(cb->env, napi_set_named_property(cb->env, results[0], "code", errCode), + SET_NAMED_PROPERTY, scope); + CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &results[1]), GET_UNDEFINED, scope); + } + if (cb->deferred) { + if (cb->errCode == RET_OK) { + CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, results[1]), RESOLVE_DEFERRED, scope); + } else { + CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, results[0]), REJECT_DEFERRED, scope); + } + } else { + napi_value handler = nullptr; + CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &handler), GET_REFERENCE, scope); + napi_value result = nullptr; + CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, handler, 2, results, &result), + CALL_FUNCTION, scope); + } + napi_close_handle_scope(cb->env, scope); + JsUtil::DeleteCallbackInfo(std::move(cb)); +} + +void JsEventTarget::EmitJsSetFunctionKeyState(int32_t userData, int32_t errCode) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(mutex_); + auto iter = callback_.find(userData); + if (iter == callback_.end()) { + MMI_HILOGE("Failed to search for userData"); + return; + } + CHKPV(iter->second); + if (iter->second->env == nullptr) { + callback_.erase(iter); + MMI_HILOGE("The env is nullptr"); + return; + } + iter->second->errCode = errCode; + + uv_loop_s *loop = nullptr; + CHKRV(iter->second->env, napi_get_uv_event_loop(iter->second->env, &loop), GET_UV_LOOP); + + uv_work_t *work = new (std::nothrow) uv_work_t; + CHKPV(work); + int32_t *uData = new (std::nothrow) int32_t(userData); + if (uData == nullptr) { + MMI_HILOGE("uData is nullptr"); + JsUtil::DeletePtr(work); + return; + } + work->data = static_cast(uData); + int32_t ret = 0; + ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, CallSetFunctionKeyState); + if (ret != 0) { + MMI_HILOGE("uv_queue_work failed"); + JsUtil::DeletePtr(work); + JsUtil::DeletePtr(uData); + } +} + +void JsEventTarget::CallSetFunctionKeyState(uv_work_t *work, int32_t status) +{ + CALL_DEBUG_ENTER; + CHKPV(work); + if (work->data == nullptr) { + JsUtil::DeletePtr(work); + MMI_HILOGE("Check data is null"); + return; + } + std::unique_ptr cb = GetCallbackInfo(work); + CHKPV(cb); + CHKPV(cb->env); + + napi_handle_scope scope = nullptr; + napi_open_handle_scope(cb->env, &scope); + CHKPV(scope); + + napi_value result; + if (cb->errCode == RET_OK) { + CHKRV_SCOPE(cb->env, napi_get_undefined(cb->env, &result), GET_UNDEFINED, scope); + } else { + CHKRV_SCOPE(cb->env, napi_create_object(cb->env, &result), CREATE_OBJECT, scope); + napi_value errCode = nullptr; + CHKRV_SCOPE(cb->env, napi_create_int32(cb->env, cb->errCode, &errCode), CREATE_INT32, scope); + CHKRV_SCOPE(cb->env, napi_set_named_property(cb->env, result, "code", errCode), SET_NAMED_PROPERTY, scope); + } + if (cb->deferred) { + if (cb->errCode == RET_OK) { + CHKRV_SCOPE(cb->env, napi_resolve_deferred(cb->env, cb->deferred, result), RESOLVE_DEFERRED, scope); + } else { + CHKRV_SCOPE(cb->env, napi_reject_deferred(cb->env, cb->deferred, result), REJECT_DEFERRED, scope); + } + } else { + napi_value handler = nullptr; + CHKRV_SCOPE(cb->env, napi_get_reference_value(cb->env, cb->ref, &handler), GET_REFERENCE, scope); + napi_value callResult = nullptr; + CHKRV_SCOPE(cb->env, napi_call_function(cb->env, nullptr, handler, 1, &result, &callResult), + CALL_FUNCTION, scope); + } + napi_close_handle_scope(cb->env, scope); + JsUtil::DeleteCallbackInfo(std::move(cb)); +} + void JsEventTarget::AddListener(napi_env env, const std::string &type, napi_value handle) { CALL_DEBUG_ENTER; @@ -952,7 +1118,7 @@ napi_value JsEventTarget::CreateCallbackInfo(napi_env env, napi_value handle, co return promise; } -napi_value JsEventTarget::GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage) +napi_value JsEventTarget::CreateBusinessError(napi_env env, int32_t errCode, std::string errMessage) { CALL_DEBUG_ENTER; napi_value result = nullptr; diff --git a/frameworks/napi/input_device/src/js_input_device_context.cpp b/frameworks/napi/input_device/src/js_input_device_context.cpp index 111d03bc169a3de15e7361eafeadce4cddb709a1..798f03f8983b82b510818e5727713f77e981ae81 100644 --- a/frameworks/napi/input_device/src/js_input_device_context.cpp +++ b/frameworks/napi/input_device/src/js_input_device_context.cpp @@ -462,6 +462,102 @@ napi_value JsInputDeviceContext::CreateEnumKeyboardType(napi_env env, napi_value return exports; } +napi_value JsInputDeviceContext::GetFunctionKeyState(napi_env env, napi_callback_info info) +{ + CALL_DEBUG_ENTER; + size_t argc = 2; + napi_value argv[2]; + CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); + if (argc == 0) { + MMI_HILOGE("At least one argument is required"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error"); + return nullptr; + } + if (!JsUtil::TypeOf(env, argv[0], napi_number)) { + MMI_HILOGE("First parameter type error"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funcKey", "number"); + return nullptr; + } + int32_t funcKey = 0; + CHKRP(env, napi_get_value_int32(env, argv[0], &funcKey), GET_INT32); + + JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env); + CHKPP(jsDev); + auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr(); + CHKPP(jsInputDeviceMgr); + if (argc == 1) { + return jsInputDeviceMgr->GetFunctionKeyState(env, funcKey); + } + if (!JsUtil::TypeOf(env, argv[1], napi_function)) { + MMI_HILOGE("Second parameter type error"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function"); + return nullptr; + } + return jsInputDeviceMgr->GetFunctionKeyState(env, funcKey, argv[1]); +} + +napi_value JsInputDeviceContext::SetFunctionKeyState(napi_env env, napi_callback_info info) +{ + CALL_DEBUG_ENTER; + size_t argc = 3; + napi_value argv[3]; + CHKRP(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); + if (argc < 2) { + MMI_HILOGE("At least two parameters are required"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "Parameter count error"); + return nullptr; + } + if (!JsUtil::TypeOf(env, argv[0], napi_number)) { + MMI_HILOGE("First parameter type error"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funcKey", "number"); + return nullptr; + } + if (!JsUtil::TypeOf(env, argv[1], napi_boolean)) { + MMI_HILOGE("Second parameter type error"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "funcKey", "bool"); + return nullptr; + } + int32_t funcKey = 0; + CHKRP(env, napi_get_value_int32(env, argv[0], &funcKey), GET_INT32); + bool state; + CHKRP(env, napi_get_value_bool(env, argv[1], &state), CREATE_BOOL); + + JsInputDeviceContext *jsDev = JsInputDeviceContext::GetInstance(env); + CHKPP(jsDev); + auto jsInputDeviceMgr = jsDev->GetJsInputDeviceMgr(); + CHKPP(jsInputDeviceMgr); + if (argc == 2) { + return jsInputDeviceMgr->SetFunctionKeyState(env, funcKey, state); + } + if (!JsUtil::TypeOf(env, argv[2], napi_function)) { + MMI_HILOGE("Third parameter type error"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function"); + return nullptr; + } + return jsInputDeviceMgr->SetFunctionKeyState(env, funcKey, state, argv[2]); +} + +napi_value JsInputDeviceContext::CreateEnumFunctionKeyType(napi_env env, napi_value exports) +{ + CALL_DEBUG_ENTER; + napi_value numLock = nullptr; + CHKRP(env, napi_create_int32(env, KeyEvent::NUM_LOCK_FUNCTION_KEY, &numLock), CREATE_INT32); + napi_value capsLock = nullptr; + CHKRP(env, napi_create_int32(env, KeyEvent::CAPS_LOCK_FUNCTION_KEY, &capsLock), CREATE_INT32); + napi_value scrollLock = nullptr; + CHKRP(env, napi_create_int32(env, KeyEvent::SCROLL_LOCK_FUNCTION_KEY, &scrollLock), CREATE_INT32); + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("NUM_LOCK_FUNCTION_KEY", numLock), + DECLARE_NAPI_STATIC_PROPERTY("CAPS_LOCK_FUNCTION_KEY", capsLock), + DECLARE_NAPI_STATIC_PROPERTY("SCROLL_LOCK_FUNCTION_KEY", scrollLock), + }; + napi_value result = nullptr; + CHKRP(env, napi_define_class(env, "FunctionKey", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS); + CHKRP(env, napi_set_named_property(env, exports, "FunctionKey", result), SET_NAMED_PROPERTY); + return exports; +} + napi_value JsInputDeviceContext::Export(napi_env env, napi_value exports) { CALL_DEBUG_ENTER; @@ -479,12 +575,18 @@ napi_value JsInputDeviceContext::Export(napi_env env, napi_value exports) DECLARE_NAPI_STATIC_FUNCTION("getKeyboardType", GetKeyboardType), DECLARE_NAPI_STATIC_FUNCTION("getDeviceList", GetDeviceList), DECLARE_NAPI_STATIC_FUNCTION("getDeviceInfo", GetDeviceInfo), + DECLARE_NAPI_STATIC_FUNCTION("getFunctionKeyState", GetFunctionKeyState), + DECLARE_NAPI_STATIC_FUNCTION("setFunctionKeyState", SetFunctionKeyState), }; CHKRP(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc), DEFINE_PROPERTIES); if (CreateEnumKeyboardType(env, exports) == nullptr) { MMI_HILOGE("Failed to create keyboard type enum"); return nullptr; } + if (CreateEnumFunctionKeyType(env, exports) == nullptr) { + THROWERR(env, "Failed to create function key type enum"); + return nullptr; + } return exports; } } // namespace MMI diff --git a/frameworks/napi/input_device/src/js_input_device_manager.cpp b/frameworks/napi/input_device/src/js_input_device_manager.cpp index 6de4913eb8d31bd15a3b26945f1d7ac8e6829258..4e42e4ebe775ce93d2a77ceb6412d164318062ea 100644 --- a/frameworks/napi/input_device/src/js_input_device_manager.cpp +++ b/frameworks/napi/input_device/src/js_input_device_manager.cpp @@ -124,6 +124,29 @@ napi_value JsInputDeviceManager::GetDeviceInfo(napi_env env, int32_t id, napi_va return ret; } +napi_value JsInputDeviceManager::GetFunctionKeyState(napi_env env, int32_t funcKey, napi_value handle) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(mutex_); + int32_t userData = InputDevImpl.GetUserData(); + napi_value ret = CreateCallbackInfo(env, handle, userData); + bool state { false }; + int32_t errCode = InputManager::GetInstance()->GetFunctionKeyState(funcKey, state); + EmitJsGetFunctionKeyState(userData, errCode, state); + return ret; +} + +napi_value JsInputDeviceManager::SetFunctionKeyState(napi_env env, int32_t funcKey, bool state, napi_value handle) +{ + CALL_DEBUG_ENTER; + std::lock_guard guard(mutex_); + int32_t userData = InputDevImpl.GetUserData(); + napi_value ret = CreateCallbackInfo(env, handle, userData); + int32_t errCode = InputManager::GetInstance()->SetFunctionKeyState(funcKey, state); + EmitJsSetFunctionKeyState(userData, errCode); + return ret; +} + void JsInputDeviceManager::ResetEnv() { CALL_DEBUG_ENTER; diff --git a/frameworks/proxy/event_handler/include/input_device_impl.h b/frameworks/proxy/event_handler/include/input_device_impl.h index 3b8463b46e2d94a86697c400c96cf43c2d9b5292..5489a1ce33e10a83803e9d75002a1835ac851a6a 100755 --- a/frameworks/proxy/event_handler/include/input_device_impl.h +++ b/frameworks/proxy/event_handler/include/input_device_impl.h @@ -53,6 +53,8 @@ public: int32_t GetInputDeviceAsync(int32_t deviceId, FunInputDevInfo callback); int32_t SupportKeys(int32_t deviceId, std::vector keyCodes, FunInputDevKeys callback); int32_t GetKeyboardType(int32_t deviceId, FunKeyboardTypes callback); + int32_t GetFunctionKeyState(int32_t funcKey, bool &state); + int32_t SetFunctionKeyState(int32_t funcKey, bool enable); void OnInputDevice(int32_t userData, std::shared_ptr devData); void OnInputDeviceIds(int32_t userData, std::vector &ids); void OnSupportKeys(int32_t userData, std::vector &keystrokeAbility); diff --git a/frameworks/proxy/event_handler/include/input_manager_impl.h b/frameworks/proxy/event_handler/include/input_manager_impl.h index a2d9f61f39aa62ad45ae099fd6516af5eb05761a..61062e45349db13a8ef89c280aeb115c9b723040 100755 --- a/frameworks/proxy/event_handler/include/input_manager_impl.h +++ b/frameworks/proxy/event_handler/include/input_manager_impl.h @@ -109,7 +109,7 @@ public: int32_t StopDeviceCooperate(std::function callback); int32_t GetInputDeviceCooperateState(const std::string &deviceId, std::function callback); int32_t SetInputDevice(const std::string& dhid, const std::string& screenId); - bool GetFunctionKeyState(int32_t funcKey); + int32_t GetFunctionKeyState(int32_t funcKey, bool &state); int32_t SetFunctionKeyState(int32_t funcKey, bool enable); private: diff --git a/frameworks/proxy/event_handler/src/input_device_impl.cpp b/frameworks/proxy/event_handler/src/input_device_impl.cpp index 6387a11db3a1b5d7af8c336a406b66ecbdbbbd76..493c1d6c2931fbf0fcaa60496759dca75995366f 100755 --- a/frameworks/proxy/event_handler/src/input_device_impl.cpp +++ b/frameworks/proxy/event_handler/src/input_device_impl.cpp @@ -172,6 +172,42 @@ int32_t InputDeviceImpl::GetKeyboardType(int32_t deviceId, FunKeyboardTypes call return MultimodalInputConnMgr->GetKeyboardType(userData_++, deviceId); } +int32_t InputDeviceImpl::GetFunctionKeyState(int32_t funcKey, bool &state) +{ +#ifdef OHOS_BUILD_ENABLE_KEYBOARD + CALL_DEBUG_ENTER; + std::lock_guard guard(mtx_); + int32_t ret = MultimodalInputConnMgr->GetFunctionKeyState(funcKey, state); + if (ret != RET_OK) { + MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + } else { + userData_++; + } + return ret; +#else + MMI_HILOGW("Keyboard device does not support"); + return ERROR_UNSUPPORT; +#endif // OHOS_BUILD_ENABLE_KEYBOARD +} + +int32_t InputDeviceImpl::SetFunctionKeyState(int32_t funcKey, bool enable) +{ +#ifdef OHOS_BUILD_ENABLE_KEYBOARD + CALL_DEBUG_ENTER; + std::lock_guard guard(mtx_); + int32_t ret = MultimodalInputConnMgr->SetFunctionKeyState(funcKey, enable); + if (ret != RET_OK) { + MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + } else { + userData_++; + } + return ret; +#else + MMI_HILOGW("Keyboard device does not support"); + return ERROR_UNSUPPORT; +#endif // OHOS_BUILD_ENABLE_KEYBOARD +} + void InputDeviceImpl::OnInputDevice(int32_t userData, std::shared_ptr devData) { CALL_DEBUG_ENTER; diff --git a/frameworks/proxy/event_handler/src/input_manager_impl.cpp b/frameworks/proxy/event_handler/src/input_manager_impl.cpp index c347cdf3eadc14f6cfb4e2dd0bcea6154041062c..2970f1377e2b1581c943c277a2dd3baa0bbeff42 100755 --- a/frameworks/proxy/event_handler/src/input_manager_impl.cpp +++ b/frameworks/proxy/event_handler/src/input_manager_impl.cpp @@ -866,36 +866,27 @@ int32_t InputManagerImpl::GetInputDeviceCooperateState(const std::string &device #endif // OHOS_BUILD_ENABLE_COOPERATE } -bool InputManagerImpl::GetFunctionKeyState(int32_t funcKey) +int32_t InputManagerImpl::GetFunctionKeyState(int32_t funcKey, bool &state) { -#ifdef OHOS_BUILD_ENABLE_KEYBOARD CALL_DEBUG_ENTER; - bool state { false }; - int32_t ret = MultimodalInputConnMgr->GetFunctionKeyState(funcKey, state); - if (ret != RET_OK) { - MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + std::lock_guard guard(mtx_); + state = false; + if (!MMIEventHdl.InitClient()) { + MMI_HILOGE("Client init failed"); + return RET_ERR; } - return state; -#else - MMI_HILOGW("Keyboard device does not support"); - return false; -#endif // OHOS_BUILD_ENABLE_KEYBOARD + return InputDevImpl.GetFunctionKeyState(funcKey, state); } int32_t InputManagerImpl::SetFunctionKeyState(int32_t funcKey, bool enable) { -#ifdef OHOS_BUILD_ENABLE_KEYBOARD CALL_DEBUG_ENTER; - int32_t ret = MultimodalInputConnMgr->SetFunctionKeyState(funcKey, enable); - if (ret != RET_OK) { - MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + std::lock_guard guard(mtx_); + if (!MMIEventHdl.InitClient()) { + MMI_HILOGE("Client init failed"); return RET_ERR; } - return RET_OK; -#else - MMI_HILOGW("Keyboard device does not support"); - return ERROR_UNSUPPORT; -#endif // OHOS_BUILD_ENABLE_KEYBOARD + return InputDevImpl.SetFunctionKeyState(funcKey, enable); } } // namespace MMI } // namespace OHOS diff --git a/frameworks/proxy/events/src/input_manager.cpp b/frameworks/proxy/events/src/input_manager.cpp index 1e4afaee108ab5508e66fb8ad17cafcb462d8c5e..57e560e820a056951fda87d65cbc66fdc6aa8f8d 100644 --- a/frameworks/proxy/events/src/input_manager.cpp +++ b/frameworks/proxy/events/src/input_manager.cpp @@ -224,9 +224,9 @@ int32_t InputManager::GetInputDeviceCooperateState(const std::string &deviceId, return InputMgrImpl.GetInputDeviceCooperateState(deviceId, callback); } -bool InputManager::GetFunctionKeyState(int32_t funcKey) +int32_t InputManager::GetFunctionKeyState(int32_t funcKey, bool &state) { - return InputMgrImpl.GetFunctionKeyState(funcKey); + return InputMgrImpl.GetFunctionKeyState(funcKey, state); } int32_t InputManager::SetFunctionKeyState(int32_t funcKey, bool enable) diff --git a/frameworks/proxy/events/test/input_manager_test.cpp b/frameworks/proxy/events/test/input_manager_test.cpp index cfb053a7c98de8d9202f24a5cfd1e7f02537515c..05a55d641bba4632abc4f8d26782e8a9f1f0823f 100755 --- a/frameworks/proxy/events/test/input_manager_test.cpp +++ b/frameworks/proxy/events/test/input_manager_test.cpp @@ -3259,8 +3259,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_SetPointerStyle_001, TestSize.Level1 HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_001, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, true); - InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY); + bool state = true; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, state); + ASSERT_TRUE(state); + } } /** @@ -3272,9 +3275,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_001, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_002, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, false); - bool result = InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY); - ASSERT_FALSE(result); + bool state = false; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::NUM_LOCK_FUNCTION_KEY, state); + ASSERT_FALSE(state); + } } /** @@ -3286,8 +3291,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_002, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_003, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, true); - InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY); + bool state = true; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, state); + ASSERT_TRUE(state); + } } /** @@ -3299,9 +3307,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_003, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_004, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, false); - bool result = InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY); - ASSERT_FALSE(result); + bool state = false; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::SCROLL_LOCK_FUNCTION_KEY, state); + ASSERT_FALSE(state); + } } /** @@ -3313,8 +3323,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_004, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_005, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, true); - InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY); + bool state = true; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, state); + ASSERT_TRUE(state); + } } /** @@ -3326,9 +3339,11 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_005, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_006, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, false); - bool result = InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY); - ASSERT_FALSE(result); + bool state = false; + if (InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, state) == RET_OK) { + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::CAPS_LOCK_FUNCTION_KEY, state); + ASSERT_FALSE(state); + } } /** @@ -3340,13 +3355,15 @@ HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_006, TestSize.Level HWTEST_F(InputManagerTest, InputManagerTest_FunctionKeyState_007, TestSize.Level1) { CALL_DEBUG_ENTER; - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, true); - bool result = InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY); - ASSERT_FALSE(result); - - InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, false); - result = InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY); - ASSERT_FALSE(result); + bool state = true; + InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, state); + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, state); + ASSERT_FALSE(state); + + state = false; + InputManager::GetInstance()->SetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, state); + InputManager::GetInstance()->GetFunctionKeyState(KeyEvent::UNKOWN_FUNCTION_KEY, state); + ASSERT_FALSE(state); } /** diff --git a/interfaces/native/innerkits/proxy/include/input_manager.h b/interfaces/native/innerkits/proxy/include/input_manager.h index 2c609e4fcdda2df81da6bb00a9c68811287f0189..de040da9955195754cd24eeae62e1b3e431ef47f 100644 --- a/interfaces/native/innerkits/proxy/include/input_manager.h +++ b/interfaces/native/innerkits/proxy/include/input_manager.h @@ -385,10 +385,10 @@ public: * NUM_LOCK_FUNCTION_KEY * CAPS_LOCK_FUNCTION_KEY * SCROLL_LOCK_FUNCTION_KEY。 - * @return 返回功能按键的使能状态,true表示功能按键使能, - * false表示功能按键未使能。 + * @param state 需要获取的功能按键的使能状态 + * @return 0 表示获取成功,其他值表示获取失败。 */ - bool GetFunctionKeyState(int32_t funcKey); + int32_t GetFunctionKeyState(int32_t funcKey, bool &state); /** * @brief 设置键盘设备指定功能按键的使能状态。 diff --git a/service/key_event_normalize/src/key_event_normalize.cpp b/service/key_event_normalize/src/key_event_normalize.cpp index e1d4cff8765c0b03b37e94fba2874a2e27d36bbd..f124cedd164dd046abeb5046fbf5c4dea1e8f36a 100644 --- a/service/key_event_normalize/src/key_event_normalize.cpp +++ b/service/key_event_normalize/src/key_event_normalize.cpp @@ -108,6 +108,7 @@ int32_t KeyEventNormalize::Normalize(struct libinput_event *event, std::shared_p void KeyEventNormalize::ResetKeyEvent(struct libinput_device* device) { + CHKPV(device); if (InputDevMgr->IsKeyboardDevice(device) || InputDevMgr->IsPointerDevice(device)) { if (keyEvent_ == nullptr) { keyEvent_ = KeyEvent::Create(); diff --git a/service/message_handle/src/server_msg_handler.cpp b/service/message_handle/src/server_msg_handler.cpp index 37b09d1bb3aa147e03a4c6ab386c9e08b53aa5f9..d22c15d809d8c64d031a47a4f3cc0ff779195d10 100644 --- a/service/message_handle/src/server_msg_handler.cpp +++ b/service/message_handle/src/server_msg_handler.cpp @@ -103,6 +103,12 @@ int32_t ServerMsgHandler::OnInjectKeyEvent(const std::shared_ptr keyEv int32_t ServerMsgHandler::OnGetFunctionKeyState(int32_t funcKey, bool &state) { CALL_INFO_TRACE; + if (funcKey != KeyEvent::NUM_LOCK_FUNCTION_KEY && + funcKey != KeyEvent::CAPS_LOCK_FUNCTION_KEY && + funcKey != KeyEvent::SCROLL_LOCK_FUNCTION_KEY) { + MMI_HILOGW("This feature button is not currently supported"); + return ERROR_UNSUPPORT; + } const auto &keyEvent = KeyEventHdr->GetKeyEvent(); CHKPR(keyEvent, ERROR_NULL_POINTER); state = keyEvent->GetFunctionKey(funcKey); @@ -113,6 +119,12 @@ int32_t ServerMsgHandler::OnGetFunctionKeyState(int32_t funcKey, bool &state) int32_t ServerMsgHandler::OnSetFunctionKeyState(int32_t funcKey, bool enable) { CALL_INFO_TRACE; + if (funcKey != KeyEvent::NUM_LOCK_FUNCTION_KEY && + funcKey != KeyEvent::CAPS_LOCK_FUNCTION_KEY && + funcKey != KeyEvent::SCROLL_LOCK_FUNCTION_KEY) { + MMI_HILOGW("This feature button is not currently supported"); + return ERROR_UNSUPPORT; + } auto device = InputDevMgr->GetKeyboardDevice(); CHKPR(device, ERROR_NULL_POINTER); if (LibinputAdapter::DeviceLedUpdate(device, funcKey, enable) != RET_OK) { diff --git a/test/fuzztest/functionkeystate_fuzzer/functionkeystate_fuzzer.cpp b/test/fuzztest/functionkeystate_fuzzer/functionkeystate_fuzzer.cpp index 2e7b6a357eea4ce11a3ad1daad16a3d70fc23e91..9f65cf204efc29f77a4d87488540ef8b5669c45a 100644 --- a/test/fuzztest/functionkeystate_fuzzer/functionkeystate_fuzzer.cpp +++ b/test/fuzztest/functionkeystate_fuzzer/functionkeystate_fuzzer.cpp @@ -39,6 +39,7 @@ size_t GetObject(T &object, const uint8_t *data, size_t size) void FunctionkeyStateFuzzTest(const uint8_t* data, size_t size) { int32_t funcKey; + bool state = false; size_t startPos = 0; startPos += GetObject(funcKey, data + startPos, size - startPos); int32_t random; @@ -46,7 +47,7 @@ void FunctionkeyStateFuzzTest(const uint8_t* data, size_t size) bool enable = (random % 2) ? false : true; InputManager::GetInstance()->SetFunctionKeyState(funcKey, enable); - InputManager::GetInstance()->GetFunctionKeyState(funcKey); + InputManager::GetInstance()->GetFunctionKeyState(funcKey, state); } } // MMI } // OHOS