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