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 <uv.h> 21 #include <mutex> 22 #include <memory> 23 #include "napi/native_api.h" 24 #include "global.h" 25 #include "async_call.h" 26 #include "keyboard_listener.h" 27 #include "js_callback_object.h" 28 29 namespace OHOS { 30 namespace MiscServices { 31 template<typename T> class BlockData { 32 public: INTERVAL(interval)33 explicit BlockData(uint32_t interval, const T &invalid = T()) : INTERVAL(interval), data_(invalid) 34 { 35 } ~BlockData()36 ~BlockData() 37 { 38 } 39 40 public: SetValue(T & data)41 void SetValue(T &data) 42 { 43 std::lock_guard<std::mutex> lock(mutex_); 44 data_ = data; 45 isSet_ = true; 46 cv_.notify_one(); 47 } 48 GetValue()49 T GetValue() 50 { 51 std::unique_lock<std::mutex> lock(mutex_); 52 cv_.wait_for(lock, std::chrono::seconds(INTERVAL), [this]() { return isSet_; }); 53 T data = data_; 54 cv_.notify_one(); 55 return data; 56 } 57 58 void Clear(const T &invalid = T()) 59 { 60 std::lock_guard<std::mutex> lock(mutex_); 61 isSet_ = false; 62 data_ = invalid; 63 cv_.notify_one(); 64 } 65 66 private: 67 bool isSet_ = false; 68 const uint32_t INTERVAL; 69 T data_; 70 std::mutex mutex_; 71 std::condition_variable cv_; 72 }; 73 class JsKeyboardDelegateSetting : public KeyboardListener { 74 public: 75 JsKeyboardDelegateSetting() = default; 76 ~JsKeyboardDelegateSetting() override = default; 77 static napi_value Init(napi_env env, napi_value info); 78 static napi_value CreateKeyboardDelegate(napi_env env, napi_callback_info info); 79 static napi_value GetKeyboardDelegate(napi_env env, napi_callback_info info); 80 static napi_value Subscribe(napi_env env, napi_callback_info info); 81 static napi_value UnSubscribe(napi_env env, napi_callback_info info); 82 bool OnKeyEvent(int32_t keyCode, int32_t keyStatus) override; 83 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override; 84 void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override; 85 void OnTextChange(const std::string &text) override; 86 87 private: 88 static napi_value GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus); 89 static napi_value GetJsConstProperty(napi_env env, uint32_t num); 90 static napi_value GetKDInstance(napi_env env, napi_callback_info info); 91 static std::shared_ptr<JsKeyboardDelegateSetting> GetKeyboardDelegateSetting(); 92 static bool InitKeyboardDelegate(); 93 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 94 static JsKeyboardDelegateSetting *GetNative(napi_env env, napi_callback_info info); 95 static bool Equals(napi_env env, napi_value value, napi_ref copy, std::thread::id threadId); 96 void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj); 97 void UnRegisterListener(napi_value callback, std::string type); 98 99 static std::string GetStringProperty(napi_env env, napi_value obj); 100 static constexpr int32_t MAX_VALUE_LEN = 1024; 101 static constexpr int32_t MAX_TIMEOUT = 5; 102 static const std::string KDS_CLASS_NAME; 103 static thread_local napi_ref KDSRef_; 104 struct CursorPara { 105 int32_t positionX = 0; 106 int32_t positionY = 0; 107 int height = 0; 108 }; 109 struct SelectionPara { 110 int32_t oldBegin = 0; 111 int32_t oldEnd = 0; 112 int32_t newBegin = 0; 113 int32_t newEnd = 0; 114 }; 115 struct KeyEventPara { 116 int32_t keyCode = 0; 117 int32_t keyStatus = 0; 118 bool isOnKeyEvent = false; 119 }; 120 struct UvEntry { 121 std::vector<std::shared_ptr<JSCallbackObject>> vecCopy; 122 std::string type; 123 CursorPara curPara; 124 SelectionPara selPara; 125 KeyEventPara keyEventPara; 126 std::shared_ptr<BlockData<bool>> isDone; 127 std::string text; UvEntryUvEntry128 UvEntry(std::vector<std::shared_ptr<JSCallbackObject>> cbVec, std::string type) : vecCopy(cbVec), type(type) 129 { 130 } 131 }; 132 using EntrySetter = std::function<void(UvEntry &)>; 133 uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); 134 uv_loop_s *loop_ = nullptr; 135 std::recursive_mutex mutex_; 136 std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_; 137 static std::mutex keyboardMutex_; 138 static std::shared_ptr<JsKeyboardDelegateSetting> keyboardDelegate_; 139 }; 140 } // namespace MiscServices 141 } // namespace OHOS 142 #endif // INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H