Fix build error due to toolchain upgrade
[platform/core/uifw/isf.git] / ism / src / isf_remote_control.cpp
1 #define Uses_SCIM_TRANSACTION
2 #define Uses_ISF_REMOTE_CLIENT
3 #define Uses_STL_STRING
4
5 #include <glib.h>
6 #include <dlog.h>
7
8 #include "scim.h"
9 #include "isf_remote_control.h"
10 #include "isf_debug.h"
11
12 #ifdef LOG_TAG
13 # undef LOG_TAG
14 #endif
15 #define LOG_TAG             "ISF_REMOTE_CONTROL"
16
17 using namespace scim;
18
19 static bool focus_flag;
20
21 struct _remote_control_client {
22     RemoteInputClient *remote_client;
23     GIOChannel *remote_client_iochannel;
24     guint remote_client_iochannel_read;
25     guint remote_client_iochannel_err;
26     guint remote_client_iochannel_hup;
27     int remote_client_id;
28     remote_control_focus_in_cb focus_in_cb;
29     void* focus_in_cb_user_data;
30     remote_control_focus_out_cb focus_out_cb;
31     void* focus_out_cb_user_data;
32     remote_control_entry_metadata_cb metadata_cb;
33     void* metadata_cb_user_data;
34     remote_control_text_updated_cb text_updated_cb;
35     void* text_updated_cb_user_data;
36     remote_control_input_resource_changed_cb input_resource_changed_cb;
37     void* input_resource_changed_cb_user_data;
38
39     _remote_control_client() : remote_client(NULL),
40                                remote_client_iochannel(NULL),
41                                remote_client_iochannel_read(0),
42                                remote_client_iochannel_err(0),
43                                remote_client_iochannel_hup(0),
44                                remote_client_id(0),
45                                focus_in_cb(NULL),
46                                focus_in_cb_user_data(NULL),
47                                focus_out_cb(NULL),
48                                focus_out_cb_user_data(NULL),
49                                metadata_cb(NULL),
50                                metadata_cb_user_data(NULL),
51                                text_updated_cb(NULL),
52                                text_updated_cb_user_data(NULL),
53                                input_resource_changed_cb(NULL),
54                                input_resource_changed_cb_user_data(NULL)
55     {
56     }
57 };
58
59 static gboolean
60 remote_handler(GIOChannel *source, GIOCondition condition, gpointer user_data)
61 {
62     if (condition == G_IO_IN) {
63         remote_control_client *client = static_cast<remote_control_client*>(user_data);
64         if (client->remote_client->has_pending_event()) {
65             switch (client->remote_client->recv_callback_message()) {
66                 case REMOTE_CONTROL_CALLBACK_FOCUS_IN:
67                 {
68                     LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_IN");
69                     focus_flag = true;
70
71                     if (client->focus_in_cb)
72                         client->focus_in_cb (client->focus_in_cb_user_data);
73                     break;
74                 }
75                 case REMOTE_CONTROL_CALLBACK_FOCUS_OUT:
76                 {
77                     LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_OUT");
78                     focus_flag = false;
79
80                     if (client->focus_out_cb)
81                         client->focus_out_cb (client->focus_out_cb_user_data);
82                     break;
83                 }
84                 case REMOTE_CONTROL_CALLBACK_ENTRY_METADATA:
85                 {
86                     if (focus_flag) {
87                         remote_control_entry_metadata_s *data = new remote_control_entry_metadata_s;
88                         int hint = 0, layout = 0, variation = 0, autocapital_type = 0, return_key_disabled = 0, return_key_type = 0;
89
90                         client->remote_client->get_entry_metadata (&hint, &layout, &variation, &autocapital_type, &return_key_disabled, &return_key_type);
91                         data->hint = static_cast<Ecore_IMF_Input_Hints> (hint);
92                         data->layout = static_cast<Ecore_IMF_Input_Panel_Layout> (layout);
93                         data->variation = variation;
94                         data->autocapital_type = static_cast<Ecore_IMF_Autocapital_Type> (autocapital_type);
95                         data->return_key_disabled = return_key_disabled;
96                         data->return_key_type = static_cast<Ecore_IMF_Input_Panel_Return_Key_Type> (return_key_type);
97
98                         LOGD ("REMOTE_CONTROL_CALLBACK_ENTRY_METADATA: hint=0x%04x, layout=%d, variation=%d, autocap=%d, retKey_disabled=%d, retKey_type=%d",
99                             data->hint, data->layout, data->variation, data->autocapital_type, data->return_key_disabled, data->return_key_type);
100
101                         if (client->metadata_cb)
102                             client->metadata_cb (client->metadata_cb_user_data, data);
103                         delete data;
104                     }
105                     break;
106                 }
107                 case REMOTE_CONTROL_CALLBACK_TEXT_UPDATED:
108                 {
109                     if (focus_flag) {
110                         String surrounding_text;
111                         int cursor = -1;
112
113                         client->remote_client->get_surrounding_text (surrounding_text, &cursor);
114                         SECURE_LOGD ("REMOTE_CONTROL_CALLBACK_TEXT_UPDATED: %d \"%s\"", cursor, surrounding_text.c_str ());
115
116                         if (client->text_updated_cb)
117                             client->text_updated_cb (client->text_updated_cb_user_data, surrounding_text.c_str (), cursor);
118                     }
119                     break;
120                 }
121                 case REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE:
122                 {
123                     if (focus_flag) {
124                         int resource = 0;
125
126                         client->remote_client->get_input_resource (&resource);
127                         LOGD ("REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE: %s", (resource ? "REMOTE" : "LOCAL"));
128
129                         if (client->input_resource_changed_cb)
130                             client->input_resource_changed_cb (client->input_resource_changed_cb_user_data,
131                                 static_cast<remote_control_input_resource> (resource));
132                     }
133                     break;
134                 }
135                 case REMOTE_CONTROL_CALLBACK_ERROR:
136                     LOGE ("REMOTE_CONTROL_CALLBACK_ERROR");
137                     break;
138                 default:
139                     break;
140             }
141         } else {
142             LOGE ("Failed to receive callback message");
143             return (gboolean)FALSE;
144         }
145     } else if (condition == G_IO_ERR || condition == G_IO_HUP) {
146         LOGE ("Failed to receive callback message");
147         return (gboolean)FALSE;
148     }
149
150     return (gboolean)TRUE;
151 }
152
153 EXAPI remote_control_client * remote_control_connect(void)
154 {
155     remote_control_client *client = new remote_control_client;
156     focus_flag = false;
157
158     if (client) {
159         client->remote_client = new RemoteInputClient;
160
161         if (!client->remote_client) {
162             LOGE ("Failed to generate remote_client");
163             goto cleanup;
164         }
165
166         if (!client->remote_client->open_connection()) {
167             LOGE ("REMOTE_CONTROL_REPLY_TIMEOUT");
168             goto cleanup;
169         }
170
171         remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
172         if (error_e) {
173             LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
174             goto cleanup;
175         }
176
177         client->remote_client_id = client->remote_client->get_panel2remote_connection_number();
178
179         if (client->remote_client_id >= 0) {
180             client->remote_client_iochannel = g_io_channel_unix_new(client->remote_client_id);
181
182             if (client->remote_client_iochannel == NULL) {
183                 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
184                 goto cleanup;
185             }
186
187             client->remote_client_iochannel_read = g_io_add_watch (client->remote_client_iochannel, G_IO_IN,  remote_handler, client);
188             client->remote_client_iochannel_err = g_io_add_watch (client->remote_client_iochannel, G_IO_ERR,  remote_handler, client);
189             client->remote_client_iochannel_hup = g_io_add_watch (client->remote_client_iochannel, G_IO_HUP,  remote_handler, client);
190         }
191         else {
192             LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
193             goto cleanup;
194         }
195     }
196     else {
197         LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
198         goto cleanup;
199     }
200
201     LOGD ("%p", client);
202     return client;
203
204 cleanup:
205     if (client && client->remote_client)
206         delete client->remote_client;
207
208     if (client)
209         delete client;
210
211     return NULL;
212 }
213
214 EXAPI int remote_control_disconnect(remote_control_client *client)
215 {
216     LOGD("%p", client);
217
218     if (client == NULL) {
219         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
220         return REMOTE_CONTROL_INVALID_PARAMETER;
221     }
222
223     if (client->remote_client) {
224         remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege ();
225         if (error_e) {
226             LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
227             return error_e;
228         }
229
230         if (client->remote_client_iochannel)
231             g_io_channel_unref (client->remote_client_iochannel);
232
233         if (client->remote_client_iochannel_read)
234             g_source_remove (client->remote_client_iochannel_read);
235
236         if (client->remote_client_iochannel_err)
237             g_source_remove (client->remote_client_iochannel_err);
238
239         if (client->remote_client_iochannel_hup)
240             g_source_remove (client->remote_client_iochannel_hup);
241
242         client->remote_client_iochannel = NULL;
243         client->remote_client_iochannel_read = 0;
244         client->remote_client_iochannel_err = 0;
245         client->remote_client_iochannel_hup = 0;
246
247         client->remote_client->close_connection();
248
249         delete client->remote_client;
250     }
251
252     delete client;
253     client = NULL;
254
255     return REMOTE_CONTROL_ERROR_NONE;
256 }
257
258 EXAPI int remote_control_focus_in_callback_set(remote_control_client *client, remote_control_focus_in_cb func, void *user_data)
259 {
260     if (client == NULL || func == NULL) {
261         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
262         return REMOTE_CONTROL_INVALID_PARAMETER;
263     }
264
265     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
266     if (error_e) {
267         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
268         return error_e;
269     }
270
271     client->focus_in_cb = func;
272     client->focus_in_cb_user_data = user_data;
273
274     LOGD ("%p", client);
275     return REMOTE_CONTROL_ERROR_NONE;
276 }
277
278 EXAPI int remote_control_focus_in_callback_unset(remote_control_client *client)
279 {
280     if (client == NULL) {
281         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
282         return REMOTE_CONTROL_INVALID_PARAMETER;
283     }
284
285     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
286     if (error_e) {
287         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
288         return error_e;
289     }
290
291     client->focus_in_cb = NULL;
292     client->focus_in_cb_user_data = NULL;
293
294     LOGD ("%p", client);
295     return REMOTE_CONTROL_ERROR_NONE;
296 }
297
298 EXAPI int remote_control_focus_out_callback_set(remote_control_client *client, remote_control_focus_out_cb func , void *user_data)
299 {
300     if (client == NULL || func == NULL) {
301         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
302         return REMOTE_CONTROL_INVALID_PARAMETER;
303     }
304
305     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
306     if (error_e) {
307         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
308         return error_e;
309     }
310
311     client->focus_out_cb = func;
312     client->focus_out_cb_user_data = user_data;
313
314     LOGD ("%p", client);
315     return REMOTE_CONTROL_ERROR_NONE;
316 }
317
318 EXAPI int remote_control_focus_out_callback_unset(remote_control_client *client)
319 {
320     if (client == NULL) {
321         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
322         return REMOTE_CONTROL_INVALID_PARAMETER;
323     }
324
325     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
326     if (error_e) {
327         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
328         return error_e;
329     }
330
331     client->focus_out_cb = NULL;
332     client->focus_out_cb_user_data = NULL;
333
334     LOGD ("%p", client);
335     return REMOTE_CONTROL_ERROR_NONE;
336 }
337
338 EXAPI int remote_control_entry_metadata_callback_set(remote_control_client *client, remote_control_entry_metadata_cb func, void *user_data)
339 {
340     if (client == NULL || func == NULL) {
341         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
342         return REMOTE_CONTROL_INVALID_PARAMETER;
343     }
344
345     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
346     if (error_e) {
347         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
348         return error_e;
349     }
350
351     client->metadata_cb = func;
352     client->metadata_cb_user_data = user_data;
353
354     LOGD ("%p", client);
355     return REMOTE_CONTROL_ERROR_NONE;
356 }
357
358 EXAPI int remote_control_entry_metadata_callback_unset(remote_control_client *client)
359 {
360     if (client == NULL) {
361         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
362         return REMOTE_CONTROL_INVALID_PARAMETER;
363     }
364
365     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
366     if (error_e) {
367         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
368         return error_e;
369     }
370
371     client->metadata_cb = NULL;
372     client->metadata_cb_user_data = NULL;
373
374     LOGD ("%p", client);
375     return REMOTE_CONTROL_ERROR_NONE;
376 }
377
378 EXAPI int remote_control_text_updated_callback_set(remote_control_client *client, remote_control_text_updated_cb func, void *user_data)
379 {
380     if (client == NULL || func == NULL) {
381         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
382         return REMOTE_CONTROL_INVALID_PARAMETER;
383     }
384
385     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
386     if (error_e) {
387         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
388         return error_e;
389     }
390
391     client->text_updated_cb = func;
392     client->text_updated_cb_user_data = user_data;
393
394     LOGD ("%p", client);
395     return REMOTE_CONTROL_ERROR_NONE;
396 }
397
398 EXAPI int remote_control_text_updated_callback_unset(remote_control_client *client)
399 {
400     if (client == NULL) {
401         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
402         return REMOTE_CONTROL_INVALID_PARAMETER;
403     }
404
405     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
406     if (error_e) {
407         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
408         return error_e;
409     }
410
411     client->text_updated_cb = NULL;
412     client->text_updated_cb_user_data = NULL;
413
414     LOGD ("%p", client);
415     return REMOTE_CONTROL_ERROR_NONE;
416 }
417
418 EXAPI int remote_control_input_resource_changed_callback_set(remote_control_client *client, remote_control_input_resource_changed_cb func , void *user_data)
419 {
420     if (client == NULL || func == NULL) {
421         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
422         return REMOTE_CONTROL_INVALID_PARAMETER;
423     }
424
425     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
426     if (error_e) {
427         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
428         return error_e;
429     }
430
431     client->input_resource_changed_cb = func;
432     client->input_resource_changed_cb_user_data = user_data;
433
434     LOGD ("%p", client);
435     return REMOTE_CONTROL_ERROR_NONE;
436 }
437
438 EXAPI int remote_control_input_resource_changed_callback_unset(remote_control_client *client)
439 {
440     if (client == NULL) {
441         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
442         return REMOTE_CONTROL_INVALID_PARAMETER;
443     }
444
445     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
446     if (error_e) {
447         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
448         return error_e;
449     }
450
451     client->input_resource_changed_cb = NULL;
452     client->input_resource_changed_cb_user_data = NULL;
453
454     LOGD ("%p", client);
455     return REMOTE_CONTROL_ERROR_NONE;
456 }
457
458 EXAPI int remote_control_send_key_event(remote_control_client *client, remote_control_key_type_e key)
459 {
460     if (client == NULL || key > REMOTE_CONTROL_KEY_CANCEL) {
461         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
462         return REMOTE_CONTROL_INVALID_PARAMETER;
463     }
464
465     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
466     if (error_e) {
467         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
468         return error_e;
469     }
470
471     char key_str[12] = {};
472     snprintf(key_str, sizeof(key_str), "%d", key);
473     String command = String ("|plain|send_key_event|") + String (key_str) + String ("|");
474
475     if (focus_flag) {
476         error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
477
478         if (!error_e)
479             SECURE_LOGD ("%p, key=%d", client, key);
480
481         return error_e;
482     }
483
484     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
485     return REMOTE_CONTROL_INVALID_OPERATION;
486 }
487
488 EXAPI int remote_control_send_commit_string(remote_control_client *client, const char *text)
489 {
490     if (client == NULL || !text) {
491         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
492         return REMOTE_CONTROL_INVALID_PARAMETER;
493     }
494
495     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
496     if (error_e) {
497         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
498         return error_e;
499     }
500
501     String command = String ("|plain|commit_string|") + String (text) + String ("|");
502
503     if (focus_flag) {
504         error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
505
506         if (!error_e)
507             SECURE_LOGD ("%p, \"%s\"", client, text);
508
509         return error_e;
510     }
511
512     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
513     return REMOTE_CONTROL_INVALID_OPERATION;
514 }
515
516 EXAPI int remote_control_update_preedit_string(remote_control_client *client, const char *text, Eina_List *attrs, int cursor_pos)
517 {
518     if (client == NULL) {
519         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
520         return REMOTE_CONTROL_INVALID_PARAMETER;
521     }
522
523     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
524     if (error_e) {
525         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
526         return error_e;
527     }
528
529     char cursor_position[10] = {};
530     snprintf(cursor_position, sizeof(cursor_position), "%d", cursor_pos);
531     String command = String ("|plain|update_preedit_string|") + String (text) + String ("|") + String (cursor_position) + String ("|");
532
533     if (focus_flag) {
534         error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
535
536         if (!error_e)
537             SECURE_LOGD ("%p, %d, \"%s\"", client, cursor_pos, text);
538
539         return error_e;
540     }
541
542     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
543     return REMOTE_CONTROL_INVALID_OPERATION;
544 }
545
546 EXAPI int remote_control_delete_surrounding_text(remote_control_client *client, int offset, int len)
547 {
548     if (client == NULL || len < 0) {
549         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
550         return REMOTE_CONTROL_INVALID_PARAMETER;
551     }
552
553     remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
554     if (error_e) {
555         LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
556         return error_e;
557     }
558
559     if (focus_flag) {
560         error_e = (remote_control_error_e)client->remote_client->delete_surrounding_text(offset, len);
561
562         if (!error_e)
563             LOGD ("%p, (%d, %d)", client, offset, len);
564
565         return error_e;
566     }
567
568     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
569     return REMOTE_CONTROL_INVALID_OPERATION;
570 }