aboutsummaryrefslogtreecommitdiff
path: root/libX11/specs/libX11/AppC.xml
blob: 7f4904c91197bbe47a27fb664a1dba2cbbfc736a (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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE appendix PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
	  "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<appendix id="extensions">
<title>Extensions</title>
<para>
<!-- .XE -->
Because X can evolve by extensions to the core protocol, 
it is important that extensions not be perceived as second-class citizens.
At some point, 
your favorite extensions may be adopted as additional parts of the 
X Standard.
</para>
<para>
<!-- .LP -->
Therefore, there should be little to distinguish the use of an extension from 
that of the core protocol.
To avoid having to initialize extensions explicitly in application programs, 
it is also important that extensions perform lazy evaluations,
automatically initializing themselves when called for the first time.  
</para>
<para>
<!-- .LP -->
This appendix describes techniques for writing extensions to Xlib that will
run at essentially the same performance as the core protocol requests.
</para>
<!-- .NT -->
<note><para>
It is expected that a given extension to X consists of multiple
requests.
Defining 10 new features as 10 separate extensions is a bad practice.
Rather, they should be packaged into a single extension
and should use minor opcodes to distinguish the requests.
</para></note>
<!-- .NE -->
<para>
<!-- .LP -->
The symbols and macros used for writing stubs to Xlib are listed in
<filename class="headerfile">&lt;X11/Xlibint.h&gt;</filename>.
</para>
<sect1 id="Basic_Protocol_Support_Routines">
<title>Basic Protocol Support Routines</title>
<para>
The basic protocol requests for extensions are 
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
and
<xref linkend='XListExtensions' xrefstyle='select: title'/>.
</para>
<indexterm significance="preferred"><primary>XQueryExtension</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XQueryExtension'>
<funcprototype>
  <funcdef>Bool <function>XQueryExtension</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>char<parameter> *name</parameter></paramdef>
  <paramdef>int<parameter> *major_opcode_return</parameter></paramdef>
  <paramdef>int<parameter> *first_event_return</parameter></paramdef>
  <paramdef>int<parameter> *first_error_return</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>display</term>
    <listitem>
      <para>Specifies the connection to the X server.</para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>name</term>
    <listitem>
      <para>Specifies the extension name.</para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>major_opcode_return</term>
    <listitem>
      <para>Returns the major opcode.</para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>first_event_return</term>
    <listitem>
      <para>Returns the first event code, if any.</para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>first_error_return</term>
    <listitem>
      <para>Returns the first error code, if any.</para>
    </listitem>
  </varlistentry>
</variablelist>

<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
function determines if the named extension is present.  
If the extension is not present,
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
returns
<symbol>False</symbol>;
otherwise, it returns
<symbol>True</symbol>.
If the extension is present,
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
returns the major opcode for the extension to major_opcode_return;
otherwise,
it returns zero.
Any minor opcode and the request formats are specific to the
extension.  
If the extension involves additional event types, 
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
returns the base event type code to first_event_return;
otherwise, 
it returns zero.  
The format of the events is specific to the extension.  
If the extension involves additional error codes, 
<xref linkend='XQueryExtension' xrefstyle='select: title'/>
returns the base error code to first_error_return;
otherwise, 
it returns zero.  
The format of additional data in the errors is specific to the extension.
</para>
<para>
<!-- .LP -->
If the extension name is not in the Host Portable Character Encoding
the result is implementation-dependent.
Uppercase and lowercase matter;
the strings ``thing'', ``Thing'', and ``thinG'' 
are all considered different names.
</para>
<indexterm significance="preferred"><primary>XListExtensions</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XListExtensions'>
<funcprototype>
  <funcdef>char **<function>XListExtensions</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> *nextensions_return</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nextensions_return</emphasis>
    </term>
    <listitem>
      <para>
Returns the number of extensions listed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XListExtensions' xrefstyle='select: title'/>
function returns a list of all extensions supported by the server.
If the data returned by the server is in the Latin Portable Character Encoding,
then the returned strings are in the Host Portable Character Encoding.
Otherwise, the result is implementation-dependent.
</para>
<indexterm significance="preferred"><primary>XFreeExtensionList</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XFreeExtensionList'>
<funcprototype>
  <funcdef><function>XFreeExtensionList</function></funcdef>
  <paramdef>char<parameter> **list</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>list</emphasis>
    </term>
    <listitem>
      <para>
Specifies the list of extension names.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XFreeExtensionList' xrefstyle='select: title'/>
function frees the memory allocated by 
<xref linkend='XListExtensions' xrefstyle='select: title'/>.
</para>
</sect1>
<sect1 id="Hooking_into_Xlib">
<title>Hooking into Xlib</title>
<para>
<!-- .LP -->
These functions allow you to hook into the library.  
They are not normally used by application programmers but are used 
by people who need to extend the core X protocol and
the X library interface.
The functions, which generate protocol requests for X, are typically
called stubs.
</para>
<para>
<!-- .LP -->
In extensions, stubs first should check to see if they have initialized 
themselves on a connection.
If they have not, they then should call 
<xref linkend='XInitExtension' xrefstyle='select: title'/>
to attempt to initialize themselves on the connection.
</para>
<para>
<!-- .LP -->
If the extension needs to be informed of GC/font allocation or
deallocation or if the extension defines new event types, 
the functions described here allow the extension to be 
called when these events occur.
</para>
<para>
<!-- .LP -->
The
<structname>XExtCodes</structname>
structure returns the information from 
<xref linkend='XInitExtension' xrefstyle='select: title'/>
and is defined in
<filename class="headerfile">&lt;X11/Xlib.h&gt;</filename>:
</para>
<para>
<!-- .LP -->
<indexterm significance="preferred"><primary>XExtCodes</primary></indexterm>
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<synopsis>
typedef struct _XExtCodes {	/* public to extension, cannot be changed */
	int extension;		/* extension number */
	int major_opcode;	/* major op-code assigned by server */
	int first_event;	/* first event number for the extension */
	int first_error;	/* first error number for the extension */
} XExtCodes;
</synopsis>
</para>
<!-- .LP -->
<!-- .eM -->
<indexterm significance="preferred"><primary>XInitExtension</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XInitExtension'>
<funcprototype>
  <funcdef>XExtCodes *<function>XInitExtension</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>char<parameter> *name</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>name</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension name.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XInitExtension' xrefstyle='select: title'/>
function determines if the named extension exists. 
Then, it allocates storage for maintaining the 
information about the extension on the connection, 
chains this onto the extension list for the connection,
and returns the information the stub implementor will need to access
the extension.
If the extension does not exist,
<xref linkend='XInitExtension' xrefstyle='select: title'/>
returns NULL.
</para>
<para>
<!-- .LP -->
If the extension name is not in the Host Portable Character Encoding,
the result is implementation-dependent.
Uppercase and lowercase matter;
the strings ``thing'', ``Thing'', and ``thinG'' 
are all considered different names.
</para>
<para>
<!-- .LP -->
The extension number in the 
<structname>XExtCodes</structname>
structure is
needed in the other calls that follow.  
This extension number is unique only to a single connection.
</para>
<!-- .LP -->
<indexterm significance="preferred"><primary>XAddExtension</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XAddExtension'>
<funcprototype>
  <funcdef>XExtCodes *<function>XAddExtension</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
For local Xlib extensions, the
<xref linkend='XAddExtension' xrefstyle='select: title'/>
function allocates the
<structname>XExtCodes</structname>
structure, bumps the extension number count,
and chains the extension onto the extension list.
(This permits extensions to Xlib without requiring server extensions.)
</para>
<sect2 id="Hooks_into_the_Library">
<title>Hooks into the Library</title>
<para>
<!-- .LP -->
These functions allow you to define procedures that are to be
called when various circumstances occur.
The procedures include the creation of a new GC for a connection,
the copying of a GC, the freeing of a GC, the creating and freeing of fonts,
the conversion of events defined by extensions to and from wire
format, and the handling of errors.
</para>
<para>
<!-- .LP -->
All of these functions return the previous procedure defined for this
extension.
</para>
<indexterm significance="preferred"><primary>XESetCloseDisplay</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetCloseDisplay'>
<funcprototype>
  <funcdef>int <function>XESetCloseDisplay</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when the display is closed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetCloseDisplay' xrefstyle='select: title'/>
function defines a procedure to be called whenever
<function>XCloseDisplay</function>
is called.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When 
<function>XCloseDisplay</function>
is called, 
your procedure is called 
with these arguments:
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<indexterm significance="preferred"><primary>XESetCreateGC</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetCreateGC'>
<funcprototype>
  <funcdef>int *<function>XESetCreateGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a GC is closed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetCreateGC' xrefstyle='select: title'/>
function defines a procedure to be called whenever
a new GC is created.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When a GC is created, 
your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>GC <parameter>gc</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<indexterm significance="preferred"><primary>XESetCopyGC</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetCopyGC'>
<funcprototype>
  <funcdef>int *<function>XESetCopyGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when GC components are copied.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetCopyGC' xrefstyle='select: title'/>
function defines a procedure to be called whenever
a GC is copied.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When a GC is copied, 
your procedure is called with these arguments:
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>GC <parameter>gc</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<funcsynopsis id='XESetFreeGC'>
<funcprototype>
  <funcdef>int *<function>XESetFreeGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>

<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a GC is freed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>

<para>
The
<xref linkend='XESetFreeGC' xrefstyle='select: title'/>
function defines a procedure to be called whenever
a GC is freed.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When a GC is freed, 
your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>GC <parameter>gc</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<!-- .LP -->
<!-- .eM -->
<indexterm significance="preferred"><primary>XESetCreateFont</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetCreateFont'>
<funcprototype>
  <funcdef>int *<function>XESetCreateFont</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a font is created.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetCreateFont' xrefstyle='select: title'/>
function defines a procedure to be called whenever
<xref linkend='XLoadQueryFont' xrefstyle='select: title'/>
and
<xref linkend='XQueryFont' xrefstyle='select: title'/>
are called.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When 
<xref linkend='XLoadQueryFont' xrefstyle='select: title'/>
or
<xref linkend='XQueryFont' xrefstyle='select: title'/>
is called, 
your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XFontStruct *<parameter>fs</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<!-- .LP -->
<!-- .eM -->
<indexterm significance="preferred"><primary>XESetFreeFont</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetFreeFont'>
<funcprototype>
  <funcdef>int *<function>XESetFreeFont</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a font is freed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetFreeFont' xrefstyle='select: title'/>
function defines a procedure to be called whenever
<xref linkend='XFreeFont' xrefstyle='select: title'/>
is called.  
It returns any previously defined procedure, usually NULL.
</para>
<para>
<!-- .LP -->
When 
<xref linkend='XFreeFont' xrefstyle='select: title'/>
is called, your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XFontStruct *<parameter>fs</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
</para>
<para>
<!-- .LP -->
<!-- .eM -->
The 
<xref linkend='XESetWireToEvent' xrefstyle='select: title'/>
and
<xref linkend='XESetEventToWire' xrefstyle='select: title'/>
functions allow you to define new events to the library.
An 
<structname>XEvent</structname>
structure always has a type code (type
<type>int</type>)
as the first component.
This uniquely identifies what kind of event it is.
The second component is always the serial number (type
<type>unsigned</type>
<type>long</type>)
of the last request processed by the server.
The third component is always a Boolean (type
<type>Bool</type>)
indicating whether the event came from a
<systemitem>SendEvent</systemitem>
protocol request.
The fourth component is always a pointer to the display
the event was read from.
The fifth component is always a resource ID of one kind or another,
usually a window, carefully selected to be useful to toolkit dispatchers.
The fifth component should always exist, even if
the event does not have a natural destination;
if there is no value
from the protocol to put in this component, initialize it to zero.
<!-- .NT -->
There is an implementation limit such that your host event
structure size cannot be bigger than the size of the
<structname>XEvent</structname>
union of structures.
There also is no way to guarantee that more than 24 elements or 96 characters
in the structure will be fully portable between machines.
</para>
<!-- .NE -->
<indexterm significance="preferred"><primary>XESetWireToEvent</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetWireToEvent'>
<funcprototype>
  <funcdef>int *<function>XESetWireToEvent</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> event_number</parameter></paramdef>
  <paramdef>Status<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>event_number</emphasis>
    </term>
    <listitem>
      <para>
Specifies the event code.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when converting an event.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetWireToEvent' xrefstyle='select: title'/>
function defines a procedure to be called when an event
needs to be converted from wire format 
(<structname>xEvent</structname>)
to host format 
(<structname>XEvent</structname>).
The event number defines which protocol event number to install a
conversion procedure for.
<xref linkend='XESetWireToEvent' xrefstyle='select: title'/>
returns any previously defined procedure.
<!-- .NT -->
You can replace a core event conversion function with one
of your own, although this is not encouraged.
It would, however, allow you to intercept a core event 
and modify it before being placed in the queue or otherwise examined.
<!-- .NE -->
When Xlib needs to convert an event from wire format to host
format, your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XEvent *<parameter>re</parameter></paramdef>
  <paramdef>xEvent *<parameter>event</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
Your procedure must return status to indicate if the conversion succeeded.
The re argument is a pointer to where the host format event should be stored,
and the event argument is the 32-byte wire event structure.
In the 
<structname>XEvent</structname>
structure you are creating, 
you must fill in the five required members of the event structure.
You should fill in the type member with the type specified for the 
<structname>xEvent</structname>
structure.
You should copy all other members from the 
<structname>xEvent</structname>
structure (wire format) to the
<structname>XEvent</structname>
structure (host format).
Your conversion procedure should return 
<symbol>True</symbol>
if the event should be placed in the queue or
<symbol>False</symbol>
if it should not be placed in the queue.
</para>
<para>
<!-- .LP -->
To initialize the serial number component of the event, call
<xref linkend='_XSetLastRequestRead' xrefstyle='select: title'/>
with the event and use the return value.
</para>
<!-- .LP -->
<indexterm significance="preferred"><primary>_XSetLastRequestRead</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XSetLastRequestRead'>
<funcprototype>
  <funcdef>unsigned long<function>_XSetLastRequestRead</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>xGenericReply<parameter> *rep</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>rep</emphasis>
    </term>
    <listitem>
      <para>
Specifies the wire event structure.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XSetLastRequestRead' xrefstyle='select: title'/>
function computes and returns a complete serial number from the partial
serial number in the event.
<!-- .sp -->
</para>
<!-- .LP -->
<indexterm significance="preferred"><primary>XESetEventToWire</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetEventToWire'>
<funcprototype>
  <funcdef>Status *<function>XESetEventToWire</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> event_number</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>event_number</emphasis>
    </term>
    <listitem>
      <para>
Specifies the event code.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when converting an event.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetEventToWire' xrefstyle='select: title'/>
function defines a procedure to be called when an event
needs to be converted from host format
(<structname>XEvent</structname>)
to wire format
(<structname>xEvent</structname>)
form.  
The event number defines which protocol event number to install a
conversion procedure for.
<xref linkend='XESetEventToWire' xrefstyle='select: title'/>
returns any previously defined procedure.
It returns zero if the conversion fails or nonzero otherwise.
<!-- .NT -->
You can replace a core event conversion function with one
of your own, although this is not encouraged.  
It would, however, allow you to intercept a core event 
and modify it before being sent to another client.
<!-- .NE -->
When Xlib needs to convert an event from host format to wire format, 
your procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XEvent *<parameter>re</parameter></paramdef>
  <paramdef>xEvent *<parameter>event</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
The re argument is a pointer to the host format event,
and the event argument is a pointer to where the 32-byte wire event 
structure should be stored.
You should fill in the type with the type from the 
<structname>XEvent</structname>
structure.
All other members then should be copied from the host format to the 
<structname>xEvent</structname>
structure.
</para>
<indexterm significance="preferred"><primary>XESetWireToError</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetWireToError'>
<funcprototype>
  <funcdef>Bool *<function>XESetWireToError</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> error_number</parameter></paramdef>
  <paramdef>Bool<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>error_number</emphasis>
    </term>
    <listitem>
      <para>
Specifies the error code.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when an error is received.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetWireToError' xrefstyle='select: title'/>
function defines a procedure to be called when an extension
error needs to be converted from wire format to host format.
The error number defines which protocol error code to install
the conversion procedure for.
<xref linkend='XESetWireToError' xrefstyle='select: title'/>
returns any previously defined procedure.
</para>
<para>
<!-- .LP -->
Use this function for extension errors that contain additional error values
beyond those in a core X error, when multiple wire errors must be combined
into a single Xlib error, or when it is necessary to intercept an
X error before it is otherwise examined.
</para>
<para>
<!-- .LP -->
When Xlib needs to convert an error from wire format to host format,
the procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XErrorEvent *<parameter>he</parameter></paramdef>
  <paramdef>xError *<parameter>we</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
The he argument is a pointer to where the host format error should be stored.
The structure pointed at by he is guaranteed to be as large as an
<structname>XEvent</structname>
structure and so can be cast to a type larger than an
<structname>XErrorEvent</structname>
to store additional values.
If the error is to be completely ignored by Xlib
(for example, several protocol error structures will be combined into
one Xlib error),
then the function should return
<symbol>False</symbol>;
otherwise, it should return
<symbol>True</symbol>.
</para>
<indexterm significance="preferred"><primary>XESetError</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetError'>
<funcprototype>
  <funcdef>int *<function>XESetError</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when an error is received.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
Inside Xlib, there are times that you may want to suppress the
calling of the external error handling when an error occurs.
This allows status to be returned on a call at the cost of the call
being synchronous (though most such functions are query operations, in any
case, and are typically programmed to be synchronous).
</para>
<para>
<!-- .LP -->
When Xlib detects a protocol error in 
<xref linkend='_XReply' xrefstyle='select: title'/>,
it calls your procedure with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>xError *<parameter>err</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
  <paramdef>int *<parameter>ret_code</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
The err argument is a pointer to the 32-byte wire format error.
The codes argument is a pointer to the extension codes structure.
The ret_code argument is the return code you may want 
<xref linkend='_XReply' xrefstyle='select: title'/>
returned to.
</para>
<para>
<!-- .LP -->
If your procedure returns a zero value, 
the error is not suppressed, and 
the client's error handler is called.
(For further information,
see <link linkend="Using_the_Default_Error_Handlers">section 11.8.2</link>.)
If your procedure returns nonzero, 
the error is suppressed, and 
<xref linkend='_XReply' xrefstyle='select: title'/>
returns the value of ret_code.
</para>
<indexterm significance="preferred"><primary>XESetErrorString</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetErrorString'>
<funcprototype>
  <funcdef>char *<function>XESetErrorString</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>char<parameter> *(*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call to obtain an error string.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The 
<xref linkend='XGetErrorText' xrefstyle='select: title'/>
function returns a string to the user for an error.
<xref linkend='XESetErrorString' xrefstyle='select: title'/>
allows you to define a procedure to be called that
should return a pointer to the error message.
The following is an example.
</para>
<para>
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>int <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>int <parameter>code</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
  <paramdef>char *<parameter>buffer</parameter></paramdef>
  <paramdef>int <parameter>nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
Your procedure is called with the error code for every error detected.
You should copy nbytes of a null-terminated string containing the
error message into buffer.
</para>
<indexterm significance="preferred"><primary>XESetPrintErrorValues</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetPrintErrorValues'>
<funcprototype>
  <funcdef>void *<function>XESetPrintErrorValues</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>void<parameter> (*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when an error is printed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XESetPrintErrorValues' xrefstyle='select: title'/>
function defines a procedure to be called when an extension
error is printed, to print the error values.
Use this function for extension errors that contain additional error values
beyond those in a core X error.
It returns any previously defined procedure.
</para>
<para>
<!-- .LP -->
When Xlib needs to print an error,
the procedure is called with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>void <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XErrorEvent *<parameter>ev</parameter></paramdef>
  <paramdef>void *<parameter>fp</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
The structure pointed at by ev is guaranteed to be as large as an
<structname>XEvent</structname>
structure and so can be cast to a type larger than an
<structname>XErrorEvent</structname>
to obtain additional values set by using
<xref linkend='XESetWireToError' xrefstyle='select: title'/>.
The underlying type of the fp argument is system dependent;
on a <acronym>POSIX</acronym>-compliant system, fp should be cast to type FILE*.
</para>
<indexterm significance="preferred"><primary>XESetFlushGC</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XESetFlushGC'>
<funcprototype>
  <funcdef>int *<function>XESetFlushGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> *(*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a GC is flushed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The procedure set by the
<xref linkend='XESetFlushGC' xrefstyle='select: title'/>
function has the same interface as the procedure set by the
<xref linkend='XESetCopyGC' xrefstyle='select: title'/>
function, but is called when a GC cache needs to be updated in the server.
</para>
<indexterm significance="preferred"><primary>XESetBeforeFlush</primary></indexterm>
<!-- .sM -->
<funcsynopsis>
<funcprototype>
  <funcdef>int *<function>XESetCopyGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>int<parameter> extension</parameter></paramdef>
  <paramdef>int<parameter> *(*proc)()</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extension</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>proc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the procedure to call when a buffer is flushed.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<function>XESetBeforeFlush</function>
function defines a procedure to be called when data is about to be
sent to the server.  When data is about to be sent, your procedure is  
called one or more times with these arguments:
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<!-- .R -->
<funcsynopsis>
<funcprototype>
  <funcdef>void <function>(*<replaceable>proc</replaceable>)</function></funcdef>
  <paramdef>Display *<parameter>display</parameter></paramdef>
  <paramdef>XExtCodes *<parameter>codes</parameter></paramdef>
  <paramdef>char *<parameter>data</parameter></paramdef>
  <paramdef>long <parameter>len</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .LP -->
<!-- .eM -->
The data argument specifies a portion of the outgoing data buffer,
and its length in bytes is specified by the len argument.
Your procedure must not alter the contents of the data and must not
do additional protocol requests to the same display.
</para>
</sect2>
<sect2 id="Hooks_onto_Xlib_Data_Structures">
<title>Hooks onto Xlib Data Structures</title>
<para>
<!-- .LP -->
Various Xlib data structures have provisions for extension procedures
to chain extension supplied data onto a list.
These structures are
<structname>GC</structname>,
<structname>Visual</structname>,
<type>Screen</type>,
<structname>ScreenFormat</structname>,
<type>Display</type>,
and 
<structname>XFontStruct</structname>.
Because the list pointer is always the first member in the structure, 
a single set of procedures can be used to manipulate the data
on these lists.
</para>
<para>
<!-- .LP -->
The following structure is used in the functions in this section
and is defined in 
<filename class="headerfile">&lt;X11/Xlib.h&gt;</filename>
</para>
<para>
<!-- .LP -->
<indexterm significance="preferred"><primary>XExtData</primary></indexterm>
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<synopsis>
typedef struct _XExtData {
	int number;	/* number returned by XInitExtension */
	struct _XExtData *next;	/* next item on list of data for structure */
	int (*free_private)();	/* if defined,  called to free private */
	XPointer private_data;	/* data private to this extension. */
} XExtData;
</synopsis>
</para>
<para>
<!-- .LP -->
<!-- .eM -->
When any of the data structures listed above are freed, 
the list is walked, and the structure's free procedure (if any) is called. 
If free is NULL, 
then the library frees both the data pointed to by the private_data member
and the structure itself. 
</para>
<para>
<!-- .LP -->
<!-- .sM -->
<!-- .TA .5i -->
<!-- .ta .5i -->
<synopsis>
union {	Display *display;
	GC gc;
	Visual *visual;
	Screen *screen;
	ScreenFormat *pixmap_format;
	XFontStruct *font } XEDataObject;
</synopsis>
</para>
<!-- .LP -->
<!-- .eM -->
<indexterm significance="preferred"><primary>XEHeadOfExtensionList</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XEHeadOfExtensionList'>
<funcprototype>
  <funcdef>XExtData **<function>XEHeadOfExtensionList</function></funcdef>
  <paramdef>XEDataObject<parameter> object</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>object</emphasis>
    </term>
    <listitem>
      <para>
Specifies the object.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XEHeadOfExtensionList' xrefstyle='select: title'/>
function returns a pointer to the list of extension structures attached
to the specified object.
In concert with 
<xref linkend='XAddToExtensionList' xrefstyle='select: title'/>,
<xref linkend='XEHeadOfExtensionList' xrefstyle='select: title'/>
allows an extension to attach arbitrary data to any of the structures
of types contained in
<structname>XEDataObject</structname>.
</para>
<!-- .LP -->
<indexterm significance="preferred"><primary>XAddToExtensionList</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XAddToExtensionList'>
<funcprototype>
  <funcdef><function>XAddToExtensionList</function></funcdef>
  <paramdef>XExtData<parameter> **structure</parameter></paramdef>
  <paramdef>XExtData<parameter> *ext_data</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>structure</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension list.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>ext_data</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension data structure to add.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The structure argument is a pointer to one of the data structures
enumerated above.
You must initialize ext_data-&gt;number with the extension number
before calling this function.
</para>
<indexterm significance="preferred"><primary>XFindOnExtensionList</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XFindOnExtensionList'>
<funcprototype>
  <funcdef>XExtData *<function>XFindOnExtensionList</function></funcdef>
  <paramdef>struct_XExtData<parameter> **structure</parameter></paramdef>
  <paramdef>int<parameter> number</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>structure</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension list.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>number</emphasis>
    </term>
    <listitem>
      <para>
Specifies the extension number from
<xref linkend='XInitExtension' xrefstyle='select: title'/>.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='XFindOnExtensionList' xrefstyle='select: title'/>
function returns the first extension data structure
for the extension numbered number.
It is expected that an extension will add at most one extension
data structure to any single data structure's extension data list.
There is no way to find additional structures.
</para>
<para>
<!-- .LP -->
The 
<xref linkend='XAllocID' xrefstyle='select: title'/>
macro, which allocates and returns a resource ID, is defined in 
<filename class="headerfile">&lt;X11/Xlib.h&gt;</filename>.
</para>
<indexterm significance="preferred"><primary>XAllocID</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XAllocID'>
<funcprototype>
  <funcdef><function>XAllocID</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
This macro is a call through the 
<type>Display</type>
structure to an internal resource ID allocator.
It returns a resource ID that you can use when creating new resources.
</para>
<para>
<!-- .LP -->
The 
<xref linkend='XAllocIDs' xrefstyle='select: title'/>
macro allocates and returns an array of resource ID.
</para>
<indexterm significance="preferred"><primary>XAllocIDs</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='XAllocIDs'>
<funcprototype>
  <funcdef><function>XAllocIDs</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>XID<parameter> *ids_return</parameter></paramdef>
  <paramdef>int<parameter> count</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>ids_return</emphasis>
    </term>
    <listitem>
      <para>
Returns the resource IDs.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>rep</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of resource IDs requested.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
This macro is a call through the 
<type>Display</type>
structure to an internal resource ID allocator.
It returns resource IDs to the array supplied by the caller.
To correctly handle automatic reuse of resource IDs, you must call
<xref linkend='XAllocIDs' xrefstyle='select: title'/>
when requesting multiple resource IDs.  This call might generate
protocol requests.
</para>
</sect2>
</sect1>
<sect1 id="GC_Caching">
<title>GC Caching</title>
<para>
<!-- .LP -->
GCs are cached by the library to allow merging of independent change
requests to the same GC into single protocol requests.
This is typically called a write-back cache.
Any extension procedure whose behavior depends on the contents of a GC
must flush the GC cache to make sure the server has up-to-date contents
in its GC.
</para>
<para>
<!-- .LP -->
The 
<xref linkend='FlushGC' xrefstyle='select: title'/>
macro checks the dirty bits in the library's GC structure and calls
<xref linkend='_XFlushGCCache' xrefstyle='select: title'/>
if any elements have changed.
The
<xref linkend='FlushGC' xrefstyle='select: title'/>
macro is defined as follows:
</para>
<indexterm significance="preferred"><primary>FlushGC</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='FlushGC'>
<funcprototype>
  <funcdef><function>FlushGC</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>GC<parameter> gc</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>gc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the GC.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
Note that if you extend the GC to add additional resource ID components,
you should ensure that the library stub sends the change request immediately.
This is because a client can free a resource immediately after
using it, so if you only stored the value in the cache without
forcing a protocol request, the resource might be destroyed before being
set into the GC.
You can use the
<xref linkend='_XFlushGCCache' xrefstyle='select: title'/>
procedure 
to force the cache to be flushed.
The
<xref linkend='_XFlushGCCache' xrefstyle='select: title'/>
procedure
is defined as follows:
</para>
<indexterm significance="preferred"><primary>_XFlushGCCache</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XFlushGCCache'>
<funcprototype>
  <funcdef><function>_XFlushGCCache</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>GC<parameter> gc</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>gc</emphasis>
    </term>
    <listitem>
      <para>
Specifies the GC.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<!-- .LP -->
<!-- .eM -->
</sect1>
<sect1 id="Graphics_Batching">
<title>Graphics Batching</title>
<para>
<!-- .LP -->
If you extend X to add more poly graphics primitives, you may be able to
take advantage of facilities in the library to allow back-to-back 
single calls to be transformed into poly requests.
This may dramatically improve performance of programs that are not
written using poly requests. 
A pointer to an 
<structname>xReq</structname>,
called last_req in the display structure, is the last request being processed.  
By checking that the last request
type, drawable, gc, and other options are the same as the new one
and that there is enough space left in the buffer, you may be able
to just extend the previous graphics request by extending the length
field of the request and appending the data to the buffer.  
This can improve performance by five times or more in naive programs.  
For example, here is the source for the 
<xref linkend='XDrawPoint' xrefstyle='select: title'/>
stub.
(Writing extension stubs is discussed in the next section.)
</para>
<!-- .sM -->
<!-- .nf -->
<programlisting>
#include &lt;X11/Xlibint.h&gt;

/* precompute the maximum size of batching request allowed */

static int size = sizeof(xPolyPointReq) + EPERBATCH * sizeof(xPoint);

XDrawPoint(dpy, d, gc, x, y)
    register Display *dpy;
    Drawable d;
    GC gc;
    int x, y; /* INT16 */
{
    xPoint *point;
    LockDisplay(dpy);
    FlushGC(dpy, gc);
    {
    register xPolyPointReq *req = (xPolyPointReq *) dpy-&gt;last_req;
    /* if same as previous request, with same drawable, batch requests */
    if (
          (req-&gt;reqType == X_PolyPoint)
       &amp;&amp; (req-&gt;drawable == d)
       &amp;&amp; (req-&gt;gc == gc-&gt;gid)
       &amp;&amp; (req-&gt;coordMode == CoordModeOrigin)
       &amp;&amp; ((dpy-&gt;bufptr + sizeof (xPoint)) &lt;= dpy-&gt;bufmax)
       &amp;&amp; (((char *)dpy-&gt;bufptr - (char *)req) &lt; size) ) {
         point = (xPoint *) dpy-&gt;bufptr;
         req-&gt;length += sizeof (xPoint) &gt;&gt; 2;
         dpy-&gt;bufptr += sizeof (xPoint);
         }

    else {
        GetReqExtra(PolyPoint, 4, req); /* 1 point = 4 bytes */
        req-&gt;drawable = d;
        req-&gt;gc = gc-&gt;gid;
        req-&gt;coordMode = CoordModeOrigin;
        point = (xPoint *) (req + 1);
        }
    point-&gt;x = x;
    point-&gt;y = y;
    }
    UnlockDisplay(dpy);
    SyncHandle();
}
</programlisting>
<!-- .fi -->
<para>
<!-- .LP -->
<!-- .eM -->
To keep clients from generating very long requests that may monopolize the 
server,
there is a symbol defined in
<filename class="headerfile">&lt;X11/Xlibint.h&gt;</filename>
of EPERBATCH on the number of requests batched.
Most of the performance benefit occurs in the first few merged requests.
Note that 
<xref linkend='FlushGC' xrefstyle='select: title'/>
is called <emphasis remap='I'>before</emphasis> picking up the value of last_req,
because it may modify this field.
</para>
</sect1>
<sect1 id="Writing_Extension_Stubs">
<title>Writing Extension Stubs</title>
<para>
<!-- .LP -->
All X requests always contain the length of the request,
expressed as a 16-bit quantity of 32 bits.
This means that a single request can be no more than 256K bytes in
length.
Some servers may not support single requests of such a length.
The value of dpy-&gt;max_request_size contains the maximum length as
defined by the server implementation.
For further information,
see <olink targetdoc='x11protocol' targetptr='Maximum-request-length'
><citetitle>X Window System Protocol</citetitle></olink>.
</para>
<sect2 id="Requests_Replies_and_Xproto.h">
<title>Requests, Replies, and Xproto.h</title>
<para>
<!-- .LP -->
The 
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
file contains three sets of definitions that
are of interest to the stub implementor:  
request names, request structures, and reply structures.
</para>
<para>
<!-- .LP -->
You need to generate a file equivalent to 
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
for your extension and need to include it in your stub procedure.
Each stub procedure also must include 
<filename class="headerfile">&lt;X11/Xlibint.h&gt;</filename>.
</para>
<para>
<!-- .LP -->
The identifiers are deliberately chosen in such a way that, if the
request is called X_DoSomething, then its request structure is
xDoSomethingReq, and its reply is xDoSomethingReply.  
The GetReq family of macros, defined in 
<filename class="headerfile">&lt;X11/Xlibint.h&gt;</filename>,
takes advantage of this naming scheme.
</para>
<para>
<!-- .LP -->
For each X request, 
there is a definition in 
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
that looks similar to this:
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
#define X_DoSomething   42
</programlisting>
In your extension header file, 
this will be a minor opcode, 
instead of a major opcode.
</para>
</sect2>
<sect2 id="Request_Format">
<title>Request Format</title>
<para>
<!-- .LP -->
Every request contains an 8-bit major opcode and a 16-bit length field
expressed in units of 4 bytes.  
Every request consists of 4 bytes of header
(containing the major opcode, the length field, and a data byte) followed by
zero or more additional bytes of data. 
The length field defines the total length of the request, including the header.
The length field in a request must equal the minimum length required to contain 
the request. 
If the specified length is smaller or larger than the required length, 
the server should generate a 
<errorname>BadLength</errorname>
error.
Unused bytes in a request are not required to be zero.  
Extensions should be designed in such a way that long protocol requests
can be split up into smaller requests,
if it is possible to exceed the maximum request size of the server.
The protocol guarantees the maximum request size to be no smaller than
4096 units (16384 bytes).
</para>
<para>
<!-- .LP -->
Major opcodes 128 through 255 are reserved for extensions.
Extensions are intended to contain multiple requests, 
so extension requests typically have an additional minor opcode encoded 
in the second data byte in the request header, 
but the placement and interpretation of this minor opcode as well as all
other fields in extension requests are not defined by the core protocol.
Every request is implicitly assigned a sequence number (starting with one)
used in replies, errors, and events.
</para>
<para>
<!-- .LP -->
To help but not cure portability problems to certain machines, the
<symbol>B16</symbol>
and
<symbol>B32</symbol>
macros have been defined so that they can become bitfield specifications 
on some machines.
For example, on a Cray,
these should be used for all 16-bit and 32-bit quantities, as discussed below.
</para>
<para>
<!-- .LP -->
Most protocol requests have a corresponding structure typedef in
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>,
which looks like:
</para>
<para>
<!-- .LP -->
<indexterm significance="preferred"><primary>xDoSomethingReq</primary></indexterm>
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<synopsis>
typedef struct _DoSomethingReq {
	CARD8 reqType;		/* X_DoSomething */
	CARD8 someDatum;	/* used differently in different requests */
	CARD16 length B16;	/* total # of bytes in request, divided by 4 */
	...
	/* request-specific data */
	...
} xDoSomethingReq;
</synopsis>
</para>
<para>
<!-- .LP -->
<!-- .eM -->
If a core protocol request has a single 32-bit argument, 
you need not declare a request structure in your extension header file.
Instead, such requests use the
<structname>xResourceReq</structname>
structure in
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>.
This structure is used for any request whose single argument is a 
<type>Window</type>,
<type>Pixmap</type>,
<type>Drawable</type>,
<type>GContext</type>,
<type>Font</type>,
<type>Cursor</type>,
<type>Colormap</type>,
<type>Atom</type>,
or
<type>VisualID</type>.
</para>
<para>
<!-- .LP -->
<indexterm significance="preferred"><primary>xResourceReq</primary></indexterm>
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<synopsis>
typedef struct _ResourceReq {
	CARD8 reqType;	/* the request type, e.g. X_DoSomething */
	BYTE pad;	/* not used */
	CARD16 length B16;	/* 2 (= total # of bytes in request, divided by 4) */
	CARD32 id B32;	/* the Window, Drawable, Font, GContext, etc. */
} xResourceReq;
</synopsis>
</para>
<para>
<!-- .LP -->
<!-- .eM -->
If convenient,
you can do something similar in your extension header file. 
</para>
<para>
<!-- .LP -->
In both of these structures, 
the reqType field identifies the type of the request (for example, 
X_MapWindow or X_CreatePixmap).  
The length field tells how long the request is
in units of 4-byte longwords. 
This length includes both the request structure itself and any
variable-length data, such as strings or lists, that follow the
request structure.  
Request structures come in different sizes, 
but all requests are padded to be multiples of four bytes long.
</para>
<para>
<!-- .LP -->
A few protocol requests take no arguments at all. 
Instead, they use the
<structname>xReq</structname>
structure in
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>,
which contains only a reqType and a length (and a pad byte).
</para>
<para>
<!-- .LP  -->
If the protocol request requires a reply, 
then
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
also contains a reply structure typedef:
</para>
<para>
<!-- .LP -->
<indexterm significance="preferred"><primary>xDoSomethingReply</primary></indexterm>
<!-- .sM -->
<!-- .TA .5i 3i -->
<!-- .ta .5i 3i -->
<synopsis>
typedef struct _DoSomethingReply {
	BYTE type;	/* always X_Reply */
	BYTE someDatum;	/* used differently in different requests */
	CARD16 sequenceNumber B16;	/* # of requests sent so far */
	CARD32 length B32;	/* # of additional bytes, divided by 4 */
	...
	/* request-specific data */
	...
} xDoSomethingReply;
</synopsis>
</para>
<para>
<!-- .LP -->
<!-- .eM -->
Most of these reply structures are 32 bytes long. 
If there are not that many reply values, 
then they contain a sufficient number of pad fields
to bring them up to 32 bytes.  
The length field is the total number of bytes in the request minus 32, 
divided by 4.  
This length will be nonzero only if:
</para>
<itemizedlist>
  <listitem>
    <para>
The reply structure is followed by variable-length data,
such as a list or string.
    </para>
  </listitem>
  <listitem>
    <para>
The reply structure is longer than 32 bytes.
    </para>
  </listitem>
</itemizedlist>
<para>
<!-- .LP -->
Only 
<systemitem>GetWindowAttributesl</systemitem>,
<systemitem>QueryFont</systemitem>,
<systemitem>QueryKeymap</systemitem>,
and
<systemitem>GetKeyboardControl</systemitem>
have reply structures longer than 32 bytes in the core protocol.
</para>
<para>
<!-- .LP -->
A few protocol requests return replies that contain no data.  
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
does not define reply structures for these.
Instead, they use the 
<structname>xGenericReply</structname>
structure, which contains only a type, length,
and sequence number (and sufficient padding to make it 32 bytes long).
</para>
</sect2>
<sect2 id="Starting_to_Write_a_Stub_Procedure">
<title>Starting to Write a Stub Procedure</title>
<para>
<!-- .LP -->
An Xlib stub procedure should start like this:
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
#include "&lt;X11/Xlibint.h&gt;

XDoSomething (arguments, ... )
/* argument declarations */
{

register XDoSomethingReq *req;
...
</programlisting>
If the protocol request has a reply, 
then the variable declarations should include the reply structure for the request.
The following is an example:
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
xDoSomethingReply rep;
</programlisting>
</para>
</sect2>
<sect2 id="Locking_Data_Structures">
<title>Locking Data Structures</title>
<para>
<!-- .LP -->
To lock the display structure for systems that
want to support multithreaded access to a single display connection,
each stub will need to lock its critical section.
Generally, this section is the point from just before the appropriate GetReq
call until all arguments to the call have been stored into the buffer.
The precise instructions needed for this locking depend upon the machine
architecture. 
Two calls, which are generally implemented as macros, have been provided.
<indexterm significance="preferred"><primary>LockDisplay</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='LockDisplay'>
<funcprototype>
  <funcdef><function>LockDisplay</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
</para>
<!-- .LP -->
<indexterm significance="preferred"><primary>UnlockDisplay</primary></indexterm>
<funcsynopsis id='UnlockDisplay'>
<funcprototype>
  <funcdef><function>UnlockDisplay</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<!-- .LP -->
<!-- .eM -->
</sect2>
<sect2 id="Sending_the_Protocol_Request_and_Arguments">
<title>Sending the Protocol Request and Arguments</title>
<para>
<!-- .LP -->
After the variable declarations, 
a stub procedure should call one of four macros defined in 
<filename class="headerfile">&lt;X11/Xlibint.h&gt;</filename>:
<function>GetReq</function>,
<function>GetReqExtra</function>,
<function>GetResReq</function>,
or 
<function>GetEmptyReq</function>.
All of these macros take, as their first argument,
the name of the protocol request as declared in 
<filename class="headerfile">&lt;X11/Xproto.h&gt;</filename>
except with X_ removed.   
Each one declares a 
<type>Display</type>
structure pointer,
called dpy, and a pointer to a request structure, called req,
which is of the appropriate type.
The macro then appends the request structure to the output buffer, 
fills in its type and length field, and sets req to point to it.
</para>
<para>
<!-- .LP -->
If the protocol request has no arguments (for instance, X_GrabServer),
then use 
<function>GetEmptyReq</function>.
</para>
<para>
<!-- .LP -->
<!-- .R  -->
<programlisting>
GetEmptyReq (DoSomething, req);
</programlisting>
If the protocol request has a single 32-bit argument (such as a
<type>Pixmap</type>,
<type>Window</type>,
<type>Drawable</type>,
<type>Atom</type>,
and so on),
then use 
<function>GetResReq</function>.
The second argument to the macro is the 32-bit object.  
<symbol>X_MapWindow</symbol>
is a good example.
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
GetResReq (DoSomething, rid, req);
</programlisting>
The rid argument is the 
<type>Pixmap</type>,
<type>Window</type>,
or other resource ID.
</para>
<para>
<!-- .LP -->
If the protocol request takes any other argument list, 
then call 
<function>GetReq</function>.
After the 
<function>GetReq</function>,
you need to set all the other fields in the request structure,
usually from arguments to the stub procedure.
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
GetReq (DoSomething, req);
/* fill in arguments here */
req-&gt;arg1 = arg1;
req-&gt;arg2 = arg2;
...
</programlisting>
A few stub procedures (such as 
<xref linkend='XCreateGC' xrefstyle='select: title'/>
and 
<xref linkend='XCreatePixmap' xrefstyle='select: title'/>)
return a resource ID to the caller but pass a resource ID as an argument
to the protocol request.   
Such procedures use the macro 
<xref linkend='XAllocID' xrefstyle='select: title'/>
to allocate a resource ID from the range of IDs 
that were assigned to this client when it opened the connection.
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
rid = req-&gt;rid = XAllocID();
...
return (rid);
</programlisting>
Finally, some stub procedures transmit a fixed amount of variable-length
data after the request.  
Typically, these procedures (such as
<xref linkend='XMoveWindow' xrefstyle='select: title'/>
and 
<xref linkend='XSetBackground' xrefstyle='select: title'/>)
are special cases of more general functions like 
<xref linkend='XMoveResizeWindow' xrefstyle='select: title'/>
and 
<xref linkend='XChangeGC' xrefstyle='select: title'/>.
These procedures use 
<function>GetReqExtra</function>,
which is the same as 
<function>GetReq</function>
except that it takes an additional argument (the number of
extra bytes to allocate in the output buffer after the request structure).  
This number should always be a multiple of four. Note that it is possible
for req to be set to NULL as a defensive measure if the requested length
exceeds the Xlib's buffer size (normally 16K).
</para>
</sect2>
<sect2 id="Variable_Length_Arguments">
<title>Variable Length Arguments</title>
<para>
<!-- .LP -->
Some protocol requests take additional variable-length data that
follow the 
<type>xDoSomethingReq</type>
structure.    
The format of this data varies from request to request. 
Some requests require a sequence of 8-bit bytes, 
others a sequence of 16-bit or 32-bit entities, 
and still others a sequence of structures.
</para>
<para>
<!-- .LP -->
It is necessary to add the length of any variable-length data to the
length field of the request structure.  
That length field is in units of 32-bit longwords.  
If the data is a string or other sequence of 8-bit bytes, 
then you must round the length up and shift it before adding:
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
req-&gt;length += (nbytes+3)&gt;&gt;2;
</programlisting>
To transmit variable-length data, use the 
<xref linkend='Data' xrefstyle='select: title'/>
macros.
If the data fits into the output buffer, 
then this macro copies it to the buffer.  
If it does not fit, however,
the 
<xref linkend='Data' xrefstyle='select: title'/>
macro calls 
<function>_XSend</function>,
which transmits first the contents of the buffer and then your data.
The 
<xref linkend='Data' xrefstyle='select: title'/>
macros take three arguments:  
the display, a pointer to the beginning of the data, 
and the number of bytes to be sent.
<!-- .sM -->
<funcsynopsis id='Data'>
<funcprototype>
  <funcdef><function>Data</function></funcdef>
  <paramdef><parameter> display</parameter></paramdef>
  <paramdef>(char<parameter> *</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
</para>
<para>
<!-- .LP -->
<!-- .eM -->
<xref linkend='Data' xrefstyle='select: title'/>,
<function>Data16</function>,
and
<function>Data32</function>
are macros that may use their last argument
more than once, so that argument should be a variable rather than
an expression such as ``nitems*sizeof(item)''.  
You should do that kind of computation in a separate statement before calling 
them.
Use the appropriate macro when sending byte, short, or long data.
</para>
<para>
<!-- .LP -->
If the protocol request requires a reply, 
then call the procedure 
<function>_XSend</function>
instead of the 
<xref linkend='Data' xrefstyle='select: title'/>
macro.  
<function>_XSend</function>
takes the same arguments, but because it sends your data immediately instead of 
copying it into the output buffer (which would later be flushed
anyway by the following call on 
<xref linkend='_XReply' xrefstyle='select: title'/>),
it is faster.
</para>
</sect2>
<sect2 id="Replies">
<title>Replies</title>
<para>
<!-- .LP -->
If the protocol request has a reply, 
then call 
<xref linkend='_XReply' xrefstyle='select: title'/>
after you have finished dealing with 
all the fixed-length and variable-length arguments.  
<xref linkend='_XReply' xrefstyle='select: title'/>
flushes the output buffer and waits for an 
<structname>xReply</structname>
packet to arrive.  
If any events arrive in the meantime,
<xref linkend='_XReply' xrefstyle='select: title'/>
places them in the queue for later use.
</para>
<indexterm significance="preferred"><primary>_XReply</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XReply'>
<funcprototype>
  <funcdef>Status <function>_XReply</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>xReply<parameter> *rep</parameter></paramdef>
  <paramdef>int<parameter> extra</parameter></paramdef>
  <paramdef>Bool<parameter> discard</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>rep</emphasis>
    </term>
    <listitem>
      <para>
Specifies the reply structure.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>extra</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of 32-bit words expected after the replay.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>discard</emphasis>
    </term>
    <listitem>
      <para>
Specifies if any data beyond that specified in the extra argument
should be discarded.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XReply' xrefstyle='select: title'/>
function waits for a reply packet and copies its contents into the
specified rep.  
<xref linkend='_XReply' xrefstyle='select: title'/>
handles error and event packets that occur before the reply is received.
<xref linkend='_XReply' xrefstyle='select: title'/>
takes four arguments:
</para>
<itemizedlist>
  <listitem>
    <para>
A 
<type>Display</type>
* structure
    </para>
  </listitem>
  <listitem>
    <para>
A pointer to a reply structure (which must be cast to an 
<structname>xReply</structname>
*)
    </para>
  </listitem>
  <listitem>
    <para>
The number of additional 32-bit words (beyond 
<function>sizeof( xReply</function>)
= 32 bytes)
in the reply structure
    </para>
  </listitem>
  <listitem>
    <para>
A Boolean that indicates whether
<xref linkend='_XReply' xrefstyle='select: title'/>
is to discard any additional bytes
beyond those it was told to read
    </para>
  </listitem>
</itemizedlist>
<para>
<!-- .LP -->
Because most reply structures are 32 bytes long, 
the third argument is usually 0.  
The only core protocol exceptions are the replies to 
<systemitem>GetWindowAttributesl</systemitem>,
<systemitem>QueryFont</systemitem>,
<systemitem>QueryKeymap</systemitem>,
and 
<systemitem>GetKeyboardControl</systemitem>,
which have longer replies.
</para>
<para>
<!-- .LP -->
The last argument should be 
<symbol>False</symbol>
if the reply structure is followed
by additional variable-length data (such as a list or string).  
It should be 
<symbol>True</symbol>
if there is not any variable-length data.
<!-- .NT -->
This last argument is provided for upward-compatibility reasons
to allow a client to communicate properly with a hypothetical later
version of the server that sends more data than the client expected.
For example, some later version of 
<systemitem>GetWindowAttributesl</systemitem>
might use a
larger, but compatible, 
<structname>xGetWindowAttributesReply</structname>
that contains additional attribute data at the end.
<!-- .NE -->
<xref linkend='_XReply' xrefstyle='select: title'/>
returns 
<symbol>True</symbol>
if it received a reply successfully or 
<symbol>False</symbol>
if it received any sort of error. 
</para>
<para>
<!-- .LP -->
For a request with a reply that is not followed by variable-length
data, you write something like:
</para>
<para>
<!-- .LP -->
<!-- .R -->
<programlisting>
_XReply(display, (xReply *)&amp;rep, 0, True);
*ret1 = rep.ret1;
*ret2 = rep.ret2;
*ret3 = rep.ret3;
...
UnlockDisplay(dpy);
SyncHandle();
return (rep.ret4);
}
</programlisting>
If there is variable-length data after the reply, 
change the 
<symbol>True</symbol>
to 
<symbol>False</symbol>,
and use the appropriate
<xref linkend='_XRead' xrefstyle='select: title'/>
function to read the variable-length data.
</para>
<!-- .LP -->
<!-- .sM -->
<funcsynopsis id='_XRead'>
<funcprototype>
  <funcdef><function>_XRead</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>char<parameter> *data_return</parameter></paramdef>
  <paramdef>long<parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>data_return</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XRead' xrefstyle='select: title'/>
function reads the specified number of bytes into data_return.
</para>
<!-- .LP -->
<!-- .sM -->
<funcsynopsis id='_XRead16'>
<funcprototype>
  <funcdef><function>_XRead16</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>short<parameter> *data_return</parameter></paramdef>
  <paramdef>long<parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>data_return</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XRead16' xrefstyle='select: title'/>
function reads the specified number of bytes,
unpacking them as 16-bit quantities,
into the specified array as shorts.
</para>
<!-- .LP -->
<!-- .sM -->
<funcsynopsis id='_XRead32'>
<funcprototype>
  <funcdef><function>_XRead32</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>long<parameter> *data_return</parameter></paramdef>
  <paramdef>long<parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>data_return</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XRead32' xrefstyle='select: title'/>
function reads the specified number of bytes,
unpacking them as 32-bit quantities,
into the specified array as longs.
</para>
<!-- .LP -->
<!-- .sM -->
<funcsynopsis id='_XRead16Pad'>
<funcprototype>
  <funcdef><function>_XRead16Pad</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>short<parameter> *data_return</parameter></paramdef>
  <paramdef>long<parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>data_return</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XRead16Pad' xrefstyle='select: title'/>
function reads the specified number of bytes,
unpacking them as 16-bit quantities,
into the specified array as shorts.
If the number of bytes is not a multiple of four,
<xref linkend='_XRead16Pad' xrefstyle='select: title'/>
reads and discards up to two additional pad bytes.
</para>
<!-- .LP -->
<!-- .sM -->
<funcsynopsis id='_XReadPad'>
<funcprototype>
  <funcdef><function>_XReadPad</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>char<parameter> *data_return</parameter></paramdef>
  <paramdef>long<parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>data_return</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
The
<xref linkend='_XReadPad' xrefstyle='select: title'/>
function reads the specified number of bytes into data_return.
If the number of bytes is not a multiple of four,
<xref linkend='_XReadPad' xrefstyle='select: title'/>
reads and discards up to three additional pad bytes.
</para>
<para>
<!-- .LP -->
Each protocol request is a little different. 
For further information,
see the Xlib sources for examples.
</para>
</sect2>
<sect2 id="Synchronous_Calling">
<title>Synchronous Calling</title>
<para>
<!-- .LP -->
Each procedure should have a call, just before returning to the user, 
to a macro called
<systemitem>SyncHandle</systemitem>.
If synchronous mode is enabled (see 
<function>XSynchronize</function>),
the request is sent immediately.
The library, however, waits until any error the procedure could generate
at the server has been handled.
</para>
</sect2>
<sect2 id="Allocating_and_Deallocating_Memory">
<title>Allocating and Deallocating Memory</title>
<para>
<!-- .LP -->
To support the possible reentry of these procedures, 
you must observe several conventions when allocating and deallocating memory,
most often done when returning data to the user from the window
system of a size the caller could not know in advance
(for example, a list of fonts or a list of extensions).
The standard C library functions on many systems
are not protected against signals or other multithreaded uses.
The following analogies to standard I/O library functions
have been defined:
</para>
<para>
<!-- .LP -->
These should be used in place of any calls you would make to the normal
C library functions.
</para>
<para>
<!-- .LP -->
If you need a single scratch buffer inside a critical section 
(for example, to pack and unpack data to and from the wire protocol),
the general memory allocators may be too expensive to use
(particularly in output functions, which are performance critical).  
The following function returns a scratch buffer for use within a
critical section:
</para>
<indexterm significance="preferred"><primary>_XAllocScratch</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XAllocScratch'>
<funcprototype>
  <funcdef>char *<function>_XAllocScratch</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>unsigned long <parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
This storage must only be used inside of a critical section of your
stub.  The returned pointer cannot be assumed valid after any call
that might permit another thread to execute inside Xlib.  For example,
the pointer cannot be assumed valid after any use of the
<function>GetReq</function>
or
<xref linkend='Data' xrefstyle='select: title'/>
families of macros,
after any use of
<xref linkend='_XReply' xrefstyle='select: title'/>,
or after any use of the
<function>_XSend</function>
or
<xref linkend='_XRead' xrefstyle='select: title'/>
families of functions.
</para>
<para>
<!-- .LP -->
<!-- .sp -->
The following function returns a scratch buffer for use across
critical sections:
</para>
<indexterm significance="preferred"><primary>_XAllocTemp</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XAllocTemp'>
<funcprototype>
  <funcdef>char *<function>_XAllocTemp</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>unsigned long <parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the number of bytes required.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
This storage can be used across calls that might permit another thread to
execute inside Xlib.  The storage must be explicitly returned to Xlib.
The following function returns the storage:
</para>
<indexterm significance="preferred"><primary>_XFreeTemp</primary></indexterm>
<!-- .sM -->
<funcsynopsis id='_XFreeTemp'>
<funcprototype>
  <funcdef>void <function>_XFreeTemp</function></funcdef>
  <paramdef>Display<parameter> *display</parameter></paramdef>
  <paramdef>char<parameter> *buf</parameter></paramdef>
  <paramdef>unsigned long <parameter> nbytes</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<!-- .FN -->
<variablelist>
  <varlistentry>
    <term>
      <emphasis remap='I'>display</emphasis>
    </term>
    <listitem>
      <para>
Specifies the connection to the X server.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>buf</emphasis>
    </term>
    <listitem>
      <para>
Specifies the buffer to return.
      </para>
    </listitem>
  </varlistentry>
  <varlistentry>
    <term>
      <emphasis remap='I'>nbytes</emphasis>
    </term>
    <listitem>
      <para>
Specifies the size of the buffer.
    </para>
  </listitem>
  </varlistentry>
</variablelist>
<para>
<!-- .LP -->
<!-- .eM -->
You must pass back the same pointer and size that were returned by
<xref linkend='_XAllocTemp' xrefstyle='select: title'/>.
</para>
</sect2>
<sect2 id="Portability_Considerations">
<title>Portability Considerations</title>
<para>
<!-- .LP -->
Many machine architectures, 
including many of the more recent <acronym>RISC</acronym> architectures,
do not correctly access data at unaligned locations; 
their compilers pad out structures to preserve this characteristic.
Many other machines capable of unaligned references pad inside of structures
as well to preserve alignment, because accessing aligned data is
usually much faster.
Because the library and the server use structures to access data at
arbitrary points in a byte stream,
all data in request and reply packets <emphasis remap='I'>must</emphasis> be naturally aligned;
that is, 16-bit data starts on 16-bit boundaries in the request
and 32-bit data on 32-bit boundaries.
All requests <emphasis remap='I'>must</emphasis> be a multiple of 32 bits in length to preserve
the natural alignment in the data stream.
You must pad structures out to 32-bit boundaries.
Pad information does not have to be zeroed unless you want to
preserve such fields for future use in your protocol requests.
Floating point varies radically between machines and should be
avoided completely if at all possible.
</para>
<para>
<!-- .LP -->
This code may run on machines with 16-bit ints.  
So, if any integer argument, variable, or return value either can take 
only nonnegative values or is declared as a
<type>CARD16</type>
in the protocol, be sure to declare it as
<type>unsigned</type>
<type>int</type>
and not as
<type>int</type>.
(This, of course, does not apply to Booleans or enumerations.)
</para>
<para>
<!-- .LP -->
Similarly, 
if any integer argument or return value is declared
<type>CARD32</type>
in the protocol, 
declare it as an
<type>unsigned</type>
<type>long</type>
and not as
<type>int</type>
or
<type>long</type>.
This also goes for any internal variables that may
take on values larger than the maximum 16-bit
<type>unsigned</type>
<type>int</type>.
</para>
<para>
<!-- .LP -->
The library currently assumes that a
<type>char</type>
is 8 bits, a
<type>short</type>
is 16 bits, an
<type>int</type>
is 16 or 32 bits, and a
<type>long</type>
is 32 bits.  
The 
<function>PackData</function>
macro is a half-hearted attempt to deal with the possibility of 32 bit shorts. 
However, much more work is needed to make this work properly.
</para>
</sect2>
<sect2 id="Deriving_the_Correct_Extension_Opcode">
<title>Deriving the Correct Extension Opcode</title>
<para>
<!-- .LP -->
The remaining problem a writer of an extension stub procedure faces that
the core protocol does not face is to map from the call to the proper
major and minor opcodes.  
While there are a number of strategies, 
the simplest and fastest is outlined below.
</para>
<itemizedlist>
  <listitem>
    <para>
Declare an array of pointers, _NFILE long (this is normally found
in 
<filename class="headerfile">&lt;stdio.h&gt;</filename>
and is the number of file descriptors supported on the system)
of type 
<structname>XExtCodes</structname>.
Make sure these are all initialized to NULL.
    </para>
  </listitem>
  <listitem>
    <para>
When your stub is entered, your initialization test is just to use
the display pointer passed in to access the file descriptor and an index
into the array.  
If the entry is NULL, then this is the first time you
are entering the procedure for this display.  
Call your initialization procedure and pass to it the display pointer.
    </para>
  </listitem>
  <listitem>
    <para>
Once in your initialization procedure, call 
<xref linkend='XInitExtension' xrefstyle='select: title'/>;
if it succeeds, store the pointer returned into this array.  
Make sure to establish a close display handler to allow you to zero the entry.
Do whatever other initialization your extension requires.
(For example, install event handlers and so on.)
Your initialization procedure would normally return a pointer to the
<structname>XExtCodes</structname>
structure for this extension, which is what would normally
be found in your array of pointers.
    </para>
  </listitem>
  <listitem>
    <para>
After returning from your initialization procedure, 
the stub can now continue normally, because it has its major opcode safely 
in its hand in the 
<structname>XExtCodes</structname>
structure.
<!-- .bp -->
    </para>
  </listitem>
</itemizedlist>
</sect2>
</sect1>
</appendix>