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