summaryrefslogtreecommitdiff
path: root/ism/modules/panelagent/wayland/wayland_panel_agent_module.cpp
blob: cb2d3aa107a9f8a7d861ae007944adfd8c1d5882 (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
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
/*
 * ISF(Input Service Framework)
 *
 * ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable.
 * Copyright (c) 2013 Intel Corporation
 * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd.
 *
 * Contact: Jihoon Kim <jihoon48.kim@samsung.com>, Li Zhang <li2012.zhang@samsung.com>
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#define Uses_SCIM_PANEL_CLIENT
#define Uses_SCIM_CONFIG_PATH
#define Uses_SCIM_PANEL_AGENT

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/times.h>
#include <pthread.h>
#include <langinfo.h>
#include <unistd.h>
#include <fcntl.h>

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <dlog.h>
#include <glib.h>
#include <limits.h>

#include <Eina.h>
#include <Ecore.h>
#define EFL_BETA_API_SUPPORT
#include <Ecore_Wl2.h>
#include <vconf.h>
#include <vconf-keys.h>
#include <input-method-client-protocol.h>
#include <text-client-protocol.h>

#include "scim_private.h"
#include "scim.h"
#include "isf_wsc_context.h"
#include "isf_wsc_control_ui.h"
#include "tizen_profile.h"

#include <linux/input.h>

#if ENABLE_GRAB_KEYBOARD
#include <xkbcommon/xkbcommon.h>
#endif

#include <sys/mman.h>
#include "isf_debug.h"


#ifdef LOG_TAG
# undef LOG_TAG
#endif
#define LOG_TAG                                         "ISF_WAYLAND_MODULE"

using namespace scim;

struct _WSCContextISFImpl {
    WSCContextISF           *parent;
    Ecore_Wl2_Window        *client_window;
    Ecore_IMF_Input_Mode     input_mode;
    WideString               surrounding_text;
    WideString               preedit_string;
    AttributeList            preedit_attrlist;
    WideString               commit_string;
    Ecore_IMF_Autocapital_Type autocapital_type;
    Ecore_IMF_Input_Hints    input_hint;
    Ecore_IMF_BiDi_Direction bidi_direction;
    Ecore_IMF_Input_Panel_Layout panel_layout;
    Ecore_IMF_Input_Panel_Return_Key_Type return_key_type;
    String                   mime_type;
    void                    *imdata;
    int                      imdata_size;
    int                      preedit_caret;
    int                      cursor_x;
    int                      cursor_y;
    int                      cursor_top_y;
    int                      cursor_pos;
    int                      variation;
    int                      return_key_disabled;
    bool                     use_preedit;
    bool                     is_on;
    bool                     preedit_started;
    bool                     need_commit_preedit;
    bool                     init_remote_entry_metadata;
    bool                     init_remote_surrounding_text;
    bool                     block_input_resource;
    Input_Resource           input_resource;

    WSCContextISFImpl        *next;

    /* Constructor */
    _WSCContextISFImpl() : parent(NULL),
                           client_window(0),
                           input_mode(ECORE_IMF_INPUT_MODE_FULL),
                           autocapital_type(ECORE_IMF_AUTOCAPITAL_TYPE_SENTENCE),
                           input_hint(ECORE_IMF_INPUT_HINT_NONE),
                           bidi_direction(ECORE_IMF_BIDI_DIRECTION_NEUTRAL),
                           panel_layout(ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL),
                           return_key_type(ECORE_IMF_INPUT_PANEL_RETURN_KEY_TYPE_DEFAULT),
                           imdata(NULL),
                           imdata_size(0),
                           preedit_caret(0),
                           cursor_x(0),
                           cursor_y(0),
                           cursor_top_y(0),
                           cursor_pos(-1),
                           variation(0),
                           return_key_disabled(0),
                           use_preedit(true),
                           is_on(true),
                           preedit_started(false),
                           need_commit_preedit(false),
                           init_remote_entry_metadata(true),
                           init_remote_surrounding_text(true),
                           block_input_resource(false),
                           input_resource(INPUT_RESOURCE_NONE),
                           next(NULL)
    {
    }
};

/* private functions */

static void     panel_slot_update_preedit_caret         (int                     context,
                                                         int                     caret);
static void     panel_slot_process_key_event            (int                     context,
                                                         const KeyEvent         &key);
static void     panel_slot_commit_string                (int                     context,
                                                         const WideString       &wstr,
                                                         bool                    remote_mode);
static void     panel_slot_forward_key_event            (int                     context,
                                                         const KeyEvent         &key,
                                                         bool                    remote_mode);
static void     panel_slot_update_preedit_string        (int                     context,
                                                         const WideString        str,
                                                         const WideString        commit,
                                                         const AttributeList    &attrs,
                                                         int                     caret,
                                                         bool                    remote_mode);
static void     _show_preedit_string          (int                     context);

static void     panel_req_update_bidi_direction         (WSCContextISF     *ic, int direction);

static void     remote_surrounding_get                  (WSCContextISF     *wsc_ctx);

static void     wl_im_destroy                           (void);

/* Panel iochannel handler*/
static void     panel_initialize                        (void);
static void     panel_finalize                          (void);

/* utility functions */

static bool     filter_keys                             (const char *keyname,
                                                         const char *config_path);
static void     set_ic_capabilities                     (WSCContextISF     *ic);

static void     initialize                              (void);
static void     finalize                                (void);

static void     send_wl_key_event                       (WSCContextISF *ic, const KeyEvent &key, bool fake);
static void     _hide_preedit_string                    (int context, bool update_preedit);

/* Local variables declaration */
static String                                           _language;
static WSCContextISFImpl                               *_used_ic_impl_list          = 0;
static WSCContextISFImpl                               *_free_ic_impl_list          = 0;
static WSCContextISF                                   *_ic_list                    = 0;

static KeyboardLayout                                   _keyboard_layout            = SCIM_KEYBOARD_Default;
static int                                              _valid_key_mask             = SCIM_KEY_AllMasks;

static ConfigPointer                                    _config;

static WSCContextISF                                   *_focused_ic                 = 0;

static bool                                             _scim_initialized           = false;

static int                                              _panel_client_id            = 0;
static uint32                                           _active_helper_option       = 0;

static bool                                             _on_the_spot                = true;
static bool                                             _change_keyboard_mode_by_focus_move = false;
static bool                                             _support_hw_keyboard_mode   = false;

static bool                                             _x_key_event_is_valid       = false;

static Ecore_Timer                                     *_resource_check_timer       = NULL;

static bool                                             _need_wl_im_init           = false;
static struct _wl_im                                    *_wl_im_ctx                = NULL;
static int                                              _ecore_wl2_init_count      = 0;

static bool                                             _launch_ise_on_request     = false;

#define WAYLAND_MODULE_CLIENT_ID (0)
#define MAX_PREEDIT_BUFSIZE 4000

#define MOD_SHIFT_MASK      0x01
#define MOD_CAPS_MASK       0x02
#define MOD_CONTROL_MASK    0x04
#define MOD_ALT_MASK        0x08
#define MOD_NUM_MASK        0x100
#define MOD_Mod5_MASK       0x80

#define WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_MASK 0xff0000

//////////////////////////////wayland_panel_agent_module begin//////////////////////////////////////////////////

#define scim_module_init wayland_LTX_scim_module_init
#define scim_module_exit wayland_LTX_scim_module_exit
#define scim_panel_agent_module_init wayland_LTX_scim_panel_agent_module_init
#define scim_panel_agent_module_get_instance wayland_LTX_scim_panel_agent_module_get_instance

static struct weescim _wsc                                  = {0};

InfoManager* g_info_manager = NULL;
static scim::PanelAgentPointer instance;


/////////////////////////////////////////////////////////////////////////////
// Implementation of Wayland Input Method functions.
/////////////////////////////////////////////////////////////////////////////

static void
_wsc_im_ctx_reset (void *data, struct wl_input_method_context *im_ctx)
{
    WSCContextISF *context_scim = (WSCContextISF*)data;
    LOGD ("");
    if (context_scim && context_scim->impl && context_scim == _focused_ic) {
        g_info_manager->socket_reset_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id);
    }
}

static void
_wsc_im_ctx_content_type (void *data, struct wl_input_method_context *im_ctx, uint32_t hint, uint32_t purpose)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

    LOGD ("im_context = %p hint = %04x purpose = %d", im_ctx, hint, purpose);

    // Set layout
    if (wsc_ctx->content_purpose != purpose || !wsc_ctx->layout_initialized) {
        wsc_ctx->layout_initialized = EINA_TRUE;
        wsc_ctx->content_purpose = purpose;
        isf_wsc_context_input_panel_layout_set (wsc_ctx, wsc_context_input_panel_layout_get (wsc_ctx));
    }

    if (wsc_ctx->content_hint != hint) {
        uint32_t hint_copy, old_hintbit, new_hintbit;
        const uint32_t autocap_type = WL_TEXT_INPUT_CONTENT_HINT_AUTO_CAPITALIZATION |
                                      WL_TEXT_INPUT_CONTENT_HINT_UPPERCASE |
                                      WL_TEXT_INPUT_CONTENT_HINT_WORD_CAPITALIZATION;

        hint_copy = wsc_ctx->content_hint;
        wsc_ctx->content_hint = hint;

        // Set prediction allow
        old_hintbit = hint_copy & WL_TEXT_INPUT_CONTENT_HINT_AUTO_COMPLETION;
        new_hintbit = hint & WL_TEXT_INPUT_CONTENT_HINT_AUTO_COMPLETION;
        if (old_hintbit != new_hintbit || !wsc_ctx->prediction_allow_initialized) {
            wsc_ctx->prediction_allow_initialized = EINA_TRUE;
            g_info_manager->set_prediction_allow (WAYLAND_MODULE_CLIENT_ID, wsc_context_prediction_allow_get (wsc_ctx));
        }

        // Set autocapital type
        old_hintbit = hint_copy & autocap_type;
        new_hintbit = hint & autocap_type;
        if (old_hintbit != new_hintbit || !wsc_ctx->autocapital_type_initialized) {
            wsc_ctx->autocapital_type_initialized = EINA_TRUE;
            isf_wsc_context_autocapital_type_set (wsc_ctx, wsc_context_autocapital_type_get (wsc_ctx));
        }

        // Set language
        old_hintbit = hint_copy & WL_TEXT_INPUT_CONTENT_HINT_LATIN;
        new_hintbit = hint & WL_TEXT_INPUT_CONTENT_HINT_LATIN;
        if (old_hintbit != new_hintbit || !wsc_ctx->language_initialized) {
            wsc_ctx->language_initialized = EINA_TRUE;
            isf_wsc_context_input_panel_language_set (wsc_ctx, wsc_context_input_panel_language_get (wsc_ctx));
        }

        isf_wsc_context_input_hint_set (wsc_ctx, wsc_context_input_hint_get (wsc_ctx));
    }

    if (_TV) {
        isf_wsc_context_send_entry_metadata (wsc_ctx, wsc_context_input_hint_get (wsc_ctx), wsc_context_input_panel_layout_get (wsc_ctx),
                wsc_context_input_panel_layout_variation_get (wsc_ctx), wsc_context_autocapital_type_get (wsc_ctx), wsc_ctx->return_key_disabled,
                (Ecore_IMF_Input_Panel_Return_Key_Type)wsc_ctx->return_key_type);
    }
}

static void
_wsc_im_ctx_invoke_action (void *data, struct wl_input_method_context *im_ctx, uint32_t button, uint32_t index)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

    LOGD ("invoke action. button : %d", button);

    if (button != BTN_LEFT)
        return;

    wsc_context_send_preedit_string (wsc_ctx);
}

static void
_wsc_im_ctx_commit_state (void *data, struct wl_input_method_context *im_ctx, uint32_t serial)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

    wsc_ctx->serial = serial;

    if (wsc_ctx->language)
        wl_input_method_context_language (im_ctx, wsc_ctx->serial, wsc_ctx->language);
}

static void
_wsc_im_ctx_preferred_language (void *data, struct wl_input_method_context *im_ctx, const char *language)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

    if (language && wsc_ctx->language && !strcmp (language, wsc_ctx->language))
        return;

    if (wsc_ctx->language) {
        free (wsc_ctx->language);
        wsc_ctx->language = NULL;
    }

    if (language) {
        wsc_ctx->language = strdup (language);
        LOGD ("Language changed, new: '%s'", language);
    }
}

static void
_wsc_im_ctx_return_key_type (void *data, struct wl_input_method_context *im_ctx, uint32_t return_key_type)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD ("im_context = %p return key type = %d", im_ctx, return_key_type);
    if (!wsc_ctx) return;

    if (wsc_ctx->return_key_type != return_key_type) {
        wsc_ctx->return_key_type = return_key_type;
        isf_wsc_context_input_panel_return_key_type_set (wsc_ctx, (Ecore_IMF_Input_Panel_Return_Key_Type)wsc_ctx->return_key_type);
        if (_TV)
            isf_wsc_context_send_entry_metadata (wsc_ctx, wsc_context_input_hint_get (wsc_ctx), wsc_context_input_panel_layout_get (wsc_ctx),
                    wsc_context_input_panel_layout_variation_get (wsc_ctx), wsc_context_autocapital_type_get (wsc_ctx), wsc_ctx->return_key_disabled,
                    (Ecore_IMF_Input_Panel_Return_Key_Type)wsc_ctx->return_key_type);
    }
}

static void
_wsc_im_ctx_return_key_disabled (void *data, struct wl_input_method_context *im_ctx, uint32_t disabled)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    Eina_Bool return_key_disabled = !!disabled;

    LOGD ("im_context = %p return key disabled = %d", im_ctx, return_key_disabled);
    if (!wsc_ctx) return;

    if (wsc_ctx->return_key_disabled != return_key_disabled) {
        wsc_ctx->return_key_disabled = return_key_disabled;
        isf_wsc_context_input_panel_return_key_disabled_set (wsc_ctx, wsc_ctx->return_key_disabled);
        if (_TV)
            isf_wsc_context_send_entry_metadata (wsc_ctx, wsc_context_input_hint_get (wsc_ctx), wsc_context_input_panel_layout_get (wsc_ctx),
                    wsc_context_input_panel_layout_variation_get (wsc_ctx), wsc_context_autocapital_type_get (wsc_ctx), wsc_ctx->return_key_disabled,
                    (Ecore_IMF_Input_Panel_Return_Key_Type)wsc_ctx->return_key_type);
    }
}

static void
_wsc_im_ctx_input_panel_data (void *data, struct wl_input_method_context *im_ctx, const char *input_panel_data, uint32_t input_panel_data_length)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    LOGD ("im_context = %p input panel data = %s len = %d", im_ctx, input_panel_data, input_panel_data_length);
    if (!wsc_ctx) return;

    if (wsc_ctx->impl) {
        if (wsc_ctx->impl->imdata) {
            free(wsc_ctx->impl->imdata);
        }

        wsc_ctx->impl->imdata = calloc(1, input_panel_data_length);
        if (wsc_ctx->impl->imdata)
            memcpy(wsc_ctx->impl->imdata, input_panel_data, input_panel_data_length);

        wsc_ctx->impl->imdata_size = input_panel_data_length;
    }

    isf_wsc_context_input_panel_imdata_set (wsc_ctx, (void *)input_panel_data, input_panel_data_length);
}

static void
_wsc_im_ctx_bidi_direction (void *data, struct wl_input_method_context *im_ctx, uint32_t bidi_direction)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD ("im_context = %p bidi_direction = %d", im_ctx, bidi_direction);
    if (!wsc_ctx) return;

    if (wsc_ctx->bidi_direction != bidi_direction) {
        wsc_ctx->bidi_direction = bidi_direction;

        isf_wsc_context_bidi_direction_set (wsc_ctx, (Ecore_IMF_BiDi_Direction)wsc_ctx->bidi_direction);
    }
}

static void
_wsc_im_ctx_cursor_position (void *data, struct wl_input_method_context *im_ctx, uint32_t cursor_pos)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD ("im_context = %p cursor_pos = %d", im_ctx, cursor_pos);
    if (!wsc_ctx || !wsc_ctx->impl) return;
    wsc_ctx->impl->cursor_pos = cursor_pos;
    wsc_ctx->surrounding_cursor = cursor_pos;
    LOGD ("wsc_ctx->surrounding_cursor = %d", wsc_ctx->surrounding_cursor);
    g_info_manager->socket_update_cursor_position (cursor_pos);

    if (_TV)
        remote_surrounding_get (wsc_ctx);
}

static void
_wsc_im_ctx_process_input_device_event (void *data, struct wl_input_method_context *im_ctx, uint32_t type, const char *input_data, uint32_t input_data_len)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD("im_context = %p type = %d, data = (%p) %d", im_ctx, type, input_data, input_data_len);
    if (!wsc_ctx) return;

    isf_wsc_context_process_input_device_event (wsc_ctx, type, input_data, input_data_len);
}

static void
_wsc_im_ctx_filter_key_event (void *data, struct wl_input_method_context *im_ctx, uint32_t serial, uint32_t time, const char *keyname, uint32_t state, uint32_t modifiers, const char *dev_name, uint32_t dev_class, uint32_t dev_subclass, uint32_t keycode)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

#if !(ENABLE_GRAB_KEYBOARD)
    isf_wsc_context_filter_key_event (wsc_ctx, serial, time, keyname,
            ((wl_keyboard_key_state)state) == WL_KEYBOARD_KEY_STATE_PRESSED, modifiers, dev_name, dev_class, dev_subclass, keycode);
#endif
}

static void
_wsc_im_ctx_capital_mode (void *data, struct wl_input_method_context *im_ctx, uint32_t mode)
{
    LOGD ("capital mode %d", mode);
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;
    wsc_ctx->caps_mode = mode;
    isf_wsc_context_input_panel_caps_mode_set (wsc_ctx, mode);
}

static void
_wsc_im_ctx_prediction_hint (void *data, struct wl_input_method_context *im_ctx, const char *prediction_hint)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD ("im_context = %p, prediction hint = %s", im_ctx, prediction_hint);
    if (!wsc_ctx) return;

    isf_wsc_context_input_panel_prediction_hint_set (wsc_ctx, prediction_hint);
}

static void
_wsc_im_ctx_mime_type (void *data, struct wl_input_method_context *im_ctx, const char *mime_type)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    LOGD ("im_context = %p, mime_type = %s", im_ctx, mime_type);
    if (!wsc_ctx) return;

    if (wsc_ctx->impl) {
        wsc_ctx->impl->mime_type = String (mime_type);
    }

    isf_wsc_context_input_panel_mime_type_accept_set (wsc_ctx, mime_type);
}

static void
_wsc_im_ctx_finalized_content (void *data, struct wl_input_method_context *im_ctx, const char *text, uint32_t cursor_pos)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;

    SECURE_LOGD ("im_context = %p, text = %s, cursor_pos = %d", im_ctx, text, cursor_pos);
    if (!wsc_ctx) return;

    isf_wsc_context_input_panel_finalize_content (wsc_ctx, text, cursor_pos);
}

static void
_wsc_im_ctx_prediction_hint_data (void *data, struct wl_input_method_context *im_ctx, const char *key, const char * value)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx) return;

    isf_wsc_context_input_panel_prediction_hint_data_set (wsc_ctx, key, value);
}

static const struct wl_input_method_context_listener wsc_im_context_listener = {
     _wsc_im_ctx_reset,
     _wsc_im_ctx_content_type,
     _wsc_im_ctx_invoke_action,
     _wsc_im_ctx_commit_state,
     _wsc_im_ctx_preferred_language,
     _wsc_im_ctx_return_key_type,
     _wsc_im_ctx_return_key_disabled,
     _wsc_im_ctx_input_panel_data,
     _wsc_im_ctx_bidi_direction,
     _wsc_im_ctx_cursor_position,
     _wsc_im_ctx_process_input_device_event,
     _wsc_im_ctx_filter_key_event,
     _wsc_im_ctx_capital_mode,
     _wsc_im_ctx_prediction_hint,
     _wsc_im_ctx_mime_type,
     _wsc_im_ctx_finalized_content,
     _wsc_im_ctx_prediction_hint_data
};

#if ENABLE_GRAB_KEYBOARD
static void
_init_keysym2keycode (WSCContextISF *wsc_ctx)
{
    uint32_t i = 0;
    uint32_t code;
    uint32_t num_syms;
    const xkb_keysym_t *syms;

    if (!wsc_ctx || !wsc_ctx->state)
        return;

    for (i = 0; i < 256; i++) {
        code = i + 8;
        num_syms = xkb_key_get_syms (wsc_ctx->state, code, &syms);

        if (num_syms == 1)
            wsc_ctx->_keysym2keycode[syms[0]] = i;
    }
}

static void
_fini_keysym2keycode (WSCContextISF *wsc_ctx)
{
    wsc_ctx->_keysym2keycode.clear ();
}

static void
_wsc_im_keyboard_keymap (void *data,
        struct wl_keyboard *wl_keyboard,
        uint32_t format,
        int32_t fd,
        uint32_t size)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    char *map_str;

    if (!wsc_ctx) {
        close (fd);
        return;
    }

    _fini_keysym2keycode (wsc_ctx);

    if (wsc_ctx->state) {
        xkb_state_unref (wsc_ctx->state);
        wsc_ctx->state = NULL;
    }

    if (wsc_ctx->keymap) {
        xkb_map_unref (wsc_ctx->keymap);
        wsc_ctx->keymap = NULL;
    }

    if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
        close (fd);
        return;
    }

    map_str = (char*)mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    if (map_str == MAP_FAILED) {
        close (fd);
        return;
    }

    wsc_ctx->keymap =
        xkb_map_new_from_string (wsc_ctx->xkb_context,
                map_str,
                XKB_KEYMAP_FORMAT_TEXT_V1,
                (xkb_keymap_compile_flags)0);

    munmap (map_str, size);
    close (fd);

    if (!wsc_ctx->keymap) {
        LOGW ("failed to compile keymap");
        return;
    }

    wsc_ctx->state = xkb_state_new (wsc_ctx->keymap);
    if (!wsc_ctx->state) {
        LOGW ("failed to create XKB state");
        xkb_map_unref (wsc_ctx->keymap);
        return;
    }

    wsc_ctx->control_mask =
        1 << xkb_map_mod_get_index (wsc_ctx->keymap, "Control");
    wsc_ctx->alt_mask =
        1 << xkb_map_mod_get_index (wsc_ctx->keymap, "Mod1");
    wsc_ctx->shift_mask =
        1 << xkb_map_mod_get_index (wsc_ctx->keymap, "Shift");

    LOGD ("create _keysym2keycode");
    _init_keysym2keycode (wsc_ctx);
}

static void
_wsc_im_keyboard_key (void *data,
        struct wl_keyboard *wl_keyboard,
        uint32_t serial,
        uint32_t time,
        uint32_t key,
        uint32_t state_w)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    uint32_t code;
    uint32_t num_syms;
    const xkb_keysym_t *syms;
    xkb_keysym_t sym;
    char keyname[64] = {0};
    enum wl_keyboard_key_state state = (wl_keyboard_key_state)state_w;

    if (!wsc_ctx || !wsc_ctx->state)
        return;

    code = key + 8;
    num_syms = xkb_key_get_syms (wsc_ctx->state, code, &syms);

    sym = XKB_KEY_NoSymbol;
    if (num_syms == 1) {
        sym = syms[0];
        xkb_keysym_get_name (sym, keyname, 64);
    }

    isf_wsc_context_filter_key_event (wsc_ctx, serial, time, code, sym, keyname,
                state);
}

static void
_wsc_im_keyboard_modifiers (void *data,
        struct wl_keyboard *wl_keyboard,
        uint32_t serial,
        uint32_t mods_depressed,
        uint32_t mods_latched,
        uint32_t mods_locked,
        uint32_t group)
{
    WSCContextISF *wsc_ctx = (WSCContextISF*)data;
    if (!wsc_ctx || !wsc_ctx->state)
        return;

    struct wl_input_method_context *context = wsc_ctx->im_ctx;
    xkb_mod_mask_t mask;

    xkb_state_update_mask (wsc_ctx->state, mods_depressed,
            mods_latched, mods_locked, 0, 0, group);
    mask = xkb_state_serialize_mods (wsc_ctx->state,
            (xkb_state_component)(XKB_STATE_DEPRESSED | XKB_STATE_LATCHED));

    wsc_ctx->modifiers = 0;
    if (mask & wsc_ctx->control_mask)
        wsc_ctx->modifiers |= SCIM_KEY_ControlMask;
    if (mask & wsc_ctx->alt_mask)
        wsc_ctx->modifiers |= SCIM_KEY_AltMask;
    if (mask & wsc_ctx->shift_mask)
        wsc_ctx->modifiers |= SCIM_KEY_ShiftMask;

    wl_input_method_context_modifiers (context, serial,
            mods_depressed, mods_depressed,
            mods_latched, group);
}

static const struct wl_keyboard_listener wsc_im_keyboard_listener = {
    _wsc_im_keyboard_keymap,
    NULL, /* enter */
    NULL, /* leave */
    _wsc_im_keyboard_key,
    _wsc_im_keyboard_modifiers
};
#endif

static void
wl_im_destroy ()
{
    if (!_wl_im_ctx || !_wl_im_ctx->wsc || !_wl_im_ctx->wsc->wsc_ctx)
        return;

    WSCContextISF *wsc_ctx = _wl_im_ctx->wsc->wsc_ctx;

    if (_wl_im_ctx->need_focus_event)
        isf_wsc_context_focus_out (wsc_ctx);

#if ENABLE_GRAB_KEYBOARD
    if (wsc_ctx->keyboard) {
        wl_keyboard_destroy (wsc_ctx->keyboard);
        wsc_ctx->keyboard = NULL;
    }

    _fini_keysym2keycode (wsc_ctx);

    if (wsc_ctx->state) {
        xkb_state_unref (wsc_ctx->state);
        wsc_ctx->state = NULL;
    }

    if (wsc_ctx->keymap) {
        xkb_map_unref (wsc_ctx->keymap);
        wsc_ctx->keymap = NULL;
    }

    if (wsc_ctx->xkb_context) {
        xkb_context_unref (wsc_ctx->xkb_context);
        wsc_ctx->xkb_context = NULL;
    }
#endif

    if (wsc_ctx->im_ctx) {
        wl_input_method_context_destroy (wsc_ctx->im_ctx);
        wsc_ctx->im_ctx = NULL;
    }

    if (wsc_ctx->preedit_str) {
        free (wsc_ctx->preedit_str);
        wsc_ctx->preedit_str = NULL;
    }

    if (wsc_ctx->surrounding_text) {
        free (wsc_ctx->surrounding_text);
        wsc_ctx->surrounding_text = NULL;
    }

    if (wsc_ctx->remote_surrounding_text) {
        free (wsc_ctx->remote_surrounding_text);
        wsc_ctx->remote_surrounding_text = NULL;
    }

    if (wsc_ctx->language) {
        free (wsc_ctx->language);
        wsc_ctx->language = NULL;
    }

    wsc_ctx->layout_initialized = EINA_FALSE;
    wsc_ctx->prediction_allow_initialized = EINA_FALSE;
    wsc_ctx->autocapital_type_initialized = EINA_FALSE;
    wsc_ctx->language_initialized = EINA_FALSE;

    if (_resource_check_timer)
        ecore_timer_del (_resource_check_timer);
    _resource_check_timer = NULL;

    isf_wsc_context_del (wsc_ctx);
    delete wsc_ctx;
    _wl_im_ctx->wsc->wsc_ctx = NULL;
    _wl_im_ctx->input_method = NULL;
    _wl_im_ctx->im_ctx = NULL;
    _wl_im_ctx->need_focus_event = EINA_FALSE;
    _need_wl_im_init = false;
}

static void
_wsc_im_activate (void *data, struct wl_input_method *input_method, struct wl_input_method_context *im_ctx, uint32_t text_input_id, uint32_t focus_in_event)
{
    struct weescim *wsc = (weescim*)data;
    if (!wsc) return;

    WSCContextISF *wsc_ctx = new WSCContextISF;
    if (!wsc_ctx) {
        return;
    }

    if (_need_wl_im_init)
        wl_im_destroy ();

#if ENABLE_GRAB_KEYBOARD
    wsc_ctx->xkb_context = xkb_context_new ((xkb_context_flags)0);
    if (wsc_ctx->xkb_context == NULL) {
        LOGW ("Failed to create XKB context");
        delete wsc_ctx;
        return;
    }
    wsc_ctx->state = NULL;
    wsc_ctx->keymap = NULL;
    wsc_ctx->modifiers = 0;
#endif

    wsc_ctx->id = text_input_id;
    wsc->wsc_ctx = wsc_ctx;
    wsc_ctx->ctx = wsc;
    wsc_ctx->surrounding_text = NULL;
    wsc_ctx->remote_surrounding_text = NULL;
    wsc_ctx->surrounding_cursor = 0;
    LOGD ("wsc_ctx->surrounding_cursor = %d", wsc_ctx->surrounding_cursor);

    get_language (&wsc_ctx->language);

    wsc_ctx->preedit_str = strdup ("");
    wsc_ctx->content_hint = WL_TEXT_INPUT_CONTENT_HINT_NONE;
    wsc_ctx->content_purpose = WL_TEXT_INPUT_CONTENT_PURPOSE_NORMAL;

    wsc_ctx->im_ctx = im_ctx;
    wl_input_method_context_add_listener (im_ctx, &wsc_im_context_listener, wsc_ctx);

#if ENABLE_GRAB_KEYBOARD
    wsc_ctx->keyboard = wl_input_method_context_grab_keyboard (im_ctx);
    if (wsc_ctx->keyboard)
        wl_keyboard_add_listener (wsc_ctx->keyboard, &wsc_im_keyboard_listener, wsc_ctx);
#endif

    if (wsc_ctx->language)
        wl_input_method_context_language (im_ctx, wsc_ctx->serial, wsc_ctx->language);

    isf_wsc_context_add (wsc_ctx);

    if (focus_in_event) {
        isf_wsc_context_focus_in (wsc_ctx);
        _wl_im_ctx->need_focus_event = EINA_TRUE;
    }
}

static void
_wsc_im_deactivate (void *data, struct wl_input_method *input_method, struct wl_input_method_context *im_ctx, uint32_t focus_out_event)
{
    struct weescim *wsc = (weescim*)data;
    if (!wsc || !wsc->wsc_ctx) return;

    /* When the focus_in/input_panel_shutdown event is called,
     * it is not possible to know the information of wl_input_method to destroy */
    _wl_im_ctx->wsc = wsc;
    _wl_im_ctx->input_method = input_method;
    _wl_im_ctx->im_ctx = im_ctx;
    _need_wl_im_init = true;

    if (focus_out_event) {
        isf_wsc_context_focus_out (wsc->wsc_ctx);
        _wl_im_ctx->need_focus_event = EINA_FALSE;
    }

    if (_launch_ise_on_request)
        wl_im_destroy ();
}

static void
_wsc_im_show_input_panel (void *data, struct wl_input_method *input_method, struct wl_input_method_context *im_ctx)
{
    struct weescim *wsc = (weescim*)data;
    if (!wsc || !wsc->wsc_ctx) return;

    isf_wsc_context_input_panel_show (wsc->wsc_ctx);

    if (_TV)
        remote_surrounding_get (wsc->wsc_ctx);
}

static void
_wsc_im_hide_input_panel (void *data, struct wl_input_method *input_method, struct wl_input_method_context *im_ctx)
{
    struct weescim *wsc = (weescim*)data;
    if (!wsc || !wsc->wsc_ctx) return;

    isf_wsc_context_input_panel_hide (wsc->wsc_ctx);
}

static const struct wl_input_method_listener wsc_im_listener = {
    _wsc_im_activate,
    _wsc_im_deactivate,
    _wsc_im_show_input_panel,
    _wsc_im_hide_input_panel
};

static bool
_wsc_setup (struct weescim *wsc)
{
    Eina_Iterator *globals;
    struct wl_registry *registry;
    Ecore_Wl2_Global *global;

    if (!wsc) return false;

    Ecore_Wl2_Display *wl2_display = ecore_wl2_display_connect (NULL);
    if (!wl2_display) {
        LOGW ("failed to connect");
        return false;
    }

    if (!(registry = ecore_wl2_display_registry_get (wl2_display))) {
        LOGW ("failed to get wl_registry");
        return false;
    }

    if (!(globals = ecore_wl2_display_globals_get (wl2_display))) {
        LOGW ("failed to get wl_globals");
        return false;
    }

    EINA_ITERATOR_FOREACH(globals, global) {
        if (strcmp (global->interface, "wl_input_method") == 0)
            wsc->im = (wl_input_method*)wl_registry_bind (registry, global->id, &wl_input_method_interface, 1);
    }
    eina_iterator_free (globals);

    if (wsc->im == NULL) {
        LOGW ("Failed because wl_input_method is null");
        return false;
    }

    /* Input method listener */
    LOGD ("Adding wl_input_method listener");

    if (wsc->im)
        wl_input_method_add_listener (wsc->im, &wsc_im_listener, wsc);
    else {
        LOGW ("Couldn't get wayland input method interface");
        return false;
    }

    return true;
}


//////////////////////////////wayland_panel_agent_module end//////////////////////////////////////////////////

WSCContextISF *
get_focused_ic ()
{
    return _focused_ic;
}

int
get_panel_client_id (void)
{
    return _panel_client_id;
}

void
get_language (char **language)
{
    *language = strdup (_language.c_str ());
}

static unsigned int
get_time (void)
{
    unsigned int tint;
    struct timeval tv;
    struct timezone tz;           /* is not used since ages */
    gettimeofday (&tv, &tz);
    tint = (unsigned int)(tv.tv_sec * 1000);
    tint = tint / 1000 * 1000;
    tint = (unsigned int)(tint + tv.tv_usec / 1000);
    return tint;
}

/* Function Implementations */
static WSCContextISFImpl *
new_ic_impl (WSCContextISF *parent)
{
    WSCContextISFImpl *impl = NULL;

    if (_free_ic_impl_list != NULL) {
        impl = _free_ic_impl_list;
        _free_ic_impl_list = _free_ic_impl_list->next;
    } else {
        impl = new WSCContextISFImpl;
        if (impl == NULL)
            return NULL;
    }

    impl->next = _used_ic_impl_list;
    _used_ic_impl_list = impl;

    impl->parent = parent;

    return impl;
}

static void
delete_ic_impl (WSCContextISFImpl *impl)
{
    WSCContextISFImpl *rec = _used_ic_impl_list, *last = 0;

    for (; rec != 0; last = rec, rec = rec->next) {
        if (rec == impl) {
            if (last != 0)
                last->next = rec->next;
            else
                _used_ic_impl_list = rec->next;

            rec->next = _free_ic_impl_list;
            _free_ic_impl_list = rec;

            if (rec->imdata) {
                free (rec->imdata);
                rec->imdata = NULL;
            }

            rec->imdata_size = 0;
            rec->parent = 0;
            rec->client_window = 0;
            rec->mime_type = String ();
            rec->surrounding_text = WideString ();
            rec->preedit_string = WideString ();
            rec->preedit_attrlist.clear ();
            rec->commit_string = WideString ();
            rec->block_input_resource = true;
            return;
        }
    }
}

static void
delete_all_ic_impl (void)
{
    WSCContextISFImpl *it = _used_ic_impl_list;

    while (it != 0) {
        _used_ic_impl_list = it->next;
        delete it;
        it = _used_ic_impl_list;
    }

    it = _free_ic_impl_list;
    while (it != 0) {
        _free_ic_impl_list = it->next;
        delete it;
        it = _free_ic_impl_list;
    }
}

static WSCContextISF *
find_ic (int id)
{
    WSCContextISFImpl *rec = _used_ic_impl_list;

    while (rec != 0) {
        if (rec->parent && rec->parent->id == id)
            return rec->parent;
        rec = rec->next;
    }

    return 0;
}

static bool
check_valid_ic (WSCContextISF *ic)
{
    if (ic && ic->impl && ic->ctx)
        return true;
    else
        return false;
}

void context_scim_imdata_get (WSCContextISF *wsc_ctx, void *data, int *length, int max_len)
{
    WSCContextISF* context_scim = wsc_ctx;
    LOGD ("");
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";

    if (length)
        *length = 0;

    if (context_scim && context_scim->impl && context_scim->impl->imdata && data && length) {
        if (max_len > context_scim->impl->imdata_size)
            max_len = context_scim->impl->imdata_size;

        memcpy (data, context_scim->impl->imdata, max_len);

        *length = max_len;
    }
}

void context_scim_mime_type_get (WSCContextISF *wsc_ctx, char *mime_types, int max_len)
{
    WSCContextISF* context_scim = wsc_ctx;
    LOGD ("");
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";

    if (mime_types && max_len > 0) {
        /* Initialize mime_types to have an empty string */
        mime_types[0] = '\0';
        if (context_scim && context_scim->impl) {
            if (!(context_scim->impl->mime_type).empty ()) {
                int length = (context_scim->impl->mime_type).length ();
                if (max_len > length)
                    max_len = length;
                memcpy ((void*)mime_types, (context_scim->impl->mime_type).c_str (), max_len);
            }
        }
    }
}

static char *
insert_text (const char *text, uint32_t offset, const char *insert)
{
    uint32_t tlen = strlen (text), ilen = strlen (insert);
    char *new_text = (char*)malloc (tlen + ilen + 1);
    if (new_text) {
        if ((unsigned int) tlen < offset)
            offset = tlen;
        memcpy (new_text, text, offset);
        memcpy (new_text + offset, insert, ilen);
        memcpy (new_text + offset + ilen, text + offset, tlen - offset);
        new_text[tlen + ilen] = '\0';
    }

    return new_text;
}

static Eina_Bool
change_block_status_timer_cb (void *data)
{
    WSCContextISF* context_scim = static_cast<WSCContextISF*>(data);
    if (context_scim && context_scim->impl)
        context_scim->impl->block_input_resource = false;

    _resource_check_timer = NULL;

    return ECORE_CALLBACK_CANCEL;
}

static void
check_input_resource (WSCContextISF *wsc_ctx, Input_Resource input_res)
{
    WSCContextISF* context_scim = wsc_ctx;
    LOGD ("Input resource : %d", input_res);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";

    if (context_scim && context_scim->impl) {
        if (context_scim->impl->input_resource == input_res)
            return;

        if (_TV)
            if (context_scim->impl->input_resource != input_res && input_res != INPUT_RESOURCE_NONE)
                g_info_manager->remoteinput_callback_input_resource (input_res);

        if (context_scim->impl->input_resource == INPUT_RESOURCE_REMOTE && input_res == INPUT_RESOURCE_LOCAL) {
            if (context_scim->impl->need_commit_preedit) {
                WideString wstr = context_scim->impl->preedit_string;
                _hide_preedit_string (context_scim->id, false);
                wsc_context_commit_string (context_scim, utf8_wcstombs (wstr).c_str ());

                context_scim->impl->need_commit_preedit = false;
                context_scim->impl->preedit_string.clear ();
            }

            if (_resource_check_timer)
                ecore_timer_del (_resource_check_timer);
            _resource_check_timer = ecore_timer_add (2.0, change_block_status_timer_cb, context_scim);
            context_scim->impl->block_input_resource = true;
        }
        context_scim->impl->input_resource = input_res;
    }
}

/* Public functions */
void
isf_wsc_context_init (void)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");

    if (!_scim_initialized) {
        _ecore_wl2_init_count = ecore_wl2_init ();
        if (_ecore_wl2_init_count > 0) {
            initialize ();
            _scim_initialized = true;
            isf_wsc_input_panel_init ();
            //isf_wsc_context_set_hardware_keyboard_mode(context_scim);
        }
        else
            LOGE("Failed to initialize Ecore_Wl");
    }
}

void
isf_wsc_context_shutdown (void)
{
    LOGD ("");
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";

    if (_scim_initialized) {
        _scim_initialized = false;

        isf_wsc_input_panel_shutdown ();
        finalize ();

        if (_ecore_wl2_init_count > 0) {
            Ecore_Wl2_Display *wl2_display = ecore_wl2_connected_display_get (NULL);
            if (wl2_display) {
                ecore_wl2_display_disconnect (wl2_display);
            }
            ecore_wl2_shutdown ();
            _ecore_wl2_init_count = 0;
        }
    }
}

void
isf_wsc_context_add (WSCContextISF *wsc_ctx)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    WSCContextISF* context_scim = wsc_ctx;

    if (!context_scim) return;
    context_scim->surrounding_text_fd_read_handler = NULL;
    context_scim->selection_text_fd_read_handler = NULL;
    context_scim->remote_surrounding_text_fd_read_handler = NULL;
    context_scim->surrounding_text = NULL;
    context_scim->remote_surrounding_text = NULL;
    context_scim->selection_text = NULL;

    context_scim->impl                      = new_ic_impl (context_scim);
    if (context_scim->impl == NULL) {
        std::cerr << "memory allocation failed in " << __FUNCTION__ << "\n";
        return;
    }

    context_scim->impl->client_window       = 0;
    context_scim->impl->preedit_caret       = 0;
    context_scim->impl->cursor_x            = 0;
    context_scim->impl->cursor_y            = 0;
    context_scim->impl->cursor_pos          = -1;
    context_scim->impl->cursor_top_y        = 0;
    context_scim->impl->is_on               = true;
    context_scim->impl->use_preedit         = _on_the_spot;
    context_scim->impl->preedit_started     = false;
    context_scim->impl->need_commit_preedit = false;

    if (!_ic_list)
        context_scim->next = NULL;
    else
        context_scim->next = _ic_list;
    _ic_list = context_scim;

    context_scim->impl->is_on = _config->read (String (SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), context_scim->impl->is_on);

    g_info_manager->register_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id, "");
    set_ic_capabilities (context_scim);
    SCIM_DEBUG_FRONTEND (2) << "input context created: id = " << context_scim->id << "\n";
}

void
isf_wsc_context_del (WSCContextISF *wsc_ctx)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");

    if (!_ic_list) return;

    WSCContextISF *context_scim = wsc_ctx;
    if (!context_scim) return;

    if (context_scim->selection_text_fd_read_handler) {
        int fd = ecore_main_fd_handler_fd_get (context_scim->selection_text_fd_read_handler);
        if (fd >= 0)
            close (fd);
        ecore_main_fd_handler_del (context_scim->selection_text_fd_read_handler);
        context_scim->selection_text_fd_read_handler = NULL;
    }

    if (context_scim->selection_text) {
        free (context_scim->selection_text);
        context_scim->selection_text = NULL;
    }

    if (context_scim->surrounding_text_fd_read_handler) {
        int fd = ecore_main_fd_handler_fd_get (context_scim->surrounding_text_fd_read_handler);
        if (fd >= 0)
            close (fd);
        ecore_main_fd_handler_del (context_scim->surrounding_text_fd_read_handler);
        context_scim->surrounding_text_fd_read_handler = NULL;
    }

    if (context_scim->remote_surrounding_text_fd_read_handler) {
        int fd = ecore_main_fd_handler_fd_get (context_scim->remote_surrounding_text_fd_read_handler);
        if (fd >= 0)
            close (fd);
        ecore_main_fd_handler_del (context_scim->remote_surrounding_text_fd_read_handler);
        context_scim->remote_surrounding_text_fd_read_handler = NULL;
    }

    if (context_scim->surrounding_text) {
        free (context_scim->surrounding_text);
        context_scim->surrounding_text = NULL;
    }

    if (context_scim->remote_surrounding_text) {
        free (context_scim->remote_surrounding_text);
        context_scim->remote_surrounding_text = NULL;
    }

    if (context_scim->id != _ic_list->id) {
        WSCContextISF * pre = _ic_list;
        WSCContextISF * cur = _ic_list->next;
        while (cur != NULL) {
            if (cur->id == context_scim->id) {
                pre->next = cur->next;
                break;
            }
            pre = cur;
            cur = cur->next;
        }
    } else {
        _ic_list = _ic_list->next;
    }

    if (context_scim->impl) {
        if (context_scim == _focused_ic) {
            g_info_manager->socket_turn_off ();
            g_info_manager->focus_out (WAYLAND_MODULE_CLIENT_ID, context_scim->id);
        }

        g_info_manager->remove_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id);

        if (context_scim->impl) {
            delete_ic_impl (context_scim->impl);
            context_scim->impl = 0;
        }
    }

    if (context_scim == _focused_ic)
        _focused_ic = 0;
}

void
isf_wsc_context_focus_in (WSCContextISF *wsc_ctx)
{
    WSCContextISF* context_scim = wsc_ctx;
    if (!context_scim)
        return;

    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__<< "(" << context_scim->id << ")...\n";

    if (_focused_ic) {
        if (_focused_ic == context_scim) {
            SCIM_DEBUG_FRONTEND(1) << "It's already focused.\n";
            return;
        }
        SCIM_DEBUG_FRONTEND(1) << "Focus out previous IC first: " << _focused_ic->id << "\n";
        isf_wsc_context_focus_out (_focused_ic);
    }

    if (_change_keyboard_mode_by_focus_move) {
        //if h/w keyboard mode, keyboard mode will be changed to s/w mode when the entry get the focus.
        LOGD ("Keyboard mode is changed H/W->S/W because of focus_in.");
        isf_wsc_context_set_keyboard_mode (wsc_ctx, TOOLBAR_HELPER_MODE);
    }

    if (context_scim && context_scim->impl) {
        _focused_ic = context_scim;

        context_scim->impl->is_on = _config->read (String (SCIM_CONFIG_FRONTEND_IM_OPENED_BY_DEFAULT), context_scim->impl->is_on);
        context_scim->impl->surrounding_text.clear ();
        context_scim->impl->preedit_string.clear ();
        context_scim->impl->preedit_attrlist.clear ();
        context_scim->impl->commit_string.clear ();
        context_scim->impl->preedit_caret = 0;
        context_scim->impl->preedit_started = false;

        g_info_manager->register_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id, "");

        set_ic_capabilities (context_scim);

        //FIXME: modify the parameter ic->impl->si->get_factory_uuid ()
        g_info_manager->focus_in (WAYLAND_MODULE_CLIENT_ID, context_scim->id, "");
        if (context_scim->impl->is_on) {
            g_info_manager->socket_turn_on ();
            //            _panel_client.hide_preedit_string (context_scim->id);
            //            _panel_client.hide_aux_string (context_scim->id);
            //            _panel_client.hide_lookup_table (context_scim->id);
            //FIXME
            #if 0 //REMOVE_SCIM_LAUNCHER
            context_scim->impl->si->set_layout (wsc_context_input_panel_layout_get (wsc_ctx));
            context_scim->impl->si->set_prediction_allow (context_scim->impl->prediction_allow);

            if (context_scim->impl->imdata)
                context_scim->impl->si->set_imdata ((const char*)context_scim->impl->imdata, context_scim->impl->imdata_size);
            #endif
            LOGD ("set autocapital type : %d, ctx : %p", context_scim->impl->autocapital_type, wsc_ctx);
            g_info_manager->set_autocapital_type ((int)context_scim->impl->autocapital_type);
        } else {
            g_info_manager->socket_turn_off ();
        }

        g_info_manager->get_active_helper_option (WAYLAND_MODULE_CLIENT_ID, _active_helper_option);

        if (_TV)
            g_info_manager->remoteinput_callback_focus_in ();

        context_scim->impl->init_remote_entry_metadata = false;
        context_scim->impl->init_remote_surrounding_text = false;
        context_scim->impl->block_input_resource = false;
        context_scim->impl->input_resource = INPUT_RESOURCE_NONE;
    }
}

void
isf_wsc_context_focus_out (WSCContextISF *wsc_ctx)
{
    WSCContextISF* context_scim = wsc_ctx;

    if (!context_scim) return;

    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "(" << context_scim->id << ")...\n";

    if (context_scim && context_scim->impl && context_scim == _focused_ic) {
        LOGD ("ctx : %p", wsc_ctx);

        if (context_scim->impl->need_commit_preedit) {
            _hide_preedit_string (context_scim->id, false);

            wsc_context_commit_preedit_string (context_scim);
            g_info_manager->socket_reset_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id);
        }

        context_scim->impl->cursor_pos = -1;
//          if (context_scim->impl->shared_si) context_scim->impl->si->reset ();
        g_info_manager->focus_out (WAYLAND_MODULE_CLIENT_ID, context_scim->id);

        _focused_ic = 0;

        if (_TV)
        g_info_manager->remoteinput_callback_focus_out ();

        context_scim->impl->surrounding_text.clear ();
        context_scim->impl->preedit_string.clear ();
        context_scim->impl->preedit_attrlist.clear ();
        context_scim->impl->commit_string.clear ();
        context_scim->impl->preedit_caret = 0;
        context_scim->impl->preedit_started = false;
        context_scim->impl->need_commit_preedit = false;

        context_scim->impl->init_remote_entry_metadata = true;
        context_scim->impl->init_remote_surrounding_text = true;
        context_scim->impl->block_input_resource = true;
        context_scim->impl->input_resource = INPUT_RESOURCE_NONE;
    }
    _x_key_event_is_valid = false;
}

void
isf_wsc_context_preedit_string_get (WSCContextISF *wsc_ctx, char** str, int *cursor_pos)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    WSCContextISF* context_scim = wsc_ctx;

    if (context_scim && context_scim->impl && context_scim->impl->is_on) {
        String mbs = utf8_wcstombs (context_scim->impl->preedit_string);

        if (str) {
            if (mbs.length ())
                *str = strdup (mbs.c_str ());
            else
                *str = strdup ("");
        }

        if (cursor_pos) {
            //*cursor_pos = context_scim->impl->preedit_caret;
            mbs = utf8_wcstombs (
                context_scim->impl->preedit_string.substr (0, context_scim->impl->preedit_caret));
            *cursor_pos = mbs.length ();
        }
    } else {
        if (str)
            *str = strdup ("");

        if (cursor_pos)
            *cursor_pos = 0;
    }
}

void
isf_wsc_context_autocapital_type_set (WSCContextISF* wsc_ctx, Ecore_IMF_Autocapital_Type autocapital_type)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " = " << autocapital_type << "...\n";
    WSCContextISF* context_scim = wsc_ctx;

    if (context_scim && context_scim->impl && context_scim->impl->autocapital_type != autocapital_type) {
        context_scim->impl->autocapital_type = autocapital_type;
        if (context_scim == _focused_ic) {
            LOGD ("ctx : %p. set autocapital type : %d", wsc_ctx, autocapital_type);
            g_info_manager->set_autocapital_type ((int)autocapital_type);
        }
    }
}

void
isf_wsc_context_bidi_direction_set (WSCContextISF* wsc_ctx, Ecore_IMF_BiDi_Direction direction)
{
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";

    WSCContextISF *context_scim = wsc_ctx;

    if (context_scim && context_scim->impl) {
        if (context_scim->impl->bidi_direction != direction) {
            context_scim->impl->bidi_direction = direction;

            if (context_scim == _focused_ic) {
                LOGD ("ctx : %p, bidi direction : %#x", wsc_ctx, direction);
                panel_req_update_bidi_direction (context_scim, direction);
            }
        }
    }
}

void
isf_wsc_context_send_surrounding_text (WSCContextISF* wsc_ctx, const char *text, int cursor)
{
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";

    WSCContextISF *context_scim = wsc_ctx;

    if (!context_scim || !context_scim->impl || !text)
        return;

    char *conv_text = strdup (utf8_wcstombs (context_scim->impl->surrounding_text).c_str ());
    if (!conv_text)
        return;

    if (!context_scim->impl->init_remote_surrounding_text || strcmp (conv_text, text) != 0 || context_scim->impl->cursor_pos != cursor) {
        SECURE_LOGD("remote surrounding text : \"%s\"", text);
        context_scim->impl->surrounding_text = utf8_mbstowcs (String (text));
        context_scim->impl->cursor_pos = cursor;

        if (_TV) {
            if (context_scim->impl->input_resource != INPUT_RESOURCE_REMOTE) {
                if (context_scim->impl->panel_layout == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD) {
                    g_info_manager->remoteinput_callback_surrounding_text (String (""), 0);
                }
                else {
                    g_info_manager->remoteinput_callback_surrounding_text (String (text), context_scim->impl->cursor_pos);
                }
            }
        }
        context_scim->impl->init_remote_surrounding_text = true;
    }
    free (conv_text);
}

void
isf_wsc_context_send_entry_metadata (WSCContextISF* wsc_ctx, Ecore_IMF_Input_Hints hint,
                                     Ecore_IMF_Input_Panel_Layout layout, int variation,
                                     Ecore_IMF_Autocapital_Type type, int return_key_disabled,
                                     Ecore_IMF_Input_Panel_Return_Key_Type return_key_type)
{
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";

    WSCContextISF *context_scim = wsc_ctx;

    if (context_scim && context_scim->impl) {
        if (!context_scim->impl->init_remote_entry_metadata || (context_scim->impl->input_hint != hint || context_scim->impl->panel_layout != layout ||
            context_scim->impl->variation != variation || context_scim->impl->autocapital_type != type ||
            context_scim->impl->return_key_disabled != return_key_disabled || context_scim->impl->return_key_type != return_key_type)) {
            if (context_scim->impl->panel_layout != layout || context_scim->impl->variation != variation) {
                if (context_scim->impl->input_resource == INPUT_RESOURCE_REMOTE)
                    context_scim->impl->input_resource = INPUT_RESOURCE_NONE;
            }

            context_scim->impl->input_hint = hint;
            context_scim->impl->panel_layout = layout;
            context_scim->impl->variation = variation;
            context_scim->impl->autocapital_type = type;
            context_scim->impl->return_key_disabled = return_key_disabled;
            context_scim->impl->return_key_type = return_key_type;

            if (_TV)
                g_info_manager->remoteinput_callback_entry_metadata (context_scim->impl->input_hint, context_scim->impl->panel_layout,
                        context_scim->impl->variation, context_scim->impl->autocapital_type, context_scim->impl->return_key_disabled,
                        context_scim->impl->return_key_type);

            context_scim->impl->init_remote_entry_metadata = true;

            if (_TV)
                remote_surrounding_get (wsc_ctx);
        }
    }
}

#if ENABLE_GRAB_KEYBOARD
static
bool is_number_key (const char *str)
{
    if (!str) return false;

    int result = atoi (str);

    if (result == 0) {
        if (!strcmp (str, "0"))
            return true;
        else
            return false;
    }
    else
        return true;
}

void
isf_wsc_context_filter_key_event (WSCContextISF* wsc_ctx,
                                  uint32_t serial,
                                  uint32_t timestamp, uint32_t keycode, uint32_t symcode,
                                  char *keyname,
                                  enum wl_keyboard_key_state state)

{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");

    if (!wsc_ctx) return;

    KeyEvent key(symcode, wsc_ctx->modifiers);

    bool ignore_key = filter_keys (keyname, SCIM_CONFIG_HOTKEYS_FRONTEND_IGNORE_KEY);

    if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
        key.mask = SCIM_KEY_ReleaseMask;
    } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
        if (!ignore_key) {
            /* Hardware input detect code */
            if (get_keyboard_mode () == TOOLBAR_HELPER_MODE &&
                timestamp > 1 &&
                _support_hw_keyboard_mode &&
                strncmp (keyname, "XF86", 4)) {
                bool hw_key_detect = false;

                if (_TV) {
                    if (strcmp (keyname, "Down") &&
                        strcmp (keyname, "KP_Down") &&
                        strcmp (keyname, "Up") &&
                        strcmp (keyname, "KP_Up") &&
                        strcmp (keyname, "Right") &&
                        strcmp (keyname, "KP_Right") &&
                        strcmp (keyname, "Left") &&
                        strcmp (keyname, "KP_Left") &&
                        strcmp (keyname, "Return") &&
                        strcmp (keyname, "Pause") &&
                        strcmp (keyname, "NoSymbol") &&
                        !is_number_key (keyname)) {
                        hw_key_detect = true;
                    }
                } else {
                    if (key.code != 0x1008ff26 && key.code != 0xFF69) {
                        /* XF86back, Cancel (Power + Volume down) key */
                        hw_key_detect = true;
                    }
                }

                if (hw_key_detect) {
                    isf_wsc_context_set_keyboard_mode (wsc_ctx, TOOLBAR_KEYBOARD_MODE);
                    ISF_SAVE_LOG ("Changed keyboard mode from S/W to H/W (code: %x, name: %s)", key.code, keyname);
                    LOGD ("Hardware keyboard mode, active helper option: %d", _active_helper_option);
                }
            }
        }
    }

    if (!ignore_key) {
        if (!_focused_ic || !_focused_ic->impl || !_focused_ic->impl->is_on) {
            LOGD ("ic is off");
        } else {
            static uint32 _serial = 0;
            g_info_manager->process_key_event (key, ++_serial);
        }
    } else {
        send_wl_key_event (wsc_ctx, key, false);
    }
}
#else
void
isf_wsc_context_filter_key_event (WSCContextISF* wsc_ctx,
                                  uint32_t serial,
                                  uint32_t timestamp, const char *keysym,
                                  bool press, uint32_t modifiers,
                                  const char *dev_name, uint32_t dev_class, uint32_t dev_subclass, uint32_t keycode)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");

    if (!wsc_ctx) return;

    if (!keysym) {
        LOGD("key is NULL");
        return;
    }

    String _key(keysym);
    KeyEvent key;

    if (!scim_string_to_key (key, _key)) {
        xkb_keysym_t code = xkb_keysym_from_name(keysym, XKB_KEYSYM_NO_FLAGS);
        if (code == XKB_KEY_NoSymbol) {
            code = xkb_keysym_from_name(keysym, XKB_KEYSYM_CASE_INSENSITIVE);
        }
        key.code = code;
    }
    scim_set_device_info (key, dev_name ? dev_name : "", dev_class, dev_subclass);

    bool ignore_key = filter_keys (keysym, SCIM_CONFIG_HOTKEYS_FRONTEND_IGNORE_KEY);

    if (modifiers & MOD_SHIFT_MASK)
        key.mask |= SCIM_KEY_ShiftMask;
    if (modifiers & MOD_ALT_MASK)
        key.mask |= SCIM_KEY_AltMask;
    if (modifiers & MOD_CONTROL_MASK)
        key.mask |= SCIM_KEY_ControlMask;

    if (modifiers & MOD_CAPS_MASK)
        key.mask |= SCIM_KEY_CapsLockMask;
    if (modifiers & MOD_NUM_MASK)
        key.mask |= SCIM_KEY_NumLockMask;

    if (!press) {
        key.mask |= SCIM_KEY_ReleaseMask;
    } else {
        if (!ignore_key) {
            /* Hardware input detect code */
            if (get_keyboard_mode () == TOOLBAR_HELPER_MODE &&
                timestamp > 1 &&
                _support_hw_keyboard_mode &&
                strncmp (keysym, "XF86", 4)) {
                bool hw_key_detect = false;

                if (_TV) {
                    if (strcmp (keysym, "Down") &&
                        strcmp (keysym, "KP_Down") &&
                        strcmp (keysym, "Up") &&
                        strcmp (keysym, "KP_Up") &&
                        strcmp (keysym, "Right") &&
                        strcmp (keysym, "KP_Right") &&
                        strcmp (keysym, "Left") &&
                        strcmp (keysym, "KP_Left") &&
                        strcmp (keysym, "Return") &&
                        strcmp (keysym, "KP_Enter") &&
                        strcmp (keysym, "Pause") &&
                        strcmp (keysym, "NoSymbol") &&
                        key.dev_subclass != ECORE_DEVICE_SUBCLASS_REMOCON) {
                        hw_key_detect = true;
                    }
                } else {
                    if (key.code != 0x1008ff26 && key.code != 0xFF69) {
                        /* XF86back, Cancel (Power + Volume down) key */
                        hw_key_detect = true;
                    }
                }

                if (hw_key_detect) {
                    isf_wsc_context_set_keyboard_mode (wsc_ctx, TOOLBAR_KEYBOARD_MODE);
                    ISF_SAVE_LOG ("Changed keyboard mode from S/W to H/W (code: %x, key : %s)", key.code, keysym);
                    LOGD ("Hardware keyboard mode, active helper option: %d", _active_helper_option);
                }
            }
        }
    }

    if (!ignore_key) {
        if (!_focused_ic || !_focused_ic->impl || !_focused_ic->impl->is_on) {
            LOGD ("ic is off");
        } else if (g_info_manager->process_key_event (key, serial, keycode)){
            return;
        }
    }

    if (!instance.null ())
        instance->process_key_event_done (WAYLAND_MODULE_CLIENT_ID, wsc_ctx->id, key, EINA_FALSE, serial);
}
#endif

static void
wsc_commit_preedit (WSCContextISF* wsc_ctx)
{
    char* surrounding_text;

    if (!wsc_ctx || !wsc_ctx->preedit_str ||
        strlen (wsc_ctx->preedit_str) == 0)
        return;

    wl_input_method_context_cursor_position (wsc_ctx->im_ctx,
                                             0, 0);

    if (strlen (wsc_ctx->preedit_str) > MAX_PREEDIT_BUFSIZE) {
        char str_buffer[MAX_PREEDIT_BUFSIZE];

        memcpy (str_buffer, wsc_ctx->preedit_str, MAX_PREEDIT_BUFSIZE - 1);
        str_buffer[MAX_PREEDIT_BUFSIZE - 1] = '\0';
        char *old_preedit_str = wsc_ctx->preedit_str;
        wsc_ctx->preedit_str = (char*)realloc (wsc_ctx->preedit_str, sizeof(char) * MAX_PREEDIT_BUFSIZE);
        if (wsc_ctx->preedit_str) {
            memcpy (wsc_ctx->preedit_str, str_buffer, strlen(str_buffer));
            wsc_ctx->preedit_str[MAX_PREEDIT_BUFSIZE - 1] = '\0';
        } else {
            free (old_preedit_str);
            LOGE ("realloc failed");
            return;
        }
    }

    wl_input_method_context_commit_string (wsc_ctx->im_ctx,
                                           wsc_ctx->serial,
                                           wsc_ctx->preedit_str);

    if (wsc_ctx->surrounding_text) {
        surrounding_text = insert_text (wsc_ctx->surrounding_text,
                                        wsc_ctx->surrounding_cursor,
                                        wsc_ctx->preedit_str);

        free (wsc_ctx->surrounding_text);
        wsc_ctx->surrounding_text = surrounding_text;
        wsc_ctx->surrounding_cursor += strlen (wsc_ctx->preedit_str);
        LOGD ("wsc_ctx->surrounding_cursor = %d", wsc_ctx->surrounding_cursor);
    } else {
        wsc_ctx->surrounding_text = strdup (wsc_ctx->preedit_str);
        wsc_ctx->surrounding_cursor = strlen (wsc_ctx->preedit_str);
        LOGD ("wsc_ctx->surrounding_cursor = %d", wsc_ctx->surrounding_cursor);
    }

    if (wsc_ctx->preedit_str)
        free (wsc_ctx->preedit_str);

    wsc_ctx->preedit_str = strdup ("");
}

static void
wsc_send_preedit_style (WSCContextISF* wsc_ctx)
{
    if (!wsc_ctx) return;
    if (wsc_ctx->impl && wsc_ctx->impl->is_on) {
        String mbs = utf8_wcstombs (wsc_ctx->impl->preedit_string);

        if (!wsc_ctx->impl->preedit_attrlist.empty ()) {
            if (mbs.length ()) {
                uint32_t preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_DEFAULT;
                int start_index, end_index;
                int wlen = wsc_ctx->impl->preedit_string.length ();
                AttributeList::const_iterator i;
                bool *attrs_flag = new bool [mbs.length ()];
                if (attrs_flag) {
                    memset (attrs_flag, 0, mbs.length () * sizeof (bool));
                    for (i = wsc_ctx->impl->preedit_attrlist.begin ();
                         i != wsc_ctx->impl->preedit_attrlist.end (); ++i) {
                        start_index = i->get_start ();
                        end_index = i->get_end ();
                        if (end_index <= wlen && start_index < end_index && i->get_type () != SCIM_ATTR_DECORATE_NONE) {
                            start_index = g_utf8_offset_to_pointer (mbs.c_str (), i->get_start ()) - mbs.c_str ();
                            end_index = g_utf8_offset_to_pointer (mbs.c_str (), i->get_end ()) - mbs.c_str ();
                            if (i->get_type () == SCIM_ATTR_DECORATE) {
                                switch (i->get_value ())
                                {
                                    case SCIM_ATTR_DECORATE_UNDERLINE:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_UNDERLINE;
                                        break;
                                    case SCIM_ATTR_DECORATE_REVERSE:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_REVERSE;
                                        break;
                                    case SCIM_ATTR_DECORATE_HIGHLIGHT:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_HIGHLIGHT;
                                        break;
                                    case SCIM_ATTR_DECORATE_BGCOLOR1:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_BGCOLOR1;
                                        break;
                                    case SCIM_ATTR_DECORATE_BGCOLOR2:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_BGCOLOR2;
                                        break;
                                    case SCIM_ATTR_DECORATE_BGCOLOR3:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_BGCOLOR3;
                                        break;
                                    case SCIM_ATTR_DECORATE_BGCOLOR4:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_BGCOLOR4;
                                        break;
                                    default:
                                        preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_DEFAULT;
                                        break;
                                }

                                if (preedit_style)
                                  wl_input_method_context_preedit_styling (wsc_ctx->im_ctx,
                                                                           start_index,
                                                                           end_index,
                                                                           preedit_style);
                                switch (i->get_value ())
                                {
                                    case SCIM_ATTR_DECORATE_UNDERLINE:
                                    case SCIM_ATTR_DECORATE_REVERSE:
                                    case SCIM_ATTR_DECORATE_HIGHLIGHT:
                                    case SCIM_ATTR_DECORATE_BGCOLOR1:
                                    case SCIM_ATTR_DECORATE_BGCOLOR2:
                                    case SCIM_ATTR_DECORATE_BGCOLOR3:
                                    case SCIM_ATTR_DECORATE_BGCOLOR4:
                                        // Record which character has attribute.
                                        for (int pos = start_index; pos < end_index; ++pos)
                                            attrs_flag [pos] = 1;
                                        break;
                                    default:
                                        break;
                                }
                            } else if (i->get_type () == SCIM_ATTR_FOREGROUND) {
                                SCIM_DEBUG_FRONTEND(4) << "SCIM_ATTR_FOREGROUND\n";
                            } else if (i->get_type () == SCIM_ATTR_BACKGROUND) {
                                SCIM_DEBUG_FRONTEND(4) << "SCIM_ATTR_BACKGROUND\n";
                            }
                        }
                    }
                    // Add underline for all characters which don't have attribute.
                    for (unsigned int pos = 0; pos < mbs.length (); ++pos) {
                        if (!attrs_flag [pos]) {
                            int begin_pos = pos;
                            while (pos < mbs.length () && !attrs_flag [pos])
                                ++pos;
                            // use REVERSE style as default
                            preedit_style = WL_TEXT_INPUT_PREEDIT_STYLE_UNDERLINE;
                            start_index = begin_pos;
                            end_index = pos;

                            wl_input_method_context_preedit_styling (wsc_ctx->im_ctx,
                                                                     start_index,
                                                                     end_index,
                                                                     preedit_style);
                        }
                    }
                    delete [] attrs_flag;
                }
            }
        }
    } else {
        if (wsc_ctx->impl && !wsc_ctx->impl->preedit_attrlist.empty ())
            wsc_ctx->impl->preedit_attrlist.clear ();
    }
}

static void
wsc_send_preedit (WSCContextISF* wsc_ctx, int32_t cursor)
{
    LOGD ("");

    if (!wsc_ctx) return;

    uint32_t index = strlen (wsc_ctx->preedit_str);

    if (cursor >= 0)
        index = cursor;

    /* Note : Since the current wayland_immodule implementation does not call
     * PREEDIT_CHANGED callback even when preedit_cursor or preedit_style gets updated, for now
     * we must update preedit_string also whenever preedit_cursor or preedit_style is updated.
     * So the below 3 lines cannot be called separately. */
    wsc_send_preedit_style (wsc_ctx);
    wl_input_method_context_preedit_cursor (wsc_ctx->im_ctx, index);
    wl_input_method_context_preedit_string (wsc_ctx->im_ctx,
                                            wsc_ctx->serial,
                                            wsc_ctx->preedit_str,
                                            utf8_wcstombs (wsc_ctx->impl->commit_string).c_str ());
}

bool wsc_context_surrounding_get (WSCContextISF *wsc_ctx, char **text, int *cursor_pos)
{
    if (!wsc_ctx)
        return false;

    if (text) {
        if (wsc_ctx->surrounding_text)
            *text = strdup (wsc_ctx->surrounding_text);
        else
            *text = strdup ("");
    }

    if (cursor_pos)
        *cursor_pos = wsc_ctx->surrounding_cursor;

    return true;
}

static Eina_Bool
remote_surrounding_text_fd_read_func (void* data, Ecore_Fd_Handler* fd_handler) {
    if (fd_handler == NULL || data == NULL)
        return ECORE_CALLBACK_RENEW;

    WSCContextISF* wsc_ctx = (WSCContextISF*)data;

    int fd = ecore_main_fd_handler_fd_get (fd_handler);
    if (fd < 0)
            return ECORE_CALLBACK_RENEW;

    char buff[512];
    int len = read (fd, buff, sizeof (buff) - 1);

    if (len == 0) {
        SECURE_LOGD ("remote_surrounding_text : %s, surrounding_cursor : %d", wsc_ctx->remote_surrounding_text, wsc_ctx->surrounding_cursor);
        isf_wsc_context_send_surrounding_text (wsc_ctx, wsc_ctx->remote_surrounding_text ? wsc_ctx->remote_surrounding_text : "", wsc_ctx->surrounding_cursor);
    } else if (len < 0) {
        LOGW ("failed");
    } else {
        buff[len] = '\0';
        if (wsc_ctx->remote_surrounding_text == NULL) {
            if (len >= (int)sizeof(int)) {
                /* Add one byte for terminating NULL character and subtract <int> byte for cursor position */
                wsc_ctx->remote_surrounding_text = (char*)malloc (len + 1 - sizeof(int));
                if (wsc_ctx->remote_surrounding_text) {
                    memcpy(&(wsc_ctx->surrounding_cursor), buff, sizeof(int));
                    memcpy (wsc_ctx->remote_surrounding_text, buff + sizeof(int), len - sizeof(int));
                    wsc_ctx->remote_surrounding_text[len - sizeof(int)] = '\0';
                    return ECORE_CALLBACK_RENEW;
                } else {
                    LOGE ("malloc failed");
                }
            }
        } else {
            int old_len = strlen (wsc_ctx->remote_surrounding_text);
            void * _new = realloc (wsc_ctx->remote_surrounding_text, len + old_len + 1);
            if (_new) {
                wsc_ctx->remote_surrounding_text = (char*)_new;
                memcpy (wsc_ctx->remote_surrounding_text + old_len, buff, len);
                wsc_ctx->remote_surrounding_text[old_len + len] = '\0';
                return ECORE_CALLBACK_RENEW;
            } else {
                LOGE ("realloc failed");
            }
        }
    }

    if (wsc_ctx->remote_surrounding_text_fd_read_handler) {
        close (fd);
        ecore_main_fd_handler_del (wsc_ctx->remote_surrounding_text_fd_read_handler);
        wsc_ctx->remote_surrounding_text_fd_read_handler = NULL;
    }

    if (wsc_ctx->remote_surrounding_text) {
        free (wsc_ctx->remote_surrounding_text);
        wsc_ctx->remote_surrounding_text = NULL;
    }

    return ECORE_CALLBACK_CANCEL;
}

static void
remote_surrounding_get (WSCContextISF *wsc_ctx)
{
    if (wsc_ctx && wsc_ctx->im_ctx && wsc_ctx->remote_surrounding_text_fd_read_handler)
        return;

    int filedes[2];
    if (pipe2(filedes, O_CLOEXEC | O_NONBLOCK) == -1) {
        LOGW ("create pipe failed");
        return;
    } else {
        LOGD("%d,%d", filedes[0], filedes[1]);
        if (wsc_ctx && wsc_ctx->im_ctx) {
            wl_input_method_context_get_surrounding_text (wsc_ctx->im_ctx, UINT_MAX, UINT_MAX, filedes[1]);
            Ecore_Wl2_Display *wl2_display = ecore_wl2_connected_display_get (NULL);
            if (wl2_display)
                ecore_wl2_display_flush (wl2_display);
        }
        close (filedes[1]);

        if (wsc_ctx && wsc_ctx->im_ctx) {
            if (wsc_ctx->remote_surrounding_text) {
                free (wsc_ctx->remote_surrounding_text);
                wsc_ctx->remote_surrounding_text = NULL;
            }

            wsc_ctx->remote_surrounding_text_fd_read_handler = ecore_main_fd_handler_add (filedes[0], ECORE_FD_READ, remote_surrounding_text_fd_read_func, wsc_ctx, NULL, NULL);
        }
    }
}

Ecore_IMF_Input_Panel_Layout wsc_context_input_panel_layout_get (WSCContextISF *wsc_ctx)
{
    Ecore_IMF_Input_Panel_Layout layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL;

    if (!wsc_ctx)
        return layout;

    switch (wsc_ctx->content_purpose) {
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_SIGNED:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_DECIMAL:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_SIGNEDDECIMAL:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_NUMBER:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBER;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DATE:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_MONTH;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_TIME:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DATETIME:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_DATETIME;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_PHONE:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_PHONENUMBER;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_URL:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_URL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_EMAIL:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_EMAIL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_PASSWORD:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_PASSWORD_DIGITS:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_HEX:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_HEX;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_TERMINAL:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_TERMINAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_IP:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_IP;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_EMOTICON:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_EMOTICON;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_VOICE:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_VOICE;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_NORMAL:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_FILENAME:
        case WL_TEXT_INPUT_CONTENT_PURPOSE_NAME:
        default:
            layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL;
            break;
    }

    return layout;
}

int wsc_context_input_panel_layout_variation_get (WSCContextISF *wsc_ctx)
{
    int layout_variation = 0;

    if (!wsc_ctx)
        return layout_variation;

    switch (wsc_ctx->content_purpose) {
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY_VARIATION_NORMAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_SIGNED:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY_VARIATION_SIGNED;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_DECIMAL:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY_VARIATION_DECIMAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_DIGITS_SIGNEDDECIMAL:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBERONLY_VARIATION_SIGNED_AND_DECIMAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_PASSWORD:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NORMAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_PASSWORD_DIGITS:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD_VARIATION_NUMBERONLY;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_NORMAL:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL_VARIATION_NORMAL;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_FILENAME:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL_VARIATION_FILENAME;
            break;
        case WL_TEXT_INPUT_CONTENT_PURPOSE_NAME:
            layout_variation = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL_VARIATION_PERSON_NAME;
            break;
        default:
            layout_variation = 0;
            break;
    }

    return layout_variation;
}

Ecore_IMF_Autocapital_Type wsc_context_autocapital_type_get (WSCContextISF *wsc_ctx)
{
    Ecore_IMF_Autocapital_Type autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_NONE;

    if (!wsc_ctx)
        return autocapital_type;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_AUTO_CAPITALIZATION)
        autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_SENTENCE;
    else if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_WORD_CAPITALIZATION)
        autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_WORD;
    else if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_UPPERCASE)
        autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_ALLCHARACTER;
    else
        autocapital_type = ECORE_IMF_AUTOCAPITAL_TYPE_NONE;

    return autocapital_type;
}

bool wsc_context_input_panel_caps_lock_mode_get (WSCContextISF *wsc_ctx)
{
    if (!wsc_ctx)
        return false;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_UPPERCASE)
        return true;

    return false;
}

Ecore_IMF_Input_Panel_Lang wsc_context_input_panel_language_get (WSCContextISF *wsc_ctx)
{
    Ecore_IMF_Input_Panel_Lang language = ECORE_IMF_INPUT_PANEL_LANG_AUTOMATIC;

    if (!wsc_ctx)
        return language;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_LATIN)
        language = ECORE_IMF_INPUT_PANEL_LANG_ALPHABET;
    else
        language = ECORE_IMF_INPUT_PANEL_LANG_AUTOMATIC;

    return language;
}

bool wsc_context_input_panel_password_mode_get (WSCContextISF *wsc_ctx)
{
    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_PASSWORD)
        return true;

    if (wsc_context_input_panel_layout_get (wsc_ctx) == ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD)
        return true;

    return false;
}

Ecore_IMF_Input_Hints wsc_context_input_hint_get (WSCContextISF *wsc_ctx)
{
    int input_hint = ECORE_IMF_INPUT_HINT_NONE;

    if (!wsc_ctx)
        return (Ecore_IMF_Input_Hints)input_hint;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_SENSITIVE_DATA)
        input_hint |= ECORE_IMF_INPUT_HINT_SENSITIVE_DATA;
    else
        input_hint &= ~ECORE_IMF_INPUT_HINT_SENSITIVE_DATA;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_AUTO_COMPLETION)
        input_hint |= ECORE_IMF_INPUT_HINT_AUTO_COMPLETE;
    else
        input_hint &= ~ECORE_IMF_INPUT_HINT_AUTO_COMPLETE;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_MULTILINE)
        input_hint |= ECORE_IMF_INPUT_HINT_MULTILINE;
    else
        input_hint &= ~ECORE_IMF_INPUT_HINT_MULTILINE;

    // autofill
    switch (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_MASK)
    {
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_DATE:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_DATE;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_DAY:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_DAY;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_MONTH:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_MONTH;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_YEAR:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_CREDIT_CARD_EXPIRATION_YEAR;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_CREDIT_CARD_NUMBER:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_CREDIT_CARD_NUMBER;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_EMAIL_ADDRESS:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_EMAIL_ADDRESS;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_PHONE:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_PHONE;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_POSTAL_ADDRESS:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_POSTAL_ADDRESS;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_POSTAL_CODE:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_POSTAL_CODE;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_ID:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_ID;
        break;
    case WL_TEXT_INPUT_CONTENT_HINT_AUTOFILL_NAME:
        input_hint |= ECORE_IMF_INPUT_HINT_AUTOFILL_NAME;
        break;
    }

    return (Ecore_IMF_Input_Hints)input_hint;
}

Eina_Bool wsc_context_prediction_allow_get (WSCContextISF *wsc_ctx)
{
    if (!wsc_ctx)
        return EINA_FALSE;

    if (wsc_ctx->content_hint & WL_TEXT_INPUT_CONTENT_HINT_AUTO_COMPLETION)
        return EINA_TRUE;
    else
        return EINA_FALSE;
}

Ecore_IMF_BiDi_Direction wsc_context_bidi_direction_get (WSCContextISF *wsc_ctx)
{
    return (Ecore_IMF_BiDi_Direction)wsc_ctx->bidi_direction;
}

void wsc_context_delete_surrounding (WSCContextISF *wsc_ctx, int offset, int len)
{
    LOGD ("offset = %d, len = %d", offset, len);

    if (!wsc_ctx)
        return;

    wl_input_method_context_delete_surrounding_text (wsc_ctx->im_ctx, offset, len);
}

void wsc_context_set_selection (WSCContextISF *wsc_ctx, int start, int end)
{
    if (!wsc_ctx)
        return;

    wl_input_method_context_selection_region (wsc_ctx->im_ctx, wsc_ctx->serial, start, end);
}

void wsc_context_commit_string (WSCContextISF *wsc_ctx, const char *str)
{
    if (!wsc_ctx)
        return;

    if (wsc_ctx->preedit_str) {
        free (wsc_ctx->preedit_str);
        wsc_ctx->preedit_str = NULL;
    }

    wsc_ctx->preedit_str = strdup (str);
    wsc_commit_preedit (wsc_ctx);
}

void wsc_context_commit_preedit_string (WSCContextISF *wsc_ctx)
{
    char* preedit_str = NULL;
    int cursor_pos = 0;

    if (!wsc_ctx)
        return;

    isf_wsc_context_preedit_string_get (wsc_ctx, &preedit_str, &cursor_pos);

    if (wsc_ctx->preedit_str) {
        free (wsc_ctx->preedit_str);
        wsc_ctx->preedit_str = NULL;
    }

    wsc_ctx->preedit_str = preedit_str;
    wsc_commit_preedit (wsc_ctx);
}

void wsc_context_send_preedit_string (WSCContextISF *wsc_ctx)
{
    char* preedit_str = NULL;
    int cursor_pos = 0;

    if (!wsc_ctx)
        return;

    isf_wsc_context_preedit_string_get (wsc_ctx, &preedit_str, &cursor_pos);

    if (wsc_ctx->preedit_str) {
        free (wsc_ctx->preedit_str);
        wsc_ctx->preedit_str = NULL;
    }

    wsc_ctx->preedit_str = preedit_str;
    wsc_send_preedit (wsc_ctx, cursor_pos);
}

void wsc_context_send_key (WSCContextISF *wsc_ctx, uint32_t keysym, uint32_t modifiers, uint32_t time, bool press)
{
    if (!wsc_ctx || !wsc_ctx->im_ctx)
        return;

    wl_input_method_context_keysym (wsc_ctx->im_ctx, wsc_ctx->serial, time,
            keysym, press ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED, modifiers);
}

static void
set_ic_capabilities (WSCContextISF *ic)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
#if 0 //FIXME
    if (ic && ic->impl) {
        unsigned int cap = SCIM_CLIENT_CAP_ALL_CAPABILITIES;

        if (!_on_the_spot || !ic->impl->use_preedit)
            cap -= SCIM_CLIENT_CAP_ONTHESPOT_PREEDIT;

        //FIXME:add this interface
        //_info_manager->update_client_capabilities (cap);
    }
#endif
}

//useless
#if 0
/* Panel Requestion functions. */
static void
panel_req_show_help (WSCContextISF *ic)
{
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";

    String help;

    help =  String (_("Smart Common Input Method platform ")) +
            String (SCIM_VERSION) +
            String (_("\n(C) 2002-2005 James Su <suzhe@tsinghua.org.cn>\n\n"));

    if (ic && ic->impl && ic->impl->si) {
        IMEngineFactoryPointer sf = _backend->get_factory (ic->impl->si->get_factory_uuid ());
        if (sf) {
            help += utf8_wcstombs (sf->get_name ());
            help += String (_(":\n\n"));

            help += utf8_wcstombs (sf->get_help ());
            help += String (_("\n\n"));

            help += utf8_wcstombs (sf->get_credits ());
        }

        g_info_manager->socket_show_help (help);
    }

    g_info_manager->remoteinput_callback_focus_out ();
    LOGD("Remote control button click");
}
#endif

static void
panel_req_update_bidi_direction   (WSCContextISF *ic, int direction)
{
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << "...\n";

    if (ic)
        g_info_manager->update_ise_bidi_direction (WAYLAND_MODULE_CLIENT_ID, direction);
}

static bool
filter_keys (const char *keyname, const char *config_path)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    if (!keyname)
        return false;

    std::vector <String> keys;
    scim_split_string_list (keys, _config->read (String (config_path), String ("")), ',');

    for (unsigned int i = 0; i < keys.size (); ++i) {
        if (!strcmp (keyname, keys [i].c_str ())) {
            return true;
        }
    }

    return false;
}

static void
panel_initialize (void)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    String display_name;
    {
        const char *p = getenv ("DISPLAY");
        if (p) display_name = String (p);
    }
    g_info_manager->add_client (WAYLAND_MODULE_CLIENT_ID, 2, FRONTEND_CLIENT);
    _panel_client_id = WAYLAND_MODULE_CLIENT_ID;
    g_info_manager->register_panel_client (_panel_client_id, _panel_client_id);
    WSCContextISF* context_scim = _ic_list;
    _launch_ise_on_request = scim_global_config_read (String (SCIM_GLOBAL_CONFIG_LAUNCH_ISE_ON_REQUEST), false);

    while (context_scim != NULL) {
        //FIXME:modify the parameter
        g_info_manager->register_input_context (WAYLAND_MODULE_CLIENT_ID, context_scim->id, "");

        context_scim = context_scim->next;
    }

    if (_focused_ic) {
        _focused_ic = 0;
    }
}

static void
panel_finalize (void)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    g_info_manager->del_client (WAYLAND_MODULE_CLIENT_ID);
}

static void
panel_slot_update_preedit_caret (int context, int caret)
{
    LOGD ("");
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " context=" << context << " caret=" << caret << " ic=" << ic << "\n";

    if (ic && ic->impl && _focused_ic == ic && ic->impl->preedit_caret != caret) {
        ic->impl->preedit_caret = caret;
        if (ic->impl->use_preedit) {
            if (!ic->impl->preedit_started) {
                if (check_valid_ic (ic))
                    ic->impl->preedit_started = true;
            }
            wsc_send_preedit (ic, caret);
        } else {
            g_info_manager->socket_update_preedit_caret (caret);
        }
    }
}

static void
panel_slot_process_key_event (int context, const KeyEvent &key)
{
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND(1) << __FUNCTION__ << " context=" << context << " key=" << key.get_key_string () << " ic=" << ic << "\n";

    if (!(ic && ic->impl))
        return;

    if ((_focused_ic != NULL) && (_focused_ic != ic))
        return;

    send_wl_key_event (ic, key, false);
}

static void
panel_slot_commit_string (int context, const WideString &wstr, bool remote_mode)
{
    LOGD ("");
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " context=" << context << " str=" << utf8_wcstombs (wstr) << " ic=" << ic << "\n";

    if (ic && ic->impl) {
        if (_focused_ic != ic)
            return;

        if (remote_mode) {
            if (ic->impl->block_input_resource) {
                LOGW ("block remote input");
                return;
            }
            check_input_resource (ic, INPUT_RESOURCE_REMOTE);

            if (ic->impl->panel_layout == ECORE_IMF_INPUT_PANEL_LAYOUT_URL)
                wsc_context_delete_surrounding (ic, INT_MIN/2, INT_MAX);

            wsc_context_commit_string (ic, utf8_wcstombs (wstr).c_str ());
            ic->impl->need_commit_preedit = false;
            ic->impl->preedit_string.clear ();
        } else {
            check_input_resource (ic, INPUT_RESOURCE_LOCAL);
            wsc_context_commit_string (ic, utf8_wcstombs (wstr).c_str ());
        }
    }
}

static void
panel_slot_forward_key_event (int context, const KeyEvent &key, bool remote_mode)
{
    LOGD ("");
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " context=" << context << " key=" << key.get_key_string () << " ic=" << ic << "\n";

    if (!(ic && ic->impl))
        return;

    if ((_focused_ic != NULL) && (_focused_ic != ic))
        return;

    if (remote_mode) {
        if (ic->impl->block_input_resource) {
            LOGW ("block remote input");
            return;
        }
        check_input_resource (ic, INPUT_RESOURCE_REMOTE);
    } else {
        check_input_resource (ic, INPUT_RESOURCE_LOCAL);
    }

    if (key.get_key_string ().length () >= 116)
        return;

    send_wl_key_event (ic, key, true);
}

static void
panel_slot_update_preedit_string (int context, const WideString str, const WideString commit, const AttributeList &attrs, int caret, bool remote_mode)
{
    LOGD ("");
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " context=" << context << " caret=" << caret << " ic=" << ic << "\n";

    if (ic && ic->impl && _focused_ic == ic) {
        if (remote_mode) {
            if (ic->impl->block_input_resource) {
                LOGW ("block remote input");
                return;
            }
            check_input_resource (ic, INPUT_RESOURCE_REMOTE);
        }

        if (!ic->impl->is_on)
            ic->impl->is_on = true;

        if (ic->impl->preedit_string != str || str.length ()) {
            ic->impl->preedit_string   = str;
            ic->impl->preedit_attrlist = attrs;
            ic->impl->commit_string = commit;

            if (ic->impl->use_preedit) {
                if (!ic->impl->preedit_started) {
                    if (!check_valid_ic (ic))
                        return;

                    ic->impl->preedit_started = true;
                    ic->impl->need_commit_preedit = true;
                }
                if (caret >= 0 && caret <= (int)str.length ())
                    ic->impl->preedit_caret    = caret;
                else
                    ic->impl->preedit_caret    = str.length ();

                if (ic->impl->panel_layout == ECORE_IMF_INPUT_PANEL_LAYOUT_URL)
                    wsc_context_delete_surrounding (ic, INT_MIN/2, INT_MAX);

                wsc_context_send_preedit_string (ic);
            } else {
                String _str = utf8_wcstombs (str);
                g_info_manager->socket_update_preedit_string (_str, attrs, (uint32)caret);
            }
        }
    }
}

static void
_show_preedit_string (int context)
{
    LOGD ("");
    WSCContextISF* ic = find_ic (context);
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << " context=" << context << "\n";

    if (ic && ic->impl && _focused_ic == ic) {
        if (!ic->impl->is_on)
            ic->impl->is_on = true;

        if (ic->impl->use_preedit) {
            if (!ic->impl->preedit_started) {
                if (check_valid_ic (ic)) {
                    ic->impl->preedit_started     = true;
                    ic->impl->need_commit_preedit = true;
                }
            }
        } else {
            g_info_manager->socket_show_preedit_string ();
        }
    }
}

static void
_hide_preedit_string (int context, bool update_preedit)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    WSCContextISF* ic = find_ic (context);

    if (ic && ic->impl && _focused_ic == ic) {
        if (!ic->impl->is_on)
            ic->impl->is_on = true;

        bool emit = false;
        if (ic->impl->preedit_string.length ()) {
            ic->impl->preedit_string = WideString ();
            ic->impl->preedit_caret  = 0;
            ic->impl->preedit_attrlist.clear ();
            emit = true;
        }
        ic->impl->commit_string = WideString ();
        if (ic->impl->use_preedit) {
            if (update_preedit && emit) {
                if (!check_valid_ic (ic))
                    return;
            }
            if (ic->impl->preedit_started) {
                if (check_valid_ic (ic)) {
                    ic->impl->preedit_started     = false;
                    ic->impl->need_commit_preedit = false;
                }
            }
            wsc_context_send_preedit_string (ic);
        } else {
            g_info_manager->socket_hide_preedit_string ();
        }
    }
}

void
initialize (void)
{
    LOGD ("Initializing Wayland ISF IMModule...");

    // Get system language.
    _language = scim_get_locale_language (scim_get_current_locale ());

    panel_initialize ();
}

static void
finalize (void)
{
    LOGD ("Finalizing Ecore ISF IMModule...");

    SCIM_DEBUG_FRONTEND(2) << "Finalize all IC partially.\n";
    while (_used_ic_impl_list) {
        // In case in "shared input method" mode,
        // all contexts share only one instance,
        // so we need point the reference pointer correctly before finalizing.
        #if 0 //REMOVE
        if (_used_ic_impl_list->si) {
            _used_ic_impl_list->si->set_frontend_data (static_cast <void*> (_used_ic_impl_list->parent));
        }
        #endif
        if (_used_ic_impl_list->parent && _used_ic_impl_list->parent->ctx) {
            isf_wsc_context_del (_used_ic_impl_list->parent);
        }
    }

    delete_all_ic_impl ();

    SCIM_DEBUG_FRONTEND(2) << " Releasing Config...\n";
    _config.reset ();
    _focused_ic = NULL;
    _ic_list = NULL;

    _scim_initialized = false;
    panel_finalize ();
}

static uint32_t _keyname_to_keysym (uint32_t keyname, uint32_t *modifiers)
{
    if (!modifiers)
        return keyname;

    if ((keyname >= '0' && keyname <= '9') ||
        (keyname >= 'a' && keyname <= 'z')) {
        return keyname;
    } else if (keyname >= 'A' && keyname <= 'Z') {
        *modifiers |= MOD_SHIFT_MASK;
        return keyname + 32;
    }

    switch (keyname) {
        case '!':
            *modifiers |= MOD_SHIFT_MASK;
            return '1';
        case '@':
            *modifiers |= MOD_SHIFT_MASK;
            return '2';
        case '#':
            *modifiers |= MOD_SHIFT_MASK;
            return '3';
        case '$':
            *modifiers |= MOD_SHIFT_MASK;
            return '4';
        case '%':
            *modifiers |= MOD_SHIFT_MASK;
            return '5';
        case '^':
            *modifiers |= MOD_SHIFT_MASK;
            return '6';
        case '&':
            *modifiers |= MOD_SHIFT_MASK;
            return '7';
        case '*':
            *modifiers |= MOD_SHIFT_MASK;
            return '8';
        case '(':
            *modifiers |= MOD_SHIFT_MASK;
            return '9';
        case ')':
            *modifiers |= MOD_SHIFT_MASK;
            return '0';
        case '_':
            *modifiers |= MOD_SHIFT_MASK;
            return '-';
        case '+':
            *modifiers |= MOD_SHIFT_MASK;
            return '=';
        case '{':
            *modifiers |= MOD_SHIFT_MASK;
            return '[';
        case '}':
            *modifiers |= MOD_SHIFT_MASK;
            return ']';
        case '|':
            *modifiers |= MOD_SHIFT_MASK;
            return '\\';
        case ':':
            *modifiers |= MOD_SHIFT_MASK;
            return ';';
        case '\"':
            *modifiers |= MOD_SHIFT_MASK;
            return '\'';
        case '<':
            *modifiers |= MOD_SHIFT_MASK;
            return ',';
        case '>':
            *modifiers |= MOD_SHIFT_MASK;
            return '.';
        case '?':
            *modifiers |= MOD_SHIFT_MASK;
            return '/';
        default:
            return keyname;
    }
}

static void send_wl_key_event (WSCContextISF *ic, const KeyEvent &key, bool fake)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    uint32_t time = 0;
    uint32_t modifiers = 0;

    if (!fake) {
        time = get_time ();
    } else {
        modifiers     |= MOD_Mod5_MASK;
    }

    if (key.is_shift_down ())
        modifiers |= MOD_SHIFT_MASK;
    if (key.is_alt_down ())
        modifiers |= MOD_ALT_MASK;
    if (key.is_control_down ())
        modifiers |= MOD_CONTROL_MASK;

    _keyname_to_keysym (key.code, &modifiers);

    if (ic)
        wsc_context_send_key (ic, key.code, modifiers, time, key.is_key_press ());
}

static void
reload_config_callback (const ConfigPointer &config)
{
    SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
    LOGD ("");
    //FIXME:_frontend_hotkey_matcher and _imengine_hotkey_matcher should be added
    //_frontend_hotkey_matcher.load_hotkeys (config);
    //_imengine_hotkey_matcher.load_hotkeys (config);

    KeyEvent key;
    scim_string_to_key (key,
                        config->read (String (SCIM_CONFIG_HOTKEYS_FRONTEND_VALID_KEY_MASK),
                                      String ("Shift+Control+Alt+Lock")));

    _valid_key_mask = (key.mask > 0) ? (key.mask) : 0xFFFF;
    _valid_key_mask |= SCIM_KEY_ReleaseMask;
    // Special treatment for two backslash keys on jp106 keyboard.
    _valid_key_mask |= SCIM_KEY_QuirkKanaRoMask;

    _on_the_spot = config->read (String (SCIM_CONFIG_FRONTEND_ON_THE_SPOT), _on_the_spot);
    //_shared_input_method = config->read (String (SCIM_CONFIG_FRONTEND_SHARED_INPUT_METHOD), _shared_input_method);
    _change_keyboard_mode_by_focus_move = scim_global_config_read (String (SCIM_GLOBAL_CONFIG_CHANGE_KEYBOARD_MODE_BY_FOCUS_MOVE), _change_keyboard_mode_by_focus_move);
    _support_hw_keyboard_mode = scim_global_config_read (String (SCIM_GLOBAL_CONFIG_SUPPORT_HW_KEYBOARD_MODE), _support_hw_keyboard_mode);

    // Get keyboard layout setting
    // Flush the global config first, in order to load the new configs from disk.
    scim_global_config_flush ();

    _keyboard_layout = scim_get_default_keyboard_layout ();
}

class WaylandPanelAgent: public PanelAgentBase
{
    Connection _config_connection;

public:
    WaylandPanelAgent ()
        : PanelAgentBase ("wayland") {
    }
    ~WaylandPanelAgent () {
        stop ();
    }
    bool initialize (InfoManager* info_manager, const String& display, bool resident) {
        LOGD ("");
        g_info_manager = info_manager;
        isf_wsc_context_init ();

        if (!_wsc_setup (&_wsc)) {
            return false;
        }

        _config_connection = _config->signal_connect_reload (slot (reload_config_callback));

        _wl_im_ctx = new _wl_im;
        if (!_wl_im_ctx) {
            LOGW ("Can not create wl_im_ctx");
            return false;
        }

        return true;
    }
    bool valid (void) const {
        return true;
    }

    void stop (void) {
        if (_need_wl_im_init)
            wl_im_destroy ();

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

        _config_connection.disconnect ();
        isf_wsc_context_shutdown ();
    }

public:
    void
    exit (int id, uint32 contextid) {
        LOGD ("client id:%d", id);
        finalize ();
    }

    void
    update_preedit_caret (int id, uint32 context_id, uint32 caret) {
        LOGD ("client id:%d", id);
        panel_slot_update_preedit_caret (context_id, caret);
    }

    void
    socket_helper_key_event (int id, uint32 context_id,  int cmd , KeyEvent& key) {
        LOGD ("client id:%d", id);

        if (cmd == SCIM_TRANS_CMD_PROCESS_KEY_EVENT)
            panel_slot_process_key_event (context_id, key);
        else
            panel_slot_forward_key_event (context_id, key, false);
    }

    void
    commit_string (int id, uint32 context_id, const WideString& wstr) {
        LOGD ("client id:%d", id);
        panel_slot_commit_string (context_id, wstr, false);
    }

    void
    forward_key_event (int id, uint32 context_id, const KeyEvent &key) {
        LOGD ("client id:%d", id);
        panel_slot_forward_key_event (context_id, key, false);
    }

    void
    remote_commit_string (int id, uint32 context_id, const WideString& wstr) {
        LOGD ("client id:%d", id);
        panel_slot_commit_string (context_id, wstr, true);
    }

    void
    remote_update_preedit_string (int id, uint32 context_id, const WideString str, const WideString commit, const AttributeList &attrs, uint32 caret) {
        LOGD ("client id:%d", id);
        panel_slot_update_preedit_string (context_id, str, commit, attrs, caret, true);
    }

    void
    remote_forward_key_event (int id, uint32 context_id, const KeyEvent &key) {
        LOGD ("client id:%d", id);
        panel_slot_forward_key_event (context_id, key, true);
    }

    void
    remote_delete_surrounding_text (int id, uint32 context_id, uint32 offset, uint32 len) {
        LOGD ("client id:%d", id);

        if (_focused_ic) {
            if (_focused_ic->impl->block_input_resource)
                return;

            check_input_resource (_focused_ic, INPUT_RESOURCE_REMOTE);
            wsc_context_delete_surrounding (_focused_ic, offset, len);
        }
    }

    void
    update_ise_input_context (int client, uint32 context, uint32 type, uint32 value) {
        if (!_focused_ic || !_focused_ic->im_ctx)
            return;

        if (type == ECORE_IMF_INPUT_PANEL_LANGUAGE_EVENT) {
            WSCContextISF* ic = find_ic (context);
            if (ic && ic->language) {
                wl_input_method_context_language (_focused_ic->im_ctx, _focused_ic->serial, ic->language);
            }
            else {
                LOGE("language locale query failed : %p %s", ic, (ic ? ic->language : "NULL"));
                wl_input_method_context_language (_focused_ic->im_ctx, _focused_ic->serial, "");
            }
        }
        wl_input_method_context_input_panel_event (_focused_ic->im_ctx, _focused_ic->serial, type, value);
    }

    void
    update_ise_language_locale (int client, uint32 context, String locale) {
        if (!_focused_ic || !_focused_ic->im_ctx)
            return;

        WSCContextISF* ic = find_ic (context);
        if (ic && locale.length() > 0) {
            if (ic->language) {
                free(ic->language);
                ic->language = NULL;
            }
            ic->language = strdup(locale.c_str());
        }
    }

#if 0
    void
    request_help (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        panel_slot_request_help (context_id);
    }


    void
    request_factory_menu (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        panel_slot_request_factory_menu (context_id);
    }

    void
    change_factory (int id, uint32 context_id, const String& uuid) {
        LOGD ("client id:%d", id);
        panel_slot_change_factory (context_id, uuid);
    }


    void
    reset_keyboard_ise (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        panel_slot_reset_keyboard_ise (context_id);
    }


    void
    update_keyboard_ise (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        panel_slot_update_keyboard_ise (context_id);
    }
#endif

    void
    show_preedit_string (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        _show_preedit_string (context_id);
    }

    void
    hide_preedit_string (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        _hide_preedit_string (context_id, true);
    }

    void
    update_preedit_string (int id, uint32 context_id, WideString preedit, WideString commit, AttributeList& attrs, uint32 caret) {
        LOGD ("client id:%d", id);
        SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
        WSCContextISF* ic = find_ic (context_id);

        if (ic && ic->impl && _focused_ic == ic) {
            if (!ic->impl->is_on)
                ic->impl->is_on = true;

            check_input_resource (ic, INPUT_RESOURCE_LOCAL);

            ic->impl->preedit_string   = preedit;
            ic->impl->preedit_attrlist = attrs;
            ic->impl->commit_string   = commit;

            if (ic->impl->use_preedit) {
                if (!ic->impl->preedit_started) {
                    if (!check_valid_ic (ic))
                        return;

                    ic->impl->preedit_started = true;
                    ic->impl->need_commit_preedit = true;
                }
                if (caret <= preedit.length ())
                    ic->impl->preedit_caret    = caret;
                else
                    ic->impl->preedit_caret    = preedit.length ();
                wsc_context_send_preedit_string (ic);
            } else {
                String _str = utf8_wcstombs (preedit);
                g_info_manager->socket_update_preedit_string (_str, attrs, (uint32)caret);
            }
        }
    }

    void
    recapture_string (int id, uint32 context_id, int offset, int len, WideString preedit, WideString commit, AttributeList& attrs) {
        LOGD ("client id:%d", id);
        SCIM_DEBUG_FRONTEND (1) << __FUNCTION__ << "...\n";
        WSCContextISF* ic = find_ic (context_id);
        String preedit_str = utf8_wcstombs (preedit);
        String commit_str = utf8_wcstombs (commit);

        if (ic && ic->impl && _focused_ic == ic) {
            if (!ic->impl->is_on)
                ic->impl->is_on = true;

            check_input_resource (ic, INPUT_RESOURCE_LOCAL);

            ic->impl->preedit_string   = preedit;
            ic->impl->preedit_attrlist = attrs;
            ic->impl->commit_string   = commit;

            if (ic->impl->use_preedit) {
                if (!ic->impl->preedit_started) {
                    if (!check_valid_ic (ic))
                        return;

                    ic->impl->preedit_started = true;
                    ic->impl->need_commit_preedit = true;
                }
                ic->impl->preedit_caret    = preedit.length ();

                wl_input_method_context_preedit_cursor (ic->im_ctx, strlen(preedit_str.c_str()));
                wsc_send_preedit_style (ic);

                wl_input_method_context_recapture_string (ic->im_ctx, ic->serial,
                                                          offset, len, preedit_str.c_str(), preedit_str.c_str(), commit_str.c_str());
            } else {
                g_info_manager->socket_recapture_string (offset, len, preedit_str, commit_str, attrs);
            }
        }
    }

    static Eina_Bool
    surrounding_text_fd_read_func (void* data, Ecore_Fd_Handler* fd_handler) {
        if (fd_handler == NULL || data == NULL)
            return ECORE_CALLBACK_RENEW;

        WSCContextISF* wsc_ctx = (WSCContextISF*)data;

        int fd = ecore_main_fd_handler_fd_get (fd_handler);
        if (fd < 0)
            return ECORE_CALLBACK_RENEW;

        char buff[512];
        int len = read (fd, buff, sizeof (buff) - 1);
        if (len == 0) {
            LOGD ("update, wsc_ctx->surrounding_cursor = %d", wsc_ctx->surrounding_cursor);
            g_info_manager->socket_update_surrounding_text (wsc_ctx->surrounding_text ? wsc_ctx->surrounding_text : "", wsc_ctx->surrounding_cursor);
        } else if (len < 0) {
            LOGW ("failed");
        } else {
            buff[len] = '\0';
            if (wsc_ctx->surrounding_text == NULL) {
                if (len >= (int)sizeof(int)) {
                    /* Add one byte for terminating NULL character and subtract <int> byte for cursor position */
                    wsc_ctx->surrounding_text = (char*)malloc (len + 1 - sizeof(int));
                    if (wsc_ctx->surrounding_text) {
                        memcpy(&(wsc_ctx->surrounding_cursor), buff, sizeof(int));
                        memcpy (wsc_ctx->surrounding_text, buff + sizeof(int), len - sizeof(int));
                        wsc_ctx->surrounding_text[len - sizeof(int)] = '\0';
                        return ECORE_CALLBACK_RENEW;
                    } else {
                        LOGE ("malloc failed");
                    }
                }
            } else {
                int old_len = strlen (wsc_ctx->surrounding_text);
                void * _new = realloc (wsc_ctx->surrounding_text, len + old_len + 1);
                if (_new) {
                    wsc_ctx->surrounding_text = (char*)_new;
                    memcpy (wsc_ctx->surrounding_text + old_len, buff, len);
                    wsc_ctx->surrounding_text[old_len + len] = '\0';
                    return ECORE_CALLBACK_RENEW;
                } else {
                    LOGE ("realloc failed");
                }
            }
        }

        if (wsc_ctx->surrounding_text_fd_read_handler) {
            close (fd);
            ecore_main_fd_handler_del (wsc_ctx->surrounding_text_fd_read_handler);
            wsc_ctx->surrounding_text_fd_read_handler = NULL;
        }

        if (wsc_ctx->surrounding_text) {
            free (wsc_ctx->surrounding_text);
            wsc_ctx->surrounding_text = NULL;
        }

        return ECORE_CALLBACK_RENEW;
    }

    void
    socket_helper_get_surrounding_text (int id, uint32 context_id, uint32 maxlen_before, uint32 maxlen_after) {
        LOGD ("client id:%d", id);

        int filedes[2];
        if (pipe2(filedes, O_CLOEXEC | O_NONBLOCK) == -1) {
            LOGW ("create pipe failed");
            return;
        }
        LOGD("%d,%d", filedes[0], filedes[1]);
        WSCContextISF* ic = find_ic (context_id);
        if (!ic) return;

        if (ic->im_ctx)
            wl_input_method_context_get_surrounding_text (ic->im_ctx, maxlen_before, maxlen_after, filedes[1]);

        Ecore_Wl2_Display *wl2_display = ecore_wl2_connected_display_get (NULL);
        if (wl2_display)
            ecore_wl2_display_flush (wl2_display);
        close (filedes[1]);

        if (ic->surrounding_text_fd_read_handler) {
            int fd = ecore_main_fd_handler_fd_get (ic->surrounding_text_fd_read_handler);
            if (fd >= 0)
                close (fd);
            ecore_main_fd_handler_del (ic->surrounding_text_fd_read_handler);
            ic->surrounding_text_fd_read_handler = NULL;
        }

        if (ic->surrounding_text) {
            free (ic->surrounding_text);
            ic->surrounding_text = NULL;
        }

        ic->surrounding_text_fd_read_handler = ecore_main_fd_handler_add (filedes[0], ECORE_FD_READ, surrounding_text_fd_read_func, ic, NULL, NULL);
    }

    void
    socket_helper_delete_surrounding_text (int id, uint32 context_id, uint32 offset, uint32 len) {
        LOGD ("client id:%d", id);
        //panel_slot_delete_surrounding_text (context_id, offset, len);
        if (_focused_ic) {
            check_input_resource (_focused_ic, INPUT_RESOURCE_LOCAL);
            wsc_context_delete_surrounding (_focused_ic, offset, len);
        }
    }

    void
    socket_helper_set_selection (int id, uint32 context_id, uint32 start, uint32 end) {
        LOGD ("client id:%d", id);
        if (_focused_ic)
            wsc_context_set_selection (_focused_ic, start, end);
    }

    void
    send_private_command (int id, uint32 context_id, const String& command) {
        LOGD ("client id:%d", id);
        //panel_slot_send_private_command (context_id, command);
        if (_focused_ic && _focused_ic->im_ctx)
            wl_input_method_context_private_command (_focused_ic->im_ctx, _focused_ic->serial, command.c_str ());
    }

    void
    commit_content (int id, uint32 context_id, const String& content, const String& description, const String& mime_types) {
        LOGD ("client id:%d", id);
        if (_focused_ic && _focused_ic->im_ctx)
            wl_input_method_context_commit_content (_focused_ic->im_ctx, _focused_ic->serial, content.c_str (), description.c_str (), mime_types.c_str ());
    }

    void
    hide_helper_ise (int id, uint32 context_id)
    {
        LOGD ("client id:%d", id);
        WSCContextISF* ic = find_ic (context_id);

        if (ic) {
            wl_input_method_context_hide_input_panel (ic->im_ctx, ic->serial);
        }
    }

    static Eina_Bool
    selection_text_fd_read_func (void* data, Ecore_Fd_Handler* fd_handler) {
        if (fd_handler == NULL || data == NULL)
            return ECORE_CALLBACK_RENEW;

        WSCContextISF* wsc_ctx = (WSCContextISF*)data;
        int fd = ecore_main_fd_handler_fd_get (fd_handler);
        if (fd < 0)
            return ECORE_CALLBACK_RENEW;

        char buff[512];
        int len = read (fd, buff, sizeof (buff) - 1);
        if (len == 0) {
            LOGD ("update");
            g_info_manager->socket_update_selection (wsc_ctx->selection_text ? wsc_ctx->selection_text : "");
        } else if (len < 0) {
            LOGW ("failed");
        } else {
            buff[len] = '\0';
            if (wsc_ctx->selection_text == NULL) {
                wsc_ctx->selection_text = (char*)malloc (len + 1);
                if (wsc_ctx->selection_text) {
                    memcpy (wsc_ctx->selection_text, buff, len);
                    wsc_ctx->selection_text[len] = '\0';
                    return ECORE_CALLBACK_RENEW;
                } else {
                    LOGE ("malloc failed");
                }
            } else {
                int old_len = strlen (wsc_ctx->selection_text);
                void * _new = realloc (wsc_ctx->selection_text, len + old_len + 1);
                if (_new) {
                    wsc_ctx->selection_text = (char*)_new;
                    memcpy (wsc_ctx->selection_text + old_len, buff, len);
                    wsc_ctx->selection_text[old_len + len] = '\0';
                    return ECORE_CALLBACK_RENEW;
                } else {
                    LOGE ("realloc failed");
                }
            }
        }

        if (wsc_ctx->selection_text_fd_read_handler) {
            close (fd);
            ecore_main_fd_handler_del (wsc_ctx->selection_text_fd_read_handler);
            wsc_ctx->selection_text_fd_read_handler = NULL;
        }

        if (wsc_ctx->selection_text) {
            free (wsc_ctx->selection_text);
            wsc_ctx->selection_text = NULL;
        }

        return ECORE_CALLBACK_RENEW;
    }

    void
    socket_helper_get_selection (int id, uint32 context_id) {
        LOGD ("client id:%d", id);

        int filedes[2];
        if (pipe2(filedes, O_CLOEXEC | O_NONBLOCK) ==-1 ) {
            LOGW ("create pipe failed");
            return;
        }
        LOGD("%d,%d", filedes[0], filedes[1]);

        WSCContextISF* ic = find_ic (context_id);
        if (!ic) return;

        wl_input_method_context_get_selection_text (ic->im_ctx, filedes[1]);
        Ecore_Wl2_Display *wl2_display = ecore_wl2_connected_display_get (NULL);
        if (wl2_display)
            ecore_wl2_display_flush (wl2_display);
        close (filedes[1]);

        if (ic->selection_text_fd_read_handler) {
            int fd = ecore_main_fd_handler_fd_get (ic->selection_text_fd_read_handler);
            if (fd >= 0)
                close (fd);

            ecore_main_fd_handler_del (ic->selection_text_fd_read_handler);
            ic->selection_text_fd_read_handler = NULL;
        }

        if (ic->selection_text) {
            free (ic->selection_text);
            ic->selection_text = NULL;
        }

        ic->selection_text_fd_read_handler = ecore_main_fd_handler_add (filedes[0], ECORE_FD_READ, selection_text_fd_read_func, ic, NULL, NULL);
    }

    void process_key_event_done (int id, uint32 context_id, KeyEvent &key, uint32 ret, uint32 serial) {
        LOGD ("client id:%d", id);
        WSCContextISF* ic = find_ic (context_id);
        if (!ic) return;

#if ENABLE_GRAB_KEYBOARD
        if (ret == EINA_FALSE) {
            send_wl_key_event (ic, key, false);
        }
#else
        wl_input_method_context_filter_key_event_done (ic->im_ctx, serial, ret);
#endif
    }

    void request_ise_hide (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        WSCContextISF* ic = find_ic (context_id);
        if (!ic) return;

        wl_input_method_context_hide_input_panel (ic->im_ctx, ic->serial);
    }

    void
    update_ise_geometry (int id, uint32 context_id, uint32 x, uint32 y, uint32 width, uint32 height) {
        LOGD ("client id:%d", id);

        if (_focused_ic && _focused_ic->im_ctx) {
            wl_input_method_context_update_ise_geometry (_focused_ic->im_ctx, _focused_ic->serial, x, y, width, height);
        }
    }

    void helper_candidate_show(int id, uint32 context_id, const String&  uuid)
    {
        WSCContextISF* ic = find_ic(context_id);
        if (!ic) return;

        if (ic->im_ctx) {
            wl_input_method_context_update_candidate_state(ic->im_ctx, 1);
        }
    }

    void helper_candidate_hide(int id, uint32 context_id, const String&  uuid)
    {
        WSCContextISF* ic = find_ic(context_id);
        if (!ic) return;

        if (ic->im_ctx) {
            wl_input_method_context_update_candidate_state(ic->im_ctx, 0);
        }
    }

    void update_entry_metadata (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        WSCContextISF* ic = find_ic(context_id);
        if (!ic || !ic->im_ctx) return;

        if (ic->layout_initialized)
            isf_wsc_context_input_panel_layout_set (ic, wsc_context_input_panel_layout_get (ic));

        if (ic->prediction_allow_initialized)
            g_info_manager->set_prediction_allow (WAYLAND_MODULE_CLIENT_ID, wsc_context_prediction_allow_get (ic));

        if (ic->autocapital_type_initialized)
            isf_wsc_context_autocapital_type_set (ic, wsc_context_autocapital_type_get (ic));

        if (ic->language_initialized)
            isf_wsc_context_input_panel_language_set (ic, wsc_context_input_panel_language_get (ic));

        g_info_manager->socket_update_cursor_position (ic->surrounding_cursor);
        isf_wsc_context_input_panel_return_key_type_set (ic, (Ecore_IMF_Input_Panel_Return_Key_Type)ic->return_key_type);
        isf_wsc_context_input_panel_return_key_disabled_set (ic, ic->return_key_disabled);

        if (ic->impl)
            isf_wsc_context_input_panel_imdata_set (ic, (void *)ic->impl->imdata, ic->impl->imdata_size);

        isf_wsc_context_bidi_direction_set (ic, (Ecore_IMF_BiDi_Direction)ic->bidi_direction);
        isf_wsc_context_input_panel_caps_mode_set (ic, ic->caps_mode);

        if (ic->impl)
            isf_wsc_context_input_panel_mime_type_accept_set (ic, ic->impl->mime_type.c_str ());

        if (_TV) {
            isf_wsc_context_send_entry_metadata (ic, wsc_context_input_hint_get (ic),	wsc_context_input_panel_layout_get (ic),
                    wsc_context_input_panel_layout_variation_get (ic), wsc_context_autocapital_type_get (ic), ic->return_key_disabled,
                    (Ecore_IMF_Input_Panel_Return_Key_Type)ic->return_key_type);
        }
    }

    void request_ise_reshow (int id, uint32 context_id) {
        LOGD ("client id:%d", id);
        WSCContextISF* ic = find_ic (context_id);
        if (!ic) return;

        wl_input_method_context_reshow_input_panel (ic->im_ctx);
    }
};

extern "C" {

    EXAPI void scim_module_init (void)
    {
        LOGD ("");
    }

    EXAPI void scim_module_exit (void)
    {
        LOGD ("");
        instance.reset ();
    }

    EXAPI void scim_panel_agent_module_init (const scim::ConfigPointer& config)
    {
        LOGD ("");
        _config = config;
    }

    EXAPI scim::PanelAgentPointer scim_panel_agent_module_get_instance ()
    {
        scim::PanelAgentBase* _instance = NULL;
        if (instance.null ()) {
            try {
                _instance = new WaylandPanelAgent ();
            } catch (...) {
                delete _instance;
                _instance = NULL;
            }

            if (_instance)
                instance = _instance;
        }
        return instance;
    }
}

/*
vi:ts=4:nowrap:expandtab
*/