1 #define Uses_SCIM_TRANSACTION
2 #define Uses_ISF_REMOTE_CLIENT
3 #define Uses_STL_STRING
9 #include "isf_remote_control.h"
10 #include "isf_debug.h"
15 #define LOG_TAG "ISF_REMOTE_CONTROL"
19 static bool focus_flag;
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;
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;
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),
46 focus_in_cb_user_data(NULL),
48 focus_out_cb_user_data(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)
60 remote_handler(GIOChannel *source, GIOCondition condition, gpointer user_data)
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:
68 LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_IN");
71 if (client->focus_in_cb)
72 client->focus_in_cb (client->focus_in_cb_user_data);
75 case REMOTE_CONTROL_CALLBACK_FOCUS_OUT:
77 LOGD ("REMOTE_CONTROL_CALLBACK_FOCUS_OUT");
80 if (client->focus_out_cb)
81 client->focus_out_cb (client->focus_out_cb_user_data);
84 case REMOTE_CONTROL_CALLBACK_ENTRY_METADATA:
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;
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);
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);
101 if (client->metadata_cb)
102 client->metadata_cb (client->metadata_cb_user_data, data);
107 case REMOTE_CONTROL_CALLBACK_TEXT_UPDATED:
110 String surrounding_text;
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 ());
116 if (client->text_updated_cb)
117 client->text_updated_cb (client->text_updated_cb_user_data, surrounding_text.c_str (), cursor);
121 case REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE:
126 client->remote_client->get_input_resource (&resource);
127 LOGD ("REMOTE_CONTROL_CALLBACK_INPUT_RESOURCE: %s", (resource ? "REMOTE" : "LOCAL"));
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));
135 case REMOTE_CONTROL_CALLBACK_ERROR:
136 LOGE ("REMOTE_CONTROL_CALLBACK_ERROR");
142 LOGE ("Failed to receive callback message");
143 return (gboolean)FALSE;
145 } else if (condition == G_IO_ERR || condition == G_IO_HUP) {
146 LOGE ("Failed to receive callback message");
147 return (gboolean)FALSE;
150 return (gboolean)TRUE;
153 EXAPI remote_control_client * remote_control_connect(void)
155 remote_control_client *client = new remote_control_client;
159 client->remote_client = new RemoteInputClient;
161 if (!client->remote_client) {
162 LOGE ("Failed to generate remote_client");
166 if (!client->remote_client->open_connection()) {
167 LOGE ("REMOTE_CONTROL_REPLY_TIMEOUT");
171 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
173 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
177 client->remote_client_id = client->remote_client->get_panel2remote_connection_number();
179 if (client->remote_client_id >= 0) {
180 client->remote_client_iochannel = g_io_channel_unix_new(client->remote_client_id);
182 if (client->remote_client_iochannel == NULL) {
183 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
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);
192 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
197 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
205 if (client && client->remote_client)
206 delete client->remote_client;
214 EXAPI int remote_control_disconnect(remote_control_client *client)
218 if (client == NULL) {
219 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
220 return REMOTE_CONTROL_INVALID_PARAMETER;
223 if (client->remote_client) {
224 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege ();
226 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
230 if (client->remote_client_iochannel)
231 g_io_channel_unref (client->remote_client_iochannel);
233 if (client->remote_client_iochannel_read)
234 g_source_remove (client->remote_client_iochannel_read);
236 if (client->remote_client_iochannel_err)
237 g_source_remove (client->remote_client_iochannel_err);
239 if (client->remote_client_iochannel_hup)
240 g_source_remove (client->remote_client_iochannel_hup);
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;
247 client->remote_client->close_connection();
249 delete client->remote_client;
255 return REMOTE_CONTROL_ERROR_NONE;
258 EXAPI int remote_control_focus_in_callback_set(remote_control_client *client, remote_control_focus_in_cb func, void *user_data)
260 if (client == NULL || func == NULL) {
261 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
262 return REMOTE_CONTROL_INVALID_PARAMETER;
265 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
267 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
271 client->focus_in_cb = func;
272 client->focus_in_cb_user_data = user_data;
275 return REMOTE_CONTROL_ERROR_NONE;
278 EXAPI int remote_control_focus_in_callback_unset(remote_control_client *client)
280 if (client == NULL) {
281 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
282 return REMOTE_CONTROL_INVALID_PARAMETER;
285 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
287 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
291 client->focus_in_cb = NULL;
292 client->focus_in_cb_user_data = NULL;
295 return REMOTE_CONTROL_ERROR_NONE;
298 EXAPI int remote_control_focus_out_callback_set(remote_control_client *client, remote_control_focus_out_cb func , void *user_data)
300 if (client == NULL || func == NULL) {
301 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
302 return REMOTE_CONTROL_INVALID_PARAMETER;
305 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
307 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
311 client->focus_out_cb = func;
312 client->focus_out_cb_user_data = user_data;
315 return REMOTE_CONTROL_ERROR_NONE;
318 EXAPI int remote_control_focus_out_callback_unset(remote_control_client *client)
320 if (client == NULL) {
321 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
322 return REMOTE_CONTROL_INVALID_PARAMETER;
325 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
327 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
331 client->focus_out_cb = NULL;
332 client->focus_out_cb_user_data = NULL;
335 return REMOTE_CONTROL_ERROR_NONE;
338 EXAPI int remote_control_entry_metadata_callback_set(remote_control_client *client, remote_control_entry_metadata_cb func, void *user_data)
340 if (client == NULL || func == NULL) {
341 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
342 return REMOTE_CONTROL_INVALID_PARAMETER;
345 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
347 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
351 client->metadata_cb = func;
352 client->metadata_cb_user_data = user_data;
355 return REMOTE_CONTROL_ERROR_NONE;
358 EXAPI int remote_control_entry_metadata_callback_unset(remote_control_client *client)
360 if (client == NULL) {
361 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
362 return REMOTE_CONTROL_INVALID_PARAMETER;
365 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
367 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
371 client->metadata_cb = NULL;
372 client->metadata_cb_user_data = NULL;
375 return REMOTE_CONTROL_ERROR_NONE;
378 EXAPI int remote_control_text_updated_callback_set(remote_control_client *client, remote_control_text_updated_cb func, void *user_data)
380 if (client == NULL || func == NULL) {
381 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
382 return REMOTE_CONTROL_INVALID_PARAMETER;
385 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
387 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
391 client->text_updated_cb = func;
392 client->text_updated_cb_user_data = user_data;
395 return REMOTE_CONTROL_ERROR_NONE;
398 EXAPI int remote_control_text_updated_callback_unset(remote_control_client *client)
400 if (client == NULL) {
401 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
402 return REMOTE_CONTROL_INVALID_PARAMETER;
405 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
407 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
411 client->text_updated_cb = NULL;
412 client->text_updated_cb_user_data = NULL;
415 return REMOTE_CONTROL_ERROR_NONE;
418 EXAPI int remote_control_input_resource_changed_callback_set(remote_control_client *client, remote_control_input_resource_changed_cb func , void *user_data)
420 if (client == NULL || func == NULL) {
421 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
422 return REMOTE_CONTROL_INVALID_PARAMETER;
425 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
427 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
431 client->input_resource_changed_cb = func;
432 client->input_resource_changed_cb_user_data = user_data;
435 return REMOTE_CONTROL_ERROR_NONE;
438 EXAPI int remote_control_input_resource_changed_callback_unset(remote_control_client *client)
440 if (client == NULL) {
441 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
442 return REMOTE_CONTROL_INVALID_PARAMETER;
445 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
447 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
451 client->input_resource_changed_cb = NULL;
452 client->input_resource_changed_cb_user_data = NULL;
455 return REMOTE_CONTROL_ERROR_NONE;
458 EXAPI int remote_control_send_key_event(remote_control_client *client, remote_control_key_type_e key)
460 if (client == NULL || key > REMOTE_CONTROL_KEY_CANCEL) {
461 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
462 return REMOTE_CONTROL_INVALID_PARAMETER;
465 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
467 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
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 ("|");
476 error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
479 SECURE_LOGD ("%p, key=%d", client, key);
484 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
485 return REMOTE_CONTROL_INVALID_OPERATION;
488 EXAPI int remote_control_send_commit_string(remote_control_client *client, const char *text)
490 if (client == NULL || !text) {
491 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
492 return REMOTE_CONTROL_INVALID_PARAMETER;
495 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
497 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
501 String command = String ("|plain|commit_string|") + String (text) + String ("|");
504 error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
507 SECURE_LOGD ("%p, \"%s\"", client, text);
512 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
513 return REMOTE_CONTROL_INVALID_OPERATION;
516 EXAPI int remote_control_update_preedit_string(remote_control_client *client, const char *text, Eina_List *attrs, int cursor_pos)
518 if (client == NULL) {
519 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
520 return REMOTE_CONTROL_INVALID_PARAMETER;
523 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
525 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
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 ("|");
534 error_e = (remote_control_error_e)client->remote_client->send_remote_input_message(command.c_str ());
537 SECURE_LOGD ("%p, %d, \"%s\"", client, cursor_pos, text);
542 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
543 return REMOTE_CONTROL_INVALID_OPERATION;
546 EXAPI int remote_control_delete_surrounding_text(remote_control_client *client, int offset, int len)
548 if (client == NULL || len < 0) {
549 LOGE ("REMOTE_CONTROL_INVALID_PARAMETER");
550 return REMOTE_CONTROL_INVALID_PARAMETER;
553 remote_control_error_e error_e = (remote_control_error_e)client->remote_client->check_privilege();
555 LOGE ("REMOTE_CONTROL_PERMISSION_DENIED");
560 error_e = (remote_control_error_e)client->remote_client->delete_surrounding_text(offset, len);
563 LOGD ("%p, (%d, %d)", client, offset, len);
568 LOGE ("REMOTE_CONTROL_INVALID_OPERATION");
569 return REMOTE_CONTROL_INVALID_OPERATION;