Merge branch 'tizen_3.0' into tizen
[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 <Ecore.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     Ecore_Fd_Handler *remote_fd_handler = NULL;
24     int remote_client_id;
25     remote_control_focus_in_cb focus_in_cb;
26     void* focus_in_cb_user_data;
27     remote_control_focus_out_cb focus_out_cb;
28     void* focus_out_cb_user_data;
29     remote_control_entry_metadata_cb metadata_cb;
30     void* metadata_cb_user_data;
31     remote_control_text_updated_cb text_updated_cb;
32     void* text_updated_cb_user_data;
33     remote_control_input_resource_changed_cb input_resource_changed_cb;
34     void* input_resource_changed_cb_user_data;
35 };
36
37 static Eina_Bool
38 remote_handler(void *data, Ecore_Fd_Handler *fd_handler)
39 {
40     remote_control_client *client = static_cast<remote_control_client*>(data);
41
42     if (client->remote_client.has_pending_event()) {
43         switch (client->remote_client.recv_callback_message()) {
44             case REMOTE_CONTROL_CALLBACK_FOCUS_IN:
45             {
46                 LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_IN");
47                 focus_flag = true;
48                 client->focus_in_cb (client->focus_in_cb_user_data);
49                 break;
50             }
51             case REMOTE_CONTROL_CALLBACK_FOCUS_OUT:
52             {
53                 LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_OUT");
54                 focus_flag = false;
55                 client->focus_out_cb (client->focus_out_cb_user_data);
56                 break;
57             }
58             case REMOTE_CONTROL_CALLBACK_ENTRY_METADATA:
59             {
60                 if (focus_flag) {
61                     remote_control_entry_metadata_s *data = new remote_control_entry_metadata_s;
62                     int hint = 0, layout = 0, variation = 0, autocapital_type = 0, return_key_disabled = 0;
63
64                     client->remote_client.get_entry_metadata (&hint, &layout, &variation, &autocapital_type, &return_key_disabled);
65                     data->hint = static_cast<Ecore_IMF_Input_Hints> (hint);
66                     data->layout = static_cast<Ecore_IMF_Input_Panel_Layout> (layout);
67                     data->variation = variation;
68                     data->autocapital_type = static_cast<Ecore_IMF_Autocapital_Type> (autocapital_type);
69                     data->return_key_disabled = return_key_disabled;
70
71                     LOGD ("REMOTE_CONTROL_CALLBACK_ENTRY_METADATA hint: 0x%04x, layout: %d, variation: %d, autocapital: %d, return_key: %d",
72                         data->hint, data->layout, data->variation, data->autocapital_type, data->return_key_disabled);
73                     client->metadata_cb (client->metadata_cb_user_data, data);
74                     delete data;
75                 }
76                 break;
77             }
78             case REMOTE_CONTROL_CALLBACK_TEXT_UPDATED:
79             {
80                 if (focus_flag) {
81                     String surrounding_text;
82                     int cursor = -1;
83
84                     client->remote_client.get_surrounding_text (surrounding_text, &cursor);
85                     SECURE_LOGD ("REMOTE_CONTROL_CALLBACK_TEXT_UPDATED \"%s\" %d", surrounding_text.c_str (), cursor);
86                     client->text_updated_cb (client->text_updated_cb_user_data, surrounding_text.c_str (), cursor);
87                 }
88                 break;
89             }
90             case REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE:
91             {
92                 if (focus_flag) {
93                     int resource = 0;
94
95                     client->remote_client.get_input_resource (&resource);
96                     LOGD ("REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE: %s", (resource ? "REMOTE" : "LOCAL"));
97                     client->input_resource_changed_cb (client->input_resource_changed_cb_user_data,
98                         static_cast<remote_control_input_resource> (resource));
99                 }
100                 break;
101             }
102             case REMOTE_CONTROL_CALLBACK_ERROR:
103                 break;
104             default:
105                 break;
106         }
107     }
108     return ECORE_CALLBACK_RENEW;
109 }
110
111 EXAPI remote_control_client * remote_control_connect(void)
112 {
113     remote_control_client *client = new remote_control_client;
114     focus_flag = false;
115
116     if (client) {
117         RemoteInputClient *remote_client = new RemoteInputClient;
118         client->remote_client = *remote_client;
119         if (!client->remote_client.open_connection()) {
120             LOGE ("REMOTE_CONTROL_REPLY_TIMEOUT");
121             goto cleanup;
122         }
123
124         remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
125         if (error_e)
126             goto cleanup;
127
128         client->remote_client_id = client->remote_client.get_panel2remote_connection_number();
129
130         if (client->remote_client_id >= 0) {
131             client->remote_fd_handler = ecore_main_fd_handler_add(client->remote_client_id, ECORE_FD_READ, remote_handler, client, NULL, NULL);
132
133             if (client->remote_fd_handler == NULL) {
134                 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
135                 goto cleanup;
136             }
137         }
138         else {
139             LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
140             goto cleanup;
141         }
142     }
143     else {
144         LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
145         goto cleanup;
146     }
147
148     LOGD ("%p", client);
149     return client;
150 cleanup:
151     if (client)
152         delete client;
153
154     return NULL;
155 }
156
157 EXAPI int remote_control_disconnect(remote_control_client *client)
158 {
159     if (client == NULL) {
160         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
161         return REMOTE_CONTROL_INVALID_PARAMETER;
162     }
163
164     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
165     if (error_e)
166         return error_e;
167
168     if (client->remote_fd_handler)
169         ecore_main_fd_handler_del(client->remote_fd_handler);
170
171     client->remote_client.close_connection();
172
173     LOGD ("%p", client);
174     delete client;
175     client = NULL;
176
177     return REMOTE_CONTROL_ERROR_NONE;
178 }
179
180 EXAPI int remote_control_focus_in_callback_set(remote_control_client *client, remote_control_focus_in_cb func, void *user_data)
181 {
182     if (client == NULL || func == NULL) {
183         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
184         return REMOTE_CONTROL_INVALID_PARAMETER;
185     }
186
187     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
188     if (error_e)
189         return error_e;
190
191     client->focus_in_cb = func;
192     client->focus_in_cb_user_data = user_data;
193
194     LOGD ("%p", client);
195     return REMOTE_CONTROL_ERROR_NONE;
196 }
197
198 EXAPI int remote_control_focus_in_callback_unset(remote_control_client *client)
199 {
200     if (client == NULL) {
201         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
202         return REMOTE_CONTROL_INVALID_PARAMETER;
203     }
204
205     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
206     if (error_e)
207         return error_e;
208
209     client->focus_in_cb = NULL;
210     client->focus_in_cb_user_data = NULL;
211
212     LOGD ("%p", client);
213     return REMOTE_CONTROL_ERROR_NONE;
214 }
215
216 EXAPI int remote_control_focus_out_callback_set(remote_control_client *client, remote_control_focus_out_cb func , void *user_data)
217 {
218     if (client == NULL || func == NULL) {
219         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
220         return REMOTE_CONTROL_INVALID_PARAMETER;
221     }
222
223     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
224     if (error_e)
225         return error_e;
226
227     client->focus_out_cb = func;
228     client->focus_out_cb_user_data = user_data;
229
230     LOGD ("%p", client);
231     return REMOTE_CONTROL_ERROR_NONE;
232 }
233
234 EXAPI int remote_control_focus_out_callback_unset(remote_control_client *client)
235 {
236     if (client == NULL) {
237         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
238         return REMOTE_CONTROL_INVALID_PARAMETER;
239     }
240
241     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
242     if (error_e)
243         return error_e;
244
245     client->focus_out_cb = NULL;
246     client->focus_out_cb_user_data = NULL;
247
248     LOGD ("%p", client);
249     return REMOTE_CONTROL_ERROR_NONE;
250 }
251
252 EXAPI int remote_control_entry_metadata_callback_set(remote_control_client *client, remote_control_entry_metadata_cb func, void *user_data)
253 {
254     if (client == NULL || func == NULL) {
255         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
256         return REMOTE_CONTROL_INVALID_PARAMETER;
257     }
258
259     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
260     if (error_e)
261         return error_e;
262
263     client->metadata_cb = func;
264     client->metadata_cb_user_data = user_data;
265
266     LOGD ("%p", client);
267     return REMOTE_CONTROL_ERROR_NONE;
268 }
269
270 EXAPI int remote_control_entry_metadata_callback_unset(remote_control_client *client)
271 {
272     if (client == NULL) {
273         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
274         return REMOTE_CONTROL_INVALID_PARAMETER;
275     }
276
277     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
278     if (error_e)
279         return error_e;
280
281     client->metadata_cb = NULL;
282     client->metadata_cb_user_data = NULL;
283
284     LOGD ("%p", client);
285     return REMOTE_CONTROL_ERROR_NONE;
286 }
287
288 EXAPI int remote_control_text_updated_callback_set(remote_control_client *client, remote_control_text_updated_cb func, void *user_data)
289 {
290     if (client == NULL || func == NULL) {
291         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
292         return REMOTE_CONTROL_INVALID_PARAMETER;
293     }
294
295     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
296     if (error_e)
297         return error_e;
298
299     client->text_updated_cb = func;
300     client->text_updated_cb_user_data = user_data;
301
302     LOGD ("%p", client);
303     return REMOTE_CONTROL_ERROR_NONE;
304 }
305
306 EXAPI int remote_control_text_updated_callback_unset(remote_control_client *client)
307 {
308     if (client == NULL) {
309         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
310         return REMOTE_CONTROL_INVALID_PARAMETER;
311     }
312
313     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
314     if (error_e)
315         return error_e;
316
317     client->text_updated_cb = NULL;
318     client->text_updated_cb_user_data = NULL;
319
320     LOGD ("%p", client);
321     return REMOTE_CONTROL_ERROR_NONE;
322 }
323
324 EXAPI int remote_control_input_resource_changed_callback_set(remote_control_client *client, remote_control_input_resource_changed_cb func , void *user_data)
325 {
326     if (client == NULL || func == NULL) {
327         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
328         return REMOTE_CONTROL_INVALID_PARAMETER;
329     }
330
331     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
332     if (error_e)
333         return error_e;
334
335     client->input_resource_changed_cb = func;
336     client->input_resource_changed_cb_user_data = user_data;
337
338     LOGD ("%p", client);
339     return REMOTE_CONTROL_ERROR_NONE;
340 }
341
342 EXAPI int remote_control_input_resource_changed_callback_unset(remote_control_client *client)
343 {
344     if (client == NULL) {
345         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
346         return REMOTE_CONTROL_INVALID_PARAMETER;
347     }
348
349     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
350     if (error_e)
351         return error_e;
352
353     client->input_resource_changed_cb = NULL;
354     client->input_resource_changed_cb_user_data = NULL;
355
356     LOGD ("%p", client);
357     return REMOTE_CONTROL_ERROR_NONE;
358 }
359
360 EXAPI int remote_control_send_key_event(remote_control_client *client, remote_control_key_type_e key)
361 {
362     if (client == NULL || key <= REMOTE_CONTROL_KEY_ENTER || key >= REMOTE_CONTROL_KEY_CANCEL)
363         return REMOTE_CONTROL_INVALID_PARAMETER;
364
365     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
366     if (error_e)
367         return error_e;
368
369     char key_str[10] = {};
370     snprintf(key_str, sizeof(key_str), "%d", key);
371     String command = String ("|plain|send_key_event|") + String (key_str) + String ("|");
372
373     if (focus_flag) {
374         error_e = (remote_control_error_e)client->remote_client.send_remote_input_message(command.c_str ());
375
376         if (!error_e)
377             SECURE_LOGD ("%p, key=%d", client, key);
378
379         return error_e;
380     }
381
382     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
383     return REMOTE_CONTROL_INVALID_OPERATION;
384 }
385
386 EXAPI int remote_control_send_commit_string(remote_control_client *client, const char *text)
387 {
388     if (client == NULL || !text)
389         return REMOTE_CONTROL_INVALID_PARAMETER;
390
391     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
392     if (error_e)
393         return error_e;
394
395     String command = String ("|plain|commit_string|") + String (text) + String ("|");
396
397     if (focus_flag) {
398         error_e = (remote_control_error_e)client->remote_client.send_remote_input_message(command.c_str ());
399
400         if (!error_e)
401             SECURE_LOGD ("%p, \"%s\"", client, text);
402
403         return error_e;
404     }
405
406     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
407     return REMOTE_CONTROL_INVALID_OPERATION;
408 }
409
410 EXAPI int remote_control_update_preedit_string(remote_control_client *client, const char *text, Eina_List *attrs, int cursor_pos)
411 {
412     if (client == NULL)
413         return REMOTE_CONTROL_INVALID_PARAMETER;
414
415     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
416     if (error_e)
417         return error_e;
418
419     char cursor_position[10] = {};
420     snprintf(cursor_position, sizeof(cursor_position), "%d", cursor_pos);
421     String command = String ("|plain|update_preedit_string|") + String (text) + String ("|") + String (cursor_position) + String ("|");
422
423     if (focus_flag) {
424         error_e = (remote_control_error_e)client->remote_client.send_remote_input_message(command.c_str ());
425
426         if (!error_e)
427             SECURE_LOGD ("%p, \"%s\", %d", client, text, cursor_pos);
428
429         return error_e;
430     }
431
432     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
433     return REMOTE_CONTROL_INVALID_OPERATION;
434 }
435
436 EXAPI int remote_control_delete_surrounding_text(remote_control_client *client, int offset, int len)
437 {
438     if (client == NULL || len < 0) {
439         LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
440         return REMOTE_CONTROL_INVALID_PARAMETER;
441     }
442
443     remote_control_error_e error_e = (remote_control_error_e)client->remote_client.check_privilege();
444     if (error_e)
445         return error_e;
446
447     if (focus_flag) {
448         error_e = (remote_control_error_e)client->remote_client.delete_surrounding_text(offset, len);
449
450         if (!error_e)
451             LOGD ("%p, (%d, %d)", client, offset, len);
452
453         return error_e;
454     }
455
456     LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
457     return REMOTE_CONTROL_INVALID_OPERATION;
458 }
459