summaryrefslogtreecommitdiff
path: root/hw/xfree86/common/xf86Config.c
blob: f8c1b6567bb1369ad3672f7ed9e5403dce8b61c4 (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
/*
 * Loosely based on code bearing the following copyright:
 *
 *   Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 */

/*
 * Copyright 1992-2003 by The XFree86 Project, Inc.
 * Copyright 1997 by Metro Link, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the copyright holder(s)
 * and author(s) shall not be used in advertising or otherwise to promote
 * the sale, use or other dealings in this Software without prior written
 * authorization from the copyright holder(s) and author(s).
 */

/*
 *
 * Authors:
 *	Dirk Hohndel <hohndel@XFree86.Org>
 *	David Dawes <dawes@XFree86.Org>
 *      Marc La France <tsi@XFree86.Org>
 *      Egbert Eich <eich@XFree86.Org>
 *      ... and others
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#ifdef XF86DRI
#include <sys/types.h>
#include <grp.h>
#endif

#include "xf86.h"
#include "xf86Parser.h"
#include "xf86tokens.h"
#include "xf86Config.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include "configProcs.h"
#include "globals.h"
#include "extension.h"
#include "xf86pciBus.h"

#include "xf86Xinput.h"

#include "xkbsrv.h"

#include "picture.h"

/*
 * These paths define the way the config file search is done.  The escape
 * sequences are documented in parser/scan.c.
 */
#ifndef ROOT_CONFIGPATH
#define ROOT_CONFIGPATH	"%A," "%R," \
			"/etc/X11/%R," "%P/etc/X11/%R," \
			"%E," "%F," \
			"/etc/X11/%F," "%P/etc/X11/%F," \
			"/etc/X11/%X," "/etc/%X," \
			"%P/etc/X11/%X.%H," \
			"%P/etc/X11/%X," \
			"%P/lib/X11/%X.%H," \
			"%P/lib/X11/%X"
#endif
#ifndef USER_CONFIGPATH
#define USER_CONFIGPATH	"/etc/X11/%S," "%P/etc/X11/%S," \
			"/etc/X11/%G," "%P/etc/X11/%G," \
			"/etc/X11/%X," "/etc/%X," \
			"%P/etc/X11/%X.%H," \
			"%P/etc/X11/%X," \
			"%P/lib/X11/%X.%H," \
			"%P/lib/X11/%X"
#endif
#ifndef ROOT_CONFIGDIRPATH
#define ROOT_CONFIGDIRPATH	"%A," "%R," \
				"/etc/X11/%R," "%C/X11/%R," \
				"/etc/X11/%X," "%C/X11/%X"
#endif
#ifndef USER_CONFIGDIRPATH
#define USER_CONFIGDIRPATH	"/etc/X11/%R," "%C/X11/%R," \
				"/etc/X11/%X," "%C/X11/%X"
#endif
#ifndef SYS_CONFIGDIRPATH
#define SYS_CONFIGDIRPATH	"/usr/share/X11/%X," "%D/X11/%X"
#endif
#ifndef PROJECTROOT
#define PROJECTROOT	"/usr/X11R6"
#endif

static ModuleDefault ModuleDefaults[] = {
    {.name = "extmod",   .toLoad = TRUE,    .load_opt=NULL},
#ifdef DBE
    {.name = "dbe",      .toLoad = TRUE,    .load_opt=NULL},
#endif
#ifdef GLXEXT
    {.name = "glx",      .toLoad = TRUE,    .load_opt=NULL},
#endif
#ifdef XRECORD
    {.name = "record",   .toLoad = TRUE,    .load_opt=NULL},
#endif
#ifdef XF86DRI
    {.name = "dri",      .toLoad = TRUE,    .load_opt=NULL},
#endif
#ifdef DRI2
    {.name = "dri2",     .toLoad = TRUE,    .load_opt=NULL},
#endif
    {.name = NULL,       .toLoad = FALSE,   .load_opt=NULL}
};


/* Forward declarations */
static Bool configScreen(confScreenPtr screenp, XF86ConfScreenPtr conf_screen,
			 int scrnum, MessageType from);
static Bool configMonitor(MonPtr monitorp, XF86ConfMonitorPtr conf_monitor);
static Bool configDevice(GDevPtr devicep, XF86ConfDevicePtr conf_device,
			 Bool active);
static Bool configInput(InputInfoPtr pInfo, XF86ConfInputPtr conf_input,
			MessageType from);
static Bool configDisplay(DispPtr displayp, XF86ConfDisplayPtr conf_display);
static Bool addDefaultModes(MonPtr monitorp);
#ifdef XF86DRI
static void configDRI(XF86ConfDRIPtr drip);
#endif
static void configExtensions(XF86ConfExtensionsPtr conf_ext);

/*
 * xf86GetPathElem --
 *	Extract a single element from the font path string starting at
 *	pnt.  The font path element will be returned, and pnt will be
 *	updated to point to the start of the next element, or set to
 *	NULL if there are no more.
 */
static char *
xf86GetPathElem(char **pnt)
{
  char *p1;

  p1 = *pnt;
  *pnt = index(*pnt, ',');
  if (*pnt != NULL) {
    **pnt = '\0';
    *pnt += 1;
  }
  return p1;
}

/*
 * xf86ValidateFontPath --
 *	Validates the user-specified font path.  Each element that
 *	begins with a '/' is checked to make sure the directory exists.
 *	If the directory exists, the existence of a file named 'fonts.dir'
 *	is checked.  If either check fails, an error is printed and the
 *	element is removed from the font path.
 */

#define DIR_FILE "/fonts.dir"
static char *
xf86ValidateFontPath(char *path)
{
  char *tmp_path, *out_pnt, *path_elem, *next, *p1, *dir_elem;
  struct stat stat_buf;
  int flag;
  int dirlen;

  tmp_path = calloc(1,strlen(path)+1);
  out_pnt = tmp_path;
  path_elem = NULL;
  next = path;
  while (next != NULL) {
    path_elem = xf86GetPathElem(&next);
    if (*path_elem == '/') {
      dir_elem = xnfcalloc(1, strlen(path_elem) + 1);
      if ((p1 = strchr(path_elem, ':')) != 0)
	dirlen = p1 - path_elem;
      else
	dirlen = strlen(path_elem);
      strncpy(dir_elem, path_elem, dirlen);
      dir_elem[dirlen] = '\0';
      flag = stat(dir_elem, &stat_buf);
      if (flag == 0)
	if (!S_ISDIR(stat_buf.st_mode))
	  flag = -1;
      if (flag != 0) {
        xf86Msg(X_WARNING, "The directory \"%s\" does not exist.\n", dir_elem);
	xf86ErrorF("\tEntry deleted from font path.\n");
	free(dir_elem);
	continue;
      }
      else {
	XNFasprintf(&p1, "%s%s", dir_elem, DIR_FILE);
	flag = stat(p1, &stat_buf);
	if (flag == 0)
	  if (!S_ISREG(stat_buf.st_mode))
	    flag = -1;
	free(p1);
	if (flag != 0) {
	  xf86Msg(X_WARNING,
		  "`fonts.dir' not found (or not valid) in \"%s\".\n", 
		  dir_elem);
	  xf86ErrorF("\tEntry deleted from font path.\n");
	  xf86ErrorF("\t(Run 'mkfontdir' on \"%s\").\n", dir_elem);
	  free(dir_elem);
	  continue;
	}
      }
      free(dir_elem);
    }

    /*
     * Either an OK directory, or a font server name.  So add it to
     * the path.
     */
    if (out_pnt != tmp_path)
      *out_pnt++ = ',';
    strcat(out_pnt, path_elem);
    out_pnt += strlen(path_elem);
  }
  return tmp_path;
}


/*
 * use the datastructure that the parser provides and pick out the parts
 * that we need at this point
 */
char **
xf86ModulelistFromConfig(pointer **optlist)
{
    int count = 0, i = 0;
    char **modulearray;
    char *ignore[] = { "GLcore", "speedo", "bitmap", "drm",
		       "freetype", "type1",
		       NULL };
    pointer *optarray;
    XF86LoadPtr modp;
    Bool found;
    
    /*
     * make sure the config file has been parsed and that we have a
     * ModulePath set; if no ModulePath was given, use the default
     * ModulePath
     */
    if (xf86configptr == NULL) {
        xf86Msg(X_ERROR, "Cannot access global config data structure\n");
        return NULL;
    }
    
    if (xf86configptr->conf_modules) {
        /* Walk the disable list and let people know what we've parsed to
         * not be loaded 
         */
        modp = xf86configptr->conf_modules->mod_disable_lst;
        while (modp) {
            xf86Msg(X_WARNING, "\"%s\" will not be loaded unless you've specified it to be loaded elsewhere.\n", modp->load_name);
	        modp = (XF86LoadPtr) modp->list.next;
        }
        /*
         * Walk the default settings table. For each module listed to be
         * loaded, make sure it's in the mod_load_lst. If it's not, make
         * sure it's not in the mod_no_load_lst. If it's not disabled,
         * append it to mod_load_lst
         */
         for (i=0 ; ModuleDefaults[i].name != NULL ; i++) {
            if (ModuleDefaults[i].toLoad == FALSE) {
                xf86Msg(X_WARNING, "\"%s\" is not to be loaded by default. Skipping.\n", ModuleDefaults[i].name);
                continue;
            }
            found = FALSE;
            modp = xf86configptr->conf_modules->mod_load_lst;
            while (modp) {
                if (strcmp(modp->load_name, ModuleDefaults[i].name) == 0) {
                    xf86Msg(X_INFO, "\"%s\" will be loaded. This was enabled by default and also specified in the config file.\n", ModuleDefaults[i].name);
                    found = TRUE;
                    break;
                }
	        modp = (XF86LoadPtr) modp->list.next;
            }
            if (found == FALSE) {
                modp = xf86configptr->conf_modules->mod_disable_lst;
                while (modp) {
                    if (strcmp(modp->load_name, ModuleDefaults[i].name) == 0) {
                        xf86Msg(X_INFO, "\"%s\" will be loaded even though the default is to disable it.\n", ModuleDefaults[i].name);
                        found = TRUE;
                        break;
                    }
	                modp = (XF86LoadPtr) modp->list.next;
                }
            }
            if (found == FALSE) {
		XF86LoadPtr ptr = (XF86LoadPtr)xf86configptr->conf_modules;
                xf86addNewLoadDirective(ptr, ModuleDefaults[i].name, XF86_LOAD_MODULE, ModuleDefaults[i].load_opt);
                xf86Msg(X_INFO, "\"%s\" will be loaded by default.\n", ModuleDefaults[i].name);
            }
         }
    } else {
	xf86configptr->conf_modules = xnfcalloc(1, sizeof(XF86ConfModuleRec));
	for (i=0 ; ModuleDefaults[i].name != NULL ; i++) {
	    if (ModuleDefaults[i].toLoad == TRUE) {
		XF86LoadPtr ptr = (XF86LoadPtr)xf86configptr->conf_modules;
		xf86addNewLoadDirective(ptr, ModuleDefaults[i].name, XF86_LOAD_MODULE, ModuleDefaults[i].load_opt);
	    }
	}
    }

	    /*
	     * Walk the list of modules in the "Module" section to determine how
	     * many we have.
	    */
	    modp = xf86configptr->conf_modules->mod_load_lst;
	    while (modp) {
                for (i = 0; ignore[i]; i++) {
                    if (strcmp(modp->load_name, ignore[i]) == 0)
                        modp->ignore = 1;
                }
                if (!modp->ignore)
	            count++;
	        modp = (XF86LoadPtr) modp->list.next;
	    }

    /*
     * allocate the memory and walk the list again to fill in the pointers
     */
    modulearray = xnfalloc((count + 1) * sizeof(char*));
    optarray = xnfalloc((count + 1) * sizeof(pointer));
    count = 0;
    if (xf86configptr->conf_modules) {
	    modp = xf86configptr->conf_modules->mod_load_lst;
	    while (modp) {
            if (!modp->ignore) {
	            modulearray[count] = modp->load_name;
	            optarray[count] = modp->load_opt;
	            count++;
            }
	        modp = (XF86LoadPtr) modp->list.next;
	    }
    }
    modulearray[count] = NULL;
    optarray[count] = NULL;
    if (optlist)
	    *optlist = optarray;
    else
	    free(optarray);
    return modulearray;
}


char **
xf86DriverlistFromConfig(void)
{
    int count = 0;
    int j;
    char **modulearray;
    screenLayoutPtr slp;
    
    /*
     * make sure the config file has been parsed and that we have a
     * ModulePath set; if no ModulePath was given, use the default
     * ModulePath
     */
    if (xf86configptr == NULL) {
        xf86Msg(X_ERROR, "Cannot access global config data structure\n");
        return NULL;
    }
    
    /*
     * Walk the list of driver lines in active "Device" sections to
     * determine now many implicitly loaded modules there are.
     *
     */
    if (xf86ConfigLayout.screens) {
        slp = xf86ConfigLayout.screens;
        while ((slp++)->screen) {
	    count++;
        }
    }

    /*
     * Handle the set of inactive "Device" sections.
     */
    j = 0;
    while (xf86ConfigLayout.inactives[j++].identifier)
	count++;

    if (count == 0)
	return NULL;

    /*
     * allocate the memory and walk the list again to fill in the pointers
     */
    modulearray = xnfalloc((count + 1) * sizeof(char*));
    count = 0;
    slp = xf86ConfigLayout.screens;
    while (slp->screen) {
	modulearray[count] = slp->screen->device->driver;
	count++;
	slp++;
    }

    j = 0;

    while (xf86ConfigLayout.inactives[j].identifier) 
	modulearray[count++] = xf86ConfigLayout.inactives[j++].driver;

    modulearray[count] = NULL;

    /* Remove duplicates */
    for (count = 0; modulearray[count] != NULL; count++) {
	int i;

	for (i = 0; i < count; i++)
	    if (xf86NameCmp(modulearray[i], modulearray[count]) == 0) {
		modulearray[count] = "";
		break;
	    }
    }
    return modulearray;
}

char **
xf86InputDriverlistFromConfig(void)
{
    int count = 0;
    char **modulearray;
    InputInfoPtr *idp;

    /*
     * make sure the config file has been parsed and that we have a
     * ModulePath set; if no ModulePath was given, use the default
     * ModulePath
     */
    if (xf86configptr == NULL) {
        xf86Msg(X_ERROR, "Cannot access global config data structure\n");
        return NULL;
    }
    
    /*
     * Walk the list of driver lines in active "InputDevice" sections to
     * determine now many implicitly loaded modules there are.
     */
    if (xf86ConfigLayout.inputs) {
        idp = xf86ConfigLayout.inputs;
        while (*idp) {
	    count++;
	    idp++;
        }
    }

    if (count == 0)
	return NULL;

    /*
     * allocate the memory and walk the list again to fill in the pointers
     */
    modulearray = xnfalloc((count + 1) * sizeof(char*));
    count = 0;
    idp = xf86ConfigLayout.inputs;
    while (idp && *idp) {
        modulearray[count] = (*idp)->driver;
	count++;
	idp++;
    }
    modulearray[count] = NULL;

    /* Remove duplicates */
    for (count = 0; modulearray[count] != NULL; count++) {
	int i;

	for (i = 0; i < count; i++)
	    if (xf86NameCmp(modulearray[i], modulearray[count]) == 0) {
		modulearray[count] = "";
		break;
	    }
    }
    return modulearray;
}

static void
fixup_video_driver_list(char **drivers)
{
    static const char *fallback[4] = { "vesa", "fbdev", "wsfb", NULL };
    char **end, **drv;
    char *x;
    int i;

    /* walk to the end of the list */
    for (end = drivers; *end && **end; end++) ;
    end--;

    /*
     * for each of the fallback drivers, if we find it in the list,
     * swap it with the last available non-fallback driver.
     */
    for (i = 0; fallback[i]; i++) {
        for (drv = drivers; drv != end; drv++) {
            if (strstr(*drv, fallback[i])) {
                x = *drv; *drv = *end; *end = x;
                end--;
                break;
            }
        }
    }
}

static char **
GenerateDriverlist(char * dirname)
{
    char **ret;
    const char *subdirs[] = { dirname, NULL };
    static const char *patlist[] = {"(.*)_drv\\.so", NULL};
    ret = LoaderListDirs(subdirs, patlist);
    
    /* fix up the probe order for video drivers */
    if (strstr(dirname, "drivers") && ret != NULL)
        fixup_video_driver_list(ret);

    return ret;
}

char **
xf86DriverlistFromCompile(void)
{
    static char **driverlist = NULL;

    if (!driverlist)
        driverlist = GenerateDriverlist("drivers");

    return driverlist;
}

/*
 * xf86ConfigError --
 *      Print a READABLE ErrorMessage!!!  All information that is 
 *      available is printed.
 */
static void
xf86ConfigError(char *msg, ...)
{
    va_list ap;

    ErrorF("\nConfig Error:\n");
    va_start(ap, msg);
    VErrorF(msg, ap);
    va_end(ap);
    ErrorF("\n");
    return;
}

static void
configFiles(XF86ConfFilesPtr fileconf)
{
    MessageType	 pathFrom;
    Bool	 must_copy;
    int		 size, countDirs;
    char	*temp_path, *log_buf, *start, *end;

    /* FontPath */
    must_copy = TRUE;

    temp_path = defaultFontPath ? defaultFontPath : "";
    if (xf86fpFlag)
	pathFrom = X_CMDLINE;
    else if (fileconf && fileconf->file_fontpath) {
	pathFrom = X_CONFIG;
	if (xf86Info.useDefaultFontPath) {
	    if (asprintf(&defaultFontPath, "%s%s%s", fileconf->file_fontpath,
			 *temp_path ? "," : "", temp_path) == -1)
		defaultFontPath = NULL;
	    else
		must_copy = FALSE;
	}
	else
	    defaultFontPath = fileconf->file_fontpath;
    }
    else
	pathFrom = X_DEFAULT;
    temp_path = defaultFontPath ? defaultFontPath : "";

    /* xf86ValidateFontPath modifies its argument, but returns a copy of it. */
    temp_path = must_copy ? xnfstrdup(defaultFontPath) : defaultFontPath;
    defaultFontPath = xf86ValidateFontPath(temp_path);
    free(temp_path);

    /* make fontpath more readable in the logfiles */
    countDirs = 1;
    temp_path = defaultFontPath;
    while ((temp_path = index(temp_path, ',')) != NULL) {
	countDirs++;
	temp_path++;
    }

    log_buf = xnfalloc(strlen(defaultFontPath) + (2 * countDirs) + 1);
    temp_path = log_buf;
    start = defaultFontPath;
    while((end = index(start, ',')) != NULL) {
      size = (end - start) + 1;
      *(temp_path++) = '\t';
      strncpy(temp_path, start, size);
      temp_path += size;
      *(temp_path++) = '\n';
      start += size;
    }
    /* copy last entry */
    *(temp_path++) = '\t';
    strcpy(temp_path, start);
    xf86Msg(pathFrom, "FontPath set to:\n%s\n", log_buf);
    free(log_buf);
  
  /* ModulePath */

  if (fileconf) {
    if (xf86ModPathFrom != X_CMDLINE && fileconf->file_modulepath) {
      xf86ModulePath = fileconf->file_modulepath;
      xf86ModPathFrom = X_CONFIG;
    }
  }

  xf86Msg(xf86ModPathFrom, "ModulePath set to \"%s\"\n", xf86ModulePath);

  if (!xf86xkbdirFlag && fileconf && fileconf->file_xkbdir) {
    XkbBaseDirectory = fileconf->file_xkbdir;
    xf86Msg(X_CONFIG, "XKB base directory set to \"%s\"\n",
	    XkbBaseDirectory);
  }
#if 0
  /* LogFile */
  /*
   * XXX The problem with this is that the log file is already open.
   * One option might be to copy the exiting contents to the new location.
   * and re-open it.  The down side is that the default location would
   * already have been overwritten.  Another option would be to start with
   * unique temporary location, then copy it once the correct name is known.
   * A problem with this is what happens if the server exits before that
   * happens.
   */
  if (xf86LogFileFrom == X_DEFAULT && fileconf->file_logfile) {
    xf86LogFile = fileconf->file_logfile;
    xf86LogFileFrom = X_CONFIG;
  }
#endif

  return;
}

typedef enum {
    FLAG_NOTRAPSIGNALS,
    FLAG_DONTVTSWITCH,
    FLAG_DONTZAP,
    FLAG_DONTZOOM,
    FLAG_DISABLEVIDMODE,
    FLAG_ALLOWNONLOCAL,
    FLAG_ALLOWMOUSEOPENFAIL,
    FLAG_SAVER_BLANKTIME,
    FLAG_DPMS_STANDBYTIME,
    FLAG_DPMS_SUSPENDTIME,
    FLAG_DPMS_OFFTIME,
    FLAG_PIXMAP,
    FLAG_NOPM,
    FLAG_XINERAMA,
    FLAG_LOG,
    FLAG_RENDER_COLORMAP_MODE,
    FLAG_RANDR,
    FLAG_AIGLX,
    FLAG_IGNORE_ABI,
    FLAG_ALLOW_EMPTY_INPUT,
    FLAG_USE_DEFAULT_FONT_PATH,
    FLAG_AUTO_ADD_DEVICES,
    FLAG_AUTO_ENABLE_DEVICES,
    FLAG_GLX_VISUALS,
    FLAG_DRI2,
    FLAG_USE_SIGIO
} FlagValues;

/**
 * NOTE: the last value for each entry is NOT the default. It is set to TRUE
 * if the parser found the option in the config file.
 */
static OptionInfoRec FlagOptions[] = {
  { FLAG_NOTRAPSIGNALS,		"NoTrapSignals",		OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_DONTVTSWITCH,		"DontVTSwitch",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_DONTZAP,		"DontZap",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_DONTZOOM,		"DontZoom",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_DISABLEVIDMODE,	"DisableVidModeExtension",	OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_ALLOWNONLOCAL,		"AllowNonLocalXvidtune",	OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_ALLOWMOUSEOPENFAIL,	"AllowMouseOpenFail",		OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_SAVER_BLANKTIME,	"BlankTime"		,	OPTV_INTEGER,
	{0}, FALSE },
  { FLAG_DPMS_STANDBYTIME,	"StandbyTime",			OPTV_INTEGER,
	{0}, FALSE },
  { FLAG_DPMS_SUSPENDTIME,	"SuspendTime",			OPTV_INTEGER,
	{0}, FALSE },
  { FLAG_DPMS_OFFTIME,		"OffTime",			OPTV_INTEGER,
	{0}, FALSE },
  { FLAG_PIXMAP,		"Pixmap",			OPTV_INTEGER,
	{0}, FALSE },
  { FLAG_NOPM,			"NoPM",				OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_XINERAMA,		"Xinerama",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_LOG,			"Log",				OPTV_STRING,
	{0}, FALSE },
  { FLAG_RENDER_COLORMAP_MODE,	"RenderColormapMode",		OPTV_STRING,
        {0}, FALSE },
  { FLAG_RANDR,			"RandR",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_AIGLX,			"AIGLX",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_IGNORE_ABI,		"IgnoreABI",			OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_USE_DEFAULT_FONT_PATH,  "UseDefaultFontPath",		OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_AUTO_ADD_DEVICES,       "AutoAddDevices",		OPTV_BOOLEAN,
        {0}, FALSE },
  { FLAG_AUTO_ENABLE_DEVICES,    "AutoEnableDevices",		OPTV_BOOLEAN,
        {0}, FALSE },
  { FLAG_GLX_VISUALS,		"GlxVisuals",			OPTV_STRING,
        {0}, FALSE },
  { FLAG_DRI2,			"DRI2",				OPTV_BOOLEAN,
	{0}, FALSE },
  { FLAG_USE_SIGIO,		"UseSIGIO",			OPTV_BOOLEAN,
	{0}, FALSE },
  { -1,				NULL,				OPTV_NONE,
	{0}, FALSE },
};

static Bool
configServerFlags(XF86ConfFlagsPtr flagsconf, XF86OptionPtr layoutopts)
{
    XF86OptionPtr optp, tmp;
    int i;
    Pix24Flags pix24 = Pix24DontCare;
    Bool value;
    MessageType from;
    const char *s;
    XkbRMLVOSet set;
    /* Default options. */
    set.rules = "base";
    set.model = "pc105";
    set.layout = "us";
    set.variant = NULL;
    set.options = NULL;

    /*
     * Merge the ServerLayout and ServerFlags options.  The former have
     * precedence over the latter.
     */
    optp = NULL;
    if (flagsconf && flagsconf->flg_option_lst)
	optp = xf86optionListDup(flagsconf->flg_option_lst);
    if (layoutopts) {
	tmp = xf86optionListDup(layoutopts);
	if (optp)
	    optp = xf86optionListMerge(optp, tmp);
	else
	    optp = tmp;
    }

    xf86ProcessOptions(-1, optp, FlagOptions);

    xf86GetOptValBool(FlagOptions, FLAG_NOTRAPSIGNALS, &xf86Info.notrapSignals);
    xf86GetOptValBool(FlagOptions, FLAG_DONTVTSWITCH, &xf86Info.dontVTSwitch);
    xf86GetOptValBool(FlagOptions, FLAG_DONTZAP, &xf86Info.dontZap);
    xf86GetOptValBool(FlagOptions, FLAG_DONTZOOM, &xf86Info.dontZoom);

    xf86GetOptValBool(FlagOptions, FLAG_IGNORE_ABI, &xf86Info.ignoreABI);
    if (xf86Info.ignoreABI) {
	    xf86Msg(X_CONFIG, "Ignoring ABI Version\n");
    }

    if (xf86SIGIOSupported()) {
	xf86Info.useSIGIO = xf86ReturnOptValBool(FlagOptions, FLAG_USE_SIGIO, USE_SIGIO_BY_DEFAULT);
	if (xf86IsOptionSet(FlagOptions, FLAG_USE_SIGIO)) {
	    from = X_CONFIG;
	} else {
	    from = X_DEFAULT;
	}
	if (!xf86Info.useSIGIO) {
	    xf86Msg(from, "Disabling SIGIO handlers for input devices\n");
	} else if (from == X_CONFIG) {
	    xf86Msg(from, "Enabling SIGIO handlers for input devices\n");
	}
    } else {
	xf86Info.useSIGIO = FALSE;
    }

    if (xf86IsOptionSet(FlagOptions, FLAG_AUTO_ADD_DEVICES)) {
        xf86GetOptValBool(FlagOptions, FLAG_AUTO_ADD_DEVICES,
                          &xf86Info.autoAddDevices);
        from = X_CONFIG;
    }
    else {
        from = X_DEFAULT;
    }
    xf86Msg(from, "%sutomatically adding devices\n",
            xf86Info.autoAddDevices ? "A" : "Not a");

    if (xf86IsOptionSet(FlagOptions, FLAG_AUTO_ENABLE_DEVICES)) {
        xf86GetOptValBool(FlagOptions, FLAG_AUTO_ENABLE_DEVICES,
                          &xf86Info.autoEnableDevices);
        from = X_CONFIG;
    }
    else {
        from = X_DEFAULT;
    }
    xf86Msg(from, "%sutomatically enabling devices\n",
            xf86Info.autoEnableDevices ? "A" : "Not a");

    /*
     * Set things up based on the config file information.  Some of these
     * settings may be overridden later when the command line options are
     * checked.
     */
#ifdef XF86VIDMODE
    if (xf86GetOptValBool(FlagOptions, FLAG_DISABLEVIDMODE, &value))
	xf86Info.vidModeEnabled = !value;
    if (xf86GetOptValBool(FlagOptions, FLAG_ALLOWNONLOCAL, &value))
	xf86Info.vidModeAllowNonLocal = value;
#endif

    if (xf86GetOptValBool(FlagOptions, FLAG_ALLOWMOUSEOPENFAIL, &value))
	xf86Info.allowMouseOpenFail = value;

    xf86Info.pmFlag = TRUE;
    if (xf86GetOptValBool(FlagOptions, FLAG_NOPM, &value)) 
	xf86Info.pmFlag = !value;
    {
	if ((s = xf86GetOptValString(FlagOptions, FLAG_LOG))) {
	    if (!xf86NameCmp(s,"flush")) {
		xf86Msg(X_CONFIG, "Flushing logfile enabled\n");
		xf86Info.log = LogFlush;
		LogSetParameter(XLOG_FLUSH, TRUE);
	    } else if (!xf86NameCmp(s,"sync")) {
		xf86Msg(X_CONFIG, "Syncing logfile enabled\n");
		xf86Info.log = LogSync;
		LogSetParameter(XLOG_FLUSH, TRUE);
		LogSetParameter(XLOG_SYNC, TRUE);
	    } else {
		xf86Msg(X_WARNING,"Unknown Log option\n");
	    }
        }
    }
    
    {
	if ((s = xf86GetOptValString(FlagOptions, FLAG_RENDER_COLORMAP_MODE))){
	    int policy = PictureParseCmapPolicy (s);
	    if (policy == PictureCmapPolicyInvalid)
		xf86Msg(X_WARNING, "Unknown colormap policy \"%s\"\n", s);
	    else
	    {
		xf86Msg(X_CONFIG, "Render colormap policy set to %s\n", s);
		PictureCmapPolicy = policy;
	    }
	}
    }

#ifdef RANDR
    xf86Info.disableRandR = FALSE;
    xf86Info.randRFrom = X_DEFAULT;
    if (xf86GetOptValBool(FlagOptions, FLAG_RANDR, &value)) {
	xf86Info.disableRandR = !value;
	xf86Info.randRFrom = X_CONFIG;
    }
#endif

    xf86Info.aiglx = TRUE;
    xf86Info.aiglxFrom = X_DEFAULT;
    if (xf86GetOptValBool(FlagOptions, FLAG_AIGLX, &value)) {
	xf86Info.aiglx = value;
	xf86Info.aiglxFrom = X_CONFIG;
    }

#ifdef GLXEXT
    xf86Info.glxVisuals = XF86_GlxVisualsTypical;
    xf86Info.glxVisualsFrom = X_DEFAULT;
    if ((s = xf86GetOptValString(FlagOptions, FLAG_GLX_VISUALS))) {
	if (!xf86NameCmp(s, "minimal")) {
	    xf86Info.glxVisuals = XF86_GlxVisualsMinimal;
	} else if (!xf86NameCmp(s, "typical")) {
	    xf86Info.glxVisuals = XF86_GlxVisualsTypical;
	} else if (!xf86NameCmp(s, "all")) {
	    xf86Info.glxVisuals = XF86_GlxVisualsAll;
	} else {
	    xf86Msg(X_WARNING,"Unknown GlxVisuals option\n");
	}
    }

    if (xf86GetOptValBool(FlagOptions, FLAG_AIGLX, &value)) {
	xf86Info.aiglx = value;
	xf86Info.aiglxFrom = X_CONFIG;
    }
#endif

    /* if we're not hotplugging, force some input devices to exist */
    xf86Info.forceInputDevices = !(xf86Info.autoAddDevices && xf86Info.autoEnableDevices);

    /* when forcing input devices, we use kbd. otherwise evdev, so use the
     * evdev rules set. */
#if defined(linux)
    if (!xf86Info.forceInputDevices)
        set.rules = "evdev";
#endif
    XkbSetRulesDflts(&set);

    xf86Info.useDefaultFontPath = TRUE;
    xf86Info.useDefaultFontPathFrom = X_DEFAULT;
    if (xf86GetOptValBool(FlagOptions, FLAG_USE_DEFAULT_FONT_PATH, &value)) {
	xf86Info.useDefaultFontPath = value;
	xf86Info.useDefaultFontPathFrom = X_CONFIG;
    }

/* Make sure that timers don't overflow CARD32's after multiplying */
#define MAX_TIME_IN_MIN (0x7fffffff / MILLI_PER_MIN)

    i = -1;
    xf86GetOptValInteger(FlagOptions, FLAG_SAVER_BLANKTIME, &i);
    if ((i >= 0) && (i < MAX_TIME_IN_MIN))
	ScreenSaverTime = defaultScreenSaverTime = i * MILLI_PER_MIN;
    else if (i != -1)
	xf86ConfigError("BlankTime value %d outside legal range of 0 - %d minutes",
			i, MAX_TIME_IN_MIN);

#ifdef DPMSExtension
    i = -1;
    xf86GetOptValInteger(FlagOptions, FLAG_DPMS_STANDBYTIME, &i);
    if ((i >= 0) && (i < MAX_TIME_IN_MIN))
	DPMSStandbyTime = i * MILLI_PER_MIN;
    else if (i != -1)
	xf86ConfigError("StandbyTime value %d outside legal range of 0 - %d minutes",
			i, MAX_TIME_IN_MIN);
    i = -1;
    xf86GetOptValInteger(FlagOptions, FLAG_DPMS_SUSPENDTIME, &i);
    if ((i >= 0) && (i < MAX_TIME_IN_MIN))
	DPMSSuspendTime = i * MILLI_PER_MIN;
    else if (i != -1)
	xf86ConfigError("SuspendTime value %d outside legal range of 0 - %d minutes",
			i, MAX_TIME_IN_MIN);
    i = -1;
    xf86GetOptValInteger(FlagOptions, FLAG_DPMS_OFFTIME, &i);
    if ((i >= 0) && (i < MAX_TIME_IN_MIN))
	DPMSOffTime = i * MILLI_PER_MIN;
    else if (i != -1)
	xf86ConfigError("OffTime value %d outside legal range of 0 - %d minutes",
			i, MAX_TIME_IN_MIN);
#endif

    i = -1;
    xf86GetOptValInteger(FlagOptions, FLAG_PIXMAP, &i);
    switch (i) {
    case 24:
	pix24 = Pix24Use24;
	break;
    case 32:
	pix24 = Pix24Use32;
	break;
    case -1:
	break;
    default:
	xf86ConfigError("Pixmap option's value (%d) must be 24 or 32\n", i);
	return FALSE;
    }
    if (xf86Pix24 != Pix24DontCare) {
	xf86Info.pixmap24 = xf86Pix24;
	xf86Info.pix24From = X_CMDLINE;
    } else if (pix24 != Pix24DontCare) {
	xf86Info.pixmap24 = pix24;
	xf86Info.pix24From = X_CONFIG;
    } else {
	xf86Info.pixmap24 = Pix24DontCare;
	xf86Info.pix24From = X_DEFAULT;
    }

#ifdef PANORAMIX
    from = X_DEFAULT;
    if (!noPanoramiXExtension)
      from = X_CMDLINE;
    else if (xf86GetOptValBool(FlagOptions, FLAG_XINERAMA, &value)) {
      noPanoramiXExtension = !value;
      from = X_CONFIG;
    }
    if (!noPanoramiXExtension)
      xf86Msg(from, "Xinerama: enabled\n");
#endif

#ifdef DRI2
    xf86Info.dri2 = FALSE;
    xf86Info.dri2From = X_DEFAULT;
    if (xf86GetOptValBool(FlagOptions, FLAG_DRI2, &value)) {
	xf86Info.dri2 = value;
	xf86Info.dri2From = X_CONFIG;
    }
#endif

    return TRUE;
}

Bool xf86DRI2Enabled(void)
{
    return xf86Info.dri2;
}

/**
 * Search for the pInfo in the null-terminated list given and remove (and
 * free) it if present. All other devices are moved forward.
 */
static void
freeDevice(InputInfoPtr *list, InputInfoPtr pInfo)
{
    InputInfoPtr *devs;

    for (devs = list; devs && *devs; devs++) {
	if (*devs == pInfo) {
	    free(*devs);
	    for (; devs && *devs; devs++)
		devs[0] = devs[1];
	    break;
	}
    }
}

/**
 * Append pInfo to the null-terminated list, allocating space as necessary.
 * pInfo is used as the last element.
 */
static InputInfoPtr*
addDevice(InputInfoPtr *list, InputInfoPtr pInfo)
{
    InputInfoPtr *devs;
    int count = 1;

    for (devs = list; devs && *devs; devs++)
	count++;

    list = xnfrealloc(list, (count + 1) * sizeof(InputInfoPtr));
    list[count] = NULL;

    list[count - 1] = pInfo;
    return list;
}

/*
 * Locate the core input devices.  These can be specified/located in
 * the following ways, in order of priority:
 *
 *  1. The InputDevices named by the -pointer and -keyboard command line
 *     options.
 *  2. The "CorePointer" and "CoreKeyboard" InputDevices referred to by
 *     the active ServerLayout.
 *  3. The first InputDevices marked as "CorePointer" and "CoreKeyboard".
 *  4. The first InputDevices that use 'keyboard' or 'kbd' and a valid mouse
 *     driver (mouse, synaptics, evdev, vmmouse, void)
 *  5. Default devices with an empty (default) configuration.  These defaults
 *     will reference the 'mouse' and 'keyboard' drivers.
 */

static Bool
checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
{
    InputInfoPtr corePointer = NULL, coreKeyboard = NULL;
    Bool foundPointer = FALSE, foundKeyboard = FALSE;
    const char *pointerMsg = NULL, *keyboardMsg = NULL;
    InputInfoPtr *devs, /* iterator */
            indp;
    InputInfoPtr Pointer, Keyboard;
    XF86ConfInputPtr confInput;
    XF86ConfInputRec defPtr, defKbd;
    MessageType from = X_DEFAULT;
    const char *mousedrivers[] = { "mouse", "synaptics", "evdev", "vmmouse",
				   "void", NULL };

    /*
     * First check if a core pointer or core keyboard have been specified
     * in the active ServerLayout.  If more than one is specified for either,
     * remove the core attribute from the later ones.
     */
    for (devs = servlayoutp->inputs; devs && *devs; devs++) {
        indp = *devs;
	if (indp->options &&
	    xf86CheckBoolOption(indp->options, "CorePointer", FALSE)) {
	    if (!corePointer) {
		corePointer = indp;
	    }
	}
	if (indp->options &&
	    xf86CheckBoolOption(indp->options, "CoreKeyboard", FALSE)) {
	    if (!coreKeyboard) {
		coreKeyboard = indp;
	    }
	}
    }

    confInput = NULL;

    /* 1. Check for the -pointer command line option. */
    if (xf86PointerName) {
	confInput = xf86findInput(xf86PointerName,
				  xf86configptr->conf_input_lst);
	if (!confInput) {
	    xf86Msg(X_ERROR, "No InputDevice section called \"%s\"\n",
		    xf86PointerName);
	    return FALSE;
	}
	from = X_CMDLINE;
	/*
	 * If one was already specified in the ServerLayout, it needs to be
	 * removed.
	 */
	if (corePointer) {
	    freeDevice(servlayoutp->inputs, corePointer);
	    corePointer = NULL;
	}
	foundPointer = TRUE;
    }

    /* 2. ServerLayout-specified core pointer. */
    if (corePointer) {
	foundPointer = TRUE;
	from = X_CONFIG;
    }

    /* 3. First core pointer device. */
    if (!foundPointer && (xf86Info.forceInputDevices || implicitLayout)) {
	XF86ConfInputPtr p;

	for (p = xf86configptr->conf_input_lst; p; p = p->list.next) {
	    if (p->inp_option_lst &&
		xf86CheckBoolOption(p->inp_option_lst, "CorePointer", FALSE)) {
		confInput = p;
		foundPointer = TRUE;
		from = X_DEFAULT;
		pointerMsg = "first core pointer device";
		break;
	    }
	}
    }

    /* 4. First pointer with an allowed mouse driver. */
    if (!foundPointer && xf86Info.forceInputDevices) {
	const char **driver = mousedrivers;
	confInput = xf86findInput(CONF_IMPLICIT_POINTER,
				  xf86configptr->conf_input_lst);
	while (*driver && !confInput) {
	    confInput = xf86findInputByDriver(*driver,
					      xf86configptr->conf_input_lst);
	    driver++;
	}
	if (confInput) {
	    foundPointer = TRUE;
	    from = X_DEFAULT;
	    pointerMsg = "first mouse device";
	}
    }

    /* 5. Built-in default. */
    if (!foundPointer && xf86Info.forceInputDevices) {
	memset(&defPtr, 0, sizeof(defPtr));
	defPtr.inp_identifier = strdup("<default pointer>");
	defPtr.inp_driver = strdup("mouse");
	confInput = &defPtr;
	foundPointer = TRUE;
	from = X_DEFAULT;
	pointerMsg = "default mouse configuration";
    }

    /* Add the core pointer device to the layout, and set it to Core. */
    if (foundPointer && confInput) {
	Pointer = xf86AllocateInput();
	if (Pointer)
	    foundPointer = configInput(Pointer, confInput, from);
	if (foundPointer) {
	    Pointer->options = xf86addNewOption(Pointer->options,
					       xnfstrdup("CorePointer"), "on");
	    servlayoutp->inputs = addDevice(servlayoutp->inputs, Pointer);
	}
    }

    if (!foundPointer && xf86Info.forceInputDevices) {
	/* This shouldn't happen. */
	xf86Msg(X_ERROR, "Cannot locate a core pointer device.\n");
	xf86DeleteInput(Pointer, 0);
	return FALSE;
    }

    confInput = NULL;

    /* 1. Check for the -keyboard command line option. */
    if (xf86KeyboardName) {
	confInput = xf86findInput(xf86KeyboardName,
				  xf86configptr->conf_input_lst);
	if (!confInput) {
	    xf86Msg(X_ERROR, "No InputDevice section called \"%s\"\n",
		    xf86KeyboardName);
	    return FALSE;
	}
	from = X_CMDLINE;
	/*
	 * If one was already specified in the ServerLayout, it needs to be
	 * removed.
	 */
	if (coreKeyboard) {
	    freeDevice(servlayoutp->inputs, coreKeyboard);
	    coreKeyboard = NULL;
	}
	foundKeyboard = TRUE;
    }

    /* 2. ServerLayout-specified core keyboard. */
    if (coreKeyboard) {
	foundKeyboard = TRUE;
	from = X_CONFIG;
    }

    /* 3. First core keyboard device. */
    if (!foundKeyboard && (xf86Info.forceInputDevices || implicitLayout)) {
	XF86ConfInputPtr p;

	for (p = xf86configptr->conf_input_lst; p; p = p->list.next) {
	    if (p->inp_option_lst &&
		xf86CheckBoolOption(p->inp_option_lst, "CoreKeyboard", FALSE)) {
		confInput = p;
		foundKeyboard = TRUE;
		from = X_DEFAULT;
		keyboardMsg = "first core keyboard device";
		break;
	    }
	}
    }

    /* 4. First keyboard with 'keyboard' or 'kbd' as the driver. */
    if (!foundKeyboard && xf86Info.forceInputDevices) {
	confInput = xf86findInput(CONF_IMPLICIT_KEYBOARD,
				  xf86configptr->conf_input_lst);
	if (!confInput) {
	    confInput = xf86findInputByDriver("kbd",
					      xf86configptr->conf_input_lst);
	}
	if (confInput) {
	    foundKeyboard = TRUE;
	    from = X_DEFAULT;
	    keyboardMsg = "first keyboard device";
	}
    }

    /* 5. Built-in default. */
    if (!foundKeyboard && xf86Info.forceInputDevices) {
	memset(&defKbd, 0, sizeof(defKbd));
	defKbd.inp_identifier = strdup("<default keyboard>");
	defKbd.inp_driver = strdup("kbd");
	confInput = &defKbd;
	foundKeyboard = TRUE;
	keyboardMsg = "default keyboard configuration";
	from = X_DEFAULT;
    }

    /* Add the core keyboard device to the layout, and set it to Core. */
    if (foundKeyboard && confInput) {
	Keyboard = xf86AllocateInput();
	if (Keyboard)
	    foundKeyboard = configInput(Keyboard, confInput, from);
	if (foundKeyboard) {
	    Keyboard->options = xf86addNewOption(Keyboard->options,
						xnfstrdup("CoreKeyboard"), "on");
	    servlayoutp->inputs = addDevice(servlayoutp->inputs, Keyboard);
	}
    }

    if (!foundKeyboard && xf86Info.forceInputDevices) {
	/* This shouldn't happen. */
	xf86Msg(X_ERROR, "Cannot locate a core keyboard device.\n");
	xf86DeleteInput(Keyboard, 0);
	return FALSE;
    }

    if (pointerMsg) {
	if (implicitLayout)
	    xf86Msg(X_DEFAULT, "No Layout section. Using the %s.\n",
	            pointerMsg);
	else
	    xf86Msg(X_DEFAULT, "The core pointer device wasn't specified "
	            "explicitly in the layout.\n"
	            "\tUsing the %s.\n", pointerMsg);
    }

    if (keyboardMsg) {
	if (implicitLayout)
	    xf86Msg(X_DEFAULT, "No Layout section. Using the %s.\n",
	            keyboardMsg);
	else
	    xf86Msg(X_DEFAULT, "The core keyboard device wasn't specified "
	            "explicitly in the layout.\n"
	            "\tUsing the %s.\n", keyboardMsg);
    }

    if (!xf86Info.forceInputDevices && !(foundPointer && foundKeyboard)) {
#if defined(CONFIG_HAL) || defined(CONFIG_UDEV) || defined(CONFIG_WSCONS)
	const char *config_backend;
#if defined(CONFIG_HAL)
	config_backend = "HAL";
#elif defined(CONFIG_UDEV)
	config_backend = "udev";
#else
	config_backend = "wscons";
#endif
	xf86Msg(X_INFO, "The server relies on %s to provide the list of "
	                "input devices.\n\tIf no devices become available, "
	                "reconfigure %s or disable AutoAddDevices.\n",
			config_backend, config_backend);
#else
	xf86Msg(X_WARNING, "Hotplugging requested but the server was "
			   "compiled without a config backend. "
			   "No input devices were configured, the server "
			   "will start without any input devices.\n");
#endif
    }

    return TRUE;
}

typedef enum {
    LAYOUT_ISOLATEDEVICE,
    LAYOUT_SINGLECARD
} LayoutValues;

static OptionInfoRec LayoutOptions[] = {
  { LAYOUT_ISOLATEDEVICE,      "IsolateDevice",        OPTV_STRING,
       {0}, FALSE },
  { LAYOUT_SINGLECARD,         "SingleCard",           OPTV_BOOLEAN,
       {0}, FALSE },
  { -1,                                NULL,                   OPTV_NONE,
       {0}, FALSE },
};

static Bool
configInputDevices(XF86ConfLayoutPtr layout, serverLayoutPtr servlayoutp)
{
    XF86ConfInputrefPtr irp;
    InputInfoPtr *indp;
    int count = 0;

    /*
     * Count the number of input devices.
     */
    irp = layout->lay_input_lst;
    while (irp) {
	count++;
	irp = (XF86ConfInputrefPtr)irp->list.next;
    }
    DebugF("Found %d input devices in the layout section %s\n",
	    count, layout->lay_identifier);
    indp = xnfcalloc((count + 1), sizeof(InputInfoPtr));
    indp[count] = NULL;
    irp = layout->lay_input_lst;
    count = 0;
    while (irp) {
	indp[count] = xf86AllocateInput();
	if (!configInput(indp[count], irp->iref_inputdev, X_CONFIG)) {
	    do {
		free(indp[count]);
	    } while(count--);
	    free(indp);
	    return FALSE;
	}
	indp[count]->options = xf86OptionListMerge(indp[count]->options,
						   irp->iref_option_lst);
	count++;
	irp = (XF86ConfInputrefPtr)irp->list.next;
    }
    servlayoutp->inputs = indp;

    return TRUE;
}


/*
 * figure out which layout is active, which screens are used in that layout,
 * which drivers and monitors are used in these screens
 */
static Bool
configLayout(serverLayoutPtr servlayoutp, XF86ConfLayoutPtr conf_layout,
	     char *default_layout)
{
    XF86ConfAdjacencyPtr adjp;
    XF86ConfInactivePtr idp;
    int saved_count, count = 0;
    int scrnum;
    XF86ConfLayoutPtr l;
    MessageType from;
    screenLayoutPtr slp;
    GDevPtr gdp;
    int i = 0, j;

    if (!servlayoutp)
	return FALSE;

    /*
     * which layout section is the active one?
     *
     * If there is a -layout command line option, use that one, otherwise
     * pick the first one.
     */
    from = X_DEFAULT;
    if (xf86LayoutName != NULL)
	from = X_CMDLINE;
    else if (default_layout) {
	xf86LayoutName = default_layout;
	from = X_CONFIG;
    }
    if (xf86LayoutName != NULL) {
	if ((l = xf86findLayout(xf86LayoutName, conf_layout)) == NULL) {
	    xf86Msg(X_ERROR, "No ServerLayout section called \"%s\"\n",
		    xf86LayoutName);
	    return FALSE;
	}
	conf_layout = l;
    }
    xf86Msg(from, "ServerLayout \"%s\"\n", conf_layout->lay_identifier);
    adjp = conf_layout->lay_adjacency_lst;

    /*
     * we know that each screen is referenced exactly once on the left side
     * of a layout statement in the Layout section. So to allocate the right
     * size for the array we do a quick walk of the list to figure out how
     * many sections we have
     */
    while (adjp) {
        count++;
        adjp = (XF86ConfAdjacencyPtr)adjp->list.next;
    }

    DebugF("Found %d screens in the layout section %s",
           count, conf_layout->lay_identifier);
    if (!count) /* alloc enough storage even if no screen is specified */
        count = 1;

    slp = xnfcalloc(1, (count + 1) * sizeof(screenLayoutRec));
    slp[count].screen = NULL;
    /*
     * now that we have storage, loop over the list again and fill in our
     * data structure; at this point we do not fill in the adjacency
     * information as it is not clear if we need it at all
     */
    adjp = conf_layout->lay_adjacency_lst;
    count = 0;
    while (adjp) {
        slp[count].screen = xnfcalloc(1, sizeof(confScreenRec));
	if (adjp->adj_scrnum < 0)
	    scrnum = count;
	else
	    scrnum = adjp->adj_scrnum;
	if (!configScreen(slp[count].screen, adjp->adj_screen, scrnum,
			  X_CONFIG)) {
	    do {
		free(slp[count].screen);
	    } while(count--);
	    free(slp);
	    return FALSE;
	}
	slp[count].x = adjp->adj_x;
	slp[count].y = adjp->adj_y;
	slp[count].refname = adjp->adj_refscreen;
	switch (adjp->adj_where) {
	case CONF_ADJ_OBSOLETE:
	    slp[count].where = PosObsolete;
	    slp[count].topname = adjp->adj_top_str;
	    slp[count].bottomname = adjp->adj_bottom_str;
	    slp[count].leftname = adjp->adj_left_str;
	    slp[count].rightname = adjp->adj_right_str;
	    break;
	case CONF_ADJ_ABSOLUTE:
	    slp[count].where = PosAbsolute;
	    break;
	case CONF_ADJ_RIGHTOF:
	    slp[count].where = PosRightOf;
	    break;
	case CONF_ADJ_LEFTOF:
	    slp[count].where = PosLeftOf;
	    break;
	case CONF_ADJ_ABOVE:
	    slp[count].where = PosAbove;
	    break;
	case CONF_ADJ_BELOW:
	    slp[count].where = PosBelow;
	    break;
	case CONF_ADJ_RELATIVE:
	    slp[count].where = PosRelative;
	    break;
	}
        count++;
        adjp = (XF86ConfAdjacencyPtr)adjp->list.next;
    }

    /* No screen was specified in the layout. take the first one from the
     * config file, or - if it is NULL - configScreen autogenerates one for
     * us */
    if (!count)
    {
        slp[0].screen = xnfcalloc(1, sizeof(confScreenRec));
	if (!configScreen(slp[0].screen, xf86configptr->conf_screen_lst,
                          0, X_CONFIG)) {
	    free(slp[0].screen);
	    free(slp);
	    return FALSE;
	}
    }

    /* XXX Need to tie down the upper left screen. */

    /* Fill in the refscreen and top/bottom/left/right values */
    for (i = 0; i < count; i++) {
	for (j = 0; j < count; j++) {
	    if (slp[i].refname &&
		strcmp(slp[i].refname, slp[j].screen->id) == 0) {
		slp[i].refscreen = slp[j].screen;
	    }
	    if (slp[i].topname &&
		strcmp(slp[i].topname, slp[j].screen->id) == 0) {
		slp[i].top = slp[j].screen;
	    }
	    if (slp[i].bottomname &&
		strcmp(slp[i].bottomname, slp[j].screen->id) == 0) {
		slp[i].bottom = slp[j].screen;
	    }
	    if (slp[i].leftname &&
		strcmp(slp[i].leftname, slp[j].screen->id) == 0) {
		slp[i].left = slp[j].screen;
	    }
	    if (slp[i].rightname &&
		strcmp(slp[i].rightname, slp[j].screen->id) == 0) {
		slp[i].right = slp[j].screen;
	    }
	}
	if (slp[i].where != PosObsolete
	    && slp[i].where != PosAbsolute
	    && !slp[i].refscreen) {
	    xf86Msg(X_ERROR,"Screen %s doesn't exist: deleting placement\n",
		     slp[i].refname);
	    slp[i].where = PosAbsolute;
	    slp[i].x = 0;
	    slp[i].y = 0;
	}
    }

    if (!count)
	saved_count = 1;
    else
	saved_count = count;
    /*
     * Count the number of inactive devices.
     */
    count = 0;
    idp = conf_layout->lay_inactive_lst;
    while (idp) {
        count++;
        idp = (XF86ConfInactivePtr)idp->list.next;
    }
    DebugF("Found %d inactive devices in the layout section %s\n",
           count, conf_layout->lay_identifier);
    gdp = xnfalloc((count + 1) * sizeof(GDevRec));
    gdp[count].identifier = NULL;
    idp = conf_layout->lay_inactive_lst;
    count = 0;
    while (idp) {
	if (!configDevice(&gdp[count], idp->inactive_device, FALSE))
	    goto bail;
        count++;
        idp = (XF86ConfInactivePtr)idp->list.next;
    }

    if (!configInputDevices(conf_layout, servlayoutp))
	goto bail;

    servlayoutp->id = conf_layout->lay_identifier;
    servlayoutp->screens = slp;
    servlayoutp->inactives = gdp;
    servlayoutp->options = conf_layout->lay_option_lst;
    from = X_DEFAULT;

    return TRUE;

bail:
    do {
	free(slp[saved_count].screen);
    } while(saved_count--);
    free(slp);
    free(gdp);
    return FALSE;
}

/*
 * No layout section, so find the first Screen section and set that up as
 * the only active screen.
 */
static Bool
configImpliedLayout(serverLayoutPtr servlayoutp, XF86ConfScreenPtr conf_screen,
                    XF86ConfigPtr xf86configptr)
{
    MessageType from;
    XF86ConfScreenPtr s;
    screenLayoutPtr slp;
    InputInfoPtr *indp;
    XF86ConfLayoutRec layout;

    if (!servlayoutp)
	return FALSE;

    /*
     * which screen section is the active one?
     *
     * If there is a -screen option, use that one, otherwise use the first
     * one.
     */

    from = X_CONFIG;
    if (xf86ScreenName != NULL) {
	if ((s = xf86findScreen(xf86ScreenName, conf_screen)) == NULL) {
	    xf86Msg(X_ERROR, "No Screen section called \"%s\"\n",
		    xf86ScreenName);
	    return FALSE;
	}
	conf_screen = s;
	from = X_CMDLINE;
    }

    /* We have exactly one screen */

    slp = xnfcalloc(1, 2 * sizeof(screenLayoutRec));
    slp[0].screen = xnfcalloc(1, sizeof(confScreenRec));
    slp[1].screen = NULL;
    if (!configScreen(slp[0].screen, conf_screen, 0, from)) {
	free(slp);
	return FALSE;
    }
    servlayoutp->id = "(implicit)";
    servlayoutp->screens = slp;
    servlayoutp->inactives = xnfcalloc(1, sizeof(GDevRec));
    servlayoutp->options = NULL;

    memset(&layout, 0, sizeof(layout));
    layout.lay_identifier = servlayoutp->id;
    if (xf86layoutAddInputDevices(xf86configptr, &layout) > 0) {
	if (!configInputDevices(&layout, servlayoutp))
	    return FALSE;
	from = X_DEFAULT;
    } else {
	/* Set up an empty input device list, then look for some core devices. */
	indp = xnfalloc(sizeof(InputInfoPtr));
	*indp = NULL;
	servlayoutp->inputs = indp;
    }

    return TRUE;
}

static Bool
configXvAdaptor(confXvAdaptorPtr adaptor, XF86ConfVideoAdaptorPtr conf_adaptor)
{
    int count = 0;
    XF86ConfVideoPortPtr conf_port;

    xf86Msg(X_CONFIG, "|   |-->VideoAdaptor \"%s\"\n",
	    conf_adaptor->va_identifier);
    adaptor->identifier = conf_adaptor->va_identifier;
    adaptor->options = conf_adaptor->va_option_lst;
    if (conf_adaptor->va_busid || conf_adaptor->va_driver) {
	xf86Msg(X_CONFIG, "|   | Unsupported device type, skipping entry\n");
	return FALSE;
    }

    /*
     * figure out how many videoport subsections there are and fill them in
     */
    conf_port = conf_adaptor->va_port_lst;
    while(conf_port) {
        count++;
        conf_port = (XF86ConfVideoPortPtr)conf_port->list.next;
    }
    adaptor->ports = xnfalloc((count) * sizeof(confXvPortRec));
    adaptor->numports = count;
    count = 0;
    conf_port = conf_adaptor->va_port_lst;
    while(conf_port) {
	adaptor->ports[count].identifier = conf_port->vp_identifier;
	adaptor->ports[count].options = conf_port->vp_option_lst;
        count++;
        conf_port = (XF86ConfVideoPortPtr)conf_port->list.next;
    }

    return TRUE;
}

static Bool
configScreen(confScreenPtr screenp, XF86ConfScreenPtr conf_screen, int scrnum,
	     MessageType from)
{
    int count = 0;
    XF86ConfDisplayPtr dispptr;
    XF86ConfAdaptorLinkPtr conf_adaptor;
    Bool defaultMonitor = FALSE;
    XF86ConfScreenRec local_conf_screen;

    if (!conf_screen) {
        memset(&local_conf_screen, 0, sizeof(local_conf_screen));
        conf_screen = &local_conf_screen;
        conf_screen->scrn_identifier = "Default Screen Section";
        xf86Msg(X_DEFAULT, "No screen section available. Using defaults.\n");
    }

    xf86Msg(from, "|-->Screen \"%s\" (%d)\n", conf_screen->scrn_identifier,
	    scrnum);
    /*
     * now we fill in the elements of the screen
     */
    screenp->id         = conf_screen->scrn_identifier;
    screenp->screennum  = scrnum;
    screenp->defaultdepth = conf_screen->scrn_defaultdepth;
    screenp->defaultbpp = conf_screen->scrn_defaultbpp;
    screenp->defaultfbbpp = conf_screen->scrn_defaultfbbpp;
    screenp->monitor    = xnfcalloc(1, sizeof(MonRec));
    /* If no monitor is specified, create a default one. */
    if (!conf_screen->scrn_monitor) {
	XF86ConfMonitorRec defMon;

	memset(&defMon, 0, sizeof(defMon));
	defMon.mon_identifier = "<default monitor>";
	if (!configMonitor(screenp->monitor, &defMon))
	    return FALSE;
	defaultMonitor = TRUE;
    } else {
	if (!configMonitor(screenp->monitor,conf_screen->scrn_monitor))
	    return FALSE;
    }
    /* Configure the device. If there isn't one configured, attach to the
     * first inactive one that we can configure. If there's none that work,
     * set it to NULL so that the section can be autoconfigured later */
    screenp->device     = xnfcalloc(1, sizeof(GDevRec));
    if ((!conf_screen->scrn_device) && (xf86configptr->conf_device_lst)) {
        conf_screen->scrn_device = xf86configptr->conf_device_lst;
	xf86Msg(X_DEFAULT, "No device specified for screen \"%s\".\n"
		"\tUsing the first device section listed.\n", screenp->id);
    }
    if (configDevice(screenp->device,conf_screen->scrn_device, TRUE)) {
        screenp->device->myScreenSection = screenp;
    } else {
        screenp->device = NULL;
    }
    screenp->options = conf_screen->scrn_option_lst;
    
    /*
     * figure out how many display subsections there are and fill them in
     */
    dispptr = conf_screen->scrn_display_lst;
    while(dispptr) {
        count++;
        dispptr = (XF86ConfDisplayPtr)dispptr->list.next;
    }
    screenp->displays   = xnfalloc((count) * sizeof(DispRec));
    screenp->numdisplays = count;
    
    /* Fill in the default Virtual size, if any */
    if (conf_screen->scrn_virtualX && conf_screen->scrn_virtualY) {
	for (count = 0, dispptr = conf_screen->scrn_display_lst;
	     dispptr;
	     dispptr = (XF86ConfDisplayPtr)dispptr->list.next, count++) {
	    screenp->displays[count].virtualX = conf_screen->scrn_virtualX;
	    screenp->displays[count].virtualY = conf_screen->scrn_virtualY;
	}
    }

    /* Now do the per-Display Virtual sizes */
    count = 0;
    dispptr = conf_screen->scrn_display_lst;
    while(dispptr) {
        configDisplay(&(screenp->displays[count]),dispptr);
        count++;
        dispptr = (XF86ConfDisplayPtr)dispptr->list.next;
    }

    /*
     * figure out how many videoadaptor references there are and fill them in
     */
    conf_adaptor = conf_screen->scrn_adaptor_lst;
    while(conf_adaptor) {
        count++;
        conf_adaptor = (XF86ConfAdaptorLinkPtr)conf_adaptor->list.next;
    }
    screenp->xvadaptors = xnfalloc((count) * sizeof(confXvAdaptorRec));
    screenp->numxvadaptors = 0;
    conf_adaptor = conf_screen->scrn_adaptor_lst;
    while(conf_adaptor) {
        if (configXvAdaptor(&(screenp->xvadaptors[screenp->numxvadaptors]),
			    conf_adaptor->al_adaptor))
    	    screenp->numxvadaptors++;
        conf_adaptor = (XF86ConfAdaptorLinkPtr)conf_adaptor->list.next;
    }

    if (defaultMonitor) {
	xf86Msg(X_DEFAULT, "No monitor specified for screen \"%s\".\n"
		"\tUsing a default monitor configuration.\n", screenp->id);
    }
    return TRUE;
}

typedef enum {
    MON_REDUCEDBLANKING,
    MON_MAX_PIX_CLOCK,
} MonitorValues;

static OptionInfoRec MonitorOptions[] = {
  { MON_REDUCEDBLANKING,      "ReducedBlanking",        OPTV_BOOLEAN,
       {0}, FALSE },
  { MON_MAX_PIX_CLOCK,	      "MaxPixClock",		OPTV_FREQ,
       {0}, FALSE },
  { -1,                                NULL,                   OPTV_NONE,
       {0}, FALSE },
};

static Bool
configMonitor(MonPtr monitorp, XF86ConfMonitorPtr conf_monitor)
{
    int count;
    DisplayModePtr mode,last = NULL;
    XF86ConfModeLinePtr cmodep;
    XF86ConfModesPtr modes;
    XF86ConfModesLinkPtr modeslnk = conf_monitor->mon_modes_sect_lst;
    Gamma zeros = {0.0, 0.0, 0.0};
    float badgamma = 0.0;
    double maxPixClock;
    
    xf86Msg(X_CONFIG, "|   |-->Monitor \"%s\"\n",
	    conf_monitor->mon_identifier);
    monitorp->id = conf_monitor->mon_identifier;
    monitorp->vendor = conf_monitor->mon_vendor;
    monitorp->model = conf_monitor->mon_modelname;
    monitorp->Modes = NULL;
    monitorp->Last = NULL;
    monitorp->gamma = zeros;
    monitorp->widthmm = conf_monitor->mon_width;
    monitorp->heightmm = conf_monitor->mon_height;
    monitorp->reducedblanking = FALSE;
    monitorp->maxPixClock = 0;
    monitorp->options = conf_monitor->mon_option_lst;

    /*
     * fill in the monitor structure
     */    
    for( count = 0 ;
	 count < conf_monitor->mon_n_hsync && count < MAX_HSYNC;
	 count++) {
        monitorp->hsync[count].hi = conf_monitor->mon_hsync[count].hi;
        monitorp->hsync[count].lo = conf_monitor->mon_hsync[count].lo;
    }
    monitorp->nHsync = count;
    for( count = 0 ;
	 count < conf_monitor->mon_n_vrefresh && count < MAX_VREFRESH;
	 count++) {
        monitorp->vrefresh[count].hi = conf_monitor->mon_vrefresh[count].hi;
        monitorp->vrefresh[count].lo = conf_monitor->mon_vrefresh[count].lo;
    }
    monitorp->nVrefresh = count;

    /*
     * first we collect the mode lines from the UseModes directive
     */
    while(modeslnk)
    {
        modes = xf86findModes (modeslnk->ml_modes_str, 
			       xf86configptr->conf_modes_lst);
	modeslnk->ml_modes = modes;
	
	    
	/* now add the modes found in the modes
	   section to the list of modes for this
	   monitor unless it has been added before
	   because we are reusing the same section 
	   for another screen */
	if (xf86itemNotSublist(
			       (GenericListPtr)conf_monitor->mon_modeline_lst,
			       (GenericListPtr)modes->mon_modeline_lst)) {
	    conf_monitor->mon_modeline_lst = (XF86ConfModeLinePtr)
	        xf86addListItem(
				(GenericListPtr)conf_monitor->mon_modeline_lst,
				(GenericListPtr)modes->mon_modeline_lst);
	}
	modeslnk = modeslnk->list.next;
    }

    /*
     * we need to hook in the mode lines now
     * here both data structures use lists, only our internal one
     * is double linked
     */
    cmodep = conf_monitor->mon_modeline_lst;
    while( cmodep ) {
        mode = xnfcalloc(1, sizeof(DisplayModeRec));
	mode->type       = 0;
        mode->Clock      = cmodep->ml_clock;
        mode->HDisplay   = cmodep->ml_hdisplay;
        mode->HSyncStart = cmodep->ml_hsyncstart;
        mode->HSyncEnd   = cmodep->ml_hsyncend;
        mode->HTotal     = cmodep->ml_htotal;
        mode->VDisplay   = cmodep->ml_vdisplay;
        mode->VSyncStart = cmodep->ml_vsyncstart;
        mode->VSyncEnd   = cmodep->ml_vsyncend;
        mode->VTotal     = cmodep->ml_vtotal;
        mode->Flags      = cmodep->ml_flags;
        mode->HSkew      = cmodep->ml_hskew;
        mode->VScan      = cmodep->ml_vscan;
        mode->name       = xnfstrdup(cmodep->ml_identifier);
        if( last ) {
            mode->prev = last;
            last->next = mode;
        }
        else {
            /*
             * this is the first mode
             */
            monitorp->Modes = mode;
            mode->prev = NULL;
        }
        last = mode;
        cmodep = (XF86ConfModeLinePtr)cmodep->list.next;
    }
    if(last){
      last->next = NULL;
    }
    monitorp->Last = last;

    /* add the (VESA) default modes */
    if (! addDefaultModes(monitorp) )
	return FALSE;

    if (conf_monitor->mon_gamma_red > GAMMA_ZERO)
	monitorp->gamma.red = conf_monitor->mon_gamma_red;
    if (conf_monitor->mon_gamma_green > GAMMA_ZERO)
	monitorp->gamma.green = conf_monitor->mon_gamma_green;
    if (conf_monitor->mon_gamma_blue > GAMMA_ZERO)
	monitorp->gamma.blue = conf_monitor->mon_gamma_blue;
    
    /* Check that the gamma values are within range */
    if (monitorp->gamma.red > GAMMA_ZERO &&
	(monitorp->gamma.red < GAMMA_MIN ||
	 monitorp->gamma.red > GAMMA_MAX)) {
	badgamma = monitorp->gamma.red;
    } else if (monitorp->gamma.green > GAMMA_ZERO &&
	(monitorp->gamma.green < GAMMA_MIN ||
	 monitorp->gamma.green > GAMMA_MAX)) {
	badgamma = monitorp->gamma.green;
    } else if (monitorp->gamma.blue > GAMMA_ZERO &&
	(monitorp->gamma.blue < GAMMA_MIN ||
	 monitorp->gamma.blue > GAMMA_MAX)) {
	badgamma = monitorp->gamma.blue;
    }
    if (badgamma > GAMMA_ZERO) {
	xf86ConfigError("Gamma value %.f is out of range (%.2f - %.1f)\n",
			badgamma, GAMMA_MIN, GAMMA_MAX);
	    return FALSE;
    }

    xf86ProcessOptions(-1, monitorp->options, MonitorOptions);
    xf86GetOptValBool(MonitorOptions, MON_REDUCEDBLANKING,
                      &monitorp->reducedblanking);
    if (xf86GetOptValFreq(MonitorOptions, MON_MAX_PIX_CLOCK, OPTUNITS_KHZ,
			  &maxPixClock) == TRUE) {
	monitorp->maxPixClock = (int) maxPixClock;
    }
	
    return TRUE;
}

static int
lookupVisual(const char *visname)
{
    int i;

    if (!visname || !*visname)
	return -1;

    for (i = 0; i <= DirectColor; i++) {
	if (!xf86nameCompare(visname, xf86VisualNames[i]))
	    break;
    }

    if (i <= DirectColor)
	return i;

    return -1;
}


static Bool
configDisplay(DispPtr displayp, XF86ConfDisplayPtr conf_display)
{
    int count = 0;
    XF86ModePtr modep;
    
    displayp->frameX0           = conf_display->disp_frameX0;
    displayp->frameY0           = conf_display->disp_frameY0;
    displayp->virtualX          = conf_display->disp_virtualX;
    displayp->virtualY          = conf_display->disp_virtualY;
    displayp->depth             = conf_display->disp_depth;
    displayp->fbbpp             = conf_display->disp_bpp;
    displayp->weight.red        = conf_display->disp_weight.red;
    displayp->weight.green      = conf_display->disp_weight.green;
    displayp->weight.blue       = conf_display->disp_weight.blue;
    displayp->blackColour.red   = conf_display->disp_black.red;
    displayp->blackColour.green = conf_display->disp_black.green;
    displayp->blackColour.blue  = conf_display->disp_black.blue;
    displayp->whiteColour.red   = conf_display->disp_white.red;
    displayp->whiteColour.green = conf_display->disp_white.green;
    displayp->whiteColour.blue  = conf_display->disp_white.blue;
    displayp->options           = conf_display->disp_option_lst;
    if (conf_display->disp_visual) {
	displayp->defaultVisual = lookupVisual(conf_display->disp_visual);
	if (displayp->defaultVisual == -1) {
	    xf86ConfigError("Invalid visual name: \"%s\"",
			    conf_display->disp_visual);
	    return FALSE;
	}
    } else {
	displayp->defaultVisual = -1;
    }
	
    /*
     * now hook in the modes
     */
    modep = conf_display->disp_mode_lst;
    while(modep) {
        count++;
        modep = (XF86ModePtr)modep->list.next;
    }
    displayp->modes = xnfalloc((count+1) * sizeof(char*));
    modep = conf_display->disp_mode_lst;
    count = 0;
    while(modep) {
        displayp->modes[count] = modep->mode_name;
        count++;
        modep = (XF86ModePtr)modep->list.next;
    }
    displayp->modes[count] = NULL;
    
    return TRUE;
}

static Bool
configDevice(GDevPtr devicep, XF86ConfDevicePtr conf_device, Bool active)
{
    int i;

    if (!conf_device) {
        return FALSE;
    }

    if (active)
	xf86Msg(X_CONFIG, "|   |-->Device \"%s\"\n",
		conf_device->dev_identifier);
    else
	xf86Msg(X_CONFIG, "|-->Inactive Device \"%s\"\n",
		conf_device->dev_identifier);

    devicep->identifier = conf_device->dev_identifier;
    devicep->vendor = conf_device->dev_vendor;
    devicep->board = conf_device->dev_board;
    devicep->chipset = conf_device->dev_chipset;
    devicep->ramdac = conf_device->dev_ramdac;
    devicep->driver = conf_device->dev_driver;
    devicep->active = active;
    devicep->videoRam = conf_device->dev_videoram;
    devicep->BiosBase = conf_device->dev_bios_base;
    devicep->MemBase = conf_device->dev_mem_base;
    devicep->IOBase = conf_device->dev_io_base;
    devicep->clockchip = conf_device->dev_clockchip;
    devicep->busID = conf_device->dev_busid;
    devicep->textClockFreq = conf_device->dev_textclockfreq;
    devicep->chipID = conf_device->dev_chipid;
    devicep->chipRev = conf_device->dev_chiprev;
    devicep->options = conf_device->dev_option_lst;
    devicep->irq = conf_device->dev_irq;
    devicep->screen = conf_device->dev_screen;

    for (i = 0; i < MAXDACSPEEDS; i++) {
	if (i < CONF_MAXDACSPEEDS)
            devicep->dacSpeeds[i] = conf_device->dev_dacSpeeds[i];
	else
	    devicep->dacSpeeds[i] = 0;
    }
    devicep->numclocks = conf_device->dev_clocks;
    if (devicep->numclocks > MAXCLOCKS)
	devicep->numclocks = MAXCLOCKS;
    for (i = 0; i < devicep->numclocks; i++) {
	devicep->clock[i] = conf_device->dev_clock[i];
    }
    devicep->claimed = FALSE;

    return TRUE;
}

#ifdef XF86DRI
static void
configDRI(XF86ConfDRIPtr drip)
{
    struct group       *grp;

    xf86ConfigDRI.group      = -1;
    xf86ConfigDRI.mode       = 0;

    if (drip) {
	if (drip->dri_group_name) {
	    if ((grp = getgrnam(drip->dri_group_name)))
		xf86ConfigDRI.group = grp->gr_gid;
	} else {
	    if (drip->dri_group >= 0)
		xf86ConfigDRI.group = drip->dri_group;
	}
	xf86ConfigDRI.mode = drip->dri_mode;
    }
}
#endif

static void
configExtensions(XF86ConfExtensionsPtr conf_ext)
{
    XF86OptionPtr o;

    if (conf_ext && conf_ext->ext_option_lst) {
	for (o = conf_ext->ext_option_lst; o; o = xf86NextOption(o)) {
	    char *name   = xf86OptionName(o);
	    char *val    = xf86OptionValue(o);
	    char *n;
	    Bool  enable = TRUE;

	    /* Handle "No<ExtensionName>" */
	    n = xf86NormalizeName(name);
	    if (strncmp(n, "no", 2) == 0) {
		name += 2;
		enable = FALSE;
	    }

	    if (!val ||
		xf86NameCmp(val, "enable") == 0 ||
		xf86NameCmp(val, "enabled") == 0 ||
		xf86NameCmp(val, "on") == 0 ||
		xf86NameCmp(val, "1") == 0 ||
		xf86NameCmp(val, "yes") == 0 ||
		xf86NameCmp(val, "true") == 0) {
		/* NOTHING NEEDED -- enabling is handled below */
	    } else if (xf86NameCmp(val, "disable") == 0 ||
                       xf86NameCmp(val, "disabled") == 0 ||
		       xf86NameCmp(val, "off") == 0 ||
		       xf86NameCmp(val, "0") == 0 ||
		       xf86NameCmp(val, "no") == 0 ||
		       xf86NameCmp(val, "false") == 0) {
		enable = !enable;
	    } else {
		xf86Msg(X_WARNING, "Ignoring unrecognized value \"%s\"\n", val);
		free(n);
		continue;
	    }

	    if (EnableDisableExtension(name, enable)) {
		xf86Msg(X_CONFIG, "Extension \"%s\" is %s\n",
			name, enable ? "enabled" : "disabled");
	    } else {
		xf86Msg(X_WARNING, "Ignoring unrecognized extension \"%s\"\n",
                        name);
	    }
	    free(n);
	}
    }
}

static Bool
configInput(InputInfoPtr inputp, XF86ConfInputPtr conf_input, MessageType from)
{
    xf86Msg(from, "|-->Input Device \"%s\"\n", conf_input->inp_identifier);
    inputp->name = conf_input->inp_identifier;
    inputp->driver = conf_input->inp_driver;
    inputp->options = conf_input->inp_option_lst;
    inputp->attrs = NULL;

    return TRUE;
}

static Bool
modeIsPresent(DisplayModePtr mode, MonPtr monitorp)
{
    DisplayModePtr knownmodes = monitorp->Modes;

    /* all I can think of is a linear search... */
    while(knownmodes != NULL)
    {
	if(!strcmp(mode->name, knownmodes->name) &&
	   !(knownmodes->type & M_T_DEFAULT))
	    return TRUE;
	knownmodes = knownmodes->next;
    }
    return FALSE;
}

static Bool
addDefaultModes(MonPtr monitorp)
{
    DisplayModePtr mode;
    DisplayModePtr last = monitorp->Last;
    int i = 0;

    for (i = 0; i < xf86NumDefaultModes; i++)
    {
	mode = xf86DuplicateMode(&xf86DefaultModes[i]);
	if (!modeIsPresent(mode, monitorp))
	{
	    monitorp->Modes = xf86ModesAdd(monitorp->Modes, mode);
	    last = mode;
	} else {
	    free(mode);
	}
    }
    monitorp->Last = last;

    return TRUE;
}

static void
checkInput(serverLayoutPtr layout, Bool implicit_layout) {
    checkCoreInputDevices(layout, implicit_layout);

    /* Unless we're forcing input devices, disable mouse/kbd devices in the
     * config. Otherwise the same physical device is added multiple times,
     * leading to duplicate events.
     */
    if (!xf86Info.forceInputDevices && layout->inputs)
    {
        InputInfoPtr *dev = layout->inputs;
        BOOL warned = FALSE;

        while(*dev)
        {
            if (strcmp((*dev)->driver, "kbd") == 0 ||
                strcmp((*dev)->driver, "mouse") == 0 ||
                strcmp((*dev)->driver, "vmmouse") == 0)
            {
                InputInfoPtr *current;
                if (!warned)
                {
                    xf86Msg(X_WARNING, "Hotplugging is on, devices using "
                            "drivers 'kbd', 'mouse' or 'vmmouse' will be disabled.\n");
                    warned = TRUE;
                }

                xf86Msg(X_WARNING, "Disabling %s\n", (*dev)->name);

                current = dev;
                free(*dev);

                do {
                    *current = *(current + 1);
                    current++;
                } while(*current);
            } else
                dev++;
        }
    }
}

/*
 * load the config file and fill the global data structure
 */
ConfigStatus
xf86HandleConfigFile(Bool autoconfig)
{
    const char *filename, *dirname, *sysdirname;
    char *filesearch, *dirsearch;
    MessageType filefrom = X_DEFAULT;
    MessageType dirfrom = X_DEFAULT;
    char *scanptr;
    Bool singlecard = 0;
    Bool implicit_layout = FALSE;

    if (!autoconfig) {
	if (getuid() == 0) {
	    filesearch = ROOT_CONFIGPATH;
	    dirsearch = ROOT_CONFIGDIRPATH;
	} else {
	    filesearch = USER_CONFIGPATH;
	    dirsearch = USER_CONFIGDIRPATH;
	}

	if (xf86ConfigFile)
	    filefrom = X_CMDLINE;
	if (xf86ConfigDir)
	    dirfrom = X_CMDLINE;

	xf86initConfigFiles();
	sysdirname = xf86openConfigDirFiles(SYS_CONFIGDIRPATH, NULL,
					    PROJECTROOT);
	dirname = xf86openConfigDirFiles(dirsearch, xf86ConfigDir, PROJECTROOT);
	filename = xf86openConfigFile(filesearch, xf86ConfigFile, PROJECTROOT);
	if (filename) {
	    xf86MsgVerb(filefrom, 0, "Using config file: \"%s\"\n", filename);
	    xf86ConfigFile = xnfstrdup(filename);
	} else {
	    if (xf86ConfigFile)
		xf86Msg(X_ERROR, "Unable to locate/open config file: \"%s\"\n",
			xf86ConfigFile);
	}
	if (dirname) {
	    xf86MsgVerb(dirfrom, 0, "Using config directory: \"%s\"\n",
			dirname);
	    xf86ConfigDir = xnfstrdup(dirname);
	} else {
	    if (xf86ConfigDir)
		xf86Msg(X_ERROR,
			"Unable to locate/open config directory: \"%s\"\n",
			xf86ConfigDir);
	}
	if (sysdirname)
	    xf86MsgVerb(X_DEFAULT, 0, "Using system config directory \"%s\"\n",
			sysdirname);
	if (!filename && !dirname && !sysdirname)
	    return CONFIG_NOFILE;
    }

    if ((xf86configptr = xf86readConfigFile ()) == NULL) {
	xf86Msg(X_ERROR, "Problem parsing the config file\n");
	return CONFIG_PARSE_ERROR;
    }
    xf86closeConfigFile ();

    /* Initialise a few things. */

    /*
     * now we convert part of the information contained in the parser
     * structures into our own structures.
     * The important part here is to figure out which Screen Sections
     * in the XF86Config file are active so that we can piece together
     * the modes that we need later down the road.
     * And while we are at it, we'll decode the rest of the stuff as well
     */

    /* First check if a layout section is present, and if it is valid. */

    if (xf86configptr->conf_layout_lst == NULL || xf86ScreenName != NULL) {
	if (xf86ScreenName == NULL) {
	    xf86Msg(X_DEFAULT,
		    "No Layout section.  Using the first Screen section.\n");
	}
	if (!configImpliedLayout(&xf86ConfigLayout,
				 xf86configptr->conf_screen_lst,
				 xf86configptr)) {
            xf86Msg(X_ERROR, "Unable to determine the screen layout\n");
	    return CONFIG_PARSE_ERROR;
	}
	implicit_layout = TRUE;
    } else {
	if (xf86configptr->conf_flags != NULL) {
	  char *dfltlayout = NULL;
 	  pointer optlist = xf86configptr->conf_flags->flg_option_lst;
	
	  if (optlist && xf86FindOption(optlist, "defaultserverlayout"))
	    dfltlayout = xf86SetStrOption(optlist, "defaultserverlayout", NULL);
	  if (!configLayout(&xf86ConfigLayout, xf86configptr->conf_layout_lst,
			  dfltlayout)) {
	    xf86Msg(X_ERROR, "Unable to determine the screen layout\n");
	    return CONFIG_PARSE_ERROR;
	  }
	} else {
	  if (!configLayout(&xf86ConfigLayout, xf86configptr->conf_layout_lst,
			  NULL)) {
	    xf86Msg(X_ERROR, "Unable to determine the screen layout\n");
	    return CONFIG_PARSE_ERROR;
	  }
	}
    }

    xf86ProcessOptions(-1, xf86ConfigLayout.options, LayoutOptions);

    if ((scanptr = xf86GetOptValString(LayoutOptions, LAYOUT_ISOLATEDEVICE))) {
       ; /* IsolateDevice specified; overrides SingleCard */
    } else {
       xf86GetOptValBool(LayoutOptions, LAYOUT_SINGLECARD, &singlecard);
       if (singlecard)
           scanptr = xf86ConfigLayout.screens->screen->device->busID;
    }
    if (scanptr) {
       if (strncmp(scanptr, "PCI:", 4) != 0) {
           xf86Msg(X_WARNING, "Bus types other than PCI not yet isolable.\n"
                              "\tIgnoring IsolateDevice option.\n");
       } else
           xf86PciIsolateDevice(scanptr);
    }

    /* Now process everything else */
    if (!configServerFlags(xf86configptr->conf_flags,xf86ConfigLayout.options)){
             ErrorF ("Problem when converting the config data structures\n");
             return CONFIG_PARSE_ERROR;
    }

    configFiles(xf86configptr->conf_files);
    configExtensions(xf86configptr->conf_extensions);
#ifdef XF86DRI
    configDRI(xf86configptr->conf_dri);
#endif

    checkInput(&xf86ConfigLayout, implicit_layout);

    /*
     * Handle some command line options that can override some of the
     * ServerFlags settings.
     */
#ifdef XF86VIDMODE
    if (xf86VidModeDisabled)
	xf86Info.vidModeEnabled = FALSE;
    if (xf86VidModeAllowNonLocal)
	xf86Info.vidModeAllowNonLocal = TRUE;
#endif

    if (xf86AllowMouseOpenFail)
	xf86Info.allowMouseOpenFail = TRUE;

    return CONFIG_OK;
}

Bool
xf86PathIsSafe(const char *path)
{
    return (xf86pathIsSafe(path) != 0);
}