Fix memory leak detected by valgrind
[platform/core/uifw/ise-default.git] / src / ise.cpp
1 /*
2  * Copyright (c) 2012 - 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 <stdlib.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <vconf.h>
24 #include <Ecore.h>
25 #include <Ecore_IMF.h>
26 #include <Elementary.h>
27 #include <sclui.h>
28 #include <sclutils.h>
29 #include <inputmethod.h>
30 #include <inputmethod_internal.h>
31 #include <app_control.h>
32 #include <app_preference.h>
33 #include <ode/internal-encryption.h>
34 #include <notification.h>
35 #include "clipboard.h"
36 #include "autofill.h"
37 #include "ise.h"
38 #include "utils.h"
39 #include "option.h"
40 #include "languages.h"
41 #include "candidate-factory.h"
42 #include "ise-emoticon-mode.h"
43 #include "ise-emoticon-list.h"
44 #include "ise-stt-mode.h"
45 #include "ise-stt-option.h"
46 #include "ise-language-change.h"
47 #include "ise-tutorial-mode.h"
48 #include "modeindicator.h"
49 #include "w-input-smartreply.h"
50 #include "ise-floating-mode.h"
51 #include "ise-dbus.h"
52 #include "ise-sticker-mode.h"
53 #include "ise-nmt-mode.h"
54 #include "nmt.h"
55 #include "guidepopup.h"
56
57 #define EXIT_ISE_ON_HIDE 0
58 #define DEFER_ISE_CREATION 0
59
60 #define CANDIDATE_WINDOW_HEIGHT 84
61 using namespace scl;
62 #include <vector>
63 using namespace std;
64
65 static CSCLUI *g_ui = NULL;
66 CSCLUI* get_ui() { return g_ui; }
67
68 static int g_imdata_state = 0;
69
70 static sclboolean g_need_send_shift_event = FALSE;
71
72 extern void set_ise_imdata(const char * buf, size_t &len);
73 static void init_recent_used_punctuation();
74 static void update_recent_used_punctuation(const char *key_value);
75 static void set_ime_size(bool floating_mode, ISE_CANDIDATE_REQUEST candidate_req);
76 static sclboolean g_punctuation_popup_opened = FALSE;
77 static sclboolean g_popup_opened = FALSE;
78 static vector<string> g_recent_used_punctuation;
79 static const int MAX_DEFAULT_PUNCTUATION = 6;
80 static string g_default_punctuation[MAX_DEFAULT_PUNCTUATION] = {"-", "@", "'", "!", "?", ","};
81 static string g_current_punctuation[MAX_DEFAULT_PUNCTUATION-1] = {"RCENT1", "RCENT2", "RCENT3", "RCENT4", "RCENT5"};
82 static vector<string> g_softcandidate_string;
83 static bool g_input_panel_show = false;
84 static bool g_caps_mode_pending = false;
85 static bool g_floating_mode = false;
86 static bool g_candidate_more_view = false;
87 static bool g_ise_created = false;
88
89 static vector<string> g_lookup_table_strings;
90 static vector<string> g_smartreply_strings;
91 #if EXIT_ISE_ON_HIDE
92 static Ecore_Timer *exit_timer = NULL;
93 #endif
94
95 #define SOFT_CANDIDATE_DELETE_TIME (100.0/1000)
96 static Ecore_Timer *g_softcandidate_hide_timer = NULL;
97
98 static int g_ic = 0;
99 static int g_ic_smartreply = -1;
100
101 static KEYBOARD_STATE g_keyboard_state = {
102     0,
103     0,
104     ISE_LAYOUT_STYLE_NORMAL,
105     0,
106     FALSE,
107     TRUE,
108     FALSE,
109     "",
110     KEY_MODIFIER_NONE,
111     FALSE,
112     FALSE
113 };
114
115 KEYBOARD_STATE* get_keyboard_state() {
116     return &g_keyboard_state;
117 }
118
119 #define ISE_LAYOUT_NUMBERONLY_VARIATION_MAX 4
120 /*static const sclchar *_ise_numberonly_variation_name[ISE_LAYOUT_NUMBERONLY_VARIATION_MAX] = {
121     "DEFAULT", "SIG", "DEC", "SIGDEC"
122 };*/
123
124 #define SIG_DEC_SIZE        2
125 static scluint              _click_count = 0;
126 static const char          *_sig_dec[SIG_DEC_SIZE] = {".", "-"};
127 static scluint              _sig_dec_event[SIG_DEC_SIZE] = {'.', '-'};
128 static Ecore_Timer         *_commit_timer = NULL;
129
130 static sclu32               _context_layout = ISE_LAYOUT_STYLE_NORMAL;
131 static sclu32               _context_layout_variation = 0;
132
133 static Candidate           *g_candidate = NULL;
134 Candidate* get_candidate() {
135     return g_candidate;
136 }
137
138 static ISELanguageManager _language_manager;
139
140 class CandidateEventListener: public EventListener
141 {
142     public:
143         void on_event(const EventDesc &desc)
144         {
145             CSCLUI *ui = get_ui();
146             const MultiEventDesc &multidesc = dynamic_cast<const MultiEventDesc &>(desc);
147             LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
148
149             unsigned int smartreply_size = input_smartreply_get_reply_num();
150
151             switch (multidesc.type) {
152                 case MultiEventDesc::CANDIDATE_ITEM_MOUSE_DOWN:
153                     if (ime_autofill_get_exist()) {
154                         if (multidesc.index == 0) {
155                             string autofill_string = ime_autofill_get_string();
156                             ise_send_string(autofill_string.c_str());
157                         } else if (multidesc.index < (int)smartreply_size + 1) {
158                             ise_send_string(g_softcandidate_string[multidesc.index].c_str());
159                             ise_update_table(g_smartreply_strings);
160                         } else {
161                             if (info && info->load_in_ime)
162                                 ime_select_candidate(multidesc.index - smartreply_size - 1);
163                             else
164                                 engine_loader_select_candidate(multidesc.index - smartreply_size - 1);
165                         }
166                     } else {
167                         if (multidesc.index < (int)smartreply_size) {
168                             ise_send_string(g_softcandidate_string[multidesc.index].c_str());
169                             ise_update_table(g_smartreply_strings);
170                         }
171                         else {
172                             if (info && info->load_in_ime)
173                                 ime_select_candidate(multidesc.index - smartreply_size);
174                             else
175                                 engine_loader_select_candidate(multidesc.index - smartreply_size);
176                         }
177                     }
178                     break;
179                 case MultiEventDesc::CANDIDATE_MORE_VIEW_SHOW:
180                     // when more parts shows, click on the candidate will
181                     // not affect the key click event
182                     g_candidate_more_view = true;
183                     if (!g_input_panel_show)
184                         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
185                     if (ui)
186                         ui->disable_input_events(TRUE);
187                     break;
188                 case MultiEventDesc::CANDIDATE_MORE_VIEW_HIDE:
189                     g_candidate_more_view = false;
190                     if (!g_input_panel_show)
191                         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
192                     if (ui)
193                         ui->disable_input_events(FALSE);
194                     break;
195                 default: break;
196             }
197         }
198 };
199 static CandidateEventListener g_candidate_event_listener;
200
201 #define MVK_Shift_L 0xffe1
202 #define MVK_Caps_Lock 0xffe5
203 #define MVK_Shift_Off 0xffe1
204 #define MVK_Shift_On 0xffe2
205 #define MVK_Shift_Lock 0xffe6
206 #define MVK_Shift_Enable 0x9fe7
207 #define MVK_Shift_Disable 0x9fe8
208 #define MVK_space 0x020
209 #define MVK_Done 0xff0d
210
211 #define CM_KEY_LIST_SIZE         7
212 #define MULTITAP_TIMEOUT         3.0
213 #define USER_KEYSTRING_OPTION    "OPTION"
214 #define USER_KEYSTRING_EMOTICON  "EMOTICON_LAYOUT"
215 #define USER_KEYSTRING_CLIPBOARD "CLIPBOARD"
216 #define USER_KEYSTRING_VOICE     "STT_3X4"
217 #define USER_KEYSTRING_FLOATING  "FLOATING"
218 #define USER_KEYSTRING_STICKER   "STICKER_LAYOUT"
219 #define USER_KEYSTRING_TRANSLATION "TRANSLATION_LAYOUT"
220
221 #define USER_VOICE_LANGUAGE     "LANGUAGE"
222
223 static sclboolean           _cm_popup_opened = FALSE;
224 static const char          *_cm_key_list[CM_KEY_LIST_SIZE] = {USER_KEYSTRING_OPTION, USER_KEYSTRING_EMOTICON, USER_KEYSTRING_CLIPBOARD,
225                                                               USER_KEYSTRING_VOICE, USER_KEYSTRING_FLOATING, USER_KEYSTRING_STICKER, USER_KEYSTRING_TRANSLATION};
226 static scluint              _current_cm_key_id = 0;
227
228 /*
229  * This callback class will receive all response events from SCL
230  * So you should perform desired tasks in this class.
231  */
232 class CUIEventCallback : public ISCLUIEventCallback
233 {
234 public :
235     Ecore_Timer* word_timer = NULL;
236     static Eina_Bool _multi_tap_timer_cb(void *data)
237     {
238         LOGD("Time Out");
239         ise_send_event(MVK_Done, KEY_MASK_NULL);
240         return ECORE_CALLBACK_CANCEL;
241     }
242     SCLEventReturnType on_event_key_clicked(SclUIEventDesc event_desc);
243     SCLEventReturnType on_event_drag_state_changed(SclUIEventDesc event_desc);
244     SCLEventReturnType on_event_notification(SCLUINotiType noti_type, SclNotiDesc *etc_info);
245
246 private:
247     void on_event_string_key(CSCLUI *ui, SclUIEventDesc event_desc);
248     void on_event_control_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state);
249     void on_event_character_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state);
250     void on_event_modechange_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret);
251     void process_user_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret);
252 };
253
254 static CUIEventCallback callback;
255
256 int ise_get_imdata_state()
257 {
258     return g_imdata_state;
259 }
260
261 void ise_set_imdata_state(int state)
262 {
263     g_imdata_state = state;
264 }
265
266 static void update_candidate_table()
267 {
268     g_softcandidate_string.clear();
269     vector<string>::iterator iter;
270
271     g_smartreply_strings.clear();
272     input_smartreply_deinit();
273     g_ic_smartreply = -1;
274
275     // add autofill string
276     if (ime_autofill_get_exist())
277         g_softcandidate_string.push_back(ime_autofill_get_string());
278
279     // add lookup table string(s)
280     iter = g_lookup_table_strings.begin();
281     for (; iter != g_lookup_table_strings.end(); ++iter)
282     {
283         g_softcandidate_string.push_back(string(iter->c_str()));
284     }
285
286     ise_update_table(g_softcandidate_string);
287 }
288
289 static void _input_smartreply_notify_cb(void *user_data)
290 {
291     g_smartreply_strings.clear();
292     char *candidate = NULL;
293
294     if (input_smartreply_is_enabled()) {
295         /* Append newly added smartreply list */
296         int len = input_smartreply_get_reply_num();
297         if (len > 0) {
298             for (int i = 0; i < len; i++) {
299                 int type;
300                 char *reply = (char *)"";
301                 reply = input_smartreply_get_nth_item(i, &type);
302                 if (reply == NULL)
303                     continue;
304                 SECURE_LOGD("SmartReply = [%d] %s", i, reply);
305                 candidate = reply;
306                 if (candidate) {
307                     g_smartreply_strings.push_back(string(candidate));
308                     free(candidate);
309                     candidate = NULL;
310                 }
311             }
312             g_ic_smartreply = g_ic;
313
314             ise_app_candidate_show();
315             g_softcandidate_string = g_smartreply_strings;
316             ise_update_table(g_softcandidate_string);
317         }
318     }
319 }
320
321 sclboolean
322 check_ic_temporary(int ic)
323 {
324     if ((ic & 0xFFFF) == 0) {
325         return TRUE;
326     }
327     return FALSE;
328 }
329
330 static void _reset_shift_state(void)
331 {
332     CSCLUI *ui = get_ui();
333     if (ui) {
334         /* Reset all shift state variables */
335         SCLShiftState old_shift_state = ui->get_shift_state();
336         SCLShiftState new_shift_state = SCL_SHIFT_STATE_OFF;
337         if (old_shift_state != new_shift_state) {
338             g_need_send_shift_event = true;
339             ui->set_shift_state(new_shift_state);
340         }
341         LOGD("Shift state changed from (%d) to (%d)\n", (int)old_shift_state, (int)new_shift_state);
342     }
343 }
344
345 static void set_caps_mode(sclboolean mode) {
346     CSCLUI *ui = get_ui();
347     LOGD("mode : %d\n", mode);
348     if (ui) {
349         if (ui->get_shift_state() != SCL_SHIFT_STATE_LOCK) {
350             ui->set_shift_state(mode ? SCL_SHIFT_STATE_ON : SCL_SHIFT_STATE_OFF);
351             ui->set_autocapital_shift_state(!mode);
352
353             bool load_in_ime = false;
354             const sclchar *cur_lang = _language_manager.get_current_language();
355             if (cur_lang) {
356                 LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
357                 if (info)
358                     load_in_ime = info->load_in_ime;
359             }
360
361             if (load_in_ime)
362                 ime_send_imengine_event(mode ? MVK_Shift_On : MVK_Shift_Off, 0);
363             else
364                 engine_loader_send_imengine_event(mode ? MVK_Shift_On : MVK_Shift_Off, 0);
365         }
366     }
367 }
368
369 static void _reset_multitap_state(bool skip_commit = false)
370 {
371     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
372     if (!keyboard_state) return;
373
374     LOGD("keyboard_state.prev_modifier : %d", keyboard_state->prev_modifier);
375     if (keyboard_state->prev_modifier == KEY_MODIFIER_MULTITAP_START ||
376         keyboard_state->prev_modifier == KEY_MODIFIER_MULTITAP_REPEAT) {
377         if (!skip_commit) {
378             ise_send_string(keyboard_state->multitap_value.c_str());
379         }
380         ise_update_preedit_string("");
381     }
382     keyboard_state->multitap_value = "";
383     keyboard_state->prev_modifier = KEY_MODIFIER_NONE;
384     if (g_caps_mode_pending) {
385         g_caps_mode_pending = false;
386         set_caps_mode(keyboard_state->caps_mode);
387     }
388 }
389
390 static void ise_set_cm_private_key(scluint cm_key_id)
391 {
392     CSCLUI *ui = get_ui();
393     if (cm_key_id >= CM_KEY_LIST_SIZE || ui == NULL) {
394         LOGE("cm_key_id=%d\n", cm_key_id);
395         return;
396     }
397
398     if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_EMOTICON) == 0) {
399         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
400             const_cast<sclchar*>("icon/54x54/icon_emotion_nor.png"),
401             const_cast<sclchar*>("icon/54x54/icon_emotion_press.png"),
402             const_cast<sclchar*>("icon/54x54/icon_emotion_dim.png")};
403         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_EMOTICON), TRUE);
404     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_OPTION) == 0) {
405         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
406             const_cast<sclchar*>("icon/54x54/icon_setting_nor.png"),
407             const_cast<sclchar*>("icon/54x54/icon_setting_press.png"),
408             const_cast<sclchar*>("icon/54x54/icon_setting_dim.png")};
409         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_OPTION), TRUE);
410     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_CLIPBOARD) == 0) {
411         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
412             const_cast<sclchar*>("icon/54x54/icon_clipboard_nor.png"),
413             const_cast<sclchar*>("icon/54x54/icon_clipboard_press.png"),
414             const_cast<sclchar*>("icon/54x54/icon_clipboard_dim.png")};
415         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_CLIPBOARD), TRUE);
416     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_VOICE) == 0) {
417         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
418             const_cast<sclchar*>("icon/54x54/icon_mic_nor.png"),
419             const_cast<sclchar*>("icon/54x54/icon_mic_press.png"),
420             const_cast<sclchar*>("icon/54x54/icon_mic_dim.png")};
421         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_VOICE), TRUE);
422     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_FLOATING) == 0) {
423         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
424             const_cast<sclchar*>("icon/54x54/icon_floating_keypad_nor.png"),
425             const_cast<sclchar*>("icon/54x54/icon_floating_keypad_press.png"),
426             const_cast<sclchar*>("icon/54x54/icon_floating_keypad_dim.png")};
427         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_FLOATING), TRUE);
428     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_TRANSLATION) == 0) {
429         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
430             const_cast<sclchar*>("icon/54x54/icon_translation_nor.png"),
431             const_cast<sclchar*>("icon/54x54/icon_translation_press.png"),
432             const_cast<sclchar*>("icon/54x54/icon_translation_dim.png")};
433         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_TRANSLATION), TRUE);
434     } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_STICKER) == 0) {
435         sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
436             const_cast<sclchar*>("icon/54x54/icon_sticker_nor.png"),
437             const_cast<sclchar*>("icon/54x54/icon_sticker_press.png"),
438             const_cast<sclchar*>("icon/54x54/icon_sticker_dim.png")};
439         ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_STICKER), TRUE);
440     }
441 }
442
443 static void ise_update_space_key(void)
444 {
445     CSCLUI *ui = get_ui();
446     if (ui == NULL) {
447         LOGE("ui = NULL\n");
448         return;
449     }
450
451     scluint num = _language_manager.get_enabled_languages_num();
452     LOGD("language number: %d\n", num);
453     if (num <= 1) {
454         LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
455         if (info && info->enabled) {
456 #ifdef _WEARABLE
457             sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
458                 const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png"),
459                 const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png"),
460                 const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png")};
461             ui->set_private_key("SPACE_KEY", const_cast<sclchar*>(info->display_name.c_str()), imagelabel, NULL, 0, const_cast<sclchar*>("Space"), TRUE);
462 #else
463             ui->enable_button("SPACE_ARROW_LEFT", false);
464             ui->enable_button("SPACE_ARROW_RIGHT", false);
465 #endif
466         }
467     } else {
468 #ifdef _WEARABLE
469         ui->unset_private_key("SPACE_KEY");
470 #else
471         ui->enable_button("SPACE_ARROW_LEFT", true);
472         ui->enable_button("SPACE_ARROW_RIGHT", true);
473 #endif
474     }
475 }
476
477 static scluint ise_get_cm_key_id(const sclchar *key_value)
478 {
479     for (int i = 0; i < CM_KEY_LIST_SIZE; ++i) {
480         if (0 == strcmp (key_value, _cm_key_list[i]))
481             return i;
482     }
483     return 0;
484 }
485
486 static bool ise_is_emoticons_disabled(void)
487 {
488     bool ret = true;
489
490     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
491     if (!keyboard_state) return ret;
492
493     sclu32 current_layout = keyboard_state->layout;
494     LOGD("layout=%d\n", current_layout);
495
496     if ((current_layout == ISE_LAYOUT_STYLE_NORMAL) ||
497         (current_layout == ISE_LAYOUT_STYLE_NUMBER) ||
498         (current_layout == ISE_LAYOUT_STYLE_EMOTICON))
499         ret = false;
500
501     if (g_imdata_state & IMDATA_ACTION_DISABLE_EMOTICONS)
502         ret = true;
503
504     return ret;
505 }
506
507 static Eina_Bool softcandidate_hide_timer_callback(void *data)
508 {
509     LOGD("Enter\n");
510     set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_HIDE);
511
512     Candidate *candidate = get_candidate();
513     if (candidate) {
514         candidate->hide();
515     }
516     return ECORE_CALLBACK_CANCEL;
517 }
518
519 static void delete_softcandidate_hide_timer(void)
520 {
521     if (g_softcandidate_hide_timer) {
522         ecore_timer_del(g_softcandidate_hide_timer);
523         g_softcandidate_hide_timer = NULL;
524     }
525 }
526
527 static void add_softcandidate_hide_timer(void)
528 {
529     delete_softcandidate_hide_timer();
530     g_softcandidate_hide_timer = ecore_timer_add(SOFT_CANDIDATE_DELETE_TIME, softcandidate_hide_timer_callback, NULL);
531 }
532
533 static void create_softcandidate(void)
534 {
535     if (!g_candidate) {
536         g_candidate = CandidateFactory::make_candidate(CANDIDATE_MULTILINE, ime_get_main_window());
537         if (g_candidate) {
538             g_candidate->add_event_listener(&g_candidate_event_listener);
539         }
540     }
541 }
542
543 /**
544  * Send the given string to input framework
545  */
546 void
547 ise_send_string(const sclchar *key_value)
548 {
549     int ic = -1;
550     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
551     if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
552         ic = keyboard_state->ic;
553     }
554     ime_commit_string(key_value);
555     LOGD("ic : %x, %s\n", ic, key_value);
556 }
557
558 /**
559 * Send the preedit string to input framework
560 */
561 void
562 ise_update_preedit_string(const sclchar *str, const sclboolean underline)
563 {
564     int ic = -1;
565     Eina_List *attr_list = NULL;
566     ime_preedit_attribute *preedit_attr;
567
568     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
569     if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
570         ic = keyboard_state->ic;
571     }
572     if (underline) {
573         /* Count UTF-8 string length */
574         int len = 0;
575         const sclchar *s = str;
576         while (*s) len += (*s++ & 0xc0) != 0x80;
577
578         preedit_attr = (ime_preedit_attribute *)calloc(1, sizeof(ime_preedit_attribute));
579         if (preedit_attr) {
580             preedit_attr->start = 0;
581             preedit_attr->length = len;
582             preedit_attr->type = IME_ATTR_FONTSTYLE;
583             preedit_attr->value = IME_ATTR_FONTSTYLE_UNDERLINE;
584             attr_list = eina_list_append(attr_list, (void *)preedit_attr);
585         }
586
587         ime_update_preedit_string(str, attr_list);
588     } else {
589         ime_update_preedit_string(str, NULL);
590     }
591     LOGD("ic : %x, %s\n", ic, str);
592 }
593
594 /**
595 * Send the given event to input framework
596 */
597 void
598 ise_send_event(sclulong key_event, sclulong key_mask)
599 {
600     int ic = -1;
601     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
602     if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
603         ic = keyboard_state->ic;
604     }
605     ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_PRESSED, false);
606     ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_RELEASED, false);
607
608     LOGD("ic : %x, %lx\n", (unsigned int)ic, key_event);
609 }
610
611 /**
612 * Forward the given event to input framework
613 */
614 void
615 ise_forward_key_event(sclulong key_event)
616 {
617     int ic = -1;
618     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
619     if (!check_ic_temporary(keyboard_state->ic)) {
620         ic = keyboard_state->ic;
621     }
622     ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_PRESSED, true);
623     ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_RELEASED, true);
624
625     LOGD("ic : %x, %lx\n", (unsigned int)ic, key_event);
626 }
627
628 /**
629  * @brief Delete commit timer.
630  *
631  * @return void
632  */
633 static void delete_commit_timer(void)
634 {
635     if (_commit_timer != NULL) {
636         ecore_timer_del(_commit_timer);
637         _commit_timer = NULL;
638     }
639 }
640
641 /**
642  * @brief Callback function for commit timer.
643  *
644  * @param data Data to pass when it is called.
645  *
646  * @return ECORE_CALLBACK_CANCEL
647  */
648 static Eina_Bool commit_timeout(void *data)
649 {
650     if (_commit_timer != NULL) {
651         ime_hide_preedit_string();
652         ise_forward_key_event(_sig_dec_event[(_click_count-1)%SIG_DEC_SIZE]);
653         _click_count = 0;
654     }
655     _commit_timer = NULL;
656     return ECORE_CALLBACK_CANCEL;
657 }
658
659 static sclboolean
660 on_input_mode_changed(const sclchar *key_value, sclulong key_event, sclint key_type)
661 {
662     sclboolean ret = FALSE;
663
664     CSCLUI *ui = get_ui();
665     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
666     if (ui && keyboard_state) {
667         if (key_value) {
668             SECURE_LOGD("key_value : %s\n", key_value);
669             if (strcmp(key_value, "CUR_LANG") == 0) {
670                 keyboard_state->disable_force_latin = TRUE;
671                 ret = _language_manager.select_current_language();
672             }
673             if (strcmp(key_value, "NEXT_LANG") == 0) {
674                 keyboard_state->disable_force_latin = TRUE;
675                 ret = _language_manager.select_next_language();
676             }
677         }
678
679         const sclchar *cur_lang = _language_manager.get_current_language();
680         if (cur_lang) {
681             LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
682             if (info) {
683                 if (info->accepts_caps_mode) {
684                     if (info->load_in_ime)
685                         ime_send_imengine_event(MVK_Shift_Enable, 0);
686                     else
687                         engine_loader_send_imengine_event(MVK_Shift_Enable, 0);
688
689                     set_caps_mode(keyboard_state->caps_mode);
690                 } else {
691                     if (info->load_in_ime)
692                         ime_send_imengine_event(MVK_Shift_Disable, 0);
693                     else
694                         engine_loader_send_imengine_event(MVK_Shift_Disable, 0);
695
696                     ui->set_shift_state(SCL_SHIFT_STATE_OFF);
697                 }
698             }
699         }
700
701         if (ise_sticker_is_show())
702             ise_sticker_destroy_layout();
703
704         if (ise_emoticon_is_show()) {
705             ise_emoticon_destroy_layout();
706         }
707
708         if (ise_nmt_is_show()) {
709             ise_nmt_destroy_layout();
710         }
711
712         if (key_value) {
713             if (!strcmp(key_value, USER_KEYSTRING_EMOTICON)) {
714                 ise_emoticon_init_list();
715 #ifdef _WEARABLE
716                     ise_emoticon_set_current_group(EMOTICON_GROUP_1);
717 #else
718                 if (ise_emoticon_get_recent_list_size() == 0)
719                     ise_emoticon_set_current_group(EMOTICON_GROUP_1);
720                 else
721                     ise_emoticon_set_current_group(EMOTICON_GROUP_RECENTLY_USED);
722 #endif
723                 SCLRotation rotation = ui->get_rotation();
724                 ise_emoticon_show_layout(ise_emoticon_get_current_group(), ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
725             }
726             else if (!strcmp(key_value, USER_KEYSTRING_TRANSLATION)) {
727                 LOGD("");
728                 SCLRotation rotation = ui->get_rotation();
729                 ise_nmt_show_layout(ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
730             }
731         }
732     }
733
734     return ret;
735 }
736
737 SCLEventReturnType CUIEventCallback::on_event_notification(SCLUINotiType noti_type, SclNotiDesc *etc_info)
738 {
739     CSCLUI *ui = get_ui();
740     SCLEventReturnType ret = SCL_EVENT_PASS_ON;
741     LOGD("noti type: %d, g_need_send_shift_event: %d\n", noti_type, g_need_send_shift_event);
742
743     if (noti_type == SCL_UINOTITYPE_SHIFT_STATE_CHANGE) {
744         if (g_need_send_shift_event) {
745             const sclchar *cur_lang = _language_manager.get_current_language();
746             if (cur_lang) {
747                 LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
748                 SclNotiShiftStateChangeDesc *desc = static_cast<SclNotiShiftStateChangeDesc*>(etc_info);
749                 if (info && desc) {
750                     if (info->accepts_caps_mode) {
751                         LOGD("shift state: %d\n", desc->shift_state);
752                         if (desc->shift_state == SCL_SHIFT_STATE_OFF) {
753                             if (info->load_in_ime)
754                                 ime_send_imengine_event(MVK_Shift_Off, 0);
755                             else
756                                 engine_loader_send_imengine_event(MVK_Shift_Off, 0);
757                         } else if (desc->shift_state == SCL_SHIFT_STATE_ON) {
758                             if (info->load_in_ime)
759                                 ime_send_imengine_event(MVK_Shift_On, 0);
760                             else
761                                 engine_loader_send_imengine_event(MVK_Shift_On, 0);
762                         } else if (desc->shift_state == SCL_SHIFT_STATE_LOCK) {
763                             if (info->load_in_ime)
764                                 ime_send_imengine_event(MVK_Shift_Lock, 0);
765                             else
766                                 engine_loader_send_imengine_event(MVK_Shift_Lock, 0);
767                         }
768                         ret = SCL_EVENT_PASS_ON;
769                     }
770                 }
771             }
772             g_need_send_shift_event = FALSE;
773         }
774     } else if (noti_type == SCL_UINOTITYPE_POPUP_OPENING) {
775         vector<string>::reverse_iterator iter = g_recent_used_punctuation.rbegin();
776         int punc_pos = 0;
777         for (; iter != g_recent_used_punctuation.rend(); ++iter)
778         {
779             if (ui)
780                 ui->set_string_substitution(g_current_punctuation[punc_pos].c_str(), iter->c_str());
781             punc_pos++;
782         }
783         SclNotiPopupOpeningDesc *openingDesc = (SclNotiPopupOpeningDesc *)etc_info;
784         if (ui && 0 == strcmp(openingDesc->input_mode, "CM_POPUP")) {
785             if (!ise_sticker_check_sticker_exists())
786                 ui->enable_button("STICKER_KEY", EINA_FALSE);
787
788             if (nmt_is_disabled())
789                 ui->enable_button("TRANSLATION_KEY", false);
790             else
791                 ui->enable_button("TRANSLATION_KEY", true);
792
793             if (ise_is_emoticons_disabled())
794                 ui->enable_button("EMOTICON_KEY", false);
795             else
796                 ui->enable_button("EMOTICON_KEY", true);
797         }
798     } else if (noti_type == SCL_UINOTITYPE_POPUP_OPENED) {
799         g_popup_opened = TRUE;
800         SclNotiPopupOpenedDesc *openedDesc = (SclNotiPopupOpenedDesc *)etc_info;
801         if (0 == strcmp(openedDesc->input_mode, "PUNCTUATION_POPUP")) {
802             g_punctuation_popup_opened = TRUE;
803         } else if (0 == strcmp(openedDesc->input_mode, "CM_POPUP")) {
804             _cm_popup_opened = TRUE;
805         }
806     } else if (noti_type == SCL_UINOTITYPE_POPUP_CLOSED) {
807         g_popup_opened = FALSE;
808         SclNotiPopupClosedDesc *closedDesc = (SclNotiPopupClosedDesc *)etc_info;
809         if (closedDesc && closedDesc->input_mode) {
810             if (0 == strcmp(closedDesc->input_mode, "PUNCTUATION_POPUP")) {
811                 g_punctuation_popup_opened = FALSE;
812             } else if (0 == strcmp(closedDesc->input_mode, "CM_POPUP")) {
813                 _cm_popup_opened = FALSE;
814             }
815         }
816     } else if (noti_type == SCL_UINOTITYPE_INPUT_MODE_CHANGE) {
817         SclNotiInputModeChangeDesc *desc = static_cast<SclNotiInputModeChangeDesc*>(etc_info);
818         if (desc && ui) {
819             set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
820
821             const char *input_mode = ui->get_input_mode();
822             if (input_mode) {
823                 if (strcmp(input_mode, "STT_3X4") == 0 &&
824                         strcmp(desc->input_mode, "STT_3X4") != 0) {
825                     ise_hide_stt_mode();
826                 }
827                 if (strcmp(input_mode, "STT_3X4") != 0 &&
828                         strcmp(desc->input_mode, "STT_3X4") == 0) {
829                     ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
830                 }
831             }
832         }
833     }
834
835     return ret;
836 }
837
838 bool get_landscape_device(int degree)
839 {
840     sclint width = 0;
841     sclint height = 0;
842     bool landscape_device = false;
843
844     CSCLUI *ui = get_ui();
845     if (!ui) return false;
846
847     ui->get_screen_resolution(&width, &height);
848
849     if (degree == 0 || degree == 180) {
850         if (width > height)
851             landscape_device = true;
852     }
853     else {
854         if (width < height)
855             landscape_device = true;
856     }
857
858     return landscape_device;
859 }
860
861 SCLEventReturnType CUIEventCallback::on_event_drag_state_changed(SclUIEventDesc event_desc)
862 {
863     SECURE_LOGD("button %s is clicked\n", event_desc.key_value);
864     if (event_desc.event_type == EVENT_TYPE_MOVE) {
865         if (event_desc.key_event == MVK_space) {
866             SclRectangle rectangle = {0};
867             CSCLUI *ui = get_ui();
868             if (ui) ui->get_button_geometry("SPACE_KEY", &rectangle);
869             ise_show_space_flick_language_change_popup(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
870         }
871     } else if (event_desc.event_type == EVENT_TYPE_RELEASE) {
872         if (event_desc.key_event == MVK_space) {
873             ise_destroy_space_flick_language_change_popup();
874         }
875
876         if (g_floating_mode)
877             ime_set_floating_drag_end();
878     } else if (event_desc.event_type == EVENT_TYPE_PRESS) {
879 #if defined(_MOBILE) || defined(_COMMON)
880         CONFIG_VALUES *config_values = get_config_values();
881
882         if (config_values && (!config_values->first_guideset) && event_desc.key_value && (strncmp(event_desc.key_value, "OPTION", strlen(event_desc.key_value)) == 0)) {
883             ise_show_help_popup(event_desc.key_event, NATIVE_WINDOW_CAST(ime_get_main_window()));//show help popup in on_event_drag_state_changed other than on_event_key_clicked for fixing help popup show delay issue
884             LOGD("setting popup show\n");
885             return SCL_EVENT_PASS_ON;
886         } else {
887             ise_destroy_popup_setting();//destroy popup manually in case of abnormal situation
888         }
889 #endif
890         if (g_floating_mode && event_desc.mouse_current_point.y <= FLOATING_TITLE_BAR_HEIGHT
891             && event_desc.mouse_current_point.y >= 0)
892             ime_set_floating_drag_start();
893     }
894     return SCL_EVENT_PASS_ON;
895 }
896
897 static void launch_option()
898 {
899     app_control_h app_control;
900     int ret = app_control_create(&app_control);
901     if (ret != APP_CONTROL_ERROR_NONE) {
902         LOGW("app_control_create returned %d\n", ret);
903         return;
904     }
905
906     ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
907     if (ret != APP_CONTROL_ERROR_NONE) {
908         LOGW("app_control_set_operation returned %d\n", ret);
909         goto end;
910     }
911
912     ret = app_control_set_app_id(app_control, "org.tizen.ise-default-setting");
913     if (ret != APP_CONTROL_ERROR_NONE) {
914         LOGW("app_control_set_app_id returned %d\n", ret);
915         goto end;
916     }
917
918     app_control_add_extra_data(app_control, "caller", "ise-default");
919     app_control_set_launch_mode(app_control, APP_CONTROL_LAUNCH_MODE_GROUP);
920
921     ret = app_control_send_launch_request(app_control, NULL, NULL);
922     if (ret != APP_CONTROL_ERROR_NONE) {
923         goto end;
924     }
925
926 end:
927     if (app_control)
928         app_control_destroy(app_control);
929 }
930
931 void CUIEventCallback::on_event_string_key(CSCLUI *ui, SclUIEventDesc event_desc)
932 {
933     if (event_desc.key_modifier != KEY_MODIFIER_MULTITAP_START &&
934         event_desc.key_modifier != KEY_MODIFIER_MULTITAP_REPEAT) {
935         if (event_desc.key_event) {
936             ise_forward_key_event(event_desc.key_event);
937         } else {
938             ise_send_string(event_desc.key_value);
939         }
940     }
941
942     if (!g_popup_opened) {
943         const sclchar *input_mode = ui->get_input_mode();
944         if (input_mode && ((0 == strcmp(input_mode, "SYM_QTY_1")) || (0 == strcmp(input_mode, "SYM_QTY_2")))) {
945             update_recent_used_punctuation(event_desc.key_value);
946         }
947     } else if (g_punctuation_popup_opened) {
948         update_recent_used_punctuation(event_desc.key_value);
949     }
950 }
951
952 void CUIEventCallback::on_event_control_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state)
953 {
954     commit_timeout(NULL);
955
956     const char *long_shift = "LongShift";
957     const char *caps_lock = "CapsLock";
958     const char *delete_all = "DeleteAll";
959     const char *hide_panel = "Hide";
960
961     if (strncmp(event_desc.key_value, long_shift, strlen(long_shift)) == 0) {
962         LOGD("shift key is longpress\n");
963         ui->set_shift_state(SCL_SHIFT_STATE_ON);
964         g_need_send_shift_event = TRUE;
965         //ise_send_event (MVK_Shift_Lock, KEY_MASK_NULL);
966     } else if (strncmp(event_desc.key_value, caps_lock, strlen(caps_lock)) == 0) {
967         bool load_in_ime = false;
968         const sclchar *cur_lang = _language_manager.get_current_language();
969         if (cur_lang) {
970             LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
971             if (info)
972                 load_in_ime = info->load_in_ime;
973         }
974
975         if (ui->get_shift_state() != SCL_SHIFT_STATE_LOCK) {
976             ui->set_shift_state(SCL_SHIFT_STATE_LOCK);
977             if (load_in_ime)
978                 ime_send_imengine_event(MVK_Shift_Lock, 0);
979             else
980                 engine_loader_send_imengine_event(MVK_Shift_Lock, 0);
981         } else {
982             ui->set_shift_state(SCL_SHIFT_STATE_OFF);
983             if (load_in_ime)
984                 ime_send_imengine_event(MVK_Shift_Off, 0);
985             else
986                 engine_loader_send_imengine_event(MVK_Shift_Off, 0);
987         }
988         //g_need_send_shift_event = TRUE;
989     } else if (strncmp(event_desc.key_value, delete_all, strlen(delete_all)) == 0) {
990         ime_delete_surrounding_text((INT_MAX / 2) * -1, INT_MAX);
991     } else if (strncmp(event_desc.key_value, hide_panel, strlen(hide_panel)) == 0) {
992         ise_hide();
993         ime_request_hide();
994     } else if (event_desc.key_event) {
995         if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
996             ise_stt_stop();
997         }
998 #ifdef _WEARABLE
999         if (event_desc.key_event == MVK_Done) {
1000             LOGD("ENTER");
1001             //commit the preedit string first
1002             ise_send_event(event_desc.key_event, KEY_MASK_NULL);
1003         }
1004 #endif
1005         ise_send_event(event_desc.key_event, KEY_MASK_NULL);
1006         if (event_desc.key_event == MVK_Shift_L) {
1007             g_need_send_shift_event = TRUE;
1008         }
1009     }
1010 }
1011
1012 void CUIEventCallback::on_event_character_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state)
1013 {
1014     sclboolean need_forward = FALSE;
1015     // FIXME : Should decide when to forward key events
1016     const sclchar *input_mode = ui->get_input_mode();
1017     if (input_mode) {
1018         if (strcmp(input_mode, "SYM_QTY_1") == 0 ||
1019             strcmp(input_mode, "SYM_QTY_2") == 0 ||
1020             strcmp(input_mode, "PHONE_3X4") == 0 ||
1021             strcmp(input_mode, "IPv6_3X4_123") == 0 ||
1022             strcmp(input_mode, "IPv6_3X4_ABC") == 0 ||
1023             strcmp(input_mode, "NUMONLY_3X4") == 0 ||
1024             strcmp(input_mode, "NUMONLY_3X4_SIG") == 0 ||
1025             strcmp(input_mode, "NUMONLY_3X4_DEC") == 0 ||
1026             strcmp(input_mode, "NUMONLY_3X4_SIGDEC") == 0 ||
1027             strcmp(input_mode, "DATETIME_3X4") == 0) {
1028             need_forward = TRUE;
1029         }
1030     }
1031     if (input_mode && strcmp (input_mode, "NUMONLY_3X4_SIGDEC") == 0 &&
1032         strcmp(event_desc.key_value, ".") == 0) {
1033         ime_update_preedit_string(_sig_dec[_click_count%SIG_DEC_SIZE], NULL);
1034         ime_show_preedit_string();
1035         delete_commit_timer();
1036         _commit_timer = ecore_timer_add(1.0, commit_timeout, NULL);
1037         _click_count++;
1038     } else if (event_desc.key_event) {
1039         commit_timeout(NULL);
1040         if (need_forward) {
1041             ise_forward_key_event(event_desc.key_event);
1042         } else {
1043             if (_cm_popup_opened) {
1044                 static sclchar current_cm_symbol[2] = {'\0'};
1045                 sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
1046                     const_cast<sclchar*>(" "),
1047                     const_cast<sclchar*>(" "),
1048                     const_cast<sclchar*>(" ")};
1049                 if (event_desc.key_value) {
1050                     current_cm_symbol[0] = event_desc.key_value[0];
1051                     current_cm_symbol[1] = '\0';
1052                 }
1053                 ui->set_private_key("CM_KEY",
1054                         const_cast<sclchar*>(current_cm_symbol),
1055                         imagelabel, NULL, 0,
1056                         const_cast<sclchar*>(current_cm_symbol), TRUE);
1057
1058                 _cm_popup_opened = FALSE;
1059                 _current_cm_key_id = -1;
1060             }
1061 #ifdef _WEARABLE
1062             /*
1063              * change the keyboard mode of GLM from QWERTY to KEYPAD
1064              * ensure the Number keypad won't be affected
1065              */
1066             if (keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBER) {
1067                 ime_send_key_event(IME_KEY_Print, IME_KEY_MASK_CONTROL, false);
1068             } else {
1069                 Candidate *candidate = get_candidate();
1070                 if (word_timer == NULL && (!candidate || !candidate->get_visible())) {
1071                     word_timer = ecore_timer_add(MULTITAP_TIMEOUT, _multi_tap_timer_cb, NULL);
1072                 }
1073                 ime_send_key_event(IME_KEY_Select, IME_KEY_MASK_CONTROL, false);
1074             }
1075 #endif
1076             ise_send_event(event_desc.key_event, KEY_MASK_NULL);
1077         }
1078     }
1079
1080     if (input_mode) {
1081         if ((strcmp(input_mode, "SYM_QTY_1") == 0) || (0 == strcmp(input_mode, "SYM_QTY_2"))) {
1082             update_recent_used_punctuation(event_desc.key_value);
1083         }
1084     }
1085 }
1086
1087 void CUIEventCallback::on_event_modechange_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret)
1088 {
1089 #if defined(_MOBILE) || defined(_COMMON)
1090     if (guide_popup_get_visible()) {//popup is showing
1091         return;
1092     }
1093 #endif
1094
1095     if (strcmp(event_desc.key_value, USER_VOICE_LANGUAGE) == 0) {
1096         if (!get_setting_window_open_status()) {
1097             ise_hide_stt_mode();
1098 #ifdef _WEARABLE
1099             hide_indicator_window();
1100 #endif
1101             create_setting_window();
1102         }
1103     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_VOICE) == 0) {
1104         keyboard_state->layout = ISE_LAYOUT_STYLE_VOICE;
1105         ui->set_input_mode("STT_3X4");
1106
1107     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0) {
1108         launch_option();
1109
1110         ret = SCL_EVENT_DONE;
1111     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_CLIPBOARD) == 0) {
1112         clipboard_show();
1113
1114         ret = SCL_EVENT_DONE;
1115     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_FLOATING) == 0) {
1116         CONFIG_VALUES *config_values = get_config_values();
1117
1118         if (g_floating_mode) {
1119             if (config_values) {
1120                 config_values->floating_mode = false;
1121             }
1122         } else {
1123             if (config_values) {
1124                 config_values->floating_mode = true;
1125             }
1126         }
1127         write_ise_config_values();
1128
1129         ret = SCL_EVENT_DONE;
1130     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0) {
1131         SCLRotation rotation = ui->get_rotation();
1132         ise_sticker_show_layout(STICKER_GROUP_RECENTLY_USED, ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
1133     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
1134         if (ise_sticker_is_show())
1135             ise_sticker_destroy_layout();
1136
1137         SCLRotation rotation = ui->get_rotation();
1138         ise_nmt_show_layout(ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
1139     } else if (on_input_mode_changed(event_desc.key_value, event_desc.key_event, event_desc.key_type)) {
1140         ret = SCL_EVENT_DONE;
1141     }
1142
1143     if (_cm_popup_opened) {
1144         if (strcmp(event_desc.key_value, USER_KEYSTRING_EMOTICON) == 0 ||
1145             strcmp(event_desc.key_value, USER_KEYSTRING_VOICE) == 0 ||
1146             strcmp(event_desc.key_value, USER_KEYSTRING_FLOATING) == 0 ||
1147             strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0 ||
1148             strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
1149             scluint id = ise_get_cm_key_id(event_desc.key_value);
1150             if (id != _current_cm_key_id) {
1151                 _current_cm_key_id = id;
1152                 ise_set_cm_private_key(_current_cm_key_id);
1153             }
1154         }
1155         _cm_popup_opened = FALSE;
1156     }
1157
1158     if (strlen(event_desc.key_value) == 1) {
1159         const char allowed_chars_in_modechange[] = {
1160             ',', '?', '!', '`', '~', '@', '-'
1161         };
1162         const int allowed_chars_in_modechange_num =
1163             sizeof(allowed_chars_in_modechange) / sizeof(char);
1164         for (unsigned int loop = 0;loop < sizeof(allowed_chars_in_modechange_num);loop++) {
1165             if (*(event_desc.key_value) == allowed_chars_in_modechange[loop]) {
1166                 ise_send_string(event_desc.key_value);
1167             }
1168         }
1169     }
1170 }
1171
1172 void CUIEventCallback::process_user_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret)
1173 {
1174     if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0) {
1175         //open_option_window(NULL, ROTATION_TO_DEGREE(ui->get_rotation()));
1176         launch_option();
1177
1178         ret = SCL_EVENT_DONE;
1179     } else if (strcmp(event_desc.key_value, USER_KEYSTRING_CLIPBOARD) == 0) {
1180         clipboard_show();
1181         ret = SCL_EVENT_DONE;
1182     } else if (strcmp(event_desc.key_value, "Cancel") == 0) {
1183         ret = SCL_EVENT_DONE;
1184         const sclchar *input_mode = ui->get_input_mode();
1185         if (input_mode && strcmp(input_mode, "STT_3X4") == 0 ) {
1186             keyboard_state->need_reset = TRUE;
1187             voice_result_string_flush();
1188             ise_set_layout(keyboard_state->layout, keyboard_state->layout_variation);
1189             if (keyboard_state->visible_state)
1190                 ise_show(keyboard_state->ic);
1191         }
1192 #ifdef _TV
1193         ise_send_event(IME_KEY_Cancel, KEY_MASK_NULL);
1194         ime_request_hide();
1195 #endif
1196     }  else if (strcmp(event_desc.key_value, "Done") == 0) {
1197 #ifdef _TV
1198         ret = SCL_EVENT_DONE;
1199         ise_send_event(IME_KEY_Return, KEY_MASK_NULL);
1200         ime_request_hide();
1201 #endif
1202     } else if (strcmp(event_desc.key_value, "Translate") == 0) {
1203         ise_nmt_translate();
1204     }  else {
1205         const sclchar *input_mode = ui->get_input_mode();
1206         if ((NULL != input_mode) && (!strcmp(input_mode, "EMOTICON_LAYOUT"))) {
1207             if (ise_emoticon_is_show()) {
1208                 ise_emoticon_destroy_layout();
1209             }
1210 #ifdef _WEARABLE
1211             emoticon_group_t group_id = EMOTICON_GROUP_1;
1212             if (ise_emoticon_get_current_group() < EMOTICON_GROUP_3)
1213                 group_id = (emoticon_group_t)(ise_emoticon_get_current_group() + 1);
1214
1215             ise_set_emoticon_label(group_id);
1216 #else
1217             emoticon_group_t group_id = ise_emoticon_get_group_id(event_desc.key_value);
1218 #endif
1219             if ((group_id >= 0) && (group_id < MAX_EMOTICON_GROUP)) {
1220                 SCLRotation rotation = ui->get_rotation();
1221                 ise_emoticon_show_layout(group_id, ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
1222             }
1223         }
1224
1225         if (input_mode && (!strcmp(input_mode, "STICKER_LAYOUT"))) {
1226             if (ise_sticker_is_show())
1227                 ise_sticker_change_group(ise_sticker_get_group_id(event_desc.key_value));
1228         }
1229     }
1230
1231     if (_cm_popup_opened) {
1232         if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0 ||
1233             strcmp(event_desc.key_value, USER_KEYSTRING_CLIPBOARD) == 0 ||
1234             strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0 ||
1235             strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
1236             scluint id = ise_get_cm_key_id(event_desc.key_value);
1237             if (id != _current_cm_key_id) {
1238                 _current_cm_key_id = id;
1239                 ise_set_cm_private_key(_current_cm_key_id);
1240             }
1241         }
1242         _cm_popup_opened = FALSE;
1243     }
1244 }
1245
1246 SCLEventReturnType CUIEventCallback::on_event_key_clicked(SclUIEventDesc event_desc)
1247 {
1248     SCLEventReturnType ret = SCL_EVENT_PASS_ON;
1249     if (word_timer != NULL) {
1250         ecore_timer_del(word_timer);
1251         word_timer = NULL;
1252     }
1253
1254     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1255     if (!keyboard_state) return ret;
1256
1257     if (event_desc.key_modifier == KEY_MODIFIER_MULTITAP_START) {
1258         if (!keyboard_state->multitap_value.empty()) {
1259             ise_send_string(keyboard_state->multitap_value.c_str());
1260         }
1261         ise_update_preedit_string(event_desc.key_value);
1262         keyboard_state->multitap_value = event_desc.key_value;
1263     } else if (event_desc.key_modifier == KEY_MODIFIER_MULTITAP_REPEAT) {
1264         ise_update_preedit_string(event_desc.key_value);
1265         keyboard_state->multitap_value = event_desc.key_value;
1266     } else {
1267         _reset_multitap_state();
1268     }
1269     keyboard_state->prev_modifier = event_desc.key_modifier;
1270
1271     CSCLUI *ui = get_ui();
1272     if (ui) {
1273         switch (event_desc.key_type) {
1274         case KEY_TYPE_STRING:
1275             on_event_string_key(ui, event_desc);
1276             break;
1277         case KEY_TYPE_CHAR:
1278             on_event_character_key(ui, event_desc, keyboard_state);
1279             break;
1280         case KEY_TYPE_CONTROL:
1281             on_event_control_key(ui, event_desc, keyboard_state);
1282             break;
1283         case KEY_TYPE_MODECHANGE:
1284             on_event_modechange_key(ui, event_desc, keyboard_state, ret);
1285             break;
1286         case KEY_TYPE_USER:
1287             process_user_key(ui, event_desc, keyboard_state, ret);
1288             break;
1289         default:
1290             break;
1291         }
1292     }
1293
1294     return ret;
1295 }
1296
1297 void
1298 ise_set_layout(sclu32 layout, sclu32 layout_variation)
1299 {
1300     /* Check if the layoutIdx is in the valid range */
1301     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1302     if (layout < ISE_LAYOUT_STYLE_MAX) {
1303         if (keyboard_state->layout != layout ||
1304             keyboard_state->layout_variation != layout_variation) {
1305             keyboard_state->need_reset = TRUE;
1306         }
1307         keyboard_state->layout = layout;
1308         keyboard_state->layout_variation = layout_variation;
1309         LOGD("layout:%d, variation:%d\n", keyboard_state->layout, keyboard_state->layout_variation);
1310     }
1311 }
1312
1313 void
1314 ise_reset_context()
1315 {
1316     LOGD("");
1317     _reset_multitap_state(true);
1318     CONFIG_VALUES *config_values = get_config_values();
1319     if (config_values) {
1320         _language_manager.reset_language(config_values->selected_language.c_str());
1321     }
1322 }
1323
1324 void
1325 ise_reset_input_context()
1326 {
1327     LOGD("");
1328     _reset_multitap_state(true);
1329     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1330     if (keyboard_state) keyboard_state->disable_force_latin = FALSE;
1331     CONFIG_VALUES *config_values = get_config_values();
1332     if (config_values) {
1333         _language_manager.reset_language(config_values->selected_language.c_str());
1334     }
1335 }
1336
1337 void
1338 ise_focus_in(int ic)
1339 {
1340     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1341     if (!keyboard_state) return;
1342
1343     LOGD("ic : %x , %x , g_ic : %x , %x, g_focused_ic : %x , %x\n", ic, check_ic_temporary(ic),
1344             keyboard_state->ic, check_ic_temporary(keyboard_state->ic),
1345             keyboard_state->focused_ic, check_ic_temporary(keyboard_state->focused_ic));
1346     if (check_ic_temporary(keyboard_state->ic) && !check_ic_temporary(ic)) {
1347         keyboard_state->ic = ic;
1348     }
1349     keyboard_state->focused_ic = ic;
1350 }
1351
1352 static void save_autofill_data()
1353 {
1354     char *text = NULL;
1355     int cursor;
1356
1357     if (ime_autofill_get_hint() == 0)
1358         return;
1359
1360     ime_get_surrounding_text(-1, -1, &text, &cursor);
1361     SECURE_LOGD("surrounding text : %s\n", text);
1362     if (!text) return;
1363
1364     ime_autofill_save_string(text);
1365
1366     free(text);
1367 }
1368
1369 void
1370 ise_focus_out(int ic)
1371 {
1372     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1373     if (keyboard_state) keyboard_state->focused_ic = 0;
1374     _reset_multitap_state(true);
1375
1376     save_autofill_data();
1377 }
1378
1379 bool ise_is_guideline_popup_enable_layout(void)
1380 {
1381     bool ret;
1382     switch (_context_layout)
1383     {
1384         case ISE_LAYOUT_STYLE_PHONENUMBER:
1385         case ISE_LAYOUT_STYLE_IP:
1386         case ISE_LAYOUT_STYLE_MONTH:
1387         case ISE_LAYOUT_STYLE_NUMBERONLY:
1388         case ISE_LAYOUT_STYLE_HEX:
1389         case ISE_LAYOUT_STYLE_TERMINAL:
1390         case ISE_LAYOUT_STYLE_DATETIME:
1391         case ISE_LAYOUT_STYLE_PASSWORD:
1392             ret = false;
1393             break;
1394         default:
1395             ret = true;
1396             break;
1397     }
1398     return ret;
1399 }
1400
1401 #if EXIT_ISE_ON_HIDE
1402 static Eina_Bool exit_timer_cb(void *data)
1403 {
1404     if (exit_timer) ecore_timer_del(exit_timer);
1405     exit_timer = NULL;
1406     elm_exit();
1407     return ECORE_CALLBACK_CANCEL;
1408 }
1409 #endif
1410
1411 void
1412 ise_show(int ic)
1413 {
1414     CONFIG_VALUES *config_values = get_config_values();
1415 #if EXIT_ISE_ON_HIDE
1416     if (exit_timer) ecore_timer_del(exit_timer);
1417     exit_timer = NULL;
1418 #endif
1419     sclboolean reset_inputmode = FALSE;
1420     g_input_panel_show = true;
1421
1422     CSCLUI *ui = get_ui();
1423     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1424     if (ui && keyboard_state) {
1425         read_ise_config_values();
1426
1427         if (config_values) {
1428             _language_manager.set_enabled_languages(config_values->enabled_languages);
1429         }
1430         const sclchar *cur_lang = _language_manager.get_current_language();
1431
1432 #ifdef _WEARABLE
1433         ise_check_wearable_candidate();
1434         /*
1435          *Clear the personalized data
1436          */
1437         if (config_values && config_values->dataclear) {
1438             ime_send_key_event(IME_KEY_Clear, IME_KEY_MASK_CONTROL, false);
1439             config_values->dataclear = FALSE;
1440         }
1441 #endif
1442         LOGD("ic : %x , %x , g_ic : %x , %x, g_focused_ic : %x , %x\n", ic, check_ic_temporary(ic),
1443                 keyboard_state->ic, check_ic_temporary(keyboard_state->ic),
1444                 keyboard_state->focused_ic, check_ic_temporary(keyboard_state->focused_ic));
1445
1446         if (check_ic_temporary(ic) && !check_ic_temporary(keyboard_state->focused_ic)) {
1447             ic = keyboard_state->focused_ic;
1448         }
1449
1450         if (!check_ic_temporary(ic) && check_ic_temporary(keyboard_state->focused_ic)) {
1451             keyboard_state->focused_ic = ic;
1452         }
1453
1454         if (ic == keyboard_state->focused_ic) {
1455             switch (keyboard_state->layout)
1456             {
1457                 case ISE_LAYOUT_STYLE_PHONENUMBER:
1458                 case ISE_LAYOUT_STYLE_IP:
1459                 case ISE_LAYOUT_STYLE_MONTH:
1460                 case ISE_LAYOUT_STYLE_NUMBERONLY:
1461                     ime_set_imengine(DEFAULT_KEYBOARD_ISE_UUID);
1462                     break;
1463                 default:
1464                     break;
1465             }
1466         }
1467
1468         const char *input_mode = ui->get_input_mode();
1469
1470         /* Reset input mode if the input context value has changed */
1471         if (ic != keyboard_state->ic) {
1472             /* Do not reset input mode if STT's setting window was opened */
1473             if (get_setting_window_open_status() && input_mode && strcmp(input_mode, "STT_3X4") == 0) {
1474                 LOGD("Setting window was opened while using STT, skip resetting input mode");
1475             } else {
1476                 reset_inputmode = TRUE;
1477             }
1478         }
1479
1480         keyboard_state->ic = ic;
1481         /* Reset input mode if the current language is not the selected language */
1482         if (cur_lang) {
1483             if (config_values && config_values->selected_language.compare(cur_lang) != 0) {
1484                 reset_inputmode = TRUE;
1485             }
1486         }
1487
1488         /* No matter what, just reset the inputmode if it needs to */
1489         if (keyboard_state->need_reset) {
1490             /* Do not reset input mode if STT's setting window was opened */
1491             if (get_setting_window_open_status() && input_mode && strcmp(input_mode, "STT_3X4") == 0) {
1492                 LOGD("Setting window was opened while using STT, skip resetting input mode");
1493             } else {
1494                 reset_inputmode = TRUE;
1495             }
1496         }
1497         keyboard_state->need_reset = FALSE;
1498
1499         /* If the current layout requires latin language and current our language is not latin, enable the primary latin */
1500         sclboolean force_primary_latin = FALSE;
1501         LANGUAGE_INFO *info =
1502             (config_values ? _language_manager.get_language_info(config_values->selected_language.c_str()) : NULL);
1503         if (info) {
1504             if (!info->is_latin_language) {
1505                 if (!keyboard_state->disable_force_latin) {
1506                     if (g_ise_default_values[keyboard_state->layout].force_latin) {
1507                         force_primary_latin = TRUE;
1508                     }
1509                     else {
1510                         if (keyboard_state->prefer_latin) {
1511                             force_primary_latin = TRUE;
1512                         }
1513                     }
1514                 }
1515             }
1516         }
1517
1518         if (force_primary_latin) {
1519             /* If there is enabled latin-based language, select it */
1520             sclboolean selected = FALSE;
1521             for (scluint loop = 0;!selected && loop < _language_manager.get_languages_num();loop++) {
1522                 LANGUAGE_INFO *info = _language_manager.get_language_info(loop);
1523                 if (info) {
1524                     if (info->enabled && info->is_latin_language) {
1525                         selected = _language_manager.select_language(info->name.c_str());
1526                         if (selected) force_primary_latin = FALSE;
1527                     }
1528                 }
1529             }
1530             if (!selected) {
1531                 _language_manager.set_language_enabled_temporarily(PRIMARY_LATIN_LANGUAGE, TRUE);
1532             }
1533         }
1534
1535         if (reset_inputmode) {
1536             ise_reset_context();
1537
1538             bool filename_layout = false;
1539
1540             /* Turn the shift state off if we need to reset our input mode, only when auto-capitalization is not set  */
1541             if (!(keyboard_state->caps_mode)) {
1542                 ui->set_shift_state(SCL_SHIFT_STATE_OFF);
1543             }
1544             if (keyboard_state->layout < ISE_LAYOUT_STYLE_MAX) {
1545                 sclu32 layout_index = keyboard_state->layout;
1546                 if (keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBERONLY &&
1547                     keyboard_state->layout_variation > 0 &&
1548                     keyboard_state->layout_variation < ISE_LAYOUT_NUMBERONLY_VARIATION_MAX) {
1549                     layout_index = ISE_LAYOUT_STYLE_NUMBERONLY_SIG + keyboard_state->layout_variation - 1;
1550                 } else if (keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWORD && keyboard_state->layout_variation > 0) {
1551                     layout_index = ISE_LAYOUT_STYLE_PASSWD_3X4;
1552                 }
1553
1554                 if (keyboard_state->layout == ISE_LAYOUT_STYLE_NORMAL &&
1555                     keyboard_state->layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL_VARIATION_FILENAME)
1556                     filename_layout = true;
1557                 else
1558                     filename_layout = false;
1559
1560                 ui->enable_button("exclamation", !filename_layout);
1561                 ui->enable_button("question", !filename_layout);
1562                 ui->enable_button("divide", !filename_layout);
1563                 ui->enable_button("multiply", !filename_layout);
1564                 ui->enable_button("colon", !filename_layout);
1565                 ui->enable_button("quotation", !filename_layout);
1566
1567                 ui->enable_button("CM_KEY", !access("/home", X_OK));
1568
1569                 LOGD("new layout index : %d\n", layout_index);
1570                 /* If this layout requires specific input mode, set it */
1571                 if (strlen(g_ise_default_values[layout_index].input_mode) > 0) {
1572                     ui->set_input_mode(g_ise_default_values[layout_index].input_mode);
1573                     set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
1574                 } else {
1575                     if (force_primary_latin) {
1576                         _language_manager.select_language(PRIMARY_LATIN_LANGUAGE, TRUE);
1577                     } else if (config_values) {
1578                         if (!(_language_manager.select_language(config_values->selected_language.c_str()))) {
1579                             _language_manager.select_language(PRIMARY_LATIN_LANGUAGE);
1580                         }
1581                     }
1582                 }
1583                 ui->set_cur_sublayout(g_ise_default_values[layout_index].sublayout_name);
1584                 if (ise_emoticon_is_show()) {
1585                     ise_emoticon_destroy_layout();
1586                 }
1587
1588                 if (keyboard_state->layout == ISE_LAYOUT_STYLE_EMOTICON) {
1589                     ise_emoticon_init_list();
1590 #ifdef _WEARABLE
1591                     ise_emoticon_set_current_group(EMOTICON_GROUP_1);
1592                     ise_set_emoticon_label(1);
1593 #else
1594                     if (ise_emoticon_get_recent_list_size() == 0)
1595                         ise_emoticon_set_current_group(EMOTICON_GROUP_1);
1596                     else
1597                         ise_emoticon_set_current_group(EMOTICON_GROUP_RECENTLY_USED);
1598 #endif
1599                     SCLRotation rotation = ui->get_rotation();
1600                     ise_emoticon_show_layout(ise_emoticon_get_current_group(), ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
1601                 }
1602
1603                 if (ise_sticker_is_show())
1604                     ise_sticker_destroy_layout();
1605
1606                 if (ise_nmt_is_show())
1607                     ise_nmt_destroy_layout();
1608             }
1609         }
1610
1611         if (info) {
1612             if (info->accepts_caps_mode) {
1613                 // FIXME this if condition means the AC is off
1614                 if (keyboard_state->layout != ISE_LAYOUT_STYLE_NORMAL) {
1615                     ui->set_autocapital_shift_state(TRUE);
1616                     ui->set_shift_state(SCL_SHIFT_STATE_OFF);
1617                 } else {
1618                     ise_send_event(MVK_Shift_Enable, KEY_MASK_NULL);
1619                     // Auto Capital is supported only in normal layout
1620                     if (keyboard_state->caps_mode) {
1621                         ui->set_autocapital_shift_state(FALSE);
1622                     }
1623                 }
1624             } else {
1625                 ui->set_autocapital_shift_state(TRUE);
1626                 ise_send_event(MVK_Shift_Disable, KEY_MASK_NULL);
1627                 ui->set_shift_state(SCL_SHIFT_STATE_OFF);
1628             }
1629         } else {
1630             ui->set_autocapital_shift_state(TRUE);
1631         }
1632
1633         // Update CM key button
1634         if (_current_cm_key_id < CM_KEY_LIST_SIZE) {
1635             if (strcmp(_cm_key_list[_current_cm_key_id], USER_KEYSTRING_EMOTICON) == 0) {
1636                 if (ise_is_emoticons_disabled())
1637                     ise_set_cm_private_key(ise_get_cm_key_id(USER_KEYSTRING_OPTION));
1638                 else
1639                     ise_set_cm_private_key(_current_cm_key_id);
1640             }
1641         }
1642
1643         // Update space key button
1644         ise_update_space_key();
1645
1646         ui->show();
1647         ui->disable_input_events(FALSE);
1648 #ifdef _IVI
1649         ui->enable_button("CM_KEY", false);
1650 #endif
1651
1652 #if defined(_MOBILE) || defined(_COMMON)
1653         if (ise_is_guideline_popup_enable_layout()) {
1654             if ((config_values && config_values->first_guidechange) && (_language_manager.get_enabled_languages_num() > 1)) {
1655                 ise_show_help_popup(MVK_space, NATIVE_WINDOW_CAST(ime_get_main_window()));
1656                 LOGD("space popup show\n");
1657             } else {
1658                 ise_destroy_popup_space();
1659             }
1660         }
1661 #endif
1662
1663         // Update IME size
1664         if (config_values && config_values->floating_mode != g_floating_mode)
1665             g_floating_mode = config_values->floating_mode;
1666
1667         Candidate *candidate = get_candidate();
1668         if (candidate && candidate->get_visible())
1669             set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
1670         else
1671             set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
1672     }
1673
1674     keyboard_state->visible_state = TRUE;
1675
1676 #ifdef _WEARABLE
1677     int dot_num = 5;
1678     switch (_context_layout) {
1679         case ISE_LAYOUT_STYLE_PHONENUMBER:
1680         case ISE_LAYOUT_STYLE_IP:
1681         case ISE_LAYOUT_STYLE_MONTH:
1682         case ISE_LAYOUT_STYLE_NUMBERONLY:
1683         case ISE_LAYOUT_STYLE_DATETIME:
1684         case ISE_LAYOUT_STYLE_TERMINAL:
1685             dot_num = 0;
1686             break;
1687         case ISE_LAYOUT_STYLE_EMAIL:
1688         case ISE_LAYOUT_STYLE_URL:
1689             dot_num = 4;
1690             break;
1691         case ISE_LAYOUT_STYLE_PASSWORD:
1692             if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
1693                 _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
1694                 dot_num = 0;
1695             else
1696                 dot_num = 3;
1697             break;
1698         default:
1699             dot_num = 5;
1700     }
1701
1702     if (dot_num > 0) {
1703         sclint width = 0;
1704         sclint height = 0;
1705         if (ui)
1706             ui->get_screen_resolution(&width, &height);
1707
1708         create_indicator_window(width, height);
1709         destroy_indicator_dots();
1710
1711         int focus_dot = 0;
1712         switch (keyboard_state->layout) {
1713             case ISE_LAYOUT_STYLE_NUMBER:
1714                 focus_dot = 1;
1715                 break;
1716             case ISE_LAYOUT_STYLE_HEX:
1717                 focus_dot = 2;
1718                 break;
1719             case ISE_LAYOUT_STYLE_EMOTICON:
1720                 focus_dot = 3;
1721                 break;
1722             case ISE_LAYOUT_STYLE_VOICE:
1723                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
1724                     _context_layout == ISE_LAYOUT_STYLE_URL)
1725                     focus_dot = 3;
1726                 else
1727                     focus_dot = 4;
1728                 break;
1729             default:
1730                 break;
1731         }
1732
1733         create_indicator_dots(dot_num, focus_dot);
1734         show_indicator_window();
1735     } else {
1736         hide_indicator_window();
1737     }
1738     if (keyboard_state->layout == ISE_LAYOUT_STYLE_NORMAL && config_values && config_values->number_tutorial_enable) {
1739         ise_show_tutorial_mode_popup(keyboard_state->layout);
1740         config_values->number_tutorial_enable = false;
1741         write_ise_config_values();
1742     }
1743 #endif
1744
1745     if (get_setting_window_open_status()) {
1746         ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
1747     }
1748     set_setting_window_open_status(FALSE);
1749
1750     if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
1751         ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
1752     }
1753 }
1754
1755 /**
1756  * Sets screen rotation
1757  */
1758 void
1759 ise_set_screen_rotation(int degree)
1760 {
1761     CSCLUI *ui = get_ui();
1762     if (ui) {
1763         ui->set_rotation(DEGREE_TO_SCLROTATION(degree));
1764     }
1765
1766     Candidate *candidate = get_candidate();
1767     if (candidate) {
1768         candidate->rotate(degree);
1769         if (candidate->get_visible()) {
1770             candidate->update(g_softcandidate_string);
1771         }
1772     }
1773 }
1774
1775 void
1776 ise_set_accessibility_state(bool state)
1777 {
1778     CSCLUI *ui = get_ui();
1779     if (ui) {
1780         ui->enable_tts(state);
1781     }
1782 }
1783
1784 void
1785 ise_hide()
1786 {
1787     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
1788     CSCLUI *ui = get_ui();
1789     if (ui) {
1790         /* There's no need to update screen when hiding */
1791         ui->set_update_pending(TRUE);
1792         ui->disable_input_events(TRUE);
1793         ui->hide();
1794     }
1795     CONFIG_VALUES *config_values = get_config_values();
1796     if (config_values) {
1797         _language_manager.reset_language(config_values->selected_language.c_str());
1798     }
1799
1800     _click_count = 0;
1801     delete_commit_timer();
1802     ise_destroy_popup_space();
1803     ise_destroy_popup_setting();
1804
1805     if (keyboard_state) keyboard_state->visible_state = FALSE;
1806
1807     _reset_shift_state();
1808     _reset_multitap_state(true);
1809
1810     /* If we were in STT mode, try to reset input mode on our next show event */
1811     if (ui && keyboard_state) {
1812         const char *inputmode = ui->get_input_mode();
1813         if (inputmode && strcmp(inputmode, "STT_3X4") == 0 ) {
1814             keyboard_state->need_reset = TRUE;
1815             ise_hide_stt_mode();
1816         }
1817     }
1818
1819     g_input_panel_show = false;
1820     Candidate *candidate = get_candidate();
1821     if (candidate && candidate->get_visible())
1822         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
1823 #ifdef _WEARABLE
1824     hide_indicator_window();
1825     if (check_is_tutorial_show()) {
1826         ise_destroy_tutorial_mode_popup();
1827     }
1828 #endif
1829 #if EXIT_ISE_ON_HIDE
1830     if (exit_timer) ecore_timer_del(exit_timer);
1831     exit_timer = ecore_timer_add(1.0, exit_timer_cb, NULL);
1832 #endif
1833 }
1834
1835 static void ise_keypad_mode_changed_cb(const char *key, void *user_data)
1836 {
1837     read_ise_keypad_mode();
1838 }
1839
1840 static void ise_enabled_languages_changed_cb(const char *key, void *user_data)
1841 {
1842     read_ise_enabled_languages();
1843 }
1844
1845 static void ise_selected_language_changed_cb(const char *key, void *user_data)
1846 {
1847     read_ise_selected_language();
1848 }
1849
1850 static void ise_autocapital_mode_changed_cb(const char *key, void *user_data)
1851 {
1852     read_ise_autocapital_mode();
1853 }
1854
1855 static void ise_autopunctuate_mode_changed_cb(const char *key, void *user_data)
1856 {
1857     read_ise_autopunctuation_mode();
1858 }
1859
1860 static void ise_sound_mode_changed_cb(const char *key, void *user_data)
1861 {
1862     read_ise_sound_mode();
1863
1864     CONFIG_VALUES *config_values = get_config_values();
1865     CSCLUI *ui = get_ui();
1866     if (config_values && ui) {
1867         ui->enable_sound(config_values->sound_on);
1868     }
1869 }
1870
1871 static void ise_vibration_mode_changed_cb(const char *key, void *user_data)
1872 {
1873     read_ise_vibration_mode();
1874
1875     CONFIG_VALUES *config_values = get_config_values();
1876     CSCLUI *ui = get_ui();
1877     if (config_values && ui) {
1878         ui->enable_vibration(config_values->vibration_on);
1879     }
1880 }
1881
1882 static void ise_character_preview_mode_changed_cb(const char *key, void *user_data)
1883 {
1884     read_ise_character_preview_mode();
1885
1886     CONFIG_VALUES *config_values = get_config_values();
1887     CSCLUI *ui = get_ui();
1888     if (config_values && ui) {
1889         ui->enable_magnifier(config_values->preview_on);
1890     }
1891 }
1892
1893 static void ise_setting_guide_popup_changed_cb(const char *key, void *user_data)
1894 {
1895     read_ise_setting_guide_popup_mode();
1896 }
1897
1898 static void ise_language_guide_popup_changed_cb(const char *key, void *user_data)
1899 {
1900     read_ise_language_guide_popup_mode();
1901 }
1902
1903 static void ise_floating_mode_changed_cb(const char *key, void *user_data)
1904 {
1905     read_ise_floating_mode();
1906     CONFIG_VALUES *config_values = get_config_values();
1907     if (config_values) {
1908         g_floating_mode = config_values->floating_mode;
1909     }
1910
1911     ime_set_floating_mode(g_floating_mode);
1912     if (g_candidate && g_floating_mode != g_candidate->get_floating_mode()) {
1913         delete_softcandidate_hide_timer();
1914         delete g_candidate;
1915         g_candidate = NULL;
1916
1917         create_softcandidate();
1918         if (g_candidate && g_candidate->get_visible()) {
1919             g_candidate->show();
1920             set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
1921             return;
1922         }
1923     }
1924     set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
1925 }
1926
1927 static void register_preference_changed_callback(void)
1928 {
1929     if (preference_set_changed_cb(ISE_CONFIG_KEYPAD_MODE, ise_keypad_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1930         LOGW("Failed to set ise_keypad_mode_changed_cb()");
1931
1932     if (preference_set_changed_cb(ISE_CONFIG_ENABLED_LANGUAGES, ise_enabled_languages_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1933         LOGW("Failed to set ise_enabled_languages_changed_cb()");
1934
1935     if (preference_set_changed_cb(ISE_CONFIG_SELECTED_LANGUAGE, ise_selected_language_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1936         LOGW("Failed to set ise_selected_language_changed_cb()");
1937
1938     if (preference_set_changed_cb(ISE_CONFIG_AUTO_CAPITALISE, ise_autocapital_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1939         LOGW("Failed to set ise_autocapital_mode_changed_cb()");
1940
1941     if (preference_set_changed_cb(ISE_CONFIG_AUTO_PUNCTUATE, ise_autopunctuate_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1942         LOGW("Failed to set ise_autopunctuate_mode_changed_cb()");
1943
1944     if (preference_set_changed_cb(ISE_CONFIG_SOUND_ON, ise_sound_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1945         LOGW("Failed to set ise_sound_mode_changed_cb()");
1946
1947     if (preference_set_changed_cb(ISE_CONFIG_VIBRATION_ON, ise_vibration_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1948         LOGW("Failed to set ise_vibration_mode_changed_cb()");
1949
1950     if (preference_set_changed_cb(ISE_CONFIG_PREVIEW_ON, ise_character_preview_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1951         LOGW("Failed to set ise_character_preview_mode_changed_cb()");
1952
1953     if (preference_set_changed_cb(ISE_CONFIG_FIRST_GUIDELINE_POPUP_FOR_SETTING, ise_setting_guide_popup_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1954         LOGW("Failed to set ise_setting_guide_popup_changed_cb()");
1955
1956     if (preference_set_changed_cb(ISE_CONFIG_FIRST_GUIDELINE_POPUP_FOR_LANGUAGE_CHANGE, ise_language_guide_popup_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1957         LOGW("Failed to set ise_language_guide_popup_changed_cb()");
1958
1959     if (preference_set_changed_cb(ISE_CONFIG_FLOATING_MODE, ise_floating_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
1960         LOGW("Failed to set ise_floating_mode_changed_cb()");
1961 }
1962
1963 static void ise_mount_changed_cb(void *user_data)
1964 {
1965     register_preference_changed_callback();
1966 }
1967
1968 void
1969 ise_create()
1970 {
1971     LOGD("");
1972
1973     CONFIG_VALUES *config_values = get_config_values();
1974
1975     if (!g_ui) {
1976         g_ui = new CSCLUI;
1977     }
1978
1979     bindtextdomain(PACKAGE, LOCALEDIR);
1980     textdomain(PACKAGE);
1981
1982     /* Set scl_parser_type
1983      * default type is text xml
1984      * use command: export sclres_type="sclres_binary" to enable use binary resource
1985      * please make sure there is sclresource.bin in resource folder
1986      * Or you can use `xml2binary $resource_dir` to generate the sclresource.bin
1987      * xml2binary is in the libscl-ui-devel package
1988      */
1989     SCLParserType scl_parser_type = SCL_PARSER_TYPE_XML;
1990     char* sclres_type = getenv("sclres_type");
1991     if (sclres_type != NULL && 0 == strcmp("sclres_binary", sclres_type)) {
1992         scl_parser_type = SCL_PARSER_TYPE_BINARY_XML;
1993     } else {
1994         scl_parser_type = SCL_PARSER_TYPE_XML;
1995     }
1996
1997     if (g_ui) {
1998         if (ime_get_main_window()) {
1999             g_ise_created = true;
2000
2001             sclboolean succeeded = FALSE;
2002
2003             const sclchar *entry_path = MAIN_ENTRY_XML_PATH;
2004             _language_manager.set_resource_file_path(entry_path);
2005             const sclchar *resource_file_path = _language_manager.get_resource_file_path();
2006
2007             if (resource_file_path) {
2008                 if (strlen(resource_file_path) > 0) {
2009                     succeeded = g_ui->init(ime_get_main_window(), scl_parser_type, resource_file_path);
2010                 }
2011             }
2012             if (!succeeded) {
2013                 g_ui->init(ime_get_main_window(), scl_parser_type, MAIN_ENTRY_XML_PATH);
2014             }
2015
2016             g_ui->set_longkey_duration(elm_config_longpress_timeout_get() * 1000);
2017
2018             /* Default ISE callback */
2019             g_ui->set_ui_event_callback(&callback);
2020
2021             /* Accumulated customized ISE callbacks, depending on the input modes */
2022             for (scluint loop = 0;loop < _language_manager.get_languages_num();loop++) {
2023                 LANGUAGE_INFO *language = _language_manager.get_language_info(loop);
2024                 if (language) {
2025                     for (scluint inner_loop = 0;inner_loop < language->input_modes.size();inner_loop++) {
2026                         INPUT_MODE_INFO &info = language->input_modes.at(inner_loop);
2027                         LOGD("Registering callback for input mode %s : %p\n", info.name.c_str(), language->callback);
2028                         g_ui->set_ui_event_callback(language->callback, info.name.c_str());
2029                     }
2030                 }
2031             }
2032
2033             read_ise_config_values();
2034             if (access(ISE_CONFIG_FILE_PATH, F_OK) == 0 && !config_values->init_flag)
2035                 read_ise_config_file();
2036
2037             if (config_values) {
2038                 _language_manager.set_enabled_languages(config_values->enabled_languages);
2039                 _language_manager.select_language(config_values->selected_language.c_str());
2040                 vconf_set_bool(VCONFKEY_AUTOCAPITAL_ALLOW_BOOL, config_values->auto_capitalise);
2041                 vconf_set_bool(VCONFKEY_AUTOPERIOD_ALLOW_BOOL, config_values->auto_punctuate);
2042                 g_ui->enable_sound(config_values->sound_on);
2043                 g_ui->enable_vibration(config_values->vibration_on);
2044                 g_ui->enable_magnifier(config_values->preview_on);
2045                 g_floating_mode = config_values->floating_mode;
2046             }
2047 #ifdef _TV
2048             g_ui->enable_highlight_ui(TRUE);
2049 #endif
2050             ime_set_floating_mode(g_floating_mode);
2051         }
2052
2053         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
2054
2055         bool exist = false;
2056
2057         preference_is_existing(ISE_CONFIG_KEYPAD_MODE, &exist);
2058         if (!exist)
2059             write_ise_config_values();
2060
2061         int state;
2062         if (ode_internal_encryption_get_state(&state) == ODE_ERROR_NONE) {
2063             if (state == ODE_STATE_ENCRYPTED) {
2064                 if (ode_internal_encryption_set_mount_event_cb(ise_mount_changed_cb, NULL) != ODE_ERROR_NONE)
2065                     LOGW("Failed to set mount_event_cb()");
2066             } else if (state == ODE_STATE_UNENCRYPTED) {
2067                 register_preference_changed_callback();
2068             }
2069         } else {
2070             LOGW("Failed to get encryption_state");
2071         }
2072     }
2073     init_recent_used_punctuation();
2074 }
2075
2076 void
2077 ise_destroy()
2078 {
2079     ise_hide_stt_mode();
2080
2081     CSCLUI *ui = get_ui();
2082     if (ui) {
2083         LOGD("calling ui->fini()\n");
2084         ui->fini();
2085         LOGD("deleting ui\n");
2086         delete ui;
2087         ui = NULL;
2088         g_ui = NULL;
2089     }
2090
2091     if (g_candidate) {
2092         delete g_candidate;
2093         g_candidate = NULL;
2094     }
2095
2096 #ifdef _WEARABLE
2097     destroy_indicator_window();
2098 #endif
2099
2100     /* This is necessary. If this is not called, 3rd party IME might have auto period input regardless its settings */
2101     vconf_set_bool(VCONFKEY_AUTOPERIOD_ALLOW_BOOL, false);
2102
2103 #if EXIT_ISE_ON_HIDE
2104     if (exit_timer) ecore_timer_del(exit_timer);
2105     exit_timer = NULL;
2106 #endif
2107 }
2108
2109 void
2110 ise_app_candidate_show()
2111 {
2112 #ifdef _WEARABLE
2113     if (!g_candidate)
2114         return;
2115
2116     if (!g_candidate->get_visible())
2117         return;
2118 #endif
2119     LOGD("Enter\n");
2120     delete_softcandidate_hide_timer();
2121
2122     create_softcandidate();
2123
2124     Candidate *candidate = get_candidate();
2125     if (candidate) {
2126         candidate->show();
2127         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
2128     }
2129 }
2130
2131 void
2132 ise_app_candidate_hide()
2133 {
2134     LOGD("Enter\n");
2135
2136     Candidate *candidate = get_candidate();
2137     if (!candidate || !candidate->get_visible()) {
2138         LOGD("No candidate\n");
2139         return;
2140     }
2141
2142     if (g_ic_smartreply != -1 || ime_autofill_get_exist())
2143         return;
2144
2145     add_softcandidate_hide_timer();
2146 }
2147
2148 #ifdef _WEARABLE
2149 void ise_check_wearable_candidate()
2150 {
2151     CONFIG_VALUES *config_values = get_config_values();
2152     if (!config_values) return;
2153
2154     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2155     if (!keyboard_state) return;
2156
2157     if (!config_values->prediction_on) {
2158         ise_app_candidate_hide();
2159     } else if (keyboard_state->layout == ISE_LAYOUT_STYLE_PHONENUMBER ||
2160                   keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE ||
2161                   keyboard_state->layout == ISE_LAYOUT_STYLE_IP ||
2162                   keyboard_state->layout == ISE_LAYOUT_STYLE_MONTH ||
2163                   keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBERONLY ||
2164                   keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWD_3X4||
2165                   keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWORD) {
2166         ise_app_candidate_hide();
2167     } else {
2168         ise_app_candidate_show();
2169     }
2170 }
2171
2172 void ise_set_emoticon_label(int group_id)
2173 {
2174     const int BUF_LEN = 16;
2175     char buf[BUF_LEN] = {0};
2176     snprintf(buf, BUF_LEN, "%d/3", group_id);
2177     CSCLUI *ui = get_ui();
2178     if (ui) ui->set_private_key("EMOTICON_GROUP_ID", buf, NULL, NULL, 0, const_cast<sclchar*>("EMOTICON_GROUP_NEXT"), TRUE);
2179 }
2180 #endif
2181
2182 // when it is the time to auto_cap, the
2183 // ise_set_caps_mode is called.
2184 // -------------------------------------------------------
2185 // For example: [How are you. Fine.], the
2186 // auto-capital process is as below:
2187 // Note: "["<--this is the beginning,
2188 // "|"<--this is the cursor position
2189 // 1) call ise_set_caps_mode, auto_cap = on
2190 //    input: "H",
2191 //    result: [H|
2192 // 2) call ise_set_caps_mode, auto_cap = off
2193 //    input: "o"
2194 //    result: [Ho|
2195 // 3) input: "w are you. "
2196 //    result: [How are you. |
2197 // 4) call ise_set_caps_mode, auto_cap = on
2198 //    input: "F"
2199 //    result: [How are you. F
2200 // 5) input: "ine."
2201 //    result: [How are you. Fine.|
2202 // --------------------------------------------------------
2203 // If we want to change the auto_cap, eg,
2204 // if we want to input [How Are you.]
2205 // Note the "Are" is not use auto-capital rule.
2206 // we should use:
2207 //    ise_send_event(MVK_Shift_On, SclCoreKeyMask_Null);
2208 // when we are want to input "A"
2209 // following input still has the auto_cap rule.
2210 void
2211 ise_set_caps_mode(unsigned int mode)
2212 {
2213     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2214     if (!keyboard_state) return;
2215
2216     LOGD("caps_mode : %d\n", mode);
2217     if (mode) {
2218         keyboard_state->caps_mode = TRUE;
2219     } else {
2220         keyboard_state->caps_mode = FALSE;
2221     }
2222     g_caps_mode_pending = false;
2223     const sclchar *cur_lang = _language_manager.get_current_language();
2224     if (cur_lang) {
2225         LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
2226         if (info) {
2227             if (info->accepts_caps_mode) {
2228                 /* If we are inputting multitap character, do not manipulate shift mode */
2229                 if (keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_START &&
2230                     keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_REPEAT) {
2231                     set_caps_mode(keyboard_state->caps_mode);
2232                 } else {
2233                     g_caps_mode_pending = true;
2234                     LOGD("Currently composing multitap string, skipping caps request");
2235                 }
2236             }
2237         }
2238     }
2239 }
2240
2241 void
2242 ise_update_cursor_position(int position)
2243 {
2244     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2245     if (!keyboard_state) return;
2246
2247     LOGD("cursor position : %d\n", position);
2248     CSCLUI *ui = get_ui();
2249     if (ui && keyboard_state->layout == ISE_LAYOUT_STYLE_URL) {
2250 #ifndef _TV
2251         if (position > 0) {
2252             ui->set_string_substitution("www.", ".com");
2253         } else {
2254             ui->unset_string_substitution("www.");
2255         }
2256 #endif
2257     }
2258 }
2259
2260 void ise_set_return_key_type(unsigned int type)
2261 {
2262     const int BUF_LEN = 256;
2263     char buf[BUF_LEN] = {0};
2264
2265     CSCLUI *ui = get_ui();
2266     if (!ui) return;
2267
2268     LOGD("return key type : %d\n", type);
2269     switch (type)
2270     {
2271     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DONE:
2272         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_DONE);
2273         break;
2274     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_GO:
2275         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_GO);
2276         break;
2277     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_JOIN:
2278         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_JOIN);
2279         break;
2280     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_LOGIN:
2281         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_LOGIN);
2282         break;
2283     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_NEXT:
2284         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_NEXT);
2285         break;
2286     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH:
2287         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEARCH);
2288         break;
2289     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEND:
2290         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEND);
2291         break;
2292     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SIGNIN:
2293         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SIGNIN);
2294         break;
2295     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT:
2296         break;
2297     default:
2298         LOGW("Unknown return key type : %d\n", type);
2299         type = ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT;
2300         break;
2301     }
2302
2303     if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT) {
2304         ui->unset_private_key("Enter");
2305 #ifdef _TV
2306         ui->unset_private_key("Done");
2307 #endif
2308     } else {
2309 #ifdef _WEARABLE
2310         if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH) {
2311             sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
2312                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search.png"),
2313                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search_p.png"),
2314                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search_d.png")};
2315             ui->set_private_key("Enter", const_cast<sclchar*>(""), imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
2316             return;
2317         }
2318 #endif
2319
2320 #ifdef _TV
2321         ui->set_private_key("Done", buf, NULL, NULL, 0, const_cast<sclchar*>("Done"), TRUE);
2322 #else
2323         static sclchar *imagelabel[SCL_BUTTON_STATE_MAX] = {
2324             const_cast<sclchar*>(" "), const_cast<sclchar*>(" "), const_cast<sclchar*>(" ")
2325         };
2326
2327         ui->set_private_key("Enter", buf, imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
2328 #endif
2329         LOGD("return key label : %s\n", buf);
2330     }
2331 }
2332
2333 void ise_set_return_key_disable(unsigned int disabled)
2334 {
2335     CSCLUI *ui = get_ui();
2336     LOGD("enable : %d\n", !disabled);
2337 #ifdef _TV
2338     if (ui)
2339         ui->enable_button("Done", !disabled);
2340 #else
2341     if (ui)
2342         ui->enable_button("Enter", !disabled);
2343 #endif
2344 }
2345
2346 void ise_get_language_locale(char **locale)
2347 {
2348     LANGUAGE_INFO *info = _language_manager.get_current_language_info();
2349     if (info) {
2350         if (!(info->locale_string.empty())) {
2351             *locale = strdup(info->locale_string.c_str());
2352         }
2353     }
2354 }
2355
2356 void ise_update_table(const vector<string> &vec_str)
2357 {
2358     Candidate *candidate = get_candidate();
2359     if (!candidate || !candidate->get_visible()) {
2360         create_softcandidate();
2361     }
2362
2363     candidate = get_candidate();
2364     if (candidate) {
2365         candidate->update(vec_str);
2366     }
2367 }
2368
2369 void ise_process_key_event(scim::KeyEvent& key, sclu32 &ret)
2370 {
2371     Eina_Bool back_key_pressed = EINA_FALSE;
2372     Eina_Bool back_key_released = EINA_FALSE;
2373
2374     if (key.get_key_string().compare("XF86Back") == 0) {
2375         back_key_pressed = EINA_TRUE;
2376     } else if (key.get_key_string().compare("KeyRelease+XF86Back") == 0) {
2377         back_key_released = EINA_TRUE;
2378     }
2379
2380     if (back_key_pressed || back_key_released) {
2381         if (g_popup_opened == TRUE) {
2382             if (back_key_released) {
2383                 CSCLUI *ui = get_ui();
2384                 if (ui) ui->close_all_popups();
2385             }
2386             ret = 1;
2387             return;
2388         }
2389     }
2390
2391     ret = 0;
2392     CSCLUI *ui = get_ui();
2393 #ifdef _TV
2394     Candidate *candidate = get_candidate();
2395     if (candidate) {
2396         if (!candidate->soft_candidate_flag()) {
2397             if (ui) {
2398                 if (key.dev_name.compare("ime") != 0) {
2399                     ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2400                 }
2401             }
2402         }
2403         if (!ret) {
2404             ret = candidate->soft_candidate_handle_key_event(key.get_key_string().c_str());
2405         }
2406     } else {
2407         if (ui) {
2408             /* Process this key event if it was not generated by ime */
2409             if (key.dev_name.compare("ime") != 0) {
2410                 ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2411             }
2412         }
2413     }
2414 #else
2415     if (ui) {
2416         /* Process this key event if it was not generated by ime */
2417         if (key.dev_name.compare("ime") != 0) {
2418             ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2419         }
2420     }
2421 #endif
2422 }
2423
2424 static void init_recent_used_punctuation()
2425 {
2426     if (g_recent_used_punctuation.empty())
2427     {
2428         g_recent_used_punctuation.push_back("#");
2429         g_recent_used_punctuation.push_back("$");
2430         g_recent_used_punctuation.push_back("%");
2431         g_recent_used_punctuation.push_back("^");
2432         g_recent_used_punctuation.push_back("&");
2433     }
2434 }
2435
2436 static void update_recent_used_punctuation(const char * key_value)
2437 {
2438     if (NULL == key_value)
2439     {
2440         return;
2441     }
2442     for (int i = 0; i < 10; ++i)
2443     {
2444         char buf[5] = {0};
2445         snprintf(buf, sizeof(buf), "%d", i);
2446         if (strcmp(key_value, buf) == 0)
2447         {
2448             return;
2449         }
2450     }
2451     string strKey = string(key_value);
2452     for (int i = 0; i < MAX_DEFAULT_PUNCTUATION; ++i)
2453     {
2454         if (0 == strKey.compare(g_default_punctuation[i].c_str()))
2455         {
2456             return;
2457         }
2458     }
2459     vector<string>::iterator iter = g_recent_used_punctuation.begin();
2460     for (; iter != g_recent_used_punctuation.end(); ++iter)
2461     {
2462         if (0 == strKey.compare(iter->c_str()))
2463         {
2464             break;
2465         }
2466     }
2467     if (iter != g_recent_used_punctuation.end())
2468     {
2469         g_recent_used_punctuation.erase(iter);
2470     }
2471     g_recent_used_punctuation.push_back(strKey);
2472     if (g_recent_used_punctuation.size() > MAX_DEFAULT_PUNCTUATION-1)
2473     {
2474         g_recent_used_punctuation.erase(g_recent_used_punctuation.begin());
2475     }
2476 }
2477
2478 static void set_ime_size(bool floating_mode, ISE_CANDIDATE_REQUEST candidate_req)
2479 {
2480     CSCLUI *ui = get_ui();
2481     if (!ui)
2482         return;
2483
2484     Candidate *candidate = get_candidate();
2485
2486     const char *input_mode = ui->get_input_mode();
2487     if (!input_mode)
2488         return;
2489
2490     SclSize size_portrait = ui->get_input_mode_size(input_mode, DISPLAYMODE_PORTRAIT);
2491     SclSize size_landscape = ui->get_input_mode_size(input_mode, DISPLAYMODE_LANDSCAPE);
2492
2493     if (floating_mode) {
2494         size_portrait.width *= FLOATING_SCALE_RATE;
2495         size_portrait.height *= FLOATING_SCALE_RATE;
2496         size_landscape.width *= FLOATING_SCALE_RATE;
2497         size_landscape.height *= FLOATING_SCALE_RATE;
2498         switch (candidate_req) {
2499             case ISE_CANDIDATE_REQ_NONE:
2500                 if (candidate && candidate->get_visible()) {
2501                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
2502                     size_portrait.height += candidate->get_height();
2503                     size_landscape.height += candidate->get_height();
2504                 } else {
2505                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
2506                 }
2507                 break;
2508             case ISE_CANDIDATE_REQ_SHOW:
2509                 if (candidate) {
2510                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
2511                     if (g_input_panel_show || g_candidate_more_view) {
2512                         size_portrait.height += candidate->get_height();
2513                         size_landscape.height += candidate->get_height();
2514                     } else {
2515                         size_portrait.height = candidate->get_height();
2516                         size_landscape.height = candidate->get_height();
2517                     }
2518                 }
2519                 break;
2520             case ISE_CANDIDATE_REQ_HIDE:
2521                 ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
2522                 break;
2523             default: break;
2524         }
2525
2526         if (ui->get_custom_scale_rate_x() != FLOATING_SCALE_RATE || ui->get_custom_scale_rate_y() != FLOATING_SCALE_RATE)
2527             ui->set_custom_scale_rate(FLOATING_SCALE_RATE, FLOATING_SCALE_RATE);
2528
2529         ime_set_size(size_portrait.width, size_portrait.height + FLOATING_TITLE_BAR_HEIGHT,
2530             size_landscape.width, size_landscape.height + FLOATING_TITLE_BAR_HEIGHT);
2531
2532 #if defined(_MOBILE) || defined(_COMMON)
2533         ise_destroy_move_handler();
2534         int rotation = elm_win_rotation_get(NATIVE_WINDOW_CAST(ime_get_main_window()));
2535         int handler_width = (rotation == 0 || rotation == 180) ? size_portrait.width : size_landscape.width;
2536         ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
2537 #endif
2538     } else {
2539         switch (candidate_req) {
2540             case ISE_CANDIDATE_REQ_NONE:
2541                 if (candidate && candidate->get_visible()) {
2542                     ui->set_custom_starting_coordinates(0, candidate->get_height());
2543                     size_portrait.height += candidate->get_height();
2544                     size_landscape.height += candidate->get_height();
2545                 } else {
2546                     ui->set_custom_starting_coordinates(0, 0);
2547                 }
2548                 break;
2549             case ISE_CANDIDATE_REQ_SHOW:
2550                 if (candidate) {
2551                     ui->set_custom_starting_coordinates(0, candidate->get_height());
2552                     if (g_input_panel_show || g_candidate_more_view) {
2553                         size_portrait.height += candidate->get_height();
2554                         size_landscape.height += candidate->get_height();
2555                     } else {
2556                         size_portrait.height = candidate->get_height();
2557                         size_landscape.height = candidate->get_height();
2558                     }
2559                 }
2560                 break;
2561             case ISE_CANDIDATE_REQ_HIDE:
2562                 ui->set_custom_starting_coordinates(0, 0);
2563                 break;
2564             default: break;
2565         }
2566
2567         if (ui->get_custom_scale_rate_x() != 1.0 || ui->get_custom_scale_rate_y() != 1.0)
2568             ui->set_custom_scale_rate(1.0, 1.0);
2569
2570         ime_set_size(size_portrait.width, size_portrait.height, size_landscape.width, size_landscape.height);
2571
2572 #if defined(_MOBILE) || defined(_COMMON)
2573         ise_destroy_move_handler();
2574 #endif
2575     }
2576 }
2577
2578 static void ime_app_create_cb(void *user_data)
2579 {
2580     if (!engine_loader_dbus_init(NULL))
2581         LOGE("Failed to initialize dbus");
2582
2583 #if !(DEFER_ISE_CREATION)
2584     ise_create();
2585 #endif
2586     elm_app_name_set(PACKAGE);
2587
2588     char *elm_scale = getenv("ELM_SCALE");
2589     LOGD("ELM_SCALE : %s", elm_scale);
2590
2591     if (elm_scale)
2592         elm_app_base_scale_set(atof(elm_scale));
2593
2594     clipboard_init();
2595
2596     nmt_init();
2597 }
2598
2599 static void ime_app_exit_cb(void *user_data)
2600 {
2601     ise_hide();
2602     ise_destroy();
2603
2604     if (!engine_loader_dbus_shutdown())
2605         LOGE("Failed to finalize dbus");
2606
2607     clipboard_shutdown();
2608
2609     nmt_shutdown();
2610 }
2611
2612 static void show_autofill_data()
2613 {
2614     ime_autofill_set_exist(false);
2615     string autofill_string = ime_autofill_get_string();
2616
2617     SECURE_LOGD("autofill string : %s", autofill_string.c_str());
2618
2619     if (!autofill_string.empty()) {
2620         ime_autofill_set_exist(true);
2621
2622         ise_app_candidate_show();
2623         update_candidate_table();
2624     }
2625 }
2626
2627 static void ime_app_show_cb(int ic, ime_context_h ime_ctx, void *user_data)
2628 {
2629     Ise_Context iseContext;
2630     bool return_key_state, prediction_allow, password_mode, caps_mode;
2631     ime_layout_variation_e layout_variation;
2632
2633     if (!g_ise_created)
2634         ise_create();
2635
2636     ime_context_get_layout(ime_ctx, &iseContext.layout);
2637
2638     ime_context_get_layout_variation(ime_ctx, &layout_variation);
2639     iseContext.layout_variation =  (int)layout_variation;
2640
2641     ime_context_get_cursor_position(ime_ctx, &iseContext.cursor_pos);
2642     ime_context_get_autocapital_type(ime_ctx, &iseContext.autocapital_type);
2643     ime_context_get_return_key_type(ime_ctx, &iseContext.return_key_type);
2644     ime_context_get_return_key_state(ime_ctx, &return_key_state);
2645
2646     ime_context_get_prediction_mode(ime_ctx, &prediction_allow);
2647     iseContext.prediction_allow = prediction_allow;
2648
2649     ime_context_get_password_mode(ime_ctx, &password_mode);
2650     iseContext.password_mode = password_mode;
2651
2652     ime_context_get_input_hint(ime_ctx, &iseContext.input_hint);
2653     ime_context_get_bidi_direction(ime_ctx, &iseContext.bidi_direction);
2654     ime_context_get_language(ime_ctx, &iseContext.language);
2655     ime_context_get_caps_mode(ime_ctx, &caps_mode);
2656     iseContext.caps_mode = caps_mode;
2657
2658     iseContext.return_key_disabled = return_key_state;
2659
2660     engine_loader_set_input_hint((uint32_t)iseContext.input_hint);
2661     engine_loader_update_bidi_direction((uint32_t)iseContext.bidi_direction);
2662
2663     g_ic = ic;
2664
2665     ime_autofill_set_hint(iseContext.input_hint & ECORE_IMF_INPUT_HINT_AUTOFILL_MASK);
2666
2667     LOGD("input hint : %x, autofill hint : %x\n", iseContext.input_hint, ime_autofill_get_hint());
2668
2669     // show autofill data
2670     show_autofill_data();
2671
2672     //g_ise_common->set_keyboard_ise_by_uuid(KEYBD_ISE_UUID);
2673
2674     /* Don't update screen until all the information is correctly set */
2675     CSCLUI *ui = get_ui();
2676     if (ui)
2677         ui->set_update_pending(TRUE);
2678
2679     ise_reset_context(); // reset ISE
2680
2681     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2682
2683     if (iseContext.language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
2684         LOGD("prefer latin");
2685         if (keyboard_state)
2686             keyboard_state->prefer_latin = TRUE;
2687     }
2688     else {
2689         LOGD("prefer automatic");
2690         if (keyboard_state)
2691             keyboard_state->prefer_latin = FALSE;
2692     }
2693
2694     _context_layout = iseContext.layout;
2695     _context_layout_variation = iseContext.layout_variation;
2696     ise_set_layout(iseContext.layout, iseContext.layout_variation);
2697
2698     ise_set_return_key_type(iseContext.return_key_type);
2699     ise_set_return_key_disable(iseContext.return_key_disabled);
2700
2701     ise_set_caps_mode(iseContext.caps_mode);
2702     ise_update_cursor_position(iseContext.cursor_pos);
2703
2704     ise_show(ic);
2705
2706     /* Now we update the whole screen */
2707     if (ui)
2708         ui->set_update_pending(FALSE);
2709 }
2710
2711 static void ime_app_hide_cb(int ic, void *user_data)
2712 {
2713     LOGD("Enter\n");
2714     ise_hide();
2715
2716     clipboard_set_mime_type(false);
2717
2718     g_ic_smartreply = -1;
2719 }
2720
2721 static void ime_app_return_key_type_set_cb(Ecore_IMF_Input_Panel_Return_Key_Type type, void *user_data)
2722 {
2723     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2724     if (!keyboard_state) return;
2725
2726     LOGD("return key type : %d\n", type);
2727     ise_set_return_key_type(type);
2728
2729     if (keyboard_state->visible_state)
2730         ise_show(keyboard_state->ic);
2731 }
2732
2733 static void ime_app_return_key_state_set_cb(bool disabled, void *user_data)
2734 {
2735     LOGD("return key disabled : %d\n", disabled);
2736     ise_set_return_key_disable(disabled);
2737 }
2738
2739 static void ime_app_language_set_cb(Ecore_IMF_Input_Panel_Lang language, void *user_data)
2740 {
2741     LOGD("language : %d\n", language);
2742
2743     // if (language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
2744     //     ise_explictly_set_language(PRIMARY_LATIN_LANGUAGE_INDEX);
2745     // }
2746 }
2747
2748 static void ime_app_input_context_reset_cb(void *user_data)
2749 {
2750     ise_reset_input_context();
2751     engine_loader_reset_input_context();
2752 }
2753
2754 static void ime_app_cursor_position_updated_cb(int cursor_pos, void *user_data)
2755 {
2756     LOGD("cursor position : %d\n", cursor_pos);
2757     ise_update_cursor_position(cursor_pos);
2758     engine_loader_update_cursor_position(cursor_pos);
2759 }
2760
2761 static void ime_app_language_requested_cb(void *user_data, char **lang_code)
2762 {
2763     ise_get_language_locale(lang_code);
2764 }
2765
2766 static void ime_app_surrounding_text_updated_cb(int context_id, const char *text, int cursor_pos, void *user_data)
2767 {
2768     SECURE_LOGD("surrounding text:%s, cursor=%d\n", text, cursor_pos);
2769     ime_delete_surrounding_text(-cursor_pos, strlen(text));
2770 }
2771
2772 static void ime_app_focus_in_cb(int context_id, void *user_data)
2773 {
2774     LOGD("Enter\n");
2775     ise_focus_in(context_id);
2776     Candidate *candidate = get_candidate();
2777     if (input_smartreply_get_reply_num() == 0) {
2778         if (candidate && candidate->get_visible()) {
2779             ise_app_candidate_hide();
2780             candidate->hide();
2781         }
2782     }
2783
2784     engine_loader_focus_in();
2785 }
2786
2787 static void ime_app_focus_out_cb(int context_id, void *user_data)
2788 {
2789     LOGD("Enter\n");
2790     ise_focus_out(context_id);
2791     g_imdata_state = 0;
2792     clipboard_set_mime_type(false);
2793     input_smartreply_deinit();
2794     g_ic_smartreply = -1;
2795
2796     ime_autofill_set_exist(false);
2797     ime_autofill_set_hint(0);
2798     ime_autofill_set_app_id("");
2799     ime_autofill_set_resource_id("");
2800
2801     g_smartreply_strings.clear();
2802     g_lookup_table_strings.clear();
2803     g_softcandidate_string.clear();
2804
2805     engine_loader_focus_out();
2806 }
2807
2808 static void ime_app_layout_set_cb(Ecore_IMF_Input_Panel_Layout layout, void *user_data)
2809 {
2810     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2811     if (!keyboard_state) return;
2812
2813     LOGD("layout=%d\n", layout);
2814     /* Check if the layoutIdx is in the valid range */
2815     if (static_cast<int>(layout) < static_cast<int>(ISE_LAYOUT_STYLE_MAX)) {
2816         if (keyboard_state->layout != layout) {
2817             keyboard_state->need_reset = TRUE;
2818         }
2819         keyboard_state->layout = layout;
2820         _context_layout = layout;
2821         _context_layout_variation = 0;
2822     }
2823     if (keyboard_state->visible_state)
2824         ise_show(keyboard_state->ic);
2825
2826     engine_loader_set_layout(static_cast<uint32_t>(layout));
2827 }
2828
2829 static void ime_app_input_hint_set_cb(Ecore_IMF_Input_Hints input_hint, void *user_data)
2830 {
2831     LOGD("input hint=%u\n", input_hint);
2832 }
2833
2834 static void ime_app_rotation_degree_changed_cb(int degree, void *user_data)
2835 {
2836     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2837     if (!keyboard_state) return;
2838
2839     CSCLUI *ui = get_ui();
2840     ise_set_screen_rotation(degree);
2841
2842     LOGD("degree=%d\n", degree);
2843 #if defined(_MOBILE) || defined(_COMMON)
2844     if (ui && g_floating_mode) {
2845         int handler_width;
2846         ise_destroy_move_handler();
2847         if (degree == 0 || degree == 180) {
2848             SclSize size_portrait = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_PORTRAIT);
2849             handler_width = size_portrait.width * FLOATING_SCALE_RATE;
2850         } else {
2851             SclSize size_landscape = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_LANDSCAPE);
2852             handler_width = size_landscape.width * FLOATING_SCALE_RATE;
2853         }
2854         ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
2855     }
2856 #endif
2857     if (ise_emoticon_is_show()) {
2858         ise_emoticon_destroy_layout();
2859     }
2860     if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
2861         ise_hide_stt_mode();
2862     }
2863     if (keyboard_state->layout == ISE_LAYOUT_STYLE_EMOTICON) {
2864         ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
2865     } else if (ui) {
2866         const sclchar *input_mode = ui->get_input_mode();
2867         if (input_mode) {
2868             if (!(strcmp(input_mode, "EMOTICON_LAYOUT")))
2869                 ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
2870             else if (!(strcmp(input_mode, "STT_3X4")))
2871                 ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
2872         }
2873     }
2874 }
2875
2876 static void ime_app_accessibility_state_changed_cb(bool state, void *user_data)
2877 {
2878     LOGD("state=%d\n", state);
2879     ise_set_accessibility_state(state);
2880 }
2881
2882 static void ime_app_imdata_set_cb(void *data, unsigned int data_length, void *user_data)
2883 {
2884     LOGD("Enter\n");
2885     g_imdata_state = 0;
2886     size_t _len = data_length;
2887     set_ise_imdata((sclchar *)data, _len);
2888     engine_loader_set_imdata((const char*)data, (uint32_t)data_length);
2889 }
2890
2891 static bool ime_app_process_key_event_cb(ime_key_code_e keycode, ime_key_mask_e keymask, ime_device_info_h dev_info, void *user_data)
2892 {
2893     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2894     if (!keyboard_state) return FALSE;
2895
2896     scim::KeyEvent key(keycode, keymask);
2897     unsigned int ret;
2898     char *dev_name = NULL;
2899     Ecore_IMF_Device_Class dev_class;
2900     Ecore_IMF_Device_Subclass dev_subclass;
2901
2902     if (ime_device_info_get_name(dev_info, &dev_name) == IME_ERROR_NONE) {
2903         key.dev_name = dev_name;
2904     }
2905
2906     if (ime_device_info_get_class(dev_info, &dev_class) == IME_ERROR_NONE) {
2907         key.dev_class = dev_class;
2908     }
2909
2910     if (ime_device_info_get_subclass(dev_info, &dev_subclass) == IME_ERROR_NONE) {
2911         key.dev_subclass = dev_subclass;
2912     }
2913
2914     if (keyboard_state->visible_state) {
2915         ise_process_key_event(key, ret);
2916     } else {
2917         if (key.code == IME_KEY_space && (key.mask & IME_KEY_MASK_SHIFT)) {
2918             if (key.mask & IME_KEY_MASK_RELEASED) {
2919                 if (_language_manager.select_next_language()) {
2920                     LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
2921                     if (info) {
2922                         notification_status_message_post(info->display_name.c_str());
2923                     }
2924                 } else {
2925                     LOGE("Failed to change language");
2926                 }
2927             }
2928
2929             ret = TRUE;
2930         } else {
2931             ret = FALSE;
2932         }
2933     }
2934
2935     if (dev_name)
2936         free(dev_name);
2937
2938     return ret;
2939 }
2940
2941 static void ime_app_process_key_event_with_imengine_cb(scim::KeyEvent &key, uint32_t serial, void *user_data)
2942 {
2943     LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
2944     if (info && info->need_surrounding_text)
2945         engine_loader_process_key_event(key, serial, true);
2946     else
2947         engine_loader_process_key_event(key, serial, false);
2948 }
2949
2950 static void ime_app_caps_mode_changed_cb(int mode, void *user_data)
2951 {
2952     ise_set_caps_mode(mode);
2953 }
2954
2955 static void ime_app_candidate_show_cb(int context_id, void *user_data)
2956 {
2957 #ifdef _WEARABLE
2958     ise_check_wearable_candidate();
2959 #else
2960     ise_app_candidate_show();
2961 #endif
2962 }
2963
2964 static void ime_app_candidate_hide_cb(int context_id, void *user_data)
2965 {
2966 #ifdef _WEARABLE
2967     ise_check_wearable_candidate();
2968 #else
2969     ise_app_candidate_hide();
2970 #endif
2971 }
2972
2973 static void ime_app_lookup_table_changed_cb(Eina_List *list, void *user_data)
2974 {
2975     vector<string> candidate_strings;
2976     char *candidate;
2977     void *data;
2978     Eina_List *l;
2979
2980     g_lookup_table_strings.clear();
2981
2982     if (list) {
2983         EINA_LIST_FOREACH(list, l, data) {
2984             candidate = (char *)data;
2985             if (candidate) {
2986                 g_lookup_table_strings.push_back(string(candidate));
2987                 candidate_strings.push_back(string(candidate));
2988             }
2989         }
2990     }
2991
2992     if (input_smartreply_get_reply_num() > 0) {
2993         if (candidate_strings[0] == "#" && candidate_strings[1] == "$") {
2994             char *text = NULL;
2995             int cursor;
2996             ime_get_surrounding_text(0, 0, &text, &cursor);
2997             if (text)
2998                 free(text);
2999
3000             if (cursor == 0)
3001                 return;
3002         }
3003     }
3004
3005     update_candidate_table();
3006 }
3007
3008 #ifdef _WEARABLE
3009 static sclu32 get_rotate_layout(ime_input_device_rotary_direction_e direction)
3010 {
3011     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
3012     if (!keyboard_state) return ISE_LAYOUT_STYLE_NORMAL;
3013
3014     sclu32 new_layout = keyboard_state->layout;
3015
3016     if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE) {
3017         LOGD("CLOCKWISE\n");
3018         switch (keyboard_state->layout) {
3019             case ISE_LAYOUT_STYLE_NORMAL:
3020             case ISE_LAYOUT_STYLE_EMAIL:
3021             case ISE_LAYOUT_STYLE_URL:
3022             case ISE_LAYOUT_STYLE_PASSWORD:
3023                 if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
3024                         _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
3025                     ;   // PASSWORD NUMBER ONLY, do nothing
3026                 else
3027                     new_layout = ISE_LAYOUT_STYLE_NUMBER;
3028                 break;
3029             case ISE_LAYOUT_STYLE_NUMBER:
3030                 new_layout = ISE_LAYOUT_STYLE_HEX;
3031                 break;
3032             case ISE_LAYOUT_STYLE_HEX:
3033                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3034                         _context_layout == ISE_LAYOUT_STYLE_URL)
3035                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3036                 else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3037                     new_layout = _context_layout;
3038                 else
3039                     new_layout = ISE_LAYOUT_STYLE_EMOTICON;
3040                 break;
3041             case ISE_LAYOUT_STYLE_EMOTICON:
3042                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3043                         _context_layout == ISE_LAYOUT_STYLE_URL ||
3044                         _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3045                     new_layout = _context_layout;
3046                 else
3047                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3048                 break;
3049             case ISE_LAYOUT_STYLE_VOICE:
3050                 new_layout = ISE_LAYOUT_STYLE_NORMAL;
3051                 break;
3052             default:
3053                 ;
3054         }
3055     } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE) {
3056         LOGD("COUNTER_CLOCKWISE\n");
3057         switch (keyboard_state->layout) {
3058             case ISE_LAYOUT_STYLE_NORMAL:
3059             case ISE_LAYOUT_STYLE_EMAIL:
3060             case ISE_LAYOUT_STYLE_URL:
3061             case ISE_LAYOUT_STYLE_PASSWORD:
3062                 if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
3063                         _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
3064                     ;   // PASSWORD NUMBER ONLY, do nothing
3065                 else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3066                     new_layout = ISE_LAYOUT_STYLE_HEX;
3067                 else
3068                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3069                 break;
3070             case ISE_LAYOUT_STYLE_NUMBER:
3071                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3072                         _context_layout == ISE_LAYOUT_STYLE_URL ||
3073                         _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3074                     new_layout = _context_layout;
3075                 else
3076                     new_layout = ISE_LAYOUT_STYLE_NORMAL;
3077                 break;
3078             case ISE_LAYOUT_STYLE_HEX:
3079                 new_layout = ISE_LAYOUT_STYLE_NUMBER;
3080                 break;
3081             case ISE_LAYOUT_STYLE_EMOTICON:
3082                 new_layout = ISE_LAYOUT_STYLE_HEX;
3083                 break;
3084             case ISE_LAYOUT_STYLE_VOICE:
3085                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3086                         _context_layout == ISE_LAYOUT_STYLE_URL)
3087                     new_layout = ISE_LAYOUT_STYLE_HEX;
3088                 else
3089                     new_layout = ISE_LAYOUT_STYLE_EMOTICON;
3090                 break;
3091             default:
3092                 ;
3093         }
3094     }
3095
3096     return new_layout;
3097 }
3098
3099 static void ime_app_process_input_device_event_cb(ime_input_device_type_e device_type, ime_input_device_event_h device_event, void *user_data)
3100 {
3101     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
3102     if (!keyboard_state) return;
3103     if (device_type != IME_INPUT_DEVICE_TYPE_ROTARY) return;
3104
3105     ime_input_device_rotary_direction_e direction;
3106     if (IME_ERROR_NONE != ime_input_device_rotary_get_direction(device_event, &direction)) return;
3107
3108     sclu32 new_layout = get_rotate_layout(direction);
3109
3110     CONFIG_VALUES *config_values = get_config_values();
3111     if (check_is_tutorial_show() && config_values) {
3112         read_ise_config_values();
3113         if ((direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && !config_values->number_tutorial_enable && !config_values->symbol_tutorial_enable) ||
3114                 (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && config_values->symbol_tutorial_enable)) {
3115             new_layout = keyboard_state->layout;
3116         } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && config_values->symbol_tutorial_enable) {
3117             ise_destroy_tutorial_mode_popup();
3118             config_values->symbol_tutorial_enable = false;
3119             write_ise_config_values();
3120         } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && !config_values->number_tutorial_enable) {
3121             ise_destroy_tutorial_mode_popup();
3122             new_layout = ISE_LAYOUT_STYLE_NUMBER;
3123             if (!config_values->symbol_tutorial_enable) {
3124                 ise_show_tutorial_mode_popup(new_layout);
3125                 config_values->symbol_tutorial_enable = true;
3126                 write_ise_config_values();
3127             }
3128         }
3129     }
3130
3131     if (new_layout != keyboard_state->layout && new_layout < ISE_LAYOUT_STYLE_MAX) {
3132         keyboard_state->need_reset = TRUE;
3133         keyboard_state->layout = new_layout;
3134
3135         if (keyboard_state->visible_state) {
3136             _reset_multitap_state();
3137             if (config_values) {
3138                 _language_manager.reset_language(config_values->selected_language.c_str());
3139             }
3140             ise_show(keyboard_state->ic);
3141         }
3142     }
3143 }
3144 #endif
3145
3146 static void ime_app_prediction_hint_set_cb(const char *prediction_hint, void *user_data)
3147 {
3148     char *sender = (char *)"mms";
3149     char *caller_id = (char *)"mms";
3150     char *hint = (char *)prediction_hint;
3151
3152     if (!prediction_hint) return;
3153     SECURE_LOGD("prediction hint : %s\n", prediction_hint);
3154
3155     if (strlen(prediction_hint) > 0) {
3156         input_smartreply_init(caller_id, sender, hint);
3157         input_smartreply_set_notify(_input_smartreply_notify_cb, NULL);
3158
3159         if (input_smartreply_is_enabled()) {
3160             input_smartreply_get_reply_async();
3161         }
3162     }
3163 }
3164
3165 static void ime_app_mime_type_set_request_cb(const char *mime_types, void *user_data)
3166 {
3167     LOGD("mime type : %s\n", mime_types);
3168
3169     clipboard_set_mime_type(true);
3170     clipboard_init_sel_type();
3171
3172     string str(mime_types), text_key = "text/", image_key = "image/";
3173
3174     if (str.find(text_key) != string::npos)
3175         clipboard_add_sel_type(CLIPBOARD_SEL_TEXT);
3176
3177     if (str.find(image_key) != string::npos)
3178         clipboard_add_sel_type(CLIPBOARD_SEL_IMAGE);
3179 }
3180
3181 static void ime_app_prediction_hint_data_set_cb(const char *key, const char *value, void *user_data)
3182 {
3183     SECURE_LOGD("key : %s, value : %s\n", key, value);
3184
3185     if (string(key) == "appid")
3186         ime_autofill_set_app_id(value);
3187     else if (string(key) == "res_id")
3188         ime_autofill_set_resource_id(value);
3189 }
3190
3191 static void ime_app_autocapital_type_set_cb(uint32_t type, void *user_data)
3192 {
3193     LOGD("autocapital type : %u\n", type);
3194     engine_loader_set_autocapital_type(type);
3195 }
3196
3197 static void ime_app_prediction_allow_set_cb(uint32_t prediction_allow, void *user_data)
3198 {
3199     LOGD("prediction allow : %u\n", prediction_allow);
3200     engine_loader_set_prediction_allow(prediction_allow);
3201 }
3202
3203 static void ime_app_trigger_property_set_cb(const char *property, void *user_data)
3204 {
3205     LOGD("trigger property : %s\n", property);
3206     engine_loader_trigger_property(property);
3207 }
3208
3209 static void ime_app_candidate_more_window_show_cb(void *user_data)
3210 {
3211     LOGD("");
3212     engine_loader_show_candidate_more_window();
3213 }
3214
3215 static void ime_app_candidate_more_window_hide_cb(void *user_data)
3216 {
3217     LOGD("");
3218     engine_loader_hide_candidate_more_window();
3219 }
3220
3221 static void ime_app_aux_select_cb(uint32_t item, void *user_data)
3222 {
3223     LOGD("aux select : %u\n", item);
3224     engine_loader_select_aux(item);
3225 }
3226
3227 static void ime_app_candidate_select_cb(uint32_t item, void *user_data)
3228 {
3229     LOGD("candidate select : %u\n", item);
3230     engine_loader_select_candidate(item);
3231 }
3232
3233 static void ime_app_candidate_table_page_up_cb(void *user_data)
3234 {
3235     LOGD("");
3236     engine_loader_candidate_table_page_up();
3237 }
3238
3239 static void ime_app_candidate_table_page_down_cb(void *user_data)
3240 {
3241     LOGD("");
3242     engine_loader_candidate_table_page_down();
3243 }
3244
3245 static void ime_app_candidate_table_page_size_chaned_cb(uint32_t size, void *user_data)
3246 {
3247     LOGD("candidate page size : %u\n", size);
3248     engine_loader_change_candidate_page_size(size);
3249 }
3250
3251 static void ime_app_candidate_item_layout_set_cb(vector<uint32_t> item, void *user_data)
3252 {
3253     LOGD("item layout size : %zu", item.size());
3254     engine_loader_set_candidate_item_layout(item);
3255 }
3256
3257 static void ime_app_displayed_candidate_number_chaned_cb(uint32_t page_num, void *user_data)
3258 {
3259     LOGD("candidate number changed : %u\n", page_num);
3260     engine_loader_change_candidate_number(page_num);
3261 }
3262
3263 static void ime_app_candidate_item_long_pressed_cb(uint32_t index, void *user_data)
3264 {
3265     LOGD("candidate item : %u\n", index);
3266     engine_loader_long_press_candidate_item(index);
3267 }
3268
3269 #ifdef __cplusplus
3270 extern "C"{
3271 #endif
3272 EXPORTED void ime_app_main(int argc, char **argv)
3273 {
3274     ime_callback_s basic_callback = {
3275         ime_app_create_cb,
3276         ime_app_exit_cb,
3277         ime_app_show_cb,
3278         ime_app_hide_cb
3279     };
3280
3281     ime_event_set_focus_in_cb(ime_app_focus_in_cb, NULL);
3282     ime_event_set_focus_out_cb(ime_app_focus_out_cb, NULL);
3283     ime_event_set_rotation_degree_changed_cb(ime_app_rotation_degree_changed_cb, NULL);
3284     ime_event_set_accessibility_state_changed_cb(ime_app_accessibility_state_changed_cb, NULL);
3285     ime_event_set_layout_set_cb(ime_app_layout_set_cb, NULL);
3286     ime_event_set_caps_mode_changed_cb(ime_app_caps_mode_changed_cb, NULL);
3287     ime_event_set_cursor_position_updated_cb(ime_app_cursor_position_updated_cb, NULL);
3288     ime_event_set_surrounding_text_updated_cb(ime_app_surrounding_text_updated_cb, NULL);
3289     ime_event_set_return_key_type_set_cb(ime_app_return_key_type_set_cb, NULL);
3290     ime_event_set_return_key_state_set_cb(ime_app_return_key_state_set_cb, NULL);
3291     ime_event_set_language_set_cb(ime_app_language_set_cb, NULL);
3292     ime_event_set_imdata_set_cb(ime_app_imdata_set_cb, NULL);
3293     ime_event_set_process_key_event_cb(ime_app_process_key_event_cb, NULL);
3294     ime_event_set_process_key_event_with_imengine_cb(ime_app_process_key_event_with_imengine_cb, NULL);
3295     ime_event_set_input_hint_set_cb(ime_app_input_hint_set_cb, NULL);
3296
3297     ime_event_set_candidate_show_cb(ime_app_candidate_show_cb, NULL);
3298     ime_event_set_candidate_hide_cb(ime_app_candidate_hide_cb, NULL);
3299     ime_event_set_lookup_table_changed_cb(ime_app_lookup_table_changed_cb, NULL);
3300
3301 #ifdef _WEARABLE
3302     ime_event_set_process_input_device_event_cb(ime_app_process_input_device_event_cb, NULL);
3303 #endif
3304
3305     ime_event_set_input_context_reset_cb(ime_app_input_context_reset_cb, NULL);
3306     ime_event_set_language_requested_cb(ime_app_language_requested_cb, NULL);
3307
3308     ime_event_set_prediction_hint_set_cb(ime_app_prediction_hint_set_cb, NULL);
3309     ime_event_set_mime_type_set_request_cb(ime_app_mime_type_set_request_cb, NULL);
3310     ime_event_set_prediction_hint_data_set_cb(ime_app_prediction_hint_data_set_cb, NULL);
3311
3312     ime_event_set_autocapital_type_set_cb(ime_app_autocapital_type_set_cb, NULL);
3313     ime_event_set_prediction_allow_set_cb(ime_app_prediction_allow_set_cb, NULL);
3314     ime_event_set_trigger_property_set_cb(ime_app_trigger_property_set_cb, NULL);
3315     ime_event_set_candidate_more_window_show_cb(ime_app_candidate_more_window_show_cb, NULL);
3316     ime_event_set_candidate_more_window_hide_cb(ime_app_candidate_more_window_hide_cb, NULL);
3317     ime_event_set_aux_select_cb(ime_app_aux_select_cb, NULL);
3318     ime_event_set_candidate_select_cb(ime_app_candidate_select_cb, NULL);
3319     ime_event_set_candidate_table_page_up_cb(ime_app_candidate_table_page_up_cb, NULL);
3320     ime_event_set_candidate_table_page_down_cb(ime_app_candidate_table_page_down_cb, NULL);
3321     ime_event_set_candidate_table_page_size_chaned_cb(ime_app_candidate_table_page_size_chaned_cb, NULL);
3322     ime_event_set_candidate_item_layout_set_cb(ime_app_candidate_item_layout_set_cb, NULL);
3323     ime_event_set_displayed_candidate_number_chaned_cb(ime_app_displayed_candidate_number_chaned_cb, NULL);
3324     ime_event_set_candidate_item_long_pressed_cb(ime_app_candidate_item_long_pressed_cb, NULL);
3325
3326 #if DEFER_ISE_CREATION
3327     ime_set_window_creation_defer_flag(TRUE);
3328 #endif
3329
3330     ime_run(&basic_callback, NULL);
3331 }
3332 #ifdef __cplusplus
3333 }
3334 #endif
3335
3336 EXPORTED int main(int argc, char *argv[])
3337 {
3338     ime_app_main(argc, argv);
3339
3340     return 0;
3341 }