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 "global.h"
16 #include "native_inputmethod_types.h"
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20
OH_PrivateCommand_Create(char key[],size_t keyLength)21 InputMethod_PrivateCommand *OH_PrivateCommand_Create(char key[], size_t keyLength)
22 {
23 return new InputMethod_PrivateCommand({ std::string(key, keyLength), false });
24 }
OH_PrivateCommand_Destroy(InputMethod_PrivateCommand * command)25 void OH_PrivateCommand_Destroy(InputMethod_PrivateCommand *command)
26 {
27 if (command == nullptr) {
28 IMSA_HILOGE("command is nullptr");
29 return;
30 }
31 delete command;
32 }
OH_PrivateCommand_SetKey(InputMethod_PrivateCommand * command,char key[],size_t keyLength)33 InputMethod_ErrorCode OH_PrivateCommand_SetKey(InputMethod_PrivateCommand *command, char key[], size_t keyLength)
34 {
35 if (command == nullptr) {
36 IMSA_HILOGE("command is nullptr");
37 return IME_ERR_NULL_POINTER;
38 }
39 if (key == nullptr) {
40 IMSA_HILOGE("key is nullptr");
41 return IME_ERR_NULL_POINTER;
42 }
43 command->key = std::string(key, keyLength);
44 return IME_ERR_OK;
45 }
OH_PrivateCommand_SetBoolValue(InputMethod_PrivateCommand * command,bool value)46 InputMethod_ErrorCode OH_PrivateCommand_SetBoolValue(InputMethod_PrivateCommand *command, bool value)
47 {
48 if (command == nullptr) {
49 IMSA_HILOGE("command is nullptr");
50 return IME_ERR_NULL_POINTER;
51 }
52 command->value = value;
53 return IME_ERR_OK;
54 }
OH_PrivateCommand_SetIntValue(InputMethod_PrivateCommand * command,int32_t value)55 InputMethod_ErrorCode OH_PrivateCommand_SetIntValue(InputMethod_PrivateCommand *command, int32_t value)
56 {
57 if (command == nullptr) {
58 IMSA_HILOGE("command is nullptr");
59 return IME_ERR_NULL_POINTER;
60 }
61 command->value = value;
62 return IME_ERR_OK;
63 }
OH_PrivateCommand_SetStrValue(InputMethod_PrivateCommand * command,char value[],size_t valueLength)64 InputMethod_ErrorCode OH_PrivateCommand_SetStrValue(
65 InputMethod_PrivateCommand *command, char value[], size_t valueLength)
66 {
67 if (command == nullptr) {
68 IMSA_HILOGE("command is nullptr");
69 return IME_ERR_NULL_POINTER;
70 }
71 if (value == nullptr) {
72 IMSA_HILOGE("value is nullptr");
73 return IME_ERR_NULL_POINTER;
74 }
75 command->value = std::string(value, valueLength);
76 return IME_ERR_OK;
77 }
78
OH_PrivateCommand_GetKey(InputMethod_PrivateCommand * command,const char ** key,size_t * keyLength)79 InputMethod_ErrorCode OH_PrivateCommand_GetKey(InputMethod_PrivateCommand *command, const char **key, size_t *keyLength)
80 {
81 if (command == nullptr) {
82 IMSA_HILOGE("command is nullptr");
83 return IME_ERR_NULL_POINTER;
84 }
85 if (key == nullptr) {
86 IMSA_HILOGE("key is nullptr");
87 return IME_ERR_NULL_POINTER;
88 }
89 if (keyLength == nullptr) {
90 IMSA_HILOGE("keyLength is nullptr");
91 return IME_ERR_NULL_POINTER;
92 }
93 *key = command->key.c_str();
94 *keyLength = command->key.length();
95 return IME_ERR_OK;
96 }
97
OH_PrivateCommand_GetValueType(InputMethod_PrivateCommand * command,InputMethod_CommandValueType * type)98 InputMethod_ErrorCode OH_PrivateCommand_GetValueType(
99 InputMethod_PrivateCommand *command, InputMethod_CommandValueType *type)
100 {
101 if (command == nullptr) {
102 IMSA_HILOGE("command is nullptr");
103 return IME_ERR_NULL_POINTER;
104 }
105 if (type == nullptr) {
106 IMSA_HILOGE("type is nullptr");
107 return IME_ERR_NULL_POINTER;
108 }
109 if (std::holds_alternative<bool>(command->value)) {
110 *type = IME_COMMAND_VALUE_TYPE_BOOL;
111 } else if (std::holds_alternative<int32_t>(command->value)) {
112 *type = IME_COMMAND_VALUE_TYPE_INT32;
113 } else if (std::holds_alternative<std::string>(command->value)) {
114 *type = IME_COMMAND_VALUE_TYPE_STRING;
115 } else {
116 IMSA_HILOGE("value is not bool or int or string");
117 *type = IME_COMMAND_VALUE_TYPE_NONE;
118 }
119
120 return IME_ERR_OK;
121 }
122
OH_PrivateCommand_GetBoolValue(InputMethod_PrivateCommand * command,bool * value)123 InputMethod_ErrorCode OH_PrivateCommand_GetBoolValue(InputMethod_PrivateCommand *command, bool *value)
124 {
125 if (command == nullptr) {
126 IMSA_HILOGE("command is nullptr");
127 return IME_ERR_NULL_POINTER;
128 }
129
130 if (value == nullptr) {
131 IMSA_HILOGE("value is nullptr");
132 return IME_ERR_NULL_POINTER;
133 }
134
135 if (!std::holds_alternative<bool>(command->value)) {
136 IMSA_HILOGE("value is not bool");
137 return IME_ERR_QUERY_FAILED;
138 }
139 *value = std::get<bool>(command->value);
140 return IME_ERR_OK;
141 }
OH_PrivateCommand_GetIntValue(InputMethod_PrivateCommand * command,int32_t * value)142 InputMethod_ErrorCode OH_PrivateCommand_GetIntValue(InputMethod_PrivateCommand *command, int32_t *value)
143 {
144 if (command == nullptr) {
145 IMSA_HILOGE("command is nullptr");
146 return IME_ERR_NULL_POINTER;
147 }
148
149 if (value == nullptr) {
150 IMSA_HILOGE("value is nullptr");
151 return IME_ERR_NULL_POINTER;
152 }
153
154 if (!std::holds_alternative<int32_t>(command->value)) {
155 IMSA_HILOGE("value is not int32_t");
156 return IME_ERR_QUERY_FAILED;
157 }
158
159 *value = std::get<int32_t>(command->value);
160 return IME_ERR_OK;
161 }
OH_PrivateCommand_GetStrValue(InputMethod_PrivateCommand * command,const char ** value,size_t * valueLength)162 InputMethod_ErrorCode OH_PrivateCommand_GetStrValue(
163 InputMethod_PrivateCommand *command, const char **value, size_t *valueLength)
164 {
165 if (command == nullptr) {
166 IMSA_HILOGE("command is nullptr");
167 return IME_ERR_NULL_POINTER;
168 }
169 if (value == nullptr) {
170 IMSA_HILOGE("value is nullptr");
171 return IME_ERR_NULL_POINTER;
172 }
173
174 if (valueLength == nullptr) {
175 IMSA_HILOGE("valueLength is nullptr");
176 return IME_ERR_NULL_POINTER;
177 }
178
179 if (!std::holds_alternative<std::string>(command->value)) {
180 IMSA_HILOGE("value is not string");
181 return IME_ERR_QUERY_FAILED;
182 }
183
184 *value = std::get<std::string>(command->value).c_str();
185 *valueLength = std::get<std::string>(command->value).length();
186 return IME_ERR_OK;
187 }
188 #ifdef __cplusplus
189 }
190 #endif /* __cplusplus */