2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <remote_input.h>
22 #include <remote_input_private.h>
23 #include "privilege_checker_private.h"
28 #define LOG_TAG "REMOTE_INPUT"
30 #define REMOTE_PRIVILEGE "http://tizen.org/privilege/internal/default/platform"
32 static remote_input_error_e _remote_check_privilege() {
34 remote_input_error_e ret = REMOTE_INPUT_ERROR_NONE;
36 if (inputmethod_cynara_initialize() == false) {
37 LOGE("cynara_initialize () == false");
38 return REMOTE_INPUT_ERROR_PERMISSION_DENIED;
41 snprintf(uid, 16, "%d", getuid());
42 if (check_privilege(uid, REMOTE_PRIVILEGE) == false) {
43 LOGE("check_privilege(uid, REMOTE_PRIVILEGE) == false. uid : %s", uid);
44 ret = REMOTE_INPUT_ERROR_PERMISSION_DENIED;
47 inputmethod_cynara_finish();
52 static void _metadata_updated_cb(void *user_data, remote_control_entry_metadata_s *data)
54 remote_input_h remote_handle = (remote_input_h)user_data;
56 if (remote_handle == NULL) {
57 LOGE("remote handle is not available");
61 remote_handle->hint = data->hint;
62 remote_handle->layout = data->layout;
63 remote_handle->variation = data->variation;
64 remote_handle->autocapital_type = data->autocapital_type;
65 remote_handle->return_key_disabled = data->return_key_disabled;
66 remote_handle->return_key_type = data->return_key_type;
68 if (remote_handle->metadata_updated_cb) {
69 remote_handle->metadata_updated_cb(remote_handle->metadata_updated_cb_user_data);
73 static void _text_updated_cb(void *user_data, const char *text, int cursor_pos)
75 remote_input_h remote_handle = (remote_input_h)user_data;
77 if (remote_handle == NULL) {
78 LOGE("remote handle is not available");
82 if (remote_handle->text_updated_cb) {
83 remote_handle->text_updated_cb(text, cursor_pos, remote_handle->text_updated_cb_user_data);
87 static void _resource_updated_cb(void *user_data, remote_control_input_resource resource)
89 remote_input_h remote_handle = (remote_input_h)user_data;
91 if (remote_handle == NULL) {
92 LOGE("remote handle is not available");
96 if (remote_handle->resource_changed_cb) {
97 remote_handle->resource_changed_cb(resource == REMOTE_CONTROL_INPUT_RESOURCE_LOCAL ? REMOTE_INPUT_RESOURCE_LOCAL : REMOTE_INPUT_RESOURCE_REMOTE,
98 remote_handle->resource_changed_cb_user_data);
102 EXPORT_API int remote_input_create(remote_input_h *remote_handle)
104 remote_input_error_e ret = REMOTE_INPUT_ERROR_NONE;
106 if (!remote_handle) {
107 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
108 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
111 ret = _remote_check_privilege();
112 if (ret != REMOTE_INPUT_ERROR_NONE) {
113 LOGE("REMOTE_INPUT_ERROR_PERMISSION_DENIED");
117 struct remote_input_s *remote_input = (remote_input_h)calloc(1, sizeof(struct remote_input_s));
119 LOGE("REMOTE_INPUT_ERROR_OUT_OF_MEMORY");
120 return REMOTE_INPUT_ERROR_OUT_OF_MEMORY;
123 remote_input->remote_client = remote_control_connect();
124 if (!remote_input->remote_client) {
125 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
128 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
131 *remote_handle = (remote_input_h)remote_input;
136 EXPORT_API int remote_input_destroy(remote_input_h remote_handle)
138 if (!remote_handle || !remote_handle->remote_client) {
139 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
140 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
143 if (remote_control_disconnect(remote_handle->remote_client) != REMOTE_CONTROL_ERROR_NONE) {
144 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
145 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
147 remote_handle->remote_client = NULL;
151 remote_handle = NULL;
153 return REMOTE_INPUT_ERROR_NONE;
156 EXPORT_API int remote_input_focus_in_callback_set(remote_input_h remote_handle, remote_input_focus_in_cb callback, void *user_data)
158 if (!remote_handle || !remote_handle->remote_client || !callback) {
159 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
160 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
163 remote_control_focus_in_callback_set(remote_handle->remote_client, callback, user_data);
165 return REMOTE_INPUT_ERROR_NONE;
168 EXPORT_API int remote_input_focus_in_callback_unset(remote_input_h remote_handle)
170 if (!remote_handle || !remote_handle->remote_client) {
171 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
172 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
175 remote_control_focus_in_callback_unset(remote_handle->remote_client);
177 return REMOTE_INPUT_ERROR_NONE;
180 EXPORT_API int remote_input_focus_out_callback_set(remote_input_h remote_handle, remote_input_focus_out_cb callback, void *user_data)
182 if (!remote_handle || !remote_handle->remote_client || !callback) {
183 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
184 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
187 remote_control_focus_out_callback_set(remote_handle->remote_client, callback, user_data);
189 return REMOTE_INPUT_ERROR_NONE;
192 EXPORT_API int remote_input_focus_out_callback_unset(remote_input_h remote_handle)
194 if (!remote_handle || !remote_handle->remote_client) {
195 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
196 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
199 remote_control_focus_out_callback_unset(remote_handle->remote_client);
201 return REMOTE_INPUT_ERROR_NONE;
204 EXPORT_API int remote_input_metadata_updated_callback_set(remote_input_h remote_handle, remote_input_metadata_updated_cb callback, void *user_data)
206 if (!remote_handle || !remote_handle->remote_client || !callback) {
207 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
208 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
211 remote_control_entry_metadata_callback_set(remote_handle->remote_client, _metadata_updated_cb, (void *)remote_handle);
212 remote_handle->metadata_updated_cb = callback;
213 remote_handle->metadata_updated_cb_user_data = user_data;
215 return REMOTE_INPUT_ERROR_NONE;
218 EXPORT_API int remote_input_metadata_updated_callback_unset(remote_input_h remote_handle)
220 if (!remote_handle || !remote_handle->remote_client) {
221 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
222 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
225 remote_control_entry_metadata_callback_unset(remote_handle->remote_client);
226 remote_handle->metadata_updated_cb = NULL;
227 remote_handle->metadata_updated_cb_user_data = NULL;
229 return REMOTE_INPUT_ERROR_NONE;
232 EXPORT_API int remote_input_text_updated_callback_set(remote_input_h remote_handle, remote_input_text_updated_cb callback, void *user_data)
234 if (!remote_handle || !remote_handle->remote_client || !callback) {
235 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
236 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
239 remote_control_text_updated_callback_set(remote_handle->remote_client, _text_updated_cb, (void *)remote_handle);
240 remote_handle->text_updated_cb = callback;
241 remote_handle->text_updated_cb_user_data = user_data;
243 return REMOTE_INPUT_ERROR_NONE;
246 EXPORT_API int remote_input_text_updated_callback_unset(remote_input_h remote_handle)
248 if (!remote_handle || !remote_handle->remote_client) {
249 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
250 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
253 remote_control_text_updated_callback_unset(remote_handle->remote_client);
254 remote_handle->text_updated_cb = NULL;
255 remote_handle->text_updated_cb_user_data = NULL;
257 return REMOTE_INPUT_ERROR_NONE;
260 EXPORT_API int remote_input_resource_changed_callback_set(remote_input_h remote_handle, remote_input_resource_changed_cb callback, void *user_data)
262 if (!remote_handle || !remote_handle->remote_client || !callback) {
263 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
264 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
267 remote_control_input_resource_changed_callback_set(remote_handle->remote_client, _resource_updated_cb, (void *)remote_handle);
268 remote_handle->resource_changed_cb = callback;
269 remote_handle->resource_changed_cb_user_data = user_data;
271 return REMOTE_INPUT_ERROR_NONE;
274 EXPORT_API int remote_input_resource_changed_callback_unset(remote_input_h remote_handle)
276 if (!remote_handle || !remote_handle->remote_client) {
277 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
278 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
281 remote_control_input_resource_changed_callback_unset(remote_handle->remote_client);
282 remote_handle->resource_changed_cb = NULL;
283 remote_handle->resource_changed_cb_user_data = NULL;
285 return REMOTE_INPUT_ERROR_NONE;
288 EXPORT_API int remote_input_get_input_hint(remote_input_h remote_handle, Ecore_IMF_Input_Hints *input_hint)
290 if (!remote_handle || !input_hint) {
291 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
292 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
295 *input_hint = remote_handle->hint;
297 return REMOTE_INPUT_ERROR_NONE;
300 EXPORT_API int remote_input_get_layout(remote_input_h remote_handle, Ecore_IMF_Input_Panel_Layout *layout)
302 if (!remote_handle || !layout) {
303 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
304 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
307 *layout = remote_handle->layout;
309 return REMOTE_INPUT_ERROR_NONE;
312 EXPORT_API int remote_input_get_layout_variation(remote_input_h remote_handle, int *variation)
314 if (!remote_handle || !variation) {
315 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
316 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
319 *variation = remote_handle->variation;
321 return REMOTE_INPUT_ERROR_NONE;
324 EXPORT_API int remote_input_get_autocapital_type(remote_input_h remote_handle, Ecore_IMF_Autocapital_Type *autocapital_type)
326 if (!remote_handle || !autocapital_type) {
327 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
328 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
331 *autocapital_type = remote_handle->autocapital_type;
333 return REMOTE_INPUT_ERROR_NONE;
336 EXPORT_API int remote_input_get_return_key_state(remote_input_h remote_handle, bool *return_key_state)
338 if (!remote_handle || !return_key_state) {
339 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
340 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
343 *return_key_state = remote_handle->return_key_disabled;
345 return REMOTE_INPUT_ERROR_NONE;
348 EXPORT_API int remote_input_get_return_key_type(remote_input_h remote_handle, Ecore_IMF_Input_Panel_Return_Key_Type *return_key_type)
350 if (!remote_handle || !return_key_type) {
351 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
352 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
355 *return_key_type = remote_handle->return_key_type;
357 return REMOTE_INPUT_ERROR_NONE;
360 EXPORT_API int remote_input_send_key_event(remote_input_h remote_handle, remote_input_key_type_e key)
362 if (!remote_handle || key > REMOTE_INPUT_KEY_CANCEL) {
363 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
364 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
367 if (remote_control_send_key_event(remote_handle->remote_client, (remote_control_key_type_e)key) != REMOTE_CONTROL_ERROR_NONE) {
368 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
369 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
372 return REMOTE_INPUT_ERROR_NONE;
375 EXPORT_API int remote_input_send_commit_string(remote_input_h remote_handle, const char *text)
377 if (!remote_handle || !text) {
378 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
379 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
382 if (remote_control_send_commit_string(remote_handle->remote_client, text) != REMOTE_CONTROL_ERROR_NONE) {
383 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
384 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
387 return REMOTE_INPUT_ERROR_NONE;
390 EXPORT_API int remote_input_update_preedit_string(remote_input_h remote_handle, const char *text, int cursor_pos)
392 if (!remote_handle || !text || cursor_pos < -1) {
393 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
394 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
397 if (remote_control_update_preedit_string(remote_handle->remote_client, text, NULL, cursor_pos) != REMOTE_CONTROL_ERROR_NONE) {
398 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
399 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
402 return REMOTE_INPUT_ERROR_NONE;
405 EXPORT_API int remote_input_delete_surrounding_text(remote_input_h remote_handle, int offset, int len)
407 if (!remote_handle || len <= 0) {
408 LOGE("REMOTE_INPUT_ERROR_INVALID_PARAMETER");
409 return REMOTE_INPUT_ERROR_INVALID_PARAMETER;
412 if (remote_control_delete_surrounding_text(remote_handle->remote_client, offset, len) != REMOTE_CONTROL_ERROR_NONE) {
413 LOGE("REMOTE_INPUT_ERROR_OPERATION_FAILED");
414 return REMOTE_INPUT_ERROR_OPERATION_FAILED;
417 return REMOTE_INPUT_ERROR_NONE;