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