5498ae1c32e66814be3c2bbd916f8b19041938bc
[platform/core/uifw/libscl-core.git] / src / sclconnection-isf.cpp
1 /*
2  * Copyright (c) 2014 - 2015 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
18 #include "sclconnection-isf.h"
19 #include "sclcoreimpl.h"
20 #include <Elementary.h>
21 #include <dlog.h>
22
23 #ifdef WEBSOCKET
24 #include "legacy_support/websocket.h"
25 extern CWebHelperAgentWebSocket g_websocket;
26 #endif
27
28 using namespace scl;
29
30 static scim::ConfigPointer _scim_config(0);
31
32 /* Slot functions for calling appropriate callback functions */
33 static void slot_exit(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
34     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
35     if (impl) {
36         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
37         if (callback) {
38             callback->on_ise_hide(ic, ic_uuid.c_str());
39             LOGD("call on_exit");
40             callback->on_exit();
41         }
42         if (agent) {
43             agent->update_ise_exit();
44         }
45     }
46     LOGD("exit");
47     elm_exit();
48 }
49
50 static void slot_attach_input_context(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
51     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
52     if (impl) {
53         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
54         if (callback) {
55             callback->on_attach_input_context(ic, ic_uuid.c_str());
56         }
57     }
58 }
59
60 static void slot_detach_input_context(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
61     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
62     if (impl) {
63         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
64         if (callback) {
65             callback->on_detach_input_context(ic, ic_uuid.c_str());
66         }
67     }
68 }
69
70 static void slot_reload_config(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
71     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
72     if (impl) {
73         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
74         if (callback) {
75             callback->on_reload_config(ic, ic_uuid.c_str());
76         }
77     }
78 }
79
80 static void slot_update_screen(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid, int screen_number) {
81     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
82     if (impl) {
83         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
84         if (callback) {
85             //callback->on_update_screen(ic, ic_uuid.c_str(), screen_number);
86         }
87     }
88 }
89
90 static void slot_update_spot_location(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid, int x, int y) {
91     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
92     if (impl) {
93         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
94         if (callback) {
95             callback->on_update_spot_location(ic, ic_uuid.c_str(), x, y);
96         }
97     }
98 }
99
100 static void slot_update_cursor_position(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid, int cursor_pos) {
101     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
102     if (impl) {
103         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
104         if (callback) {
105             callback->on_update_cursor_position(ic, ic_uuid.c_str(), cursor_pos);
106 #ifdef WEBSOCKET
107             g_websocket.on_update_cursor_position(ic, cursor_pos);
108 #endif
109         }
110     }
111 }
112
113 static void slot_update_surrounding_text(const scim::HelperAgent *agent, int ic, const scim::String &text, int cursor) {
114     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
115     if (impl) {
116         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
117         if (callback) {
118             callback->on_update_surrounding_text(ic, text.c_str(), cursor);
119 #ifdef WEBSOCKET
120             g_websocket.on_update_surrounding_text(ic, text.c_str(), cursor);
121 #endif
122         }
123     }
124 }
125
126 static void slot_trigger_property(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid, const scim::String &property) {
127     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
128     if (impl) {
129         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
130         if (callback) {
131             //callback->on_trigger_property(ic, ic_uuid.c_str(), property.c_str());
132         }
133     }
134 }
135
136 static void slot_focus_out(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
137     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
138     if (impl) {
139         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
140         if (callback) {
141             callback->on_focus_out(ic, ic_uuid.c_str());
142 #ifdef WEBSOCKET
143             g_websocket.on_focus_out(ic);
144 #endif
145         }
146     }
147 }
148
149 static void slot_focus_in(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
150     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
151     if (impl) {
152         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
153         if (callback) {
154             callback->on_focus_in(ic, ic_uuid.c_str());
155 #ifdef WEBSOCKET
156             g_websocket.on_focus_in(ic);
157 #endif
158         }
159     }
160 }
161
162 static void slot_ise_show(const scim::HelperAgent *agent, int ic, char *buf, size_t &len) {
163     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
164     if (impl) {
165         /* Make sure the appropriate keyboard ise was selected -> is this really necessary? */
166         //impl->set_keyboard_ise_by_uuid(impl->get_keyboard_ise_uuid().c_str());
167
168         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
169         if (callback) {
170             /* Check if effect is enabled */
171             Ise_Context ise_context;
172             memset(&ise_context, 0x00, sizeof(ise_context));
173
174             if (len >= sizeof(Ise_Context)) {
175                 memcpy(&ise_context, buf, sizeof(ise_context));
176
177                 if (ise_context.imdata_size > 0) {
178                     callback->on_set_imdata(buf + sizeof(ise_context), ise_context.imdata_size);
179                 }
180             } else {
181                 LOGD("\n-=-= WARNING - buf %p len %d size %d \n", buf, len, sizeof(ise_context));
182             }
183             CSCLCoreUI* ui = impl->get_core_ui();
184             if (ui) {
185                 ui->process_keyboard_ui_state_change(KEYBOARD_UI_STATE_WILL_SHOW);
186             }
187             callback->on_ise_show(ic, impl->get_screen_rotation_degree(), ise_context);
188             if (ui) {
189                 ui->process_keyboard_ui_state_change(KEYBOARD_UI_STATE_DID_SHOW);
190             }
191
192 #ifdef WEBSOCKET
193             g_websocket.on_set_layout(ise_context.layout);
194             g_websocket.on_update_cursor_position(ic, ise_context.cursor_pos);
195             g_websocket.on_show(ic);
196 #endif
197         }
198     }
199 }
200
201 static void slot_ise_hide(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
202     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
203     if (impl) {
204         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
205         if (callback) {
206             CSCLCoreUI* ui = impl->get_core_ui();
207             if (ui) {
208                 ui->process_keyboard_ui_state_change(KEYBOARD_UI_STATE_WILL_HIDE);
209             }
210             callback->on_ise_hide(ic, ic_uuid.c_str());
211             if (ui) {
212                 ui->process_keyboard_ui_state_change(KEYBOARD_UI_STATE_DID_HIDE);
213             }
214
215 #ifdef WEBSOCKET
216             g_websocket.on_hide(ic);
217 #endif
218         }
219     }
220 }
221
222 static void slot_get_geometry(const scim::HelperAgent *agent, struct scim::rectinfo &info) {
223     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
224     if (impl) {
225         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
226         if (callback) {
227             callback->on_get_geometry(&(info.pos_x), &(info.pos_y), &(info.width), &(info.height));
228         }
229     }
230 }
231
232 static void slot_set_mode(const scim::HelperAgent *agent, scim::uint32 &mode) {
233     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
234     if (impl) {
235         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
236         if (callback) {
237             callback->on_set_mode(mode);
238         }
239     }
240 }
241
242 static void slot_set_language(const scim::HelperAgent *agent, scim::uint32 &language) {
243     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
244     if (impl) {
245         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
246         if (callback) {
247             callback->on_set_language(language);
248 #ifdef WEBSOCKET
249             g_websocket.on_set_language(language);
250 #endif
251         }
252     }
253 }
254
255 static void slot_set_imdata(const scim::HelperAgent *agent, char *buf, size_t &len) {
256     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
257     if (impl) {
258         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
259         if (callback) {
260             scl32 _len = static_cast<scl32>(reinterpret_cast<size_t>(len) & 0xffffffff);
261             callback->on_set_imdata(buf, _len);
262 #ifdef WEBSOCKET
263             g_websocket.on_set_imdata(buf, len);
264 #endif
265         }
266     }
267 }
268
269 static void slot_get_imdata(const scim::HelperAgent *, char **buf, size_t &len) {
270     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
271     if (impl) {
272         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
273         if (callback) {
274             sclu32 _len = 0;
275             callback->on_get_imdata(buf, &_len);
276 #ifdef WEBSOCKET
277             g_websocket.on_get_imdata(buf, &_len);
278 #endif
279             len = _len;
280         }
281     }
282 }
283
284 static void slot_get_language_locale(const scim::HelperAgent *, int ic, char **locale) {
285     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
286     if (impl) {
287         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
288         if (callback) {
289             callback->on_get_language_locale(ic, locale);
290         }
291     }
292 }
293
294 static void slot_set_return_key_type(const scim::HelperAgent *agent, scim::uint32 &type) {
295     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
296     if (impl) {
297         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
298         if (callback) {
299             callback->on_set_return_key_type(type);
300 #ifdef WEBSOCKET
301             g_websocket.on_set_return_key_type(type);
302 #endif
303         }
304     }
305 }
306
307 static void slot_get_return_key_type(const scim::HelperAgent *agent, scim::uint32 &type) {
308     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
309     if (impl) {
310         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
311         if (callback) {
312             callback->on_get_return_key_type(&type);
313 #ifdef WEBSOCKET
314             g_websocket.on_get_return_key_type(&type);
315 #endif
316         }
317     }
318 }
319
320 static void slot_set_return_key_disable(const scim::HelperAgent *agent, scim::uint32 &disabled) {
321     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
322     if (impl) {
323         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
324         if (callback) {
325             callback->on_set_return_key_disable(disabled);
326 #ifdef WEBSOCKET
327             g_websocket.on_set_return_key_disable(disabled);
328 #endif
329         }
330     }
331 }
332
333 static void slot_get_return_key_disable(const scim::HelperAgent *agent, scim::uint32 &disabled) {
334     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
335     if (impl) {
336         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
337         if (callback) {
338             callback->on_get_return_key_disable(&disabled);
339 #ifdef WEBSOCKET
340             g_websocket.on_get_return_key_disable(&disabled);
341 #endif
342         }
343     }
344 }
345
346 static void slot_set_layout(const scim::HelperAgent *agent, scim::uint32 &layout) {
347     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
348     if (impl) {
349         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
350         if (callback) {
351             callback->on_set_layout(layout);
352 #ifdef WEBSOCKET
353             g_websocket.on_set_layout(layout);
354 #endif
355         }
356     }
357 }
358
359 static void slot_get_layout(const scim::HelperAgent *agent, scim::uint32 &layout) {
360     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
361     if (impl) {
362         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
363         if (callback) {
364             callback->on_get_layout(&layout);
365 #ifdef WEBSOCKET
366             g_websocket.on_get_layout(&layout);
367 #endif
368         }
369     }
370 }
371
372 static void slot_set_caps_mode(const scim::HelperAgent *agent, scim::uint32 &mode) {
373     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
374     if (impl) {
375         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
376         if (callback) {
377             callback->on_set_caps_mode(mode);
378         }
379     }
380 }
381
382 static void slot_reset_input_context(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
383     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
384     if (impl) {
385         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
386         if (callback) {
387             callback->on_reset_input_context(ic, uuid.c_str());
388 #ifdef WEBSOCKET
389             g_websocket.on_reset_input_context(ic);
390 #endif
391         }
392     }
393 }
394
395 static void slot_update_candidate_geometry(const scim::HelperAgent *agent, int ic, const scim::String &uuid, const scim::rectinfo &info) {
396     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
397     if (impl) {
398         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
399         if (callback) {
400             callback->on_update_candidate_geometry(ic, uuid.c_str(), info.pos_x, info.pos_y, info.width, info.height);
401         }
402     }
403 }
404 static void slot_update_keyboard_ise(const scim::HelperAgent *agent, int ic, const scim::String &uuid,
405                                                   const scim::String &ise_name, const scim::String &ise_uuid) {
406     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
407     if (impl) {
408         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
409         if (callback) {
410             callback->on_update_keyboard_ise(ic, uuid.c_str(), ise_name.c_str(), ise_uuid.c_str());
411         }
412     }
413 }
414
415 static void slot_candidate_more_window_show(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
416     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
417     if (impl) {
418         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
419         if (callback) {
420             callback->on_candidate_more_window_show(ic, uuid.c_str());
421         }
422     }
423 }
424
425 static void slot_candidate_more_window_hide(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
426     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
427     if (impl) {
428         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
429         if (callback) {
430             callback->on_candidate_more_window_hide(ic, uuid.c_str());
431         }
432     }
433 }
434
435 static void slot_select_aux(const scim::HelperAgent *agent, int ic, const scim::String &uuid, int index) {
436     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
437     if (impl) {
438         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
439         if (callback) {
440             callback->on_select_aux(ic, uuid.c_str(), index);
441         }
442     }
443 }
444
445 static void slot_select_candidate(const scim::HelperAgent *agent, int ic, const scim::String &uuid, int index) {
446     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
447     if (impl) {
448         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
449         if (callback) {
450             callback->on_select_candidate(ic, uuid.c_str(), index);
451         }
452     }
453 }
454
455 static void slot_candidate_table_page_up(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
456     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
457     if (impl) {
458         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
459         if (callback) {
460             callback->on_candidate_table_page_up(ic, uuid.c_str());
461         }
462     }
463 }
464
465 static void slot_candidate_table_page_down(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
466     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
467     if (impl) {
468         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
469         if (callback) {
470             callback->on_candidate_table_page_down(ic, uuid.c_str());
471         }
472     }
473 }
474
475 static void slot_update_candidate_table_page_size(const scim::HelperAgent *, int ic, const scim::String &uuid, int page_size) {
476     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
477     if (impl) {
478         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
479         if (callback) {
480             callback->on_update_candidate_table_page_size(ic, uuid.c_str(), page_size);
481         }
482     }
483 }
484
485 static void slot_update_lookup_table(const scim::HelperAgent *, scim::LookupTable &table) {
486     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
487     if (impl) {
488         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
489         if (callback) {
490             SclCandidateTable lookup_table;
491             lookup_table.page_size = table.get_page_size();
492             lookup_table.current_page_start = table.get_current_page_start();
493             lookup_table.cursor_pos = table.get_cursor_pos();
494             lookup_table.cursor_visible = table.is_cursor_visible();
495             lookup_table.page_size_fixed = table.is_page_size_fixed();
496             lookup_table.candidate_labels.clear();
497             for (int page_index = 0;page_index < lookup_table.page_size;page_index++) {
498                 scim::WideString label = table.get_candidate_label(page_index);
499                 lookup_table.candidate_labels.push_back(scim::utf8_wcstombs(label).c_str());
500             }
501
502             int nCandidateSize = table.get_current_page_size();
503             for (int index = 0; index < nCandidateSize; ++index) {
504                 scim::WideString candidate_str = table.get_candidate_in_current_page(index);
505                 lookup_table.candidate.push_back(scim::utf8_wcstombs(candidate_str).c_str());
506             }
507
508             callback->on_update_lookup_table(lookup_table);
509
510             std::vector<scim::WideString> labels;
511             for (unsigned int loop = 0;loop < lookup_table.candidate_labels.size();loop++) {
512                 labels.push_back(scim::utf8_mbstowcs(lookup_table.candidate_labels.at(loop).c_str()));
513             }
514             table.set_candidate_labels(labels);
515         }
516     }
517 }
518
519 static void slot_select_associate(const scim::HelperAgent *agent, int ic, const scim::String &uuid, int index) {
520     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
521     if (impl) {
522         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
523         if (callback) {
524             callback->on_select_associate(ic, uuid.c_str(), index);
525         }
526     }
527 }
528
529 static void slot_associate_table_page_up(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
530     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
531     if (impl) {
532         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
533         if (callback) {
534             callback->on_associate_table_page_up(ic, uuid.c_str());
535         }
536     }
537 }
538
539 static void slot_associate_table_page_down(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
540     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
541     if (impl) {
542         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
543         if (callback) {
544             callback->on_associate_table_page_down(ic, uuid.c_str());
545         }
546     }
547 }
548
549 static void slot_update_associate_table_page_size(const scim::HelperAgent *, int ic, const scim::String &uuid, int page_size) {
550     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
551     if (impl) {
552         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
553         if (callback) {
554             callback->on_update_associate_table_page_size(ic, uuid.c_str(), page_size);
555         }
556     }
557 }
558
559 static void slot_show_ise_option_window(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
560     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
561     if (impl) {
562         CSCLCoreUI *core_ui = impl->get_core_ui();
563         if (core_ui) {
564             LOGD("slot_show_ise_option_window() called!!!\n");
565             core_ui->create_option_window(OPTION_WINDOW_TYPE_SETTING_APPLICATION);
566         }
567     }
568 }
569
570 static void slot_resume_ise_option_window(const scim::HelperAgent *agent, int ic, const scim::String &uuid) {
571     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
572     if (impl) {
573         CSCLCoreUI *core_ui = impl->get_core_ui();
574         if (core_ui) {
575             LOGD("slot_resume_ise_option_window() called!!!\n");
576             core_ui->show_option_window(OPTION_WINDOW_TYPE_SETTING_APPLICATION);
577         }
578     }
579 }
580
581 static void slot_check_ise_option_window(const scim::HelperAgent *agent, sclu32 &avail) {
582     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
583     if (impl) {
584         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
585         if (callback) {
586             callback->on_check_option_window_availability(reinterpret_cast<sclboolean *>(&avail));
587         }
588     }
589 }
590
591 static void slot_process_key_event(const scim::HelperAgent *agent, scim::KeyEvent &key, scim::uint32 &ret) {
592     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
593     if (impl) {
594         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
595         if (callback) {
596             callback->on_process_key_event(key, &ret);
597 #ifdef WEBSOCKETweb_ret
598             g_websocket.on_process_key_event(key.code, key.mask, key.layout, &ret);
599 #endif
600         }
601     }
602 }
603
604 static void slot_candidate_show(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
605     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
606     if (impl) {
607         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
608         if (callback) {
609             callback->on_candidate_show(ic, ic_uuid.c_str());
610         }
611     }
612 }
613
614 static void slot_candidate_hide(const scim::HelperAgent *agent, int ic, const scim::String &ic_uuid) {
615     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
616     if (impl) {
617         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
618         if (callback) {
619             callback->on_candidate_hide(ic, ic_uuid.c_str());
620         }
621     }
622 }
623
624 static void slot_process_input_device_event(const scim::HelperAgent *agent, scim::uint32 &type, char *data, size_t &len, scim::uint32 &ret) {
625     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
626     if (impl) {
627         ISCLCoreEventCallback *callback = impl->get_core_event_callback();
628         if (callback) {
629             /* Input Device Event callback is available in versions after 1.1 */
630             if (check_interface_version(callback, 1, 1)) {
631                 callback->on_process_input_device_event(type, data, len, &ret);
632             }
633         }
634     }
635 }
636
637 /* Internal input handler function */
638 Eina_Bool input_handler(void *data, Ecore_Fd_Handler *fd_handler)
639 {
640     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
641     if (impl) {
642         scim::HelperAgent *agent = static_cast<scim::HelperAgent*>(data);
643         if (agent) {
644             if (agent->has_pending_event()) {
645                 if (!(agent->filter_event())) {
646                     LOGD("helper_agent.filter_event() failed!!!\n");
647                     impl->fini();
648                     elm_exit();
649                 }
650             } else {
651                 LOGD("helper_agent.has_pending_event() failed!!!\n");
652                 impl->fini();
653                 elm_exit();
654             }
655         }
656     }
657
658     return ECORE_CALLBACK_RENEW;
659 }
660
661 CSCLConnectionISF::CSCLConnectionISF()
662 {
663     m_initialized = FALSE;
664     m_fd_handler = NULL;
665
666     m_backend_identifier = "ISF";
667 }
668
669 CSCLConnectionISF::~CSCLConnectionISF()
670 {
671 }
672
673 sclboolean CSCLConnectionISF::init()
674 {
675     LOGD("Enter\n");
676
677     CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
678     if (impl) {
679         sclchar *uuid = impl->get_uuid();
680         if (uuid) {
681             m_helper_info.uuid = uuid;
682         }
683     }
684
685     if (!m_initialized) {
686         m_helper_agent.signal_connect_exit(scim::slot(slot_exit));
687         m_helper_agent.signal_connect_attach_input_context(scim::slot(slot_attach_input_context));
688         m_helper_agent.signal_connect_detach_input_context(scim::slot(slot_detach_input_context));
689         m_helper_agent.signal_connect_reload_config(scim::slot(slot_reload_config));
690         m_helper_agent.signal_connect_update_screen(scim::slot(slot_update_screen));
691         m_helper_agent.signal_connect_update_spot_location(scim::slot(slot_update_spot_location));
692         m_helper_agent.signal_connect_update_cursor_position(scim::slot(slot_update_cursor_position));
693         m_helper_agent.signal_connect_update_surrounding_text(scim::slot(slot_update_surrounding_text));
694         m_helper_agent.signal_connect_trigger_property(scim::slot(slot_trigger_property));
695         //m_helper_agent.signal_connect_process_imengine_event (slot (slot_process_imengine_event));
696         m_helper_agent.signal_connect_focus_out(scim::slot(slot_focus_out));
697         m_helper_agent.signal_connect_focus_in(scim::slot(slot_focus_in));
698         m_helper_agent.signal_connect_ise_show(scim::slot(slot_ise_show));
699         m_helper_agent.signal_connect_ise_hide(scim::slot(slot_ise_hide));
700         m_helper_agent.signal_connect_get_geometry(scim::slot(slot_get_geometry));
701         m_helper_agent.signal_connect_set_mode(scim::slot(slot_set_mode));
702         m_helper_agent.signal_connect_set_language(scim::slot(slot_set_language));
703         m_helper_agent.signal_connect_set_imdata(scim::slot(slot_set_imdata));
704         m_helper_agent.signal_connect_get_imdata(scim::slot(slot_get_imdata));
705         m_helper_agent.signal_connect_get_language_locale(scim::slot(slot_get_language_locale));
706         m_helper_agent.signal_connect_set_return_key_type(scim::slot(slot_set_return_key_type));
707         m_helper_agent.signal_connect_get_return_key_type(scim::slot(slot_get_return_key_type));
708         m_helper_agent.signal_connect_set_return_key_disable(scim::slot(slot_set_return_key_disable));
709         m_helper_agent.signal_connect_get_return_key_disable(scim::slot(slot_get_return_key_disable));
710         m_helper_agent.signal_connect_get_layout(scim::slot(slot_get_layout));
711         m_helper_agent.signal_connect_set_layout(scim::slot(slot_set_layout));
712         m_helper_agent.signal_connect_set_caps_mode(scim::slot(slot_set_caps_mode));
713         m_helper_agent.signal_connect_reset_input_context(scim::slot(slot_reset_input_context));
714         m_helper_agent.signal_connect_update_candidate_geometry(scim::slot(slot_update_candidate_geometry));
715         m_helper_agent.signal_connect_update_keyboard_ise(scim::slot(slot_update_keyboard_ise));
716         //m_helper_agent.signal_connect_update_keyboard_ise_list (slot (slot_update_keyboard_ise_list));
717         m_helper_agent.signal_connect_candidate_more_window_show(scim::slot(slot_candidate_more_window_show));
718         m_helper_agent.signal_connect_candidate_more_window_hide(scim::slot(slot_candidate_more_window_hide));
719         m_helper_agent.signal_connect_select_aux(scim::slot(slot_select_aux));
720         m_helper_agent.signal_connect_select_candidate(scim::slot(slot_select_candidate));
721         m_helper_agent.signal_connect_candidate_table_page_up(scim::slot(slot_candidate_table_page_up));
722         m_helper_agent.signal_connect_candidate_table_page_down(scim::slot(slot_candidate_table_page_down));
723         m_helper_agent.signal_connect_update_candidate_table_page_size(scim::slot(slot_update_candidate_table_page_size));
724         m_helper_agent.signal_connect_update_lookup_table(scim::slot(slot_update_lookup_table));
725         m_helper_agent.signal_connect_select_associate(scim::slot(slot_select_associate));
726         m_helper_agent.signal_connect_associate_table_page_up(scim::slot(slot_associate_table_page_up));
727         m_helper_agent.signal_connect_associate_table_page_down(scim::slot(slot_associate_table_page_down));
728         m_helper_agent.signal_connect_update_associate_table_page_size(scim::slot(slot_update_associate_table_page_size));
729         m_helper_agent.signal_connect_show_option_window(scim::slot(slot_show_ise_option_window));
730         m_helper_agent.signal_connect_resume_option_window(scim::slot(slot_resume_ise_option_window));
731         m_helper_agent.signal_connect_check_option_window(scim::slot(slot_check_ise_option_window));
732         m_helper_agent.signal_connect_process_key_event(scim::slot(slot_process_key_event));
733         m_helper_agent.signal_connect_candidate_show(scim::slot(slot_candidate_show));
734         m_helper_agent.signal_connect_candidate_hide(scim::slot(slot_candidate_hide));
735         m_helper_agent.signal_connect_process_input_device_event(scim::slot(slot_process_input_device_event));
736
737         m_initialized = TRUE;
738     }
739
740     if (_scim_config.null()) {
741         scim::ConfigPointer config_pointer = scim::ConfigBase::get(true, "socket");
742         if (config_pointer.null()) {
743             config_pointer = new scim::DummyConfig();
744         }
745         _scim_config = config_pointer;
746     }
747
748     return TRUE;
749 }
750
751 void CSCLConnectionISF::fini()
752 {
753     if (!_scim_config.null()) {
754         _scim_config.reset();
755     }
756     scim::ConfigBase::set(0);
757     close_connection();
758     m_initialized = FALSE;
759 }
760
761 void CSCLConnectionISF::open_connection(const sclchar *display)
762 {
763     if (m_initialized) {
764         m_helper_agent.open_connection(m_helper_info, display);
765         int fd = m_helper_agent.get_connection_number();
766
767         if (fd >= 0) {
768 #ifndef WAYLAND
769             Evas_Object *main_window = NULL;
770             CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
771             if (impl) {
772                 main_window = NATIVE_WINDOW_CAST(impl->get_main_window());
773             }
774
775             Ecore_X_Window xwindow = elm_win_xwindow_get(main_window);
776             char xid[255];
777             snprintf(xid, 255, "%d", xwindow);
778             scim::Property prop(xid, "XID", "", "");
779             scim::PropertyList props;
780             props.push_back(prop);
781             m_helper_agent.register_properties(props);
782 #endif
783
784             m_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, input_handler, &m_helper_agent, NULL, NULL);
785         }
786     }
787 }
788 void CSCLConnectionISF::close_connection()
789 {
790     if (m_initialized) {
791         if (m_fd_handler) {
792             ecore_main_fd_handler_del(m_fd_handler);
793             m_fd_handler = NULL;
794         }
795         m_helper_agent.update_ise_exit();
796         m_helper_agent.close_connection();
797     }
798 }
799
800 void CSCLConnectionISF::config_reload()
801 {
802     if (m_initialized) {
803         m_helper_agent.reload_config();
804     }
805 }
806
807 sclboolean CSCLConnectionISF::config_read_int(const sclchar *name, sclint &value)
808 {
809     sclboolean ret = FALSE;
810     if (m_initialized && _scim_config) {
811         value = _scim_config->read(name, value);
812         ret = TRUE;
813     }
814     return ret;
815 }
816
817 sclboolean CSCLConnectionISF::config_read_string(const sclchar *name, std::string &value)
818 {
819     sclboolean ret = FALSE;
820     if (m_initialized && _scim_config) {
821         value = _scim_config->read(name, value);
822         ret = TRUE;
823     }
824     return ret;
825 }
826
827 sclboolean CSCLConnectionISF::config_write_int(const sclchar *name, sclint value)
828 {
829     sclboolean ret = FALSE;
830     if (m_initialized && _scim_config) {
831         _scim_config->write(name, value);
832         ret = TRUE;
833     }
834     return ret;
835 }
836
837 sclboolean CSCLConnectionISF::config_write_string(const sclchar *name, const std::string value)
838 {
839     sclboolean ret = FALSE;
840     if (m_initialized && _scim_config) {
841         _scim_config->write(name, value);
842         ret = TRUE;
843     }
844     return ret;
845 }
846
847 sclboolean CSCLConnectionISF::config_erase(const sclchar *name)
848 {
849     sclboolean ret = FALSE;
850     if (m_initialized && _scim_config) {
851         _scim_config->erase(name);
852         ret = TRUE;
853     }
854     return ret;
855 }
856
857 sclboolean CSCLConnectionISF::config_flush(void)
858 {
859     sclboolean ret = FALSE;
860     if (m_initialized && _scim_config) {
861         _scim_config->flush();
862         ret = TRUE;
863     }
864     return ret;
865 }
866
867 void CSCLConnectionISF::send_imengine_event(sclint ic, const sclchar *ic_uuid, const sclint command, const sclu32 value)
868 {
869     if (m_initialized) {
870         scim::String uuid;
871         if (ic_uuid) {
872             uuid = scim::String(ic_uuid);
873         }
874         scim::Transaction trans;
875         trans.put_command(command);
876         trans.put_data(value);
877         m_helper_agent.send_imengine_event(ic, uuid, trans);
878     }
879 }
880
881 void CSCLConnectionISF::reset_keyboard_ise()
882 {
883     if (m_initialized) {
884         m_helper_agent.reset_keyboard_ise();
885     }
886 }
887
888 void CSCLConnectionISF::flush_keyboard_ise()
889 {
890     if (m_initialized) {
891         m_helper_agent.flush_keyboard_ise();
892     }
893 }
894
895 void CSCLConnectionISF::send_key_event(sclint ic, const sclchar *ic_uuid, sclu32 keycode, sclu16 keymask)
896 {
897     if (m_initialized) {
898         scim::String uuid;
899         if (ic_uuid) {
900             uuid = scim::String(ic_uuid);
901         }
902         scim::KeyEvent event;
903         event.code = keycode;
904         event.mask = keymask;
905         m_helper_agent.send_key_event(ic, uuid, event);
906     }
907 }
908
909 void CSCLConnectionISF::forward_key_event(sclint ic, const sclchar *ic_uuid, sclu32 keycode, sclu16 keymask)
910 {
911     if (m_initialized) {
912         scim::String uuid;
913         if (ic_uuid) {
914             uuid = scim::String(ic_uuid);
915         }
916         scim::KeyEvent event;
917         event.code = keycode;
918         event.mask = keymask;
919         m_helper_agent.forward_key_event(ic, uuid, event);
920     }
921 }
922
923 void CSCLConnectionISF::commit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str)
924 {
925     if (m_initialized) {
926         scim::String uuid;
927         if (ic_uuid) {
928             uuid = scim::String(ic_uuid);
929         }
930         m_helper_agent.commit_string(ic, uuid, scim::utf8_mbstowcs(str));
931     }
932 }
933
934 void CSCLConnectionISF::select_candidate(int index)
935 {
936     if (m_initialized) {
937         m_helper_agent.select_candidate(index);
938     }
939 }
940
941 void CSCLConnectionISF::show_preedit_string(sclint ic, const sclchar *ic_uuid)
942 {
943     if (m_initialized) {
944         scim::String uuid;
945         if (ic_uuid) {
946             uuid = scim::String(ic_uuid);
947         }
948         m_helper_agent.show_preedit_string(ic, uuid);
949     }
950 }
951
952 void CSCLConnectionISF::show_aux_string(void)
953 {
954     if (m_initialized) {
955         m_helper_agent.show_aux_string();
956     }
957 }
958
959 void CSCLConnectionISF::show_candidate_string(void)
960 {
961     if (m_initialized) {
962         m_helper_agent.show_candidate_string();
963     }
964 }
965
966 void CSCLConnectionISF::show_associate_string(void)
967 {
968     if (m_initialized) {
969         m_helper_agent.show_associate_string();
970     }
971 }
972
973 void CSCLConnectionISF::hide_preedit_string(sclint ic, const sclchar *ic_uuid)
974 {
975     if (m_initialized) {
976         scim::String uuid;
977         if (ic_uuid) {
978             uuid = scim::String(ic_uuid);
979         }
980         m_helper_agent.hide_preedit_string(ic, uuid);
981     }
982 }
983
984 void CSCLConnectionISF::hide_aux_string(void)
985 {
986     if (m_initialized) {
987         m_helper_agent.hide_aux_string();
988     }
989 }
990
991 void CSCLConnectionISF::hide_candidate_string(void)
992 {
993     if (m_initialized) {
994         m_helper_agent.hide_candidate_string();
995     }
996 }
997
998 void CSCLConnectionISF::hide_associate_string(void)
999 {
1000     if (m_initialized) {
1001         m_helper_agent.hide_associate_string();
1002     }
1003 }
1004
1005 void CSCLConnectionISF::update_preedit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str)
1006 {
1007     if (m_initialized) {
1008         scim::AttributeList list;
1009         scim::String uuid;
1010         if (ic_uuid) {
1011             uuid = scim::String(ic_uuid);
1012         }
1013         m_helper_agent.update_preedit_string(ic, uuid, scim::utf8_mbstowcs(str), list);
1014
1015         if (str && strlen(str) > 0) {
1016             show_preedit_string(ic, ic_uuid);
1017         } else {
1018             hide_preedit_string(ic, ic_uuid);
1019         }
1020     }
1021 }
1022
1023 void CSCLConnectionISF::update_preedit_string(sclint ic, const sclchar *ic_uuid, const sclchar *str, const scim::AttributeList &attrs)
1024 {
1025     if (m_initialized) {
1026         scim::String uuid;
1027         if (ic_uuid) {
1028             uuid = scim::String(ic_uuid);
1029         }
1030         m_helper_agent.update_preedit_string(ic, uuid, scim::utf8_mbstowcs(str), attrs);
1031
1032         if (str && strlen(str) > 0) {
1033             show_preedit_string(ic, ic_uuid);
1034         } else {
1035             hide_preedit_string(ic, ic_uuid);
1036         }
1037     }
1038 }
1039
1040 void CSCLConnectionISF::update_aux_string(const sclchar *str)
1041 {
1042     if (m_initialized) {
1043         scim::AttributeList list;
1044         m_helper_agent.update_aux_string(scim::String(str), list);
1045     }
1046 }
1047
1048 void CSCLConnectionISF::update_input_context(sclu32 type, sclu32 value)
1049 {
1050     if (m_initialized) {
1051         m_helper_agent.update_input_context(type, value);
1052     }
1053 }
1054
1055 void CSCLConnectionISF::update_geometry(sclint x, sclint y, sclint width, sclint height)
1056 {
1057     if (m_initialized) {
1058         m_helper_agent.update_geometry(x, y, width, height);
1059     }
1060 }
1061
1062 void CSCLConnectionISF::get_surrounding_text(const sclchar *ic_uuid, sclint maxlen_before, sclint maxlen_after) const
1063 {
1064     if (m_initialized) {
1065         scim::String uuid;
1066         if (ic_uuid) {
1067             uuid = scim::String(ic_uuid);
1068         }
1069         m_helper_agent.get_surrounding_text(uuid, maxlen_before, maxlen_after);
1070     }
1071 }
1072
1073 sclint CSCLConnectionISF::get_surrounding_text(sclint maxlen_before, sclint maxlen_after, sclchar **text, int &cursor)
1074 {
1075     if (m_initialized) {
1076         scim::String surrounding_text;
1077         m_helper_agent.get_surrounding_text(maxlen_before, maxlen_after, surrounding_text, cursor);
1078
1079         if (text) {
1080             *text = strdup(surrounding_text.c_str());
1081             if (*text == NULL) {
1082                 return -1;
1083             }
1084         }
1085     }
1086     return 0;
1087 }
1088
1089 void CSCLConnectionISF::delete_surrounding_text(sclint offset, sclint len) const
1090 {
1091     if (m_initialized) {
1092         m_helper_agent.delete_surrounding_text(offset, len);
1093     }
1094 }
1095
1096 void CSCLConnectionISF::set_candidate_position(sclint left, sclint top)
1097 {
1098     if (m_initialized) {
1099         m_helper_agent.set_candidate_position(left, top);
1100     }
1101 }
1102
1103 void CSCLConnectionISF::enable_soft_candidate(sclboolean enable)
1104 {
1105     if (m_initialized) {
1106         if (enable)
1107             m_helper_agent.set_candidate_style(scim::ONE_LINE_CANDIDATE, scim::SOFT_CANDIDATE_WINDOW);
1108         else
1109             m_helper_agent.set_candidate_style(scim::ONE_LINE_CANDIDATE, scim::FIXED_CANDIDATE_WINDOW);
1110     }
1111 }
1112
1113 void CSCLConnectionISF::candidate_hide(void)
1114 {
1115     if (m_initialized) {
1116         m_helper_agent.candidate_hide();
1117     }
1118 }
1119
1120 void CSCLConnectionISF::set_keyboard_ise_by_uuid(const sclchar *uuid)
1121 {
1122     if (m_initialized) {
1123         m_helper_agent.set_keyboard_ise_by_uuid(uuid);
1124     }
1125 }
1126
1127 void CSCLConnectionISF::get_keyboard_ise(const sclchar *uuid)
1128 {
1129     if (m_initialized) {
1130         m_helper_agent.get_keyboard_ise(uuid);
1131     }
1132 }
1133
1134 void CSCLConnectionISF::set_selection(sclint start, sclint end)
1135 {
1136     if (m_initialized) {
1137         m_helper_agent.set_selection(start, end);
1138     }
1139 }
1140
1141 void CSCLConnectionISF::send_private_command(const sclchar *command)
1142 {
1143     if (m_initialized) {
1144         m_helper_agent.send_private_command(command);
1145     }
1146 }
1147
1148 void CSCLConnectionISF::get_selection_text(sclchar **text)
1149 {
1150     if (m_initialized) {
1151         scim::String selection_text;
1152         m_helper_agent.get_selection_text(selection_text);
1153
1154         if (text)
1155             *text = strdup(selection_text.c_str());
1156     }
1157 }
1158
1159 void CSCLConnectionISF::request_ise_hide()
1160 {
1161     if (m_initialized) {
1162         m_helper_agent.request_ise_hide();
1163     }
1164 }
1165
1166 extern "C"
1167 {
1168     EXAPI void scim_module_init(void) {
1169     }
1170
1171     EXAPI void scim_module_exit(void) {
1172     }
1173
1174     EXAPI void scim_helper_module_run_helper(const scim::String &uuid, const scim::ConfigPointer &config, const scim::String &display) {
1175         _scim_config = config;
1176         CSCLCoreImpl *impl = CSCLCoreImpl::get_instance();
1177         if (impl) {
1178             impl->on_run(uuid.c_str(), display.c_str());
1179         }
1180     }
1181 }