1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 16 #define INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 17 18 #include <uv.h> 19 20 #include "async_call.h" 21 #include "event_handler.h" 22 #include "global.h" 23 #include "input_method_controller.h" 24 #include "input_method_status.h" 25 #include "js_callback_object.h" 26 27 namespace OHOS { 28 namespace MiscServices { 29 struct ListInputContext : public AsyncCall::Context { 30 InputMethodStatus inputMethodStatus = InputMethodStatus::ALL; 31 std::vector<Property> properties; 32 std::vector<SubProperty> subProperties; 33 Property property; 34 napi_status status = napi_generic_failure; ListInputContextListInputContext35 ListInputContext() : Context(nullptr, nullptr){}; ListInputContextListInputContext36 ListInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 37 operatorListInputContext38 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 39 { 40 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 41 return Context::operator()(env, argc, argv, self); 42 } operatorListInputContext43 napi_status operator()(napi_env env, napi_value *result) override 44 { 45 if (status != napi_ok) { 46 output_ = nullptr; 47 return status; 48 } 49 return Context::operator()(env, result); 50 } 51 }; 52 53 struct DisplayOptionalInputMethodContext : public AsyncCall::Context { 54 napi_status status = napi_generic_failure; 55 bool isDisplayed = false; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext56 DisplayOptionalInputMethodContext() : Context(nullptr, nullptr){}; DisplayOptionalInputMethodContextDisplayOptionalInputMethodContext57 DisplayOptionalInputMethodContext(InputAction input, OutputAction output) 58 : Context(std::move(input), std::move(output)){}; 59 operatorDisplayOptionalInputMethodContext60 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 61 { 62 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 63 return Context::operator()(env, argc, argv, self); 64 } operatorDisplayOptionalInputMethodContext65 napi_status operator()(napi_env env, napi_value *result) override 66 { 67 if (status != napi_ok) { 68 output_ = nullptr; 69 return status; 70 } 71 return Context::operator()(env, result); 72 } 73 }; 74 75 struct GetInputMethodControllerContext : public AsyncCall::Context { 76 bool isStopInput = false; 77 napi_status status = napi_generic_failure; GetInputMethodControllerContextGetInputMethodControllerContext78 GetInputMethodControllerContext() : Context(nullptr, nullptr){}; GetInputMethodControllerContextGetInputMethodControllerContext79 GetInputMethodControllerContext(InputAction input, OutputAction output) 80 : Context(std::move(input), std::move(output)){}; 81 operatorGetInputMethodControllerContext82 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 83 { 84 CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); 85 return Context::operator()(env, argc, argv, self); 86 } operatorGetInputMethodControllerContext87 napi_status operator()(napi_env env, napi_value *result) override 88 { 89 if (status != napi_ok) { 90 output_ = nullptr; 91 return status; 92 } 93 return Context::operator()(env, result); 94 } 95 }; 96 97 struct GetInputMethodStateContext : public AsyncCall::Context { 98 napi_status status = napi_generic_failure; 99 EnabledStatus enableStatus = EnabledStatus::DISABLED; GetInputMethodStateContextGetInputMethodStateContext100 GetInputMethodStateContext() : Context(nullptr, nullptr){}; GetInputMethodStateContextGetInputMethodStateContext101 GetInputMethodStateContext(InputAction input, OutputAction output) 102 : Context(std::move(input), std::move(output)){}; operatorGetInputMethodStateContext103 napi_status operator()(napi_env env, napi_value *result) override 104 { 105 if (status != napi_ok) { 106 output_ = nullptr; 107 return status; 108 } 109 return Context::operator()(env, result); 110 } 111 }; 112 113 class JsGetInputMethodSetting : public ImeEventListener { 114 public: 115 JsGetInputMethodSetting() = default; 116 ~JsGetInputMethodSetting() = default; 117 static napi_value Init(napi_env env, napi_value exports); 118 static napi_value GetSetting(napi_env env, napi_callback_info info); 119 static napi_value GetInputMethodSetting(napi_env env, napi_callback_info info); 120 static napi_value ListInputMethod(napi_env env, napi_callback_info info); 121 static napi_value ListInputMethodSubtype(napi_env env, napi_callback_info info); 122 static napi_value ListCurrentInputMethodSubtype(napi_env env, napi_callback_info info); 123 static napi_value GetInputMethods(napi_env env, napi_callback_info info); 124 static napi_value GetInputMethodsSync(napi_env env, napi_callback_info info); 125 static napi_value GetAllInputMethods(napi_env env, napi_callback_info info); 126 static napi_value GetAllInputMethodsSync(napi_env env, napi_callback_info info); 127 static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info); 128 static napi_value ShowOptionalInputMethods(napi_env env, napi_callback_info info); 129 static napi_value IsPanelShown(napi_env env, napi_callback_info info); 130 static napi_value GetInputMethodState(napi_env env, napi_callback_info info); 131 static napi_value Subscribe(napi_env env, napi_callback_info info); 132 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 133 static std::shared_ptr<JsGetInputMethodSetting> GetInputMethodSettingInstance(); 134 void OnImeChange(const Property &property, const SubProperty &subProperty) override; 135 void OnImeShow(const ImeWindowInfo &info) override; 136 void OnImeHide(const ImeWindowInfo &info) override; 137 138 private: 139 static napi_status GetInputMethodProperty(napi_env env, napi_value argv, std::shared_ptr<ListInputContext> ctxt); 140 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 141 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 142 static napi_value GetIMSetting(napi_env env, napi_callback_info info, bool needThrowException); 143 static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler(); 144 int32_t RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 145 void UnRegisterListener(napi_value callback, std::string type, bool &isUpdateFlag); 146 void OnPanelStatusChange(const std::string &type, const InputWindowInfo &info); 147 struct UvEntry { 148 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 149 std::string type; 150 Property property; 151 SubProperty subProperty; 152 std::vector<InputWindowInfo> windowInfo; UvEntryUvEntry153 UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type) 154 : vecCopy(cbVec), type(type) 155 { 156 } 157 }; 158 using EntrySetter = std::function<void(UvEntry &)>; 159 std::shared_ptr<UvEntry> GetEntry(const std::string &type, EntrySetter entrySetter = nullptr); 160 static const std::string IMS_CLASS_NAME; 161 static thread_local napi_ref IMSRef_; 162 static std::mutex eventHandlerMutex_; 163 static std::shared_ptr<AppExecFwk::EventHandler> handler_; 164 uv_loop_s *loop_ = nullptr; 165 std::recursive_mutex mutex_; 166 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 167 static std::mutex msMutex_; 168 static std::shared_ptr<JsGetInputMethodSetting> inputMethod_; 169 170 PanelFlag softKbShowingFlag_{ FLG_CANDIDATE_COLUMN }; 171 PanelFlag GetSoftKbShowingFlag(); 172 void SetSoftKbShowingFlag(PanelFlag flag); 173 }; 174 } // namespace MiscServices 175 } // namespace OHOS 176 #endif // INTERFACE_KITS_JS_GETINPUT_METHOD_SETTING_H 177