Initial commit
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / common / xf86AutoConfig.c
blob4f6a98aefbc760f98ed264608652876f8651682b
1 /*
2 * Copyright 2003 by David H. Dawes.
3 * Copyright 2003 by X-Oz Technologies.
4 * All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Except as contained in this notice, the name of the copyright holder(s)
25 * and author(s) shall not be used in advertising or otherwise to promote
26 * the sale, use or other dealings in this Software without prior written
27 * authorization from the copyright holder(s) and author(s).
29 * Author: David Dawes <dawes@XFree86.Org>.
32 #ifdef HAVE_XORG_CONFIG_H
33 #include <xorg-config.h>
34 #endif
36 #include "xf86.h"
37 #include "xf86Parser.h"
38 #include "xf86tokens.h"
39 #include "xf86Config.h"
40 #include "xf86Priv.h"
41 #include "xf86_OSlib.h"
43 /* Sections for the default built-in configuration. */
45 #define BUILTIN_MODULE_SECTION \
46 "Section \"Module\"\n" \
47 "\tLoad\t\"extmod\"\n" \
48 "\tLoad\t\"dbe\"\n" \
49 "\tLoad\t\"glx\"\n" \
50 "\tLoad\t\"freetype\"\n" \
51 "\tLoad\t\"type1\"\n" \
52 "\tLoad\t\"record\"\n" \
53 "\tLoad\t\"dri\"\n" \
54 "EndSection\n\n"
56 #define BUILTIN_DEVICE_NAME \
57 "\"Builtin Default %s Device %d\""
59 #define BUILTIN_DEVICE_SECTION_PRE \
60 "Section \"Device\"\n" \
61 "\tIdentifier\t" BUILTIN_DEVICE_NAME "\n" \
62 "\tDriver\t\"%s\"\n"
64 #define BUILTIN_DEVICE_SECTION_POST \
65 "EndSection\n\n"
67 #define BUILTIN_DEVICE_SECTION \
68 BUILTIN_DEVICE_SECTION_PRE \
69 BUILTIN_DEVICE_SECTION_POST
71 #define BUILTIN_MONITOR_NAME \
72 "\"Builtin Default Monitor\""
74 #define BUILTIN_MONITOR_SECTION \
75 "Section \"Monitor\"\n" \
76 "\tIdentifier\t" BUILTIN_MONITOR_NAME "\n" \
77 "EndSection\n\n"
79 #define BUILTIN_SCREEN_NAME \
80 "\"Builtin Default %s Screen %d\""
82 #define BUILTIN_SCREEN_SECTION \
83 "Section \"Screen\"\n" \
84 "\tIdentifier\t" BUILTIN_SCREEN_NAME "\n" \
85 "\tDevice\t" BUILTIN_DEVICE_NAME "\n" \
86 "\tMonitor\t" BUILTIN_MONITOR_NAME "\n" \
87 "EndSection\n\n"
89 #define BUILTIN_LAYOUT_SECTION_PRE \
90 "Section \"ServerLayout\"\n" \
91 "\tIdentifier\t\"Builtin Default Layout\"\n"
93 #define BUILTIN_LAYOUT_SCREEN_LINE \
94 "\tScreen\t" BUILTIN_SCREEN_NAME "\n"
96 #define BUILTIN_LAYOUT_SECTION_POST \
97 "EndSection\n\n"
99 static const char **builtinConfig = NULL;
100 static int builtinLines = 0;
101 static const char *deviceList[] = {
102 "fbdev",
103 "vesa",
104 "vga",
105 NULL
109 * A built-in config file is stored as an array of strings, with each string
110 * representing a single line. AppendToConfig() breaks up the string "s"
111 * into lines, and appends those lines it to builtinConfig.
114 static void
115 AppendToList(const char *s, const char ***list, int *lines)
117 char *str, *newstr, *p;
119 str = xnfstrdup(s);
120 for (p = strtok(str, "\n"); p; p = strtok(NULL, "\n")) {
121 (*lines)++;
122 *list = xnfrealloc(*list, (*lines + 1) * sizeof(**list));
123 newstr = xnfalloc(strlen(p) + 2);
124 strcpy(newstr, p);
125 strcat(newstr, "\n");
126 (*list)[*lines - 1] = newstr;
127 (*list)[*lines] = NULL;
129 xfree(str);
132 static void
133 FreeList(const char ***list, int *lines)
135 int i;
137 for (i = 0; i < *lines; i++) {
138 if ((*list)[i])
139 xfree((*list)[i]);
141 xfree(*list);
142 *list = NULL;
143 *lines = 0;
146 static void
147 FreeConfig(void)
149 FreeList(&builtinConfig, &builtinLines);
152 static void
153 AppendToConfig(const char *s)
155 AppendToList(s, &builtinConfig, &builtinLines);
158 static const char *
159 videoPtrToDriverName(pciVideoPtr info)
162 * things not handled yet:
163 * amd/cyrix/nsc
164 * xgi
167 switch (info->vendor)
169 case 0x1142: return "apm";
170 case 0xedd8: return "ark";
171 case 0x1a03: return "ast";
172 case 0x1002: return "ati";
173 case 0x102c: return "chips";
174 case 0x1013: return "cirrus";
175 case 0x8086:
176 if ((info->chipType == 0x00d1) || (info->chipType == 0x7800))
177 return "i740";
178 else return "i810";
179 case 0x102b: return "mga";
180 case 0x10c8: return "neomagic";
181 case 0x105d: return "i128";
182 case 0x10de: case 0x12d2: return "nv";
183 case 0x1163: return "rendition";
184 case 0x5333:
185 switch (info->chipType)
187 case 0x88d0: case 0x88d1: case 0x88f0: case 0x8811:
188 case 0x8812: case 0x8814: case 0x8901:
189 return "s3";
190 case 0x5631: case 0x883d: case 0x8a01: case 0x8a10:
191 case 0x8c01: case 0x8c03: case 0x8904: case 0x8a13:
192 return "s3virge";
193 default:
194 return "savage";
196 case 0x1039: return "sis";
197 case 0x126f: return "siliconmotion";
198 case 0x121a:
199 if (info->chipType < 0x0003)
200 return "voodoo";
201 else
202 return "tdfx";
203 case 0x3d3d: return "glint";
204 case 0x1023: return "trident";
205 case 0x100c: return "tseng";
206 case 0x1106: return "via";
207 case 0x15ad: return "vmware";
208 default: break;
210 return NULL;
213 Bool
214 xf86AutoConfig(void)
216 const char **p;
217 char buf[1024];
218 pciVideoPtr *pciptr, info = NULL;
219 char *driver = NULL;
220 ConfigStatus ret;
222 /* Find the primary device, and get some information about it. */
223 if (xf86PciVideoInfo) {
224 for (pciptr = xf86PciVideoInfo; (info = *pciptr); pciptr++) {
225 if (xf86IsPrimaryPci(info)) {
226 break;
229 if (!info) {
230 ErrorF("Primary device is not PCI\n");
232 } else {
233 ErrorF("xf86PciVideoInfo is not set\n");
236 if (info)
237 driver = videoPtrToDriverName(info);
239 AppendToConfig(BUILTIN_MODULE_SECTION);
240 AppendToConfig(BUILTIN_MONITOR_SECTION);
242 if (driver) {
243 snprintf(buf, sizeof(buf), BUILTIN_DEVICE_SECTION_PRE,
244 driver, 0, driver);
245 AppendToConfig(buf);
246 ErrorF("New driver is \"%s\"\n", driver);
247 buf[0] = '\t';
248 AppendToConfig(BUILTIN_DEVICE_SECTION_POST);
249 snprintf(buf, sizeof(buf), BUILTIN_SCREEN_SECTION,
250 driver, 0, driver, 0);
251 AppendToConfig(buf);
254 for (p = deviceList; *p; p++) {
255 snprintf(buf, sizeof(buf), BUILTIN_DEVICE_SECTION, *p, 0, *p);
256 AppendToConfig(buf);
257 snprintf(buf, sizeof(buf), BUILTIN_SCREEN_SECTION, *p, 0, *p, 0);
258 AppendToConfig(buf);
261 AppendToConfig(BUILTIN_LAYOUT_SECTION_PRE);
262 if (driver) {
263 snprintf(buf, sizeof(buf), BUILTIN_LAYOUT_SCREEN_LINE, driver, 0);
264 AppendToConfig(buf);
266 for (p = deviceList; *p; p++) {
267 snprintf(buf, sizeof(buf), BUILTIN_LAYOUT_SCREEN_LINE, *p, 0);
268 AppendToConfig(buf);
270 AppendToConfig(BUILTIN_LAYOUT_SECTION_POST);
272 xf86MsgVerb(X_DEFAULT, 0,
273 "Using default built-in configuration (%d lines)\n",
274 builtinLines);
276 xf86MsgVerb(X_DEFAULT, 3, "--- Start of built-in configuration ---\n");
277 for (p = builtinConfig; *p; p++)
278 xf86ErrorFVerb(3, "\t%s", *p);
279 xf86MsgVerb(X_DEFAULT, 3, "--- End of built-in configuration ---\n");
281 xf86setBuiltinConfig(builtinConfig);
282 ret = xf86HandleConfigFile(TRUE);
283 FreeConfig();
285 if (ret != CONFIG_OK)
286 xf86Msg(X_ERROR, "Error parsing the built-in default configuration.\n");
288 return (ret == CONFIG_OK);