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 "input_method_controller.h"
16 #include "native_inputmethod_types.h"
17 #include "native_inputmethod_utils.h"
18 #ifdef __cplusplus
19 extern "C" {
20 #endif /* __cplusplus */
21 using namespace OHOS::MiscServices;
OH_InputMethodProxy_ShowKeyboard(InputMethod_InputMethodProxy * inputMethodProxy)22 InputMethod_ErrorCode OH_InputMethodProxy_ShowKeyboard(InputMethod_InputMethodProxy *inputMethodProxy)
23 {
24 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
25 if (errCode != IME_ERR_OK) {
26 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
27 return errCode;
28 }
29 return ErrorCodeConvert(InputMethodController::GetInstance()->ShowCurrentInput());
30 }
OH_InputMethodProxy_HideKeyboard(InputMethod_InputMethodProxy * inputMethodProxy)31 InputMethod_ErrorCode OH_InputMethodProxy_HideKeyboard(InputMethod_InputMethodProxy *inputMethodProxy)
32 {
33 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
34 if (errCode != IME_ERR_OK) {
35 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
36 return errCode;
37 }
38 return ErrorCodeConvert(InputMethodController::GetInstance()->HideCurrentInput());
39 }
OH_InputMethodProxy_NotifySelectionChange(InputMethod_InputMethodProxy * inputMethodProxy,char16_t text[],size_t length,int start,int end)40 InputMethod_ErrorCode OH_InputMethodProxy_NotifySelectionChange(
41 InputMethod_InputMethodProxy *inputMethodProxy, char16_t text[], size_t length, int start, int end)
42 {
43 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
44 if (errCode != IME_ERR_OK) {
45 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
46 return errCode;
47 }
48 if (text == nullptr) {
49 IMSA_HILOGE("text is nullptr");
50 return IME_ERR_NULL_POINTER;
51 }
52
53 if (length > MAX_TEXT_LENGTH) {
54 IMSA_HILOGE("text length is too long length=%{public}zu", length);
55 return IME_ERR_PARAMCHECK;
56 }
57 return ErrorCodeConvert(
58 InputMethodController::GetInstance()->OnSelectionChange(std::u16string(text, length), start, end));
59 }
OH_InputMethodProxy_NotifyConfigurationChange(InputMethod_InputMethodProxy * inputMethodProxy,InputMethod_EnterKeyType enterKey,InputMethod_TextInputType textType)60 InputMethod_ErrorCode OH_InputMethodProxy_NotifyConfigurationChange(InputMethod_InputMethodProxy *inputMethodProxy,
61 InputMethod_EnterKeyType enterKey, InputMethod_TextInputType textType)
62 {
63 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
64 if (errCode != IME_ERR_OK) {
65 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
66 return errCode;
67 }
68 Configuration info;
69 info.SetEnterKeyType(static_cast<EnterKeyType>(enterKey));
70 info.SetTextInputType(static_cast<TextInputType>(textType));
71 return ErrorCodeConvert(InputMethodController::GetInstance()->OnConfigurationChange(info));
72 }
73
OH_InputMethodProxy_NotifyCursorUpdate(InputMethod_InputMethodProxy * inputMethodProxy,InputMethod_CursorInfo * cursorInfo)74 InputMethod_ErrorCode OH_InputMethodProxy_NotifyCursorUpdate(
75 InputMethod_InputMethodProxy *inputMethodProxy, InputMethod_CursorInfo *cursorInfo)
76 {
77 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
78 if (errCode != IME_ERR_OK) {
79 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
80 return errCode;
81 }
82 if (cursorInfo == nullptr) {
83 IMSA_HILOGE("cursorInfo is nullptr");
84 return IME_ERR_NULL_POINTER;
85 }
86 return ErrorCodeConvert(InputMethodController::GetInstance()->OnCursorUpdate(
87 CursorInfo({ cursorInfo->left, cursorInfo->top, cursorInfo->width, cursorInfo->height })));
88 }
89
OH_InputMethodProxy_SendPrivateCommand(InputMethod_InputMethodProxy * inputMethodProxy,InputMethod_PrivateCommand * privateCommand[],size_t size)90 InputMethod_ErrorCode OH_InputMethodProxy_SendPrivateCommand(
91 InputMethod_InputMethodProxy *inputMethodProxy, InputMethod_PrivateCommand *privateCommand[], size_t size)
92 {
93 auto errCode = IsValidInputMethodProxy(inputMethodProxy);
94 if (errCode != IME_ERR_OK) {
95 IMSA_HILOGE("invalid state, errCode=%{public}d", errCode);
96 return errCode;
97 }
98 if (privateCommand == nullptr) {
99 IMSA_HILOGE("privateCommand is nullptr");
100 return IME_ERR_NULL_POINTER;
101 }
102
103 std::unordered_map<std::string, PrivateDataValue> command;
104
105 for (size_t i = 0; i < size; i++) {
106 if (privateCommand[i] == nullptr) {
107 IMSA_HILOGE("privateCommand[%zu] is nullptr", i);
108 return IME_ERR_NULL_POINTER;
109 }
110 command.emplace(privateCommand[i]->key, privateCommand[i]->value);
111 }
112 return ErrorCodeConvert(InputMethodController::GetInstance()->SendPrivateCommand(command));
113 }
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */