Remove unused code
[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     }
2089
2090     if (g_candidate) {
2091         delete g_candidate;
2092         g_candidate = NULL;
2093     }
2094
2095 #ifdef _WEARABLE
2096     destroy_indicator_window();
2097 #endif
2098
2099     /* This is necessary. If this is not called, 3rd party IME might have auto period input regardless its settings */
2100     vconf_set_bool(VCONFKEY_AUTOPERIOD_ALLOW_BOOL, false);
2101
2102 #if EXIT_ISE_ON_HIDE
2103     if (exit_timer) ecore_timer_del(exit_timer);
2104     exit_timer = NULL;
2105 #endif
2106 }
2107
2108 void
2109 ise_app_candidate_show()
2110 {
2111 #ifdef _WEARABLE
2112     if (!g_candidate)
2113         return;
2114
2115     if (!g_candidate->get_visible())
2116         return;
2117 #endif
2118     LOGD("Enter\n");
2119     delete_softcandidate_hide_timer();
2120
2121     create_softcandidate();
2122
2123     Candidate *candidate = get_candidate();
2124     if (candidate) {
2125         candidate->show();
2126         set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
2127     }
2128 }
2129
2130 void
2131 ise_app_candidate_hide()
2132 {
2133     LOGD("Enter\n");
2134
2135     Candidate *candidate = get_candidate();
2136     if (!candidate || !candidate->get_visible()) {
2137         LOGD("No candidate\n");
2138         return;
2139     }
2140
2141     if (g_ic_smartreply != -1 || ime_autofill_get_exist())
2142         return;
2143
2144     add_softcandidate_hide_timer();
2145 }
2146
2147 #ifdef _WEARABLE
2148 void ise_check_wearable_candidate()
2149 {
2150     CONFIG_VALUES *config_values = get_config_values();
2151     if (!config_values) return;
2152
2153     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2154     if (!keyboard_state) return;
2155
2156     if (!config_values->prediction_on) {
2157         ise_app_candidate_hide();
2158     } else if (keyboard_state->layout == ISE_LAYOUT_STYLE_PHONENUMBER ||
2159                   keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE ||
2160                   keyboard_state->layout == ISE_LAYOUT_STYLE_IP ||
2161                   keyboard_state->layout == ISE_LAYOUT_STYLE_MONTH ||
2162                   keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBERONLY ||
2163                   keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWD_3X4||
2164                   keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWORD) {
2165         ise_app_candidate_hide();
2166     } else {
2167         ise_app_candidate_show();
2168     }
2169 }
2170
2171 void ise_set_emoticon_label(int group_id)
2172 {
2173     const int BUF_LEN = 16;
2174     char buf[BUF_LEN] = {0};
2175     snprintf(buf, BUF_LEN, "%d/3", group_id);
2176     CSCLUI *ui = get_ui();
2177     if (ui) ui->set_private_key("EMOTICON_GROUP_ID", buf, NULL, NULL, 0, const_cast<sclchar*>("EMOTICON_GROUP_NEXT"), TRUE);
2178 }
2179 #endif
2180
2181 // when it is the time to auto_cap, the
2182 // ise_set_caps_mode is called.
2183 // -------------------------------------------------------
2184 // For example: [How are you. Fine.], the
2185 // auto-capital process is as below:
2186 // Note: "["<--this is the beginning,
2187 // "|"<--this is the cursor position
2188 // 1) call ise_set_caps_mode, auto_cap = on
2189 //    input: "H",
2190 //    result: [H|
2191 // 2) call ise_set_caps_mode, auto_cap = off
2192 //    input: "o"
2193 //    result: [Ho|
2194 // 3) input: "w are you. "
2195 //    result: [How are you. |
2196 // 4) call ise_set_caps_mode, auto_cap = on
2197 //    input: "F"
2198 //    result: [How are you. F
2199 // 5) input: "ine."
2200 //    result: [How are you. Fine.|
2201 // --------------------------------------------------------
2202 // If we want to change the auto_cap, eg,
2203 // if we want to input [How Are you.]
2204 // Note the "Are" is not use auto-capital rule.
2205 // we should use:
2206 //    ise_send_event(MVK_Shift_On, SclCoreKeyMask_Null);
2207 // when we are want to input "A"
2208 // following input still has the auto_cap rule.
2209 void
2210 ise_set_caps_mode(unsigned int mode)
2211 {
2212     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2213     if (!keyboard_state) return;
2214
2215     LOGD("caps_mode : %d\n", mode);
2216     if (mode) {
2217         keyboard_state->caps_mode = TRUE;
2218     } else {
2219         keyboard_state->caps_mode = FALSE;
2220     }
2221     g_caps_mode_pending = false;
2222     const sclchar *cur_lang = _language_manager.get_current_language();
2223     if (cur_lang) {
2224         LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
2225         if (info) {
2226             if (info->accepts_caps_mode) {
2227                 /* If we are inputting multitap character, do not manipulate shift mode */
2228                 if (keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_START &&
2229                     keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_REPEAT) {
2230                     set_caps_mode(keyboard_state->caps_mode);
2231                 } else {
2232                     g_caps_mode_pending = true;
2233                     LOGD("Currently composing multitap string, skipping caps request");
2234                 }
2235             }
2236         }
2237     }
2238 }
2239
2240 void
2241 ise_update_cursor_position(int position)
2242 {
2243     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2244     if (!keyboard_state) return;
2245
2246     LOGD("cursor position : %d\n", position);
2247     CSCLUI *ui = get_ui();
2248     if (ui && keyboard_state->layout == ISE_LAYOUT_STYLE_URL) {
2249 #ifndef _TV
2250         if (position > 0) {
2251             ui->set_string_substitution("www.", ".com");
2252         } else {
2253             ui->unset_string_substitution("www.");
2254         }
2255 #endif
2256     }
2257 }
2258
2259 void ise_set_return_key_type(unsigned int type)
2260 {
2261     const int BUF_LEN = 256;
2262     char buf[BUF_LEN] = {0};
2263
2264     CSCLUI *ui = get_ui();
2265     if (!ui) return;
2266
2267     LOGD("return key type : %d\n", type);
2268     switch (type)
2269     {
2270     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DONE:
2271         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_DONE);
2272         break;
2273     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_GO:
2274         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_GO);
2275         break;
2276     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_JOIN:
2277         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_JOIN);
2278         break;
2279     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_LOGIN:
2280         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_LOGIN);
2281         break;
2282     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_NEXT:
2283         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_NEXT);
2284         break;
2285     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH:
2286         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEARCH);
2287         break;
2288     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEND:
2289         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEND);
2290         break;
2291     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SIGNIN:
2292         snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SIGNIN);
2293         break;
2294     case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT:
2295         break;
2296     default:
2297         LOGW("Unknown return key type : %d\n", type);
2298         type = ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT;
2299         break;
2300     }
2301
2302     if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT) {
2303         ui->unset_private_key("Enter");
2304 #ifdef _TV
2305         ui->unset_private_key("Done");
2306 #endif
2307     } else {
2308 #ifdef _WEARABLE
2309         if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH) {
2310             sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
2311                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search.png"),
2312                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search_p.png"),
2313                 const_cast<sclchar*>("w_sip_3x4_btn_ic_search_d.png")};
2314             ui->set_private_key("Enter", const_cast<sclchar*>(""), imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
2315             return;
2316         }
2317 #endif
2318
2319 #ifdef _TV
2320         ui->set_private_key("Done", buf, NULL, NULL, 0, const_cast<sclchar*>("Done"), TRUE);
2321 #else
2322         static sclchar *imagelabel[SCL_BUTTON_STATE_MAX] = {
2323             const_cast<sclchar*>(" "), const_cast<sclchar*>(" "), const_cast<sclchar*>(" ")
2324         };
2325
2326         ui->set_private_key("Enter", buf, imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
2327 #endif
2328         LOGD("return key label : %s\n", buf);
2329     }
2330 }
2331
2332 void ise_set_return_key_disable(unsigned int disabled)
2333 {
2334     CSCLUI *ui = get_ui();
2335     LOGD("enable : %d\n", !disabled);
2336 #ifdef _TV
2337     if (ui)
2338         ui->enable_button("Done", !disabled);
2339 #else
2340     if (ui)
2341         ui->enable_button("Enter", !disabled);
2342 #endif
2343 }
2344
2345 void ise_get_language_locale(char **locale)
2346 {
2347     LANGUAGE_INFO *info = _language_manager.get_current_language_info();
2348     if (info) {
2349         if (!(info->locale_string.empty())) {
2350             *locale = strdup(info->locale_string.c_str());
2351         }
2352     }
2353 }
2354
2355 void ise_update_table(const vector<string> &vec_str)
2356 {
2357     Candidate *candidate = get_candidate();
2358     if (!candidate || !candidate->get_visible()) {
2359         create_softcandidate();
2360     }
2361
2362     candidate = get_candidate();
2363     if (candidate) {
2364         candidate->update(vec_str);
2365     }
2366 }
2367
2368 void ise_process_key_event(scim::KeyEvent& key, sclu32 &ret)
2369 {
2370     Eina_Bool back_key_pressed = EINA_FALSE;
2371     Eina_Bool back_key_released = EINA_FALSE;
2372
2373     if (key.get_key_string().compare("XF86Back") == 0) {
2374         back_key_pressed = EINA_TRUE;
2375     } else if (key.get_key_string().compare("KeyRelease+XF86Back") == 0) {
2376         back_key_released = EINA_TRUE;
2377     }
2378
2379     if (back_key_pressed || back_key_released) {
2380         if (g_popup_opened == TRUE) {
2381             if (back_key_released) {
2382                 CSCLUI *ui = get_ui();
2383                 if (ui) ui->close_all_popups();
2384             }
2385             ret = 1;
2386             return;
2387         }
2388     }
2389
2390     ret = 0;
2391     CSCLUI *ui = get_ui();
2392 #ifdef _TV
2393     Candidate *candidate = get_candidate();
2394     if (candidate) {
2395         if (!candidate->soft_candidate_flag()) {
2396             if (ui) {
2397                 if (key.dev_name.compare("ime") != 0) {
2398                     ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2399                 }
2400             }
2401         }
2402         if (!ret) {
2403             ret = candidate->soft_candidate_handle_key_event(key.get_key_string().c_str());
2404         }
2405     } else {
2406         if (ui) {
2407             /* Process this key event if it was not generated by ime */
2408             if (key.dev_name.compare("ime") != 0) {
2409                 ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2410             }
2411         }
2412     }
2413 #else
2414     if (ui) {
2415         /* Process this key event if it was not generated by ime */
2416         if (key.dev_name.compare("ime") != 0) {
2417             ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
2418         }
2419     }
2420 #endif
2421 }
2422
2423 static void init_recent_used_punctuation()
2424 {
2425     if (g_recent_used_punctuation.empty())
2426     {
2427         g_recent_used_punctuation.push_back("#");
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     }
2433 }
2434
2435 static void update_recent_used_punctuation(const char * key_value)
2436 {
2437     if (NULL == key_value)
2438     {
2439         return;
2440     }
2441     for (int i = 0; i < 10; ++i)
2442     {
2443         char buf[5] = {0};
2444         snprintf(buf, sizeof(buf), "%d", i);
2445         if (strcmp(key_value, buf) == 0)
2446         {
2447             return;
2448         }
2449     }
2450     string strKey = string(key_value);
2451     for (int i = 0; i < MAX_DEFAULT_PUNCTUATION; ++i)
2452     {
2453         if (0 == strKey.compare(g_default_punctuation[i].c_str()))
2454         {
2455             return;
2456         }
2457     }
2458     vector<string>::iterator iter = g_recent_used_punctuation.begin();
2459     for (; iter != g_recent_used_punctuation.end(); ++iter)
2460     {
2461         if (0 == strKey.compare(iter->c_str()))
2462         {
2463             break;
2464         }
2465     }
2466     if (iter != g_recent_used_punctuation.end())
2467     {
2468         g_recent_used_punctuation.erase(iter);
2469     }
2470     g_recent_used_punctuation.push_back(strKey);
2471     if (g_recent_used_punctuation.size() > MAX_DEFAULT_PUNCTUATION-1)
2472     {
2473         g_recent_used_punctuation.erase(g_recent_used_punctuation.begin());
2474     }
2475 }
2476
2477 static void set_ime_size(bool floating_mode, ISE_CANDIDATE_REQUEST candidate_req)
2478 {
2479     CSCLUI *ui = get_ui();
2480     if (!ui)
2481         return;
2482
2483     Candidate *candidate = get_candidate();
2484
2485     const char *input_mode = ui->get_input_mode();
2486     if (!input_mode)
2487         return;
2488
2489     SclSize size_portrait = ui->get_input_mode_size(input_mode, DISPLAYMODE_PORTRAIT);
2490     SclSize size_landscape = ui->get_input_mode_size(input_mode, DISPLAYMODE_LANDSCAPE);
2491
2492     if (floating_mode) {
2493         size_portrait.width *= FLOATING_SCALE_RATE;
2494         size_portrait.height *= FLOATING_SCALE_RATE;
2495         size_landscape.width *= FLOATING_SCALE_RATE;
2496         size_landscape.height *= FLOATING_SCALE_RATE;
2497         switch (candidate_req) {
2498             case ISE_CANDIDATE_REQ_NONE:
2499                 if (candidate && candidate->get_visible()) {
2500                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
2501                     size_portrait.height += candidate->get_height();
2502                     size_landscape.height += candidate->get_height();
2503                 } else {
2504                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
2505                 }
2506                 break;
2507             case ISE_CANDIDATE_REQ_SHOW:
2508                 if (candidate) {
2509                     ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
2510                     if (g_input_panel_show || g_candidate_more_view) {
2511                         size_portrait.height += candidate->get_height();
2512                         size_landscape.height += candidate->get_height();
2513                     } else {
2514                         size_portrait.height = candidate->get_height();
2515                         size_landscape.height = candidate->get_height();
2516                     }
2517                 }
2518                 break;
2519             case ISE_CANDIDATE_REQ_HIDE:
2520                 ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
2521                 break;
2522             default: break;
2523         }
2524
2525         if (ui->get_custom_scale_rate_x() != FLOATING_SCALE_RATE || ui->get_custom_scale_rate_y() != FLOATING_SCALE_RATE)
2526             ui->set_custom_scale_rate(FLOATING_SCALE_RATE, FLOATING_SCALE_RATE);
2527
2528         ime_set_size(size_portrait.width, size_portrait.height + FLOATING_TITLE_BAR_HEIGHT,
2529             size_landscape.width, size_landscape.height + FLOATING_TITLE_BAR_HEIGHT);
2530
2531 #if defined(_MOBILE) || defined(_COMMON)
2532         ise_destroy_move_handler();
2533         int rotation = elm_win_rotation_get(NATIVE_WINDOW_CAST(ime_get_main_window()));
2534         int handler_width = (rotation == 0 || rotation == 180) ? size_portrait.width : size_landscape.width;
2535         ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
2536 #endif
2537     } else {
2538         switch (candidate_req) {
2539             case ISE_CANDIDATE_REQ_NONE:
2540                 if (candidate && candidate->get_visible()) {
2541                     ui->set_custom_starting_coordinates(0, candidate->get_height());
2542                     size_portrait.height += candidate->get_height();
2543                     size_landscape.height += candidate->get_height();
2544                 } else {
2545                     ui->set_custom_starting_coordinates(0, 0);
2546                 }
2547                 break;
2548             case ISE_CANDIDATE_REQ_SHOW:
2549                 if (candidate) {
2550                     ui->set_custom_starting_coordinates(0, candidate->get_height());
2551                     if (g_input_panel_show || g_candidate_more_view) {
2552                         size_portrait.height += candidate->get_height();
2553                         size_landscape.height += candidate->get_height();
2554                     } else {
2555                         size_portrait.height = candidate->get_height();
2556                         size_landscape.height = candidate->get_height();
2557                     }
2558                 }
2559                 break;
2560             case ISE_CANDIDATE_REQ_HIDE:
2561                 ui->set_custom_starting_coordinates(0, 0);
2562                 break;
2563             default: break;
2564         }
2565
2566         if (ui->get_custom_scale_rate_x() != 1.0 || ui->get_custom_scale_rate_y() != 1.0)
2567             ui->set_custom_scale_rate(1.0, 1.0);
2568
2569         ime_set_size(size_portrait.width, size_portrait.height, size_landscape.width, size_landscape.height);
2570
2571 #if defined(_MOBILE) || defined(_COMMON)
2572         ise_destroy_move_handler();
2573 #endif
2574     }
2575 }
2576
2577 static void ime_app_create_cb(void *user_data)
2578 {
2579     if (!engine_loader_dbus_init(NULL))
2580         LOGE("Failed to initialize dbus");
2581
2582 #if !(DEFER_ISE_CREATION)
2583     ise_create();
2584 #endif
2585     elm_app_name_set(PACKAGE);
2586
2587     char *elm_scale = getenv("ELM_SCALE");
2588     LOGD("ELM_SCALE : %s", elm_scale);
2589
2590     if (elm_scale)
2591         elm_app_base_scale_set(atof(elm_scale));
2592
2593     clipboard_init();
2594
2595     nmt_init();
2596 }
2597
2598 static void ime_app_exit_cb(void *user_data)
2599 {
2600     ise_hide();
2601     ise_destroy();
2602
2603     if (!engine_loader_dbus_shutdown())
2604         LOGE("Failed to finalize dbus");
2605
2606     clipboard_shutdown();
2607
2608     nmt_shutdown();
2609 }
2610
2611 static void show_autofill_data()
2612 {
2613     ime_autofill_set_exist(false);
2614     string autofill_string = ime_autofill_get_string();
2615
2616     SECURE_LOGD("autofill string : %s", autofill_string.c_str());
2617
2618     if (!autofill_string.empty()) {
2619         ime_autofill_set_exist(true);
2620
2621         ise_app_candidate_show();
2622         update_candidate_table();
2623     }
2624 }
2625
2626 static void ime_app_show_cb(int ic, ime_context_h ime_ctx, void *user_data)
2627 {
2628     Ise_Context iseContext;
2629     bool return_key_state, prediction_allow, password_mode, caps_mode;
2630     ime_layout_variation_e layout_variation;
2631
2632     if (!g_ise_created)
2633         ise_create();
2634
2635     ime_context_get_layout(ime_ctx, &iseContext.layout);
2636
2637     ime_context_get_layout_variation(ime_ctx, &layout_variation);
2638     iseContext.layout_variation =  (int)layout_variation;
2639
2640     ime_context_get_cursor_position(ime_ctx, &iseContext.cursor_pos);
2641     ime_context_get_autocapital_type(ime_ctx, &iseContext.autocapital_type);
2642     ime_context_get_return_key_type(ime_ctx, &iseContext.return_key_type);
2643     ime_context_get_return_key_state(ime_ctx, &return_key_state);
2644
2645     ime_context_get_prediction_mode(ime_ctx, &prediction_allow);
2646     iseContext.prediction_allow = prediction_allow;
2647
2648     ime_context_get_password_mode(ime_ctx, &password_mode);
2649     iseContext.password_mode = password_mode;
2650
2651     ime_context_get_input_hint(ime_ctx, &iseContext.input_hint);
2652     ime_context_get_bidi_direction(ime_ctx, &iseContext.bidi_direction);
2653     ime_context_get_language(ime_ctx, &iseContext.language);
2654     ime_context_get_caps_mode(ime_ctx, &caps_mode);
2655     iseContext.caps_mode = caps_mode;
2656
2657     iseContext.return_key_disabled = return_key_state;
2658
2659     engine_loader_set_input_hint((uint32_t)iseContext.input_hint);
2660     engine_loader_update_bidi_direction((uint32_t)iseContext.bidi_direction);
2661
2662     g_ic = ic;
2663
2664     ime_autofill_set_hint(iseContext.input_hint & ECORE_IMF_INPUT_HINT_AUTOFILL_MASK);
2665
2666     LOGD("input hint : %x, autofill hint : %x\n", iseContext.input_hint, ime_autofill_get_hint());
2667
2668     // show autofill data
2669     show_autofill_data();
2670
2671     //g_ise_common->set_keyboard_ise_by_uuid(KEYBD_ISE_UUID);
2672
2673     /* Don't update screen until all the information is correctly set */
2674     CSCLUI *ui = get_ui();
2675     if (ui)
2676         ui->set_update_pending(TRUE);
2677
2678     ise_reset_context(); // reset ISE
2679
2680     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2681
2682     if (iseContext.language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
2683         LOGD("prefer latin");
2684         if (keyboard_state)
2685             keyboard_state->prefer_latin = TRUE;
2686     }
2687     else {
2688         LOGD("prefer automatic");
2689         if (keyboard_state)
2690             keyboard_state->prefer_latin = FALSE;
2691     }
2692
2693     _context_layout = iseContext.layout;
2694     _context_layout_variation = iseContext.layout_variation;
2695     ise_set_layout(iseContext.layout, iseContext.layout_variation);
2696
2697     ise_set_return_key_type(iseContext.return_key_type);
2698     ise_set_return_key_disable(iseContext.return_key_disabled);
2699
2700     ise_set_caps_mode(iseContext.caps_mode);
2701     ise_update_cursor_position(iseContext.cursor_pos);
2702
2703     ise_show(ic);
2704
2705     /* Now we update the whole screen */
2706     if (ui)
2707         ui->set_update_pending(FALSE);
2708 }
2709
2710 static void ime_app_hide_cb(int ic, void *user_data)
2711 {
2712     LOGD("Enter\n");
2713     ise_hide();
2714
2715     clipboard_set_mime_type(false);
2716
2717     g_ic_smartreply = -1;
2718 }
2719
2720 static void ime_app_return_key_type_set_cb(Ecore_IMF_Input_Panel_Return_Key_Type type, void *user_data)
2721 {
2722     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2723     if (!keyboard_state) return;
2724
2725     LOGD("return key type : %d\n", type);
2726     ise_set_return_key_type(type);
2727
2728     if (keyboard_state->visible_state)
2729         ise_show(keyboard_state->ic);
2730 }
2731
2732 static void ime_app_return_key_state_set_cb(bool disabled, void *user_data)
2733 {
2734     LOGD("return key disabled : %d\n", disabled);
2735     ise_set_return_key_disable(disabled);
2736 }
2737
2738 static void ime_app_language_set_cb(Ecore_IMF_Input_Panel_Lang language, void *user_data)
2739 {
2740     LOGD("language : %d\n", language);
2741
2742     // if (language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
2743     //     ise_explictly_set_language(PRIMARY_LATIN_LANGUAGE_INDEX);
2744     // }
2745 }
2746
2747 static void ime_app_input_context_reset_cb(void *user_data)
2748 {
2749     ise_reset_input_context();
2750     engine_loader_reset_input_context();
2751 }
2752
2753 static void ime_app_cursor_position_updated_cb(int cursor_pos, void *user_data)
2754 {
2755     LOGD("cursor position : %d\n", cursor_pos);
2756     ise_update_cursor_position(cursor_pos);
2757     engine_loader_update_cursor_position(cursor_pos);
2758 }
2759
2760 static void ime_app_language_requested_cb(void *user_data, char **lang_code)
2761 {
2762     ise_get_language_locale(lang_code);
2763 }
2764
2765 static void ime_app_surrounding_text_updated_cb(int context_id, const char *text, int cursor_pos, void *user_data)
2766 {
2767     SECURE_LOGD("surrounding text:%s, cursor=%d\n", text, cursor_pos);
2768     ime_delete_surrounding_text(-cursor_pos, strlen(text));
2769 }
2770
2771 static void ime_app_focus_in_cb(int context_id, void *user_data)
2772 {
2773     LOGD("Enter\n");
2774     ise_focus_in(context_id);
2775     Candidate *candidate = get_candidate();
2776     if (input_smartreply_get_reply_num() == 0) {
2777         if (candidate && candidate->get_visible()) {
2778             ise_app_candidate_hide();
2779             candidate->hide();
2780         }
2781     }
2782
2783     engine_loader_focus_in();
2784 }
2785
2786 static void ime_app_focus_out_cb(int context_id, void *user_data)
2787 {
2788     LOGD("Enter\n");
2789     ise_focus_out(context_id);
2790     g_imdata_state = 0;
2791     clipboard_set_mime_type(false);
2792     input_smartreply_deinit();
2793     g_ic_smartreply = -1;
2794
2795     ime_autofill_set_exist(false);
2796     ime_autofill_set_hint(0);
2797     ime_autofill_set_app_id("");
2798     ime_autofill_set_resource_id("");
2799
2800     g_smartreply_strings.clear();
2801     g_lookup_table_strings.clear();
2802     g_softcandidate_string.clear();
2803
2804     engine_loader_focus_out();
2805 }
2806
2807 static void ime_app_layout_set_cb(Ecore_IMF_Input_Panel_Layout layout, void *user_data)
2808 {
2809     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2810     if (!keyboard_state) return;
2811
2812     LOGD("layout=%d\n", layout);
2813     /* Check if the layoutIdx is in the valid range */
2814     if (static_cast<int>(layout) < static_cast<int>(ISE_LAYOUT_STYLE_MAX)) {
2815         if (keyboard_state->layout != layout) {
2816             keyboard_state->need_reset = TRUE;
2817         }
2818         keyboard_state->layout = layout;
2819         _context_layout = layout;
2820         _context_layout_variation = 0;
2821     }
2822     if (keyboard_state->visible_state)
2823         ise_show(keyboard_state->ic);
2824
2825     engine_loader_set_layout(static_cast<uint32_t>(layout));
2826 }
2827
2828 static void ime_app_input_hint_set_cb(Ecore_IMF_Input_Hints input_hint, void *user_data)
2829 {
2830     LOGD("input hint=%u\n", input_hint);
2831 }
2832
2833 static void ime_app_rotation_degree_changed_cb(int degree, void *user_data)
2834 {
2835     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2836     if (!keyboard_state) return;
2837
2838     CSCLUI *ui = get_ui();
2839     ise_set_screen_rotation(degree);
2840
2841     LOGD("degree=%d\n", degree);
2842 #if defined(_MOBILE) || defined(_COMMON)
2843     if (ui && g_floating_mode) {
2844         int handler_width;
2845         ise_destroy_move_handler();
2846         if (degree == 0 || degree == 180) {
2847             SclSize size_portrait = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_PORTRAIT);
2848             handler_width = size_portrait.width * FLOATING_SCALE_RATE;
2849         } else {
2850             SclSize size_landscape = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_LANDSCAPE);
2851             handler_width = size_landscape.width * FLOATING_SCALE_RATE;
2852         }
2853         ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
2854     }
2855 #endif
2856     if (ise_emoticon_is_show()) {
2857         ise_emoticon_destroy_layout();
2858     }
2859     if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
2860         ise_hide_stt_mode();
2861     }
2862     if (keyboard_state->layout == ISE_LAYOUT_STYLE_EMOTICON) {
2863         ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
2864     } else if (ui) {
2865         const sclchar *input_mode = ui->get_input_mode();
2866         if (input_mode) {
2867             if (!(strcmp(input_mode, "EMOTICON_LAYOUT")))
2868                 ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
2869             else if (!(strcmp(input_mode, "STT_3X4")))
2870                 ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
2871         }
2872     }
2873 }
2874
2875 static void ime_app_accessibility_state_changed_cb(bool state, void *user_data)
2876 {
2877     LOGD("state=%d\n", state);
2878     ise_set_accessibility_state(state);
2879 }
2880
2881 static void ime_app_imdata_set_cb(void *data, unsigned int data_length, void *user_data)
2882 {
2883     LOGD("Enter\n");
2884     g_imdata_state = 0;
2885     size_t _len = data_length;
2886     set_ise_imdata((sclchar *)data, _len);
2887     engine_loader_set_imdata((const char*)data, (uint32_t)data_length);
2888 }
2889
2890 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)
2891 {
2892     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
2893     if (!keyboard_state) return FALSE;
2894
2895     scim::KeyEvent key(keycode, keymask);
2896     unsigned int ret;
2897     char *dev_name = NULL;
2898     Ecore_IMF_Device_Class dev_class;
2899     Ecore_IMF_Device_Subclass dev_subclass;
2900
2901     if (ime_device_info_get_name(dev_info, &dev_name) == IME_ERROR_NONE) {
2902         key.dev_name = dev_name;
2903     }
2904
2905     if (ime_device_info_get_class(dev_info, &dev_class) == IME_ERROR_NONE) {
2906         key.dev_class = dev_class;
2907     }
2908
2909     if (ime_device_info_get_subclass(dev_info, &dev_subclass) == IME_ERROR_NONE) {
2910         key.dev_subclass = dev_subclass;
2911     }
2912
2913     if (keyboard_state->visible_state) {
2914         ise_process_key_event(key, ret);
2915     } else {
2916         if (key.code == IME_KEY_space && (key.mask & IME_KEY_MASK_SHIFT)) {
2917             if (key.mask & IME_KEY_MASK_RELEASED) {
2918                 if (_language_manager.select_next_language()) {
2919                     LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
2920                     if (info) {
2921                         notification_status_message_post(info->display_name.c_str());
2922                     }
2923                 } else {
2924                     LOGE("Failed to change language");
2925                 }
2926             }
2927
2928             ret = TRUE;
2929         } else {
2930             ret = FALSE;
2931         }
2932     }
2933
2934     if (dev_name)
2935         free(dev_name);
2936
2937     return ret;
2938 }
2939
2940 static void ime_app_process_key_event_with_imengine_cb(scim::KeyEvent &key, uint32_t serial, void *user_data)
2941 {
2942     LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
2943     if (info && info->need_surrounding_text)
2944         engine_loader_process_key_event(key, serial, true);
2945     else
2946         engine_loader_process_key_event(key, serial, false);
2947 }
2948
2949 static void ime_app_caps_mode_changed_cb(int mode, void *user_data)
2950 {
2951     ise_set_caps_mode(mode);
2952 }
2953
2954 static void ime_app_candidate_show_cb(int context_id, void *user_data)
2955 {
2956 #ifdef _WEARABLE
2957     ise_check_wearable_candidate();
2958 #else
2959     ise_app_candidate_show();
2960 #endif
2961 }
2962
2963 static void ime_app_candidate_hide_cb(int context_id, void *user_data)
2964 {
2965 #ifdef _WEARABLE
2966     ise_check_wearable_candidate();
2967 #else
2968     ise_app_candidate_hide();
2969 #endif
2970 }
2971
2972 static void ime_app_lookup_table_changed_cb(Eina_List *list, void *user_data)
2973 {
2974     vector<string> candidate_strings;
2975     char *candidate;
2976     void *data;
2977     Eina_List *l;
2978
2979     g_lookup_table_strings.clear();
2980
2981     if (list) {
2982         EINA_LIST_FOREACH(list, l, data) {
2983             candidate = (char *)data;
2984             if (candidate) {
2985                 g_lookup_table_strings.push_back(string(candidate));
2986                 candidate_strings.push_back(string(candidate));
2987             }
2988         }
2989     }
2990
2991     if (input_smartreply_get_reply_num() > 0) {
2992         if (candidate_strings[0] == "#" && candidate_strings[1] == "$") {
2993             char *text = NULL;
2994             int cursor;
2995             ime_get_surrounding_text(0, 0, &text, &cursor);
2996             if (text)
2997                 free(text);
2998
2999             if (cursor == 0)
3000                 return;
3001         }
3002     }
3003
3004     update_candidate_table();
3005 }
3006
3007 #ifdef _WEARABLE
3008 static sclu32 get_rotate_layout(ime_input_device_rotary_direction_e direction)
3009 {
3010     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
3011     if (!keyboard_state) return ISE_LAYOUT_STYLE_NORMAL;
3012
3013     sclu32 new_layout = keyboard_state->layout;
3014
3015     if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE) {
3016         LOGD("CLOCKWISE\n");
3017         switch (keyboard_state->layout) {
3018             case ISE_LAYOUT_STYLE_NORMAL:
3019             case ISE_LAYOUT_STYLE_EMAIL:
3020             case ISE_LAYOUT_STYLE_URL:
3021             case ISE_LAYOUT_STYLE_PASSWORD:
3022                 if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
3023                         _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
3024                     ;   // PASSWORD NUMBER ONLY, do nothing
3025                 else
3026                     new_layout = ISE_LAYOUT_STYLE_NUMBER;
3027                 break;
3028             case ISE_LAYOUT_STYLE_NUMBER:
3029                 new_layout = ISE_LAYOUT_STYLE_HEX;
3030                 break;
3031             case ISE_LAYOUT_STYLE_HEX:
3032                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3033                         _context_layout == ISE_LAYOUT_STYLE_URL)
3034                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3035                 else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3036                     new_layout = _context_layout;
3037                 else
3038                     new_layout = ISE_LAYOUT_STYLE_EMOTICON;
3039                 break;
3040             case ISE_LAYOUT_STYLE_EMOTICON:
3041                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3042                         _context_layout == ISE_LAYOUT_STYLE_URL ||
3043                         _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3044                     new_layout = _context_layout;
3045                 else
3046                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3047                 break;
3048             case ISE_LAYOUT_STYLE_VOICE:
3049                 new_layout = ISE_LAYOUT_STYLE_NORMAL;
3050                 break;
3051             default:
3052                 ;
3053         }
3054     } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE) {
3055         LOGD("COUNTER_CLOCKWISE\n");
3056         switch (keyboard_state->layout) {
3057             case ISE_LAYOUT_STYLE_NORMAL:
3058             case ISE_LAYOUT_STYLE_EMAIL:
3059             case ISE_LAYOUT_STYLE_URL:
3060             case ISE_LAYOUT_STYLE_PASSWORD:
3061                 if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
3062                         _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
3063                     ;   // PASSWORD NUMBER ONLY, do nothing
3064                 else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3065                     new_layout = ISE_LAYOUT_STYLE_HEX;
3066                 else
3067                     new_layout = ISE_LAYOUT_STYLE_VOICE;
3068                 break;
3069             case ISE_LAYOUT_STYLE_NUMBER:
3070                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3071                         _context_layout == ISE_LAYOUT_STYLE_URL ||
3072                         _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
3073                     new_layout = _context_layout;
3074                 else
3075                     new_layout = ISE_LAYOUT_STYLE_NORMAL;
3076                 break;
3077             case ISE_LAYOUT_STYLE_HEX:
3078                 new_layout = ISE_LAYOUT_STYLE_NUMBER;
3079                 break;
3080             case ISE_LAYOUT_STYLE_EMOTICON:
3081                 new_layout = ISE_LAYOUT_STYLE_HEX;
3082                 break;
3083             case ISE_LAYOUT_STYLE_VOICE:
3084                 if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
3085                         _context_layout == ISE_LAYOUT_STYLE_URL)
3086                     new_layout = ISE_LAYOUT_STYLE_HEX;
3087                 else
3088                     new_layout = ISE_LAYOUT_STYLE_EMOTICON;
3089                 break;
3090             default:
3091                 ;
3092         }
3093     }
3094
3095     return new_layout;
3096 }
3097
3098 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)
3099 {
3100     KEYBOARD_STATE *keyboard_state = get_keyboard_state();
3101     if (!keyboard_state) return;
3102     if (device_type != IME_INPUT_DEVICE_TYPE_ROTARY) return;
3103
3104     ime_input_device_rotary_direction_e direction;
3105     if (IME_ERROR_NONE != ime_input_device_rotary_get_direction(device_event, &direction)) return;
3106
3107     sclu32 new_layout = get_rotate_layout(direction);
3108
3109     CONFIG_VALUES *config_values = get_config_values();
3110     if (check_is_tutorial_show() && config_values) {
3111         read_ise_config_values();
3112         if ((direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && !config_values->number_tutorial_enable && !config_values->symbol_tutorial_enable) ||
3113                 (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && config_values->symbol_tutorial_enable)) {
3114             new_layout = keyboard_state->layout;
3115         } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && config_values->symbol_tutorial_enable) {
3116             ise_destroy_tutorial_mode_popup();
3117             config_values->symbol_tutorial_enable = false;
3118             write_ise_config_values();
3119         } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && !config_values->number_tutorial_enable) {
3120             ise_destroy_tutorial_mode_popup();
3121             new_layout = ISE_LAYOUT_STYLE_NUMBER;
3122             if (!config_values->symbol_tutorial_enable) {
3123                 ise_show_tutorial_mode_popup(new_layout);
3124                 config_values->symbol_tutorial_enable = true;
3125                 write_ise_config_values();
3126             }
3127         }
3128     }
3129
3130     if (new_layout != keyboard_state->layout && new_layout < ISE_LAYOUT_STYLE_MAX) {
3131         keyboard_state->need_reset = TRUE;
3132         keyboard_state->layout = new_layout;
3133
3134         if (keyboard_state->visible_state) {
3135             _reset_multitap_state();
3136             if (config_values) {
3137                 _language_manager.reset_language(config_values->selected_language.c_str());
3138             }
3139             ise_show(keyboard_state->ic);
3140         }
3141     }
3142 }
3143 #endif
3144
3145 static void ime_app_prediction_hint_set_cb(const char *prediction_hint, void *user_data)
3146 {
3147     char *sender = (char *)"mms";
3148     char *caller_id = (char *)"mms";
3149     char *hint = (char *)prediction_hint;
3150
3151     if (!prediction_hint) return;
3152     SECURE_LOGD("prediction hint : %s\n", prediction_hint);
3153
3154     if (strlen(prediction_hint) > 0) {
3155         input_smartreply_init(caller_id, sender, hint);
3156         input_smartreply_set_notify(_input_smartreply_notify_cb, NULL);
3157
3158         if (input_smartreply_is_enabled()) {
3159             input_smartreply_get_reply_async();
3160         }
3161     }
3162 }
3163
3164 static void ime_app_mime_type_set_request_cb(const char *mime_types, void *user_data)
3165 {
3166     LOGD("mime type : %s\n", mime_types);
3167
3168     clipboard_set_mime_type(true);
3169     clipboard_init_sel_type();
3170
3171     string str(mime_types), text_key = "text/", image_key = "image/";
3172
3173     if (str.find(text_key) != string::npos)
3174         clipboard_add_sel_type(CLIPBOARD_SEL_TEXT);
3175
3176     if (str.find(image_key) != string::npos)
3177         clipboard_add_sel_type(CLIPBOARD_SEL_IMAGE);
3178 }
3179
3180 static void ime_app_prediction_hint_data_set_cb(const char *key, const char *value, void *user_data)
3181 {
3182     SECURE_LOGD("key : %s, value : %s\n", key, value);
3183
3184     if (string(key) == "appid")
3185         ime_autofill_set_app_id(value);
3186     else if (string(key) == "res_id")
3187         ime_autofill_set_resource_id(value);
3188 }
3189
3190 static void ime_app_autocapital_type_set_cb(uint32_t type, void *user_data)
3191 {
3192     LOGD("autocapital type : %u\n", type);
3193     engine_loader_set_autocapital_type(type);
3194 }
3195
3196 static void ime_app_prediction_allow_set_cb(uint32_t prediction_allow, void *user_data)
3197 {
3198     LOGD("prediction allow : %u\n", prediction_allow);
3199     engine_loader_set_prediction_allow(prediction_allow);
3200 }
3201
3202 static void ime_app_trigger_property_set_cb(const char *property, void *user_data)
3203 {
3204     LOGD("trigger property : %s\n", property);
3205     engine_loader_trigger_property(property);
3206 }
3207
3208 static void ime_app_candidate_more_window_show_cb(void *user_data)
3209 {
3210     LOGD("");
3211     engine_loader_show_candidate_more_window();
3212 }
3213
3214 static void ime_app_candidate_more_window_hide_cb(void *user_data)
3215 {
3216     LOGD("");
3217     engine_loader_hide_candidate_more_window();
3218 }
3219
3220 static void ime_app_aux_select_cb(uint32_t item, void *user_data)
3221 {
3222     LOGD("aux select : %u\n", item);
3223     engine_loader_select_aux(item);
3224 }
3225
3226 static void ime_app_candidate_select_cb(uint32_t item, void *user_data)
3227 {
3228     LOGD("candidate select : %u\n", item);
3229     engine_loader_select_candidate(item);
3230 }
3231
3232 static void ime_app_candidate_table_page_up_cb(void *user_data)
3233 {
3234     LOGD("");
3235     engine_loader_candidate_table_page_up();
3236 }
3237
3238 static void ime_app_candidate_table_page_down_cb(void *user_data)
3239 {
3240     LOGD("");
3241     engine_loader_candidate_table_page_down();
3242 }
3243
3244 static void ime_app_candidate_table_page_size_chaned_cb(uint32_t size, void *user_data)
3245 {
3246     LOGD("candidate page size : %u\n", size);
3247     engine_loader_change_candidate_page_size(size);
3248 }
3249
3250 static void ime_app_candidate_item_layout_set_cb(vector<uint32_t> item, void *user_data)
3251 {
3252     LOGD("item layout size : %zu", item.size());
3253     engine_loader_set_candidate_item_layout(item);
3254 }
3255
3256 static void ime_app_displayed_candidate_number_chaned_cb(uint32_t page_num, void *user_data)
3257 {
3258     LOGD("candidate number changed : %u\n", page_num);
3259     engine_loader_change_candidate_number(page_num);
3260 }
3261
3262 static void ime_app_candidate_item_long_pressed_cb(uint32_t index, void *user_data)
3263 {
3264     LOGD("candidate item : %u\n", index);
3265     engine_loader_long_press_candidate_item(index);
3266 }
3267
3268 #ifdef __cplusplus
3269 extern "C"{
3270 #endif
3271 EXPORTED void ime_app_main(int argc, char **argv)
3272 {
3273     ime_callback_s basic_callback = {
3274         ime_app_create_cb,
3275         ime_app_exit_cb,
3276         ime_app_show_cb,
3277         ime_app_hide_cb
3278     };
3279
3280     ime_event_set_focus_in_cb(ime_app_focus_in_cb, NULL);
3281     ime_event_set_focus_out_cb(ime_app_focus_out_cb, NULL);
3282     ime_event_set_rotation_degree_changed_cb(ime_app_rotation_degree_changed_cb, NULL);
3283     ime_event_set_accessibility_state_changed_cb(ime_app_accessibility_state_changed_cb, NULL);
3284     ime_event_set_layout_set_cb(ime_app_layout_set_cb, NULL);
3285     ime_event_set_caps_mode_changed_cb(ime_app_caps_mode_changed_cb, NULL);
3286     ime_event_set_cursor_position_updated_cb(ime_app_cursor_position_updated_cb, NULL);
3287     ime_event_set_surrounding_text_updated_cb(ime_app_surrounding_text_updated_cb, NULL);
3288     ime_event_set_return_key_type_set_cb(ime_app_return_key_type_set_cb, NULL);
3289     ime_event_set_return_key_state_set_cb(ime_app_return_key_state_set_cb, NULL);
3290     ime_event_set_language_set_cb(ime_app_language_set_cb, NULL);
3291     ime_event_set_imdata_set_cb(ime_app_imdata_set_cb, NULL);
3292     ime_event_set_process_key_event_cb(ime_app_process_key_event_cb, NULL);
3293     ime_event_set_process_key_event_with_imengine_cb(ime_app_process_key_event_with_imengine_cb, NULL);
3294     ime_event_set_input_hint_set_cb(ime_app_input_hint_set_cb, NULL);
3295
3296     ime_event_set_candidate_show_cb(ime_app_candidate_show_cb, NULL);
3297     ime_event_set_candidate_hide_cb(ime_app_candidate_hide_cb, NULL);
3298     ime_event_set_lookup_table_changed_cb(ime_app_lookup_table_changed_cb, NULL);
3299
3300 #ifdef _WEARABLE
3301     ime_event_set_process_input_device_event_cb(ime_app_process_input_device_event_cb, NULL);
3302 #endif
3303
3304     ime_event_set_input_context_reset_cb(ime_app_input_context_reset_cb, NULL);
3305     ime_event_set_language_requested_cb(ime_app_language_requested_cb, NULL);
3306
3307     ime_event_set_prediction_hint_set_cb(ime_app_prediction_hint_set_cb, NULL);
3308     ime_event_set_mime_type_set_request_cb(ime_app_mime_type_set_request_cb, NULL);
3309     ime_event_set_prediction_hint_data_set_cb(ime_app_prediction_hint_data_set_cb, NULL);
3310
3311     ime_event_set_autocapital_type_set_cb(ime_app_autocapital_type_set_cb, NULL);
3312     ime_event_set_prediction_allow_set_cb(ime_app_prediction_allow_set_cb, NULL);
3313     ime_event_set_trigger_property_set_cb(ime_app_trigger_property_set_cb, NULL);
3314     ime_event_set_candidate_more_window_show_cb(ime_app_candidate_more_window_show_cb, NULL);
3315     ime_event_set_candidate_more_window_hide_cb(ime_app_candidate_more_window_hide_cb, NULL);
3316     ime_event_set_aux_select_cb(ime_app_aux_select_cb, NULL);
3317     ime_event_set_candidate_select_cb(ime_app_candidate_select_cb, NULL);
3318     ime_event_set_candidate_table_page_up_cb(ime_app_candidate_table_page_up_cb, NULL);
3319     ime_event_set_candidate_table_page_down_cb(ime_app_candidate_table_page_down_cb, NULL);
3320     ime_event_set_candidate_table_page_size_chaned_cb(ime_app_candidate_table_page_size_chaned_cb, NULL);
3321     ime_event_set_candidate_item_layout_set_cb(ime_app_candidate_item_layout_set_cb, NULL);
3322     ime_event_set_displayed_candidate_number_chaned_cb(ime_app_displayed_candidate_number_chaned_cb, NULL);
3323     ime_event_set_candidate_item_long_pressed_cb(ime_app_candidate_item_long_pressed_cb, NULL);
3324
3325 #if DEFER_ISE_CREATION
3326     ime_set_window_creation_defer_flag(TRUE);
3327 #endif
3328
3329     ime_run(&basic_callback, NULL);
3330 }
3331 #ifdef __cplusplus
3332 }
3333 #endif
3334
3335 EXPORTED int main(int argc, char *argv[])
3336 {
3337     ime_app_main(argc, argv);
3338
3339     return 0;
3340 }