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