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