1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H 17 #define INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <uv.h> 23 24 #include "async_call.h" 25 #include "global.h" 26 #include "input_attribute.h" 27 #include "js_callback_object.h" 28 #include "keyboard_listener.h" 29 #include "key_event_consumer_proxy.h" 30 #include "napi/native_api.h" 31 32 namespace OHOS { 33 namespace MiscServices { 34 class JsKeyboardDelegateSetting : public KeyboardListener { 35 public: 36 JsKeyboardDelegateSetting() = default; 37 ~JsKeyboardDelegateSetting() override = default; 38 static napi_value Init(napi_env env, napi_value exports); 39 static napi_value CreateKeyboardDelegate(napi_env env, napi_callback_info info); 40 static napi_value GetKeyboardDelegate(napi_env env, napi_callback_info info); 41 static napi_value Subscribe(napi_env env, napi_callback_info info); 42 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 43 bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer) override; 44 bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) override; 45 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override; 46 void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override; 47 void OnTextChange(const std::string &text) override; 48 void OnEditorAttributeChange(const InputAttribute &inputAttribute) override; 49 bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId, 50 const sptr<IRemoteObject> &channelObject) override; 51 void OnKeyEventConsumeResult(bool isConsumed, sptr<KeyEventConsumerProxy> consumer); 52 void OnKeyCodeConsumeResult(bool isConsumed, sptr<KeyEventConsumerProxy> consumer); 53 54 private: 55 static napi_value GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus); 56 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 57 static napi_value GetKDInstance(napi_env env, napi_callback_info info); 58 static std::shared_ptr<JsKeyboardDelegateSetting> GetKeyboardDelegateSetting(); 59 static bool InitKeyboardDelegate(); 60 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 61 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 62 void UnRegisterListener(napi_value callback, std::string type); 63 static constexpr int32_t MAX_TIMEOUT = 2000; 64 static const std::string KDS_CLASS_NAME; 65 static thread_local napi_ref KDSRef_; 66 struct CursorPara { 67 int32_t positionX = 0; 68 int32_t positionY = 0; 69 int height = 0; 70 }; 71 struct SelectionPara { 72 int32_t oldBegin = 0; 73 int32_t oldEnd = 0; 74 int32_t newBegin = 0; 75 int32_t newEnd = 0; 76 }; 77 struct KeyEventPara { 78 int32_t keyCode = 0; 79 int32_t keyStatus = 0; 80 bool isOnKeyEvent = false; 81 }; 82 struct UvEntry { 83 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 84 std::string type; 85 CursorPara curPara; 86 SelectionPara selPara; 87 KeyEventPara keyEventPara; 88 std::shared_ptr<MMI::KeyEvent> fullKeyEventPara; 89 std::string text; 90 sptr<KeyEventConsumerProxy> keyEvenetConsumer = nullptr; 91 InputAttribute inputAttribute; UvEntryUvEntry92 UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type) 93 : vecCopy(cbVec), type(type) 94 { 95 } 96 }; 97 using EntrySetter = std::function<void(UvEntry &)>; 98 static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler(); 99 std::shared_ptr<UvEntry> GetEntry(const std::string &type, EntrySetter entrySetter = nullptr); 100 static void DealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, 101 const std::shared_ptr<UvEntry> &keyEventEntry, const std::shared_ptr<UvEntry> &keyCodeEntry, uint64_t cbId, 102 const sptr<IRemoteObject> &channelObject); 103 std::recursive_mutex mutex_; 104 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 105 static std::mutex keyboardMutex_; 106 static std::shared_ptr<JsKeyboardDelegateSetting> keyboardDelegate_; 107 static std::mutex eventHandlerMutex_; 108 static std::shared_ptr<AppExecFwk::EventHandler> handler_; 109 110 bool keyEventConsume_ = false; 111 bool keyCodeConsume_ = false; 112 bool keyEventResult_ = false; 113 bool keyCodeResult_ = false; 114 }; 115 } // namespace MiscServices 116 } // namespace OHOS 117 #endif // INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H