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