From c35908d485d8535120bbb1846650bda7d29e13fc Mon Sep 17 00:00:00 2001 From: guojin31 Date: Sat, 2 Aug 2025 20:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E6=9D=BF=C3=A6ark1.2=C3=A6=C2=94?= =?UTF-8?q?=E9=9D=99=E6=80=81=C2=B9=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guojin31 --- bundle.json | 3 +- .../ets/taihe/keyboardPanelManager/BUILD.gn | 105 ++++++++++ .../idl/ohos.keyboardPanelManager.taihe | 70 +++++++ .../keyboardPanelManager/include/ani_common.h | 191 ++++++++++++++++++ .../include/keyboard_panel_manager_listener.h | 56 +++++ .../src/ani_constructor.cpp | 14 ++ .../src/keyboard_panel_manager_listener.cpp | 129 ++++++++++++ .../src/ohos.keyboardPanelManager.impl.cpp | 108 ++++++++++ 8 files changed, 675 insertions(+), 1 deletion(-) create mode 100644 frameworks/ets/taihe/keyboardPanelManager/BUILD.gn create mode 100644 frameworks/ets/taihe/keyboardPanelManager/idl/ohos.keyboardPanelManager.taihe create mode 100644 frameworks/ets/taihe/keyboardPanelManager/include/ani_common.h create mode 100644 frameworks/ets/taihe/keyboardPanelManager/include/keyboard_panel_manager_listener.h create mode 100644 frameworks/ets/taihe/keyboardPanelManager/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/keyboardPanelManager/src/keyboard_panel_manager_listener.cpp create mode 100644 frameworks/ets/taihe/keyboardPanelManager/src/ohos.keyboardPanelManager.impl.cpp diff --git a/bundle.json b/bundle.json index 0c346b2e3..288c0760b 100644 --- a/bundle.json +++ b/bundle.json @@ -73,7 +73,8 @@ "//base/inputmethod/imf/frameworks/js/napi/inputmethodlist:inputmethodlist", "//base/inputmethod/imf/frameworks/js/napi/inputmethodpanel:panel", "//base/inputmethod/imf/frameworks/ndk:ohinputmethod", - "//base/inputmethod/imf/frameworks/ets/taihe/inputMethod:inputmethod_taihe" + "//base/inputmethod/imf/frameworks/ets/taihe/inputMethod:inputmethod_taihe", + "//base/inputmethod/imf/frameworks/ets/taihe/keyboardPanelManager:keyboardPanelManager.taihe" ], "service_group": [ "//base/inputmethod/imf/etc/init:inputmethodservice.cfg", diff --git a/frameworks/ets/taihe/keyboardPanelManager/BUILD.gn b/frameworks/ets/taihe/keyboardPanelManager/BUILD.gn new file mode 100644 index 000000000..d1027a69d --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/BUILD.gn @@ -0,0 +1,105 @@ +# Copyright (c) 2025 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//base/inputmethod/imf/inputmethod.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") + +subsystem_name = "inputmethod" +part_name = "imf" +taihe_generated_file_path = "taihefilepath/out/subsystem_name/$part_name/keyboard" + +copy_taihe_idl("copy_taihe") { + sources = [ + "idl/ohos.keyboardPanelManager.taihe", + ] + +external_deps = [] + +deps = ["../inputMethod:copy_taihe"] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "taihegeneratedfilepath" + deps = [":copy_taihe"] + outputs = [" + "$taihe_generated_file_path/src/ohos.keyboardPanelManager.ani.cpp", + "$taihe_generated_file_path/src/ohos.keyboardPanelManager.abi.c", + ] +} + +taihe_shared_library("keyboardPanelManager_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + taihe_generated_file_path = "taihegeneratedfilepath"subsystemname=" + subsystem_name" + part_name = "$partname" + includedirs = [ + "include"," + "${inputmethod_path}/frameworks/js/napi/inputmethodclient", + ] + sources = get_target_outputs(":run_taihe") + sources += [ + "src/ani_constructor.cpp", + "src/keyboard_panel_manager_listener.cpp", + "src/ohos.keyboardPanelManager.impl.cpp", + "inputmethodpath/frameworks/js/napi/inputmethodclient/jsutils.cpp", + ] + deps = [ + ":runtaihe", + "${inputmethod_path}/frameworks/js/napi/common:inputmethod_js_common", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client", + ] + external_deps = [ + "ability_base:want", + "ability_runtime:abilitykit_native", + "ability_runtime:extensionkit_native", + "bundle_framework:bms_ani_common", + "c_utils:utils", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog" + ] +} + +generate_static_abc("keyboardPanelManager_abc") { + base_url = "taihegeneratedfilepath" + files = [ + "taihe_generated_file_path/@ohos.keyboardPanelManager.ets" + ] + is_boot_abc = "True" + device_dst_file = "/system/framework/keyboardPanelManager_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("keyboardPanelManager_etc") { + source = "targetoutdir/keyboardPanelManagerabc.abc"moduleinstalldir="framework"partname=" + part_name" + subsystem_name = "$subsystem_name" + deps = [ ":keyboardPanelManager_abc"] +} + +group("keyboardPanelManager_taihe") { + deps = [ + ":keyboardPanelManager_taihe_native", + ":keyboardPanelManager_etc" + ] +} diff --git a/frameworks/ets/taihe/keyboardPanelManager/idl/ohos.keyboardPanelManager.taihe b/frameworks/ets/taihe/keyboardPanelManager/idl/ohos.keyboardPanelManager.taihe new file mode 100644 index 000000000..20cd52d25 --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/idl/ohos.keyboardPanelManager.taihe @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 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. + */ + + +@!namespace("@ohos.keyboardPanelManager", "keyboardPanelManager") +@!sts_inject(""" +static { loadLibrary("keyboardPanelManager_taihe_native.z") } +""") +from ohos.inputMethod use InputMethodProperty; +from ohos.inputMethod.Panel use PanelFlag; + +union PrivateDataValue { + string: String; + boolean: bool; + number: i32; +} + +struct SysPanelStatus { + inputType: i32; + flag: PanelFlag; + width: f64; + height: f64; + isPanelRaised: bool; + needFuncButton: bool; +} + +@!sts_inject(""" + function on(type: string, callback: (arg0 : Object) => void) { + switch(type) { + case "isPanelShow": return onIsPanelShow(callback as (status: SysPanelStatus) => void, callback); + case "panelPrivateCommand": return onPanelPrivateDataCommand(callback as (info: Record) => void, callback); + default: throw new Error(Unknown type: ${type}); + } + } + function off(type: string, callback?: (arg0 : Object) => void) { + switch(type) { + case "isPanelShow": return offIsPanelShow(callback); + case "panelPrivateCommand": return offPanelPrivateDataCommand(callback); + default: throw new Error(Unknown type: ${type}); + } + } +""") + +function onIsPanelShow(f: (status: SysPanelStatus) => void, opq: Opaque); +function offIsPanelShow(opq: Optional); +function onPanelPrivateDataCommand(f: (info: @record Map) => void, opq: Opaque); +function offPanelPrivateDataCommand(opq: Optional); + +@gen_async("sendPrivateCommand") +@gen_promise("sendPrivateCommand") +function SendPrivateCommandSync(privateCommand: @record Map): void; +@gen_async("getSmartMenuCfg") +@gen_promise("getSmartMenuCfg") +function GetSmartMenuCfgSync(): String; +function GetDefaultInputMethod(): InputMethodProperty; +@gen_async("connectSystemCmd") +@gen_promise("connectSystemCmd") +function ConnectSystemCmdSync(): void; diff --git a/frameworks/ets/taihe/keyboardPanelManager/include/ani_common.h b/frameworks/ets/taihe/keyboardPanelManager/include/ani_common.h new file mode 100644 index 000000000..6c23b98d2 --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/include/ani_common.h @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2025 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 KEYBOARD_PANEL_MANAGER_TAIHE_ANI_COMMON_H +#define KEYBOARD_PANEL_MANAGER_TAIHE_ANI_COMMON_H + +#include +#include + +#include "ani_common.h" +#include "ime_system_channel.h" +#include "input_method_utils.h" +#include "js_utils.h" +#include "ohos.keyboardPanelManager.proj.hpp" +#include "ohos.keyboardPanelManager.impl.hpp" +#include "taihe/runtime.hpp" + + +using InputMethodProperty_t = ::ohos::inputMethod::InputMethodProperty; +using SysPanelStatus_t = ::ohos::keyboardPanelManager::SysPanelStatus; +using PanelFlag_t = ::ohos::inputMethod::Panel::PanelFlag; +using PrivateDataValue_t = ::ohos::keyboardPanelManager::PrivateDataValue; +namespace OHOS { +namespace MiscServices { +class PropertyConverter { +public: + static InputMethodProperty_t ConvertProperty(const std::shared_ptr &obj) + { + return ConvertPropertyImpl(*obj); + } + + static InputMethodProperty_t ConvertProperty(const Property &obj) + { + return ConvertPropertyImpl(obj); + } + + static SysPanelStatus_t ConvertSysPanelStatus(const std::shared_ptr &obj) + { + return ConvertSysPanelStatusImpl(*obj); + } + + static SysPanelStatus_t ConvertSysPanelStatus(const SysPanelStatus &obj) + { + return ConvertSysPanelStatusImpl(obj); + } + + static ::taihe::map<::taihe::string, PrivateDataValue_t> ConvertPrivateCommand(const std::unordered_map &privateCommand) + { + ::taihe::map<::taihe::string, PrivateDataValue_t> privateDataCommand_t; + for (auto &item : privateCommand) { + if (item.first.empty()) { + continue; + } + if (std::holds_alternative(item.second)) { + privateDataCommand_t.emplace(::taihe::string(item.first), + PrivateDataValue_t::make_number(std::get(item.second))); + continue; + } + if (std::holds_alternative(item.second)) { + privateDataCommand_t.emplace(::taihe::string(item.first), + PrivateDataValue_t::make_string(std::get(item.second))); + continue; + } + if (std::holds_alternative(item.second)) { + privateDataCommand_t.emplace(::taihe::string(item.first), + PrivateDataValue_t::make_boolean(std::get(item.second))); + continue; + } + } + return privateDataCommand_t; + } + + static std::unordered_map ConvertPrivateCommand(const ::taihe::map_view<::taihe::string, + PrivateDataValue_t> &privateCommand_t) + { + std::unordered_map privateCommand; + for (auto &item : privateCommand_t) { + if (item.first.empty()) { + continue; + } + OHOS::MiscServices::PrivateDataValue value; + switch (item.second.get_tag()) { + case ::ohos::keyboardPanelManager::PrivateDataValue::tag_t::number: + value = item.second.get_number_ref(); + break; + case ::ohos::keyboardPanelManager::PrivateDataValue::tag_t::string: + value = std::string(item.second.get_string_ref()); + break; + case ::ohos::keyboardPanelManager::PrivateDataValue::tag_t::boolean: + value = item.second.get_boolean_ref(); + break; + } + privateCommand.insert(std::make_pair(std::string(item.first), value)); + } + return privateCommand; + } + +private: + template + static InputMethodProperty_t ConvertPropertyImpl(T &&obj) + { + static_assert(std::is_same_v, "Invalid type for Property conversion"); + InputMethodProperty_t result{}; + result.name = std::forward(obj).name; + result.id = obj.id; + result.label = taihe::optionaltaihe::string(std::in_place_t{}, obj.label); + result.labelId = taihe::optional(std::in_place_t{}, obj.labelId); + result.icon = taihe::optionaltaihe::string(std::in_place_t{}, obj.icon); + result.iconId = taihe::optional(std::in_place_t{}, obj.iconId); + return result; + } + + template + static SysPanelStatus_t ConvertSysPanelStatusImpl(T &&obj) + { + static_assert(std::is_same_v, SysPanelStatus>, "Invalid type for SysPanelStatus conversion"); + int value = static_cast(obj.inputType); + SysPanelStatus_t result{value, PanelFlag_t::from_value(obj.flag)}; + result.width = obj.width; + result.height = obj.height; + result.isPanelRaised = obj.isPanelRaised; + result.needFuncButton = obj.needFuncButton; + return result; + } +}; + +using callbackType = std::variant, + taihe::callback)>>; + +struct CallbackObject { + CallbackObject(callbackType cb, ani_ref ref) : callback(cb), ref(ref) + { + } + void Release() + { + taihe::env_guard guard; + if (auto *env = guard.get_env()) { + env->GlobalReference_Delete(ref); + } + } + callbackType callback; + ani_ref ref; +}; + +class GlobalRefGuard { + ani_env *env_ = nullptr; + ani_ref ref_ = nullptr; + +public: +GlobalRefGuard(ani_env *env, ani_object obj) : env_(env) +{ + if (!env_) + return; + if (ANI_OK != env_->GlobalReference_Create(obj, &ref_)) { + ref_ = nullptr; + } + } + explicit operator bool() const + { + return ref_ != nullptr; + } + ani_ref get() const + { + return ref_; + } + ~GlobalRefGuard() + { + if (env_ && ref_) { + env_->GlobalReference_Delete(ref_); + } + } + + GlobalRefGuard(const GlobalRefGuard &) = delete; + GlobalRefGuard &operator=(const GlobalRefGuard &) = delete; +}; +} // namespace MiscServices +} // namespace OHOS +#endif \ No newline at end of file diff --git a/frameworks/ets/taihe/keyboardPanelManager/include/keyboard_panel_manager_listener.h b/frameworks/ets/taihe/keyboardPanelManager/include/keyboard_panel_manager_listener.h new file mode 100644 index 000000000..544ec52a7 --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/include/keyboard_panel_manager_listener.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 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 TAIHE_KEYBOARD_PANEL_MANAGER_LISTENER_H +#define TAIHE_KEYBOARD_PANEL_MANAGER_LISTENER_H + +#include "ani_common.h" +#include "block_queue.h" +#include "event_handler.h" +#include "ime_system_channel.h" +#include "ohos.keyboardPanelManager.proj.hpp" +#include "ohos.keyboardPanelManager.impl.hpp" + +namespace OHOS { +namespace MiscServices { +struct PrivateCommandInfo { + std::chrono::system_clock::time_point timestamp{}; + std::unordered_map privateCommand; + bool operator==(const PrivateCommandInfo &info) const + { + return (timestamp == info.timestamp && privateCommand == info.privateCommand); + } +}; + +class KeyboardPanelManagerListener : public OnSystemCmdListener { +public: + KeyboardPanelManagerListener () = default; + ~KeyboardPanelManagerListener () = default; + static sptr GetInstance(); + void ReceivePrivateCommand(const std::unordered_map &privateCommand) override; + void NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) override; + void RegisterListener(std::string const &type, callbackType &&cb, uintptr_t opq); + void UnRegisterListener(std::string const &type, taihe::optional_view opq); + + static BlockQueue privateCommandQueue; + private: + static std::mutex managerMutex_; + static sptr keyboardPanelManagerListener_; + std::mutex mutex_; + std::map> jsCbMap_; +}; +} // namespace MiscServices +} // namespace OHOS +#endif // FRAMEWORKS_ETS_TAIHE_KEYBOARD_PANEL_MANAGER_LISTENER_H \ No newline at end of file diff --git a/frameworks/ets/taihe/keyboardPanelManager/src/ani_constructor.cpp b/frameworks/ets/taihe/keyboardPanelManager/src/ani_constructor.cpp new file mode 100644 index 000000000..da6b432e8 --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/src/ani_constructor.cpp @@ -0,0 +1,14 @@ +#include "taihe/runtime.hpp" +#include "ohos.keyboardPanelManager.ani.hpp" +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != ohos::keyboardPanelManager::ANIRegister(env)) { + std::cerr << "Error from ohos::keyboardPanelManager::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/frameworks/ets/taihe/keyboardPanelManager/src/keyboard_panel_manager_listener.cpp b/frameworks/ets/taihe/keyboardPanelManager/src/keyboard_panel_manager_listener.cpp new file mode 100644 index 000000000..bff8b83ef --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/src/keyboard_panel_manager_listener.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2025 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 "keyboard_panel_manager_listener.h" + +namespace OHOS { +namespace MiscServices { +constexpr int32_t MAX_WAIT_TIME_PRIVATE_COMMAND = 2000; +BlockQueue KeyboardPanelManagerListener::privateCommandQueue{ MAX_WAIT_TIME_PRIVATE_COMMAND }; +std::mutex KeyboardPanelManagerListener::managerMutex_; +sptr KeyboardPanelManagerListener::keyboardPanelManagerListener_{ nullptr }; + +sptr KeyboardPanelManagerListener::GetInstance() +{ + if (keyboardPanelManagerListener_ == nullptr) { + std::lock_guardstd::mutex lock(managerMutex_); + if (keyboardPanelManagerListener_ == nullptr) { + keyboardPanelManagerListener_ = new (std::nothrow) KeyboardPanelManagerListener(); + } + } + return keyboardPanelManagerListener_; +} + +void KeyboardPanelManagerListener::RegisterListener(std::string const &type, callbackType &&cb, uintptr_t opq) +{ + std::lock_guardstd::mutex lock(mutex_); + ani_object callbackObj = reinterpret_cast(opq); + ani_ref callbackRef; + ani_env *env = taihe::get_env(); + if (env == nullptr || ANI_OK != env->GlobalReference_Create(callbackObj, &callbackRef)) { + IMSA_HILOGE("ani_env is nullptr or GlobalReference_Create failed, type: %{public}s!", type.c_str()); + return; + } + auto &cbVec = jsCbMap_[type]; + bool isDuplicate = + std::any_of(cbVec.begin(), cbVec.end(), [env, callbackRef](std::unique_ptr &obj) { + ani_boolean isEqual = false; + return (ANI_OK == env->Reference_StrictEquals(callbackRef, obj->ref, &isEqual)) && isEqual; + }); + if (isDuplicate) { + env->GlobalReference_Delete(callbackRef); + IMSA_HILOGI("callback already registered, type: %{public}s!", type.c_str()); + return; + } + cbVec.emplace_back(std::make_unique(cb, callbackRef)); + IMSA_HILOGI("register callback success, type: %{public}s!", type.c_str()); +} + +void KeyboardPanelManagerListener::UnRegisterListener(std::string const &type, taihe::optional_view opq) +{ + std::lock_guardstd::mutex lock(mutex_); + const auto iter = jsCbMap_.find(type); + if (iter == jsCbMap_.end()) { + IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str()); + return; + } + + if (!opq.has_value()) { + for (auto &uniquePtr : iter->second) { + uniquePtr->Release(); + } + jsCbMap_.erase(iter); + IMSA_HILOGE("callback is nullptr!"); + return; + } + + ani_env *env = taihe::get_env(); + if (env == nullptr) { + IMSA_HILOGE("ani_env is nullptr!"); + return; + } + + GlobalRefGuard guard(env, reinterpret_cast(opq.value())); + if (!guard) { + IMSA_HILOGE("GlobalRefGuard is false!"); + return; + } + + const auto pred = [env, targetRef = guard.get()](std::unique_ptr &obj) { + ani_boolean is_equal = false; + return (ANI_OK == env->Reference_StrictEquals(targetRef, obj->ref, &is_equal)) && is_equal; + }; + auto &callbacks = iter->second; + const auto it = std::find_if(callbacks.begin(), callbacks.end(), pred); + if (it != callbacks.end()) { + it->get()->Release(); + callbacks.erase(it); + IMSA_HILOGI("UnRegisterListener one type:%{public}s", type.c_str()); + } + if (callbacks.empty()) { + jsCbMap_.erase(iter); + IMSA_HILOGI("UnRegisterListener callbacks is empty type:%{public}s", type.c_str()); + } +} + +void KeyboardPanelManagerListener::ReceivePrivateCommand(const std::unordered_map &privateCommand) +{ + std::lock_guardstd::mutex lock(mutex_); + auto &cbVec = jsCbMap_["panelPrivateCommand"]; + for (auto &cb : cbVec) { + auto &func = std::get)>>(cb->callback); + func(PropertyConverter::ConvertPrivateCommand(privateCommand)); + } +} + +void KeyboardPanelManagerListener::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) +{ + std::lock_guardstd::mutex lock(mutex_); + auto &cbVec = jsCbMap_["isPanelShow"]; + for (auto &cb : cbVec) { + auto &func = std::get>(cb->callback); + func(PropertyConverter::ConvertSysPanelStatus(sysPanelStatus)); + } +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/taihe/keyboardPanelManager/src/ohos.keyboardPanelManager.impl.cpp b/frameworks/ets/taihe/keyboardPanelManager/src/ohos.keyboardPanelManager.impl.cpp new file mode 100644 index 000000000..71f41c794 --- /dev/null +++ b/frameworks/ets/taihe/keyboardPanelManager/src/ohos.keyboardPanelManager.impl.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 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 "ohos.keyboardPanelManager.proj.hpp" +#include "ohos.keyboardPanelManager.impl.hpp" + +#include "ani_common.h" +#include "ime_system_channel.h" +#include "js_utils.h" +#include "keyboard_panel_manager_listener.h" +#include "stdexcept" +#include "taihe/runtime.hpp" + +using namespace taihe; +using namespace ::ohos::keyboardPanelManager; +using namespace OHOS::MiscServices; +namespace { +void onIsPanelShow(::taihe::callback_view f, uintptr_t opq) +{ + KeyboardPanelManagerListener::GetInstance()->RegisterListener("isPanelShow", std::forward(f), opq); +} + +void offIsPanelShow(::taihe::optional_view opq) +{ + KeyboardPanelManagerListener::GetInstance()->UnRegisterListener("isPanelShow", opq); +} + +void onPanelPrivateDataCommand(::taihe::callback_view info)> f, uintptr_t opq) +{ + KeyboardPanelManagerListener::GetInstance()->RegisterListener("panelPrivateCommand", std::forward(f), opq); +} + +void offPanelPrivateDataCommand(::taihe::optional_view opq) +{ + KeyboardPanelManagerListener::GetInstance()->UnRegisterListener("panelPrivateCommand", opq); +} + +void SendPrivateCommandSync(::taihe::map_view<::taihe::string, PrivateDataValue_t> privateCommand) +{ + std::unordered_map privateDataCommand; + PrivateCommandInfo info; + privateDataCommand = PropertyConverter::ConvertPrivateCommand(privateCommand); + info = { std::chrono::system_clock::now(), privateDataCommand }; + KeyboardPanelManagerListener::privateCommandQueue.Push(info); + KeyboardPanelManagerListener::privateCommandQueue.Wait(info); + int32_t ret = OHOS::MiscServices::ImeSystemCmdChannel::GetInstance()->SendPrivateCommand(privateDataCommand); + KeyboardPanelManagerListener::privateCommandQueue.Pop(); + if (ret != ErrorCode::NO_ERROR) { + taihe::set_business_error(JsUtils::Convert(ret), "failed to send privateCommand!"); + IMSA_HILOGE("failed to failed to send privateCommand!"); + } +} + +::taihe::string GetSmartMenuCfgSync() { + std::string smartMenu; + smartMenu = OHOS::MiscServices::ImeSystemCmdChannel::GetInstance()->GetSmartMenuCfg(); + if (smartMenu.empty()) { + IMSA_HILOGI("smartMenu is null!"); + } + return smartMenu; +} + +ohos::inputMethod::InputMethodProperty GetDefaultInputMethod() { + InputMethodProperty_t inputMethodProperty{}; + std::shared_ptr property = std::make_shared(); + int32_t ret = OHOS::MiscServices::ImeSystemCmdChannel::GetInstance()->GetDefaultImeCfg(property); + if (ret != ErrorCode::NO_ERROR) { + taihe::set_business_error(JsUtils::Convert(ret), "failed to get default input method!"); + IMSA_HILOGE("failed to get default input method!"); + return inputMethodProperty; + } + inputMethodProperty = PropertyConverter::ConvertProperty(property); + return inputMethodProperty; +} + +void ConnectSystemCmdSync() { + auto manager = KeyboardPanelManagerListener::GetInstance(); + int32_t ret = ImeSystemCmdChannel::GetInstance()->ConnectSystemCmd(manager); + if (ret != ErrorCode::NO_ERROR) { + taihe::set_business_error(JsUtils::Convert(ret), "failed to connect system cmd!"); + IMSA_HILOGE("failed to connect system cmd!"); + } +} +} // namespace +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_onIsPanelShow(onIsPanelShow); +TH_EXPORT_CPP_API_offIsPanelShow(offIsPanelShow); +TH_EXPORT_CPP_API_onPanelPrivateDataCommand(onPanelPrivateDataCommand); +TH_EXPORT_CPP_API_offPanelPrivateDataCommand(offPanelPrivateDataCommand); +TH_EXPORT_CPP_API_SendPrivateCommandSync(SendPrivateCommandSync); +TH_EXPORT_CPP_API_GetSmartMenuCfgSync(GetSmartMenuCfgSync); +TH_EXPORT_CPP_API_GetDefaultInputMethod(GetDefaultInputMethod); +TH_EXPORT_CPP_API_ConnectSystemCmdSync(ConnectSystemCmdSync); +// NOLINTEND \ No newline at end of file -- Gitee