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