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