summaryrefslogtreecommitdiff
path: root/src/ise.cpp
blob: 30ca0133b9ce66b8219ec8137073f2f077aef1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
/*
 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vconf.h>
#include <Ecore.h>
#include <Ecore_IMF.h>
#include <Elementary.h>
#include <sclui.h>
#include <sclutils.h>
#include <inputmethod.h>
#include <inputmethod_internal.h>
#include <app_control.h>
#include <app_preference.h>
#include <notification.h>
#include "autofill.h"
#include "ise.h"
#include "utils.h"
#include "option.h"
#include "languages.h"
#include "candidate-factory.h"
#include "ise-emoticon-mode.h"
#include "ise-emoticon-list.h"
#include "ise-stt-mode.h"
#include "ise-stt-option.h"
#include "ise-language-change.h"
#include "ise-tutorial-mode.h"
#include "modeindicator.h"
#include "w-input-smartreply.h"
#include "ise-floating-mode.h"
#include "ise-dbus.h"
#include "ise-sticker-mode.h"
#include "ise-nmt-mode.h"
#include "nmt.h"
#include "guidepopup.h"

#define EXIT_ISE_ON_HIDE 0
#define DEFER_ISE_CREATION 0

#define CANDIDATE_WINDOW_HEIGHT 84
using namespace scl;
#include <vector>
using namespace std;

static CSCLUI *g_ui = NULL;
CSCLUI* get_ui() { return g_ui; }

static int g_imdata_state = 0;

static sclboolean g_need_send_shift_event = FALSE;

extern void set_ise_imdata(const char * buf, size_t &len);
static void init_recent_used_punctuation();
static void update_recent_used_punctuation(const char *key_value);
static void set_ime_size(bool floating_mode, ISE_CANDIDATE_REQUEST candidate_req);
static sclboolean g_punctuation_popup_opened = FALSE;
static sclboolean g_popup_opened = FALSE;
static vector<string> g_recent_used_punctuation;
static const int MAX_DEFAULT_PUNCTUATION = 6;
static string g_default_punctuation[MAX_DEFAULT_PUNCTUATION] = {"-", "@", "'", "!", "?", ","};
static string g_current_punctuation[MAX_DEFAULT_PUNCTUATION-1] = {"RCENT1", "RCENT2", "RCENT3", "RCENT4", "RCENT5"};
static vector<string> g_softcandidate_string;
static bool g_input_panel_show = false;
static bool g_caps_mode_pending = false;
static bool g_floating_mode = false;
static bool g_candidate_more_view = false;
static bool g_ise_created = false;

static vector<string> g_lookup_table_strings;
static vector<string> g_smartreply_strings;
#if EXIT_ISE_ON_HIDE
static Ecore_Timer *exit_timer = NULL;
#endif

#define SOFT_CANDIDATE_DELETE_TIME (100.0/1000)
static Ecore_Timer *g_softcandidate_hide_timer = NULL;

static int g_ic = 0;
static int g_ic_smartreply = -1;

static KEYBOARD_STATE g_keyboard_state = {
    0,
    0,
    ISE_LAYOUT_STYLE_NORMAL,
    0,
    FALSE,
    TRUE,
    FALSE,
    "",
    KEY_MODIFIER_NONE,
    FALSE,
    FALSE
};

KEYBOARD_STATE* get_keyboard_state() {
    return &g_keyboard_state;
}

#define ISE_LAYOUT_NUMBERONLY_VARIATION_MAX 4
/*static const sclchar *_ise_numberonly_variation_name[ISE_LAYOUT_NUMBERONLY_VARIATION_MAX] = {
    "DEFAULT", "SIG", "DEC", "SIGDEC"
};*/

#define SIG_DEC_SIZE        2
static scluint              _click_count = 0;
static const char          *_sig_dec[SIG_DEC_SIZE] = {".", "-"};
static scluint              _sig_dec_event[SIG_DEC_SIZE] = {'.', '-'};
static Ecore_Timer         *_commit_timer = NULL;

static sclu32               _context_layout = ISE_LAYOUT_STYLE_NORMAL;
static sclu32               _context_layout_variation = 0;

static Candidate           *g_candidate = NULL;
Candidate* get_candidate() {
    return g_candidate;
}

static ISELanguageManager _language_manager;

class CandidateEventListener: public EventListener
{
    public:
        void on_event(const EventDesc &desc)
        {
            CSCLUI *ui = get_ui();
            const MultiEventDesc &multidesc = dynamic_cast<const MultiEventDesc &>(desc);
            LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());

            unsigned int smartreply_size = input_smartreply_get_reply_num();

            switch (multidesc.type) {
                case MultiEventDesc::CANDIDATE_ITEM_MOUSE_DOWN:
                    if (ime_autofill_get_exist()) {
                        if (multidesc.index == 0) {
                            string autofill_string = ime_autofill_get_string();
                            ise_send_string(autofill_string.c_str());
                        } else if (multidesc.index < (int)smartreply_size + 1) {
                            ise_send_string(g_softcandidate_string[multidesc.index].c_str());
                            ise_update_table(g_smartreply_strings);
                        } else {
                            if (info && info->load_in_ime)
                                ime_select_candidate(multidesc.index - smartreply_size - 1);
                            else
                                engine_loader_select_candidate(multidesc.index - smartreply_size - 1);
                        }
                    } else {
                        if (multidesc.index < (int)smartreply_size) {
                            ise_send_string(g_softcandidate_string[multidesc.index].c_str());
                            ise_update_table(g_smartreply_strings);
                        }
                        else {
                            if (info && info->load_in_ime)
                                ime_select_candidate(multidesc.index - smartreply_size);
                            else
                                engine_loader_select_candidate(multidesc.index - smartreply_size);
                        }
                    }
                    break;
                case MultiEventDesc::CANDIDATE_MORE_VIEW_SHOW:
                    // when more parts shows, click on the candidate will
                    // not affect the key click event
                    g_candidate_more_view = true;
                    if (!g_input_panel_show)
                        set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
                    if (ui)
                        ui->disable_input_events(TRUE);
                    break;
                case MultiEventDesc::CANDIDATE_MORE_VIEW_HIDE:
                    g_candidate_more_view = false;
                    if (!g_input_panel_show)
                        set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
                    if (ui)
                        ui->disable_input_events(FALSE);
                    break;
                default: break;
            }
        }
};
static CandidateEventListener g_candidate_event_listener;

#define MVK_Shift_L 0xffe1
#define MVK_Caps_Lock 0xffe5
#define MVK_Shift_Off 0xffe1
#define MVK_Shift_On 0xffe2
#define MVK_Shift_Lock 0xffe6
#define MVK_Shift_Enable 0x9fe7
#define MVK_Shift_Disable 0x9fe8
#define MVK_space 0x020
#define MVK_Done 0xff0d

#define CM_KEY_LIST_SIZE         7
#define MULTITAP_TIMEOUT         3.0
#define USER_KEYSTRING_OPTION    "OPTION"
#define USER_KEYSTRING_EMOTICON  "EMOTICON_LAYOUT"
#define USER_KEYSTRING_VOICE     "STT_3X4"
#define USER_KEYSTRING_FLOATING  "FLOATING"
#define USER_KEYSTRING_STICKER   "STICKER_LAYOUT"
#define USER_KEYSTRING_TRANSLATION "TRANSLATION_LAYOUT"

#define USER_VOICE_LANGUAGE     "LANGUAGE"

static sclboolean           _cm_popup_opened = FALSE;
static const char          *_cm_key_list[CM_KEY_LIST_SIZE] = {USER_KEYSTRING_OPTION, USER_KEYSTRING_EMOTICON,
                                                              USER_KEYSTRING_VOICE, USER_KEYSTRING_FLOATING, USER_KEYSTRING_STICKER, USER_KEYSTRING_TRANSLATION};
static scluint              _current_cm_key_id = 0;

/*
 * This callback class will receive all response events from SCL
 * So you should perform desired tasks in this class.
 */
class CUIEventCallback : public ISCLUIEventCallback
{
public :
    Ecore_Timer* word_timer = NULL;
    static Eina_Bool _multi_tap_timer_cb(void *data)
    {
        LOGD("Time Out");
        ise_send_event(MVK_Done, KEY_MASK_NULL);
        return ECORE_CALLBACK_CANCEL;
    }
    SCLEventReturnType on_event_key_clicked(SclUIEventDesc event_desc);
    SCLEventReturnType on_event_drag_state_changed(SclUIEventDesc event_desc);
    SCLEventReturnType on_event_notification(SCLUINotiType noti_type, SclNotiDesc *etc_info);

private:
    void on_event_string_key(CSCLUI *ui, SclUIEventDesc event_desc);
    void on_event_control_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state);
    void on_event_character_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state);
    void on_event_modechange_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret);
    void process_user_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret);
};

static CUIEventCallback callback;

int ise_get_imdata_state()
{
    return g_imdata_state;
}

void ise_set_imdata_state(int state)
{
    g_imdata_state = state;
}

static void update_candidate_table()
{
    g_softcandidate_string.clear();
    vector<string>::iterator iter;

    g_smartreply_strings.clear();
    input_smartreply_deinit();
    g_ic_smartreply = -1;

    // add autofill string
    if (ime_autofill_get_exist())
        g_softcandidate_string.push_back(ime_autofill_get_string());

    // add lookup table string(s)
    iter = g_lookup_table_strings.begin();
    for (; iter != g_lookup_table_strings.end(); ++iter)
    {
        g_softcandidate_string.push_back(string(iter->c_str()));
    }

    ise_update_table(g_softcandidate_string);
}

static void _input_smartreply_notify_cb(void *user_data)
{
    g_smartreply_strings.clear();
    char *candidate = NULL;

    if (input_smartreply_is_enabled()) {
        /* Append newly added smartreply list */
        int len = input_smartreply_get_reply_num();
        if (len > 0) {
            for (int i = 0; i < len; i++) {
                int type;
                char *reply = (char *)"";
                reply = input_smartreply_get_nth_item(i, &type);
                if (reply == NULL)
                    continue;
                SECURE_LOGD("SmartReply = [%d] %s", i, reply);
                candidate = reply;
                if (candidate) {
                    g_smartreply_strings.push_back(string(candidate));
                    free(candidate);
                    candidate = NULL;
                }
            }
            g_ic_smartreply = g_ic;

            ise_app_candidate_show();
            g_softcandidate_string = g_smartreply_strings;
            ise_update_table(g_softcandidate_string);
        }
    }
}

sclboolean
check_ic_temporary(int ic)
{
    if ((ic & 0xFFFF) == 0) {
        return TRUE;
    }
    return FALSE;
}

static void _reset_shift_state(void)
{
    CSCLUI *ui = get_ui();
    if (ui) {
        /* Reset all shift state variables */
        SCLShiftState old_shift_state = ui->get_shift_state();
        SCLShiftState new_shift_state = SCL_SHIFT_STATE_OFF;
        if (old_shift_state != new_shift_state) {
            g_need_send_shift_event = true;
            ui->set_shift_state(new_shift_state);
        }
        LOGD("Shift state changed from (%d) to (%d)\n", (int)old_shift_state, (int)new_shift_state);
    }
}

static void set_caps_mode(sclboolean mode) {
    CSCLUI *ui = get_ui();
    LOGD("mode : %d\n", mode);
    if (ui) {
        if (ui->get_shift_state() != SCL_SHIFT_STATE_LOCK) {
            ui->set_shift_state(mode ? SCL_SHIFT_STATE_ON : SCL_SHIFT_STATE_OFF);
            ui->set_autocapital_shift_state(!mode);

            bool load_in_ime = false;
            const sclchar *cur_lang = _language_manager.get_current_language();
            if (cur_lang) {
                LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
                if (info)
                    load_in_ime = info->load_in_ime;
            }

            if (load_in_ime)
                ime_send_imengine_event(mode ? MVK_Shift_On : MVK_Shift_Off, 0);
            else
                engine_loader_send_imengine_event(mode ? MVK_Shift_On : MVK_Shift_Off, 0);
        }
    }
}

static void _reset_multitap_state(bool skip_commit = false)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("keyboard_state.prev_modifier : %d", keyboard_state->prev_modifier);
    if (keyboard_state->prev_modifier == KEY_MODIFIER_MULTITAP_START ||
        keyboard_state->prev_modifier == KEY_MODIFIER_MULTITAP_REPEAT) {
        if (!skip_commit) {
            ise_send_string(keyboard_state->multitap_value.c_str());
        }
        ise_update_preedit_string("");
    }
    keyboard_state->multitap_value = "";
    keyboard_state->prev_modifier = KEY_MODIFIER_NONE;
    if (g_caps_mode_pending) {
        g_caps_mode_pending = false;
        set_caps_mode(keyboard_state->caps_mode);
    }
}

static void ise_set_cm_private_key(scluint cm_key_id)
{
    CSCLUI *ui = get_ui();
    if (cm_key_id >= CM_KEY_LIST_SIZE || ui == NULL) {
        LOGE("cm_key_id=%d\n", cm_key_id);
        return;
    }

    if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_EMOTICON) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_emotion_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_emotion_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_emotion_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_EMOTICON), TRUE);
    } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_OPTION) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_setting_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_setting_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_setting_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_OPTION), TRUE);
    } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_VOICE) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_mic_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_mic_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_mic_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_VOICE), TRUE);
    } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_FLOATING) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_floating_keypad_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_floating_keypad_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_floating_keypad_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_FLOATING), TRUE);
    } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_TRANSLATION) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_translation_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_translation_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_translation_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_TRANSLATION), TRUE);
    } else if (strcmp(_cm_key_list[cm_key_id], USER_KEYSTRING_STICKER) == 0) {
        sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>("icon/54x54/icon_sticker_nor.png"),
            const_cast<sclchar*>("icon/54x54/icon_sticker_press.png"),
            const_cast<sclchar*>("icon/54x54/icon_sticker_dim.png")};
        ui->set_private_key("CM_KEY", const_cast<sclchar*>(" "), imagelabel, NULL, 0, const_cast<sclchar*>(USER_KEYSTRING_STICKER), TRUE);
    }
}

static void ise_update_space_key(void)
{
    CSCLUI *ui = get_ui();
    if (ui == NULL) {
        LOGE("ui = NULL\n");
        return;
    }

    scluint num = _language_manager.get_enabled_languages_num();
    LOGD("language number: %d\n", num);
    if (num <= 1) {
        LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
        if (info && info->enabled) {
#ifdef _WEARABLE
            sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
                const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png"),
                const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png"),
                const_cast<sclchar*>("w_sip_3x4_btn_ic_space_no_arrow.png")};
            ui->set_private_key("SPACE_KEY", const_cast<sclchar*>(info->display_name.c_str()), imagelabel, NULL, 0, const_cast<sclchar*>("Space"), TRUE);
#else
            ui->enable_button("SPACE_ARROW_LEFT", false);
            ui->enable_button("SPACE_ARROW_RIGHT", false);
#endif
        }
    } else {
#ifdef _WEARABLE
        ui->unset_private_key("SPACE_KEY");
#else
        ui->enable_button("SPACE_ARROW_LEFT", true);
        ui->enable_button("SPACE_ARROW_RIGHT", true);
#endif
    }
}

static scluint ise_get_cm_key_id(const sclchar *key_value)
{
    for (int i = 0; i < CM_KEY_LIST_SIZE; ++i) {
        if (0 == strcmp (key_value, _cm_key_list[i]))
            return i;
    }
    return 0;
}

static bool ise_is_emoticons_disabled(void)
{
    bool ret = true;

    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return ret;

    sclu32 current_layout = keyboard_state->layout;
    LOGD("layout=%d\n", current_layout);

    if ((current_layout == ISE_LAYOUT_STYLE_NORMAL) ||
        (current_layout == ISE_LAYOUT_STYLE_NUMBER) ||
        (current_layout == ISE_LAYOUT_STYLE_EMOTICON))
        ret = false;

    if (g_imdata_state & IMDATA_ACTION_DISABLE_EMOTICONS)
        ret = true;

    return ret;
}

static Eina_Bool softcandidate_hide_timer_callback(void *data)
{
    LOGD("Enter\n");
    set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_HIDE);

    Candidate *candidate = get_candidate();
    if (candidate) {
        candidate->hide();
    }
    return ECORE_CALLBACK_CANCEL;
}

static void delete_softcandidate_hide_timer(void)
{
    if (g_softcandidate_hide_timer) {
        ecore_timer_del(g_softcandidate_hide_timer);
        g_softcandidate_hide_timer = NULL;
    }
}

static void add_softcandidate_hide_timer(void)
{
    delete_softcandidate_hide_timer();
    g_softcandidate_hide_timer = ecore_timer_add(SOFT_CANDIDATE_DELETE_TIME, softcandidate_hide_timer_callback, NULL);
}

static void create_softcandidate(void)
{
    if (!g_candidate) {
        g_candidate = CandidateFactory::make_candidate(CANDIDATE_MULTILINE, ime_get_main_window());
        if (g_candidate) {
            g_candidate->add_event_listener(&g_candidate_event_listener);
        }
    }
}

/**
 * Send the given string to input framework
 */
void
ise_send_string(const sclchar *key_value)
{
    int ic = -1;
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
        ic = keyboard_state->ic;
    }
    ime_commit_string(key_value);
    LOGD("ic : %x, %s\n", ic, key_value);
}

/**
* Send the preedit string to input framework
*/
void
ise_update_preedit_string(const sclchar *str, const sclboolean underline)
{
    int ic = -1;
    Eina_List *attr_list = NULL;
    ime_preedit_attribute *preedit_attr;

    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
        ic = keyboard_state->ic;
    }
    if (underline) {
        /* Count UTF-8 string length */
        int len = 0;
        const sclchar *s = str;
        while (*s) len += (*s++ & 0xc0) != 0x80;

        preedit_attr = (ime_preedit_attribute *)calloc(1, sizeof(ime_preedit_attribute));
        if (preedit_attr) {
            preedit_attr->start = 0;
            preedit_attr->length = len;
            preedit_attr->type = IME_ATTR_FONTSTYLE;
            preedit_attr->value = IME_ATTR_FONTSTYLE_UNDERLINE;
            attr_list = eina_list_append(attr_list, (void *)preedit_attr);
        }

        ime_update_preedit_string(str, attr_list);
    } else {
        ime_update_preedit_string(str, NULL);
    }
    LOGD("ic : %x, %s\n", ic, str);
}

/**
* Send the given event to input framework
*/
void
ise_send_event(sclulong key_event, sclulong key_mask)
{
    int ic = -1;
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (keyboard_state && !check_ic_temporary(keyboard_state->ic)) {
        ic = keyboard_state->ic;
    }
    ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_PRESSED, false);
    ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_RELEASED, false);

    LOGD("ic : %x, %lx\n", (unsigned int)ic, key_event);
}

/**
* Forward the given event to input framework
*/
void
ise_forward_key_event(sclulong key_event)
{
    int ic = -1;
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!check_ic_temporary(keyboard_state->ic)) {
        ic = keyboard_state->ic;
    }
    ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_PRESSED, true);
    ime_send_key_event((ime_key_code_e)key_event, IME_KEY_MASK_RELEASED, true);

    LOGD("ic : %x, %lx\n", (unsigned int)ic, key_event);
}

/**
 * @brief Delete commit timer.
 *
 * @return void
 */
static void delete_commit_timer(void)
{
    if (_commit_timer != NULL) {
        ecore_timer_del(_commit_timer);
        _commit_timer = NULL;
    }
}

/**
 * @brief Callback function for commit timer.
 *
 * @param data Data to pass when it is called.
 *
 * @return ECORE_CALLBACK_CANCEL
 */
static Eina_Bool commit_timeout(void *data)
{
    if (_commit_timer != NULL) {
        ime_hide_preedit_string();
        ise_forward_key_event(_sig_dec_event[(_click_count-1)%SIG_DEC_SIZE]);
        _click_count = 0;
    }
    _commit_timer = NULL;
    return ECORE_CALLBACK_CANCEL;
}

static sclboolean
on_input_mode_changed(const sclchar *key_value, sclulong key_event, sclint key_type)
{
    sclboolean ret = FALSE;

    CSCLUI *ui = get_ui();
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (ui && keyboard_state) {
        if (key_value) {
            SECURE_LOGD("key_value : %s\n", key_value);
            if (strcmp(key_value, "CUR_LANG") == 0) {
                keyboard_state->disable_force_latin = TRUE;
                ret = _language_manager.select_current_language();
            }
            if (strcmp(key_value, "NEXT_LANG") == 0) {
                keyboard_state->disable_force_latin = TRUE;
                ret = _language_manager.select_next_language();
            }
        }

        const sclchar *cur_lang = _language_manager.get_current_language();
        if (cur_lang) {
            LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
            if (info) {
                if (info->accepts_caps_mode) {
                    if (info->load_in_ime)
                        ime_send_imengine_event(MVK_Shift_Enable, 0);
                    else
                        engine_loader_send_imengine_event(MVK_Shift_Enable, 0);

                    set_caps_mode(keyboard_state->caps_mode);
                } else {
                    if (info->load_in_ime)
                        ime_send_imengine_event(MVK_Shift_Disable, 0);
                    else
                        engine_loader_send_imengine_event(MVK_Shift_Disable, 0);

                    ui->set_shift_state(SCL_SHIFT_STATE_OFF);
                }
            }
        }

        if (ise_sticker_is_show())
            ise_sticker_destroy_layout();

        if (ise_emoticon_is_show()) {
            ise_emoticon_destroy_layout();
        }

        if (ise_nmt_is_show()) {
            ise_nmt_destroy_layout();
        }

        if (key_value) {
            if (!strcmp(key_value, USER_KEYSTRING_EMOTICON)) {
                ise_emoticon_init_list();
#ifdef _WEARABLE
                    ise_emoticon_set_current_group(EMOTICON_GROUP_1);
#else
                if (ise_emoticon_get_recent_list_size() == 0)
                    ise_emoticon_set_current_group(EMOTICON_GROUP_1);
                else
                    ise_emoticon_set_current_group(EMOTICON_GROUP_RECENTLY_USED);
#endif
                SCLRotation rotation = ui->get_rotation();
                ise_emoticon_show_layout(ise_emoticon_get_current_group(), ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
            }
            else if (!strcmp(key_value, USER_KEYSTRING_TRANSLATION)) {
                LOGD("");
                SCLRotation rotation = ui->get_rotation();
                ise_nmt_show_layout(ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
            }
        }
    }

    return ret;
}

SCLEventReturnType CUIEventCallback::on_event_notification(SCLUINotiType noti_type, SclNotiDesc *etc_info)
{
    CSCLUI *ui = get_ui();
    SCLEventReturnType ret = SCL_EVENT_PASS_ON;
    LOGD("noti type: %d, g_need_send_shift_event: %d\n", noti_type, g_need_send_shift_event);

    if (noti_type == SCL_UINOTITYPE_SHIFT_STATE_CHANGE) {
        if (g_need_send_shift_event) {
            const sclchar *cur_lang = _language_manager.get_current_language();
            if (cur_lang) {
                LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
                SclNotiShiftStateChangeDesc *desc = static_cast<SclNotiShiftStateChangeDesc*>(etc_info);
                if (info && desc) {
                    if (info->accepts_caps_mode) {
                        LOGD("shift state: %d\n", desc->shift_state);
                        if (desc->shift_state == SCL_SHIFT_STATE_OFF) {
                            if (info->load_in_ime)
                                ime_send_imengine_event(MVK_Shift_Off, 0);
                            else
                                engine_loader_send_imengine_event(MVK_Shift_Off, 0);
                        } else if (desc->shift_state == SCL_SHIFT_STATE_ON) {
                            if (info->load_in_ime)
                                ime_send_imengine_event(MVK_Shift_On, 0);
                            else
                                engine_loader_send_imengine_event(MVK_Shift_On, 0);
                        } else if (desc->shift_state == SCL_SHIFT_STATE_LOCK) {
                            if (info->load_in_ime)
                                ime_send_imengine_event(MVK_Shift_Lock, 0);
                            else
                                engine_loader_send_imengine_event(MVK_Shift_Lock, 0);
                        }
                        ret = SCL_EVENT_PASS_ON;
                    }
                }
            }
            g_need_send_shift_event = FALSE;
        }
    } else if (noti_type == SCL_UINOTITYPE_POPUP_OPENING) {
        vector<string>::reverse_iterator iter = g_recent_used_punctuation.rbegin();
        int punc_pos = 0;
        for (; iter != g_recent_used_punctuation.rend(); ++iter)
        {
            if (ui)
                ui->set_string_substitution(g_current_punctuation[punc_pos].c_str(), iter->c_str());
            punc_pos++;
        }
        SclNotiPopupOpeningDesc *openingDesc = (SclNotiPopupOpeningDesc *)etc_info;
        if (ui && 0 == strcmp(openingDesc->input_mode, "CM_POPUP")) {
            if (!ise_sticker_check_sticker_exists())
                ui->enable_button("STICKER_KEY", EINA_FALSE);

            if (nmt_is_disabled())
                ui->enable_button("TRANSLATION_KEY", false);
            else
                ui->enable_button("TRANSLATION_KEY", true);

            if (ise_is_emoticons_disabled())
                ui->enable_button("EMOTICON_KEY", false);
            else
                ui->enable_button("EMOTICON_KEY", true);
        }
    } else if (noti_type == SCL_UINOTITYPE_POPUP_OPENED) {
        g_popup_opened = TRUE;
        SclNotiPopupOpenedDesc *openedDesc = (SclNotiPopupOpenedDesc *)etc_info;
        if (0 == strcmp(openedDesc->input_mode, "PUNCTUATION_POPUP")) {
            g_punctuation_popup_opened = TRUE;
        } else if (0 == strcmp(openedDesc->input_mode, "CM_POPUP")) {
            _cm_popup_opened = TRUE;
        }
    } else if (noti_type == SCL_UINOTITYPE_POPUP_CLOSED) {
        g_popup_opened = FALSE;
        SclNotiPopupClosedDesc *closedDesc = (SclNotiPopupClosedDesc *)etc_info;
        if (closedDesc && closedDesc->input_mode) {
            if (0 == strcmp(closedDesc->input_mode, "PUNCTUATION_POPUP")) {
                g_punctuation_popup_opened = FALSE;
            } else if (0 == strcmp(closedDesc->input_mode, "CM_POPUP")) {
                _cm_popup_opened = FALSE;
            }
        }
    } else if (noti_type == SCL_UINOTITYPE_INPUT_MODE_CHANGE) {
        SclNotiInputModeChangeDesc *desc = static_cast<SclNotiInputModeChangeDesc*>(etc_info);
        if (desc && ui) {
            set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);

            const char *input_mode = ui->get_input_mode();
            if (input_mode) {
                if (strcmp(input_mode, "STT_3X4") == 0 &&
                        strcmp(desc->input_mode, "STT_3X4") != 0) {
                    ise_hide_stt_mode();
                }
                if (strcmp(input_mode, "STT_3X4") != 0 &&
                        strcmp(desc->input_mode, "STT_3X4") == 0) {
                    ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
                }
            }
        }
    }

    return ret;
}

bool get_landscape_device(int degree)
{
    sclint width = 0;
    sclint height = 0;
    bool landscape_device = false;

    CSCLUI *ui = get_ui();
    if (!ui) return false;

    ui->get_screen_resolution(&width, &height);

    if (degree == 0 || degree == 180) {
        if (width > height)
            landscape_device = true;
    }
    else {
        if (width < height)
            landscape_device = true;
    }

    return landscape_device;
}

SCLEventReturnType CUIEventCallback::on_event_drag_state_changed(SclUIEventDesc event_desc)
{
    SECURE_LOGD("button %s is clicked\n", event_desc.key_value);
    if (event_desc.event_type == EVENT_TYPE_MOVE) {
        if (event_desc.key_event == MVK_space) {
            SclRectangle rectangle = {0};
            CSCLUI *ui = get_ui();
            if (ui) ui->get_button_geometry("SPACE_KEY", &rectangle);
            ise_show_space_flick_language_change_popup(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
        }
    } else if (event_desc.event_type == EVENT_TYPE_RELEASE) {
        if (event_desc.key_event == MVK_space) {
            ise_destroy_space_flick_language_change_popup();
        }

        if (g_floating_mode)
            ime_set_floating_drag_end();
    } else if (event_desc.event_type == EVENT_TYPE_PRESS) {
#if defined(_MOBILE) || defined(_COMMON)
        CONFIG_VALUES *config_values = get_config_values();

        if (config_values && (!config_values->first_guideset) && event_desc.key_value && (strncmp(event_desc.key_value, "OPTION", strlen(event_desc.key_value)) == 0)) {
            ise_show_help_popup(event_desc.key_event, NATIVE_WINDOW_CAST(ime_get_main_window()));//show help popup in on_event_drag_state_changed other than on_event_key_clicked for fixing help popup show delay issue
            LOGD("setting popup show\n");
            return SCL_EVENT_PASS_ON;
        } else {
            ise_destroy_popup_setting();//destroy popup manually in case of abnormal situation
        }
#endif
        if (g_floating_mode && event_desc.mouse_current_point.y <= FLOATING_TITLE_BAR_HEIGHT
            && event_desc.mouse_current_point.y >= 0)
            ime_set_floating_drag_start();
    }
    return SCL_EVENT_PASS_ON;
}

static void launch_option()
{
    app_control_h app_control;
    int ret = app_control_create(&app_control);
    if (ret != APP_CONTROL_ERROR_NONE) {
        LOGW("app_control_create returned %d\n", ret);
        return;
    }

    ret = app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
    if (ret != APP_CONTROL_ERROR_NONE) {
        LOGW("app_control_set_operation returned %d\n", ret);
        goto end;
    }

    ret = app_control_set_app_id(app_control, "org.tizen.ise-default-setting");
    if (ret != APP_CONTROL_ERROR_NONE) {
        LOGW("app_control_set_app_id returned %d\n", ret);
        goto end;
    }

    app_control_add_extra_data(app_control, "caller", "ise-default");
    app_control_set_launch_mode(app_control, APP_CONTROL_LAUNCH_MODE_GROUP);

    ret = app_control_send_launch_request(app_control, NULL, NULL);
    if (ret != APP_CONTROL_ERROR_NONE) {
        goto end;
    }

end:
    if (app_control)
        app_control_destroy(app_control);
}

void CUIEventCallback::on_event_string_key(CSCLUI *ui, SclUIEventDesc event_desc)
{
    if (event_desc.key_modifier != KEY_MODIFIER_MULTITAP_START &&
        event_desc.key_modifier != KEY_MODIFIER_MULTITAP_REPEAT) {
        if (event_desc.key_event) {
            ise_forward_key_event(event_desc.key_event);
        } else {
            ise_send_string(event_desc.key_value);
        }
    }

    if (!g_popup_opened) {
        const sclchar *input_mode = ui->get_input_mode();
        if (input_mode && ((0 == strcmp(input_mode, "SYM_QTY_1")) || (0 == strcmp(input_mode, "SYM_QTY_2")))) {
            update_recent_used_punctuation(event_desc.key_value);
        }
    } else if (g_punctuation_popup_opened) {
        update_recent_used_punctuation(event_desc.key_value);
    }
}

void CUIEventCallback::on_event_control_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state)
{
    commit_timeout(NULL);

    const char *long_shift = "LongShift";
    const char *caps_lock = "CapsLock";
    const char *delete_all = "DeleteAll";
    const char *hide_panel = "Hide";

    if (strncmp(event_desc.key_value, long_shift, strlen(long_shift)) == 0) {
        LOGD("shift key is longpress\n");
        ui->set_shift_state(SCL_SHIFT_STATE_ON);
        g_need_send_shift_event = TRUE;
        //ise_send_event (MVK_Shift_Lock, KEY_MASK_NULL);
    } else if (strncmp(event_desc.key_value, caps_lock, strlen(caps_lock)) == 0) {
        bool load_in_ime = false;
        const sclchar *cur_lang = _language_manager.get_current_language();
        if (cur_lang) {
            LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
            if (info)
                load_in_ime = info->load_in_ime;
        }

        if (ui->get_shift_state() != SCL_SHIFT_STATE_LOCK) {
            ui->set_shift_state(SCL_SHIFT_STATE_LOCK);
            if (load_in_ime)
                ime_send_imengine_event(MVK_Shift_Lock, 0);
            else
                engine_loader_send_imengine_event(MVK_Shift_Lock, 0);
        } else {
            ui->set_shift_state(SCL_SHIFT_STATE_OFF);
            if (load_in_ime)
                ime_send_imengine_event(MVK_Shift_Off, 0);
            else
                engine_loader_send_imengine_event(MVK_Shift_Off, 0);
        }
        //g_need_send_shift_event = TRUE;
    } else if (strncmp(event_desc.key_value, delete_all, strlen(delete_all)) == 0) {
        ime_delete_surrounding_text((INT_MAX / 2) * -1, INT_MAX);
    } else if (strncmp(event_desc.key_value, hide_panel, strlen(hide_panel)) == 0) {
        ise_hide();
        ime_request_hide();
    } else if (event_desc.key_event) {
        if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
            ise_stt_stop();
        }
#ifdef _WEARABLE
        if (event_desc.key_event == MVK_Done) {
            LOGD("ENTER");
            //commit the preedit string first
            ise_send_event(event_desc.key_event, KEY_MASK_NULL);
        }
#endif
        ise_send_event(event_desc.key_event, KEY_MASK_NULL);
        if (event_desc.key_event == MVK_Shift_L) {
            g_need_send_shift_event = TRUE;
        }
    }
}

void CUIEventCallback::on_event_character_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state)
{
    sclboolean need_forward = FALSE;
    // FIXME : Should decide when to forward key events
    const sclchar *input_mode = ui->get_input_mode();
    if (input_mode) {
        if (strcmp(input_mode, "SYM_QTY_1") == 0 ||
            strcmp(input_mode, "SYM_QTY_2") == 0 ||
            strcmp(input_mode, "PHONE_3X4") == 0 ||
            strcmp(input_mode, "IPv6_3X4_123") == 0 ||
            strcmp(input_mode, "IPv6_3X4_ABC") == 0 ||
            strcmp(input_mode, "NUMONLY_3X4") == 0 ||
            strcmp(input_mode, "NUMONLY_3X4_SIG") == 0 ||
            strcmp(input_mode, "NUMONLY_3X4_DEC") == 0 ||
            strcmp(input_mode, "NUMONLY_3X4_SIGDEC") == 0 ||
            strcmp(input_mode, "DATETIME_3X4") == 0) {
            need_forward = TRUE;
        }
    }
    if (input_mode && strcmp (input_mode, "NUMONLY_3X4_SIGDEC") == 0 &&
        event_desc.key_value && strcmp(event_desc.key_value, ".") == 0) {
        ime_update_preedit_string(_sig_dec[_click_count%SIG_DEC_SIZE], NULL);
        ime_show_preedit_string();
        delete_commit_timer();
        _commit_timer = ecore_timer_add(1.0, commit_timeout, NULL);
        _click_count++;
    } else if (event_desc.key_event) {
        commit_timeout(NULL);
        if (need_forward) {
            ise_forward_key_event(event_desc.key_event);
        } else {
            if (_cm_popup_opened) {
                static sclchar current_cm_symbol[2] = {'\0'};
                sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
                    const_cast<sclchar*>(" "),
                    const_cast<sclchar*>(" "),
                    const_cast<sclchar*>(" ")};
                if (event_desc.key_value) {
                    current_cm_symbol[0] = event_desc.key_value[0];
                    current_cm_symbol[1] = '\0';
                }
                ui->set_private_key("CM_KEY",
                        const_cast<sclchar*>(current_cm_symbol),
                        imagelabel, NULL, 0,
                        const_cast<sclchar*>(current_cm_symbol), TRUE);

                _cm_popup_opened = FALSE;
                _current_cm_key_id = -1;
            }
#ifdef _WEARABLE
            /*
             * change the keyboard mode of GLM from QWERTY to KEYPAD
             * ensure the Number keypad won't be affected
             */
            if (keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBER) {
                ime_send_key_event(IME_KEY_Print, IME_KEY_MASK_CONTROL, false);
            } else {
                Candidate *candidate = get_candidate();
                if (word_timer == NULL && (!candidate || !candidate->get_visible())) {
                    word_timer = ecore_timer_add(MULTITAP_TIMEOUT, _multi_tap_timer_cb, NULL);
                }
                ime_send_key_event(IME_KEY_Select, IME_KEY_MASK_CONTROL, false);
            }
#endif
            ise_send_event(event_desc.key_event, KEY_MASK_NULL);
        }
    }

    if (input_mode) {
        if ((strcmp(input_mode, "SYM_QTY_1") == 0) || (0 == strcmp(input_mode, "SYM_QTY_2"))) {
            update_recent_used_punctuation(event_desc.key_value);
        }
    }
}

void CUIEventCallback::on_event_modechange_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret)
{
#if defined(_MOBILE) || defined(_COMMON)
    if (guide_popup_get_visible()) {//popup is showing
        return;
    }
#endif

    if (strcmp(event_desc.key_value, USER_VOICE_LANGUAGE) == 0) {
        if (!get_setting_window_open_status()) {
            ise_hide_stt_mode();
#ifdef _WEARABLE
            hide_indicator_window();
#endif
            create_setting_window();
        }
    } else if (strcmp(event_desc.key_value, USER_KEYSTRING_VOICE) == 0) {
        keyboard_state->layout = ISE_LAYOUT_STYLE_VOICE;
        ui->set_input_mode("STT_3X4");

    } else if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0) {
        launch_option();

        ret = SCL_EVENT_DONE;
    } else if (strcmp(event_desc.key_value, USER_KEYSTRING_FLOATING) == 0) {
        CONFIG_VALUES *config_values = get_config_values();

        if (g_floating_mode) {
            if (config_values) {
                config_values->floating_mode = false;
            }
        } else {
            if (config_values) {
                config_values->floating_mode = true;
            }
        }
        write_ise_config_values();

        ret = SCL_EVENT_DONE;
    } else if (strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0) {
        SCLRotation rotation = ui->get_rotation();
        ise_sticker_show_layout(STICKER_GROUP_RECENTLY_USED, ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
    } else if (strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
        if (ise_sticker_is_show())
            ise_sticker_destroy_layout();

        SCLRotation rotation = ui->get_rotation();
        ise_nmt_show_layout(ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
    } else if (on_input_mode_changed(event_desc.key_value, event_desc.key_event, event_desc.key_type)) {
        ret = SCL_EVENT_DONE;
    }

    if (_cm_popup_opened) {
        if (strcmp(event_desc.key_value, USER_KEYSTRING_EMOTICON) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_VOICE) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_FLOATING) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
            scluint id = ise_get_cm_key_id(event_desc.key_value);
            if (id != _current_cm_key_id) {
                _current_cm_key_id = id;
                ise_set_cm_private_key(_current_cm_key_id);
            }
        }
        _cm_popup_opened = FALSE;
    }

    if (strlen(event_desc.key_value) == 1) {
        const char allowed_chars_in_modechange[] = {
            ',', '?', '!', '`', '~', '@', '-'
        };
        const int allowed_chars_in_modechange_num =
            sizeof(allowed_chars_in_modechange) / sizeof(char);
        for (unsigned int loop = 0;loop < sizeof(allowed_chars_in_modechange_num);loop++) {
            if (*(event_desc.key_value) == allowed_chars_in_modechange[loop]) {
                ise_send_string(event_desc.key_value);
            }
        }
    }
}

void CUIEventCallback::process_user_key(CSCLUI *ui, SclUIEventDesc event_desc, KEYBOARD_STATE *keyboard_state, SCLEventReturnType &ret)
{
    if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0) {
        //open_option_window(NULL, ROTATION_TO_DEGREE(ui->get_rotation()));
        launch_option();

        ret = SCL_EVENT_DONE;
    } else if (strcmp(event_desc.key_value, "Cancel") == 0) {
        ret = SCL_EVENT_DONE;
        const sclchar *input_mode = ui->get_input_mode();
        if (input_mode && strcmp(input_mode, "STT_3X4") == 0 ) {
            keyboard_state->need_reset = TRUE;
            voice_result_string_flush();
            ise_set_layout(keyboard_state->layout, keyboard_state->layout_variation);
            if (keyboard_state->visible_state)
                ise_show(keyboard_state->ic);
        }
#ifdef _TV
        ise_send_event(IME_KEY_Cancel, KEY_MASK_NULL);
        ime_request_hide();
#endif
    }  else if (strcmp(event_desc.key_value, "Done") == 0) {
#ifdef _TV
        ret = SCL_EVENT_DONE;
        ise_send_event(IME_KEY_Return, KEY_MASK_NULL);
        ime_request_hide();
#endif
    } else if (strcmp(event_desc.key_value, "Translate") == 0) {
        ise_nmt_translate();
    }  else {
        const sclchar *input_mode = ui->get_input_mode();
        if ((NULL != input_mode) && (!strcmp(input_mode, "EMOTICON_LAYOUT"))) {
            if (ise_emoticon_is_show()) {
                ise_emoticon_destroy_layout();
            }
#ifdef _WEARABLE
            emoticon_group_t group_id = EMOTICON_GROUP_1;
            if (ise_emoticon_get_current_group() < EMOTICON_GROUP_3)
                group_id = (emoticon_group_t)(ise_emoticon_get_current_group() + 1);

            ise_set_emoticon_label(group_id);
#else
            emoticon_group_t group_id = ise_emoticon_get_group_id(event_desc.key_value);
#endif
            if ((group_id >= 0) && (group_id < MAX_EMOTICON_GROUP)) {
                SCLRotation rotation = ui->get_rotation();
                ise_emoticon_show_layout(group_id, ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
            }
        }

        if (input_mode && (!strcmp(input_mode, "STICKER_LAYOUT"))) {
            if (ise_sticker_is_show())
                ise_sticker_change_group(ise_sticker_get_group_id(event_desc.key_value));
        }
    }

    if (_cm_popup_opened) {
        if (strcmp(event_desc.key_value, USER_KEYSTRING_OPTION) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_STICKER) == 0 ||
            strcmp(event_desc.key_value, USER_KEYSTRING_TRANSLATION) == 0) {
            scluint id = ise_get_cm_key_id(event_desc.key_value);
            if (id != _current_cm_key_id) {
                _current_cm_key_id = id;
                ise_set_cm_private_key(_current_cm_key_id);
            }
        }
        _cm_popup_opened = FALSE;
    }
}

SCLEventReturnType CUIEventCallback::on_event_key_clicked(SclUIEventDesc event_desc)
{
    SCLEventReturnType ret = SCL_EVENT_PASS_ON;
    if (word_timer != NULL) {
        ecore_timer_del(word_timer);
        word_timer = NULL;
    }

    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return ret;

    if (event_desc.key_modifier == KEY_MODIFIER_MULTITAP_START) {
        if (!keyboard_state->multitap_value.empty()) {
            ise_send_string(keyboard_state->multitap_value.c_str());
        }
        ise_update_preedit_string(event_desc.key_value);
        keyboard_state->multitap_value = event_desc.key_value;
    } else if (event_desc.key_modifier == KEY_MODIFIER_MULTITAP_REPEAT) {
        ise_update_preedit_string(event_desc.key_value);
        keyboard_state->multitap_value = event_desc.key_value;
    } else {
        _reset_multitap_state();
    }
    keyboard_state->prev_modifier = event_desc.key_modifier;

    CSCLUI *ui = get_ui();
    if (ui) {
        switch (event_desc.key_type) {
        case KEY_TYPE_STRING:
            on_event_string_key(ui, event_desc);
            break;
        case KEY_TYPE_CHAR:
            on_event_character_key(ui, event_desc, keyboard_state);
            break;
        case KEY_TYPE_CONTROL:
            on_event_control_key(ui, event_desc, keyboard_state);
            break;
        case KEY_TYPE_MODECHANGE:
            on_event_modechange_key(ui, event_desc, keyboard_state, ret);
            break;
        case KEY_TYPE_USER:
            process_user_key(ui, event_desc, keyboard_state, ret);
            break;
        default:
            break;
        }
    }

    return ret;
}

void
ise_set_layout(sclu32 layout, sclu32 layout_variation)
{
    /* Check if the layoutIdx is in the valid range */
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (layout < ISE_LAYOUT_STYLE_MAX) {
        if (keyboard_state->layout != layout ||
            keyboard_state->layout_variation != layout_variation) {
            keyboard_state->need_reset = TRUE;
        }
        keyboard_state->layout = layout;
        keyboard_state->layout_variation = layout_variation;
        LOGD("layout:%d, variation:%d\n", keyboard_state->layout, keyboard_state->layout_variation);
    }
}

void
ise_reset_context()
{
    LOGD("");
    _reset_multitap_state(true);
    CONFIG_VALUES *config_values = get_config_values();
    if (config_values) {
        _language_manager.reset_language(config_values->selected_language.c_str());
    }
}

void
ise_reset_input_context()
{
    LOGD("");
    _reset_multitap_state(true);
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (keyboard_state) keyboard_state->disable_force_latin = FALSE;
    CONFIG_VALUES *config_values = get_config_values();
    if (config_values) {
        _language_manager.reset_language(config_values->selected_language.c_str());
    }
}

void
ise_focus_in(int ic)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("ic : %x , %x , g_ic : %x , %x, g_focused_ic : %x , %x\n", ic, check_ic_temporary(ic),
            keyboard_state->ic, check_ic_temporary(keyboard_state->ic),
            keyboard_state->focused_ic, check_ic_temporary(keyboard_state->focused_ic));
    if (check_ic_temporary(keyboard_state->ic) && !check_ic_temporary(ic)) {
        keyboard_state->ic = ic;
    }
    keyboard_state->focused_ic = ic;
}

static void save_autofill_data()
{
    char *text = NULL;
    int cursor;

    if (ime_autofill_get_hint() == 0)
        return;

    ime_get_surrounding_text(-1, -1, &text, &cursor);
    SECURE_LOGD("surrounding text : %s\n", text);
    if (!text) return;

    ime_autofill_save_string(text);

    free(text);
}

void
ise_focus_out(int ic)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (keyboard_state) keyboard_state->focused_ic = 0;
    _reset_multitap_state(true);

    save_autofill_data();
}

bool ise_is_guideline_popup_enable_layout(void)
{
    bool ret;
    switch (_context_layout)
    {
        case ISE_LAYOUT_STYLE_PHONENUMBER:
        case ISE_LAYOUT_STYLE_IP:
        case ISE_LAYOUT_STYLE_MONTH:
        case ISE_LAYOUT_STYLE_NUMBERONLY:
        case ISE_LAYOUT_STYLE_HEX:
        case ISE_LAYOUT_STYLE_TERMINAL:
        case ISE_LAYOUT_STYLE_DATETIME:
        case ISE_LAYOUT_STYLE_PASSWORD:
            ret = false;
            break;
        default:
            ret = true;
            break;
    }
    return ret;
}

#if EXIT_ISE_ON_HIDE
static Eina_Bool exit_timer_cb(void *data)
{
    if (exit_timer) ecore_timer_del(exit_timer);
    exit_timer = NULL;
    elm_exit();
    return ECORE_CALLBACK_CANCEL;
}
#endif

void
ise_show(int ic)
{
    CONFIG_VALUES *config_values = get_config_values();
#if EXIT_ISE_ON_HIDE
    if (exit_timer) ecore_timer_del(exit_timer);
    exit_timer = NULL;
#endif
    sclboolean reset_inputmode = FALSE;
    g_input_panel_show = true;

    CSCLUI *ui = get_ui();
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (ui && keyboard_state) {
        read_ise_config_values();

        if (config_values) {
            _language_manager.set_enabled_languages(config_values->enabled_languages);
        }
        const sclchar *cur_lang = _language_manager.get_current_language();

#ifdef _WEARABLE
        ise_check_wearable_candidate();
        /*
         *Clear the personalized data
         */
        if (config_values && config_values->dataclear) {
            ime_send_key_event(IME_KEY_Clear, IME_KEY_MASK_CONTROL, false);
            config_values->dataclear = FALSE;
        }
#endif
        LOGD("ic : %x , %x , g_ic : %x , %x, g_focused_ic : %x , %x\n", ic, check_ic_temporary(ic),
                keyboard_state->ic, check_ic_temporary(keyboard_state->ic),
                keyboard_state->focused_ic, check_ic_temporary(keyboard_state->focused_ic));

        if (check_ic_temporary(ic) && !check_ic_temporary(keyboard_state->focused_ic)) {
            ic = keyboard_state->focused_ic;
        }

        if (!check_ic_temporary(ic) && check_ic_temporary(keyboard_state->focused_ic)) {
            keyboard_state->focused_ic = ic;
        }

        if (ic == keyboard_state->focused_ic) {
            switch (keyboard_state->layout)
            {
                case ISE_LAYOUT_STYLE_PHONENUMBER:
                case ISE_LAYOUT_STYLE_IP:
                case ISE_LAYOUT_STYLE_MONTH:
                case ISE_LAYOUT_STYLE_NUMBERONLY:
                    ime_set_imengine(DEFAULT_KEYBOARD_ISE_UUID);
                    break;
                default:
                    break;
            }
        }

        const char *input_mode = ui->get_input_mode();

        /* Reset input mode if the input context value has changed */
        if (ic != keyboard_state->ic) {
            /* Do not reset input mode if STT's setting window was opened */
            if (get_setting_window_open_status() && input_mode && strcmp(input_mode, "STT_3X4") == 0) {
                LOGD("Setting window was opened while using STT, skip resetting input mode");
            } else {
                reset_inputmode = TRUE;
            }
        }

        keyboard_state->ic = ic;
        /* Reset input mode if the current language is not the selected language */
        if (cur_lang) {
            if (config_values && config_values->selected_language.compare(cur_lang) != 0) {
                reset_inputmode = TRUE;
            }
        }

        /* No matter what, just reset the inputmode if it needs to */
        if (keyboard_state->need_reset) {
            /* Do not reset input mode if STT's setting window was opened */
            if (get_setting_window_open_status() && input_mode && strcmp(input_mode, "STT_3X4") == 0) {
                LOGD("Setting window was opened while using STT, skip resetting input mode");
            } else {
                reset_inputmode = TRUE;
            }
        }
        keyboard_state->need_reset = FALSE;

        /* If the current layout requires latin language and current our language is not latin, enable the primary latin */
        sclboolean force_primary_latin = FALSE;
        LANGUAGE_INFO *info =
            (config_values ? _language_manager.get_language_info(config_values->selected_language.c_str()) : NULL);
        if (info) {
            if (!info->is_latin_language) {
                if (!keyboard_state->disable_force_latin) {
                    if (g_ise_default_values[keyboard_state->layout].force_latin) {
                        force_primary_latin = TRUE;
                    }
                    else {
                        if (keyboard_state->prefer_latin) {
                            force_primary_latin = TRUE;
                        }
                    }
                }
            }
        }

        if (force_primary_latin) {
            /* If there is enabled latin-based language, select it */
            sclboolean selected = FALSE;
            for (scluint loop = 0;!selected && loop < _language_manager.get_languages_num();loop++) {
                LANGUAGE_INFO *info = _language_manager.get_language_info(loop);
                if (info) {
                    if (info->enabled && info->is_latin_language) {
                        selected = _language_manager.select_language(info->name.c_str());
                        if (selected) force_primary_latin = FALSE;
                    }
                }
            }
            if (!selected) {
                _language_manager.set_language_enabled_temporarily(PRIMARY_LATIN_LANGUAGE, TRUE);
            }
        }

        if (reset_inputmode) {
            ise_reset_context();

            bool filename_layout = false;

            /* Turn the shift state off if we need to reset our input mode, only when auto-capitalization is not set  */
            if (!(keyboard_state->caps_mode)) {
                ui->set_shift_state(SCL_SHIFT_STATE_OFF);
            }
            if (keyboard_state->layout < ISE_LAYOUT_STYLE_MAX) {
                sclu32 layout_index = keyboard_state->layout;
                if (keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBERONLY &&
                    keyboard_state->layout_variation > 0 &&
                    keyboard_state->layout_variation < ISE_LAYOUT_NUMBERONLY_VARIATION_MAX) {
                    layout_index = ISE_LAYOUT_STYLE_NUMBERONLY_SIG + keyboard_state->layout_variation - 1;
                } else if (keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWORD && keyboard_state->layout_variation > 0) {
                    layout_index = ISE_LAYOUT_STYLE_PASSWD_3X4;
                }

                if (keyboard_state->layout == ISE_LAYOUT_STYLE_NORMAL &&
                    keyboard_state->layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL_VARIATION_FILENAME)
                    filename_layout = true;
                else
                    filename_layout = false;

                ui->enable_button("exclamation", !filename_layout);
                ui->enable_button("question", !filename_layout);
                ui->enable_button("divide", !filename_layout);
                ui->enable_button("multiply", !filename_layout);
                ui->enable_button("colon", !filename_layout);
                ui->enable_button("quotation", !filename_layout);

                ui->enable_button("CM_KEY", !access("/home", X_OK));

                LOGD("new layout index : %d\n", layout_index);
                /* If this layout requires specific input mode, set it */
                if (strlen(g_ise_default_values[layout_index].input_mode) > 0) {
                    ui->set_input_mode(g_ise_default_values[layout_index].input_mode);
                    set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
                } else {
                    if (force_primary_latin) {
                        _language_manager.select_language(PRIMARY_LATIN_LANGUAGE, TRUE);
                    } else if (config_values) {
                        if (!(_language_manager.select_language(config_values->selected_language.c_str()))) {
                            _language_manager.select_language(PRIMARY_LATIN_LANGUAGE);
                        }
                    }
                }
                ui->set_cur_sublayout(g_ise_default_values[layout_index].sublayout_name);
                if (ise_emoticon_is_show()) {
                    ise_emoticon_destroy_layout();
                }

                if (keyboard_state->layout == ISE_LAYOUT_STYLE_EMOTICON) {
                    ise_emoticon_init_list();
#ifdef _WEARABLE
                    ise_emoticon_set_current_group(EMOTICON_GROUP_1);
                    ise_set_emoticon_label(1);
#else
                    if (ise_emoticon_get_recent_list_size() == 0)
                        ise_emoticon_set_current_group(EMOTICON_GROUP_1);
                    else
                        ise_emoticon_set_current_group(EMOTICON_GROUP_RECENTLY_USED);
#endif
                    SCLRotation rotation = ui->get_rotation();
                    ise_emoticon_show_layout(ise_emoticon_get_current_group(), ROTATION_TO_DEGREE(rotation), false, ime_get_main_window());
                }

                if (ise_sticker_is_show())
                    ise_sticker_destroy_layout();

                if (ise_nmt_is_show())
                    ise_nmt_destroy_layout();
            }
        }

        if (info) {
            if (info->accepts_caps_mode) {
                // FIXME this if condition means the AC is off
                if (keyboard_state->layout != ISE_LAYOUT_STYLE_NORMAL) {
                    ui->set_autocapital_shift_state(TRUE);
                    ui->set_shift_state(SCL_SHIFT_STATE_OFF);
                } else {
                    ise_send_event(MVK_Shift_Enable, KEY_MASK_NULL);
                    // Auto Capital is supported only in normal layout
                    if (keyboard_state->caps_mode) {
                        ui->set_autocapital_shift_state(FALSE);
                    }
                }
            } else {
                ui->set_autocapital_shift_state(TRUE);
                ise_send_event(MVK_Shift_Disable, KEY_MASK_NULL);
                ui->set_shift_state(SCL_SHIFT_STATE_OFF);
            }
        } else {
            ui->set_autocapital_shift_state(TRUE);
        }

        // Update CM key button
        if (_current_cm_key_id < CM_KEY_LIST_SIZE) {
            if (strcmp(_cm_key_list[_current_cm_key_id], USER_KEYSTRING_EMOTICON) == 0) {
                if (ise_is_emoticons_disabled())
                    ise_set_cm_private_key(ise_get_cm_key_id(USER_KEYSTRING_OPTION));
                else
                    ise_set_cm_private_key(_current_cm_key_id);
            }
        }

        // Update space key button
        ise_update_space_key();

        ui->show();
        ui->disable_input_events(FALSE);
#ifdef _IVI
        ui->enable_button("CM_KEY", false);
#endif

#if defined(_MOBILE) || defined(_COMMON)
        if (ise_is_guideline_popup_enable_layout()) {
            if ((config_values && config_values->first_guidechange) && (_language_manager.get_enabled_languages_num() > 1)) {
                ise_show_help_popup(MVK_space, NATIVE_WINDOW_CAST(ime_get_main_window()));
                LOGD("space popup show\n");
            } else {
                ise_destroy_popup_space();
            }
        }
#endif

        // Update IME size
        if (config_values && config_values->floating_mode != g_floating_mode)
            g_floating_mode = config_values->floating_mode;

        Candidate *candidate = get_candidate();
        if (candidate && candidate->get_visible())
            set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
        else
            set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
    }

    keyboard_state->visible_state = TRUE;

#ifdef _WEARABLE
    int dot_num = 5;
    switch (_context_layout) {
        case ISE_LAYOUT_STYLE_PHONENUMBER:
        case ISE_LAYOUT_STYLE_IP:
        case ISE_LAYOUT_STYLE_MONTH:
        case ISE_LAYOUT_STYLE_NUMBERONLY:
        case ISE_LAYOUT_STYLE_DATETIME:
        case ISE_LAYOUT_STYLE_TERMINAL:
            dot_num = 0;
            break;
        case ISE_LAYOUT_STYLE_EMAIL:
        case ISE_LAYOUT_STYLE_URL:
            dot_num = 4;
            break;
        case ISE_LAYOUT_STYLE_PASSWORD:
            if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
                _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
                dot_num = 0;
            else
                dot_num = 3;
            break;
        default:
            dot_num = 5;
    }

    if (dot_num > 0) {
        sclint width = 0;
        sclint height = 0;
        if (ui)
            ui->get_screen_resolution(&width, &height);

        create_indicator_window(width, height);
        destroy_indicator_dots();

        int focus_dot = 0;
        switch (keyboard_state->layout) {
            case ISE_LAYOUT_STYLE_NUMBER:
                focus_dot = 1;
                break;
            case ISE_LAYOUT_STYLE_HEX:
                focus_dot = 2;
                break;
            case ISE_LAYOUT_STYLE_EMOTICON:
                focus_dot = 3;
                break;
            case ISE_LAYOUT_STYLE_VOICE:
                if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
                    _context_layout == ISE_LAYOUT_STYLE_URL)
                    focus_dot = 3;
                else
                    focus_dot = 4;
                break;
            default:
                break;
        }

        create_indicator_dots(dot_num, focus_dot);
        show_indicator_window();
    } else {
        hide_indicator_window();
    }
    if (keyboard_state->layout == ISE_LAYOUT_STYLE_NORMAL && config_values && config_values->number_tutorial_enable) {
        ise_show_tutorial_mode_popup(keyboard_state->layout);
        config_values->number_tutorial_enable = false;
        write_ise_config_values();
    }
#endif

    if (get_setting_window_open_status()) {
        ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
    }
    set_setting_window_open_status(FALSE);

    if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
        ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
    }
}

/**
 * Sets screen rotation
 */
void
ise_set_screen_rotation(int degree)
{
    CSCLUI *ui = get_ui();
    if (ui) {
        ui->set_rotation(DEGREE_TO_SCLROTATION(degree));
    }

    Candidate *candidate = get_candidate();
    if (candidate) {
        candidate->rotate(degree);
        if (candidate->get_visible()) {
            candidate->update(g_softcandidate_string);
        }
    }
}

void
ise_set_accessibility_state(bool state)
{
    CSCLUI *ui = get_ui();
    if (ui) {
        ui->enable_tts(state);
    }
}

void
ise_hide()
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    CSCLUI *ui = get_ui();
    if (ui) {
        /* There's no need to update screen when hiding */
        ui->set_update_pending(TRUE);
        ui->disable_input_events(TRUE);
        ui->hide();
    }
    CONFIG_VALUES *config_values = get_config_values();
    if (config_values) {
        _language_manager.reset_language(config_values->selected_language.c_str());
    }

    _click_count = 0;
    delete_commit_timer();
    ise_destroy_popup_space();
    ise_destroy_popup_setting();

    if (keyboard_state) keyboard_state->visible_state = FALSE;

    _reset_shift_state();
    _reset_multitap_state(true);

    /* If we were in STT mode, try to reset input mode on our next show event */
    if (ui && keyboard_state) {
        const char *inputmode = ui->get_input_mode();
        if (inputmode && strcmp(inputmode, "STT_3X4") == 0 ) {
            keyboard_state->need_reset = TRUE;
            ise_hide_stt_mode();
        }
    }

    g_input_panel_show = false;
    Candidate *candidate = get_candidate();
    if (candidate && candidate->get_visible())
        set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
#ifdef _WEARABLE
    hide_indicator_window();
    if (check_is_tutorial_show()) {
        ise_destroy_tutorial_mode_popup();
    }
#endif
#if EXIT_ISE_ON_HIDE
    if (exit_timer) ecore_timer_del(exit_timer);
    exit_timer = ecore_timer_add(1.0, exit_timer_cb, NULL);
#endif
}

static void ise_keypad_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_keypad_mode();
}

static void ise_enabled_languages_changed_cb(const char *key, void *user_data)
{
    read_ise_enabled_languages();
}

static void ise_selected_language_changed_cb(const char *key, void *user_data)
{
    read_ise_selected_language();
}

static void ise_autocapital_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_autocapital_mode();
}

static void ise_autopunctuate_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_autopunctuation_mode();
}

static void ise_sound_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_sound_mode();

    CONFIG_VALUES *config_values = get_config_values();
    CSCLUI *ui = get_ui();
    if (config_values && ui) {
        ui->enable_sound(config_values->sound_on);
    }
}

static void ise_vibration_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_vibration_mode();

    CONFIG_VALUES *config_values = get_config_values();
    CSCLUI *ui = get_ui();
    if (config_values && ui) {
        ui->enable_vibration(config_values->vibration_on);
    }
}

static void ise_character_preview_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_character_preview_mode();

    CONFIG_VALUES *config_values = get_config_values();
    CSCLUI *ui = get_ui();
    if (config_values && ui) {
        ui->enable_magnifier(config_values->preview_on);
    }
}

static void ise_setting_guide_popup_changed_cb(const char *key, void *user_data)
{
    read_ise_setting_guide_popup_mode();
}

static void ise_language_guide_popup_changed_cb(const char *key, void *user_data)
{
    read_ise_language_guide_popup_mode();
}

static void ise_floating_mode_changed_cb(const char *key, void *user_data)
{
    read_ise_floating_mode();
    CONFIG_VALUES *config_values = get_config_values();
    if (config_values) {
        g_floating_mode = config_values->floating_mode;
    }

    ime_set_floating_mode(g_floating_mode);
    if (g_candidate && g_floating_mode != g_candidate->get_floating_mode()) {
        delete_softcandidate_hide_timer();
        delete g_candidate;
        g_candidate = NULL;

        create_softcandidate();
        if (g_candidate && g_candidate->get_visible()) {
            g_candidate->show();
            set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
            return;
        }
    }
    set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);
}

static void register_preference_changed_callback(void)
{
    if (preference_set_changed_cb(ISE_CONFIG_KEYPAD_MODE, ise_keypad_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_keypad_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_ENABLED_LANGUAGES, ise_enabled_languages_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_enabled_languages_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_SELECTED_LANGUAGE, ise_selected_language_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_selected_language_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_AUTO_CAPITALISE, ise_autocapital_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_autocapital_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_AUTO_PUNCTUATE, ise_autopunctuate_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_autopunctuate_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_SOUND_ON, ise_sound_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_sound_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_VIBRATION_ON, ise_vibration_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_vibration_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_PREVIEW_ON, ise_character_preview_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_character_preview_mode_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_FIRST_GUIDELINE_POPUP_FOR_SETTING, ise_setting_guide_popup_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_setting_guide_popup_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_FIRST_GUIDELINE_POPUP_FOR_LANGUAGE_CHANGE, ise_language_guide_popup_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_language_guide_popup_changed_cb()");

    if (preference_set_changed_cb(ISE_CONFIG_FLOATING_MODE, ise_floating_mode_changed_cb, NULL) != PREFERENCE_ERROR_NONE)
        LOGW("Failed to set ise_floating_mode_changed_cb()");
}

void
ise_create()
{
    LOGD("");

    CONFIG_VALUES *config_values = get_config_values();

    if (!g_ui) {
        g_ui = new CSCLUI;
    }

    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);

    /* Set scl_parser_type
     * default type is text xml
     * use command: export sclres_type="sclres_binary" to enable use binary resource
     * please make sure there is sclresource.bin in resource folder
     * Or you can use `xml2binary $resource_dir` to generate the sclresource.bin
     * xml2binary is in the libscl-ui-devel package
     */
    SCLParserType scl_parser_type = SCL_PARSER_TYPE_XML;
    char* sclres_type = getenv("sclres_type");
    if (sclres_type != NULL && 0 == strcmp("sclres_binary", sclres_type)) {
        scl_parser_type = SCL_PARSER_TYPE_BINARY_XML;
    } else {
        scl_parser_type = SCL_PARSER_TYPE_XML;
    }

    if (g_ui) {
        if (ime_get_main_window()) {
            g_ise_created = true;

            sclboolean succeeded = FALSE;

            const sclchar *entry_path = MAIN_ENTRY_XML_PATH;
            _language_manager.set_resource_file_path(entry_path);
            const sclchar *resource_file_path = _language_manager.get_resource_file_path();

            if (resource_file_path) {
                if (strlen(resource_file_path) > 0) {
                    succeeded = g_ui->init(ime_get_main_window(), scl_parser_type, resource_file_path);
                }
            }
            if (!succeeded) {
                g_ui->init(ime_get_main_window(), scl_parser_type, MAIN_ENTRY_XML_PATH);
            }

            g_ui->set_longkey_duration(elm_config_longpress_timeout_get() * 1000);

            /* Default ISE callback */
            g_ui->set_ui_event_callback(&callback);

            /* Accumulated customized ISE callbacks, depending on the input modes */
            for (scluint loop = 0;loop < _language_manager.get_languages_num();loop++) {
                LANGUAGE_INFO *language = _language_manager.get_language_info(loop);
                if (language) {
                    for (scluint inner_loop = 0;inner_loop < language->input_modes.size();inner_loop++) {
                        INPUT_MODE_INFO &info = language->input_modes.at(inner_loop);
                        LOGD("Registering callback for input mode %s : %p\n", info.name.c_str(), language->callback);
                        g_ui->set_ui_event_callback(language->callback, info.name.c_str());
                    }
                }
            }

            read_ise_config_values();
            if (access(ISE_CONFIG_FILE_PATH, F_OK) == 0 && !config_values->init_flag)
                read_ise_config_file();

            if (config_values) {
                _language_manager.set_enabled_languages(config_values->enabled_languages);
                _language_manager.select_language(config_values->selected_language.c_str());
                vconf_set_bool(VCONFKEY_AUTOCAPITAL_ALLOW_BOOL, config_values->auto_capitalise);
                vconf_set_bool(VCONFKEY_AUTOPERIOD_ALLOW_BOOL, config_values->auto_punctuate);
                g_ui->enable_sound(config_values->sound_on);
                g_ui->enable_vibration(config_values->vibration_on);
                g_ui->enable_magnifier(config_values->preview_on);
                g_floating_mode = config_values->floating_mode;
            }
#ifdef _TV
            g_ui->enable_highlight_ui(TRUE);
#endif
            ime_set_floating_mode(g_floating_mode);
        }

        set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_NONE);

        bool exist = false;

        preference_is_existing(ISE_CONFIG_KEYPAD_MODE, &exist);
        if (!exist)
            write_ise_config_values();

        register_preference_changed_callback();
    }
    init_recent_used_punctuation();
}

void
ise_destroy()
{
    ise_hide_stt_mode();

    CSCLUI *ui = get_ui();
    if (ui) {
        LOGD("calling ui->fini()\n");
        ui->fini();
        LOGD("deleting ui\n");
        delete ui;
        ui = NULL;
        g_ui = NULL;
    }

    if (g_candidate) {
        delete g_candidate;
        g_candidate = NULL;
    }

#ifdef _WEARABLE
    destroy_indicator_window();
#endif

    /* This is necessary. If this is not called, 3rd party IME might have auto period input regardless its settings */
    vconf_set_bool(VCONFKEY_AUTOPERIOD_ALLOW_BOOL, false);

#if EXIT_ISE_ON_HIDE
    if (exit_timer) ecore_timer_del(exit_timer);
    exit_timer = NULL;
#endif
}

void
ise_app_candidate_show()
{
#ifdef _WEARABLE
    if (!g_candidate)
        return;

    if (!g_candidate->get_visible())
        return;
#endif
    LOGD("Enter\n");
    delete_softcandidate_hide_timer();

    create_softcandidate();

    Candidate *candidate = get_candidate();
    if (candidate) {
        candidate->show();
        set_ime_size(g_floating_mode, ISE_CANDIDATE_REQ_SHOW);
    }
}

void
ise_app_candidate_hide()
{
    LOGD("Enter\n");

    Candidate *candidate = get_candidate();
    if (!candidate || !candidate->get_visible()) {
        LOGD("No candidate\n");
        return;
    }

    if (g_ic_smartreply != -1 || ime_autofill_get_exist())
        return;

    add_softcandidate_hide_timer();
}

#ifdef _WEARABLE
void ise_check_wearable_candidate()
{
    CONFIG_VALUES *config_values = get_config_values();
    if (!config_values) return;

    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    if (!config_values->prediction_on) {
        ise_app_candidate_hide();
    } else if (keyboard_state->layout == ISE_LAYOUT_STYLE_PHONENUMBER ||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE ||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_IP ||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_MONTH ||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_NUMBERONLY ||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWD_3X4||
                  keyboard_state->layout == ISE_LAYOUT_STYLE_PASSWORD) {
        ise_app_candidate_hide();
    } else {
        ise_app_candidate_show();
    }
}

void ise_set_emoticon_label(int group_id)
{
    const int BUF_LEN = 16;
    char buf[BUF_LEN] = {0};
    snprintf(buf, BUF_LEN, "%d/3", group_id);
    CSCLUI *ui = get_ui();
    if (ui) ui->set_private_key("EMOTICON_GROUP_ID", buf, NULL, NULL, 0, const_cast<sclchar*>("EMOTICON_GROUP_NEXT"), TRUE);
}
#endif

// when it is the time to auto_cap, the
// ise_set_caps_mode is called.
// -------------------------------------------------------
// For example: [How are you. Fine.], the
// auto-capital process is as below:
// Note: "["<--this is the beginning,
// "|"<--this is the cursor position
// 1) call ise_set_caps_mode, auto_cap = on
//    input: "H",
//    result: [H|
// 2) call ise_set_caps_mode, auto_cap = off
//    input: "o"
//    result: [Ho|
// 3) input: "w are you. "
//    result: [How are you. |
// 4) call ise_set_caps_mode, auto_cap = on
//    input: "F"
//    result: [How are you. F
// 5) input: "ine."
//    result: [How are you. Fine.|
// --------------------------------------------------------
// If we want to change the auto_cap, eg,
// if we want to input [How Are you.]
// Note the "Are" is not use auto-capital rule.
// we should use:
//    ise_send_event(MVK_Shift_On, SclCoreKeyMask_Null);
// when we are want to input "A"
// following input still has the auto_cap rule.
void
ise_set_caps_mode(unsigned int mode)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("caps_mode : %d\n", mode);
    if (mode) {
        keyboard_state->caps_mode = TRUE;
    } else {
        keyboard_state->caps_mode = FALSE;
    }
    g_caps_mode_pending = false;
    const sclchar *cur_lang = _language_manager.get_current_language();
    if (cur_lang) {
        LANGUAGE_INFO *info = _language_manager.get_language_info(cur_lang);
        if (info) {
            if (info->accepts_caps_mode) {
                /* If we are inputting multitap character, do not manipulate shift mode */
                if (keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_START &&
                    keyboard_state->prev_modifier != KEY_MODIFIER_MULTITAP_REPEAT) {
                    set_caps_mode(keyboard_state->caps_mode);
                } else {
                    g_caps_mode_pending = true;
                    LOGD("Currently composing multitap string, skipping caps request");
                }
            }
        }
    }
}

void
ise_update_cursor_position(int position)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("cursor position : %d\n", position);
    CSCLUI *ui = get_ui();
    if (ui && keyboard_state->layout == ISE_LAYOUT_STYLE_URL) {
#ifndef _TV
        if (position > 0) {
            ui->set_string_substitution("www.", ".com");
        } else {
            ui->unset_string_substitution("www.");
        }
#endif
    }
}

void ise_set_return_key_type(unsigned int type)
{
    const int BUF_LEN = 256;
    char buf[BUF_LEN] = {0};

    CSCLUI *ui = get_ui();
    if (!ui) return;

    LOGD("return key type : %d\n", type);
    switch (type)
    {
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DONE:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_DONE);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_GO:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_GO);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_JOIN:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_JOIN);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_LOGIN:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_LOGIN);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_NEXT:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_NEXT);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEARCH);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEND:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SEND);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SIGNIN:
        snprintf(buf, BUF_LEN, ISE_RETURN_KEY_LABEL_SIGNIN);
        break;
    case ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT:
        break;
    default:
        LOGW("Unknown return key type : %d\n", type);
        type = ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT;
        break;
    }

    if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT) {
        ui->unset_private_key("Enter");
#ifdef _TV
        ui->unset_private_key("Done");
#endif
    } else {
#ifdef _WEARABLE
        if (type == ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH) {
            sclchar* imagelabel[SCL_BUTTON_STATE_MAX] = {
                const_cast<sclchar*>("w_sip_3x4_btn_ic_search.png"),
                const_cast<sclchar*>("w_sip_3x4_btn_ic_search_p.png"),
                const_cast<sclchar*>("w_sip_3x4_btn_ic_search_d.png")};
            ui->set_private_key("Enter", const_cast<sclchar*>(""), imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
            return;
        }
#endif

#ifdef _TV
        ui->set_private_key("Done", buf, NULL, NULL, 0, const_cast<sclchar*>("Done"), TRUE);
#else
        static sclchar *imagelabel[SCL_BUTTON_STATE_MAX] = {
            const_cast<sclchar*>(" "), const_cast<sclchar*>(" "), const_cast<sclchar*>(" ")
        };

        ui->set_private_key("Enter", buf, imagelabel, NULL, 0, const_cast<sclchar*>("Enter"), TRUE);
#endif
        LOGD("return key label : %s\n", buf);
    }
}

void ise_set_return_key_disable(unsigned int disabled)
{
    CSCLUI *ui = get_ui();
    LOGD("enable : %d\n", !disabled);
#ifdef _TV
    if (ui)
        ui->enable_button("Done", !disabled);
#else
    if (ui)
        ui->enable_button("Enter", !disabled);
#endif
}

void ise_get_language_locale(char **locale)
{
    LANGUAGE_INFO *info = _language_manager.get_current_language_info();
    if (info) {
        if (!(info->locale_string.empty())) {
            *locale = strdup(info->locale_string.c_str());
        }
    }
}

void ise_update_table(const vector<string> &vec_str)
{
    Candidate *candidate = get_candidate();
    if (!candidate || !candidate->get_visible()) {
        create_softcandidate();
    }

    candidate = get_candidate();
    if (candidate) {
        candidate->update(vec_str);
    }
}

void ise_process_key_event(scim::KeyEvent& key, sclu32 &ret)
{
    Eina_Bool back_key_pressed = EINA_FALSE;
    Eina_Bool back_key_released = EINA_FALSE;

    if (key.get_key_string().compare("XF86Back") == 0) {
        back_key_pressed = EINA_TRUE;
    } else if (key.get_key_string().compare("KeyRelease+XF86Back") == 0) {
        back_key_released = EINA_TRUE;
    }

    if (back_key_pressed || back_key_released) {
        if (g_popup_opened == TRUE) {
            if (back_key_released) {
                CSCLUI *ui = get_ui();
                if (ui) ui->close_all_popups();
            }
            ret = 1;
            return;
        }
    }

    ret = 0;
    CSCLUI *ui = get_ui();
#ifdef _TV
    Candidate *candidate = get_candidate();
    if (candidate) {
        if (!candidate->soft_candidate_flag()) {
            if (ui) {
                if (key.dev_name.compare("ime") != 0) {
                    ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
                }
            }
        }
        if (!ret) {
            ret = candidate->soft_candidate_handle_key_event(key.get_key_string().c_str());
        }
    } else {
        if (ui) {
            /* Process this key event if it was not generated by ime */
            if (key.dev_name.compare("ime") != 0) {
                ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
            }
        }
    }
#else
    if (ui) {
        /* Process this key event if it was not generated by ime */
        if (key.dev_name.compare("ime") != 0) {
            ret = (sclu32)ui->process_key_event(key.get_key_string().c_str());
        }
    }
#endif
}

static void init_recent_used_punctuation()
{
    if (g_recent_used_punctuation.empty())
    {
        g_recent_used_punctuation.push_back("#");
        g_recent_used_punctuation.push_back("$");
        g_recent_used_punctuation.push_back("%");
        g_recent_used_punctuation.push_back("^");
        g_recent_used_punctuation.push_back("&");
    }
}

static void update_recent_used_punctuation(const char * key_value)
{
    if (NULL == key_value)
    {
        return;
    }
    for (int i = 0; i < 10; ++i)
    {
        char buf[5] = {0};
        snprintf(buf, sizeof(buf), "%d", i);
        if (strcmp(key_value, buf) == 0)
        {
            return;
        }
    }
    string strKey = string(key_value);
    for (int i = 0; i < MAX_DEFAULT_PUNCTUATION; ++i)
    {
        if (0 == strKey.compare(g_default_punctuation[i].c_str()))
        {
            return;
        }
    }
    vector<string>::iterator iter = g_recent_used_punctuation.begin();
    for (; iter != g_recent_used_punctuation.end(); ++iter)
    {
        if (0 == strKey.compare(iter->c_str()))
        {
            break;
        }
    }
    if (iter != g_recent_used_punctuation.end())
    {
        g_recent_used_punctuation.erase(iter);
    }
    g_recent_used_punctuation.push_back(strKey);
    if (g_recent_used_punctuation.size() > MAX_DEFAULT_PUNCTUATION-1)
    {
        g_recent_used_punctuation.erase(g_recent_used_punctuation.begin());
    }
}

static void set_ime_size(bool floating_mode, ISE_CANDIDATE_REQUEST candidate_req)
{
    CSCLUI *ui = get_ui();
    if (!ui)
        return;

    Candidate *candidate = get_candidate();

    const char *input_mode = ui->get_input_mode();
    if (!input_mode)
        return;

    SclSize size_portrait = ui->get_input_mode_size(input_mode, DISPLAYMODE_PORTRAIT);
    SclSize size_landscape = ui->get_input_mode_size(input_mode, DISPLAYMODE_LANDSCAPE);

    if (floating_mode) {
        size_portrait.width *= FLOATING_SCALE_RATE;
        size_portrait.height *= FLOATING_SCALE_RATE;
        size_landscape.width *= FLOATING_SCALE_RATE;
        size_landscape.height *= FLOATING_SCALE_RATE;
        switch (candidate_req) {
            case ISE_CANDIDATE_REQ_NONE:
                if (candidate && candidate->get_visible()) {
                    ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
                    size_portrait.height += candidate->get_height();
                    size_landscape.height += candidate->get_height();
                } else {
                    ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
                }
                break;
            case ISE_CANDIDATE_REQ_SHOW:
                if (candidate) {
                    ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT + candidate->get_height());
                    if (g_input_panel_show || g_candidate_more_view) {
                        size_portrait.height += candidate->get_height();
                        size_landscape.height += candidate->get_height();
                    } else {
                        size_portrait.height = candidate->get_height();
                        size_landscape.height = candidate->get_height();
                    }
                }
                break;
            case ISE_CANDIDATE_REQ_HIDE:
                ui->set_custom_starting_coordinates(0, FLOATING_TITLE_BAR_HEIGHT);
                break;
            default: break;
        }

        if (ui->get_custom_scale_rate_x() != FLOATING_SCALE_RATE || ui->get_custom_scale_rate_y() != FLOATING_SCALE_RATE)
            ui->set_custom_scale_rate(FLOATING_SCALE_RATE, FLOATING_SCALE_RATE);

        ime_set_size(size_portrait.width, size_portrait.height + FLOATING_TITLE_BAR_HEIGHT,
            size_landscape.width, size_landscape.height + FLOATING_TITLE_BAR_HEIGHT);

#if defined(_MOBILE) || defined(_COMMON)
        ise_destroy_move_handler();
        int rotation = elm_win_rotation_get(NATIVE_WINDOW_CAST(ime_get_main_window()));
        int handler_width = (rotation == 0 || rotation == 180) ? size_portrait.width : size_landscape.width;
        ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
#endif
    } else {
        switch (candidate_req) {
            case ISE_CANDIDATE_REQ_NONE:
                if (candidate && candidate->get_visible()) {
                    ui->set_custom_starting_coordinates(0, candidate->get_height());
                    size_portrait.height += candidate->get_height();
                    size_landscape.height += candidate->get_height();
                } else {
                    ui->set_custom_starting_coordinates(0, 0);
                }
                break;
            case ISE_CANDIDATE_REQ_SHOW:
                if (candidate) {
                    ui->set_custom_starting_coordinates(0, candidate->get_height());
                    if (g_input_panel_show || g_candidate_more_view) {
                        size_portrait.height += candidate->get_height();
                        size_landscape.height += candidate->get_height();
                    } else {
                        size_portrait.height = candidate->get_height();
                        size_landscape.height = candidate->get_height();
                    }
                }
                break;
            case ISE_CANDIDATE_REQ_HIDE:
                ui->set_custom_starting_coordinates(0, 0);
                break;
            default: break;
        }

        if (ui->get_custom_scale_rate_x() != 1.0 || ui->get_custom_scale_rate_y() != 1.0)
            ui->set_custom_scale_rate(1.0, 1.0);

        ime_set_size(size_portrait.width, size_portrait.height, size_landscape.width, size_landscape.height);

#if defined(_MOBILE) || defined(_COMMON)
        ise_destroy_move_handler();
#endif
    }
}

static void ime_app_create_cb(void *user_data)
{
    if (!engine_loader_dbus_init(NULL))
        LOGE("Failed to initialize dbus");

#if !(DEFER_ISE_CREATION)
    ise_create();
#endif
    elm_app_name_set(PACKAGE);

    char *elm_scale = getenv("ELM_SCALE");
    LOGD("ELM_SCALE : %s", elm_scale);

    if (elm_scale)
        elm_app_base_scale_set(atof(elm_scale));

    if (nmt_check_agent_install())
        nmt_init();
}

static void ime_app_exit_cb(void *user_data)
{
    engine_loader_terminate_ise();
    ise_hide();
    ise_destroy();

    if (!engine_loader_dbus_shutdown())
        LOGE("Failed to finalize dbus");

    nmt_shutdown();
}

static void show_autofill_data()
{
    ime_autofill_set_exist(false);
    string autofill_string = ime_autofill_get_string();

    SECURE_LOGD("autofill string : %s", autofill_string.c_str());

    if (!autofill_string.empty()) {
        ime_autofill_set_exist(true);

        ise_app_candidate_show();
        update_candidate_table();
    }
}

static void ime_app_show_cb(int ic, ime_context_h ime_ctx, void *user_data)
{
    Ise_Context iseContext;
    bool return_key_state, prediction_allow, password_mode, caps_mode;
    ime_layout_variation_e layout_variation;

    if (!g_ise_created)
        ise_create();

    ime_context_get_layout(ime_ctx, &iseContext.layout);

    ime_context_get_layout_variation(ime_ctx, &layout_variation);
    iseContext.layout_variation =  (int)layout_variation;

    ime_context_get_cursor_position(ime_ctx, &iseContext.cursor_pos);
    ime_context_get_autocapital_type(ime_ctx, &iseContext.autocapital_type);
    ime_context_get_return_key_type(ime_ctx, &iseContext.return_key_type);
    ime_context_get_return_key_state(ime_ctx, &return_key_state);

    ime_context_get_prediction_mode(ime_ctx, &prediction_allow);
    iseContext.prediction_allow = prediction_allow;

    ime_context_get_password_mode(ime_ctx, &password_mode);
    iseContext.password_mode = password_mode;

    ime_context_get_input_hint(ime_ctx, &iseContext.input_hint);
    ime_context_get_bidi_direction(ime_ctx, &iseContext.bidi_direction);
    ime_context_get_language(ime_ctx, &iseContext.language);
    ime_context_get_caps_mode(ime_ctx, &caps_mode);
    iseContext.caps_mode = caps_mode;

    iseContext.return_key_disabled = return_key_state;

    engine_loader_set_input_hint((uint32_t)iseContext.input_hint);
    engine_loader_update_bidi_direction((uint32_t)iseContext.bidi_direction);

    g_ic = ic;

    ime_autofill_set_hint(iseContext.input_hint & ECORE_IMF_INPUT_HINT_AUTOFILL_MASK);

    LOGD("input hint : %x, autofill hint : %x\n", iseContext.input_hint, ime_autofill_get_hint());

    // show autofill data
    show_autofill_data();

    //g_ise_common->set_keyboard_ise_by_uuid(KEYBD_ISE_UUID);

    /* Don't update screen until all the information is correctly set */
    CSCLUI *ui = get_ui();
    if (ui)
        ui->set_update_pending(TRUE);

    ise_reset_context(); // reset ISE

    KEYBOARD_STATE *keyboard_state = get_keyboard_state();

    if (iseContext.language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
        LOGD("prefer latin");
        if (keyboard_state)
            keyboard_state->prefer_latin = TRUE;
    }
    else {
        LOGD("prefer automatic");
        if (keyboard_state)
            keyboard_state->prefer_latin = FALSE;
    }

    _context_layout = iseContext.layout;
    _context_layout_variation = iseContext.layout_variation;
    ise_set_layout(iseContext.layout, iseContext.layout_variation);

    ise_set_return_key_type(iseContext.return_key_type);
    ise_set_return_key_disable(iseContext.return_key_disabled);

    ise_set_caps_mode(iseContext.caps_mode);
    ise_update_cursor_position(iseContext.cursor_pos);

    ise_show(ic);

    /* Now we update the whole screen */
    if (ui)
        ui->set_update_pending(FALSE);
}

static void ime_app_hide_cb(int ic, void *user_data)
{
    LOGD("Enter\n");
    ise_hide();

    g_ic_smartreply = -1;
}

static void ime_app_return_key_type_set_cb(Ecore_IMF_Input_Panel_Return_Key_Type type, void *user_data)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("return key type : %d\n", type);
    ise_set_return_key_type(type);

    if (keyboard_state->visible_state)
        ise_show(keyboard_state->ic);
}

static void ime_app_return_key_state_set_cb(bool disabled, void *user_data)
{
    LOGD("return key disabled : %d\n", disabled);
    ise_set_return_key_disable(disabled);
}

static void ime_app_language_set_cb(Ecore_IMF_Input_Panel_Lang language, void *user_data)
{
    LOGD("language : %d\n", language);

    // if (language == ECORE_IMF_INPUT_PANEL_LANG_ALPHABET) {
    //     ise_explictly_set_language(PRIMARY_LATIN_LANGUAGE_INDEX);
    // }
}

static void ime_app_input_context_reset_cb(void *user_data)
{
    ise_reset_input_context();
    engine_loader_reset_input_context();
}

static void ime_app_cursor_position_updated_cb(int cursor_pos, void *user_data)
{
    LOGD("cursor position : %d\n", cursor_pos);
    ise_update_cursor_position(cursor_pos);
    engine_loader_update_cursor_position(cursor_pos);
}

static void ime_app_language_requested_cb(void *user_data, char **lang_code)
{
    ise_get_language_locale(lang_code);
}

static void ime_app_surrounding_text_updated_cb(int context_id, const char *text, int cursor_pos, void *user_data)
{
    SECURE_LOGD("surrounding text:%s, cursor=%d\n", text, cursor_pos);
    ime_delete_surrounding_text(-cursor_pos, strlen(text));
}

static void ime_app_focus_in_cb(int context_id, void *user_data)
{
    LOGD("Enter\n");
    ise_focus_in(context_id);
    engine_loader_focus_in();
}

static void ime_app_focus_out_cb(int context_id, void *user_data)
{
    LOGD("Enter\n");
    ise_focus_out(context_id);
    g_imdata_state = 0;
    input_smartreply_deinit();
    g_ic_smartreply = -1;

    ime_autofill_set_exist(false);
    ime_autofill_set_hint(0);
    ime_autofill_set_app_id("");
    ime_autofill_set_resource_id("");

    g_smartreply_strings.clear();
    g_lookup_table_strings.clear();
    g_softcandidate_string.clear();

    engine_loader_focus_out();
}

static void ime_app_layout_set_cb(Ecore_IMF_Input_Panel_Layout layout, void *user_data)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    LOGD("layout=%d\n", layout);
    /* Check if the layoutIdx is in the valid range */
    if (static_cast<int>(layout) < static_cast<int>(ISE_LAYOUT_STYLE_MAX)) {
        if (keyboard_state->layout != layout) {
            keyboard_state->need_reset = TRUE;
        }
        keyboard_state->layout = layout;
        _context_layout = layout;
        _context_layout_variation = 0;
    }
    if (keyboard_state->visible_state)
        ise_show(keyboard_state->ic);

    engine_loader_set_layout(static_cast<uint32_t>(layout));
}

static void ime_app_input_hint_set_cb(Ecore_IMF_Input_Hints input_hint, void *user_data)
{
    LOGD("input hint=%u\n", input_hint);
}

static void ime_app_rotation_degree_changed_cb(int degree, void *user_data)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;

    CSCLUI *ui = get_ui();
    ise_set_screen_rotation(degree);

    LOGD("degree=%d\n", degree);
#if defined(_MOBILE) || defined(_COMMON)
    if (ui && g_floating_mode) {
        int handler_width;
        ise_destroy_move_handler();
        if (degree == 0 || degree == 180) {
            SclSize size_portrait = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_PORTRAIT);
            handler_width = size_portrait.width * FLOATING_SCALE_RATE;
        } else {
            SclSize size_landscape = ui->get_input_mode_size(ui->get_input_mode(), DISPLAYMODE_LANDSCAPE);
            handler_width = size_landscape.width * FLOATING_SCALE_RATE;
        }
        ise_show_move_handler(handler_width, FLOATING_TITLE_BAR_HEIGHT);
    }
#endif
    if (ise_emoticon_is_show()) {
        ise_emoticon_destroy_layout();
    }
    if (keyboard_state->layout == ISE_LAYOUT_STYLE_VOICE) {
        ise_hide_stt_mode();
    }
    if (keyboard_state->layout == ISE_LAYOUT_STYLE_EMOTICON) {
        ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
    } else if (ui) {
        const sclchar *input_mode = ui->get_input_mode();
        if (input_mode) {
            if (!(strcmp(input_mode, "EMOTICON_LAYOUT")))
                ise_emoticon_show_layout(ise_emoticon_get_current_group(), degree, false, ime_get_main_window());
            else if (!(strcmp(input_mode, "STT_3X4")))
                ise_show_stt_mode(NATIVE_WINDOW_CAST(ime_get_main_window()));
        }
    }
}

static void ime_app_accessibility_state_changed_cb(bool state, void *user_data)
{
    LOGD("state=%d\n", state);
    ise_set_accessibility_state(state);
}

static void ime_app_imdata_set_cb(void *data, unsigned int data_length, void *user_data)
{
    LOGD("Enter\n");
    g_imdata_state = 0;
    size_t _len = data_length;
    set_ise_imdata((sclchar *)data, _len);
    engine_loader_set_imdata((const char*)data, (uint32_t)data_length);
}

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)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return FALSE;

    scim::KeyEvent key(keycode, keymask);
    unsigned int ret;
    char *dev_name = NULL;
    Ecore_IMF_Device_Class dev_class;
    Ecore_IMF_Device_Subclass dev_subclass;

    if (ime_device_info_get_name(dev_info, &dev_name) == IME_ERROR_NONE) {
        key.dev_name = dev_name;
    }

    if (ime_device_info_get_class(dev_info, &dev_class) == IME_ERROR_NONE) {
        key.dev_class = dev_class;
    }

    if (ime_device_info_get_subclass(dev_info, &dev_subclass) == IME_ERROR_NONE) {
        key.dev_subclass = dev_subclass;
    }

    if (keyboard_state->visible_state) {
        ise_process_key_event(key, ret);
    } else {
        if (key.code == IME_KEY_space && (key.mask & IME_KEY_MASK_SHIFT)) {
            if (key.mask & IME_KEY_MASK_RELEASED) {
                if (_language_manager.select_next_language()) {
                    LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
                    if (info) {
                        notification_status_message_post(info->display_name.c_str());
                    }
                } else {
                    LOGE("Failed to change language");
                }
            }

            ret = TRUE;
        } else {
            ret = FALSE;
        }
    }

    if (dev_name)
        free(dev_name);

    return ret;
}

static void ime_app_process_key_event_with_imengine_cb(scim::KeyEvent &key, uint32_t serial, void *user_data)
{
    LANGUAGE_INFO *info = _language_manager.get_language_info(_language_manager.get_current_language());
    if (info && info->need_surrounding_text)
        engine_loader_process_key_event(key, serial, true);
    else
        engine_loader_process_key_event(key, serial, false);
}

static void ime_app_caps_mode_changed_cb(int mode, void *user_data)
{
    ise_set_caps_mode(mode);
}

static void ime_app_candidate_show_cb(int context_id, void *user_data)
{
#ifdef _WEARABLE
    ise_check_wearable_candidate();
#else
    ise_app_candidate_show();
#endif
}

static void ime_app_candidate_hide_cb(int context_id, void *user_data)
{
#ifdef _WEARABLE
    ise_check_wearable_candidate();
#else
    ise_app_candidate_hide();
#endif
}

static void ime_app_lookup_table_changed_cb(Eina_List *list, void *user_data)
{
    vector<string> candidate_strings;
    char *candidate;
    void *data;
    Eina_List *l;

    g_lookup_table_strings.clear();

    if (list) {
        EINA_LIST_FOREACH(list, l, data) {
            candidate = (char *)data;
            if (candidate) {
                g_lookup_table_strings.push_back(string(candidate));
                candidate_strings.push_back(string(candidate));
            }
        }
    }

    if (input_smartreply_get_reply_num() > 0) {
        if (candidate_strings[0] == "#" && candidate_strings[1] == "$") {
            char *text = NULL;
            int cursor;
            ime_get_surrounding_text(0, 0, &text, &cursor);
            if (text)
                free(text);

            if (cursor == 0)
                return;
        }
    }

    update_candidate_table();
}

#ifdef _WEARABLE
static sclu32 get_rotate_layout(ime_input_device_rotary_direction_e direction)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return ISE_LAYOUT_STYLE_NORMAL;

    sclu32 new_layout = keyboard_state->layout;

    if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE) {
        LOGD("CLOCKWISE\n");
        switch (keyboard_state->layout) {
            case ISE_LAYOUT_STYLE_NORMAL:
            case ISE_LAYOUT_STYLE_EMAIL:
            case ISE_LAYOUT_STYLE_URL:
            case ISE_LAYOUT_STYLE_PASSWORD:
                if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
                        _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
                    ;   // PASSWORD NUMBER ONLY, do nothing
                else
                    new_layout = ISE_LAYOUT_STYLE_NUMBER;
                break;
            case ISE_LAYOUT_STYLE_NUMBER:
                new_layout = ISE_LAYOUT_STYLE_HEX;
                break;
            case ISE_LAYOUT_STYLE_HEX:
                if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
                        _context_layout == ISE_LAYOUT_STYLE_URL)
                    new_layout = ISE_LAYOUT_STYLE_VOICE;
                else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
                    new_layout = _context_layout;
                else
                    new_layout = ISE_LAYOUT_STYLE_EMOTICON;
                break;
            case ISE_LAYOUT_STYLE_EMOTICON:
                if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
                        _context_layout == ISE_LAYOUT_STYLE_URL ||
                        _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
                    new_layout = _context_layout;
                else
                    new_layout = ISE_LAYOUT_STYLE_VOICE;
                break;
            case ISE_LAYOUT_STYLE_VOICE:
                new_layout = ISE_LAYOUT_STYLE_NORMAL;
                break;
            default:
                ;
        }
    } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE) {
        LOGD("COUNTER_CLOCKWISE\n");
        switch (keyboard_state->layout) {
            case ISE_LAYOUT_STYLE_NORMAL:
            case ISE_LAYOUT_STYLE_EMAIL:
            case ISE_LAYOUT_STYLE_URL:
            case ISE_LAYOUT_STYLE_PASSWORD:
                if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD &&
                        _context_layout_variation == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY)
                    ;   // PASSWORD NUMBER ONLY, do nothing
                else if (_context_layout == ISE_LAYOUT_STYLE_PASSWORD)
                    new_layout = ISE_LAYOUT_STYLE_HEX;
                else
                    new_layout = ISE_LAYOUT_STYLE_VOICE;
                break;
            case ISE_LAYOUT_STYLE_NUMBER:
                if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
                        _context_layout == ISE_LAYOUT_STYLE_URL ||
                        _context_layout == ISE_LAYOUT_STYLE_PASSWORD)
                    new_layout = _context_layout;
                else
                    new_layout = ISE_LAYOUT_STYLE_NORMAL;
                break;
            case ISE_LAYOUT_STYLE_HEX:
                new_layout = ISE_LAYOUT_STYLE_NUMBER;
                break;
            case ISE_LAYOUT_STYLE_EMOTICON:
                new_layout = ISE_LAYOUT_STYLE_HEX;
                break;
            case ISE_LAYOUT_STYLE_VOICE:
                if (_context_layout == ISE_LAYOUT_STYLE_EMAIL ||
                        _context_layout == ISE_LAYOUT_STYLE_URL)
                    new_layout = ISE_LAYOUT_STYLE_HEX;
                else
                    new_layout = ISE_LAYOUT_STYLE_EMOTICON;
                break;
            default:
                ;
        }
    }

    return new_layout;
}

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)
{
    KEYBOARD_STATE *keyboard_state = get_keyboard_state();
    if (!keyboard_state) return;
    if (device_type != IME_INPUT_DEVICE_TYPE_ROTARY) return;

    ime_input_device_rotary_direction_e direction;
    if (IME_ERROR_NONE != ime_input_device_rotary_get_direction(device_event, &direction)) return;

    sclu32 new_layout = get_rotate_layout(direction);

    CONFIG_VALUES *config_values = get_config_values();
    if (check_is_tutorial_show() && config_values) {
        read_ise_config_values();
        if ((direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && !config_values->number_tutorial_enable && !config_values->symbol_tutorial_enable) ||
                (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && config_values->symbol_tutorial_enable)) {
            new_layout = keyboard_state->layout;
        } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_COUNTER_CLOCKWISE && config_values->symbol_tutorial_enable) {
            ise_destroy_tutorial_mode_popup();
            config_values->symbol_tutorial_enable = false;
            write_ise_config_values();
        } else if (direction == IME_INPUT_DEVICE_ROTARY_DIRECTION_CLOCKWISE && !config_values->number_tutorial_enable) {
            ise_destroy_tutorial_mode_popup();
            new_layout = ISE_LAYOUT_STYLE_NUMBER;
            if (!config_values->symbol_tutorial_enable) {
                ise_show_tutorial_mode_popup(new_layout);
                config_values->symbol_tutorial_enable = true;
                write_ise_config_values();
            }
        }
    }

    if (new_layout != keyboard_state->layout && new_layout < ISE_LAYOUT_STYLE_MAX) {
        keyboard_state->need_reset = TRUE;
        keyboard_state->layout = new_layout;

        if (keyboard_state->visible_state) {
            _reset_multitap_state();
            if (config_values) {
                _language_manager.reset_language(config_values->selected_language.c_str());
            }
            ise_show(keyboard_state->ic);
        }
    }
}
#endif

static void ime_app_prediction_hint_set_cb(const char *prediction_hint, void *user_data)
{
    char *sender = (char *)"mms";
    char *caller_id = (char *)"mms";
    char *hint = (char *)prediction_hint;

    if (!prediction_hint) return;
    SECURE_LOGD("prediction hint : %s\n", prediction_hint);

    if (strlen(prediction_hint) > 0) {
        input_smartreply_init(caller_id, sender, hint);
        input_smartreply_set_notify(_input_smartreply_notify_cb, NULL);

        if (input_smartreply_is_enabled()) {
            input_smartreply_get_reply_async();
        }
    }
}

static void ime_app_mime_type_set_request_cb(const char *mime_types, void *user_data)
{
    LOGD("mime type : %s\n", mime_types);
}

static void ime_app_prediction_hint_data_set_cb(const char *key, const char *value, void *user_data)
{
    SECURE_LOGD("key : %s, value : %s\n", key, value);

    if (string(key) == "appid")
        ime_autofill_set_app_id(value);
    else if (string(key) == "res_id")
        ime_autofill_set_resource_id(value);
}

static void ime_app_autocapital_type_set_cb(uint32_t type, void *user_data)
{
    LOGD("autocapital type : %u\n", type);
    engine_loader_set_autocapital_type(type);
}

static void ime_app_prediction_allow_set_cb(uint32_t prediction_allow, void *user_data)
{
    LOGD("prediction allow : %u\n", prediction_allow);
    engine_loader_set_prediction_allow(prediction_allow);
}

static void ime_app_trigger_property_set_cb(const char *property, void *user_data)
{
    LOGD("trigger property : %s\n", property);
    engine_loader_trigger_property(property);
}

static void ime_app_candidate_more_window_show_cb(void *user_data)
{
    LOGD("");
    engine_loader_show_candidate_more_window();
}

static void ime_app_candidate_more_window_hide_cb(void *user_data)
{
    LOGD("");
    engine_loader_hide_candidate_more_window();
}

static void ime_app_aux_select_cb(uint32_t item, void *user_data)
{
    LOGD("aux select : %u\n", item);
    engine_loader_select_aux(item);
}

static void ime_app_candidate_select_cb(uint32_t item, void *user_data)
{
    LOGD("candidate select : %u\n", item);
    engine_loader_select_candidate(item);
}

static void ime_app_candidate_table_page_up_cb(void *user_data)
{
    LOGD("");
    engine_loader_candidate_table_page_up();
}

static void ime_app_candidate_table_page_down_cb(void *user_data)
{
    LOGD("");
    engine_loader_candidate_table_page_down();
}

static void ime_app_candidate_table_page_size_chaned_cb(uint32_t size, void *user_data)
{
    LOGD("candidate page size : %u\n", size);
    engine_loader_change_candidate_page_size(size);
}

static void ime_app_candidate_item_layout_set_cb(vector<uint32_t> item, void *user_data)
{
    LOGD("item layout size : %zu", item.size());
    engine_loader_set_candidate_item_layout(item);
}

static void ime_app_displayed_candidate_number_chaned_cb(uint32_t page_num, void *user_data)
{
    LOGD("candidate number changed : %u\n", page_num);
    engine_loader_change_candidate_number(page_num);
}

static void ime_app_candidate_item_long_pressed_cb(uint32_t index, void *user_data)
{
    LOGD("candidate item : %u\n", index);
    engine_loader_long_press_candidate_item(index);
}

#ifdef __cplusplus
extern "C"{
#endif
EXPORTED void ime_app_main(int argc, char **argv)
{
    ime_callback_s basic_callback = {
        ime_app_create_cb,
        ime_app_exit_cb,
        ime_app_show_cb,
        ime_app_hide_cb
    };

    ime_event_set_focus_in_cb(ime_app_focus_in_cb, NULL);
    ime_event_set_focus_out_cb(ime_app_focus_out_cb, NULL);
    ime_event_set_rotation_degree_changed_cb(ime_app_rotation_degree_changed_cb, NULL);
    ime_event_set_accessibility_state_changed_cb(ime_app_accessibility_state_changed_cb, NULL);
    ime_event_set_layout_set_cb(ime_app_layout_set_cb, NULL);
    ime_event_set_caps_mode_changed_cb(ime_app_caps_mode_changed_cb, NULL);
    ime_event_set_cursor_position_updated_cb(ime_app_cursor_position_updated_cb, NULL);
    ime_event_set_surrounding_text_updated_cb(ime_app_surrounding_text_updated_cb, NULL);
    ime_event_set_return_key_type_set_cb(ime_app_return_key_type_set_cb, NULL);
    ime_event_set_return_key_state_set_cb(ime_app_return_key_state_set_cb, NULL);
    ime_event_set_language_set_cb(ime_app_language_set_cb, NULL);
    ime_event_set_imdata_set_cb(ime_app_imdata_set_cb, NULL);
    ime_event_set_process_key_event_cb(ime_app_process_key_event_cb, NULL);
    ime_event_set_process_key_event_with_imengine_cb(ime_app_process_key_event_with_imengine_cb, NULL);
    ime_event_set_input_hint_set_cb(ime_app_input_hint_set_cb, NULL);

    ime_event_set_candidate_show_cb(ime_app_candidate_show_cb, NULL);
    ime_event_set_candidate_hide_cb(ime_app_candidate_hide_cb, NULL);
    ime_event_set_lookup_table_changed_cb(ime_app_lookup_table_changed_cb, NULL);

#ifdef _WEARABLE
    ime_event_set_process_input_device_event_cb(ime_app_process_input_device_event_cb, NULL);
#endif

    ime_event_set_input_context_reset_cb(ime_app_input_context_reset_cb, NULL);
    ime_event_set_language_requested_cb(ime_app_language_requested_cb, NULL);

    ime_event_set_prediction_hint_set_cb(ime_app_prediction_hint_set_cb, NULL);
    ime_event_set_mime_type_set_request_cb(ime_app_mime_type_set_request_cb, NULL);
    ime_event_set_prediction_hint_data_set_cb(ime_app_prediction_hint_data_set_cb, NULL);

    ime_event_set_autocapital_type_set_cb(ime_app_autocapital_type_set_cb, NULL);
    ime_event_set_prediction_allow_set_cb(ime_app_prediction_allow_set_cb, NULL);
    ime_event_set_trigger_property_set_cb(ime_app_trigger_property_set_cb, NULL);
    ime_event_set_candidate_more_window_show_cb(ime_app_candidate_more_window_show_cb, NULL);
    ime_event_set_candidate_more_window_hide_cb(ime_app_candidate_more_window_hide_cb, NULL);
    ime_event_set_aux_select_cb(ime_app_aux_select_cb, NULL);
    ime_event_set_candidate_select_cb(ime_app_candidate_select_cb, NULL);
    ime_event_set_candidate_table_page_up_cb(ime_app_candidate_table_page_up_cb, NULL);
    ime_event_set_candidate_table_page_down_cb(ime_app_candidate_table_page_down_cb, NULL);
    ime_event_set_candidate_table_page_size_chaned_cb(ime_app_candidate_table_page_size_chaned_cb, NULL);
    ime_event_set_candidate_item_layout_set_cb(ime_app_candidate_item_layout_set_cb, NULL);
    ime_event_set_displayed_candidate_number_chaned_cb(ime_app_displayed_candidate_number_chaned_cb, NULL);
    ime_event_set_candidate_item_long_pressed_cb(ime_app_candidate_item_long_pressed_cb, NULL);

#if DEFER_ISE_CREATION
    ime_set_window_creation_defer_flag(TRUE);
#endif

    ime_run(&basic_callback, NULL);
}
#ifdef __cplusplus
}
#endif

EXPORTED int main(int argc, char *argv[])
{
    ime_app_main(argc, argv);

    return 0;
}