• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <map>
16 #include <mutex>
17 
18 #include "global.h"
19 #include "input_method_controller.h"
20 #include "input_method_utils.h"
21 #include "inputmethod_controller_capi.h"
22 #include "native_inputmethod_types.h"
23 #include "native_inputmethod_utils.h"
24 #include "native_text_changed_listener.h"
25 using namespace OHOS::MiscServices;
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
29 
30 struct InputMethod_InputMethodProxy {
31     InputMethod_TextEditorProxy *textEditor = nullptr;
32     OHOS::sptr<NativeTextChangedListener> listener = nullptr;
33     bool attached = false;
34 };
35 
36 InputMethod_InputMethodProxy *g_inputMethodProxy = nullptr;
37 std::mutex g_textEditorProxyMapMutex;
38 
IsValidInputMethodProxy(const InputMethod_InputMethodProxy * inputMethodProxy)39 InputMethod_ErrorCode IsValidInputMethodProxy(const InputMethod_InputMethodProxy *inputMethodProxy)
40 {
41     if (inputMethodProxy == nullptr) {
42         IMSA_HILOGE("inputMethodProxy is nullptr");
43         return IME_ERR_NULL_POINTER;
44     }
45     std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
46     if (g_inputMethodProxy == nullptr) {
47         IMSA_HILOGE("g_inputMethodProxy is nullptr");
48         return IME_ERR_DETACHED;
49     }
50 
51     if (g_inputMethodProxy != inputMethodProxy) {
52         IMSA_HILOGE("g_inputMethodProxy is not equal to inputMethodProxy");
53         return IME_ERR_PARAMCHECK;
54     }
55 
56     if (!(g_inputMethodProxy->attached)) {
57         IMSA_HILOGE("g_inputMethodProxy is not attached");
58         return IME_ERR_DETACHED;
59     }
60 
61     return IME_ERR_OK;
62 }
GetInputMethodProxy(InputMethod_TextEditorProxy * textEditor)63 static InputMethod_ErrorCode GetInputMethodProxy(InputMethod_TextEditorProxy *textEditor)
64 {
65     std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
66     if (g_inputMethodProxy != nullptr && textEditor == g_inputMethodProxy->textEditor) {
67         return IME_ERR_OK;
68     }
69 
70     if (g_inputMethodProxy != nullptr && textEditor != g_inputMethodProxy->textEditor) {
71         g_inputMethodProxy->listener = nullptr;
72         delete g_inputMethodProxy;
73         g_inputMethodProxy = nullptr;
74     }
75     OHOS::sptr<NativeTextChangedListener> listener = new (std::nothrow) NativeTextChangedListener(textEditor);
76     if (listener == nullptr) {
77         IMSA_HILOGE("new NativeTextChangedListener failed");
78         return IME_ERR_NULL_POINTER;
79     }
80 
81     g_inputMethodProxy = new (std::nothrow) InputMethod_InputMethodProxy({ textEditor, listener });
82     if (g_inputMethodProxy == nullptr) {
83         IMSA_HILOGE("new InputMethod_InputMethodProxy failed");
84         listener = nullptr;
85         return IME_ERR_NULL_POINTER;
86     }
87     return IME_ERR_OK;
88 }
89 #define CHECK_MEMBER_NULL(textEditor, member)   \
90     do {                                        \
91         if ((textEditor)->member == nullptr) {  \
92             IMSA_HILOGE(#member " is nullptr"); \
93             return IME_ERR_NULL_POINTER;        \
94         }                                       \
95     } while (0)
IsValidTextEditorProxy(InputMethod_TextEditorProxy * textEditor)96 static int32_t IsValidTextEditorProxy(InputMethod_TextEditorProxy *textEditor)
97 {
98     if (textEditor == nullptr) {
99         IMSA_HILOGE("textEditor is nullptr");
100         return IME_ERR_NULL_POINTER;
101     }
102 
103     CHECK_MEMBER_NULL(textEditor, getTextConfigFunc);
104     CHECK_MEMBER_NULL(textEditor, insertTextFunc);
105     CHECK_MEMBER_NULL(textEditor, deleteForwardFunc);
106     CHECK_MEMBER_NULL(textEditor, deleteBackwardFunc);
107     CHECK_MEMBER_NULL(textEditor, sendKeyboardStatusFunc);
108     CHECK_MEMBER_NULL(textEditor, sendEnterKeyFunc);
109     CHECK_MEMBER_NULL(textEditor, moveCursorFunc);
110     CHECK_MEMBER_NULL(textEditor, handleSetSelectionFunc);
111     CHECK_MEMBER_NULL(textEditor, handleExtendActionFunc);
112     CHECK_MEMBER_NULL(textEditor, getLeftTextOfCursorFunc);
113     CHECK_MEMBER_NULL(textEditor, getRightTextOfCursorFunc);
114     CHECK_MEMBER_NULL(textEditor, getTextIndexAtCursorFunc);
115     CHECK_MEMBER_NULL(textEditor, receivePrivateCommandFunc);
116     CHECK_MEMBER_NULL(textEditor, setPreviewTextFunc);
117     CHECK_MEMBER_NULL(textEditor, finishTextPreviewFunc);
118     return IME_ERR_OK;
119 }
120 
ConstructTextConfig(const InputMethod_TextConfig & config)121 static TextConfig ConstructTextConfig(const InputMethod_TextConfig &config)
122 {
123     TextConfig textConfig;
124     textConfig.inputAttribute.inputPattern = static_cast<InputMethod_TextInputType>(config.inputType);
125     textConfig.inputAttribute.enterKeyType = static_cast<InputMethod_EnterKeyType>(config.enterKeyType);
126     textConfig.inputAttribute.isTextPreviewSupported = config.previewTextSupported;
127     textConfig.cursorInfo.left = config.cursorInfo.left;
128     textConfig.cursorInfo.top = config.cursorInfo.top;
129     textConfig.cursorInfo.width = config.cursorInfo.width;
130     textConfig.cursorInfo.height = config.cursorInfo.height;
131     textConfig.range.start = config.selectionStart;
132     textConfig.range.end = config.selectionEnd;
133     textConfig.windowId = config.windowId;
134     textConfig.positionY = config.avoidInfo.positionY;
135     textConfig.height = config.avoidInfo.height;
136     auto placeholderLength = config.placeholderLength;
137     if (placeholderLength >0 &&  config.placeholder[placeholderLength - 1] == UTF16_ENDING_SYMBOL) {
138         placeholderLength = placeholderLength -1;
139     }
140     auto abilityNameLength = config.abilityNameLength;
141     if (abilityNameLength >0 && config.abilityName[abilityNameLength - 1] == UTF16_ENDING_SYMBOL) {
142         abilityNameLength = abilityNameLength - 1;
143     }
144     textConfig.inputAttribute.placeholder = std::u16string(config.placeholder, placeholderLength);
145     textConfig.inputAttribute.abilityName =  std::u16string(config.abilityName, abilityNameLength);
146     return textConfig;
147 }
148 
OH_InputMethodController_Attach(InputMethod_TextEditorProxy * textEditor,InputMethod_AttachOptions * options,InputMethod_InputMethodProxy ** inputMethodProxy)149 InputMethod_ErrorCode OH_InputMethodController_Attach(InputMethod_TextEditorProxy *textEditor,
150     InputMethod_AttachOptions *options, InputMethod_InputMethodProxy **inputMethodProxy)
151 {
152     if ((IsValidTextEditorProxy(textEditor) != IME_ERR_OK) || options == nullptr || inputMethodProxy == nullptr) {
153         IMSA_HILOGE("invalid parameter");
154         return IME_ERR_NULL_POINTER;
155     }
156 
157     InputMethod_ErrorCode errCode = GetInputMethodProxy(textEditor);
158     if (errCode != IME_ERR_OK) {
159         return errCode;
160     }
161 
162     InputMethod_TextConfig config;
163     textEditor->getTextConfigFunc(textEditor, &config);
164 
165     auto textConfig = ConstructTextConfig(config);
166 
167     auto controller = InputMethodController::GetInstance();
168     if (controller == nullptr) {
169         IMSA_HILOGE("controller is nullptr");
170         return IME_ERR_NULL_POINTER;
171     }
172     OHOS::sptr<NativeTextChangedListener> listener = nullptr;
173     {
174         std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
175         if (g_inputMethodProxy != nullptr) {
176             listener = g_inputMethodProxy->listener;
177         }
178     }
179     AttachOptions attachOptions;
180     attachOptions.isShowKeyboard = options->showKeyboard;
181     attachOptions.requestKeyboardReason =
182         static_cast<RequestKeyboardReason>(static_cast<int32_t>(options->requestKeyboardReason));
183     int32_t err = controller->Attach(listener, attachOptions, textConfig, ClientType::CAPI);
184     if (err == ErrorCode::NO_ERROR) {
185         errCode = IME_ERR_OK;
186         std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
187         if (g_inputMethodProxy != nullptr) {
188             g_inputMethodProxy->attached = true;
189         }
190         *inputMethodProxy = g_inputMethodProxy;
191     } else {
192         errCode = ErrorCodeConvert(err);
193     }
194 
195     return errCode;
196 }
197 
ClearInputMethodProxy(void)198 void ClearInputMethodProxy(void)
199 {
200     std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
201     if (g_inputMethodProxy != nullptr) {
202         IMSA_HILOGI("g_inputMethodProxy is detached");
203         g_inputMethodProxy->attached = false;
204     }
205 }
206 
OH_InputMethodController_Detach(InputMethod_InputMethodProxy * inputMethodProxy)207 InputMethod_ErrorCode OH_InputMethodController_Detach(InputMethod_InputMethodProxy *inputMethodProxy)
208 {
209     if (inputMethodProxy == nullptr) {
210         IMSA_HILOGE("inputMethodProxy is nullptr");
211         return IME_ERR_NULL_POINTER;
212     }
213     {
214         std::lock_guard<std::mutex> guard(g_textEditorProxyMapMutex);
215         if (g_inputMethodProxy == nullptr) {
216             IMSA_HILOGE("g_inputMethodProxy is nullptr");
217             return IME_ERR_DETACHED;
218         }
219 
220         if (g_inputMethodProxy != inputMethodProxy) {
221             IMSA_HILOGE("g_inputMethodProxy is not equal to inputMethodProxy");
222             return IME_ERR_PARAMCHECK;
223         }
224 
225         IMSA_HILOGI("detach g_inputMethodProxy");
226         g_inputMethodProxy->listener = nullptr;
227         delete g_inputMethodProxy;
228         g_inputMethodProxy = nullptr;
229     }
230     auto instance  = InputMethodController::GetInstance();
231     if (instance == nullptr) {
232         IMSA_HILOGE("instance is nullptr");
233         return IME_ERR_NULL_POINTER;
234     }
235     return ErrorCodeConvert(instance->Close());
236 }
237 #ifdef __cplusplus
238 }
239 #endif /* __cplusplus */