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 <uv.h>
20 
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 
25 #include "async_call.h"
26 #include "global.h"
27 #include "input_attribute.h"
28 #include "js_callback_object.h"
29 #include "keyboard_listener.h"
30 #include "keyevent_consumer_proxy.h"
31 #include "napi/native_api.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 class JsKeyboardDelegateSetting : public KeyboardListener {
36 public:
37     JsKeyboardDelegateSetting() = default;
38     ~JsKeyboardDelegateSetting() override = default;
39     static napi_value Init(napi_env env, napi_value exports);
40     static napi_value CreateKeyboardDelegate(napi_env env, napi_callback_info info);
41     static napi_value GetKeyboardDelegate(napi_env env, napi_callback_info info);
42     static napi_value Subscribe(napi_env env, napi_callback_info info);
43     static napi_value UnSubscribe(napi_env env, napi_callback_info info);
44     bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer) override;
45     bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) override;
46     void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override;
47     void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override;
48     void OnTextChange(const std::string &text) override;
49     void OnEditorAttributeChange(const InputAttribute &inputAttribute) override;
50     bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) override;
51 
52 private:
53     static napi_value GetResultOnKeyEvent(napi_env env, int32_t keyCode, int32_t keyStatus);
54     static napi_value GetJsConstProperty(napi_env env, uint32_t num);
55     static napi_value GetKDInstance(napi_env env, napi_callback_info info);
56     static std::shared_ptr<JsKeyboardDelegateSetting> GetKeyboardDelegateSetting();
57     static bool InitKeyboardDelegate();
58     static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo);
59     void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj);
60     void UnRegisterListener(napi_value callback, std::string type);
61     static constexpr int32_t MAX_TIMEOUT = 2000;
62     static const std::string KDS_CLASS_NAME;
63     static thread_local napi_ref KDSRef_;
64     struct CursorPara {
65         int32_t positionX = 0;
66         int32_t positionY = 0;
67         int height = 0;
68     };
69     struct SelectionPara {
70         int32_t oldBegin = 0;
71         int32_t oldEnd = 0;
72         int32_t newBegin = 0;
73         int32_t newEnd = 0;
74     };
75     struct KeyEventPara {
76         int32_t keyCode = 0;
77         int32_t keyStatus = 0;
78         bool isOnKeyEvent = false;
79     };
80     struct UvEntry {
81         std::vector<std::shared_ptr<JSCallbackObject>> vecCopy;
82         std::string type;
83         CursorPara curPara;
84         SelectionPara selPara;
85         KeyEventPara keyEventPara;
86         std::shared_ptr<MMI::KeyEvent> pullKeyEventPara;
87         std::string text;
88         sptr<KeyEventConsumerProxy> keyEvenetConsumer = nullptr;
89         InputAttribute inputAttribute;
UvEntryUvEntry90         UvEntry(const std::vector<std::shared_ptr<JSCallbackObject>> &cbVec, const std::string &type)
91             : vecCopy(cbVec), type(type)
92         {
93         }
94     };
95     using EntrySetter = std::function<void(UvEntry &)>;
96     static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
97     std::shared_ptr<UvEntry> GetEntry(const std::string &type, EntrySetter entrySetter = nullptr);
98     uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr);
99     static void DealKeyEvent(const std::shared_ptr<UvEntry> &keyEventEntry,
100         const std::shared_ptr<UvEntry> &keyCodeEntry, const sptr<KeyEventConsumerProxy> &consumer);
101     uv_loop_s *loop_ = nullptr;
102     std::recursive_mutex mutex_;
103     std::map<std::string, std::vector<std::shared_ptr<JSCallbackObject>>> jsCbMap_;
104     static std::mutex keyboardMutex_;
105     static std::shared_ptr<JsKeyboardDelegateSetting> keyboardDelegate_;
106     static std::mutex eventHandlerMutex_;
107     static std::shared_ptr<AppExecFwk::EventHandler> handler_;
108 };
109 } // namespace MiscServices
110 } // namespace OHOS
111 #endif // INTERFACE_KITS_JS_KEYBOARD_DELEGATE_SETTING_H
112