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