1 /*
2  * Copyright (c) 2024 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 INPUT_METHOD_CONTROLLER_H
16 #define INPUT_METHOD_CONTROLLER_H
17 
18 #include <set>
19 #include <map>
20 #include "controller_listener.h"
21 #include "input_method_ffi_structs.h"
22 #include "event_handler.h"
23 #include "input_method_utils.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
27 class CjInputMethodController : public ControllerListener {
28 public:
29     CjInputMethodController() = default;
30     ~CjInputMethodController() = default;
31     static std::shared_ptr<CjInputMethodController> GetInstance();
32     static int32_t Attach(const CTextConfig &txtCfg, bool showKeyboard);
33     static int32_t Detach();
34     static int32_t ShowTextInput();
35     static int32_t HideTextInput();
36     static int32_t SetCallingWindow(uint32_t windowId);
37     static int32_t UpdateCursor(const CCursorInfo &cursor);
38     static int32_t ChangeSelection(const std::string &text, int32_t start, int32_t end);
39     static int32_t UpdateAttribute(const CInputAttribute &inputAttribute);
40     static int32_t ShowSoftKeyboard();
41     static int32_t HideSoftKeyboard();
42     static int32_t StopInputSession();
43     static int32_t Subscribe(int8_t type, int64_t id);
44     static int32_t Unsubscribe(int8_t type);
45     void OnSelectByRange(int32_t start, int32_t end) override;
46     void OnSelectByMovement(int32_t direction) override;
47     void InsertText(const std::u16string &text);
48     void DeleteRight(int32_t length);
49     void DeleteLeft(int32_t length);
50     void SendKeyboardStatus(const KeyboardStatus &status);
51     void SendFunctionKey(const FunctionKey &functionKey);
52     void MoveCursor(const Direction direction);
53     void HandleExtendAction(int32_t action);
54     std::u16string GetLeftText(int32_t number);
55     std::u16string GetRightText(int32_t number);
56     int32_t GetTextIndexAtCursor();
57 
58 private:
59     static std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler();
60     static constexpr int32_t MAX_TIMEOUT = 2500;
61     void RegisterListener(int8_t type, int64_t id);
62     void UnRegisterListener(int8_t type);
63     void InitInsertText(int64_t id);
64     void InitDeleteRight(int64_t id);
65     void InitDeleteLeft(int64_t id);
66     void InitSendKeyboardStatus(int64_t id);
67     void InitSendFunctionKey(int64_t id);
68     void InitMoveCursor(int64_t id);
69     void InitHandleExtendAction(int64_t id);
70     void InitGetLeftText(int64_t id);
71     void InitGetRightText(int64_t id);
72     void InitSelectByRange(int64_t id);
73     void InitSelectByMovement(int64_t id);
74     void InitGetTextIndexAtCursor(int64_t id);
75     std::recursive_mutex mutex_;
76     std::function<void(Range)> onSelectByRange;
77     std::function<void(int32_t)> onSelectByMovement;
78     std::function<void(const char* text)> insertText;
79     std::function<void(int32_t length)> deleteRight;
80     std::function<void(int32_t length)> deleteLeft;
81     std::function<void(int32_t status)> sendKeyboardStatus;
82     std::function<void(int32_t functionKey)> sendFunctionKey;
83     std::function<void(int32_t direction)> moveCursor;
84     std::function<void(int32_t action)> handleExtendAction;
85     std::function<char*(int32_t number)> getLeftText;
86     std::function<char*(int32_t number)> getRightText;
87     std::function<int32_t(void)> getTextIndexAtCursor;
88     static std::mutex controllerMutex_;
89     static std::shared_ptr<CjInputMethodController> controller_;
90 };
91 } // namespace MiscServices
92 } // namespace OHOS
93 #endif // INPUT_METHOD_CONTROLLER_H
94