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