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