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