• 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 "securec.h"
16 
17 #include "global.h"
18 #include "native_inputmethod_types.h"
19 #include "string_utils.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24 using namespace OHOS::MiscServices;
OH_TextConfig_Create(void)25 InputMethod_TextConfig *OH_TextConfig_Create(void)
26 {
27     return new (std::nothrow) InputMethod_TextConfig();
28 }
OH_TextConfig_Destroy(InputMethod_TextConfig * config)29 void OH_TextConfig_Destroy(InputMethod_TextConfig *config)
30 {
31     if (config == nullptr) {
32         IMSA_HILOGE("config is nullptr");
33         return;
34     }
35 
36     delete config;
37 }
OH_TextConfig_SetInputType(InputMethod_TextConfig * config,InputMethod_TextInputType inputType)38 InputMethod_ErrorCode OH_TextConfig_SetInputType(InputMethod_TextConfig *config, InputMethod_TextInputType inputType)
39 {
40     if (config == nullptr) {
41         IMSA_HILOGE("config is nullptr");
42         return IME_ERR_NULL_POINTER;
43     }
44     config->inputType = inputType;
45     return IME_ERR_OK;
46 }
OH_TextConfig_SetEnterKeyType(InputMethod_TextConfig * config,InputMethod_EnterKeyType enterKeyType)47 InputMethod_ErrorCode OH_TextConfig_SetEnterKeyType(
48     InputMethod_TextConfig *config, InputMethod_EnterKeyType enterKeyType)
49 {
50     if (config == nullptr) {
51         IMSA_HILOGE("config is nullptr");
52         return IME_ERR_NULL_POINTER;
53     }
54 
55     config->enterKeyType = enterKeyType;
56     return IME_ERR_OK;
57 }
OH_TextConfig_SetPreviewTextSupport(InputMethod_TextConfig * config,bool supported)58 InputMethod_ErrorCode OH_TextConfig_SetPreviewTextSupport(InputMethod_TextConfig *config, bool supported)
59 {
60     if (config == nullptr) {
61         IMSA_HILOGE("config is nullptr");
62         return IME_ERR_NULL_POINTER;
63     }
64     config->previewTextSupported = supported;
65     return IME_ERR_OK;
66 }
67 
OH_TextConfig_SetSelection(InputMethod_TextConfig * config,int32_t start,int32_t end)68 InputMethod_ErrorCode OH_TextConfig_SetSelection(InputMethod_TextConfig *config, int32_t start, int32_t end)
69 {
70     if (config == nullptr) {
71         IMSA_HILOGE("config is nullptr");
72         return IME_ERR_NULL_POINTER;
73     }
74     config->selectionStart = start;
75     config->selectionEnd = end;
76     return IME_ERR_OK;
77 }
OH_TextConfig_SetWindowId(InputMethod_TextConfig * config,int32_t windowId)78 InputMethod_ErrorCode OH_TextConfig_SetWindowId(InputMethod_TextConfig *config, int32_t windowId)
79 {
80     if (config == nullptr) {
81         IMSA_HILOGE("config is nullptr");
82         return IME_ERR_NULL_POINTER;
83     }
84 
85     config->windowId = windowId;
86     return IME_ERR_OK;
87 }
88 
OH_TextConfig_SetPlaceholder(InputMethod_TextConfig * config,const char16_t * placeholder,size_t length)89 InputMethod_ErrorCode OH_TextConfig_SetPlaceholder(InputMethod_TextConfig *config, const char16_t *placeholder,
90     size_t length)
91 {
92     if (config == nullptr) {
93         IMSA_HILOGE("config is nullptr");
94         return IME_ERR_NULL_POINTER;
95     }
96     if (length <= 0 || placeholder == nullptr) {
97         config->placeholderLength = ENDING_SYMBOL_SIZE;
98         config->placeholder[0] = UTF16_ENDING_SYMBOL;
99         return IME_ERR_OK;
100     }
101     if (length == 1 && placeholder[length - 1] == UTF16_ENDING_SYMBOL) {
102         config->placeholderLength = ENDING_SYMBOL_SIZE;
103         config->placeholder[0] = UTF16_ENDING_SYMBOL;
104         return IME_ERR_OK;
105     }
106     if (length > MAX_PLACEHOLDER_INPUT_SIZE) {
107         IMSA_HILOGW("chars length exceeds limit inputLen:%{public}zu, limit len:%{public}zu", length,
108             MAX_PLACEHOLDER_INPUT_SIZE);
109         length = MAX_PLACEHOLDER_INPUT_SIZE;
110     }
111     std::u16string u16Placeholder(placeholder, length);
112     StringUtils::TruncateUtf16String(u16Placeholder, MAX_PLACEHOLDER_SIZE);
113     IMSA_HILOGD("memcpy_s begin dest len:%{public}zu, src len:%{public}zu",
114         MAX_PLACEHOLDER_INPUT_SIZE, u16Placeholder.size());
115     if (u16Placeholder.size() > MAX_PLACEHOLDER_INPUT_SIZE) {
116         IMSA_HILOGE("function truncateUtf16String error");
117         return IME_ERR_NULL_POINTER;
118     }
119     errno_t err = memcpy_s(config->placeholder, MAX_PLACEHOLDER_INPUT_SIZE * sizeof(char16_t),
120         u16Placeholder.data(), u16Placeholder.size() * sizeof(char16_t));
121     if (err != EOK) {
122         IMSA_HILOGE("placeholder content copy error:%{public}d", (int32_t)err);
123         return IME_ERR_NULL_POINTER;
124     }
125     config->placeholderLength = u16Placeholder.size();
126     if (config->placeholder[config->placeholderLength - 1] != UTF16_ENDING_SYMBOL &&
127         config->placeholderLength < MAX_PLACEHOLDER_INPUT_SIZE) {
128         config->placeholder[config->placeholderLength] = UTF16_ENDING_SYMBOL;
129         config->placeholderLength = config->placeholderLength + 1;
130     }
131     IMSA_HILOGD("placeholderLength:%{public}zu,length:%{public}zu,lastChar16_t:%{public}u",
132         config->placeholderLength, length,
133         static_cast<uint32_t>(config->placeholder[config->placeholderLength - 1]));
134     return IME_ERR_OK;
135 }
136 
OH_TextConfig_SetAbilityName(InputMethod_TextConfig * config,const char16_t * abilityName,size_t length)137 InputMethod_ErrorCode OH_TextConfig_SetAbilityName(InputMethod_TextConfig *config, const char16_t *abilityName,
138     size_t length)
139 {
140     if (config == nullptr) {
141         IMSA_HILOGE("config is nullptr");
142         return IME_ERR_NULL_POINTER;
143     }
144     if (length <= 0 || abilityName == nullptr) {
145         config->abilityNameLength = ENDING_SYMBOL_SIZE;
146         config->abilityName[0] = UTF16_ENDING_SYMBOL;
147         return IME_ERR_OK;
148     }
149     if (length == 1 && abilityName[length - 1] == UTF16_ENDING_SYMBOL) {
150         config->abilityNameLength = ENDING_SYMBOL_SIZE;
151         config->abilityName[0] = UTF16_ENDING_SYMBOL;
152         return IME_ERR_OK;
153     }
154     if (length > MAX_ABILITY_NAME_INPUT_SIZE) {
155         IMSA_HILOGW("chars length exceeds limit inputLen:%{public}zu, limit len:%{public}zu", length,
156             MAX_ABILITY_NAME_INPUT_SIZE);
157         length = MAX_ABILITY_NAME_INPUT_SIZE;
158     }
159     std::u16string u16abilityName(abilityName, length);
160     StringUtils::TruncateUtf16String(u16abilityName, MAX_ABILITY_NAME_SIZE);
161     IMSA_HILOGD("memcpy_s begin dest len:%{public}zu, src len:%{public}zu",
162         MAX_ABILITY_NAME_INPUT_SIZE, u16abilityName.size());
163     if (u16abilityName.size() > MAX_ABILITY_NAME_INPUT_SIZE) {
164         IMSA_HILOGE("function truncateUtf16String error");
165         return IME_ERR_NULL_POINTER;
166     }
167     errno_t err = memcpy_s(config->abilityName, MAX_ABILITY_NAME_INPUT_SIZE * sizeof(char16_t),
168         u16abilityName.data(), u16abilityName.size() * sizeof(char16_t));
169     if (err != EOK) {
170         IMSA_HILOGE("abilityName content copy error:%{public}d", (int32_t)err);
171         return IME_ERR_NULL_POINTER;
172     }
173     config->abilityNameLength = u16abilityName.size();
174     if (config->abilityName[config->abilityNameLength - 1] != UTF16_ENDING_SYMBOL &&
175         config->abilityNameLength < MAX_ABILITY_NAME_INPUT_SIZE) {
176         config->abilityName[config->abilityNameLength] = UTF16_ENDING_SYMBOL;
177         config->abilityNameLength = config->abilityNameLength + 1;
178     }
179     IMSA_HILOGD("abilityNameLength:%{public}zu,length:%{public}zu,lastChar16_t:%{public}u",
180         config->abilityNameLength, length,
181         static_cast<uint32_t>(config->abilityName[config->abilityNameLength - 1]));
182     return IME_ERR_OK;
183 }
184 
OH_TextConfig_GetInputType(InputMethod_TextConfig * config,InputMethod_TextInputType * inputType)185 InputMethod_ErrorCode OH_TextConfig_GetInputType(InputMethod_TextConfig *config, InputMethod_TextInputType *inputType)
186 {
187     if (config == nullptr) {
188         IMSA_HILOGE("config is nullptr");
189         return IME_ERR_NULL_POINTER;
190     }
191     if (inputType == nullptr) {
192         IMSA_HILOGE("inputType is nullptr");
193         return IME_ERR_NULL_POINTER;
194     }
195     *inputType = config->inputType;
196     return IME_ERR_OK;
197 }
198 
OH_TextConfig_GetEnterKeyType(InputMethod_TextConfig * config,InputMethod_EnterKeyType * enterKeyType)199 InputMethod_ErrorCode OH_TextConfig_GetEnterKeyType(
200     InputMethod_TextConfig *config, InputMethod_EnterKeyType *enterKeyType)
201 {
202     if (config == nullptr) {
203         IMSA_HILOGE("config is nullptr");
204         return IME_ERR_NULL_POINTER;
205     }
206     if (enterKeyType == nullptr) {
207         IMSA_HILOGE("enterKeyType is nullptr");
208         return IME_ERR_NULL_POINTER;
209     }
210     *enterKeyType = config->enterKeyType;
211     return IME_ERR_OK;
212 }
213 
OH_TextConfig_IsPreviewTextSupported(InputMethod_TextConfig * config,bool * supported)214 InputMethod_ErrorCode OH_TextConfig_IsPreviewTextSupported(InputMethod_TextConfig *config, bool *supported)
215 {
216     if (config == nullptr) {
217         IMSA_HILOGE("config is nullptr");
218         return IME_ERR_NULL_POINTER;
219     }
220     if (supported == nullptr) {
221         IMSA_HILOGE("supported is nullptr");
222         return IME_ERR_NULL_POINTER;
223     }
224     *supported = config->previewTextSupported;
225     return IME_ERR_OK;
226 }
227 
OH_TextConfig_GetCursorInfo(InputMethod_TextConfig * config,InputMethod_CursorInfo ** cursorInfo)228 InputMethod_ErrorCode OH_TextConfig_GetCursorInfo(InputMethod_TextConfig *config, InputMethod_CursorInfo **cursorInfo)
229 {
230     if (config == nullptr) {
231         IMSA_HILOGE("config is nullptr");
232         return IME_ERR_NULL_POINTER;
233     }
234     if (cursorInfo == nullptr) {
235         IMSA_HILOGE("cursorInfo is nullptr");
236         return IME_ERR_NULL_POINTER;
237     }
238     *cursorInfo = &config->cursorInfo;
239     return IME_ERR_OK;
240 }
241 
OH_TextConfig_GetTextAvoidInfo(InputMethod_TextConfig * config,InputMethod_TextAvoidInfo ** avoidInfo)242 InputMethod_ErrorCode OH_TextConfig_GetTextAvoidInfo(
243     InputMethod_TextConfig *config, InputMethod_TextAvoidInfo **avoidInfo)
244 {
245     if (config == nullptr) {
246         IMSA_HILOGE("config is nullptr");
247         return IME_ERR_NULL_POINTER;
248     }
249     if (avoidInfo == nullptr) {
250         IMSA_HILOGE("avoidInfo is nullptr");
251         return IME_ERR_NULL_POINTER;
252     }
253     *avoidInfo = &config->avoidInfo;
254     return IME_ERR_OK;
255 }
256 
OH_TextConfig_GetSelection(InputMethod_TextConfig * config,int32_t * start,int32_t * end)257 InputMethod_ErrorCode OH_TextConfig_GetSelection(InputMethod_TextConfig *config, int32_t *start, int32_t *end)
258 {
259     if (config == nullptr) {
260         IMSA_HILOGE("config is nullptr");
261         return IME_ERR_NULL_POINTER;
262     }
263     if (start == nullptr) {
264         IMSA_HILOGE("start is nullptr");
265         return IME_ERR_NULL_POINTER;
266     }
267     if (end == nullptr) {
268         IMSA_HILOGE("end is nullptr");
269         return IME_ERR_NULL_POINTER;
270     }
271     *start = config->selectionStart;
272     *end = config->selectionEnd;
273     return IME_ERR_OK;
274 }
275 
OH_TextConfig_GetWindowId(InputMethod_TextConfig * config,int32_t * windowId)276 InputMethod_ErrorCode OH_TextConfig_GetWindowId(InputMethod_TextConfig *config, int32_t *windowId)
277 {
278     if (config == nullptr) {
279         IMSA_HILOGE("config is nullptr");
280         return IME_ERR_NULL_POINTER;
281     }
282     if (windowId == nullptr) {
283         IMSA_HILOGE("windowId is nullptr");
284         return IME_ERR_NULL_POINTER;
285     }
286     *windowId = config->windowId;
287     return IME_ERR_OK;
288 }
289 
OH_TextConfig_GetPlaceholder(InputMethod_TextConfig * config,char16_t * placeholder,size_t * length)290 InputMethod_ErrorCode OH_TextConfig_GetPlaceholder(InputMethod_TextConfig *config, char16_t *placeholder,
291     size_t *length)
292 {
293     if (config == nullptr) {
294         IMSA_HILOGE("config is nullptr");
295         return IME_ERR_NULL_POINTER;
296     }
297     if (length == nullptr) {
298         IMSA_HILOGE("length is nullptr");
299         return IME_ERR_NULL_POINTER;
300     }
301     if (config->placeholderLength <= ENDING_SYMBOL_SIZE) {
302         config->placeholderLength = ENDING_SYMBOL_SIZE;
303         config->placeholder[0] = UTF16_ENDING_SYMBOL;
304     }
305     IMSA_HILOGD("curLen:%{public}zu,inputLen:%{public}zu", config->placeholderLength, *length);
306     if (placeholder == nullptr) {
307         IMSA_HILOGE("placeholder is nullptr");
308         *length = config->placeholderLength;
309         return IME_ERR_NULL_POINTER;
310     }
311     if ((*length) < config->placeholderLength) {
312         IMSA_HILOGE("input memory is less than the length of the obtained memory. actual length:%{public}zu",
313             config->placeholderLength);
314         *length = config->placeholderLength;
315         return IME_ERR_PARAMCHECK;
316     }
317     auto byteLen = (*length) * sizeof(char16_t);
318     *length = config->placeholderLength;
319     errno_t err = memcpy_s(placeholder, byteLen, config->placeholder, config->placeholderLength * sizeof(char16_t));
320     if (err != EOK) {
321         IMSA_HILOGE("placeholder content copy error:%{public}d", (int32_t)err);
322         return IME_ERR_PARAMCHECK;
323     }
324     return IME_ERR_OK;
325 }
326 
OH_TextConfig_GetAbilityName(InputMethod_TextConfig * config,char16_t * abilityName,size_t * length)327 InputMethod_ErrorCode OH_TextConfig_GetAbilityName(InputMethod_TextConfig *config, char16_t *abilityName,
328     size_t *length)
329 {
330     if (config == nullptr) {
331         IMSA_HILOGE("config is nullptr");
332         return IME_ERR_NULL_POINTER;
333     }
334     if (length == nullptr) {
335         IMSA_HILOGE("length is nullptr");
336         return IME_ERR_NULL_POINTER;
337     }
338     if (abilityName == nullptr) {
339         IMSA_HILOGE("abilityName is nullptr");
340         *length = config->abilityNameLength;
341         return IME_ERR_NULL_POINTER;
342     }
343     if (config->abilityNameLength <= ENDING_SYMBOL_SIZE) {
344         config->abilityNameLength = ENDING_SYMBOL_SIZE;
345         config->abilityName[0] = UTF16_ENDING_SYMBOL;
346     }
347     IMSA_HILOGD("curLen:%{public}zu,inputLen:%{public}zu", config->abilityNameLength, *length);
348     if ((*length) < config->abilityNameLength) {
349         IMSA_HILOGE("input memory is less than the length of the obtained memory. actual length:%{public}zu",
350             config->abilityNameLength);
351         *length = config->abilityNameLength;
352         return IME_ERR_PARAMCHECK;
353     }
354     auto byteLen = (*length) * sizeof(char16_t);
355     *length = config->abilityNameLength;
356     errno_t err = memcpy_s(abilityName, byteLen, config->abilityName, config->abilityNameLength * sizeof(char16_t));
357     if (err != EOK) {
358         IMSA_HILOGE("abilityName content copy error:%{public}d", (int32_t)err);
359         return IME_ERR_PARAMCHECK;
360     }
361     return IME_ERR_OK;
362 }
363 #ifdef __cplusplus
364 }
365 #endif /* __cplusplus */