2a8972055655277c668d5ddb175066a75e780da9
[platform/core/api/inputmethod.git] / remote_input / src / remote_input.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <dlog.h>
21 #include <remote_input.h>
22 #include <remote_input_private.h>
23 #include "privilege_checker_private.h"
24
25 #ifdef LOG_TAG
26 #undef LOG_TAG
27 #endif
28 #define LOG_TAG "REMOTE_INPUT"
29
30 #define REMOTE_PRIVILEGE "http://tizen.org/privilege/internal/default/platform"
31
32 static remote_input_error_e _remote_check_privilege() {
33     char uid[16];
34     remote_input_error_e ret = REMOTE_INPUT_ERROR_NONE;
35
36     if (inputmethod_cynara_initialize() == false) {
37         LOGE("cynara_initialize () == false");
38         return REMOTE_INPUT_PERMISSION_DENIED;
39     }
40
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_PERMISSION_DENIED;
45     }
46
47     inputmethod_cynara_finish();
48
49     return ret;
50 }
51
52 static void _metadata_updated_cb(void *user_data, remote_control_entry_metadata_s *data)
53 {
54     remote_input_h remote_handle = (remote_input_h)user_data;
55
56     if (remote_handle == NULL) {
57         LOGE("remote handle is not available");
58         return;
59     }
60
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;
67
68     if (remote_handle->metadata_updated_cb) {
69         remote_handle->metadata_updated_cb(remote_handle->metadata_updated_cb_user_data);
70     }
71 }
72
73 static void _text_updated_cb(void *user_data, const char *text, int cursor_pos)
74 {
75     remote_input_h remote_handle = (remote_input_h)user_data;
76
77     if (remote_handle == NULL) {
78         LOGE("remote handle is not available");
79         return;
80     }
81
82     if (remote_handle->text_updated_cb) {
83         remote_handle->text_updated_cb(text, cursor_pos, remote_handle->text_updated_cb_user_data);
84     }
85 }
86
87 static void _resource_updated_cb(void *user_data, remote_control_input_resource resource)
88 {
89     remote_input_h remote_handle = (remote_input_h)user_data;
90
91     if (remote_handle == NULL) {
92         LOGE("remote handle is not available");
93         return;
94     }
95
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);
99     }
100 }
101
102 EXPORT_API int remote_input_create(remote_input_h *remote_handle)
103 {
104     remote_input_error_e ret = REMOTE_INPUT_ERROR_NONE;
105
106     if (!remote_handle) {
107         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
108         return REMOTE_INPUT_INVALID_PARAMETER;
109     }
110
111     ret = _remote_check_privilege();
112     if (ret != REMOTE_INPUT_ERROR_NONE) {
113         LOGE("REMOTE_INPUT_PERMISSION_DENIED");
114         return ret;
115     }
116
117     struct remote_input_s *remote_input = (remote_input_h)calloc(1, sizeof(struct remote_input_s));
118     if (!remote_input) {
119         LOGE("REMOTE_INPUT_OUT_OF_MEMORY");
120         return REMOTE_INPUT_OUT_OF_MEMORY;
121     }
122
123     remote_input->remote_client = remote_control_connect();
124     if (!remote_input->remote_client) {
125         LOGE("REMOTE_INPUT_OPERATION_FAILED");
126         free(remote_input);
127         remote_input = NULL;
128         return REMOTE_INPUT_OPERATION_FAILED;
129     }
130
131     *remote_handle = (remote_input_h)remote_input;
132
133     return ret;
134 }
135
136 EXPORT_API int remote_input_destroy(remote_input_h remote_handle)
137 {
138     if (!remote_handle || !remote_handle->remote_client) {
139         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
140         return REMOTE_INPUT_INVALID_PARAMETER;
141     }
142
143     if (remote_control_disconnect(remote_handle->remote_client) != REMOTE_CONTROL_ERROR_NONE) {
144         LOGE("REMOTE_INPUT_OPERATION_FAILED");
145         return REMOTE_INPUT_OPERATION_FAILED;
146     } else {
147         remote_handle->remote_client = NULL;
148     }
149
150     free(remote_handle);
151     remote_handle = NULL;
152
153     return REMOTE_INPUT_ERROR_NONE;
154 }
155
156 EXPORT_API int remote_input_focus_in_callback_set(remote_input_h remote_handle, remote_input_focus_in_cb callback, void *user_data)
157 {
158     if (!remote_handle || !remote_handle->remote_client || !callback) {
159         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
160         return REMOTE_INPUT_INVALID_PARAMETER;
161     }
162
163     remote_control_focus_in_callback_set(remote_handle->remote_client, callback, user_data);
164
165     return REMOTE_INPUT_ERROR_NONE;
166 }
167
168 EXPORT_API int remote_input_focus_in_callback_unset(remote_input_h remote_handle)
169 {
170     if (!remote_handle || !remote_handle->remote_client) {
171         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
172         return REMOTE_INPUT_INVALID_PARAMETER;
173     }
174
175     remote_control_focus_in_callback_unset(remote_handle->remote_client);
176
177     return REMOTE_INPUT_ERROR_NONE;
178 }
179
180 EXPORT_API int remote_input_focus_out_callback_set(remote_input_h remote_handle, remote_input_focus_out_cb callback, void *user_data)
181 {
182     if (!remote_handle || !remote_handle->remote_client || !callback) {
183         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
184         return REMOTE_INPUT_INVALID_PARAMETER;
185     }
186
187     remote_control_focus_out_callback_set(remote_handle->remote_client, callback, user_data);
188
189     return REMOTE_INPUT_ERROR_NONE;
190 }
191
192 EXPORT_API int remote_input_focus_out_callback_unset(remote_input_h remote_handle)
193 {
194     if (!remote_handle || !remote_handle->remote_client) {
195         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
196         return REMOTE_INPUT_INVALID_PARAMETER;
197     }
198
199     remote_control_focus_out_callback_unset(remote_handle->remote_client);
200
201     return REMOTE_INPUT_ERROR_NONE;
202 }
203
204 EXPORT_API int remote_input_metadata_updated_callback_set(remote_input_h remote_handle, remote_input_metadata_updated_cb callback, void *user_data)
205 {
206     if (!remote_handle || !remote_handle->remote_client || !callback) {
207         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
208         return REMOTE_INPUT_INVALID_PARAMETER;
209     }
210
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;
214
215     return REMOTE_INPUT_ERROR_NONE;
216 }
217
218 EXPORT_API int remote_input_metadata_updated_callback_unset(remote_input_h remote_handle)
219 {
220     if (!remote_handle || !remote_handle->remote_client) {
221         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
222         return REMOTE_INPUT_INVALID_PARAMETER;
223     }
224
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;
228
229     return REMOTE_INPUT_ERROR_NONE;
230 }
231
232 EXPORT_API int remote_input_text_updated_callback_set(remote_input_h remote_handle, remote_input_text_updated_cb callback, void *user_data)
233 {
234     if (!remote_handle || !remote_handle->remote_client || !callback) {
235         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
236         return REMOTE_INPUT_INVALID_PARAMETER;
237     }
238
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;
242
243     return REMOTE_INPUT_ERROR_NONE;
244 }
245
246 EXPORT_API int remote_input_text_updated_callback_unset(remote_input_h remote_handle)
247 {
248     if (!remote_handle || !remote_handle->remote_client) {
249         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
250         return REMOTE_INPUT_INVALID_PARAMETER;
251     }
252
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;
256
257     return REMOTE_INPUT_ERROR_NONE;
258 }
259
260 EXPORT_API int remote_input_resource_changed_callback_set(remote_input_h remote_handle, remote_input_resource_changed_cb callback, void *user_data)
261 {
262     if (!remote_handle || !remote_handle->remote_client || !callback) {
263         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
264         return REMOTE_INPUT_INVALID_PARAMETER;
265     }
266
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;
270
271     return REMOTE_INPUT_ERROR_NONE;
272 }
273
274 EXPORT_API int remote_input_resource_changed_callback_unset(remote_input_h remote_handle)
275 {
276     if (!remote_handle || !remote_handle->remote_client) {
277         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
278         return REMOTE_INPUT_INVALID_PARAMETER;
279     }
280
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;
284
285     return REMOTE_INPUT_ERROR_NONE;
286 }
287
288 EXPORT_API int remote_input_get_input_hint(remote_input_h remote_handle, Ecore_IMF_Input_Hints *input_hint)
289 {
290     if (!remote_handle || !input_hint) {
291         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
292         return REMOTE_INPUT_INVALID_PARAMETER;
293     }
294
295     *input_hint = remote_handle->hint;
296
297     return REMOTE_INPUT_ERROR_NONE;
298 }
299
300 EXPORT_API int remote_input_get_layout(remote_input_h remote_handle, Ecore_IMF_Input_Panel_Layout *layout)
301 {
302     if (!remote_handle || !layout) {
303         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
304         return REMOTE_INPUT_INVALID_PARAMETER;
305     }
306
307     *layout = remote_handle->layout;
308
309     return REMOTE_INPUT_ERROR_NONE;
310 }
311
312 EXPORT_API int remote_input_get_layout_variation(remote_input_h remote_handle, int *variation)
313 {
314     if (!remote_handle || !variation) {
315         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
316         return REMOTE_INPUT_INVALID_PARAMETER;
317     }
318
319     *variation = remote_handle->variation;
320
321     return REMOTE_INPUT_ERROR_NONE;
322 }
323
324 EXPORT_API int remote_input_get_autocapital_type(remote_input_h remote_handle, Ecore_IMF_Autocapital_Type *autocapital_type)
325 {
326     if (!remote_handle || !autocapital_type) {
327         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
328         return REMOTE_INPUT_INVALID_PARAMETER;
329     }
330
331     *autocapital_type = remote_handle->autocapital_type;
332
333     return REMOTE_INPUT_ERROR_NONE;
334 }
335
336 EXPORT_API int remote_input_get_return_key_state(remote_input_h remote_handle, bool *return_key_state)
337 {
338     if (!remote_handle || !return_key_state) {
339         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
340         return REMOTE_INPUT_INVALID_PARAMETER;
341     }
342
343     *return_key_state = remote_handle->return_key_disabled;
344
345     return REMOTE_INPUT_ERROR_NONE;
346 }
347
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)
349 {
350     if (!remote_handle || !return_key_type) {
351         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
352         return REMOTE_INPUT_INVALID_PARAMETER;
353     }
354
355     *return_key_type = remote_handle->return_key_type;
356
357     return REMOTE_INPUT_ERROR_NONE;
358 }
359
360 EXPORT_API int remote_input_send_key_event(remote_input_h remote_handle, remote_input_key_type_e key)
361 {
362     if (!remote_handle || key > REMOTE_INPUT_KEY_CANCEL) {
363         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
364         return REMOTE_INPUT_INVALID_PARAMETER;
365     }
366
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_OPERATION_FAILED");
369         return REMOTE_INPUT_OPERATION_FAILED;
370     }
371
372     return REMOTE_INPUT_ERROR_NONE;
373 }
374
375 EXPORT_API int remote_input_send_commit_string(remote_input_h remote_handle, const char *text)
376 {
377     if (!remote_handle || !text) {
378         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
379         return REMOTE_INPUT_INVALID_PARAMETER;
380     }
381
382     if (remote_control_send_commit_string(remote_handle->remote_client, text) != REMOTE_CONTROL_ERROR_NONE) {
383         LOGE("REMOTE_INPUT_OPERATION_FAILED");
384         return REMOTE_INPUT_OPERATION_FAILED;
385     }
386
387     return REMOTE_INPUT_ERROR_NONE;
388 }
389
390 EXPORT_API int remote_input_update_preedit_string(remote_input_h remote_handle, const char *text, int cursor_pos)
391 {
392     if (!remote_handle || !text || cursor_pos < -1) {
393         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
394         return REMOTE_INPUT_INVALID_PARAMETER;
395     }
396
397     if (remote_control_update_preedit_string(remote_handle->remote_client, text, NULL, cursor_pos) != REMOTE_CONTROL_ERROR_NONE) {
398         LOGE("REMOTE_INPUT_OPERATION_FAILED");
399         return REMOTE_INPUT_OPERATION_FAILED;
400     }
401
402     return REMOTE_INPUT_ERROR_NONE;
403 }
404
405 EXPORT_API int remote_input_delete_surrounding_text(remote_input_h remote_handle, int offset, int len)
406 {
407     if (!remote_handle || len <= 0) {
408         LOGE("REMOTE_INPUT_INVALID_PARAMETER");
409         return REMOTE_INPUT_INVALID_PARAMETER;
410     }
411
412     if (remote_control_delete_surrounding_text(remote_handle->remote_client, offset, len) != REMOTE_CONTROL_ERROR_NONE) {
413         LOGE("REMOTE_INPUT_OPERATION_FAILED");
414         return REMOTE_INPUT_OPERATION_FAILED;
415     }
416
417     return REMOTE_INPUT_ERROR_NONE;
418 }