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