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