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