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