From 3b8c3c504ea441681405fa68f4698c751094be43 Mon Sep 17 00:00:00 2001 From: yajunstrong Date: Thu, 17 Nov 2022 14:49:46 +0800 Subject: [PATCH 1/5] napi Refactoring - Key subscribe Signed-off-by: yajunstrong Change-Id: Ibc74a4bff57ed8fb111a0166442fece77c06e72d --- frameworks/napi/input_consumer/BUILD.gn | 7 +- ...r_module.h => js_input_consumer_context.h} | 60 +-- .../include/js_input_consumer_util.h | 54 +++ .../src/js_input_consumer_context.cpp | 225 +++++++++++ ...er_util.cpp => js_input_consumer_util.cpp} | 346 +++++++++-------- .../input_consumer/src/js_register_module.cpp | 367 ------------------ .../native_register_module.cpp} | 81 ++-- util/common/include/util_napi.h | 14 + 8 files changed, 548 insertions(+), 606 deletions(-) rename frameworks/napi/input_consumer/include/{js_register_module.h => js_input_consumer_context.h} (43%) create mode 100644 frameworks/napi/input_consumer/include/js_input_consumer_util.h create mode 100644 frameworks/napi/input_consumer/src/js_input_consumer_context.cpp rename frameworks/napi/input_consumer/src/{js_register_util.cpp => js_input_consumer_util.cpp} (38%) delete mode 100644 frameworks/napi/input_consumer/src/js_register_module.cpp rename frameworks/napi/input_consumer/{include/js_register_util.h => src/native_register_module.cpp} (34%) diff --git a/frameworks/napi/input_consumer/BUILD.gn b/frameworks/napi/input_consumer/BUILD.gn index 6416ac0b79..08c82f0e1c 100644 --- a/frameworks/napi/input_consumer/BUILD.gn +++ b/frameworks/napi/input_consumer/BUILD.gn @@ -19,15 +19,15 @@ config("inputconsumer_config") { include_dirs = [ "//foundation/multimodalinput/input/util/common/include", "//foundation/multimodalinput/input/frameworks/napi/input_consumer/include", - "//foundation/multimodalinput/input/tools/event_inject/include", "//foundation/multimodalinput/input/interfaces/native/innerkits/proxy/include", ] } ohos_shared_library("inputconsumer") { sources = [ - "src/js_register_module.cpp", - "src/js_register_util.cpp", + "src/js_input_consumer_context.cpp", + "src/js_input_consumer_util.cpp", + "src/native_register_module.cpp", ] configs = [ ":inputconsumer_config" ] @@ -39,7 +39,6 @@ ohos_shared_library("inputconsumer") { ] external_deps = [ - "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "napi:ace_napi", ] diff --git a/frameworks/napi/input_consumer/include/js_register_module.h b/frameworks/napi/input_consumer/include/js_input_consumer_context.h similarity index 43% rename from frameworks/napi/input_consumer/include/js_register_module.h rename to frameworks/napi/input_consumer/include/js_input_consumer_context.h index c6da2a7c20..fe748283f5 100644 --- a/frameworks/napi/input_consumer/include/js_register_module.h +++ b/frameworks/napi/input_consumer/include/js_input_consumer_context.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,50 +12,50 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef JS_REGISTER_MODULE_H -#define JS_REGISTER_MODULE_H +#ifndef JS_INPUT_CONSUMER_CONTEXT_H +#define JS_INPUT_CONSUMER_CONTEXT_H -#include -#include -#include #include #include -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "utils/log.h" - #include "key_event.h" #include "key_option.h" -#include "define_multimodal.h" - -#define SUCCESS_CODE 0 -#define ERROR_CODE (-1) -#define UNREGISTERED_CODE (-2) -#define PRE_KEY_MAX_COUNT 4 - -enum JS_CALLBACK_EVENT { - JS_CALLBACK_EVENT_FAILED = -1, - JS_CALLBACK_EVENT_SUCCESS = 1, - JS_CALLBACK_EVENT_EXIST = 2, - JS_CALLBACK_EVENT_NOT_EXIST = 3, -}; +#include "util_napi.h" namespace OHOS { namespace MMI { -struct KeyEventMonitorInfo { +struct SubscribeInfo { + SubscribeInfo() {} + SubscribeInfo(napi_env env) : env(env) {} napi_env env { nullptr }; - napi_async_work asyncWork { nullptr }; std::string eventType; - std::string name; - napi_value handle { nullptr }; std::shared_ptr keyEvent { nullptr }; - napi_ref callback[1] {}; + napi_ref callback { nullptr }; int32_t subscribeId { 0 }; std::shared_ptr keyOption { nullptr }; }; -typedef std::map> Callbacks; +typedef std::map>> Callbacks; + +class JsInputConsumerContext final { +public: + JsInputConsumerContext() = default; + DISALLOW_COPY_AND_MOVE(JsInputConsumerContext); + ~JsInputConsumerContext() = default; + static napi_value Init(napi_env env, napi_value exports); + +private: + static napi_value JsOff(napi_env env, napi_callback_info info); + static napi_value JsOn(napi_env env, napi_callback_info info); + static int32_t GetSubEventInfo(napi_env env, size_t argc, napi_value* argv, + std::shared_ptr &event); + static std::shared_ptr CreateKeyOption(napi_env env, napi_value object); + static std::string GetSubKeyNames(const std::shared_ptr &keyOption); + static void SubKeyEventCallback(std::shared_ptr keyEvent); + +private: + static Callbacks callbacks; +}; } // namespace MMI } // namespace OHOS -#endif // JS_REGISTER_MODULE_H \ No newline at end of file +#endif // JS_INPUT_CONSUMER_CONTEXT_H diff --git a/frameworks/napi/input_consumer/include/js_input_consumer_util.h b/frameworks/napi/input_consumer/include/js_input_consumer_util.h new file mode 100644 index 0000000000..98b21b353c --- /dev/null +++ b/frameworks/napi/input_consumer/include/js_input_consumer_util.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef JS_INPUT_CONSUMER_UTIL_H +#define JS_INPUT_CONSUMER_UTIL_H + +#include + +#include "js_input_consumer_context.h" + +namespace OHOS { +namespace MMI { + +constexpr int32_t INVALID_SUBID = -1; + +class JsInputConsumerUtil final { +public: + JsInputConsumerUtil() = default; + DISALLOW_COPY_AND_MOVE(JsInputConsumerUtil); + ~JsInputConsumerUtil() = default; + + static bool GetNamedPropertyBool(napi_env env, napi_value object, const std::string &name, bool &ret); + static bool GetNamedPropertyInt32(napi_env env, napi_value object, const std::string &name, int32_t &value); + static bool GetPreKeys(napi_env env, napi_value object, const std::string &name, std::set &preKeys); + + static bool MatchCombinationKeys(const std::shared_ptr &event, + const std::shared_ptr &keyEvent); + static int32_t GetPreSubscribeId(const Callbacks &callbacks, const std::shared_ptr &event); + static int32_t AddEventCallback(napi_env env, const std::shared_ptr &event, Callbacks &callbacks); + static int32_t DelEventCallback(napi_env env, const std::shared_ptr &event, + Callbacks &callbacks, int32_t &subscribeId); + static void EmitAsyncCallbackWork(const std::shared_ptr &event); + +private: + static bool IsMatchKeyAction(bool isFinalKeydown, int32_t keyAction); + static void UvQueueWorkAsyncCallback(uv_work_t *work, int32_t status); + static void AsyncWorkFn(napi_env env, const std::shared_ptr &event, napi_value &result); + static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, int32_t value); + static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, std::string value); +}; +} // namespace MMI +} // namespace OHOS +#endif // JS_INPUT_CONSUMER_UTIL_H diff --git a/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp new file mode 100644 index 0000000000..30124a4333 --- /dev/null +++ b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_input_consumer_context.h" + +#include "error_multimodal.h" +#include "input_manager.h" +#include "js_input_consumer_util.h" +#include "mmi_log.h" +#include "napi_constants.h" +#include "util_napi_error.h" + +namespace OHOS { +namespace MMI { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JSInputConsumerContext" }; +constexpr size_t EVENT_NAME_LEN = 64; +} // namespace + +Callbacks JsInputConsumerContext::callbacks = {}; + +std::string JsInputConsumerContext::GetSubKeyNames(const std::shared_ptr &keyOption) +{ + CHKPS(keyOption); + std::string subKeyNames = ""; + std::set preKeys { keyOption->GetPreKeys() }; + for (const auto &preKey : preKeys) { + subKeyNames += std::to_string(preKey); + subKeyNames += ","; + } + subKeyNames += std::to_string(keyOption->GetFinalKey()); + subKeyNames += ","; + subKeyNames += std::to_string(keyOption->IsFinalKeyDown()); + subKeyNames += ","; + subKeyNames += std::to_string(keyOption->GetFinalKeyDownDuration()); + MMI_HILOGD("SubKey Names:%{public}s", subKeyNames.c_str()); + return subKeyNames; +} + +std::shared_ptr JsInputConsumerContext::CreateKeyOption(napi_env env, napi_value object) +{ + auto keyOption = std::make_shared(); + std::set preKeys; + if (!JsInputConsumerUtil::GetPreKeys(env, object, "preKeys", preKeys)) { + MMI_HILOGE("Get preKeys failed"); + return nullptr; + } + keyOption->SetPreKeys(preKeys); + + int32_t finalKey = 0; + if (!JsInputConsumerUtil::GetNamedPropertyInt32(env, object, "finalKey", finalKey)) { + MMI_HILOGE("Get NamedProperty failed"); + return nullptr; + } + keyOption->SetFinalKey(finalKey); + + bool isFinalKeyDown; + if (!JsInputConsumerUtil::GetNamedPropertyBool(env, object, "isFinalKeyDown", isFinalKeyDown)) { + MMI_HILOGE("GetNamedPropertyBool failed"); + return nullptr; + } + keyOption->SetFinalKeyDown(isFinalKeyDown); + + int32_t finalKeyDownDuration = 0; + if (!JsInputConsumerUtil::GetNamedPropertyInt32(env, object, "finalKeyDownDuration", finalKeyDownDuration)) { + MMI_HILOGE("Get NamedProperty failed"); + return nullptr; + } + keyOption->SetFinalKeyDownDuration(finalKeyDownDuration); + return keyOption; +} + +int32_t JsInputConsumerContext::GetSubEventInfo(napi_env env, size_t argc, + napi_value* argv, std::shared_ptr &event) +{ + CALL_DEBUG_ENTER; + CHKPR(event, ERROR_NULL_POINTER); + CHKPR(argv, ERROR_NULL_POINTER); + + if (!UtilNapi::TypeOf(env, argv[0], napi_string)) { + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string"); + MMI_HILOGE("The first parameter is not string"); + return RET_ERR; + } + char SubType[EVENT_NAME_LEN] = { 0 }; + size_t typeLen = 0; + CHKRR(napi_get_value_string_utf8(env, argv[0], SubType, EVENT_NAME_LEN - 1, &typeLen), + GET_VALUE_STRING_UTF8, RET_ERR); + if (std::string(SubType) != SUBSCRIBE_TYPE) { + MMI_HILOGE("Type is not key"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be key"); + return RET_ERR; + } + + if (!UtilNapi::TypeOf(env, argv[1], napi_object)) { + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keyOptions", "object"); + MMI_HILOGE("The second parameter is not napi_object"); + return RET_ERR; + } + event->keyOption = CreateKeyOption(env, argv[1]); + if (event->keyOption == nullptr) { + MMI_HILOGE("Failed to create a keyOption"); + return RET_ERR; + } + event->eventType = GetSubKeyNames(event->keyOption); + if (argc == ARGC_THREE) { + if (!UtilNapi::TypeOf(env, argv[2], napi_function)) { + MMI_HILOGE("The third parameter is not napi_function"); + THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function"); + return RET_ERR; + } + const uint32_t refCount = 1; + CHKRR(napi_create_reference(env, argv[2], refCount, &event->callback), CREATE_REFERENCE, RET_ERR); + } + return RET_OK; +} + +void JsInputConsumerContext::SubKeyEventCallback(std::shared_ptr keyEvent) +{ + CALL_DEBUG_ENTER; + CHKPV(keyEvent); + for (auto iter = callbacks.begin(); iter != callbacks.end(); ++iter) { + auto &infoList = iter->second; + MMI_HILOGD("Info list size:%{public}zu, SubKey Names:%{public}s", infoList.size(), (iter->first).c_str()); + for (const auto &info : infoList) { + if (JsInputConsumerUtil::MatchCombinationKeys(info, keyEvent)) { + info->keyEvent = keyEvent; + JsInputConsumerUtil::EmitAsyncCallbackWork(info); + } + } + } +} + +napi_value JsInputConsumerContext::JsOn(napi_env env, napi_callback_info info) +{ + CALL_DEBUG_ENTER; + size_t argc = ARGC_THREE; + napi_value argv[ARGC_THREE] = { nullptr }; + CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); + if (argc < ARGC_THREE) { + MMI_HILOGE("Parameter number error"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error"); + return nullptr; + } + auto event = std::make_shared(env); + if (GetSubEventInfo(env, argc, argv, event) != RET_OK) { + MMI_HILOGE("GetEventInfo failed"); + return nullptr; + } + int32_t preSubscribeId = JsInputConsumerUtil::GetPreSubscribeId(callbacks, event); + if (preSubscribeId < 0) { + int32_t subscribeId = INVALID_SUBID; + subscribeId = InputMgr->SubscribeKeyEvent(event->keyOption, SubKeyEventCallback); + if (subscribeId < 0) { + MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId); + napi_delete_reference(env, event->callback); + return nullptr; + } + MMI_HILOGD("SubscribeId:%{public}d", subscribeId); + event->subscribeId = subscribeId; + } else { + event->subscribeId = preSubscribeId; + } + if (JsInputConsumerUtil::AddEventCallback(env, event, callbacks) != RET_OK) { + MMI_HILOGE("AddEventCallback failed"); + } + return nullptr; +} + +napi_value JsInputConsumerContext::JsOff(napi_env env, napi_callback_info info) +{ + CALL_DEBUG_ENTER; + size_t argc = ARGC_THREE; + napi_value argv[ARGC_THREE] = { nullptr }; + CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); + if (argc < ARGC_TWO) { + MMI_HILOGE("Parameter number error"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error"); + return nullptr; + } + auto event = std::make_shared(env); + if (GetSubEventInfo(env, argc, argv, event) != RET_OK) { + MMI_HILOGE("Get SubEventInfo failed"); + return nullptr; + } + int32_t subscribeId = INVALID_SUBID; + if (JsInputConsumerUtil::DelEventCallback(env, event, callbacks, subscribeId) != RET_OK) { + MMI_HILOGE("DelEventCallback failed"); + return nullptr; + } + if (subscribeId > INVALID_SUBID) { + InputMgr->UnsubscribeKeyEvent(subscribeId); + MMI_HILOGD("UnsubscribeId:%{public}d", subscribeId); + } + if (event->callback != nullptr) { + CHKRP(napi_delete_reference(env, event->callback), DELETE_REFERENCE); + } + return nullptr; +} + +napi_value JsInputConsumerContext::Init(napi_env env, napi_value exports) +{ + CALL_DEBUG_ENTER; + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("on", JsOn), + DECLARE_NAPI_FUNCTION("off", JsOff), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +} // namespace MMI +} // namespace OHOS diff --git a/frameworks/napi/input_consumer/src/js_register_util.cpp b/frameworks/napi/input_consumer/src/js_input_consumer_util.cpp similarity index 38% rename from frameworks/napi/input_consumer/src/js_register_util.cpp rename to frameworks/napi/input_consumer/src/js_input_consumer_util.cpp index 84cbbd64bb..9e61dbcaa0 100644 --- a/frameworks/napi/input_consumer/src/js_register_util.cpp +++ b/frameworks/napi/input_consumer/src/js_input_consumer_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,125 +13,188 @@ * limitations under the License. */ -#include "js_register_util.h" - -#include - -#include +#include "js_input_consumer_util.h" #include "error_multimodal.h" +#include "mmi_log.h" #include "napi_constants.h" #include "util_napi_error.h" -#include "util_napi.h" namespace OHOS { namespace MMI { namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JSRegisterUtil" }; +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JSInputConsumerUtil" }; +constexpr size_t PRE_KEYS_SIZE = 4; } // namespace -void SetNamedProperty(const napi_env &env, napi_value &object, const std::string &name, int32_t value) +void JsInputConsumerUtil::SetNamedProperty(napi_env env, + napi_value object, const std::string &name, int32_t value) { - MMI_HILOGD("%{public}s=%{public}d", name.c_str(), value); - napi_value napiValue; + MMI_HILOGD("Set named property %{public}s=%{public}d", name.c_str(), value); + napi_value napiValue = nullptr; CHKRV(napi_create_int32(env, value, &napiValue), CREATE_INT32); NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name.c_str(), napiValue)); } -void SetNamedProperty(const napi_env &env, napi_value &object, const std::string &name, std::string value) +void JsInputConsumerUtil::SetNamedProperty(napi_env env, + napi_value object, const std::string &name, std::string value) { - MMI_HILOGD("%{public}s=%{public}s", name.c_str(), value.c_str()); - napi_value napiValue; + MMI_HILOGD("Set named property %{public}s=%{public}s", name.c_str(), value.c_str()); + napi_value napiValue = nullptr; CHKRV(napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &napiValue), CREATE_STRING_UTF8); NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name.c_str(), napiValue)); } -bool GetNamedPropertyBool(const napi_env &env, const napi_value &object, const std::string &name, bool &ret) +bool JsInputConsumerUtil::GetNamedPropertyBool(napi_env env, + napi_value object, const std::string &name, bool &state) { - napi_value napiValue = {}; - napi_get_named_property(env, object, name.c_str(), &napiValue); - napi_valuetype tmpType = napi_undefined; - - CHKRF(napi_typeof(env, napiValue, &tmpType), TYPEOF); - if (tmpType != napi_boolean) { + napi_value napiValue = nullptr; + CHKRF(napi_get_named_property(env, object, name.c_str(), &napiValue), GET_NAMED_PROPERTY); + if (!UtilNapi::TypeOf(env, napiValue, napi_boolean)) { MMI_HILOGE("The value is not bool"); THROWERR_API9(env, COMMON_PARAMETER_ERROR, name.c_str(), "bool"); return false; } - CHKRF(napi_get_value_bool(env, napiValue, &ret), GET_VALUE_BOOL); - MMI_HILOGD("%{public}s=%{public}d", name.c_str(), ret); + CHKRF(napi_get_value_bool(env, napiValue, &state), GET_VALUE_BOOL); return true; } -std::optional GetNamedPropertyInt32(const napi_env &env, const napi_value &object, const std::string &name) +bool JsInputConsumerUtil::GetNamedPropertyInt32(napi_env env, + napi_value object, const std::string &name, int32_t &result) { - napi_value napiValue = {}; - napi_get_named_property(env, object, name.c_str(), &napiValue); - napi_valuetype tmpType = napi_undefined; - if (napi_typeof(env, napiValue, &tmpType) != napi_ok) { - MMI_HILOGE("Call napi_typeof failed"); - return std::nullopt; - } - if (tmpType != napi_number) { + napi_value napiValue = nullptr; + CHKRF(napi_get_named_property(env, object, name.c_str(), &napiValue), GET_NAMED_PROPERTY); + if (!UtilNapi::TypeOf(env, napiValue, napi_number)) { MMI_HILOGE("The value is not number"); THROWERR_API9(env, COMMON_PARAMETER_ERROR, name.c_str(), "number"); - return std::nullopt; + return false; } - int32_t ret; - if (napi_get_value_int32(env, napiValue, &ret) != napi_ok) { - MMI_HILOGE("Call napi_get_value_int32 failed"); - return std::nullopt; + CHKRF(napi_get_value_int32(env, napiValue, &result), GET_VALUE_INT32); + if (result < 0) { + MMI_HILOGE("finalKey:%{public}d is less 0, can not process", result); + std::string temp = name + "must be greater than or equal to 0"; + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, temp); + return false; } - MMI_HILOGD("%{public}s=%{public}d", name.c_str(), ret); - return std::make_optional(ret); + return true; } -napi_value GetPreKeys(const napi_env &env, const napi_value &value, std::set ¶ms) +bool JsInputConsumerUtil::GetPreKeys(napi_env env, + napi_value object, const std::string &name, std::set &preKeys) { - CALL_DEBUG_ENTER; + napi_value receiveValue = nullptr; + CHKRF(napi_get_named_property(env, object, name.c_str(), &receiveValue), GET_NAMED_PROPERTY); + if (receiveValue == nullptr) { + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "preKeys not found"); + return false; + } uint32_t arrayLength = 0; - CHKRP(napi_get_array_length(env, value, &arrayLength), GET_ARRAY_LENGTH); - for (uint32_t i = 0; i < arrayLength; i++) { - napi_value napiElement; - CHKRP(napi_get_element(env, value, i, &napiElement), GET_ELEMENT); - napi_valuetype valuetype; - CHKRP(napi_typeof(env, napiElement, &valuetype), TYPEOF); - if (valuetype != napi_number) { + CHKRF(napi_get_array_length(env, receiveValue, &arrayLength), GET_ARRAY_LENGTH); + for (uint32_t i = 0; i < arrayLength; ++i) { + napi_value napiElement = nullptr; + CHKRF(napi_get_element(env, receiveValue, i, &napiElement), GET_ELEMENT); + if (!UtilNapi::TypeOf(env, napiElement, napi_number)) { MMI_HILOGE("PreKeys Wrong argument type, Number expected"); THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "element of preKeys must be number"); - return nullptr; + return false; } - int32_t value = 0; - CHKRP(napi_get_value_int32(env, napiElement, &value), GET_VALUE_INT32); - if (value < 0) { - MMI_HILOGE("preKey:%{public}d is less 0, can not process", value); + int32_t preKey = 0; + CHKRF(napi_get_value_int32(env, napiElement, &preKey), GET_VALUE_INT32); + if (preKey < 0) { + MMI_HILOGE("preKey:%{public}d is less 0, can not process", preKey); THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "element of preKeys must be greater than or equal to 0"); - return nullptr; + return false; } - MMI_HILOGD("Get int array number:%{public}d", value); - if (!params.insert(value).second) { + if (!preKeys.insert(preKey).second) { MMI_HILOGE("Params insert value failed"); - return nullptr; + return false; } } - napi_value ret; - CHKRP(napi_create_int32(env, RET_OK, &ret), CREATE_INT32); - return ret; + if (preKeys.size() > PRE_KEYS_SIZE) { + MMI_HILOGE("preKeys size invalid"); + THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "preKeys size invalid"); + return false; + } + return true; } -int32_t GetPreSubscribeId(Callbacks &callbacks, KeyEventMonitorInfo *event) +bool JsInputConsumerUtil::IsMatchKeyAction(bool isFinalKeydown, int32_t keyAction) { - CHKPR(event, ERROR_NULL_POINTER); + CALL_DEBUG_ENTER; + MMI_HILOGD("isFinalKeydown:%{public}d,keyAction:%{public}d", isFinalKeydown, keyAction); + if ((isFinalKeydown && keyAction == KeyEvent::KEY_ACTION_DOWN) || + (!isFinalKeydown && keyAction == KeyEvent::KEY_ACTION_UP)) { + return true; + } + MMI_HILOGE("isFinalKeydown not matched with keyAction"); + return false; +} + +bool JsInputConsumerUtil::MatchCombinationKeys(const std::shared_ptr &event, + const std::shared_ptr &keyEvent) +{ + CALL_DEBUG_ENTER; + CHKPF(event); + CHKPF(keyEvent); + auto keyOption = event->keyOption; + CHKPF(keyOption); + std::vector items = keyEvent->GetKeyItems(); + int32_t infoFinalKey = keyOption->GetFinalKey(); + int32_t keyEventFinalKey = keyEvent->GetKeyCode(); + bool isFinalKeydown = keyOption->IsFinalKeyDown(); + MMI_HILOGD("infoFinalKey:%{public}d,keyEventFinalKey:%{public}d", infoFinalKey, keyEventFinalKey); + if (infoFinalKey != keyEventFinalKey || items.size() > PRE_KEYS_SIZE || + !IsMatchKeyAction(isFinalKeydown, keyEvent->GetKeyAction())) { + MMI_HILOGE("Param invalid"); + return false; + } + std::set infoPreKeys = keyOption->GetPreKeys(); + int32_t infoSize = 0; + for (const auto &preKey : infoPreKeys) { + if (preKey >= 0) { + infoSize++; + } + } + int32_t count = 0; + for (const auto &item : items) { + if (item.GetKeyCode() == keyEventFinalKey) { + continue; + } + auto iter = find(infoPreKeys.begin(), infoPreKeys.end(), item.GetKeyCode()); + if (iter == infoPreKeys.end()) { + MMI_HILOGW("No keyCode in preKeys"); + return false; + } + count++; + } + MMI_HILOGD("kevEventSize:%{public}d,infoSize:%{public}d", count, infoSize); + auto keyItem = keyEvent->GetKeyItem(); + CHKPF(keyItem); + auto upTime = keyEvent->GetActionTime(); + auto downTime = keyItem->GetDownTime(); + auto curDurationTime = keyOption->GetFinalKeyDownDuration(); + if (curDurationTime > 0 && (upTime - downTime >= (static_cast(curDurationTime) * 1000))) { + MMI_HILOGE("Skip, upTime - downTime >= duration"); + return false; + } + return count == infoSize; +} + +int32_t JsInputConsumerUtil::GetPreSubscribeId(const Callbacks &callbacks, + const std::shared_ptr &event) +{ + CHKPR(event, INVALID_SUBID); auto it = callbacks.find(event->eventType); if (it == callbacks.end() || it->second.empty()) { - MMI_HILOGE("The callbacks is empty"); - return JS_CALLBACK_EVENT_FAILED; + MMI_HILOGW("The callbacks is empty"); + return INVALID_SUBID; } - CHKPR(it->second.front(), ERROR_NULL_POINTER); + CHKPR(it->second.front(), INVALID_SUBID); return it->second.front()->subscribeId; } -int32_t AddEventCallback(const napi_env &env, Callbacks &callbacks, KeyEventMonitorInfo *event) +int32_t JsInputConsumerUtil::AddEventCallback(napi_env env, + const std::shared_ptr &event, Callbacks &callbacks) { CALL_DEBUG_ENTER; CHKPR(event, ERROR_NULL_POINTER); @@ -140,158 +203,111 @@ int32_t AddEventCallback(const napi_env &env, Callbacks &callbacks, KeyEventMoni callbacks[event->eventType] = {}; } napi_value handler1 = nullptr; - napi_status status = napi_get_reference_value(env, event->callback[0], &handler1); - if (status != napi_ok) { - MMI_HILOGE("Handler1 get reference value failed"); - return JS_CALLBACK_EVENT_FAILED; - } - auto it = callbacks.find(event->eventType); - for (const auto &iter : it->second) { + CHKRR(napi_get_reference_value(env, event->callback, &handler1), GET_REFERENCE_VALUE, RET_ERR); + auto iter = callbacks.find(event->eventType); + for (const auto &info : iter->second) { napi_value handler2 = nullptr; - status = napi_get_reference_value(env, (*iter).callback[0], &handler2); - if (status != napi_ok) { - MMI_HILOGE("Handler2 get reference value failed"); - return JS_CALLBACK_EVENT_FAILED; - } + CHKRR(napi_get_reference_value(env, info->callback, &handler2), GET_REFERENCE_VALUE, RET_ERR); bool isEqual = false; - status = napi_strict_equals(env, handler1, handler2, &isEqual); - if (status != napi_ok) { - MMI_HILOGE("Compare two handler failed"); - return JS_CALLBACK_EVENT_FAILED; - } + CHKRR(napi_strict_equals(env, handler1, handler2, &isEqual), STRICT_EQUALS, RET_ERR); if (isEqual) { - MMI_HILOGE("Callback already exist"); - return JS_CALLBACK_EVENT_FAILED; + MMI_HILOGW("Callback already exist"); + return RET_OK; } } - it->second.push_back(event); - return JS_CALLBACK_EVENT_SUCCESS; + iter->second.push_back(event); + return RET_OK; } -int32_t DelEventCallback(const napi_env &env, Callbacks &callbacks, - KeyEventMonitorInfo *event, int32_t &subscribeId) +int32_t JsInputConsumerUtil::DelEventCallback(napi_env env, + const std::shared_ptr &event, Callbacks &callbacks, int32_t &subscribeId) { CALL_DEBUG_ENTER; CHKPR(event, ERROR_NULL_POINTER); if (callbacks.count(event->eventType) <= 0) { MMI_HILOGE("Callback doesn't exists"); - return JS_CALLBACK_EVENT_FAILED; + return RET_ERR; } - auto &info = callbacks[event->eventType]; - MMI_HILOGD("EventType:%{public}s, keyEventMonitorInfos:%{public}zu", - event->eventType.c_str(), info.size()); + auto &infos = callbacks[event->eventType]; + MMI_HILOGD("EventType:%{public}s,keySubInfos:%{public}zu", event->eventType.c_str(), infos.size()); napi_value handler1 = nullptr; - napi_status status; - if (event->callback[0] != nullptr) { - status = napi_get_reference_value(env, event->callback[0], &handler1); - if (status != napi_ok) { - MMI_HILOGE("Handler1 get reference value failed"); - return JS_CALLBACK_EVENT_FAILED; - } + if (event->callback != nullptr) { + CHKRR(napi_get_reference_value(env, event->callback, &handler1), GET_REFERENCE_VALUE, RET_ERR); } - for (auto iter = info.begin(); iter != info.end();) { + bool isEquals = false; + for (auto iter = infos.begin(); iter != infos.end();) { if (*iter == nullptr) { - info.erase(iter++); + infos.erase(iter++); continue; } if (handler1 != nullptr) { napi_value handler2 = nullptr; - status = napi_get_reference_value(env, (*iter)->callback[0], &handler2); - if (status != napi_ok) { - MMI_HILOGE("Handler2 get reference value failed"); - return JS_CALLBACK_EVENT_FAILED; - } - bool isEquals = false; - status = napi_strict_equals(env, handler1, handler2, &isEquals); - if (status != napi_ok) { - MMI_HILOGE("Compare two handler failed"); - return JS_CALLBACK_EVENT_FAILED; - } - if (isEquals) { - status = napi_delete_reference(env, (*iter)->callback[0]); - if (status != napi_ok) { - MMI_HILOGE("Delete reference failed"); - return JS_CALLBACK_EVENT_FAILED; - } - KeyEventMonitorInfo *monitorInfo = *iter; - info.erase(iter++); - if (info.empty()) { - subscribeId = monitorInfo->subscribeId; - } - delete monitorInfo; - monitorInfo = nullptr; - MMI_HILOGD("Callback has deleted, size:%{public}zu", info.size()); - return JS_CALLBACK_EVENT_SUCCESS; - } - ++iter; - continue; + CHKRR(napi_get_reference_value(env, (*iter)->callback, &handler2), GET_REFERENCE_VALUE, RET_ERR); + CHKRR(napi_strict_equals(env, handler1, handler2, &isEquals), STRICT_EQUALS, RET_ERR); } - status = napi_delete_reference(env, (*iter)->callback[0]); - if (status != napi_ok) { + if (napi_delete_reference(env, (*iter)->callback) != napi_ok) { MMI_HILOGE("Delete reference failed"); napi_throw_error(env, nullptr, "Delete reference failed"); - return JS_CALLBACK_EVENT_FAILED; + return RET_ERR; + } + auto info = *iter; + infos.erase(iter++); + if (infos.empty()) { + subscribeId = info->subscribeId; } - KeyEventMonitorInfo *monitorInfo = *iter; - info.erase(iter++); - if (info.empty()) { - subscribeId = monitorInfo->subscribeId; + info = nullptr; + MMI_HILOGD("Callback has deleted, size:%{public}zu", infos.size()); + if (isEquals) { + break; } - delete monitorInfo; - monitorInfo = nullptr; - MMI_HILOGD("Callback has deleted, size:%{public}zu", info.size()); } - MMI_HILOGD("Callback size:%{public}zu", info.size()); - return JS_CALLBACK_EVENT_SUCCESS; + MMI_HILOGD("Callback size:%{public}zu", infos.size()); + return RET_OK; } -static void AsyncWorkFn(const napi_env &env, KeyEventMonitorInfo *event, napi_value &result) +void JsInputConsumerUtil::AsyncWorkFn(napi_env env, const std::shared_ptr &event, napi_value &result) { CHKPV(event); CHKPV(event->keyOption); - MMI_HILOGD("Status > 0 enter"); CHKRV(napi_create_object(env, &result), CREATE_OBJECT); - napi_value arr; + napi_value arr = nullptr; CHKRV(napi_create_array(env, &arr), CREATE_ARRAY); std::set preKeys = event->keyOption->GetPreKeys(); int32_t i = 0; - napi_value value; + napi_value value = nullptr; for (const auto &preKey : preKeys) { CHKRV(napi_create_int32(env, preKey, &value), CREATE_INT32); CHKRV(napi_set_element(env, arr, i, value), SET_ELEMENT); ++i; } - std::string preKeysStr = "preKeys"; - NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, preKeysStr.c_str(), arr)); - MMI::SetNamedProperty(env, result, "finalKey", event->keyOption->GetFinalKey()); - MMI::SetNamedProperty(env, result, "isFinalKeyDown", event->keyOption->IsFinalKeyDown()); - MMI::SetNamedProperty(env, result, "finalKeyDownDuration", event->keyOption->GetFinalKeyDownDuration()); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "preKeys", arr)); + SetNamedProperty(env, result, "finalKey", event->keyOption->GetFinalKey()); + SetNamedProperty(env, result, "isFinalKeyDown", event->keyOption->IsFinalKeyDown()); + SetNamedProperty(env, result, "finalKeyDownDuration", event->keyOption->GetFinalKeyDownDuration()); } -struct KeyEventMonitorInfoWorker { +struct SubscribeInfoWorker { napi_env env { nullptr }; - KeyEventMonitorInfo *reportEvent { nullptr }; + std::shared_ptr reportEvent { nullptr }; }; -void UvQueueWorkAsyncCallback(uv_work_t *work, int32_t status) +void JsInputConsumerUtil::UvQueueWorkAsyncCallback(uv_work_t *work, int32_t status) { CALL_DEBUG_ENTER; + (void)status; CHKPV(work); if (work->data == nullptr) { MMI_HILOGE("Check data is null"); delete work; - work = nullptr; return; } - (void)status; - KeyEventMonitorInfoWorker *dataWorker = static_cast(work->data); - delete work; - work = nullptr; - KeyEventMonitorInfo *event = dataWorker->reportEvent; + auto dataWorker = static_cast(work->data); + auto event = dataWorker->reportEvent; napi_env env = dataWorker->env; + delete work; delete dataWorker; - dataWorker = nullptr; CHKPV(event); + napi_handle_scope scope = nullptr; napi_open_handle_scope(env, &scope); if (scope == nullptr) { @@ -299,7 +315,7 @@ void UvQueueWorkAsyncCallback(uv_work_t *work, int32_t status) return; } napi_value callback = nullptr; - CHKRV_SCOPE(env, napi_get_reference_value(env, event->callback[0], &callback), GET_REFERENCE_VALUE, scope); + CHKRV_SCOPE(env, napi_get_reference_value(env, event->callback, &callback), GET_REFERENCE_VALUE, scope); napi_value result = nullptr; AsyncWorkFn(env, event, result); napi_value callResult = nullptr; @@ -307,7 +323,7 @@ void UvQueueWorkAsyncCallback(uv_work_t *work, int32_t status) napi_close_handle_scope(env, scope); } -void EmitAsyncCallbackWork(KeyEventMonitorInfo *reportEvent) +void JsInputConsumerUtil::EmitAsyncCallbackWork(const std::shared_ptr &reportEvent) { CALL_DEBUG_ENTER; CHKPV(reportEvent); @@ -315,7 +331,7 @@ void EmitAsyncCallbackWork(KeyEventMonitorInfo *reportEvent) CHKRV(napi_get_uv_event_loop(reportEvent->env, &loop), GET_UV_EVENT_LOOP); uv_work_t *work = new (std::nothrow) uv_work_t; CHKPV(work); - KeyEventMonitorInfoWorker *dataWorker = new (std::nothrow) KeyEventMonitorInfoWorker(); + auto dataWorker = new (std::nothrow) SubscribeInfoWorker(); if (dataWorker == nullptr) { MMI_HILOGE("dataWorker is nullptr"); delete work; diff --git a/frameworks/napi/input_consumer/src/js_register_module.cpp b/frameworks/napi/input_consumer/src/js_register_module.cpp deleted file mode 100644 index 7f4f4e34d7..0000000000 --- a/frameworks/napi/input_consumer/src/js_register_module.cpp +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "js_register_module.h" - -#include -#include - -#include "input_manager.h" -#include "js_register_util.h" -#include "napi_constants.h" -#include "util_napi_error.h" -#include "util_napi.h" - -namespace OHOS { -namespace MMI { -namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JSRegisterModule" }; -constexpr size_t EVENT_NAME_LEN = 64; -constexpr size_t PRE_KEYS_SIZE = 4; -} // namespace - -static Callbacks callbacks = {}; - -napi_value GetEventInfoAPI9(napi_env env, napi_callback_info info, KeyEventMonitorInfo* event, - std::shared_ptr keyOption) -{ - CALL_DEBUG_ENTER; - CHKPP(event); - CHKPP(keyOption); - size_t argc = 3; - napi_value argv[3] = { 0 }; - CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); - napi_valuetype valueType = napi_undefined; - if (!UtilNapi::TypeOf(env, argv[0], napi_string)) { - THROWERR_API9(env, COMMON_PARAMETER_ERROR, "type", "string"); - MMI_HILOGE("The first parameter is not string"); - return nullptr; - } - if (!UtilNapi::TypeOf(env, argv[1], napi_object)) { - THROWERR_API9(env, COMMON_PARAMETER_ERROR, "keyOptions", "object"); - MMI_HILOGE("The second parameter is not napi_object"); - return nullptr; - } - char eventType[EVENT_NAME_LEN] = { 0 }; - size_t typeLen = 0; - CHKRP(napi_get_value_string_utf8(env, argv[0], eventType, EVENT_NAME_LEN - 1, &typeLen), GET_VALUE_STRING_UTF8); - std::string type = eventType; - if (type != SUBSCRIBE_TYPE) { - MMI_HILOGE("Type is not key"); - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "type must be key"); - return nullptr; - } - napi_value receiveValue = nullptr; - CHKRP(napi_get_named_property(env, argv[1], "preKeys", &receiveValue), GET_NAMED_PROPERTY); - if (receiveValue == nullptr) { - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "preKeys not found"); - return nullptr; - } - std::set preKeys; - if (GetPreKeys(env, receiveValue, preKeys) == nullptr) { - MMI_HILOGE("Get preKeys failed"); - return nullptr; - } - if (preKeys.size() > PRE_KEYS_SIZE) { - MMI_HILOGE("preKeys size invalid"); - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "preKeys size invalid"); - return nullptr; - } - MMI_HILOGD("PreKeys size:%{public}zu", preKeys.size()); - keyOption->SetPreKeys(preKeys); - std::string subKeyNames = ""; - for (const auto &item : preKeys) { - subKeyNames += std::to_string(item); - subKeyNames += ","; - MMI_HILOGD("preKeys:%{public}d", item); - } - std::optional tempFinalKey = GetNamedPropertyInt32(env, argv[1], "finalKey"); - if (!tempFinalKey) { - MMI_HILOGE("GetNamedPropertyInt32 failed"); - return nullptr; - } - int32_t finalKey = tempFinalKey.value(); - if (finalKey < 0) { - MMI_HILOGE("finalKey:%{public}d is less 0, can not process", finalKey); - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "finalKey must be greater than or equal to 0"); - return nullptr; - } - subKeyNames += std::to_string(finalKey); - subKeyNames += ","; - keyOption->SetFinalKey(finalKey); - MMI_HILOGD("FinalKey:%{public}d", finalKey); - bool isFinalKeyDown; - if (!GetNamedPropertyBool(env, argv[1], "isFinalKeyDown", isFinalKeyDown)) { - MMI_HILOGE("GetNamedPropertyBool failed"); - return nullptr; - } - subKeyNames += std::to_string(isFinalKeyDown); - subKeyNames += ","; - keyOption->SetFinalKeyDown(isFinalKeyDown); - MMI_HILOGD("IsFinalKeyDown:%{public}d,map_key:%{public}s", - (isFinalKeyDown == true?1:0), subKeyNames.c_str()); - std::optional tempKeyDownDuration = GetNamedPropertyInt32(env, argv[1], "finalKeyDownDuration"); - if (!tempKeyDownDuration) { - MMI_HILOGE("GetNamedPropertyInt32 failed"); - return nullptr; - } - int32_t finalKeyDownDuration = tempKeyDownDuration.value(); - if (finalKeyDownDuration < 0) { - MMI_HILOGE("finalKeyDownDuration:%{public}d is less 0, can not process", finalKeyDownDuration); - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "finalKeyDownDuration must be greater than or equal to 0"); - return nullptr; - } - subKeyNames += std::to_string(finalKeyDownDuration); - keyOption->SetFinalKeyDownDuration(finalKeyDownDuration); - event->eventType = subKeyNames; - MMI_HILOGD("FinalKeyDownDuration:%{public}d", finalKeyDownDuration); - if (argc == 3) { - CHKRP(napi_typeof(env, argv[2], &valueType), TYPEOF); - if (valueType != napi_function) { - MMI_HILOGE("the third parameter is not napi_function"); - THROWERR_API9(env, COMMON_PARAMETER_ERROR, "callback", "function"); - return nullptr; - } - CHKRP(napi_create_reference(env, argv[2], 1, &event->callback[0]), REFERENCE_REF); - } else { - event->callback[0] = nullptr; - } - napi_value ret; - CHKRP(napi_create_int32(env, RET_OK, &ret), CREATE_INT32); - return ret; -} - -static bool IsMatchKeyAction(bool isFinalKeydown, int32_t keyAction) -{ - CALL_DEBUG_ENTER; - MMI_HILOGD("isFinalKeydown:%{public}d,keyAction:%{public}d", isFinalKeydown, keyAction); - if (isFinalKeydown && keyAction == KeyEvent::KEY_ACTION_DOWN) { - return true; - } - - if (!isFinalKeydown && keyAction == KeyEvent::KEY_ACTION_UP) { - return true; - } - MMI_HILOGE("isFinalKeydown not matched with keyAction"); - return false; -} - -static bool MatchCombinationKeys(KeyEventMonitorInfo* monitorInfo, std::shared_ptr keyEvent) -{ - CALL_DEBUG_ENTER; - CHKPF(monitorInfo); - CHKPF(keyEvent); - auto keyOption = monitorInfo->keyOption; - CHKPF(keyOption); - std::vector items = keyEvent->GetKeyItems(); - int32_t infoFinalKey = keyOption->GetFinalKey(); - int32_t keyEventFinalKey = keyEvent->GetKeyCode(); - bool isFinalKeydown = keyOption->IsFinalKeyDown(); - MMI_HILOGD("infoFinalKey:%{public}d,keyEventFinalKey:%{public}d", infoFinalKey, keyEventFinalKey); - if (infoFinalKey != keyEventFinalKey || items.size() > PRE_KEYS_SIZE || - !IsMatchKeyAction(isFinalKeydown, keyEvent->GetKeyAction())) { - MMI_HILOGE("Param invalid"); - return false; - } - std::set infoPreKeys = keyOption->GetPreKeys(); - int32_t infoSize = 0; - auto it = infoPreKeys.begin(); - while (it != infoPreKeys.end()) { - if (*it >= 0) { - infoSize++; - } - ++it; - } - int32_t count = 0; - for (const auto &item : items) { - if (item.GetKeyCode() == keyEventFinalKey) { - continue; - } - auto iter = find(infoPreKeys.begin(), infoPreKeys.end(), item.GetKeyCode()); - if (iter == infoPreKeys.end()) { - MMI_HILOGW("No keyCode in preKeys"); - return false; - } - count++; - } - MMI_HILOGD("kevEventSize:%{public}d,infoSize:%{public}d", count, infoSize); - auto keyItem = keyEvent->GetKeyItem(); - CHKPF(keyItem); - auto upTime = keyEvent->GetActionTime(); - auto downTime = keyItem->GetDownTime(); - auto curDurationTime = keyOption->GetFinalKeyDownDuration(); - if (curDurationTime > 0 && (upTime - downTime >= (static_cast(curDurationTime) * 1000))) { - MMI_HILOGE("Skip, upTime - downTime >= duration"); - return false; - } - return count == infoSize; -} - -static void SubKeyEventCallback(std::shared_ptr keyEvent) -{ - CALL_DEBUG_ENTER; - CHKPV(keyEvent); - auto iter = callbacks.begin(); - while (iter != callbacks.end()) { - auto &list = iter->second; - ++iter; - MMI_HILOGD("list size:%{public}zu", list.size()); - auto infoIter = list.begin(); - while (infoIter != list.end()) { - auto monitorInfo = *infoIter; - if (MatchCombinationKeys(monitorInfo, keyEvent)) { - monitorInfo->keyEvent = keyEvent; - EmitAsyncCallbackWork(monitorInfo); - } - ++infoIter; - } - } -} - -static napi_value JsOn(napi_env env, napi_callback_info info) -{ - CALL_DEBUG_ENTER; - size_t argc = 3; - napi_value argv[3] = { 0 }; - CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); - if (argc < 3) { - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error"); - return nullptr; - } - KeyEventMonitorInfo *event = new (std::nothrow) KeyEventMonitorInfo { - .env = env, - .asyncWork = nullptr, - }; - CHKPP(event); - auto keyOption = std::make_shared(); - if ((keyOption) == nullptr) { - delete event; - MMI_HILOGE("Check keyOption is null"); - return nullptr; - } - napi_valuetype valueType = napi_undefined; - if (napi_typeof(env, argv[0], &valueType) != napi_ok) { - delete event; - MMI_HILOGE("Napi typeof failed"); - return nullptr; - } - if (GetEventInfoAPI9(env, info, event, keyOption) == nullptr) { - delete event; - MMI_HILOGE("GetEventInfo failed"); - return nullptr; - } - event->keyOption = keyOption; - int32_t preSubscribeId = GetPreSubscribeId(callbacks, event); - if (preSubscribeId < 0) { - MMI_HILOGD("eventType:%{public}s,eventName:%{public}s", event->eventType.c_str(), event->name.c_str()); - int32_t subscribeId = -1; - subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, SubKeyEventCallback); - if (subscribeId < 0) { - MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId); - napi_delete_reference(env, event->callback[0]); - delete event; - return nullptr; - } - MMI_HILOGD("SubscribeId:%{public}d", subscribeId); - event->subscribeId = subscribeId; - } else { - event->subscribeId = preSubscribeId; - } - if (AddEventCallback(env, callbacks, event) < 0) { - delete event; - MMI_HILOGE("AddEventCallback failed"); - return nullptr; - } - return nullptr; -} - -static napi_value JsOff(napi_env env, napi_callback_info info) -{ - CALL_DEBUG_ENTER; - size_t argc = 3; - napi_value argv[3] = { 0 }; - CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); - if (argc < 2) { - THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error"); - return nullptr; - } - KeyEventMonitorInfo *event = new (std::nothrow) KeyEventMonitorInfo { - .env = env, - .asyncWork = nullptr, - }; - CHKPP(event); - auto keyOption = std::make_shared(); - if (keyOption == nullptr) { - delete event; - MMI_HILOGE("Check keyOption is null"); - return nullptr; - } - napi_valuetype valueType = napi_undefined; - if (napi_typeof(env, argv[0], &valueType) != napi_ok) { - delete event; - MMI_HILOGE("Napi typeof failed"); - return nullptr; - } - if (GetEventInfoAPI9(env, info, event, keyOption) == nullptr) { - delete event; - MMI_HILOGE("GetEventInfo failed"); - return nullptr; - } - int32_t subscribeId = -1; - if (DelEventCallback(env, callbacks, event, subscribeId) < 0) { - delete event; - MMI_HILOGE("DelEventCallback failed"); - return nullptr; - } - MMI_HILOGD("SubscribeId:%{public}d", subscribeId); - if (subscribeId >= 0) { - InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId); - } - if (event->callback[0] != nullptr) { - napi_delete_reference(env, event->callback[0]); - } - delete event; - return nullptr; -} - -EXTERN_C_START -static napi_value MmiInit(napi_env env, napi_value exports) -{ - CALL_DEBUG_ENTER; - napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("on", JsOn), - DECLARE_NAPI_FUNCTION("off", JsOff), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - return exports; -} -EXTERN_C_END - -static napi_module mmiModule = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = MmiInit, - .nm_modname = "multimodalInput.inputConsumer", - .nm_priv = ((void*)0), - .reserved = { 0 }, -}; - -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module_register(&mmiModule); -} -} // namespace MMI -} // namespace OHOS \ No newline at end of file diff --git a/frameworks/napi/input_consumer/include/js_register_util.h b/frameworks/napi/input_consumer/src/native_register_module.cpp similarity index 34% rename from frameworks/napi/input_consumer/include/js_register_util.h rename to frameworks/napi/input_consumer/src/native_register_module.cpp index 129221f014..77c67b3e32 100644 --- a/frameworks/napi/input_consumer/include/js_register_util.h +++ b/frameworks/napi/input_consumer/src/native_register_module.cpp @@ -1,40 +1,41 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef JS_REGISTER_UTIL_H -#define JS_REGISTER_UTIL_H - -#include - -#include "js_register_module.h" -#include "key_event.h" - -namespace OHOS { -namespace MMI { -void SetNamedProperty(const napi_env &env, napi_value &object, const std::string &name, int32_t value); -void SetNamedProperty(const napi_env &env, napi_value &object, const std::string &name, std::string value); - -bool GetNamedPropertyBool(const napi_env &env, const napi_value &object, const std::string &name, bool &ret); -std::string GetNamedPropertyString(const napi_env &env, const napi_value &object, const std::string &name); -std::optional GetNamedPropertyInt32(const napi_env &env, const napi_value &object, const std::string &name); -napi_value GetPreKeys(const napi_env &env, const napi_value &value, std::set ¶ms); -int32_t GetPreSubscribeId(Callbacks &callbacks, KeyEventMonitorInfo *event); -int32_t AddEventCallback(const napi_env &env, Callbacks &callbacks, - KeyEventMonitorInfo *event); -int32_t DelEventCallback(const napi_env &env, Callbacks &callbacks, - KeyEventMonitorInfo *event, int32_t &subscribeId); -void EmitAsyncCallbackWork(KeyEventMonitorInfo *event); -} // namespace MMI -} // namespace OHOS -#endif // JS_REGISTER_UTIL_H \ No newline at end of file +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_input_consumer_context.h" + +namespace OHOS { +namespace MMI { + +napi_value Export(napi_env env, napi_value exports) +{ + return JsInputConsumerContext::Init(env, exports); +} + +static napi_module mmiInputConsumerModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "multimodalInput.inputConsumer", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&mmiInputConsumerModule); +} +} // namespace MMI +} // namespace OHOS diff --git a/util/common/include/util_napi.h b/util/common/include/util_napi.h index e69976b03a..21e8ad87c5 100644 --- a/util/common/include/util_napi.h +++ b/util/common/include/util_napi.h @@ -21,6 +21,12 @@ namespace OHOS { namespace MMI { + +static constexpr size_t ARGC_ONE = 1; +static constexpr size_t ARGC_TWO = 2; +static constexpr size_t ARGC_THREE = 3; +static constexpr size_t ARGC_FOUR = 4; + #define CHKRV(state, desc) \ do { \ if ((state) != napi_ok) { \ @@ -45,6 +51,14 @@ namespace MMI { } \ } while (0) +#define CHKRR(state, desc, r) \ + do { \ + if ((state) != napi_ok) { \ + MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ + return r; \ + } \ + } while (0) + #define CHKRV_SCOPE(env, state, desc, scope) \ do { \ if ((state) != napi_ok) { \ -- Gitee From 2b14edb22d8f6d72569c8d078eec81a795108ec6 Mon Sep 17 00:00:00 2001 From: mayunteng Date: Thu, 29 Dec 2022 02:01:29 +0000 Subject: [PATCH 2/5] napi Refactoring - Key subscribe Signed-off-by: mayunteng Change-Id: I07566c86c93f520af85ead4310554a8e21a5b4f8 --- .../napi/input_consumer/src/js_input_consumer_context.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp index 074dd4dd86..6ce53d9b43 100644 --- a/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp +++ b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp @@ -162,7 +162,7 @@ napi_value JsInputConsumerContext::JsOn(napi_env env, napi_callback_info info) int32_t preSubscribeId = JsInputConsumerUtil::GetPreSubscribeId(callbacks, event); if (preSubscribeId < 0) { int32_t subscribeId = INVALID_SUBID; - subscribeId = InputMgr->SubscribeKeyEvent(event->keyOption, SubKeyEventCallback); + subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(event->keyOption, SubKeyEventCallback); if (subscribeId < 0) { MMI_HILOGE("SubscribeId invalid:%{public}d", subscribeId); napi_delete_reference(env, event->callback); @@ -201,7 +201,7 @@ napi_value JsInputConsumerContext::JsOff(napi_env env, napi_callback_info info) return nullptr; } if (subscribeId > INVALID_SUBID) { - InputMgr->UnsubscribeKeyEvent(subscribeId); + InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId); MMI_HILOGD("UnsubscribeId:%{public}d", subscribeId); } if (event->callback != nullptr) { -- Gitee From 51b6516ef7ba94bca3277479498ecceff774a82d Mon Sep 17 00:00:00 2001 From: mayunteng Date: Tue, 3 Jan 2023 02:17:13 +0000 Subject: [PATCH 3/5] napi Refactoring - Key subscribe Signed-off-by: mayunteng Change-Id: Ic989c2e5e57287b7fddc5bf3c85f23b52148c044 --- .../napi/input_consumer/include/js_input_consumer_context.h | 1 + frameworks/napi/input_consumer/include/js_input_consumer_util.h | 1 + 2 files changed, 2 insertions(+) diff --git a/frameworks/napi/input_consumer/include/js_input_consumer_context.h b/frameworks/napi/input_consumer/include/js_input_consumer_context.h index fe748283f5..d105951187 100644 --- a/frameworks/napi/input_consumer/include/js_input_consumer_context.h +++ b/frameworks/napi/input_consumer/include/js_input_consumer_context.h @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef JS_INPUT_CONSUMER_CONTEXT_H #define JS_INPUT_CONSUMER_CONTEXT_H diff --git a/frameworks/napi/input_consumer/include/js_input_consumer_util.h b/frameworks/napi/input_consumer/include/js_input_consumer_util.h index 585166dcd8..4106bfcdb3 100644 --- a/frameworks/napi/input_consumer/include/js_input_consumer_util.h +++ b/frameworks/napi/input_consumer/include/js_input_consumer_util.h @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef JS_INPUT_CONSUMER_UTIL_H #define JS_INPUT_CONSUMER_UTIL_H -- Gitee From 1d16f1ccc29c35826dac96bd82793c9da226dfbf Mon Sep 17 00:00:00 2001 From: mayunteng Date: Tue, 3 Jan 2023 06:57:41 +0000 Subject: [PATCH 4/5] napi Refactoring - Key subscribe Signed-off-by: mayunteng Change-Id: I06bc4f0bef8a04259533e56daf94b3be9d22dccb --- frameworks/napi/input_consumer/include/js_input_consumer_util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/napi/input_consumer/include/js_input_consumer_util.h b/frameworks/napi/input_consumer/include/js_input_consumer_util.h index 4106bfcdb3..d6e13726b7 100644 --- a/frameworks/napi/input_consumer/include/js_input_consumer_util.h +++ b/frameworks/napi/input_consumer/include/js_input_consumer_util.h @@ -42,7 +42,7 @@ public: static int32_t UnsubscribeCallback(napi_env env, const std::shared_ptr &event, Callbacks &callbacks, int32_t &subscribeId); static int32_t DelEventCallback(napi_env env, std::list> &infos, - napi_value &handler1, int32_t &subscribeId); + napi_value &handler1, int32_t &subscribeId); static void EmitAsyncCallbackWork(const std::shared_ptr &event); private: -- Gitee From 9f7b39acc6fce7362b66f1cb49b7323cb7799e7b Mon Sep 17 00:00:00 2001 From: mayunteng Date: Wed, 4 Jan 2023 03:25:18 +0000 Subject: [PATCH 5/5] napi Refactoring - Key subscribe Signed-off-by: mayunteng Change-Id: If9ea8074f60d5480f5d60b894abed6c0ce48b1fc --- .../napi/input_consumer/src/js_input_consumer_context.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp index 6ce53d9b43..bd5f304597 100644 --- a/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp +++ b/frameworks/napi/input_consumer/src/js_input_consumer_context.cpp @@ -61,7 +61,7 @@ std::shared_ptr JsInputConsumerContext::CreateKeyOption(napi_env env, int32_t finalKey = 0; if (!JsInputConsumerUtil::GetNamedPropertyInt32(env, object, "finalKey", finalKey)) { - MMI_HILOGE("Get NamedProperty failed"); + MMI_HILOGE("GetNamedProperty failed"); return nullptr; } keyOption->SetFinalKey(finalKey); @@ -75,7 +75,7 @@ std::shared_ptr JsInputConsumerContext::CreateKeyOption(napi_env env, int32_t finalKeyDownDuration = 0; if (!JsInputConsumerUtil::GetNamedPropertyInt32(env, object, "finalKeyDownDuration", finalKeyDownDuration)) { - MMI_HILOGE("Get NamedProperty failed"); + MMI_HILOGE("GetNamedProperty failed"); return nullptr; } keyOption->SetFinalKeyDownDuration(finalKeyDownDuration); @@ -220,6 +220,5 @@ napi_value JsInputConsumerContext::Init(napi_env env, napi_value exports) NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; } - } // namespace MMI } // namespace OHOS -- Gitee