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