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