File: imattr.c

package info (click to toggle)
kinput2 3.0-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,644 kB
  • ctags: 4,647
  • sloc: ansic: 45,508; makefile: 98; sh: 12
file content (2531 lines) | stat: -rw-r--r-- 61,804 bytes parent folder | download | duplicates (2)
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
#ifndef lint
static char *rcsid = "$Id: imattr.c,v 1.16 1999/01/13 08:42:03 ishisone Exp $";
#endif
/*
 * Copyright (c) 1994  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

#include "im.h"

#ifndef XNSeparatorofNestedList
#define XNSeparatorofNestedList "separatorofNesttedList"
#endif

#define PAD4(n)	((((n) + 3) / 4) * 4)

/*
 * List of supported input styles.
 */

typedef struct {
    XIMStyle xim_style;		/* X11R5 spec. */
    int conversion_style;	/* kinput2 spec. */
} InputStyle;

static InputStyle styles[] = {
    { XIMPreeditPosition|XIMStatusArea, IMSTYLE_OVER_THE_SPOT },
    { XIMPreeditPosition|XIMStatusNothing, IMSTYLE_OVER_THE_SPOT },
    { XIMPreeditArea|XIMStatusArea, IMSTYLE_OFF_THE_SPOT },
    { XIMPreeditCallbacks|XIMStatusCallbacks, IMSTYLE_ON_THE_SPOT },
    { XIMPreeditCallbacks|XIMStatusNothing, IMSTYLE_ON_THE_SPOT },
    { XIMPreeditNothing|XIMStatusNothing, IMSTYLE_SEPARATE },
    { 0 },
};

#define NEST_NONE	0
#define NEST_PREEDIT	1
#define NEST_STATUS	2

#define CHECK_ICATTR_SIZE(validsize, code) \
	if (len != validsize) { badSizeError(icp, code); return -1; }

#undef OP_C
#undef OP_S
#undef OP_G

#define OP_C	1	/* Create */
#define OP_S	2	/* SetValues */
#define OP_G	4	/* GetValues */

typedef struct {
    char *name;		/* attribute name */
    int type;		/* type of attribute value */
    int valid_ops;	/* valid operations for this attribute */
    int (*set_proc) _Pt_((IMIM *, char *, int));
    int (*get_proc) _Pt_((IMIM *, unsigned int, int));
} IMAttribute;

typedef struct {
    char *name;		/* attribute name */
    int type;		/* type of attribute value */
    int valid_ops;	/* valid operations for this attribute */
    int (*set_proc) _Pt_((IMIC *, char *, int, int, int, int));
    int (*get_proc) _Pt_((IMIC *, unsigned int, int, int, char *, int));
} ICAttribute;


/*
 * IM attributes
 */

static int getQueryInputStyle _Pt_((IMIM *imp, unsigned int id, int offset));

static IMAttribute imAttributes[] = {
    { XNQueryInputStyle, TYPE_XIM_STYLES, OP_G,
	  NULL, getQueryInputStyle },
};

static int numImAttributes = XtNumber(imAttributes);


/*
 * IC attributes
 */

static int setInputStyle _Pt_((IMIC *, char *, int, int, int, int));
static int setClientWindow _Pt_((IMIC *, char *, int, int, int, int));
static int setFocusWindow _Pt_((IMIC *, char *, int, int, int, int));
static int setPreeditAttributes _Pt_((IMIC *, char *, int, int, int, int));
static int setStatusAttributes _Pt_((IMIC *, char *, int, int, int, int));
static int setArea _Pt_((IMIC *, char *, int, int, int, int));
static int setAreaNeeded _Pt_((IMIC *, char *, int, int, int, int));
static int setForeground _Pt_((IMIC *, char *, int, int, int, int));
static int setBackground _Pt_((IMIC *, char *, int, int, int, int));
static int setColormap _Pt_((IMIC *, char *, int, int, int, int));
static int setBgPixmap _Pt_((IMIC *, char *, int, int, int, int));
static int setLineSpace _Pt_((IMIC *, char *, int, int, int, int));
static int setCursor _Pt_((IMIC *, char *, int, int, int, int));
static int setSpotLocation _Pt_((IMIC *, char *, int, int, int, int));
static int setStdColormap _Pt_((IMIC *, char *, int, int, int, int));
static int setFontSet _Pt_((IMIC *, char *, int, int, int, int));

static int getPreeditAttributes _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getStatusAttributes _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getInputStyle _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getClientWindow _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getFocusWindow _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getFilterEvents _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getArea _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getAreaNeeded _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getSpotLocation _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getColormap _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getStdColormap _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getForeground _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getBackground _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getBgPixmap _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getFontSet _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getLineSpace _Pt_((IMIC *, unsigned int, int, int, char *, int));
static int getCursor _Pt_((IMIC *, unsigned int, int, int, char *, int));

static ICAttribute icAttributes[] = {
    { XNInputStyle, TYPE_CARD32, OP_C|OP_G,
	  setInputStyle, getInputStyle },
    { XNClientWindow, TYPE_WINDOW, OP_C|OP_S|OP_G,
	  setClientWindow, getClientWindow },
    { XNFocusWindow, TYPE_WINDOW, OP_C|OP_S|OP_G,
	  setFocusWindow, getFocusWindow },
    { XNFilterEvents, TYPE_CARD32, OP_G,
	  NULL, getFilterEvents },
    { XNPreeditAttributes, TYPE_NESTED_LIST, OP_C|OP_S|OP_G,
	  setPreeditAttributes, getPreeditAttributes },
    { XNStatusAttributes, TYPE_NESTED_LIST, OP_C|OP_S|OP_G,
	  setStatusAttributes, getStatusAttributes },
    { XNArea, TYPE_XRECTANGLE, OP_C|OP_S|OP_G,
	  setArea, getArea },
    { XNAreaNeeded, TYPE_XRECTANGLE, OP_C|OP_S|OP_G,
	  setAreaNeeded, getAreaNeeded },
    { XNSpotLocation, TYPE_XPOINT, OP_C|OP_S|OP_G,
	  setSpotLocation, getSpotLocation },
    { XNColormap, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setColormap, getColormap },
    { XNStdColormap, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setStdColormap, getStdColormap },
    { XNForeground, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setForeground, getForeground },
    { XNBackground, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setBackground, getBackground },
    { XNBackgroundPixmap, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setBgPixmap, getBgPixmap },
    { XNFontSet, TYPE_XFONTSET, OP_C|OP_S|OP_G,
	  setFontSet, getFontSet },
    { XNLineSpace, TYPE_CARD16, OP_C|OP_S|OP_G,	  /* should be TYPE_INT16 */
	  setLineSpace, getLineSpace },
    { XNCursor, TYPE_CARD32, OP_C|OP_S|OP_G,
	  setCursor, getCursor },
    { XNSeparatorofNestedList, TYPE_SEPARATOR, OP_G,
	  NULL, NULL },
};

static int numIcAttributes = XtNumber(icAttributes);


static unsigned int getC16 _Pt_((char *data, int order));
static int getI16 _Pt_((char *data, int order));
static unsigned long getC32 _Pt_((char *data, int order));
static int validateClientWindow _Pt_((IMIC *icp));
static int validateFocusWindow _Pt_((IMIC *icp));
static void badSizeError _Pt_((IMIC *icp, int code));
static void unnestedError _Pt_((IMIC *icp));
static IMPSAttributes *getPSPtr _Pt_((IMIC *icp, int type));
static int getIMValues _Pt_((IMIM *imp, char *data, int len, int offset));
static int getICValues _Pt_((IMIC *icp, char *data, int len, int nest,
			     int offset, int *sepp));
static int setICValues _Pt_((IMIC *icp, char *data, int len,
			     int major, int op));
static int getPSAttributes _Pt_((IMIC *icp, unsigned int id, int nest,
				 int offset, char *data, int len));
static void changeFonts _Pt_((IMIC *icp, int preedit));
static void fillCommonDefault _Pt_((IMIC *icp, unsigned long mask));
static int getNaturalLineSpace _Pt_((IMIC *icp, int preedit));
static void fillPSDefault _Pt_((IMIC *icp, int type, unsigned long mask));
static int validateCommonAttr _Pt_((IMIC *icp, int checkonly));
static int validatePSAttr _Pt_((IMIC *icp, int type, int checkonly));
static void changeConversionAttributes _Pt_((IMIC *icp));
static void computeAreaNeeded _Pt_((IMIC *icp));
static void computeAreaForQuery _Pt_((IMIC *icp));


/*
 * Functions reading out numbers from byte buffer
 */

static unsigned int
getC16(data, order)
char *data;
int order;
{
    unsigned char *p = (unsigned char *)data;
    unsigned int x;

    x = (order == ORDER_BIG) ? ((p[0] << 8) | p[1]) : (p[0] | p[1] << 8);
    return x;
}

static int
getI16(data, order)
char *data;
int order;
{
    unsigned char *p = (unsigned char *)data;
    long l;

    l = (order == ORDER_BIG) ? ((p[0] << 8) | p[1]) : (p[0] | p[1] << 8);
    return (l < 32768) ? (int)l : (int)(l - 65536L);
}

static unsigned long
getC32(data, order)
char *data;
int order;
{
    unsigned char *p = (unsigned char *)data;
    unsigned long x;

    if (order == ORDER_BIG) {
	x = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
    } else {
	x = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
    }
    return x;
}


/*
 * Functions that check the validity of resources.
 */

static int
validateClientWindow(icp)
IMIC *icp;
{
    return IMValidateWindow(XtDisplay(icp->im->connection->proto_widget),
			    icp->common_attr.client, &icp->client_profile);
}

static int
validateFocusWindow(icp)
IMIC *icp;
{
    IMCommonAttributes *ap = &icp->common_attr;

    /*
     * This function assumes that the client window has already
     * been validated.
     */
    if ((ap->set_mask & ATTR_MASK_CLIENT) && ap->focus == ap->client) {
	icp->focus_profile = icp->client_profile;
	return 1;
    } else {
	return IMValidateWindow(XtDisplay(icp->im->connection->proto_widget),
				icp->common_attr.focus, &icp->focus_profile);
    }
}


/*
 * Functions submit errors
 */

static void
badSizeError(icp, code)
IMIC *icp;
int code;
{
    DPRINT(("bad size error for IC #%d\n", icp->id));
    IMSendError(icp->im->connection, code, icp->im->id, icp->id,
		"invalid size of attribute value");
}

static void
unnestedError(icp)
IMIC *icp;
{
    DPRINT(("unnested error for IC #%d\n", icp->id));
    IMSendError(icp->im->connection, IMBadSomething, icp->im->id, icp->id,
		"either preedit or status specification required");
}


/*
 * Functions getting IM attributes
 */

static IMPSAttributes *
getPSPtr(icp, type)
IMIC *icp;
int type;
{
    switch (type) {
    case NEST_PREEDIT: return &icp->preedit_attr;
    case NEST_STATUS: return &icp->status_attr;
    default: return NULL;
    }
}

static int
getIMValues(imp, data, len, offset)
IMIM *imp;
char *data;
int len;
int offset;		/* request offset */
{
    unsigned int id;		/* attribute ID */
    IMAttribute *attrp;
    IMConnection *conn = imp->connection;
    int byte_order = conn->byte_order;

    while (len >= 2) {
	id = getC16(data, byte_order);
	data += 2;
	len -= 2;

	if (id > numImAttributes) {
	    /* invalid attribute ID */
	    IMCancelRequest(conn, offset);
	    IMSendError(conn, IMBadSomething, imp->id, 0,
			"invalid IM attribute ID");
	    return -1;
	}

	attrp = &imAttributes[id];
	if (!(attrp->valid_ops & OP_G)) {
	    IMCancelRequest(conn, offset);
	    IMSendError(conn, IMBadSomething, imp->id, 0,
			"invalid operation (IMGetValues) for this attribute");
	    return -1;
	}

	if ((*attrp->get_proc)(imp, id, offset) < 0) return -1;
    }
    return 0;
}

/* ARGSUSED */
static int
getQueryInputStyle(imp, id, offset)
IMIM *imp;
unsigned int id;
int offset;
{
    IMConnection *conn = imp->connection;
    unsigned int num_styles, num_bytes;
    InputStyle *stp;

    TRACE(("imlib:getQueryInputStyle()\n"));

    for (num_styles = 0, stp = styles; stp->xim_style != 0; stp++) {
	num_styles++;
    }
    num_bytes = num_styles * 4 + 4;
    IMPutC16(conn, id);
    IMPutC16(conn, num_bytes);
    IMPutC16(conn, num_styles);
    IMPutC16(conn, 0);
    for (stp = styles; stp->xim_style != 0; stp++) {
	IMPutC32(conn, stp->xim_style);
    }
    return 0;
}


/*
 * Functions setting IC attributes
 */

static int
setICValues(icp, data, len, nest, op)
IMIC *icp;
char *data;
int len;
int nest;
int op;
{
    IMConnection *conn = icp->im->connection;
    unsigned int imid = icp->im->id;
    unsigned int icid = icp->id;
    unsigned int id;
    unsigned int value_len;
    unsigned int attr_len;
    char *value;
    ICAttribute *attrp;
    int byte_order = icp->im->connection->byte_order;

    TRACE(("imlib:setICValues()\n"));

    while (len > 0) {
	if (len < 4) {
	    DPRINT(("attribute data length < 4\n"));
	    IMSendError(conn, IMBadSomething, imid, icid, "Bad attribute data");
	    return -1;
	}
	id = getC16(data, byte_order);
	value_len = getC16(data + 2, byte_order);
	attr_len = PAD4(4 + value_len);

	if (attr_len > len) {
	    DPRINT(("attribute data length > request length\n"));
	    IMSendError(conn, IMBadSomething, imid, icid,
			"Bad attribute length");
	    return -1;
	}
	value = data + 4;

	if (id > numIcAttributes) {
	    DPRINT(("invalid IC attribute ID %d\n", id));
	    IMSendError(conn, IMBadSomething, imid, icid,
			"invalid IC attribute ID");
	    return -1;
	}
	attrp = &icAttributes[id];
	if (!(attrp->valid_ops & op)) {
	    DPRINT(("invalid operation (%s) for IC attr %d\n",
		    op == OP_C ? "create" : "set", id));
	    IMSendError(conn, IMBadSomething, imid, icid,
			"invalid operation for this attribute");
	    return -1;
	}

	/*
	 * Call attribute set procedure.
	 */
	if ((*attrp->set_proc)(icp, value, (int)value_len, byte_order, nest, op) < 0) {
	    /*
	     * Error has occured.  The set procedure has already sent
	     * appropriate error message, so just return here.
	     */
	    return -1;
	}

	data += attr_len;
	len -= attr_len;
    }
    return 0;
}

/* ARGSUSED */
static int
setInputStyle(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    TRACE(("imlib:setInputStyle()\n"));

    CHECK_ICATTR_SIZE(4, IMBadStyle);

    /*
     * InputStyle must be set with CreateIC.
     */
    if (op != OP_C) {
	DPRINT(("trying to change input style through SetICValues\n"));
	IMSendError(icp->im->connection, IMBadStyle,
		    icp->im->id, icp->id,
		    "InputStyle cannot be changed by SetICValues");
	return -1;
    }

    icp->common_attr.input_style = (XIMStyle)getC32(value, order);
    icp->common_attr.set_mask |= ATTR_MASK_INPUT_STYLE;
    icp->common_attr.change_mask |= ATTR_MASK_INPUT_STYLE;
    TRACE(("\tinput style: %ld\n", icp->common_attr.input_style));
    return 0;
}

/* ARGSUSED */
static int
setClientWindow(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    TRACE(("imlib:setClientWindow()\n"));

    CHECK_ICATTR_SIZE(4, IMBadClientWindow);

    /*
     * ClientWindow cannot be changed.
     */
    if (icp->common_attr.set_mask & ATTR_MASK_CLIENT) {
	DPRINT(("client window already specified\n"));
	IMSendError(icp->im->connection, IMBadClientWindow,
		    icp->im->id, icp->id, "ClientWindow already set");
	return -1;
    }

    icp->common_attr.client = (Window)getC32(value, order);
    TRACE(("\tclient window: %08lx\n", icp->common_attr.client));

    icp->common_attr.set_mask |= ATTR_MASK_CLIENT;
    icp->common_attr.change_mask |= ATTR_MASK_CLIENT;

    return 0;
}

/* ARGSUSED */
static int
setFocusWindow(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    Window focus;

    TRACE(("imlib:setFocusWindow()\n"));

    CHECK_ICATTR_SIZE(4, IMBadFocusWindow);

    focus = (Window)getC32(value, order);
    TRACE(("\tfocus window: %08lx\n", focus));

    if (!(icp->common_attr.set_mask & ATTR_MASK_FOCUS) ||
	focus != icp->common_attr.focus) {
	icp->common_attr.change_mask |= ATTR_MASK_FOCUS;
    }
    icp->common_attr.focus = focus;
    icp->common_attr.set_mask |= ATTR_MASK_FOCUS;
    return 0;
}

/* ARGSUSED */
static int
setPreeditAttributes(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    TRACE(("imlib:setPreeditAttributes()\n"));
    return setICValues(icp, value, len, NEST_PREEDIT, op);
}

/* ARGSUSED */
static int
setStatusAttributes(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    TRACE(("imlib:setStatusAttributes()\n"));
    return setICValues(icp, value, len, NEST_STATUS, op);
}

/* ARGSUSED */
static int
setArea(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    XRectangle area;

    TRACE(("imlib:setArea()\n"));

    CHECK_ICATTR_SIZE(8, IMBadArea);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    area.x = getI16(value, order);
    area.y = getI16(value + 2, order);
    area.width = getC16(value + 4, order);
    area.height = getC16(value + 6, order);
    TRACE(("\tarea: %d, %d, %d, %d\n",
	   area.x, area.y, area.width, area.height));

    if (!(ap->set_mask & ATTR_MASK_AREA) ||
	area.x != ap->area.x ||
	area.y != ap->area.y ||
	area.width != ap->area.width ||
	area.height != ap->area.height) {
	ap->change_mask |= ATTR_MASK_AREA;
    }

    ap->area.x = area.x;
    ap->area.y = area.y;
    ap->area.width = area.width;
    ap->area.height = area.height;
    ap->set_mask |= ATTR_MASK_AREA;

    return 0;
}

/* ARGSUSED */
static int
setAreaNeeded(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    XRectangle area;

    TRACE(("imlib:setAreaNeeded()\n"));

    CHECK_ICATTR_SIZE(8, IMBadArea);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    area.width = getC16(value + 4, order);
    area.height = getC16(value + 6, order);
    TRACE(("\tarea needed: %d, %d\n", area.width, area.height));

    if (!(ap->set_mask & ATTR_MASK_AREA_NEEDED) ||
	area.width != ap->area_needed.width ||
	area.height != ap->area_needed.height) {
	ap->change_mask |= ATTR_MASK_AREA_NEEDED;
    }

    ap->area_needed.width = area.width;
    ap->area_needed.height = area.height;
    ap->set_mask |= ATTR_MASK_AREA_NEEDED;

    return 0;
}

/* ARGSUSED */
static int
setForeground(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Pixel fore;

    TRACE(("imlib:setForeground()\n"));

    CHECK_ICATTR_SIZE(4, IMBadForeground);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    fore = getC32(value, order);
    TRACE(("\tforeground: %ld\n", fore));

    if (!(ap->set_mask & ATTR_MASK_FOREGROUND) || fore != ap->foreground) {
	ap->change_mask |= ATTR_MASK_FOREGROUND;
    }
    ap->foreground = fore;
    ap->set_mask |= ATTR_MASK_FOREGROUND;
    return 0;
}

/* ARGSUSED */
static int
setBackground(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Pixel back;

    TRACE(("imlib:setBackground()\n"));

    CHECK_ICATTR_SIZE(4, IMBadBackground);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    back = getC32(value, order);
    TRACE(("\tbackground: %ld\n", back));

    if (!(ap->set_mask & ATTR_MASK_BACKGROUND) || back != ap->background) {
	ap->change_mask |= ATTR_MASK_BACKGROUND;
    }
    ap->background = back;
    ap->set_mask |= ATTR_MASK_BACKGROUND;
    return 0;
}

/* ARGSUSED */
static int
setColormap(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Colormap cmap;

    TRACE(("imlib:setColormap()\n"));

    CHECK_ICATTR_SIZE(4, IMBadColormap);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    cmap = getC32(value, order);
    TRACE(("\tcolormap: %08lx\n", cmap));

    if (!(ap->set_mask & ATTR_MASK_COLORMAP) || cmap != ap->colormap) {
	ap->change_mask |= ATTR_MASK_COLORMAP;
    }
    ap->colormap = cmap;
    ap->set_mask |= ATTR_MASK_COLORMAP;
    return 0;
}

/* ARGSUSED */
static int
setBgPixmap(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Pixmap pixmap;

    TRACE(("imlib:setBgPixmap()\n"));

    CHECK_ICATTR_SIZE(4, IMBadPixmap);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    pixmap = getC32(value, order);
    TRACE(("\tbackground pixmap: %08lx\n", pixmap));

    if (!(ap->set_mask & ATTR_MASK_BG_PIXMAP) || pixmap != ap->bg_pixmap) {
	ap->change_mask |= ATTR_MASK_BG_PIXMAP;
    }

    ap->bg_pixmap = pixmap;
    ap->set_mask |= ATTR_MASK_BG_PIXMAP;

    return 0;
}

/* ARGSUSED */
static int
setLineSpace(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    int line_space;

    TRACE(("imlib:setLineSpace()\n"));

    CHECK_ICATTR_SIZE(2, IMBadSomething);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    line_space = getI16(value, order);	/* ??? linespacing is 'int' */
    TRACE(("\tline space: %d\n", line_space));

    if (!(ap->set_mask & ATTR_MASK_LINESPACE) ||
	line_space != ap->line_space) {
	ap->change_mask |= ATTR_MASK_LINESPACE;
    }
    ap->line_space = line_space;
    ap->set_mask |= ATTR_MASK_LINESPACE;
    return 0;
}

/* ARGSUSED */
static int
setCursor(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Cursor cursor;

    TRACE(("imlib:setCursor()\n"));

    CHECK_ICATTR_SIZE(4, IMBadCursor);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    cursor = getC32(value, order);
    TRACE(("\tcursor: %08lx\n", cursor));

    if (!(ap->set_mask & ATTR_MASK_CURSOR) || cursor != ap->cursor) {
	ap->change_mask |= ATTR_MASK_CURSOR;
    }
    ap->cursor = cursor;
    ap->set_mask |= ATTR_MASK_CURSOR;
    return 0;
}

/* ARGSUSED */
static int
setSpotLocation(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    XPoint spot;

    TRACE(("imlib:setSpotLocation()\n"));

    CHECK_ICATTR_SIZE(4, IMBadSpotLocation);

    if (nest == NEST_STATUS) {
	DPRINT(("spot location specified in a status attribute list\n"));
	IMSendError(icp->im->connection, IMBadSpotLocation,
		    icp->im->id, icp->id,
		    "spot location isn't a status attribute");
	return -1;
    }

    ap = &icp->preedit_attr;

    spot.x = getI16(value, order);
    spot.y = getI16(value + 2, order);
    TRACE(("\tspot location: %d, %d\n", spot.x, spot.y));

    if (!(ap->set_mask & ATTR_MASK_SPOT_LOCATION) ||
	spot.x != ap->spot_location.x || spot.y != ap->spot_location.y) {
	ap->change_mask |= ATTR_MASK_SPOT_LOCATION;
    }
    ap->spot_location.x = spot.x;
    ap->spot_location.y = spot.y;
    ap->set_mask |= ATTR_MASK_SPOT_LOCATION;
    return 0;
}

/* ARGSUSED */
static int
setStdColormap(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    Atom colormap_name;
    XStandardColormap *stdcolormaps;
    Widget w = icp->im->connection->proto_widget;
    Display *dpy = XtDisplay(w);
    int ncolormaps;
    Window root;
    XAEHandle h;
    int status;

    TRACE(("imlib:setStdColormap()\n"));

    CHECK_ICATTR_SIZE(4, IMBadAtom);

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    colormap_name = getC32(value, order);
    if (icp->common_attr.set_mask & ATTR_MASK_CLIENT) {
	root = icp->client_profile.root;
    } else if (icp->common_attr.set_mask & ATTR_MASK_FOCUS) {
	root = icp->focus_profile.root;
    } else {
	/*
	 * Client has not specified client window yet.
	 * Reading standard colormap property should been deffered
	 * until the window is set, but for now...
	 */
	DDPRINT(2, ("std colormap specified, leaving client window unspecified\n"));
	root = RootWindowOfScreen(XtScreen(w));
    }

    h = XAESetIgnoreErrors(dpy);
    status = XGetRGBColormaps(dpy, root,
			      &stdcolormaps, &ncolormaps, colormap_name);
    XAEUnset(h);
    if (!status || ncolormaps < 0) {
	DPRINT(("can't get standard colormap (%ld)\n", colormap_name));
	IMSendError(icp->im->connection, IMBadName, icp->im->id, icp->id,
		"invalid standard colormap name");
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_STD_COLORMAP) ||
	colormap_name != ap->std_colormap) {
	ap->change_mask |= ATTR_MASK_STD_COLORMAP;
    }
    ap->std_colormap = colormap_name;
    ap->colormap = stdcolormaps[0].colormap;
    TRACE(("\tstandard colormap: %ld (colormap=%08lx)\n",
	   colormap_name, ap->colormap));

    ap->set_mask |= ATTR_MASK_STD_COLORMAP | ATTR_MASK_COLORMAP;
    XFree((char *)stdcolormaps);
    return 0;
}

/* ARGSUSED */
static int
setFontSet(icp, value, len, order, nest, op)
IMIC *icp;
char *value;
int len;
int order;
int nest;
int op;
{
    IMPSAttributes *ap;
    unsigned int name_list_len;
    char *name_list;

    TRACE(("imlib:setFontSet()\n"));

    if (len < 2) {
	badSizeError(icp, IMBadName);
	return -1;
    }
    name_list_len = getC16(value, order);
    if (2 + name_list_len > len) {
	badSizeError(icp, IMBadName);
	return -1;
    }

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	unnestedError(icp);
	return -1;
    }

    name_list = XtMalloc(name_list_len + 1);
    bcopy(value + 2, name_list, name_list_len);
    name_list[name_list_len] = '\0';
    TRACE(("\tfontset: %s\n", name_list));

    if (ap->set_mask & ATTR_MASK_FONT_SET) {
	if (!strcmp(name_list, ap->font_set)) {
	    XtFree(name_list);
	} else {
	    ap->change_mask |= ATTR_MASK_FONT_SET;
	    if (ap->font_set != IMDefaultFontSet(icp->im)) {
		XtFree(ap->font_set);
	    }
	    ap->font_set = name_list;
	}
    } else {
	ap->font_set = name_list;
	ap->set_mask |= ATTR_MASK_FONT_SET;
	ap->change_mask |= ATTR_MASK_FONT_SET;
    }
    return 0;
}


/*
 * Functions getting IC attributes
 */

static int
getICValues(icp, data, len, nest, offset, sepp)
IMIC *icp;
char *data;
int len;
int nest;		/* NEST_NONE, NEST_PREEDIT or NEST_STATUS */
int offset;		/* request offset */
int *sepp;		/* Out: true if ended with a nested list separator */
{
    unsigned int id;		/* attribute ID */
    ICAttribute *attrp;
    IMConnection *conn = icp->im->connection;
    int byte_order = conn->byte_order;
    char *org_data = data;
    int r;

    TRACE(("imlib:getICValues()\n"));

    while (len >= 2) {
	id = getC16(data, byte_order);
	data += 2;
	len -= 2;

	if (id > numIcAttributes) {
	    /* invalid attribute ID */
	    DPRINT(("invalid IC attribute ID (%d) specified\n", id));
	    IMCancelRequest(conn, offset);
	    IMSendError(conn, IMBadSomething, icp->im->id, icp->id,
			"invalid IC attribute ID");
	    return -1;
	}

	attrp = &icAttributes[id];
	if (attrp->type == TYPE_SEPARATOR) {
	    /* nested list separator */
	    *sepp = 1;
	    return data - org_data;
	}

	if (!(attrp->valid_ops & OP_G)) {
	    DPRINT(("invalid operation (get) for IC attr %d\n", id));
	    IMCancelRequest(conn, offset);
	    IMSendError(conn, IMBadSomething, icp->im->id, icp->id,
			"invalid operation (ICGetValues) for this attribute");
	    return -1;
	}

	r = (*attrp->get_proc)(icp, id, nest, offset, data, len);
	/*
	 * The return value of get_proc is usually 0, indicating success.
	 * If it is less than 0, there are some errors.
	 * If it is greater than 0,
	 */
	if (r < 0) return -1;

	data += r;		/* r is extra offset */
	len -= r;
    }
    *sepp = 0;
    return data - org_data;
}

/* ARGSUSED */
static int
getPSAttributes(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    unsigned int length;
    int length_offset;
    int attr_offset;
    int nested_separator;
    int r;

    IMPutC16(conn, id);

    length_offset = IMWritePos(conn);
    IMPutC16(conn, 0);		/* dummy -- overwritten afterwards */

    attr_offset = IMWritePos(conn);

    r = getICValues(icp, data, len, nest, offset, &nested_separator);
    if (r < 0) return -1;
    if (!nested_separator) {
	/* there's no nested list separator */
	DPRINT(("nested list doesn't end with separator\n"));
	/*
	 * X11R6 Xlib sends nested attribute list which has no
	 * separator at its end.  In order to accommodate to it,
	 * don't send error for that.
	 */
#ifdef notdef
	IMCancelRequest(conn, offset);
	IMSendError(conn, IMBadSomething, icp->im->id, icp->id,
		    "corrupted nested list");
	return -1;
#endif
    }

    /*
     * Nested list is written on the output buffer.
     * Calculate the length of the list.
     */
    length = IMWritePos(conn) - attr_offset;

    /* rewrite attribute length field */
    IMRewriteC16(conn, length_offset, length);
    IMPutPad(conn);

    return r;
}

/* ARGSUSED */
static int
getPreeditAttributes(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;		/* unused */
int offset;
char *data;
int len;
{
    TRACE(("imlib:getPreeditAttributes()\n"));
    return getPSAttributes(icp, id, NEST_PREEDIT, offset, data, len);
}

/* ARGSUSED */
static int
getStatusAttributes(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;		/* unused */
int offset;
char *data;
int len;
{
    TRACE(("imlib:getStatusAttributes()\n"));
    return getPSAttributes(icp, id, NEST_STATUS, offset, data, len);
}

/* ARGSUSED */
static int
getInputStyle(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;

    TRACE(("imlib:getInputStyle()\n"));

    /*
     * Input style must have been specified, (and validated)
     * at IC creation.  No need for checking.
     */
    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, icp->common_attr.input_style);
    return 0;
}

/* ARGSUSED */
static int
getClientWindow(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;

    TRACE(("imlib:getClientWindow()\n"));

    if (icp->common_attr.set_mask & ATTR_MASK_CLIENT) {
	IMPutC16(conn, id);		/* attribute ID */
	IMPutC16(conn, 4);		/* value length */
	IMPutC32(conn, icp->common_attr.client);
	return 0;
    } else {
	/* no default is available */
	DPRINT(("getClientWindow without setting client window previously\n"));
	IMCancelRequest(conn, offset);
	IMSendError(conn, IMBadClientWindow, icp->im->id, icp->id,
		    "client window not specified yet");
	return -1;
    }
}

/* ARGSUSED */
static int
getFocusWindow(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;

    TRACE(("imlib:getFocusWindow()\n"));

    if (!(icp->common_attr.set_mask & ATTR_MASK_FOCUS)) {
	/* fill default value */
	fillCommonDefault(icp, (unsigned long)ATTR_MASK_FOCUS);
    }

    if (icp->common_attr.set_mask & ATTR_MASK_FOCUS) {
	IMPutC16(conn, id);		/* attribute ID */
	IMPutC16(conn, 4);		/* value length */
	IMPutC32(conn, icp->common_attr.focus);
	return 0;
    } else {
	/*
	 * Couldn't get the default value.  That is, neither
	 * focus window nor client window is specified yet.
	 */
	DPRINT(("getFocusWindow without setting focus/client window previously\n"));
	IMCancelRequest(conn, offset);
	IMSendError(conn, IMBadFocusWindow, icp->im->id, icp->id,
		    "neither of client/focus window not specified yet");
	return -1;
    }
}

/* ARGSUSED */
static int
getFilterEvents(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;

    TRACE(("imlib:getFilterEvents()\n"));

    /* We need only Key events */
    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, KeyPressMask | KeyReleaseMask);
    return 0;
}

/* ARGSUSED */
static int
getArea(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getArea()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_AREA)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_AREA);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 8);		/* value length */
    IMPutI16(conn, ap->area.x);
    IMPutI16(conn, ap->area.y);
    IMPutC16(conn, ap->area.width);
    IMPutC16(conn, ap->area.height);
    return 0;
}

/* ARGSUSED */
static int
getAreaNeeded(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getAreaNeeded()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    /*
     * Always call fillPSDefault to get appropriate AreaNeeded value.
     */
    fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_AREA_NEEDED);

    TRACE(("\tarea needed: %d, %d, %d, %d\n",
	   ap->area_needed.x, ap->area_needed.y,
	   ap->area_needed.width, ap->area_needed.height));

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 8);		/* value length */
    IMPutI16(conn, ap->area_needed.x);
    IMPutI16(conn, ap->area_needed.y);
    IMPutC16(conn, ap->area_needed.width);
    IMPutC16(conn, ap->area_needed.height);
    return 0;
}

/* ARGSUSED */
static int
getSpotLocation(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap = &icp->preedit_attr;

    TRACE(("imlib:getSpotLocation()\n"));

    if (nest == NEST_STATUS) {
	IMCancelRequest(conn, offset);
	IMSendError(conn, IMBadSomething, icp->im->id, icp->id,
		    "spot location isn't a status attribute");
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_SPOT_LOCATION)) {
	fillPSDefault(icp, NEST_PREEDIT,
		      (unsigned long)ATTR_MASK_SPOT_LOCATION);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutI16(conn, ap->spot_location.x);
    IMPutI16(conn, ap->spot_location.y);
    return 0;
}

/* ARGSUSED */
static int
getColormap(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getColormap()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_COLORMAP)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_COLORMAP);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, ap->colormap);
    return 0;
}

/* ARGSUSED */
static int
getStdColormap(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;
    Atom colormap_name;

    TRACE(("imlib:getStdColormap()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (ap->set_mask & ATTR_MASK_STD_COLORMAP) {
	colormap_name = ap->std_colormap;
    } else {
	/* what to do? */
	colormap_name = None;
	DPRINT(("client asks standard colormap, but not specified\n"));
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, colormap_name);
    return 0;
}

/* ARGSUSED */
static int
getForeground(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getForeground()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_FOREGROUND)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_FOREGROUND);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, ap->foreground);
    return 0;
}

/* ARGSUSED */
static int
getBackground(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getBackground()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_BACKGROUND)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_BACKGROUND);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, ap->background);
    return 0;
}

/* ARGSUSED */
static int
getBgPixmap(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getBgPixmap()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_BG_PIXMAP)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_BG_PIXMAP);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, ap->bg_pixmap);
    return 0;
}

/* ARGSUSED */
static int
getFontSet(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;
    int name_len;

    TRACE(("imlib:getFontSet()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_FONT_SET)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_FONT_SET);
    }

    name_len = strlen(ap->font_set);

    IMPutC16(conn, id);		/* attribute ID */

    IMPutC16(conn, (unsigned int)name_len);	/* value length */
    IMPutString(conn, ap->font_set, name_len);
    IMPutPad(conn);
    return 0;
}

/* ARGSUSED */
static int
getLineSpace(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getLineSpace()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_LINESPACE)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_LINESPACE);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);	/* value length */
    IMPutC32(conn, (unsigned long)ap->line_space);
    return 0;
}

/* ARGSUSED */
static int
getCursor(icp, id, nest, offset, data, len)
IMIC *icp;
unsigned int id;
int nest;
int offset;
char *data;
int len;
{
    IMConnection *conn = icp->im->connection;
    IMPSAttributes *ap;

    TRACE(("imlib:getCursor()\n"));

    if ((ap = getPSPtr(icp, nest)) == NULL) {
	IMCancelRequest(conn, offset);
	unnestedError(icp);
	return -1;
    }

    if (!(ap->set_mask & ATTR_MASK_CURSOR)) {
	fillPSDefault(icp, nest, (unsigned long)ATTR_MASK_CURSOR);
    }

    IMPutC16(conn, id);		/* attribute ID */
    IMPutC16(conn, 4);		/* value length */
    IMPutC32(conn, ap->cursor);
    return 0;
}

static void
changeFonts(icp, preedit)
IMIC *icp;
int preedit;
{
    FontBank bank = IMFontBank(icp->im);
    String font_set;
    XFontStruct ***fontsp;
    int *num_fontsp;

    TRACE(("imlib:changeFonts()\n"));

    if (preedit) {
	font_set = icp->preedit_attr.font_set;
	fontsp = &icp->fonts;
	num_fontsp = &icp->num_fonts;
    } else {
	font_set = icp->status_attr.font_set;
	fontsp = &icp->status_fonts;
	num_fontsp = &icp->num_status_fonts;
    }

    /*
     * Unload previous fonts.
     */
    if (*num_fontsp > 0) {
	FontBankFreeFonts(bank, *fontsp, *num_fontsp);
    }

    /*
     * Load new fonts and store them in the IC structure.
     */
    *fontsp = FontBankGet(bank, font_set, num_fontsp);
}


/*
 * Functions computing default attribute values
 */

static void
fillCommonDefault(icp, mask)
IMIC *icp;
unsigned long mask;
{
    IMCommonAttributes *ap = &icp->common_attr;

    TRACE(("imlib:fillCommonDefault()\n"));

    /*
     * Don't bother with the attributes which have been set.
     */
    mask &= ~ap->set_mask;

    /*
     * The only attribute that have default value is FocusWindow.
     */
    if (mask & ATTR_MASK_FOCUS) {
	/* if ClientWindow is not set... no way */
	if (mask & ATTR_MASK_CLIENT) {
	    ap->focus = ap->client;
	    ap->set_mask |= ATTR_MASK_FOCUS;
	    icp->focus_profile = icp->client_profile;
	    TRACE(("\tdefault focus window: %08lx\n", ap->focus));
	}
    }
}

static int
getNaturalLineSpace(icp, preedit)
IMIC *icp;
int preedit;
{
    XFontStruct **fonts;
    int num_fonts;
    int max_ascent = 0, max_descent = 0;
    int i;

    changeFonts(icp, preedit);
    if (preedit) {
	fonts = icp->fonts;
	num_fonts = icp->num_fonts;
    } else {
	fonts = icp->status_fonts;
	num_fonts = icp->num_status_fonts;
    }

    for (i = 0; i < num_fonts; i++) {
	XFontStruct *font = fonts[i];
	if (max_ascent < font->ascent) max_ascent = font->ascent;
	if (max_descent < font->descent) max_descent = font->descent;
    }

    if (max_ascent + max_descent < MIN_LINE_SPACING) {
	return MIN_LINE_SPACING;
    } else {
	return max_ascent + max_descent;
    }
}

static void
fillPSDefault(icp, type, mask)
IMIC *icp;
int type;	/* NEST_PREEDIT or NEST_STATUS */
unsigned long mask;
{
    IMPSAttributes *ap;
    IMConnection *conn = icp->im->connection;
    Widget pw = conn->proto_widget;
    int preedit;
#ifdef DEBUG
    char *typename = (type == NEST_PREEDIT) ? "preedit" : "status";
#endif

    TRACE(("imlib:fillPSDefault(%s)\n", typename));

    preedit = (type == NEST_PREEDIT);
    ap = preedit ? &icp->preedit_attr : &icp->status_attr;

    /*
     * Don't bother with the attributes which have been set.
     * But area_needed needs to be computed each time (to get
     * correct X and Y coordinates).
     */
    mask &= ~ap->set_mask | ATTR_MASK_AREA_NEEDED;

    if (mask & ATTR_MASK_AREA) {
	computeAreaForQuery(icp);
	ap->set_mask |= ATTR_MASK_AREA;
	DDPRINT(5, ("\tdefault %s area: %d,%d,%d,%d\n", typename,
		    ap->area.x, ap->area.y, ap->area.width, ap->area.height));
    }
    if (mask & ATTR_MASK_FOREGROUND) {
	ap->foreground = IMDefaultForeground(pw);
	ap->set_mask |= ATTR_MASK_FOREGROUND;
	DDPRINT(5, ("\tdefault %s foreground: %ld\n",
		    typename, ap->foreground));
    }
    if (mask & ATTR_MASK_BACKGROUND) {
	ap->background = IMDefaultBackground(pw);
	ap->set_mask |= ATTR_MASK_BACKGROUND;
	DDPRINT(5, ("\tdefault %s background: %ld\n",
		    typename, ap->background));
    }
    if (mask & ATTR_MASK_COLORMAP) {
	ap->colormap = pw->core.colormap;
	ap->set_mask |= ATTR_MASK_COLORMAP;
	DDPRINT(5, ("\tdefault %s colormap: %08lx\n",
		    typename, ap->colormap));
    }
    if (mask & ATTR_MASK_STD_COLORMAP) {
	/* you can't fill default. what to do? */
	DDPRINT(5, ("\tdefault %s std colormap: N/A\n", typename));
    }
    if (mask & ATTR_MASK_BG_PIXMAP) {
	ap->bg_pixmap = None;
	ap->set_mask |= ATTR_MASK_BG_PIXMAP;
	DDPRINT(5, ("\tdefault %s background pixmap: None\n", typename));
    }
    if (mask & ATTR_MASK_LINESPACE) {
	if (!(ap->set_mask & ATTR_MASK_FONT_SET)) {
	    fillPSDefault(icp, type, (unsigned long)ATTR_MASK_FONT_SET);
	}
	ap->line_space = getNaturalLineSpace(icp, type == NEST_PREEDIT);
	ap->set_mask |= ATTR_MASK_LINESPACE;
	DDPRINT(5, ("\tdefault line space: %d\n", ap->line_space));
    }
    if (mask & ATTR_MASK_CURSOR) {
	ap->cursor = None;
	ap->set_mask |= ATTR_MASK_CURSOR;
	DDPRINT(5, ("\tdefault %s cursor: None\n", typename));
    }
    if (mask & ATTR_MASK_AREA_NEEDED) {
	computeAreaNeeded(icp);
	DDPRINT(5, ("\t%s area_needed: %d,%d,%d,%d\n", typename,
		    ap->area_needed.x, ap->area_needed.y,
		    ap->area_needed.width, ap->area_needed.height));
    }
    if (mask & ATTR_MASK_FONT_SET) {
	ap->font_set = IMDefaultFontSet(icp->im);
	ap->set_mask |= ATTR_MASK_FONT_SET;
	DDPRINT(5, ("\tdefault %s fontset: %s\n", typename, ap->font_set));
    }
    if (mask & ATTR_MASK_SPOT_LOCATION) {
	ap->spot_location.x = ap->spot_location.y = 0;
	ap->set_mask |= ATTR_MASK_SPOT_LOCATION;
	DDPRINT(5, ("\tdefault spot location: %d, %d\n",
		    ap->spot_location.x, ap->spot_location.y));
    }
}

/*
 * Function validating attribute values
 */

static int
validateCommonAttr(icp, checkonly)
IMIC *icp;
int checkonly;
{
    IMCommonAttributes *ap = &icp->common_attr;
    IMConnection *conn = icp->im->connection;
    unsigned long mask = ap->change_mask;
    int ret = 0;

    TRACE(("imlib:validateCommonAttr()\n"));

    mask &= ap->set_mask;

#define SENDERROR(code, msg) \
    if (!checkonly && ret == 0) { \
	IMSendError(conn, code, icp->im->id, icp->id, msg); ret = -1; \
    }

    if (mask & ATTR_MASK_INPUT_STYLE) {
	XIMStyle xstyle = icp->common_attr.input_style;
	InputStyle *isp = styles;

	while (isp->xim_style != 0) {
	    if (isp->xim_style == xstyle) break;
	    isp++;
	}
	if (isp->xim_style == 0) {
	    DPRINT(("unsupported input style\n"));
	    SENDERROR(IMBadStyle, "unsupported input style");
	} else {
	    icp->style = isp->conversion_style;
	}
    }
    if (mask & ATTR_MASK_CLIENT) {
	if (!validateClientWindow(icp)) {
	    DPRINT(("invalid client window ID\n"));
	    SENDERROR(IMBadClientWindow, "invalid client window ID");
	}
    }
    if (mask & ATTR_MASK_FOCUS) {
	if (!validateFocusWindow(icp)) {
	    DPRINT(("invalid focus window ID\n"));
	    SENDERROR(IMBadFocusWindow, "invalid focus window ID");
	}
    }

    return ret;
#undef SENDERROR
}

static int
validatePSAttr(icp, type, checkonly)
IMIC *icp;
int type;
int checkonly;
{
    /*
     * Check validity of preedit/status attribute values.
     * 'type' is either NEST_PREEDIT or NEST_STATUS, indicating
     * whether preedit or status attributes are to be checked.
     * 'mask' is the attribute mask to be checked.
     * If all the attributes are valid, this function return 0.
     * Otherwise it issues an error message for the first invalid
     * value detected, and returns -1.
     */
    IMPSAttributes *ap;
    IMConnection *conn = icp->im->connection;
    unsigned long mask;
    int preedit;
    int ret = 0;

    TRACE(("imlib:validatePSAttr()\n"));

    preedit = (type == NEST_PREEDIT);
    ap = preedit ? &icp->preedit_attr : &icp->status_attr;

    /* do not check unset attributes */
    mask = ap->change_mask & ap->set_mask;

#define SENDERROR(code, msg) \
    if (!checkonly && ret == 0) { \
	IMSendError(conn, code, icp->im->id, icp->id, msg); ret = -1; \
    }

    if (mask & ATTR_MASK_AREA) {
	if (ap->area.width == 0 || ap->area.height == 0) {
	    ap->set_mask &= ~ATTR_MASK_AREA;
	    DPRINT(("zero area width/height\n"));
	    SENDERROR(IMBadArea, "invalid area width/height");
	}
    }

#ifdef notdef
    if (mask & ATTR_MASK_COLORMAP) {
    }

    if (mask & ATTR_MASK_BG_PIXMAP) {
    }
#endif

    if (mask & ATTR_MASK_LINESPACE) {
	if (ap->line_space < MIN_LINE_SPACING) {
	    ap->set_mask &= ~ATTR_MASK_LINESPACE;
	    /*
	     * we don't send error message in this case, because
	     * there exist some applications which send invalid line
	     * spacing and we don't want to break them.
	     */
	    DPRINT(("line space too small %d\n", ap->line_space));
	}
    }

#ifdef notdef
    if (mask & ATTR_MASK_CURSOR) {
	/* How should we check it? */
    }
#endif

    if (mask & ATTR_MASK_AREA_NEEDED) {
    }
#ifdef notdef
    if (mask & ATTR_MASK_FONT_SET) {
    }
    if (mask & ATTR_MASK_SPOT_LOCATION) {
    }
#endif

    return ret;
#undef SENDERROR
}


/*
 * Functions to extract necessary attributes and make conversion argument
 */

static void
changeConversionAttributes(icp)
IMIC *icp;
{
    unsigned long mask;
    ConversionAttributes attr;

    TRACE(("imlib:changeConversionAttributes()\n"));
    mask = IMMakeConvAttributes(icp, &attr);
    CControlChangeAttributes(icp->conversion, mask, &attr);
}

static void
computeAreaNeeded(icp)
IMIC *icp;
{
    IMPSAttributes *pattr = &icp->preedit_attr;
    IMPSAttributes *sattr = &icp->status_attr;
    IMWindowProfile *cpr = &icp->client_profile;
    int width, height;
    int min_width, min_height;
    int default_status_width;
    int font_height;

    TRACE(("computeAreaNeeded()\n"));

    if (icp->style == IMSTYLE_SEPARATE) return;

    /*
     * Get the current dimension of the client window.
     */
    (void)validateClientWindow(icp);

    /*
     * Compute the dimensions of the status region.
     */
    fillPSDefault(icp, NEST_STATUS, (unsigned long)ATTR_MASK_LINESPACE);
    font_height = sattr->line_space + 2;
    width = height = 0;
    if (sattr->set_mask & ATTR_MASK_AREA_NEEDED) {
	width = sattr->area_needed.width;
	height = sattr->area_needed.height;
	TRACE(("\tstatus areaNeeded was: (%d,%d)\n", width, height));
    }

    min_width = font_height * 3;
    min_height = font_height;
    if (min_width < MIN_AREA_WIDTH) min_width = MIN_AREA_WIDTH;
    if (min_height < MIN_AREA_HEIGHT) min_height = MIN_AREA_HEIGHT;

    if (width == 0) {
	default_status_width =
	    IMStatusWidth(icp->im->connection->proto_widget);
	if (default_status_width > 0) {
	    width = default_status_width;
	} else {
	    width = cpr->width / 5;		/* wild guess */
	}
    }
    
    if (width < min_width) width = min_width;
    if (height < min_height) height = min_height;
    
    sattr->area_needed.x = 0;
    sattr->area_needed.y = cpr->height - height;
    sattr->area_needed.width = width;
    sattr->area_needed.height = height;
    TRACE(("\tstatus areaNeeded is now: (%d, %d, %d, %d)\n",
	    sattr->area_needed.x, sattr->area_needed.y, width, height));

    /*
     * Compute the dimensions of the pre-edit region.
     */
    if (icp->style != IMSTYLE_OFF_THE_SPOT) return;

    fillPSDefault(icp, NEST_PREEDIT, (unsigned long)ATTR_MASK_LINESPACE);
    font_height = pattr->line_space + 2;
    width = height = 0;
    if (pattr->set_mask & ATTR_MASK_AREA_NEEDED) {
	width = pattr->area_needed.width;
	height = pattr->area_needed.height;
	TRACE(("\tpreedit areaNeeded was: (%d,%d)\n",
		width, height));
    }

    min_width = (cpr->width - sattr->area_needed.width) / 2;
    min_height = font_height;
    if (min_width < MIN_AREA_WIDTH) min_width = MIN_AREA_WIDTH;
    if (min_height < MIN_AREA_HEIGHT) min_height = MIN_AREA_HEIGHT;

    if (width < min_width) width = min_width;
    if (height < min_height) height = min_height;

    pattr->area_needed.x = sattr->area_needed.width;
    pattr->area_needed.y = cpr->height - height;
    pattr->area_needed.width = width;
    pattr->area_needed.height = height;
    TRACE(("\tpreedit areaNeeded is now: (%d, %d, %d, %d)\n",
	    pattr->area_needed.x, pattr->area_needed.y, width, height));
}

static void
computeAreaForQuery(icp)
IMIC *icp;
{
    IMPSAttributes *pattr = &icp->preedit_attr;
    IMPSAttributes *sattr = &icp->status_attr;

    TRACE(("computeAreaForQuery()\n"));

    if (icp->style == IMSTYLE_SEPARATE) return;

    computeAreaNeeded(icp);

    if (!(pattr->set_mask & ATTR_MASK_AREA)) {
	if (icp->style == IMSTYLE_OVER_THE_SPOT) {
	    IMWindowProfile *fpr = &icp->focus_profile;

	    pattr->area.x = 0;
	    pattr->area.y = 0;
	    pattr->area.width = fpr->width;
	    pattr->area.height = fpr->height;
	} else {	/* IMSTYLE_OFF_THE_SPOT */
	    pattr->area = pattr->area_needed;
	}
    }

    if (!(sattr->set_mask & ATTR_MASK_AREA)) {
	sattr->area = sattr->area_needed;
    }
}


/*
 * Public functions
 */

/*- IMPutIMAttrList: write list of supported IM attributes to output buffer -*/
void
IMPutIMAttrList(imp)
IMIM *imp;
{
    IMConnection *conn = imp->connection;
    IMAttribute *iap;
    int offset, list_start, list_end;
    int n;

    TRACE(("IMPutIMAttrList()\n"));

    offset = IMWritePos(conn);
    IMPutC16(conn, 0);		/* dummy. overwritten afterwards */

    list_start = IMWritePos(conn);
    for (n = 0, iap = imAttributes; n < numImAttributes; n++, iap++) {
	int length;

	IMPutC16(conn, (unsigned int)n);
	IMPutC16(conn, (unsigned int)iap->type);
	length = strlen(iap->name);
	IMPutC16(conn, (unsigned int)length);
	IMPutString(conn, iap->name, length);
	IMPutPad(conn);
    }
    list_end = IMWritePos(conn);
    IMRewriteC16(conn, offset, (unsigned int)(list_end - list_start));
}

/*- IMPutICAttrList: write list of supported IC attributes to output buffer -*/
void
IMPutICAttrList(imp)
IMIM *imp;
{
    IMConnection *conn = imp->connection;
    ICAttribute *iap;
    int offset, list_start, list_end;
    int n;

    TRACE(("IMPutICAttrList()\n"));

    offset = IMWritePos(conn);
    IMPutC16(conn, 0);		/* dummy. overwritten afterwards */
    IMPutC16(conn, 0);		/* unused */

    list_start = IMWritePos(conn);
    for (n = 0, iap = icAttributes; n < numIcAttributes; n++, iap++) {
	int length;

	IMPutC16(conn, (unsigned int)n);
	IMPutC16(conn, (unsigned int)iap->type);
	length = strlen(iap->name);
	IMPutC16(conn, (unsigned int)length);
	IMPutString(conn, iap->name, length);
	IMPutPad(conn);
    }
    list_end = IMWritePos(conn);
    IMRewriteC16(conn, offset, (unsigned int)(list_end - list_start));
}

/* ARGSUSED */
int
IMSetIMValues(imp, data, len, major)
IMIM *imp;
char *data;
int len;
int major;
{
    TRACE(("IMSetIMValues(): not supported yet\n"));
    /* not supported yet */

    IMSendError(imp->connection, IMBadProtocol, imp->id, 0,
		"this protocol is not supported yet");
    return -1;
}

int
IMGetIMValues(imp, data, len, offset)
IMIM *imp;
char *data;
int len;
int offset;		/* request offset */
{
    IMConnection *conn = imp->connection;
    int pos, list_start, list_end;

    TRACE(("IMGetIMValues()\n"));

    pos = IMWritePos(conn);
    IMPutC16(conn, 0);		/* length of the list. to be overwritten. */

    list_start = IMWritePos(conn);

    if (getIMValues(imp, data, len, offset) < 0) return -1;

    list_end = IMWritePos(conn);
    IMRewriteC16(conn, pos, (unsigned int)(list_end - list_start));
    return 0;
}

int
IMSetICValues(icp, data, len, major)
IMIC *icp;
char *data;
int len;
int major;
{
    int r1, r2, r3;

    TRACE(("IMSetICValues()\n"));

    /* clear change mask */
    icp->common_attr.change_mask = 0;
    icp->preedit_attr.change_mask = 0;
    icp->status_attr.change_mask = 0;

    /* read the specified data and set attributes */
    r1 = setICValues(icp, data, len, 0,
		     (major == XIM_CREATE_IC) ? OP_C : OP_S);

    /* validate attributes */
    r2 = IMValidateICAttributes(icp, (r1 < 0));

    /*
     * If the operation is CREATE_IC, input style attribute must be set.
     */
    if (major == XIM_CREATE_IC &&
	!(icp->common_attr.set_mask & ATTR_MASK_INPUT_STYLE) &&
	r1 == 0 && r2 == 0) {
	DPRINT(("input style not specified by CreateIC\n"));
	IMSendError(icp->im->connection, IMBadSomething, icp->im->id, icp->id,
		    "inputStyle resource must be set");
	r3 = -1;
    } else {
	r3 = 0;
    }

    /* if conversion is taking place... */
    if (icp->state & IC_CONVERTING) {
	changeConversionAttributes(icp);
    }

    return (r1 < 0 || r2 < 0 || r3 < 0) ? -1 : 0;
}

int
IMGetICValues(icp, data, len, offset)
IMIC *icp;
char *data;
int len;
int offset;		/* request offset */
{
    int nested_separator;
    int r;
    IMConnection *conn = icp->im->connection;
    int pos, list_start, list_end;

    TRACE(("IMGetICValues()\n"));

    pos = IMWritePos(conn);
    IMPutC16(conn, 0);		/* dummy. overwritten afterwards */
    IMPutC16(conn, 0);		/* unused */

    list_start = IMWritePos(conn);

    r = getICValues(icp, data, len, NEST_NONE, offset, &nested_separator);
    if (r < 0) return -1;

    list_end = IMWritePos(conn);
    IMRewriteC16(conn, pos, (unsigned int)(list_end - list_start));

    if (nested_separator) {
	/*
	 * There must have been some unbalanced NestedListSeparator.
	 */
	DPRINT(("getICvalues: unmatched separator\n"));
	IMCancelRequest(icp->im->connection, offset);
	IMSendError(icp->im->connection, IMBadSomething, icp->im->id, icp->id,
		    "corrupted nested list");
	return -1;
    }
    return 0;
}

void
IMFillDefault(icp, common_mask, preedit_mask, status_mask)
IMIC *icp;
unsigned long common_mask;
unsigned long preedit_mask;
unsigned long status_mask;
{
    TRACE(("IMFillDefault()\n"));

    if (common_mask != 0) fillCommonDefault(icp, common_mask);
    if (preedit_mask != 0) fillPSDefault(icp, NEST_PREEDIT, preedit_mask);
    if (status_mask != 0) fillPSDefault(icp, NEST_STATUS, status_mask);
}

int
IMValidateWindow(dpy, win, profilep)
Display *dpy;
Window win;
IMWindowProfile *profilep;
{
    Window root;
    int x, y;
    unsigned int width, height, border, depth;
    XAEHandle h;
    int s;

    TRACE(("IMValidateWindow(win: %08lx)\n", win));

    h = XAESetIgnoreErrors(dpy);
    s = XGetGeometry(dpy, win, &root, &x, &y,
		     &width, &height, &border, &depth);
    XAEUnset(h);

    if (profilep != NULL && s) {
	profilep->width = width;
	profilep->height = width;
	profilep->root = root;
    }
    return s;
}

int
IMValidateICAttributes(icp, checkonly)
IMIC *icp;
int checkonly;
{
    int error_occured;
    int r;

    TRACE(("IMValidateICAttributes()\n"));

    /*
     * Be careful not to send multiple error messages.
     */

    error_occured = 0;

    r = validateCommonAttr(icp, checkonly);
    if (r < 0) error_occured = 1;

    r = validatePSAttr(icp, NEST_PREEDIT, (checkonly && error_occured));
    if (r < 0) error_occured = 1;

    r = validatePSAttr(icp, NEST_STATUS, (checkonly && error_occured));
    if (r < 0) error_occured = 1;

    return error_occured ? -1 : 0;
}

void
IMFreeICAttributes(icp)
IMIC *icp;
{
    IMPSAttributes *pattr = &icp->preedit_attr;
    IMPSAttributes *sattr = &icp->status_attr;
    FontBank bank = IMFontBank(icp->im);

    TRACE(("IMFreeICAttributes()\n"));

    if (pattr->set_mask & ATTR_MASK_FONT_SET) XtFree(pattr->font_set);
    if (sattr->set_mask & ATTR_MASK_FONT_SET) XtFree(sattr->font_set);

    if (icp->num_fonts > 0) {
	FontBankFreeFonts(bank, icp->fonts, icp->num_fonts);
    }
    if (icp->num_status_fonts > 0) {
	FontBankFreeFonts(bank, icp->status_fonts, icp->num_status_fonts);
    }
}

unsigned long
IMMakeConvAttributes(icp, attr)
IMIC *icp;
ConversionAttributes *attr;
{
    IMCommonAttributes *cattr;
    IMPSAttributes *pattr, *sattr;
    unsigned long cmask;		/* changed attributes */
    unsigned long mask;			/* attributes to be set */

    TRACE(("IMMakeConvAttributes()\n"));
    mask = 0L;

    cattr = &icp->common_attr;
    pattr = &icp->preedit_attr;
    sattr = &icp->status_attr;

    /*
     * Check changes of common attributes.
     */
    cmask = cattr->change_mask;

    /* focus window */
    if (cmask & ATTR_MASK_FOCUS) {
	attr->focuswindow = cattr->focus;
	mask |= CAFocusWindow;
    }

    /*
     * Check changes of preedit attributes.
     */
    cmask = pattr->change_mask;

    /* client area */
    if (cmask & ATTR_MASK_AREA) {
	attr->clientarea.x = pattr->area.x;
	attr->clientarea.y = pattr->area.y;
	attr->clientarea.width = pattr->area.width;
	attr->clientarea.height = pattr->area.height;
	mask |= CAClientArea;
    }

    /* foreground/background */
    if (cmask & ATTR_MASK_FOREGROUND) {
	attr->foreground = pattr->foreground;
	mask |= CAColor;
    }
    if (cmask & ATTR_MASK_BACKGROUND) {
	attr->background = pattr->background;
	mask |= CAColor;
    }

    /* colormap */
    if (cmask & ATTR_MASK_COLORMAP) {
	attr->colormap = pattr->colormap;
	mask |= CAColormap;
    }

    /* background pixmap */
    if (cmask & ATTR_MASK_BG_PIXMAP) {
	attr->background_pixmap = pattr->bg_pixmap;
	mask |= CABackgroundPixmap;
    }

    /* line spacing */
    if (cmask & ATTR_MASK_LINESPACE) {
	attr->linespacing = pattr->line_space;
	mask |= CALineSpacing;
    }

    /* cursor */
    if (cmask & ATTR_MASK_CURSOR) {
	attr->cursor = pattr->cursor;
	mask |= CACursor;
    }

    /* font */
    if (cmask & ATTR_MASK_FONT_SET) {
	changeFonts(icp, 1);
	attr->fonts = icp->fonts;
	attr->num_fonts = icp->num_fonts;
	mask |= CAFonts;
    }

    /* spot location */
    if ((cmask & ATTR_MASK_SPOT_LOCATION) &&
	icp->style == IMSTYLE_OVER_THE_SPOT) {
	attr->spotx = pattr->spot_location.x;
	attr->spoty = pattr->spot_location.y;
	mask |= CASpotLocation;
    }

    /*
     * Check changes of status attributes.
     */
    cmask = sattr->change_mask;

    /* status area */
    if (cmask & ATTR_MASK_AREA) {
	attr->statusarea.x = sattr->area.x;
	attr->statusarea.y = sattr->area.y;
	attr->statusarea.width = sattr->area.width;
	attr->statusarea.height = sattr->area.height;
	mask |= CAStatusArea;
    }

    /* font */
    if (cmask & ATTR_MASK_FONT_SET) {
	changeFonts(icp, 0);
	attr->status_fonts = icp->status_fonts;
	attr->num_status_fonts = icp->num_status_fonts;
	mask |= CAStatusFonts;
    }

    return mask;
}

void
IMMoveLocation(icp, x, y)
IMIC *icp;
int x;
int y;
{
    TRACE(("IMMoveLocation()\n"));

    icp->preedit_attr.spot_location.x = x;
    icp->preedit_attr.spot_location.y = y;
    icp->preedit_attr.set_mask |= ATTR_MASK_SPOT_LOCATION;
    icp->preedit_attr.change_mask = ATTR_MASK_SPOT_LOCATION;
    if (icp->state & IC_CONVERTING) {
	changeConversionAttributes(icp);
    }
}