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