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