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

#include "xgl.h"

#ifdef GLXEXT

#include <GL/gl.h>

#include "glxserver.h"
#include "glxdrawable.h"
#include "glxscreens.h"
#include "micmap.h"

extern void
GlxSetVisualConfigs (int	       nconfigs,
		     __GLXvisualConfig *configs,
		     void	       **privates);

extern void
GlxWrapInitVisuals (miInitVisualsProcPtr *);

extern int
GlxInitVisuals (VisualPtr     *visualp,
		DepthPtr      *depthp,
		int	      *nvisualp,
		int	      *ndepthp,
		int	      *rootDepthp,
		VisualID      *defaultVisp,
		unsigned long sizes,
		int	      bitsPerRGB,
		int	      preferredVis);

extern void
__glXFlushContextCache (void);

extern __GLXscreenInfo __glDDXScreenInfo;

extern __glProcTable    __glMesaRenderTable;
extern __glProcTableEXT __glMesaRenderTableEXT;

typedef Bool	      (*GLXScreenProbeProc)    (int screen);
typedef __GLinterface *(*GLXCreateContextProc) (__GLimports      *imports,
						__GLcontextModes *modes,
						__GLinterface    *shareGC);
typedef void	      (*GLXCreateBufferProc)   (__GLXdrawablePrivate *glxPriv);
typedef GLboolean     (*GLXSwapBuffersProc)    (__GLXdrawablePrivate *glxPriv);

typedef struct _xglGLXScreenInfo {
    GLXScreenProbeProc   screenProbe;
    GLXCreateContextProc createContext;
    GLXCreateBufferProc  createBuffer;
} xglGLXScreenInfoRec, *xglGLXScreenInfoPtr;

static xglGLXScreenInfoRec screenInfoPriv;

typedef GLboolean (*GLResizeBuffersProc) (__GLdrawableBuffer   *buffer,
					  GLint		       x,
					  GLint		       y,
					  GLuint	       width,
					  GLuint	       height, 
					  __GLdrawablePrivate  *glPriv,
					  GLuint	       bufferMask);
typedef void	  (*GLFreeBuffersProc)   (__GLdrawablePrivate  *glPriv);
    
typedef struct _xglGLBuffer {
    GLXSwapBuffersProc  swapBuffers;
    GLResizeBuffersProc resizeBuffers;
    GLFreeBuffersProc   freeBuffers;
    ScreenPtr		pScreen;
    DrawablePtr		pDrawable;
    PixmapPtr		pPixmap;
    GCPtr		pGC, swapGC;
    RegionRec		damage;
    void	        *private;
} xglGLBufferRec, *xglGLBufferPtr;

typedef int xglGLXVisualConfigRec, *xglGLXVisualConfigPtr;

typedef struct _xglDisplayList *xglDisplayListPtr;

#define XGL_LIST_OP_CALLS 0
#define XGL_LIST_OP_DRAW  1
#define XGL_LIST_OP_COPY  2
#define XGL_LIST_OP_LIST  3

typedef struct _xglCopyOp {
    void (*copyProc) (struct _xglCopyOp *pOp);
    union {
	struct {
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLsizei height;
	    GLenum  type; 
	} pixels;
	struct {
	    GLenum  target;
	    GLint   level;
	    GLenum  internalformat;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLint   border; 
	} tex_image_1d;
	struct {
	    GLenum  target;
	    GLint   level;
	    GLenum  internalformat;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLsizei height;
	    GLint   border; 
	} tex_image_2d;
	struct {
	    GLenum  target;
	    GLint   level;
	    GLint   xoffset;
	    GLint   x;
	    GLint   y;
	    GLsizei width; 
	} tex_sub_image_1d;
	struct {
	    GLenum  target;
	    GLint   level;
	    GLint   xoffset;
	    GLint   yoffset;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLsizei height; 
	} tex_sub_image_2d;
	struct {
	    GLenum  target;
	    GLenum  internalformat;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	} color_table;
	struct {
	    GLenum  target;
	    GLsizei start;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	} color_sub_table;
	struct {
	    GLenum  target;
	    GLenum  internalformat;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	} convolution_filter_1d;
	struct {
	    GLenum  target;
	    GLenum  internalformat;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLsizei height;
	} convolution_filter_2d;
	struct {
	    GLenum  target;
	    GLint   level;
	    GLint   xoffset;
	    GLint   yoffset;
	    GLint   zoffset;
	    GLint   x;
	    GLint   y;
	    GLsizei width;
	    GLsizei height; 
	} tex_sub_image_3d;
    } u;
} xglCopyOpRec, *xglCopyOpPtr;

typedef struct _xglListOp {
    int type;
    union {
	GLuint		  name;
	xglCopyOpPtr	  copy;
	xglDisplayListPtr list;
    } u;
} xglListOpRec, *xglListOpPtr;

typedef struct _xglDisplayList {
    xglListOpPtr pOp;
    int		 nOp;
    int		 size;
    int		 refcnt;
} xglDisplayListRec;

typedef struct _xglTexObj {
    GLuint name;
} xglTexObjRec, *xglTexObjPtr;

typedef struct _xglGLAttributes {
    GLbitfield mask;
    GLenum     drawBuffer;
    GLenum     readBuffer;
    xRectangle viewport;
    xRectangle scissor;
    GLboolean  scissorTest;
} xglGLAttributesRec, *xglGLAttributesPtr;

typedef struct _xglGLContext {
    __GLinterface	     iface;
    __GLinterface	     *mIface;
    int			     refcnt;
    struct _xglGLContext     *shared;
    glitz_context_t	     *context;
    __glProcTableEXT	     glRenderTableEXT;
    PFNGLWINDOWPOS3FMESAPROC WindowPos3fMESA;
    Bool		     needInit;
    Bool		     target;
    glitz_surface_t	     *draw;
    int			     tx, ty;
    xglGLBufferPtr	     pDrawBuffer;
    char		     *versionString;
    GLenum		     error;
    xglHashTablePtr	     texObjects;
    xglHashTablePtr	     displayLists;
    GLuint		     list;
    GLenum		     listMode;
    xglDisplayListPtr	     pList;
    GLuint		     groupList;
    xglGLAttributesRec	     attrib;
    xglGLAttributesPtr	     pAttribStack;
    int			     nAttribStack;
} xglGLContextRec, *xglGLContextPtr;

xglGLContextPtr cctx = NULL;

static void
xglViewport (GLint   x,
	     GLint   y,
	     GLsizei width,
	     GLsizei height)
{
    cctx->attrib.viewport.x	 = x;
    cctx->attrib.viewport.y	 = y;
    cctx->attrib.viewport.width  = width;
    cctx->attrib.viewport.height = height;

    glitz_context_set_viewport (cctx->context, x, y, width, height);
}

static void
xglScissor (GLint   x,
	    GLint   y,
	    GLsizei width,
	    GLsizei height)
{
    cctx->attrib.scissor.x	= x;
    cctx->attrib.scissor.y	= y;
    cctx->attrib.scissor.width  = width;
    cctx->attrib.scissor.height = height;

    if (cctx->attrib.scissorTest)
	glitz_context_set_scissor (cctx->context, x, y, width, height);
}

static void
xglDrawBuffer (GLenum mode)
{
    if (mode != cctx->attrib.drawBuffer)
	ErrorF ("NYI xglDrawBuffer: 0x%x\n", mode);
}

static void
xglDisable (GLenum cap)
{
    switch (cap) {
    case GL_SCISSOR_TEST:
	if (cctx->attrib.scissorTest)
	{
	    cctx->attrib.scissorTest = GL_FALSE;
	    glitz_context_set_scissor (cctx->context,
				       0, 0,
				       cctx->pDrawBuffer->pDrawable->width,
				       cctx->pDrawBuffer->pDrawable->height);
	}
	break;
    default:
	glDisable (cap);
    }
}

static void
xglEnable (GLenum cap)
{
    switch (cap) {
    case GL_SCISSOR_TEST:
	cctx->attrib.scissorTest = GL_TRUE;
	glitz_context_set_scissor (cctx->context,
				   cctx->attrib.scissor.x,
				   cctx->attrib.scissor.y,
				   cctx->attrib.scissor.width,
				   cctx->attrib.scissor.height);
	break;
    default:
	glEnable (cap);
    }
}

static void
xglReadBuffer (GLenum mode)
{
    if (mode != cctx->attrib.readBuffer)
	ErrorF ("NYI xglReadBuffer: 0x%x\n", mode);
}

static void
xglPushAttrib (GLbitfield mask)
{
    xglGLAttributesPtr pAttrib;
    GLint	       depth;
    
    glGetIntegerv (GL_MAX_ATTRIB_STACK_DEPTH, &depth);
    
    if (cctx->nAttribStack == depth)
    {
	cctx->error = GL_STACK_OVERFLOW;
	return;
    }
    
    cctx->pAttribStack =
	realloc (cctx->pAttribStack,
		 sizeof (xglGLAttributesRec) * (cctx->nAttribStack + 1));
    
    if (!cctx->pAttribStack)
    {
	cctx->error = GL_OUT_OF_MEMORY;
	return;
    }

    pAttrib = &cctx->pAttribStack[cctx->nAttribStack];
    pAttrib->mask = mask;

    if (mask & GL_COLOR_BUFFER_BIT)
	pAttrib->drawBuffer = cctx->attrib.drawBuffer;

    if (mask & GL_PIXEL_MODE_BIT)
	pAttrib->readBuffer = cctx->attrib.readBuffer;
 
    if (mask & GL_SCISSOR_BIT)
    {
	pAttrib->scissorTest = cctx->attrib.scissorTest;
	pAttrib->scissor = cctx->attrib.scissor;
    }
    else if (mask & GL_ENABLE_BIT)
	pAttrib->scissorTest = cctx->attrib.scissorTest;

    if (mask & GL_VIEWPORT_BIT)
	pAttrib->viewport = cctx->attrib.viewport;

    cctx->nAttribStack++;
    
    glPushAttrib (mask);
}

static void
xglPopAttrib (void)
{
    xglGLAttributesPtr pAttrib;
    GLbitfield	       mask;
    
    if (!cctx->nAttribStack)
    {
	cctx->error = GL_STACK_UNDERFLOW;
	return;
    }

    cctx->nAttribStack--;
    
    pAttrib = &cctx->pAttribStack[cctx->nAttribStack];
    mask = pAttrib->mask;

    if (mask & GL_COLOR_BUFFER_BIT)
	xglDrawBuffer (pAttrib->drawBuffer);
	
    if (mask & GL_PIXEL_MODE_BIT)
	xglReadBuffer (pAttrib->readBuffer);
	
    if (mask & GL_SCISSOR_BIT)
    {
	xglScissor (pAttrib->scissor.x,
		    pAttrib->scissor.y,
		    pAttrib->scissor.width,
		    pAttrib->scissor.height);

	if (pAttrib->scissorTest)
	    xglEnable (GL_SCISSOR_TEST);
	else
	    xglDisable (GL_SCISSOR_TEST);
    }
    else if (mask & GL_ENABLE_BIT)
    {
	if (pAttrib->scissorTest)
	    xglEnable (GL_SCISSOR_TEST);
	else
	    xglDisable (GL_SCISSOR_TEST);
    }
    
    if (mask & GL_VIEWPORT_BIT)
	xglViewport (pAttrib->viewport.x,
		     pAttrib->viewport.y,
		     pAttrib->viewport.width,
		     pAttrib->viewport.height);

    cctx->pAttribStack =
	realloc (cctx->pAttribStack,
		 sizeof (xglGLAttributesRec) * cctx->nAttribStack);
    
    glPopAttrib ();
}

static GLint
xglGetTextureBinding (GLenum pname)
{
    xglTexObjPtr pTexObj;
    GLuint	 key;
    GLint	 texture;
    
    glGetIntegerv (pname, &texture);
    
    key = xglHashFirstEntry (cctx->shared->texObjects);
    while (key)
    {
	pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects, key);
	if (pTexObj && pTexObj->name == texture)
	    return key;
	
	key = xglHashNextEntry (cctx->shared->texObjects, key);
    }
    
    return 0;
}

#define INT_TO_BOOLEAN(I)  ((I) ? GL_TRUE : GL_FALSE)
#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)

static void
xglGetBooleanv (GLenum	  pname,
		GLboolean *params)
{
    switch (pname) {
    case GL_DOUBLEBUFFER:
	params[0] = GL_TRUE;
	break;
    case GL_DRAW_BUFFER:
	params[0] = ENUM_TO_BOOLEAN (cctx->attrib.drawBuffer);
	break;
    case GL_READ_BUFFER:
	params[0] = ENUM_TO_BOOLEAN (cctx->attrib.readBuffer);
	break;
    case GL_SCISSOR_BOX:
	params[0] = INT_TO_BOOLEAN (cctx->attrib.scissor.x);
	params[1] = INT_TO_BOOLEAN (cctx->attrib.scissor.y);
	params[2] = INT_TO_BOOLEAN (cctx->attrib.scissor.width);
	params[3] = INT_TO_BOOLEAN (cctx->attrib.scissor.height); 
	break;
    case GL_SCISSOR_TEST:
	params[0] = cctx->attrib.scissorTest;
	break;
    case GL_VIEWPORT:
	params[0] = INT_TO_BOOLEAN (cctx->attrib.viewport.x);
	params[1] = INT_TO_BOOLEAN (cctx->attrib.viewport.y);
	params[2] = INT_TO_BOOLEAN (cctx->attrib.viewport.width);
	params[3] = INT_TO_BOOLEAN (cctx->attrib.viewport.height);
	break;
    case GL_TEXTURE_BINDING_1D:
    case GL_TEXTURE_BINDING_2D:
    case GL_TEXTURE_BINDING_3D:
	/* should be safe to fall-through here */
    default:
	glGetBooleanv (pname, params);
    }
}

#define ENUM_TO_DOUBLE(E)    ((GLdouble) (E))
#define BOOLEAN_TO_DOUBLE(B) ((B) ? 1.0F : 0.0F)

static void
xglGetDoublev (GLenum	pname,
	       GLdouble *params)
{
    switch (pname) {
    case GL_DOUBLEBUFFER:
	params[0] = BOOLEAN_TO_DOUBLE (GL_TRUE);
	break;
    case GL_DRAW_BUFFER:
	params[0] = ENUM_TO_DOUBLE (cctx->attrib.drawBuffer);
	break;
    case GL_READ_BUFFER:
	params[0] = ENUM_TO_DOUBLE (cctx->attrib.readBuffer);
	break;
    case GL_SCISSOR_BOX:
	params[0] = cctx->attrib.scissor.x;
	params[1] = cctx->attrib.scissor.y;
	params[2] = cctx->attrib.scissor.width;
	params[3] = cctx->attrib.scissor.height; 
	break;
    case GL_SCISSOR_TEST:
	params[0] = BOOLEAN_TO_DOUBLE (cctx->attrib.scissorTest);
	break;
    case GL_VIEWPORT:
	params[0] = cctx->attrib.viewport.x;
	params[1] = cctx->attrib.viewport.y;
	params[2] = cctx->attrib.viewport.width;
	params[3] = cctx->attrib.viewport.height;
	break;
    case GL_TEXTURE_BINDING_1D:
    case GL_TEXTURE_BINDING_2D:
    case GL_TEXTURE_BINDING_3D:
	params[0] = xglGetTextureBinding (pname);
	break;
    default:
	glGetDoublev (pname, params);
    }
}

#define ENUM_TO_FLOAT(E)    ((GLfloat) (E))
#define BOOLEAN_TO_FLOAT(B) ((B) ? 1.0F : 0.0F)

static void
xglGetFloatv (GLenum  pname,
	      GLfloat *params)
{
    switch (pname) {
    case GL_DOUBLEBUFFER:
	params[0] = BOOLEAN_TO_FLOAT (GL_TRUE);
	break;
    case GL_DRAW_BUFFER:
	params[0] = ENUM_TO_FLOAT (cctx->attrib.drawBuffer);
	break;
    case GL_READ_BUFFER:
	params[0] = ENUM_TO_FLOAT (cctx->attrib.readBuffer);
	break;
    case GL_SCISSOR_BOX:
	params[0] = cctx->attrib.scissor.x;
	params[1] = cctx->attrib.scissor.y;
	params[2] = cctx->attrib.scissor.width;
	params[3] = cctx->attrib.scissor.height; 
	break;
    case GL_SCISSOR_TEST:
	params[0] = BOOLEAN_TO_FLOAT (cctx->attrib.scissorTest);
	break;
    case GL_VIEWPORT:
	params[0] = cctx->attrib.viewport.x;
	params[1] = cctx->attrib.viewport.y;
	params[2] = cctx->attrib.viewport.width;
	params[3] = cctx->attrib.viewport.height;
	break;
    case GL_TEXTURE_BINDING_1D:
    case GL_TEXTURE_BINDING_2D:
    case GL_TEXTURE_BINDING_3D:
	params[0] = xglGetTextureBinding (pname);
	break;
    default:
	glGetFloatv (pname, params);
    }
}

#define ENUM_TO_INT(E)    ((GLint) (E))
#define BOOLEAN_TO_INT(B) ((GLint) (B))

static void
xglGetIntegerv (GLenum pname,
		GLint  *params)
{
    switch (pname) {
    case GL_DOUBLEBUFFER:
	params[0] = BOOLEAN_TO_INT (GL_TRUE);
	break;
    case GL_DRAW_BUFFER:
	params[0] = ENUM_TO_INT (cctx->attrib.drawBuffer);
	break;
    case GL_READ_BUFFER:
	params[0] = ENUM_TO_INT (cctx->attrib.readBuffer);
	break;
    case GL_SCISSOR_BOX:
	params[0] = cctx->attrib.scissor.x;
	params[1] = cctx->attrib.scissor.y;
	params[2] = cctx->attrib.scissor.width;
	params[3] = cctx->attrib.scissor.height; 
	break;
    case GL_SCISSOR_TEST:
	params[0] = BOOLEAN_TO_INT (cctx->attrib.scissorTest);
	break;
    case GL_VIEWPORT:
	params[0] = cctx->attrib.viewport.x;
	params[1] = cctx->attrib.viewport.y;
	params[2] = cctx->attrib.viewport.width;
	params[3] = cctx->attrib.viewport.height;
	break;
    case GL_TEXTURE_BINDING_1D:
    case GL_TEXTURE_BINDING_2D:
    case GL_TEXTURE_BINDING_3D:
	params[0] = xglGetTextureBinding (pname);
	break;
    default:
	glGetIntegerv (pname, params);
    }
}

static GLboolean
xglIsEnabled (GLenum cap)
{
    switch (cap) {
    case GL_SCISSOR_TEST:
	return cctx->attrib.scissorTest;
    default:
	return glIsEnabled (cap);
    }
}

static GLenum
xglGetError (void)
{
    if (cctx->error != GL_NO_ERROR)
	return cctx->error;

    return glGetError ();
}

static const GLubyte *
xglGetString (GLenum name)
{
    switch (name) {
    case GL_VERSION:
	if (!cctx->versionString)
	{
	    static const char *version = "1.2 (%s)";
	    const char	      *nativeVersion = glGetString (GL_VERSION);

	    cctx->versionString = xalloc (strlen (version) +
					  strlen (nativeVersion));
	    if (cctx->versionString)
		sprintf (cctx->versionString, version, nativeVersion);
	}
	return cctx->versionString;
    default:
	return glGetString (name);
    }
}

static void
xglGenTextures (GLsizei n,
		GLuint  *textures)
{
    xglTexObjPtr pTexObj;
    GLuint	 name;
    
    name = xglHashFindFreeKeyBlock (cctx->shared->texObjects, n);

    glGenTextures (n, textures);

    while (n--)
    {
	pTexObj = xalloc (sizeof (xglTexObjRec));
	if (pTexObj)
	{
	    pTexObj->name = *textures;
	    xglHashInsert (cctx->shared->texObjects, name, pTexObj);
	}
	else
	{
	    glDeleteTextures (1, textures);
	    cctx->error = GL_OUT_OF_MEMORY;
	}
	
	*textures++ = name++;
    }
}

static void
xglBindTexture (GLenum target,
		GLuint texture)
{
    if (texture)
    {
	xglTexObjPtr pTexObj;
	
	pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects,
						texture);
	if (!pTexObj)
	{
	    pTexObj = xalloc (sizeof (xglTexObjRec));
	    if (!pTexObj)
	    {
		cctx->error = GL_OUT_OF_MEMORY;
		return;
	    }

	    glGenTextures (1, &pTexObj->name);
	    xglHashInsert (cctx->shared->texObjects, texture, pTexObj);
	}
	
	glBindTexture (target, pTexObj->name);
    }
    else
	glBindTexture (target, 0);
}
 
static GLboolean
xglAreTexturesResident (GLsizei	     n,
			const GLuint *textures,
			GLboolean    *residences)
{
    GLboolean allResident = GL_TRUE;
    int	      i, j;
    
    if (n < 0)
    {
	cctx->error = GL_INVALID_VALUE;
	return GL_FALSE;
    }
    
    if (!textures || !residences)
	return GL_FALSE;

    for (i = 0; i < n; i++)
    {
	xglTexObjPtr pTexObj;
	GLboolean    resident;
	
	if (textures[i] == 0)
	{
	    cctx->error = GL_INVALID_VALUE;
	    return GL_FALSE;
	}

	pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects,
						textures[i]);
	if (!pTexObj)
	{
	    cctx->error = GL_INVALID_VALUE;
	    return GL_FALSE;
	}
	
	if (glAreTexturesResident (1, &pTexObj->name, &resident))
	{
	    if (!allResident)
		residences[i] = GL_TRUE;
	}
	else
	{
	    if (allResident)
	    {
		allResident = GL_FALSE;

		for (j = 0; j < i; j++)
		    residences[j] = GL_TRUE;
	    }
	    residences[i] = GL_FALSE;
	}
    }
   
    return allResident;
}

static void
xglDeleteTextures (GLsizei	n,
		   const GLuint *textures)
{
    xglTexObjPtr pTexObj;
    
    while (n--)
    {
	pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects,
						*textures);
	if (pTexObj)
	{
	    glDeleteTextures (1, &pTexObj->name);
	    xglHashRemove (cctx->shared->texObjects, *textures);
	    xfree (pTexObj);
	}
	textures++;
    }
}

static GLboolean
xglIsTexture (GLuint texture)
{
    xglTexObjPtr pTexObj;

    if (!texture)
	return GL_FALSE;
    
    pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects, texture);
    if (pTexObj)
	return glIsTexture (pTexObj->name);

    return GL_FALSE;
}

static void
xglPrioritizeTextures (GLsizei	      n,
		       const GLuint   *textures,
		       const GLclampf *priorities)
{
    xglTexObjPtr pTexObj;
    int		 i;
    
    if (n < 0)
    {
	cctx->error = GL_INVALID_VALUE;
	return;
    }
    
    if (!priorities)
	return;
    
    for (i = 0; i < n; i++)
    {
	if (textures[i] <= 0)
	    continue;
	
	pTexObj = (xglTexObjPtr) xglHashLookup (cctx->shared->texObjects,
						textures[i]);
	if (pTexObj)
	    glPrioritizeTextures (1, &pTexObj->name, &priorities[i]);
    }
}

static xglDisplayListPtr
xglCreateList (void)
{
    xglDisplayListPtr pDisplayList;
    
    pDisplayList = xalloc (sizeof (xglDisplayListRec));
    if (!pDisplayList)
	return NULL;
    
    pDisplayList->pOp    = NULL;
    pDisplayList->nOp    = 0;
    pDisplayList->size   = 0;
    pDisplayList->refcnt = 1;

    return pDisplayList;
}

static void
xglDestroyList (xglDisplayListPtr pDisplayList)
{
    xglListOpPtr pOp = pDisplayList->pOp;
    int		 nOp = pDisplayList->nOp;

    pDisplayList->refcnt--;
    if (pDisplayList->refcnt)
	return;

    while (nOp--)
    {
	switch (pOp->type) {
	case XGL_LIST_OP_CALLS:
	case XGL_LIST_OP_DRAW:
	    glDeleteLists (pOp->u.name, 1);
	    break;    
	case XGL_LIST_OP_COPY:
	    xfree (pOp->u.copy);
	    break;
	case XGL_LIST_OP_LIST:
	    xglDestroyList (pOp->u.list);
	    break;
	}
	
	pOp++;
    }
    
    if (pDisplayList->pOp)
	xfree (pDisplayList->pOp);

    xfree (pDisplayList);
}

static Bool
xglResizeList (xglDisplayListPtr pDisplayList,
	       int		 nOp)
{
    if (pDisplayList->size < nOp)
    {
	int size = pDisplayList->nOp ? pDisplayList->nOp : 4;

	while (size < nOp)
	    size <<= 1;
	
	pDisplayList->pOp = xrealloc (pDisplayList->pOp,
				      sizeof (xglListOpRec) * size);
	if (!pDisplayList->pOp)
	    return FALSE;
	
	pDisplayList->size = size;
    }

    return TRUE;
}

static void
xglStartList (int    type,
	      GLenum mode)
{
    if (!xglResizeList (cctx->pList, cctx->pList->nOp + 1))
    {
	cctx->error = GL_OUT_OF_MEMORY;
	return;
    }
    
    cctx->pList->pOp[cctx->pList->nOp].type   = type;
    cctx->pList->pOp[cctx->pList->nOp].u.name = glGenLists (1);

    glNewList (cctx->pList->pOp[cctx->pList->nOp].u.name, mode);

    cctx->pList->nOp++;
}

static GLuint
xglGenLists (GLsizei range)
{
    xglDisplayListPtr pDisplayList;
    GLuint	      first, name;
    
    first = xglHashFindFreeKeyBlock (cctx->shared->displayLists, range);

    name = first;
    for (name = first; range--; name++)
    {
	pDisplayList = xglCreateList ();
	if (pDisplayList)
	{
	    xglHashInsert (cctx->shared->displayLists, name, pDisplayList);
	}
	else
	{
	    cctx->error = GL_OUT_OF_MEMORY;
	}
    }

    return first;
}

static void
xglNewList (GLuint list,
	    GLenum mode)
{
    if (!list)
    {
	cctx->error = GL_INVALID_VALUE;
	return;
    }

    if (cctx->list)
    {
	cctx->error = GL_INVALID_OPERATION;
	return;
    }

    cctx->pList = xglCreateList ();
    if (!cctx->pList)
    {
	cctx->error = GL_OUT_OF_MEMORY;
	return;
    }

    cctx->list     = list;
    cctx->listMode = mode;

    xglStartList (XGL_LIST_OP_CALLS, mode);
}

static void
xglEndList (void)
{
    xglDisplayListPtr pDisplayList;

    if (!cctx->list)
    {
	cctx->error = GL_INVALID_OPERATION;
	return;
    }

    glEndList ();

    pDisplayList = (xglDisplayListPtr)
	xglHashLookup (cctx->shared->displayLists, cctx->list);
    if (pDisplayList)
    {
	xglHashRemove (cctx->shared->displayLists, cctx->list);
	xglDestroyList (pDisplayList);
    }

    xglHashInsert (cctx->shared->displayLists, cctx->list, cctx->pList);

    cctx->list = 0;
}

#define XGL_GLX_DRAW_DECLARATIONS(pBox, nBox)				   \
    GCPtr	   pGC = cctx->pDrawBuffer->pGC;			   \
    BoxPtr	   (pBox) = REGION_RECTS (pGC->pCompositeClip);		   \
    int		   (nBox) = REGION_NUM_RECTS (pGC->pCompositeClip);	   \
    DrawablePtr	   pDrawable = cctx->pDrawBuffer->pDrawable;		   \
    int		   scissorX1 = cctx->attrib.scissor.x;			   \
    int		   scissorX2 = scissorX1 + cctx->attrib.scissor.width;	   \
    int		   scissorY2 = pDrawable->height - cctx->attrib.scissor.y; \
    int		   scissorY1 = scissorY2 - cctx->attrib.scissor.height

#define XGL_GLX_DRAW_SCISSOR_CLIP(box) \
    if ((box)->x1 < scissorX1)	       \
	(box)->x1 = scissorX1;	       \
    if ((box)->y1 < scissorY1)	       \
	(box)->y1 = scissorY1;	       \
    if ((box)->x2 > scissorX2)	       \
	(box)->x2 = scissorX2;	       \
    if ((box)->y2 > scissorY2)	       \
	(box)->y2 = scissorY2

#define XGL_GLX_DRAW_DAMAGE(pBox, pRegion)	      \
    if (cctx->attrib.drawBuffer == GL_FRONT)	      \
    {						      \
	REGION_INIT (pGC->pScreen, pRegion, pBox, 1); \
	REGION_UNION (pGC->pScreen,		      \
		      &cctx->pDrawBuffer->damage,     \
		      &cctx->pDrawBuffer->damage,     \
		      pRegion);			      \
	REGION_UNINIT (pGC->pScreen, pRegion);	      \
    } 

#define XGL_GLX_DRAW_SET_SCISSOR_BOX(box)		      \
    glitz_context_set_scissor (cctx->context,		      \
			       (box)->x1,		      \
			       pDrawable->height - (box)->y2, \
			       (box)->x2 - (box)->x1,	      \
			       (box)->y2 - (box)->y1)
		    
		    
static void
xglDrawList (GLuint list)
{
    RegionRec region;
    BoxRec    box;
	
    XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

    while (nBox--)
    {
	box = *pBox++;
	
	XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	
	if (box.x1 < box.x2 && box.y1 < box.y2)
	{
	    XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

	    glCallList (list);

	    XGL_GLX_DRAW_DAMAGE (&box, &region);
	}
    }
}

static void
xglCallDisplayList (xglDisplayListPtr pDisplayList)
{
    if (cctx->list)
    {
	if (!xglResizeList (cctx->pList, cctx->pList->nOp + 1))
	{
	    cctx->error = GL_OUT_OF_MEMORY;
	    return;
	}

	pDisplayList->refcnt++;

	cctx->pList->pOp[cctx->pList->nOp].type   = XGL_LIST_OP_LIST;
	cctx->pList->pOp[cctx->pList->nOp].u.list = pDisplayList;
	cctx->pList->nOp++;
    }
    else
    {
	xglListOpPtr pOp = pDisplayList->pOp;
	int	     nOp = pDisplayList->nOp;

	while (nOp--)
	{
	    switch (pOp->type) {
	    case XGL_LIST_OP_CALLS:
		glCallList (pOp->u.name);
		break;
	    case XGL_LIST_OP_DRAW:
		xglDrawList (pOp->u.name);
		break;
	    case XGL_LIST_OP_COPY:
		(*pOp->u.copy->copyProc) (pOp->u.copy);
		break;
	    case XGL_LIST_OP_LIST:
		xglCallDisplayList (pOp->u.list);
		break;
	    }

	    pOp++;
	}
    }
}

static void
xglCallList (GLuint list)
{
    xglDisplayListPtr pDisplayList;
    
    if (!list)
    {
	cctx->error = GL_INVALID_VALUE;
	return;
    }
       
    pDisplayList = (xglDisplayListPtr)
	xglHashLookup (cctx->shared->displayLists, list);
    if (pDisplayList)
	xglCallDisplayList (pDisplayList);
}

static void
xglCallLists (GLsizei	   n,
	      GLenum	   type,
	      const GLvoid *lists)
{
    xglDisplayListPtr pDisplayList;
    GLuint	      list;
    GLint	      base, i;

    glGetIntegerv (GL_LIST_BASE, &base);

    for (i = 0; i < n; i++)
    {
	switch (type) {
	case GL_BYTE:
	    list = (GLuint) *(((GLbyte *) lists) + n);
	    break;
	case GL_UNSIGNED_BYTE:
	    list = (GLuint) *(((GLubyte *) lists) + n);
	    break;
	case GL_SHORT:
	    list = (GLuint) *(((GLshort *) lists) + n);
	    break;
	case GL_UNSIGNED_SHORT:
	    list = (GLuint) *(((GLushort *) lists) + n);
	    break;
	case GL_INT:
	    list = (GLuint) *(((GLint *) lists) + n);
	    break;
	case GL_UNSIGNED_INT:
	    list = (GLuint) *(((GLuint *) lists) + n);
	    break;
	case GL_FLOAT:
	    list = (GLuint) *(((GLfloat *) lists) + n);
	    break;
	case GL_2_BYTES:
	{
	    GLubyte *ubptr = ((GLubyte *) lists) + 2 * n;
	    list = (GLuint) *ubptr * 256 + (GLuint) *(ubptr + 1);
	} break;
	case GL_3_BYTES:
	{
	    GLubyte *ubptr = ((GLubyte *) lists) + 3 * n;
	    list = (GLuint) * ubptr * 65536
		+ (GLuint) * (ubptr + 1) * 256
		+ (GLuint) * (ubptr + 2);
	} break;
	case GL_4_BYTES:
	{
	    GLubyte *ubptr = ((GLubyte *) lists) + 4 * n;
	    list = (GLuint) * ubptr * 16777216
		+ (GLuint) * (ubptr + 1) * 65536
		+ (GLuint) * (ubptr + 2) * 256
		+ (GLuint) * (ubptr + 3);
	} break;
	default:
	    cctx->error = GL_INVALID_ENUM;
	    return;
	}
	
	pDisplayList = (xglDisplayListPtr)
	    xglHashLookup (cctx->shared->displayLists, base + list);
	if (pDisplayList)
	    xglCallDisplayList (pDisplayList);
    }
}

static void
xglDeleteLists (GLuint  list,
		GLsizei range)
{
    xglDisplayListPtr pDisplayList;
    GLint	      i;
    
    if (range < 0)
    {
	cctx->error = GL_INVALID_VALUE;
	return;
    }
    
    for (i = 0; i < range; i++)
    {
	pDisplayList = (xglDisplayListPtr)
	    xglHashLookup (cctx->shared->displayLists, list + i);
	if (pDisplayList)
	{
	    xglHashRemove (cctx->shared->displayLists, list + i);
	    xglDestroyList (pDisplayList);
	}
    }
}

static GLboolean
xglIsList (GLuint list)
{
    xglDisplayListPtr pDisplayList;

    if (!list)
	return GL_FALSE;
    
    pDisplayList = (xglDisplayListPtr)
	xglHashLookup (cctx->shared->displayLists, list);
    if (pDisplayList)
	return GL_TRUE;

    return GL_FALSE;
}

static void
xglFlush (void)
{
    xglGLBufferPtr pBuffer = cctx->pDrawBuffer;
    
    glFlush ();

    if (REGION_NOTEMPTY (pBuffer->pDrawable->pScreen, &pBuffer->damage))
    {
	DamageDamageRegion (pBuffer->pDrawable, &pBuffer->damage);
	REGION_EMPTY (pBuffer->pDrawable->pScreen, &pBuffer->damage);
    }
}

static void
xglFinish (void)
{
    xglGLBufferPtr pBuffer = cctx->pDrawBuffer;
    
    glFinish ();

    if (REGION_NOTEMPTY (pBuffer->pDrawable->pScreen, &pBuffer->damage))
    {
	DamageDamageRegion (pBuffer->pDrawable, &pBuffer->damage);
	REGION_EMPTY (pBuffer->pDrawable->pScreen, &pBuffer->damage);
    }
}

static void
xglClear (GLbitfield mask)
{
    GLenum mode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glClear (mask);
	glEndList ();
	
	mode = cctx->listMode;
    }
    else
	mode = GL_COMPILE_AND_EXECUTE;

    if (mode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec	  box;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);
    
	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glClear (mask);
		
		if (mask & GL_COLOR_BUFFER_BIT)
		    XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }

    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglAccum (GLenum  op,
	  GLfloat value)
{
    if (op == GL_RETURN)
    {
	GLenum listMode;
    
	if (cctx->list)
	{
	    glEndList ();
	    xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	    glAccum (GL_RETURN, value);
	    glEndList ();
	    
	    listMode = cctx->listMode;
	}
	else
	    listMode = GL_COMPILE_AND_EXECUTE;
	
	if (listMode == GL_COMPILE_AND_EXECUTE)
	{
	    RegionRec region;
	    BoxRec    box;

	    XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);
	
	    while (nBox--)
	    {
		box = *pBox++;
		
		XGL_GLX_DRAW_SCISSOR_CLIP (&box);
		
		if (box.x1 < box.x2 && box.y1 < box.y2)
		{
		    XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		    glAccum (GL_RETURN, value);
		    
		    XGL_GLX_DRAW_DAMAGE (&box, &region);
		}
	    }
	}
	
	if (cctx->list)
	    xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
    }
    else
	glAccum (op, value);
}

static void
xglDrawArrays (GLenum  mode,
	       GLint   first,
	       GLsizei count)
{
    GLenum listMode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glDrawArrays (mode, first, count);
	glEndList ();
	    
	listMode = cctx->listMode;
    }
    else
	listMode = GL_COMPILE_AND_EXECUTE;
	
    if (listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec    box;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glDrawArrays (mode, first, count);
	
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }

    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglDrawElements (GLenum	      mode,
		 GLsizei      count,
		 GLenum	      type,
		 const GLvoid *indices)
{
    GLenum listMode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glDrawElements (mode, count, type, indices);
	glEndList ();
	    
	listMode = cctx->listMode;
    }
    else
	listMode = GL_COMPILE_AND_EXECUTE;
	
    if (listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec    box;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glDrawElements (mode, count, type, indices);
	
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }

    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglDrawPixels (GLsizei	    width,
	       GLsizei	    height,
	       GLenum	    format,
	       GLenum	    type,
	       const GLvoid *pixels)
{
    GLenum listMode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glDrawPixels (width, height, format, type, pixels);
	glEndList ();
	    
	listMode = cctx->listMode;
    }
    else
	listMode = GL_COMPILE_AND_EXECUTE;
	
    if (listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec    box;
	    
	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glDrawPixels (width, height, format, type, pixels);

		if (format != GL_STENCIL_INDEX)
		    XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }

    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglBitmap (GLsizei	 width,
	   GLsizei	 height,
	   GLfloat	 xorig,
	   GLfloat	 yorig,
	   GLfloat	 xmove,
	   GLfloat	 ymove,
	   const GLubyte *bitmap)
{
    GLenum listMode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glBitmap (width, height, xorig, yorig, xmove, ymove, bitmap);
	glEndList ();
	    
	listMode = cctx->listMode;
    }
    else
	listMode = GL_COMPILE_AND_EXECUTE;
	
    if (listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec    box;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glBitmap (width, height, xorig, yorig, xmove, ymove, bitmap);
		
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }
    
    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglRectdv (const GLdouble *v1,
	   const GLdouble *v2)
{
    GLenum listMode;
    
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
	glRectdv (v1, v2);
	glEndList ();
	    
	listMode = cctx->listMode;
    }
    else
	listMode = GL_COMPILE_AND_EXECUTE;
	
    if (listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec    box;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glRectdv (v1, v2);
		
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }

    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglRectfv (const GLfloat *v1,
	   const GLfloat *v2)
{
    GLdouble dv1[2];
    GLdouble dv2[2];

    dv1[0] = (GLdouble) v1[0];
    dv1[1] = (GLdouble) v1[1];
    dv2[0] = (GLdouble) v2[0];
    dv2[1] = (GLdouble) v2[1];

    xglRectdv (dv1, dv2);
}

static void
xglRectiv (const GLint *v1,
	   const GLint *v2)
{
    GLdouble dv1[2];
    GLdouble dv2[2];

    dv1[0] = (GLdouble) v1[0];
    dv1[1] = (GLdouble) v1[1];
    dv2[0] = (GLdouble) v2[0];
    dv2[1] = (GLdouble) v2[1];

    xglRectdv (dv1, dv2);
}

static void
xglRectsv (const GLshort *v1,
	   const GLshort *v2)
{
    GLdouble dv1[2];
    GLdouble dv2[2];

    dv1[0] = (GLdouble) v1[0];
    dv1[1] = (GLdouble) v1[1];
    dv2[0] = (GLdouble) v2[0];
    dv2[1] = (GLdouble) v2[1];

    xglRectdv (dv1, dv2);
}

static void
xglBegin (GLenum mode)
{
    if (cctx->list)
    {
	glEndList ();
	xglStartList (XGL_LIST_OP_DRAW, GL_COMPILE);
    }
    else
    {
	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);
	    
	if (nBox == 1)
	{
	    BoxRec box = *pBox;

	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);
	}
	else
	{
	    if (!cctx->groupList)
		cctx->groupList = glGenLists (1);
	    
	    glNewList (cctx->groupList, GL_COMPILE);
	}
    }
    
    glBegin (mode);
}

static void
xglEnd (void)
{
    glEnd ();
    
    if (!cctx->list || cctx->listMode == GL_COMPILE_AND_EXECUTE)
    {
	RegionRec region;
	BoxRec	  box;
	GLuint	  list;

	XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);
	
	if (cctx->list)
	{
	    list = cctx->pList->pOp[cctx->pList->nOp - 1].u.name;
	}
	else
	{
	    if (nBox == 1)
	    {
		box = *pBox++;
	    
		XGL_GLX_DRAW_SCISSOR_CLIP (&box);
		XGL_GLX_DRAW_DAMAGE (&box, &region);
		return;
	    }
	
	    list = cctx->groupList;
	}

	glEndList ();

	while (nBox--)
	{
	    box = *pBox++;
	    
	    XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	    
	    if (box.x1 < box.x2 && box.y1 < box.y2)
	    {
		XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

		glCallList (list);
		
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	    }
	}
    }
	
    if (cctx->list)
	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
}

static void
xglCopyOp (xglCopyOpPtr pOp)
{
    if (cctx->list)
    {
	xglCopyOpPtr pCopyOp;

	pCopyOp = xalloc (sizeof (xglCopyOpRec));
	if (!pCopyOp)
	{
	    cctx->error = GL_OUT_OF_MEMORY;
	    return;
	}
	
	if (!xglResizeList (cctx->pList, cctx->pList->nOp + 1))
	{
	    xfree (pCopyOp);
	    cctx->error = GL_OUT_OF_MEMORY;
	    return;
	}

	glEndList ();

	*pCopyOp = *pOp;
	
	cctx->pList->pOp[cctx->pList->nOp].type   = XGL_LIST_OP_COPY;
	cctx->pList->pOp[cctx->pList->nOp].u.copy = pCopyOp;
	cctx->pList->nOp++;

	if (cctx->listMode == GL_COMPILE_AND_EXECUTE)
	    (*pOp->copyProc) (pOp);

	xglStartList (XGL_LIST_OP_CALLS, cctx->listMode);
    }
    else
	(*pOp->copyProc) (pOp);   
}

static void
xglDoCopyPixels (xglCopyOpPtr pOp)
{
    RegionRec region;
    BoxRec    box;
	    
    XGL_GLX_DRAW_DECLARATIONS (pBox, nBox);

    while (nBox--)
    {
	box = *pBox++;
	
	XGL_GLX_DRAW_SCISSOR_CLIP (&box);
	
	if (box.x1 < box.x2 && box.y1 < box.y2)
	{
	    XGL_GLX_DRAW_SET_SCISSOR_BOX (&box);

	    glCopyPixels (cctx->tx + pOp->u.pixels.x,
			  cctx->ty + pOp->u.pixels.y,
			  pOp->u.pixels.width,
			  pOp->u.pixels.height,
			  pOp->u.pixels.type);

	    if (pOp->u.pixels.type == GL_COLOR)
		XGL_GLX_DRAW_DAMAGE (&box, &region);
	}
    }
}

static void
xglCopyPixels (GLint   x,
	       GLint   y,
	       GLsizei width,
	       GLsizei height,
	       GLenum  type)
{
    xglCopyOpRec copy;
    
    copy.copyProc	 = xglDoCopyPixels;
    copy.u.pixels.x	 = x;
    copy.u.pixels.y	 = y;
    copy.u.pixels.width  = width;
    copy.u.pixels.height = height;
    copy.u.pixels.type	 = type;

    xglCopyOp (&copy);   
}

static void
xglReadPixels (GLint   x,
	       GLint   y,
	       GLsizei width,
	       GLsizei height,
	       GLenum  format,
	       GLenum  type,
	       GLvoid  *pixels)
{
    glReadPixels (cctx->tx + x, cctx->ty + y,
		  width, height, format, type, pixels);
}

static void
xglDoCopyTexImage1D (xglCopyOpPtr pOp)
{
    glCopyTexImage1D (pOp->u.tex_image_1d.target,
		      pOp->u.tex_image_1d.level,
		      pOp->u.tex_image_1d.internalformat,
		      cctx->tx + pOp->u.tex_image_1d.x,
		      cctx->ty + pOp->u.tex_image_1d.y,
		      pOp->u.tex_image_1d.width,
		      pOp->u.tex_image_1d.border);
}

static void
xglCopyTexImage1D (GLenum  target,
		   GLint   level,
		   GLenum  internalformat,
		   GLint   x,
		   GLint   y,
		   GLsizei width,
		   GLint   border)
{
    xglCopyOpRec copy;
    
    copy.copyProc		       = xglDoCopyTexImage1D;
    copy.u.tex_image_1d.target	       = target;
    copy.u.tex_image_1d.level	       = level;
    copy.u.tex_image_1d.internalformat = internalformat;
    copy.u.tex_image_1d.x	       = x;
    copy.u.tex_image_1d.y	       = y;
    copy.u.tex_image_1d.width	       = width;
    copy.u.tex_image_1d.border	       = border;

    xglCopyOp (&copy);
}

static void
xglDoCopyTexImage2D (xglCopyOpPtr pOp)
{
    glCopyTexImage2D (pOp->u.tex_image_2d.target,
		      pOp->u.tex_image_2d.level,
		      pOp->u.tex_image_2d.internalformat,
		      cctx->tx + pOp->u.tex_image_2d.x,
		      cctx->ty + pOp->u.tex_image_2d.y,
		      pOp->u.tex_image_2d.width,
		      pOp->u.tex_image_2d.height,
		      pOp->u.tex_image_2d.border);
}

static void
xglCopyTexImage2D (GLenum  target,
		   GLint   level,
		   GLenum  internalformat,
		   GLint   x,
		   GLint   y,
		   GLsizei width,
		   GLsizei height,
		   GLint   border)
{
    xglCopyOpRec copy;
    
    copy.copyProc		       = xglDoCopyTexImage2D;
    copy.u.tex_image_2d.target	       = target;
    copy.u.tex_image_2d.level	       = level;
    copy.u.tex_image_2d.internalformat = internalformat;
    copy.u.tex_image_2d.x	       = x;
    copy.u.tex_image_2d.y	       = y;
    copy.u.tex_image_2d.width	       = width;
    copy.u.tex_image_2d.height	       = height;
    copy.u.tex_image_2d.border	       = border;

    xglCopyOp (&copy);
}

static void
xglDoCopyTexSubImage1D (xglCopyOpPtr pOp)
{
    glCopyTexSubImage1D (pOp->u.tex_sub_image_1d.target,
			 pOp->u.tex_sub_image_1d.level,
			 pOp->u.tex_sub_image_1d.xoffset,
			 cctx->tx + pOp->u.tex_sub_image_1d.x,
			 cctx->ty + pOp->u.tex_sub_image_1d.y,
			 pOp->u.tex_sub_image_1d.width);
}

static void
xglCopyTexSubImage1D (GLenum  target,
		      GLint   level,
		      GLint   xoffset,
		      GLint   x,
		      GLint   y,
		      GLsizei width)
{
    xglCopyOpRec copy;
    
    copy.copyProc		    = xglDoCopyTexSubImage1D;
    copy.u.tex_sub_image_1d.target  = target;
    copy.u.tex_sub_image_1d.level   = level;
    copy.u.tex_sub_image_1d.xoffset = xoffset;
    copy.u.tex_sub_image_1d.x	    = x;
    copy.u.tex_sub_image_1d.y	    = y;
    copy.u.tex_sub_image_1d.width   = width;

    xglCopyOp (&copy);
}

static void
xglDoCopyTexSubImage2D (xglCopyOpPtr pOp)
{
    glCopyTexSubImage2D (pOp->u.tex_sub_image_2d.target,
			 pOp->u.tex_sub_image_2d.level,
			 pOp->u.tex_sub_image_2d.xoffset,
			 pOp->u.tex_sub_image_2d.yoffset,
			 cctx->tx + pOp->u.tex_sub_image_2d.x,
			 cctx->ty + pOp->u.tex_sub_image_2d.y,
			 pOp->u.tex_sub_image_2d.width,
			 pOp->u.tex_sub_image_2d.height);
}

static void
xglCopyTexSubImage2D (GLenum  target,
		      GLint   level,
		      GLint   xoffset,
		      GLint   yoffset,
		      GLint   x,
		      GLint   y,
		      GLsizei width,
		      GLsizei height)
{
    xglCopyOpRec copy;
    
    copy.copyProc		    = xglDoCopyTexSubImage2D;
    copy.u.tex_sub_image_2d.target  = target;
    copy.u.tex_sub_image_2d.level   = level;
    copy.u.tex_sub_image_2d.xoffset = xoffset;
    copy.u.tex_sub_image_2d.yoffset = yoffset;
    copy.u.tex_sub_image_2d.x	    = x;
    copy.u.tex_sub_image_2d.y	    = y;
    copy.u.tex_sub_image_2d.width   = width;
    copy.u.tex_sub_image_2d.height  = height;

    xglCopyOp (&copy);
}    

static void
xglDoCopyColorTable (xglCopyOpPtr pOp)
{
    glCopyColorTable (pOp->u.color_table.target,
		      pOp->u.color_table.internalformat,
		      cctx->tx + pOp->u.color_table.x,
		      cctx->ty + pOp->u.color_table.y,
		      pOp->u.color_table.width);
}

static void
xglCopyColorTable (GLenum  target,
		   GLenum  internalformat,
		   GLint   x,
		   GLint   y,
		   GLsizei width)
{
    xglCopyOpRec copy;
    
    copy.copyProc		      = xglDoCopyColorTable;
    copy.u.color_table.target	      = target;
    copy.u.color_table.internalformat = internalformat;
    copy.u.color_table.x	      = x;
    copy.u.color_table.y	      = y;
    copy.u.color_table.width	      = width;
    
    xglCopyOp (&copy);
}

static void
xglDoCopyColorSubTable (xglCopyOpPtr pOp)
{
    glCopyColorTable (pOp->u.color_sub_table.target,
		      pOp->u.color_sub_table.start,
		      cctx->tx + pOp->u.color_sub_table.x,
		      cctx->ty + pOp->u.color_sub_table.y,
		      pOp->u.color_sub_table.width);
}

static void
xglCopyColorSubTable (GLenum  target,
		      GLsizei start,
		      GLint   x,
		      GLint   y,
		      GLsizei width)
{
    xglCopyOpRec copy;
    
    copy.copyProc		  = xglDoCopyColorSubTable;
    copy.u.color_sub_table.target = target;
    copy.u.color_sub_table.start  = start;
    copy.u.color_sub_table.x	  = x;
    copy.u.color_sub_table.y	  = y;
    copy.u.color_sub_table.width  = width;
    
    xglCopyOp (&copy);
}

static void
xglDoCopyConvolutionFilter1D (xglCopyOpPtr pOp)
{
    glCopyConvolutionFilter1D (pOp->u.convolution_filter_1d.target,
			       pOp->u.convolution_filter_1d.internalformat,
			       cctx->tx + pOp->u.convolution_filter_1d.x,
			       cctx->ty + pOp->u.convolution_filter_1d.y,
			       pOp->u.convolution_filter_1d.width);
}

static void
xglCopyConvolutionFilter1D (GLenum  target,
			    GLenum  internalformat,
			    GLint   x,
			    GLint   y,
			    GLsizei width)
{
    xglCopyOpRec copy;
    
    copy.copyProc				= xglDoCopyConvolutionFilter1D;
    copy.u.convolution_filter_1d.target		= target;
    copy.u.convolution_filter_1d.internalformat = internalformat;
    copy.u.convolution_filter_1d.x		= x;
    copy.u.convolution_filter_1d.y		= y;
    copy.u.convolution_filter_1d.width		= width;
    
    xglCopyOp (&copy);
}

static void
xglDoCopyConvolutionFilter2D (xglCopyOpPtr pOp)
{
    glCopyConvolutionFilter2D (pOp->u.convolution_filter_2d.target,
			       pOp->u.convolution_filter_2d.internalformat,
			       cctx->tx + pOp->u.convolution_filter_2d.x,
			       cctx->ty + pOp->u.convolution_filter_2d.y,
			       pOp->u.convolution_filter_2d.width,
			       pOp->u.convolution_filter_2d.height);
}

static void
xglCopyConvolutionFilter2D (GLenum  target,
			    GLenum  internalformat,
			    GLint   x,
			    GLint   y,
			    GLsizei width,
			    GLsizei height)
{
    xglCopyOpRec copy;
    
    copy.copyProc				= xglDoCopyConvolutionFilter2D;
    copy.u.convolution_filter_2d.target		= target;
    copy.u.convolution_filter_2d.internalformat = internalformat;
    copy.u.convolution_filter_2d.x		= x;
    copy.u.convolution_filter_2d.y		= y;
    copy.u.convolution_filter_2d.width		= width;
    copy.u.convolution_filter_2d.height		= height;
    
    xglCopyOp (&copy);
}

static void
xglDoCopyTexSubImage3D (xglCopyOpPtr pOp)
{
    glCopyTexSubImage3D (pOp->u.tex_sub_image_3d.target,
			 pOp->u.tex_sub_image_3d.level,
			 pOp->u.tex_sub_image_3d.xoffset,
			 pOp->u.tex_sub_image_3d.yoffset,
			 pOp->u.tex_sub_image_3d.zoffset,
			 cctx->tx + pOp->u.tex_sub_image_3d.x,
			 cctx->ty + pOp->u.tex_sub_image_3d.y,
			 pOp->u.tex_sub_image_3d.width,
			 pOp->u.tex_sub_image_3d.height);
}

static void
xglCopyTexSubImage3D (GLenum  target,
		      GLint   level,
		      GLint   xoffset,
		      GLint   yoffset,
		      GLint   zoffset,
		      GLint   x,
		      GLint   y,
		      GLsizei width,
		      GLsizei height)
{
    xglCopyOpRec copy;
    
    copy.copyProc		    = xglDoCopyTexSubImage3D;
    copy.u.tex_sub_image_3d.target  = target;
    copy.u.tex_sub_image_3d.level   = level;
    copy.u.tex_sub_image_3d.xoffset = xoffset;
    copy.u.tex_sub_image_3d.yoffset = yoffset;
    copy.u.tex_sub_image_3d.zoffset = zoffset;
    copy.u.tex_sub_image_3d.x	    = x;
    copy.u.tex_sub_image_3d.y	    = y;
    copy.u.tex_sub_image_3d.width   = width;
    copy.u.tex_sub_image_3d.height  = height;

    xglCopyOp (&copy);
}    

__glProcTable __glNativeRenderTable = {
    xglNewList, /* glNewList */
    xglEndList, /* glEndList */
    xglCallList, /* glCallList */
    xglCallLists, /* glCallLists */
    xglDeleteLists, /* glDeleteLists */
    xglGenLists, /* glGenLists */
    glListBase,
    xglBegin, /* glBegin */
    xglBitmap, /* glBitmap */
    glColor3bv,
    glColor3dv,
    glColor3fv,
    glColor3iv,
    glColor3sv,
    glColor3ubv,
    glColor3uiv,
    glColor3usv,
    glColor4bv,
    glColor4dv,
    glColor4fv,
    glColor4iv,
    glColor4sv,
    glColor4ubv,
    glColor4uiv,
    glColor4usv,
    glEdgeFlagv,
    xglEnd, /* glEnd */
    glIndexdv,
    glIndexfv,
    glIndexiv,
    glIndexsv,
    glNormal3bv,
    glNormal3dv,
    glNormal3fv,
    glNormal3iv,
    glNormal3sv,
    glRasterPos2dv,
    glRasterPos2fv,
    glRasterPos2iv,
    glRasterPos2sv,
    glRasterPos3dv,
    glRasterPos3fv,
    glRasterPos3iv,
    glRasterPos3sv,
    glRasterPos4dv,
    glRasterPos4fv,
    glRasterPos4iv,
    glRasterPos4sv,
    xglRectdv, /* glRectdv */
    xglRectfv, /* glRectfv */
    xglRectiv, /* glRectiv */
    xglRectsv, /* glRectsv */
    glTexCoord1dv,
    glTexCoord1fv,
    glTexCoord1iv,
    glTexCoord1sv,
    glTexCoord2dv,
    glTexCoord2fv,
    glTexCoord2iv,
    glTexCoord2sv,
    glTexCoord3dv,
    glTexCoord3fv,
    glTexCoord3iv,
    glTexCoord3sv,
    glTexCoord4dv,
    glTexCoord4fv,
    glTexCoord4iv,
    glTexCoord4sv,
    glVertex2dv,
    glVertex2fv,
    glVertex2iv,
    glVertex2sv,
    glVertex3dv,
    glVertex3fv,
    glVertex3iv,
    glVertex3sv,
    glVertex4dv,
    glVertex4fv,
    glVertex4iv,
    glVertex4sv,
    glClipPlane,
    glColorMaterial,
    glCullFace,
    glFogf,
    glFogfv,
    glFogi,
    glFogiv,
    glFrontFace,
    glHint,
    glLightf,
    glLightfv,
    glLighti,
    glLightiv,
    glLightModelf,
    glLightModelfv,
    glLightModeli,
    glLightModeliv,
    glLineStipple,
    glLineWidth,
    glMaterialf,
    glMaterialfv,
    glMateriali,
    glMaterialiv,
    glPointSize,
    glPolygonMode,
    glPolygonStipple,
    xglScissor, /* glScissor */
    glShadeModel,
    glTexParameterf,
    glTexParameterfv,
    glTexParameteri,
    glTexParameteriv,
    glTexImage1D,
    glTexImage2D,
    glTexEnvf,
    glTexEnvfv,
    glTexEnvi,
    glTexEnviv,
    glTexGend,
    glTexGendv,
    glTexGenf,
    glTexGenfv,
    glTexGeni,
    glTexGeniv,
    glFeedbackBuffer,
    glSelectBuffer,
    glRenderMode,
    glInitNames,
    glLoadName,
    glPassThrough,
    glPopName,
    glPushName,
    xglDrawBuffer, /* glDrawBuffer */
    xglClear, /* glClear */
    glClearAccum,
    glClearIndex,
    glClearColor,
    glClearStencil,
    glClearDepth,
    glStencilMask,
    glColorMask,
    glDepthMask,
    glIndexMask,
    xglAccum, /* glAccum */
    xglDisable, /* glDisable */
    xglEnable, /* glEnable */
    xglFinish, /* glFinish */
    xglFlush, /* glFlush */
    xglPopAttrib, /* glPopAttrib */
    xglPushAttrib, /* glPushAttrib */
    glMap1d,
    glMap1f,
    glMap2d,
    glMap2f,
    glMapGrid1d,
    glMapGrid1f,
    glMapGrid2d,
    glMapGrid2f,
    glEvalCoord1dv,
    glEvalCoord1fv,
    glEvalCoord2dv,
    glEvalCoord2fv,
    glEvalMesh1,
    glEvalPoint1,
    glEvalMesh2,
    glEvalPoint2,
    glAlphaFunc,
    glBlendFunc,
    glLogicOp,
    glStencilFunc,
    glStencilOp,
    glDepthFunc,
    glPixelZoom,
    glPixelTransferf,
    glPixelTransferi,
    glPixelStoref,
    glPixelStorei,
    glPixelMapfv,
    glPixelMapuiv,
    glPixelMapusv,
    xglReadBuffer, /* glReadBuffer */
    xglCopyPixels, /* glCopyPixels */
    xglReadPixels, /* glReadPixels */
    xglDrawPixels, /* glDrawPixels */
    xglGetBooleanv, /* glGetBooleanv */
    glGetClipPlane,
    xglGetDoublev, /* glGetDoublev */
    xglGetError, /* glGetError */
    xglGetFloatv, /* glGetFloatv */
    xglGetIntegerv, /* glGetIntegerv */
    glGetLightfv,
    glGetLightiv,
    glGetMapdv,
    glGetMapfv,
    glGetMapiv,
    glGetMaterialfv,
    glGetMaterialiv,
    glGetPixelMapfv,
    glGetPixelMapuiv,
    glGetPixelMapusv,
    glGetPolygonStipple,
    xglGetString, /* glGetString */
    glGetTexEnvfv,
    glGetTexEnviv,
    glGetTexGendv,
    glGetTexGenfv,
    glGetTexGeniv,
    glGetTexImage,
    glGetTexParameterfv,
    glGetTexParameteriv,
    glGetTexLevelParameterfv,
    glGetTexLevelParameteriv,
    xglIsEnabled, /* glIsEnabled */
    xglIsList, /* glIsList */
    glDepthRange,
    glFrustum,
    glLoadIdentity,
    glLoadMatrixf,
    glLoadMatrixd,
    glMatrixMode,
    glMultMatrixf,
    glMultMatrixd,
    glOrtho,
    glPopMatrix,
    glPushMatrix,
    glRotated,
    glRotatef,
    glScaled,
    glScalef,
    glTranslated,
    glTranslatef,
    xglViewport, /* glViewport */
    glArrayElement,
    xglBindTexture, /* glBindTexture */
    glColorPointer,
    glDisableClientState,
    xglDrawArrays, /* glDrawArrays */
    xglDrawElements, /* glDrawElements */
    glEdgeFlagPointer,
    glEnableClientState,
    glIndexPointer,
    glIndexubv,
    glInterleavedArrays,
    glNormalPointer,
    glPolygonOffset,
    glTexCoordPointer,
    glVertexPointer,
    xglAreTexturesResident, /* glAreTexturesResident */
    xglCopyTexImage1D, /* glCopyTexImage1D */
    xglCopyTexImage2D, /* glCopyTexImage2D */
    xglCopyTexSubImage1D, /* glCopyTexSubImage1D */
    xglCopyTexSubImage2D, /* glCopyTexSubImage2D */
    xglDeleteTextures, /* glDeleteTextures */
    xglGenTextures, /* glGenTextures */
    glGetPointerv,
    xglIsTexture, /* glIsTexture */
    xglPrioritizeTextures, /* glPrioritizeTextures */
    glTexSubImage1D,
    glTexSubImage2D,
    glPopClientAttrib,
    glPushClientAttrib,
    glBlendColor,
    glBlendEquation,
    glColorTable,
    glColorTableParameterfv,
    glColorTableParameteriv,
    xglCopyColorTable, /* glCopyColorTable */
    glGetColorTable,
    glGetColorTableParameterfv,
    glGetColorTableParameteriv,
    glColorSubTable,
    xglCopyColorSubTable, /* glCopyColorSubTable */
    glConvolutionFilter1D,
    glConvolutionFilter2D,
    glConvolutionParameterf,
    glConvolutionParameterfv,
    glConvolutionParameteri,
    glConvolutionParameteriv,
    xglCopyConvolutionFilter1D, /* glCopyConvolutionFilter1D */
    xglCopyConvolutionFilter2D, /* glCopyConvolutionFilter2D */
    glGetConvolutionFilter,
    glGetConvolutionParameterfv,
    glGetConvolutionParameteriv,
    glGetSeparableFilter,
    glSeparableFilter2D,
    glGetHistogram,
    glGetHistogramParameterfv,
    glGetHistogramParameteriv,
    glGetMinmax,
    glGetMinmaxParameterfv,
    glGetMinmaxParameteriv,
    glHistogram,
    glMinmax,
    glResetHistogram,
    glResetMinmax,
    glTexImage3D,
    glTexSubImage3D,
    xglCopyTexSubImage3D /* glCopyTexSubImage3D */
};

/* GL_ARB_multitexture */
static void
xglNoOpActiveTextureARB (GLenum texture) {}
static void
xglNoOpClientActiveTextureARB (GLenum texture) {}
static void
xglNoOpMultiTexCoord1dvARB (GLenum target, const GLdouble *v) {}
static void
xglNoOpMultiTexCoord1fvARB (GLenum target, const GLfloat *v) {}
static void
xglNoOpMultiTexCoord1ivARB (GLenum target, const GLint *v) {}
static void
xglNoOpMultiTexCoord1svARB (GLenum target, const GLshort *v) {}
static void
xglNoOpMultiTexCoord2dvARB (GLenum target, const GLdouble *v) {}
static void
xglNoOpMultiTexCoord2fvARB (GLenum target, const GLfloat *v) {}
static void
xglNoOpMultiTexCoord2ivARB (GLenum target, const GLint *v) {}
static void
xglNoOpMultiTexCoord2svARB (GLenum target, const GLshort *v) {}
static void
xglNoOpMultiTexCoord3dvARB (GLenum target, const GLdouble *v) {}
static void
xglNoOpMultiTexCoord3fvARB (GLenum target, const GLfloat *v) {}
static void
xglNoOpMultiTexCoord3ivARB (GLenum target, const GLint *v) {}
static void
xglNoOpMultiTexCoord3svARB (GLenum target, const GLshort *v) {}
static void
xglNoOpMultiTexCoord4dvARB (GLenum target, const GLdouble *v) {}
static void
xglNoOpMultiTexCoord4fvARB (GLenum target, const GLfloat *v) {}
static void
xglNoOpMultiTexCoord4ivARB (GLenum target, const GLint *v) {}
static void
xglNoOpMultiTexCoord4svARB (GLenum target, const GLshort *v) {}

/* GL_ARB_multisample */
static void
xglNoOpSampleCoverageARB (GLclampf value, GLboolean invert) {}

/* GL_EXT_texture_object */
static GLboolean
xglNoOpAreTexturesResidentEXT (GLsizei n,
			       const GLuint *textures,
			       GLboolean *residences)
{
    return GL_FALSE;
}
static void
xglNoOpGenTexturesEXT (GLsizei n, GLuint *textures) {}
static GLboolean
xglNoOpIsTextureEXT (GLuint texture)
{
    return GL_FALSE;
}

/* GL_SGIS_multisample */
static void
xglNoOpSampleMaskSGIS (GLclampf value, GLboolean invert) {}
static void
xglNoOpSamplePatternSGIS (GLenum pattern) {}

/* GL_EXT_point_parameters */
static void
xglNoOpPointParameterfEXT (GLenum pname, GLfloat param) {}
static void
xglNoOpPointParameterfvEXT (GLenum pname, const GLfloat *params) {}

/* GL_MESA_window_pos */
static void
xglNoOpWindowPos3fMESA (GLfloat x, GLfloat y, GLfloat z) {}
static void
xglWindowPos3fMESA (GLfloat x, GLfloat y, GLfloat z)
{
    (*cctx->glRenderTableEXT.WindowPos3fMESA) (cctx->tx + x, cctx->ty + y, z);
}

/* GL_EXT_blend_func_separate */
static void
xglNoOpBlendFuncSeparateEXT (GLenum sfactorRGB, GLenum dfactorRGB,
			     GLenum sfactorAlpha, GLenum dfactorAlpha) {}

/* GL_EXT_fog_coord */ 
static void
xglNoOpFogCoordfvEXT (const GLfloat *coord) {}
static void
xglNoOpFogCoorddvEXT (const GLdouble *coord) {}
static void
xglNoOpFogCoordPointerEXT (GLenum type, GLsizei stride,
			   const GLvoid *pointer) {}

/* GL_EXT_secondary_color */
static void
xglNoOpSecondaryColor3bvEXT (const GLbyte *v) {}
static void
xglNoOpSecondaryColor3dvEXT (const GLdouble *v) {}
static void
xglNoOpSecondaryColor3fvEXT (const GLfloat *v) {}
static void
xglNoOpSecondaryColor3ivEXT (const GLint *v) {}
static void
xglNoOpSecondaryColor3svEXT (const GLshort *v) {}
static void
xglNoOpSecondaryColor3ubvEXT (const GLubyte *v) {}
static void
xglNoOpSecondaryColor3uivEXT (const GLuint *v) {}
static void
xglNoOpSecondaryColor3usvEXT (const GLushort *v) {}
static void
xglNoOpSecondaryColorPointerEXT (GLint size, GLenum type, GLsizei stride,
				 const GLvoid *pointer) {}


/* GL_NV_point_sprite */
static void
xglNoOpPointParameteriNV (GLenum pname, GLint params) {}
static void
xglNoOpPointParameterivNV (GLenum pname, const GLint *params) {}

/* GL_EXT_stencil_two_side */
static void
xglNoOpActiveStencilFaceEXT (GLenum face) {}

__glProcTableEXT __glNoOpRenderTableEXT = {
    xglNoOpActiveTextureARB,
    xglNoOpClientActiveTextureARB,
    xglNoOpMultiTexCoord1dvARB,
    xglNoOpMultiTexCoord1fvARB,
    xglNoOpMultiTexCoord1ivARB,
    xglNoOpMultiTexCoord1svARB,
    xglNoOpMultiTexCoord2dvARB,
    xglNoOpMultiTexCoord2fvARB,
    xglNoOpMultiTexCoord2ivARB,
    xglNoOpMultiTexCoord2svARB,
    xglNoOpMultiTexCoord3dvARB,
    xglNoOpMultiTexCoord3fvARB,
    xglNoOpMultiTexCoord3ivARB,
    xglNoOpMultiTexCoord3svARB,
    xglNoOpMultiTexCoord4dvARB,
    xglNoOpMultiTexCoord4fvARB,
    xglNoOpMultiTexCoord4ivARB,
    xglNoOpMultiTexCoord4svARB,
    xglNoOpSampleCoverageARB,
    xglNoOpAreTexturesResidentEXT,
    xglNoOpGenTexturesEXT,
    xglNoOpIsTextureEXT,
    xglNoOpSampleMaskSGIS,
    xglNoOpSamplePatternSGIS,
    xglNoOpPointParameterfEXT,
    xglNoOpPointParameterfvEXT,
    xglNoOpWindowPos3fMESA,
    xglNoOpBlendFuncSeparateEXT,
    xglNoOpFogCoordfvEXT,
    xglNoOpFogCoorddvEXT,
    xglNoOpFogCoordPointerEXT,
    xglNoOpSecondaryColor3bvEXT,
    xglNoOpSecondaryColor3dvEXT,
    xglNoOpSecondaryColor3fvEXT,
    xglNoOpSecondaryColor3ivEXT,
    xglNoOpSecondaryColor3svEXT,
    xglNoOpSecondaryColor3ubvEXT,
    xglNoOpSecondaryColor3uivEXT,
    xglNoOpSecondaryColor3usvEXT,
    xglNoOpSecondaryColorPointerEXT,
    xglNoOpPointParameteriNV,
    xglNoOpPointParameterivNV,
    xglNoOpActiveStencilFaceEXT
};

static void
xglInitExtensions (xglGLContextPtr pContext)
{
    const char *extensions;

    pContext->glRenderTableEXT = __glNoOpRenderTableEXT;

    extensions = glGetString (GL_EXTENSIONS);

    if (strstr (extensions, "GL_ARB_multitexture"))
    {
	pContext->glRenderTableEXT.ActiveTextureARB =
	    (PFNGLACTIVETEXTUREARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glActiveTextureARB");
	pContext->glRenderTableEXT.ClientActiveTextureARB =
	    (PFNGLCLIENTACTIVETEXTUREARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glClientActiveTextureARB");
	pContext->glRenderTableEXT.MultiTexCoord1dvARB =
	    (PFNGLMULTITEXCOORD1DVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord1dvARB");
	pContext->glRenderTableEXT.MultiTexCoord1fvARB =
	    (PFNGLMULTITEXCOORD1FVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord1fvARB");
	pContext->glRenderTableEXT.MultiTexCoord1ivARB =
	    (PFNGLMULTITEXCOORD1IVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord1ivARB");
	pContext->glRenderTableEXT.MultiTexCoord1svARB =
	    (PFNGLMULTITEXCOORD1SVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord1svARB");
	pContext->glRenderTableEXT.MultiTexCoord2dvARB =
	    (PFNGLMULTITEXCOORD2DVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord2dvARB");
	pContext->glRenderTableEXT.MultiTexCoord2fvARB =
	    (PFNGLMULTITEXCOORD2FVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord2fvARB");
	pContext->glRenderTableEXT.MultiTexCoord2ivARB =
	    (PFNGLMULTITEXCOORD2IVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord2ivARB");
	pContext->glRenderTableEXT.MultiTexCoord2svARB =
	    (PFNGLMULTITEXCOORD2SVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord2svARB");
	pContext->glRenderTableEXT.MultiTexCoord3dvARB =
	    (PFNGLMULTITEXCOORD3DVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord3dvARB");
	pContext->glRenderTableEXT.MultiTexCoord3fvARB =
	    (PFNGLMULTITEXCOORD3FVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord3fvARB");
	pContext->glRenderTableEXT.MultiTexCoord3ivARB =
	    (PFNGLMULTITEXCOORD3IVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord3ivARB");
	pContext->glRenderTableEXT.MultiTexCoord3svARB =
	    (PFNGLMULTITEXCOORD3SVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord3svARB");
	pContext->glRenderTableEXT.MultiTexCoord4dvARB =
	    (PFNGLMULTITEXCOORD4DVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord4dvARB");
	pContext->glRenderTableEXT.MultiTexCoord4fvARB =
	    (PFNGLMULTITEXCOORD4FVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord4fvARB");
	pContext->glRenderTableEXT.MultiTexCoord4ivARB =
	    (PFNGLMULTITEXCOORD4IVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord4ivARB");
	pContext->glRenderTableEXT.MultiTexCoord4svARB =
	    (PFNGLMULTITEXCOORD4SVARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glMultiTexCoord4svARB");
    }

    if (strstr (extensions, "GL_ARB_multisample"))
    {
	pContext->glRenderTableEXT.SampleCoverageARB =
	    (PFNGLSAMPLECOVERAGEARBPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSampleCoverageARB");
    }

    if (strstr (extensions, "GL_EXT_texture_object"))
    {
	pContext->glRenderTableEXT.AreTexturesResidentEXT =
	    xglAreTexturesResident;
	pContext->glRenderTableEXT.GenTexturesEXT = xglGenTextures;
	pContext->glRenderTableEXT.IsTextureEXT = xglIsTexture;
    }

    if (strstr (extensions, "GL_SGIS_multisample"))
    {
	pContext->glRenderTableEXT.SampleMaskSGIS =
	    (PFNGLSAMPLEMASKSGISPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSampleMaskSGIS");
	pContext->glRenderTableEXT.SamplePatternSGIS =
	    (PFNGLSAMPLEPATTERNSGISPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSamplePatternSGIS");
    }

    if (strstr (extensions, "GL_EXT_point_parameters"))
    {
	pContext->glRenderTableEXT.PointParameterfEXT =
	    (PFNGLPOINTPARAMETERFEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glPointParameterfEXT");
	pContext->glRenderTableEXT.PointParameterfvEXT =
	    (PFNGLPOINTPARAMETERFVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glPointParameterfvEXT");
    }

    if (strstr (extensions, "GL_MESA_window_pos"))
    {
	pContext->WindowPos3fMESA =
	    (PFNGLWINDOWPOS3FMESAPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glWindowPos3fMESA");
	pContext->glRenderTableEXT.WindowPos3fMESA = xglWindowPos3fMESA;
    }

    if (strstr (extensions, "GL_EXT_blend_func_separate"))
    {
	pContext->glRenderTableEXT.BlendFuncSeparateEXT =
	    (PFNGLBLENDFUNCSEPARATEEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glBlendFuncSeparateEXT");
    }

    if (strstr (extensions, "GL_EXT_fog_coord"))
    {
	pContext->glRenderTableEXT.FogCoordfvEXT =
	    (PFNGLFOGCOORDFVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glFogCoordfvEXT");
	pContext->glRenderTableEXT.FogCoorddvEXT =
	    (PFNGLFOGCOORDDVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glFogCoorddvEXT");
	pContext->glRenderTableEXT.FogCoordPointerEXT =
	    (PFNGLFOGCOORDPOINTEREXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glFogCoordPointerEXT");
    }

    if (strstr (extensions, "GL_EXT_secondary_color"))
    {
	pContext->glRenderTableEXT.SecondaryColor3bvEXT =
	    (PFNGLSECONDARYCOLOR3BVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3bvEXT");
	pContext->glRenderTableEXT.SecondaryColor3dvEXT =
	    (PFNGLSECONDARYCOLOR3DVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3dvEXT");
	pContext->glRenderTableEXT.SecondaryColor3fvEXT =
	    (PFNGLSECONDARYCOLOR3FVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3fvEXT");
	pContext->glRenderTableEXT.SecondaryColor3ivEXT =
	    (PFNGLSECONDARYCOLOR3IVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3ivEXT");
	pContext->glRenderTableEXT.SecondaryColor3svEXT =
	    (PFNGLSECONDARYCOLOR3SVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3svEXT");
	pContext->glRenderTableEXT.SecondaryColor3ubvEXT =
	    (PFNGLSECONDARYCOLOR3UBVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3ubvEXT");
	pContext->glRenderTableEXT.SecondaryColor3uivEXT =
	    (PFNGLSECONDARYCOLOR3UIVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3uivEXT");
	pContext->glRenderTableEXT.SecondaryColor3usvEXT =
	    (PFNGLSECONDARYCOLOR3USVEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColor3usvEXT");
	pContext->glRenderTableEXT.SecondaryColorPointerEXT =
	    (PFNGLSECONDARYCOLORPOINTEREXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glSecondaryColorPointerEXT");
    }

    if (strstr (extensions, "GL_NV_point_sprite"))
    {
	pContext->glRenderTableEXT.PointParameteriNV =
	    (PFNGLPOINTPARAMETERINVPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glPointParameteriNV");
	pContext->glRenderTableEXT.PointParameterivNV =
	    (PFNGLPOINTPARAMETERIVNVPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glPointParameterivNV");
    }

    if (strstr (extensions, "GL_EXT_stencil_two_side"))
    {
	pContext->glRenderTableEXT.ActiveStencilFaceEXT =
	    (PFNGLACTIVESTENCILFACEEXTPROC)
	    glitz_context_get_proc_address (pContext->context,
					    "glActiveStencilFaceEXT");
    }
    
    pContext->needInit = FALSE;
}

static void
xglFreeContext (xglGLContextPtr pContext)
{
    pContext->refcnt--;
    if (pContext->shared == pContext)
	pContext->refcnt--;
	
    if (pContext->refcnt)
	return;

    if (pContext->shared != pContext)
	xglFreeContext (pContext->shared);

    if (pContext->texObjects)
    {
	xglTexObjPtr pTexObj;
	GLuint	     key;

	do {
	    key = xglHashFirstEntry (pContext->texObjects);
	    if (key)
	    {
		pTexObj = (xglTexObjPtr) xglHashLookup (pContext->texObjects,
							key);
		if (pTexObj)
		{
		    glDeleteTextures (1, &pTexObj->name);
		    xfree (pTexObj);
		}
		xglHashRemove (pContext->texObjects, key);
	    }
	} while (key);
	
	xglDeleteHashTable (pContext->texObjects);
    }

    if (pContext->displayLists)
    {
	xglDisplayListPtr pDisplayList;
	GLuint		  key;

	do {
	    key = xglHashFirstEntry (pContext->displayLists);
	    if (key)
	    {
		pDisplayList = (xglDisplayListPtr)
		    xglHashLookup (pContext->displayLists, key);
		if (pDisplayList)
		    xglDestroyList (pDisplayList);
		
		xglHashRemove (pContext->displayLists, key);
	    }
	} while (key);
	
	xglDeleteHashTable (pContext->displayLists);
    }

    if (pContext->groupList)
	glDeleteLists (pContext->groupList, 1);

    if (pContext->pAttribStack)
	xfree (pContext->pAttribStack);
    
    glitz_context_destroy (pContext->context);

    if (pContext->versionString)
	xfree (pContext->versionString);

    xfree (pContext);
}

static GLboolean
xglDestroyContext (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    xglFreeContext (pContext);

    return (*iface->exports.destroyContext) ((__GLcontext *) iface);
}

static GLboolean
xglLoseCurrent (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    __glXFlushContextCache ();

    return (*iface->exports.loseCurrent) ((__GLcontext *) iface);   
}

static GLboolean
xglMakeCurrent (__GLcontext *gc)
{
    xglGLContextPtr	pContext = (xglGLContextPtr) gc;
    __GLinterface	*iface = pContext->mIface;
    __GLdrawablePrivate *drawPriv = iface->imports.getDrawablePrivate (gc);
    __GLdrawablePrivate *readPriv = iface->imports.getReadablePrivate (gc);
    xglGLBufferPtr	pDrawBufferPriv = drawPriv->private;
    xglGLBufferPtr	pReadBufferPriv = readPriv->private;
    ScreenPtr		pScreen = pDrawBufferPriv->pScreen;
    DrawablePtr		pDrawable = pDrawBufferPriv->pDrawable;
    PixmapPtr		pPixmap = pDrawBufferPriv->pPixmap;
    GLboolean		status;

    pContext->target = xglPrepareTarget (pDrawable);
    if (pContext->target && pPixmap)
    {
	pContext->target = xglPrepareTarget (&pPixmap->drawable);
	if (pContext->target)
	{
	    xglPixmapPtr pFront = XGL_GET_DRAWABLE_PIXMAP_PRIV (pDrawable);
	    xglPixmapPtr pBack  = XGL_GET_PIXMAP_PRIV (pPixmap);
	    XID		 value = FALSE;
	    int		 status;
	    
	    pContext->attrib.scissor.x = pContext->attrib.scissor.y = 0;
	    pContext->attrib.scissor.width = pDrawable->width;
	    pContext->attrib.scissor.height = pDrawable->height;

	    pContext->attrib.viewport.x = pContext->attrib.viewport.y = 0;
	    pContext->attrib.viewport.width = pDrawable->width;
	    pContext->attrib.viewport.height = pDrawable->height;

	    pContext->draw = pBack->surface;

	    if (pBack->pArea)
	    {
		xglOffscreenPtr pOffscreen = (xglOffscreenPtr)
		    pBack->pArea->pRoot->closure;

		pContext->tx = pBack->pArea->x;
		pContext->ty =
		    glitz_drawable_get_height (pOffscreen->drawable) -
		    pBack->pArea->y - pDrawable->height;
	    }
	    else
		pContext->tx = pContext->ty = 0;
	    
	    pContext->pDrawBuffer = pDrawBufferPriv;
	    
	    if (!pFront->lock) pFront->lock++;
	    if (!pBack->lock) pBack->lock++;

	    glitz_context_set_surface (pContext->context, pContext->draw);

	    if (!pDrawBufferPriv->pGC)
		pDrawBufferPriv->pGC =
		    CreateGC (&pPixmap->drawable,
			      GCGraphicsExposures, &value,
			      &status);
	    
	    ValidateGC (&pPixmap->drawable, pDrawBufferPriv->pGC);
	    
	    if (!pDrawBufferPriv->swapGC)
		pDrawBufferPriv->swapGC =
		    CreateGC (pDrawable,
			      GCGraphicsExposures, &value,
			      &status);
	    
	    ValidateGC (pDrawable, pDrawBufferPriv->swapGC);

	    return GL_TRUE;
	}
    } else
	pContext->target = xglPixmapTargetNo;

    if (pPixmap)
    {
	(*pScreen->DestroyPixmap) (pPixmap);
	pDrawBufferPriv->pPixmap = NULL;
    }
    
    drawPriv->private = pDrawBufferPriv->private;
    readPriv->private = pReadBufferPriv->private;

    status = (*iface->exports.makeCurrent) ((__GLcontext *) iface);

    drawPriv->private = pDrawBufferPriv;
    readPriv->private = pReadBufferPriv;

    return status;
}

static GLboolean
xglShareContext (__GLcontext *gc,
		 __GLcontext *gcShare)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    xglGLContextPtr pContextShare = (xglGLContextPtr) gcShare;
    __GLinterface   *iface = pContext->mIface;
    __GLinterface   *ifaceShare = pContextShare->mIface;

    return (*iface->exports.shareContext) ((__GLcontext *) iface,
					   (__GLcontext *) ifaceShare);
}

static GLboolean
xglCopyContext (__GLcontext	  *dst,
		const __GLcontext *src,
		GLuint		  mask)
{
    xglGLContextPtr pDst = (xglGLContextPtr) dst;
    xglGLContextPtr pSrc = (xglGLContextPtr) src;
    __GLinterface   *dstIface = pDst->mIface;
    __GLinterface   *srcIface = pSrc->mIface;

    glitz_context_copy (pSrc->context, pDst->context, mask);
    
    return (*dstIface->exports.copyContext) ((__GLcontext *) dstIface,
					     (const __GLcontext *) srcIface,
					     mask);
}

static GLboolean
xglForceCurrent (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    if (pContext->target)
    {
	cctx = pContext;
	
	glitz_context_make_current (cctx->context);
	
	if (cctx->needInit)
	    xglInitExtensions (cctx);
	
	__glRenderTable = &__glNativeRenderTable;
	__glRenderTableEXT = &cctx->glRenderTableEXT;
	
	return GL_TRUE;
    }

    cctx = NULL;
    __glRenderTable = &__glMesaRenderTable;
    __glRenderTableEXT = &__glMesaRenderTableEXT;
	
    return (*iface->exports.forceCurrent) ((__GLcontext *) iface);
}

static GLboolean
xglNotifyResize (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.notifyResize) ((__GLcontext *) iface);
}

static void
xglNotifyDestroy (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.notifyDestroy) ((__GLcontext *) iface);
}

static void
xglNotifySwapBuffers (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.notifySwapBuffers) ((__GLcontext *) iface);
}

static struct __GLdispatchStateRec *
xglDispatchExec (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.dispatchExec) ((__GLcontext *) iface);
}

static void
xglBeginDispatchOverride (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.beginDispatchOverride) ((__GLcontext *) iface);
}

static void
xglEndDispatchOverride (__GLcontext *gc)
{
    xglGLContextPtr pContext = (xglGLContextPtr) gc;
    __GLinterface   *iface = pContext->mIface;

    return (*iface->exports.endDispatchOverride) ((__GLcontext *) iface);
}

static void
xglLoseCurrentContext (void *closure)
{
    cctx = NULL;
    __glRenderTable = &__glMesaRenderTable;
    __glRenderTableEXT = &__glMesaRenderTableEXT;

    __glXFlushContextCache ();
}

static __GLinterface *
xglCreateContext (__GLimports      *imports,
		  __GLcontextModes *modes,
		  __GLinterface    *shareGC)
{
    glitz_drawable_format_t *format;
    xglGLContextPtr	    pShareContext = (xglGLContextPtr) shareGC;
    xglGLContextPtr	    pContext;
    __GLinterface	    *shareIface = NULL;
    __GLinterface	    *iface;
    __GLXcontext	    *glxCtx = (__GLXcontext *) imports->other;
    
    XGL_SCREEN_PRIV (glxCtx->pScreen);

    format = glitz_drawable_get_format (pScreenPriv->drawable);

    pContext = xalloc (sizeof (xglGLContextRec));
    if (!pContext)
	return NULL;

    pContext->context = glitz_context_create (pScreenPriv->drawable, format);
    glitz_context_set_user_data (pContext->context, NULL, 
				 xglLoseCurrentContext);
	
    pContext->needInit	    = TRUE;
    pContext->draw	    = NULL;
    pContext->target	    = FALSE;
    pContext->versionString = NULL;
    pContext->error	    = GL_NO_ERROR;
    pContext->shared	    = NULL;
    pContext->list	    = 0;
    pContext->groupList	    = 0;
    pContext->pAttribStack  = NULL;
    pContext->nAttribStack  = 0;
    pContext->refcnt	    = 1;

    pContext->attrib.drawBuffer  = GL_BACK;
    pContext->attrib.readBuffer  = GL_BACK;
    pContext->attrib.scissorTest = GL_FALSE;
    
    if (shareGC)
    {
	pContext->texObjects   = NULL;
	pContext->displayLists = NULL;
	
	pContext->shared = pShareContext->shared;
	shareIface = pShareContext->mIface;
    }
    else
    {
	pContext->texObjects = xglNewHashTable ();
	if (!pContext->texObjects)
	{
	    xglFreeContext (pContext);
	    return NULL;
	}
	
	pContext->displayLists = xglNewHashTable ();
	if (!pContext->displayLists)
	{
	    xglFreeContext (pContext);
	    return NULL;
	}

	pContext->shared = pContext;
    }

    pContext->shared->refcnt++;
    
    iface = (*screenInfoPriv.createContext) (imports, modes, shareIface);
    if (!iface)
    {
	xglFreeContext (pContext);
	return NULL;
    }

    pContext->mIface = iface;
    pContext->iface.imports = *imports;
    
    pContext->iface.exports.destroyContext	  = xglDestroyContext;
    pContext->iface.exports.loseCurrent		  = xglLoseCurrent;
    pContext->iface.exports.makeCurrent		  = xglMakeCurrent;
    pContext->iface.exports.shareContext	  = xglShareContext;
    pContext->iface.exports.copyContext		  = xglCopyContext;
    pContext->iface.exports.forceCurrent	  = xglForceCurrent;
    pContext->iface.exports.notifyResize	  = xglNotifyResize;
    pContext->iface.exports.notifyDestroy	  = xglNotifyDestroy;
    pContext->iface.exports.notifySwapBuffers     = xglNotifySwapBuffers;
    pContext->iface.exports.dispatchExec	  = xglDispatchExec;
    pContext->iface.exports.beginDispatchOverride = xglBeginDispatchOverride;
    pContext->iface.exports.endDispatchOverride   = xglEndDispatchOverride;
    
    return (__GLinterface *) pContext;
}

static GLboolean
xglSwapBuffers (__GLXdrawablePrivate *glxPriv)
{
    __GLdrawablePrivate	*glPriv = &glxPriv->glPriv;
    xglGLBufferPtr	pBufferPriv = glPriv->private;
    DrawablePtr		pDrawable = pBufferPriv->pDrawable;
    PixmapPtr		pPixmap = pBufferPriv->pPixmap;
    GCPtr		pGC = pBufferPriv->swapGC;
    GLboolean		ret;
    
    if (pPixmap)
    {
	/* Discard front buffer damage */
	REGION_EMPTY (pGC->pScreen, &pBufferPriv->damage);
	
	if (pGC)
	{
	    (*pGC->ops->CopyArea) ((DrawablePtr) pPixmap,
				   pDrawable, pGC,
				   0, 0,
				   pPixmap->drawable.width,
				   pPixmap->drawable.height,
				   0, 0);
	    
	    return GL_TRUE;
	}

	return GL_FALSE;
    }
    
    glPriv->private = pBufferPriv->private;
    ret = (*pBufferPriv->swapBuffers) (glxPriv);
    glPriv->private = pBufferPriv;
    
    return ret;
}

static GLboolean
xglResizeBuffers (__GLdrawableBuffer  *buffer,
		  GLint		      x,
		  GLint		      y,
		  GLuint	      width,
		  GLuint	      height, 
		  __GLdrawablePrivate *glPriv,
		  GLuint	      bufferMask)
{
    xglGLBufferPtr pBufferPriv = glPriv->private;
    DrawablePtr    pDrawable = pBufferPriv->pDrawable;
    PixmapPtr	   pPixmap = pBufferPriv->pPixmap;
    ScreenPtr      pScreen = pBufferPriv->pScreen;
    Bool	   status;
    
    if (pPixmap)
    {
	if (pPixmap->drawable.width  != width ||
	    pPixmap->drawable.height != height)
	{   
	    (*pScreen->DestroyPixmap) (pPixmap);
	    pPixmap = (*pScreen->CreatePixmap) (pScreen, width, height,
						pDrawable->depth);

	    /* give good initial score */
	    XGL_GET_PIXMAP_PRIV (pPixmap)->score = 4000;
	    pBufferPriv->pPixmap = pPixmap;
	}
	ValidateGC (pDrawable, pBufferPriv->swapGC);
    }
    
    glPriv->private = pBufferPriv->private;
    status = (*pBufferPriv->resizeBuffers) (buffer,
					    x, y, width, height,
					    glPriv,
					    bufferMask);
    glPriv->private = pBufferPriv;
    
    return status;
}

static void
xglFreeBuffers (__GLdrawablePrivate *glPriv)
{
    xglGLBufferPtr pBufferPriv = glPriv->private;
    ScreenPtr      pScreen = pBufferPriv->pScreen;
    
    glPriv->private = pBufferPriv->private;

    (*pBufferPriv->freeBuffers) (glPriv);
    
    if (pBufferPriv->pPixmap)
	(*pScreen->DestroyPixmap) (pBufferPriv->pPixmap);

    if (pBufferPriv->pGC)
	FreeGC (pBufferPriv->pGC, (GContext) 0);

    if (pBufferPriv->swapGC)
	FreeGC (pBufferPriv->swapGC, (GContext) 0);
    
    xfree (pBufferPriv);
}

static void
xglCreateBuffer (__GLXdrawablePrivate *glxPriv)
{
    __GLdrawablePrivate	*glPriv = &glxPriv->glPriv;
    DrawablePtr		pDrawable = glxPriv->pDraw;
    ScreenPtr		pScreen = pDrawable->pScreen;
    xglGLBufferPtr	pBufferPriv;

    XGL_DRAWABLE_PIXMAP_PRIV (pDrawable);
    
    pBufferPriv = xalloc (sizeof (xglGLBufferRec));
    if (!pBufferPriv)
	FatalError ("No memory");

    pBufferPriv->pScreen = pScreen;
    pBufferPriv->pDrawable = pDrawable;
    pBufferPriv->pPixmap = NULL;
    pBufferPriv->pGC = NULL;
    pBufferPriv->swapGC = NULL;

    REGION_INIT (pScreen, &pBufferPriv->damage, NullBox, 0);
    
    if (glxPriv->pGlxVisual->doubleBuffer)
	pBufferPriv->pPixmap =
	    (*pScreen->CreatePixmap) (pScreen,
				      pDrawable->width,
				      pDrawable->height,
				      pDrawable->depth);

    /* give good initial score */
    pPixmapPriv->score = 4000;

    (*screenInfoPriv.createBuffer) (glxPriv);

    /* Wrap the front buffer's resize routine */
    pBufferPriv->resizeBuffers = glPriv->frontBuffer.resize;
    glPriv->frontBuffer.resize = xglResizeBuffers;

    /* Wrap the swap buffers routine */
    pBufferPriv->swapBuffers = glxPriv->swapBuffers;
    glxPriv->swapBuffers = xglSwapBuffers;

    /* Wrap private and freePrivate */
    pBufferPriv->private = glPriv->private;
    pBufferPriv->freeBuffers = glPriv->freePrivate;
    glPriv->private = (void *) pBufferPriv;
    glPriv->freePrivate = xglFreeBuffers;
}

static Bool
xglScreenProbe (int screen)
{
    Bool status;

    status = (*screenInfoPriv.screenProbe) (screen);

    /* Wrap createBuffer */
    if (__glDDXScreenInfo.createBuffer != xglCreateBuffer)
    {
	screenInfoPriv.createBuffer    = __glDDXScreenInfo.createBuffer;
	__glDDXScreenInfo.createBuffer = xglCreateBuffer;
    }
    
    /* Wrap createContext */
    if (__glDDXScreenInfo.createContext != xglCreateContext)
    {
	screenInfoPriv.createContext    = __glDDXScreenInfo.createContext;
	__glDDXScreenInfo.createContext = xglCreateContext;
    }
    
    return status;
}

Bool
xglInitVisualConfigs (ScreenPtr pScreen)
{
    miInitVisualsProcPtr    initVisualsProc = NULL;
    VisualPtr		    visuals;
    int			    nvisuals;
    DepthPtr		    depths;
    int			    ndepths;
    int			    rootDepth;
    VisualID		    defaultVis;
    glitz_drawable_format_t *format;
    xglPixelFormatPtr	    pPixel;
    __GLXvisualConfig	    *pConfig;
    xglGLXVisualConfigPtr   pConfigPriv, *ppConfigPriv;
    XID			    *installedCmaps;
    ColormapPtr		    installedCmap;
    int			    numInstalledCmaps;
    int			    numConfig = 1;
    int			    depth, bpp, i;

    XGL_SCREEN_PRIV (pScreen);
    
    depth  = pScreenPriv->pVisual->pPixel->depth;
    bpp    = pScreenPriv->pVisual->pPixel->masks.bpp;
    format = glitz_drawable_get_format (pScreenPriv->drawable);
    pPixel = pScreenPriv->pixmapFormats[depth].pPixel;

    pConfig = xcalloc (sizeof (__GLXvisualConfig), numConfig);
    if (!pConfig)
	return FALSE;

    pConfigPriv = xcalloc (sizeof (xglGLXVisualConfigRec), numConfig);
    if (!pConfigPriv)
    {
	xfree (pConfig);
	return FALSE;
    }

    ppConfigPriv = xcalloc (sizeof (xglGLXVisualConfigPtr), numConfig);
    if (!ppConfigPriv)
    {
	xfree (pConfigPriv);
	xfree (pConfig);
	return FALSE;
    }

    installedCmaps = xalloc (pScreen->maxInstalledCmaps * sizeof (XID));
    if (!installedCmaps)
    {
	xfree (ppConfigPriv);
	xfree (pConfigPriv);
	xfree (pConfig);
	return FALSE;
    }
    
    for (i = 0; i < numConfig; i++)
    {
	ppConfigPriv[i] = &pConfigPriv[i];
	
	pConfig[i].vid   = (VisualID) (-1);
	pConfig[i].class = -1;
	pConfig[i].rgba  = TRUE;
	
	pConfig[i].redSize   = format->color.red_size;
	pConfig[i].greenSize = format->color.green_size;
	pConfig[i].blueSize  = format->color.blue_size;

	pConfig[i].redMask   = pPixel->masks.red_mask;
	pConfig[i].greenMask = pPixel->masks.green_mask;
	pConfig[i].blueMask  = pPixel->masks.blue_mask;

	if (format->color.alpha_size)
	{
	    pConfig[i].alphaSize = format->color.alpha_size;
	    pConfig[i].alphaMask = pPixel->masks.alpha_mask;
	}
	else
	{
	    pConfig[i].alphaSize = 0;
	    pConfig[i].alphaMask = 0;
	}

	pConfig[i].doubleBuffer = TRUE;
	pConfig[i].depthSize = format->depth_size;
	pConfig[i].stencilSize = format->stencil_size;
	pConfig[i].stereo = FALSE;
	
	if (depth == 16)
	    pConfig[i].bufferSize = 16;
	else
	    pConfig[i].bufferSize = 32;

	pConfig[i].auxBuffers = 0;
	pConfig[i].level      = 0;
	
	pConfig[i].visualRating = GLX_NONE;
	
	pConfig[i].transparentPixel = GLX_NONE;
	pConfig[i].transparentRed   = 0;
	pConfig[i].transparentGreen = 0;
	pConfig[i].transparentBlue  = 0;
	pConfig[i].transparentAlpha = 0;
	pConfig[i].transparentIndex = 0;
    }

    GlxSetVisualConfigs (numConfig, pConfig, (void **) ppConfigPriv);

    /* Wrap screenProbe */
    if (__glDDXScreenInfo.screenProbe != xglScreenProbe)
    {
	screenInfoPriv.screenProbe    = __glDDXScreenInfo.screenProbe;
	__glDDXScreenInfo.screenProbe = xglScreenProbe;
    }

    visuals    = pScreen->visuals;
    nvisuals   = pScreen->numVisuals;
    depths     = pScreen->allowedDepths;
    ndepths    = pScreen->numDepths;
    rootDepth  = pScreen->rootDepth;
    defaultVis = pScreen->rootVisual;
    
    /* Find installed colormaps */
    numInstalledCmaps = (*pScreen->ListInstalledColormaps) (pScreen, 
							    installedCmaps);
	
    GlxWrapInitVisuals (&initVisualsProc);
    GlxInitVisuals (&visuals, &depths, &nvisuals, &ndepths, &rootDepth,
		    &defaultVis, ((unsigned long) 1 << (bpp - 1)), 8, -1);

    /* Fix up any existing installed colormaps. */
    for (i = 0; i < numInstalledCmaps; i++)
    {
	int j;
	
	installedCmap = LookupIDByType (installedCmaps[i], RT_COLORMAP);
	if (!installedCmap)
	    continue;
	
	j = installedCmap->pVisual - pScreen->visuals;
	installedCmap->pVisual = &visuals[j];
    }
    
    pScreen->visuals       = visuals;
    pScreen->numVisuals    = nvisuals;
    pScreen->allowedDepths = depths;
    pScreen->numDepths     = ndepths;
    pScreen->rootDepth     = rootDepth;
    pScreen->rootVisual    = defaultVis;

    xfree (installedCmaps);
    xfree (pConfigPriv);
    xfree (pConfig);

    ErrorF ("[glx] initialized\n");
    
    return TRUE;
}

#endif /* GLXEXT */