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