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