/[gentoo-src]/Xorgautoconfig/Xorgautoconfig.c
Gentoo

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download) (as text)
Sat May 7 22:52:24 2005 UTC (9 years, 2 months ago) by pylon
Branch: MAIN
Changes since 1.4: +3 -1 lines
File MIME type: text/x-csrc
Better backup of existing config

1 /* Xorgautoconfig: versatile xorg.conf creation tool for PowerPC
2 *
3 * Copyright (C) 2005 Lars Weiler <pylon@gentoo.org>
4 * Copyright (C) 2005 Joseph Jezak <josejx@gentoo.org>
5 *
6 * This xorg.conf creation tool is a fork from Xautoconfig-0.23
7 * (http://ftp.penguinppc.org/projects/xautocfg/)
8 *
9 * The original header-comment contained:
10 * Copyright (C) 2002-2004 Dan Burcaw <dan@ydl.net>
11 * Copyright (C) 1999-2001 Tom Rini <trini@kernel.crashing.org>
12 *
13 * Thanks for this good start for an X.Org configurator!
14 *
15 */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <assert.h>
21 #include <ctype.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/syscall.h>
30 #include <sys/mman.h>
31 #include <netinet/in.h>
32 #include <linux/fb.h>
33 #include <byteswap.h>
34 #include <dirent.h>
35
36 #include "ddcprobe/common.h"
37 #include "ddcprobe/vesamode.h"
38 #include "Xorgtext.h"
39
40 extern int bus, dev, func;
41 extern void findVideoDevice(void);
42 extern void help();
43 extern void usage();
44 extern char * macMachineType(void);
45
46 struct monitor_sync
47 {
48 int h_min;
49 int h_max;
50 int v_min;
51 int v_max;
52 };
53
54 int main(int argc, char **argv) {
55 FILE * f;
56 int i, depth, fd;
57 int rc = 0, offb = 0, fbdepth = 0, fbdev = 0, nv = 0;
58 unsigned int xsstart, xsend, xtotal = 0;
59 unsigned int ysstart, ysend, ytotal = 0;
60 double drate, vrate, hrate;
61 unsigned int htotal, vtotal;
62 char manufacturer[4];
63
64 struct monitor_sync sync;
65 struct fb_fix_screeninfo fix;
66 struct fb_var_screeninfo var;
67 struct edid1_info *edid_info = NULL;
68
69 const char * name = "/dev/fb0";
70 unsigned char *mem;
71 char *macid;
72 char *eisaid;
73 char keyboard_layout[32];
74 memset(keyboard_layout, 0, 32);
75 DIR *testdir;
76 char path[256];
77
78 if (!(fd = open(name, O_RDONLY)))
79 rc = 2; /* Failure */
80
81 if (!rc && ioctl(fd, FBIOGET_FSCREENINFO, &fix))
82 rc = 2; /* Failure */
83
84 if (!rc && ioctl(fd, FBIOGET_VSCREENINFO, &var))
85 rc = 2; /* Failure */
86
87 close(fd);
88
89 /* 640x480 framebuffer typically implies a CRT, so lets bump */
90 /* the resolution up to 800x600 to be a bit more sane */
91 if (var.xres == 640 && var.yres == 480) {
92 var.xres = 800;
93 var.yres = 600;
94 }
95
96 if (rc) {
97 fprintf(stderr, "Can't access %s. Exiting.\n", name);
98 return -1;
99 }
100
101 if (argc > 1) {
102 int args;
103 for(args = 1; args<argc; args++) {
104 if (!strcmp(argv[args], "--fbdepth"))
105 fbdepth = 1;
106 else if (!strcmp(argv[args], "--fbdev"))
107 fbdev = 1;
108 else if (!strcmp(argv[args], "--safe"))
109 fbdev = fbdepth = 1;
110 else if (!strncmp(argv[args], "--keymap=", 9)) {
111 strncpy(keyboard_layout, argv[args] + 9 , 29);
112 strcat(keyboard_layout, "\"\n");
113 }
114 else if (!strcmp(argv[args], "--list-keymaps")) {
115 printf("%s", keymap_list);
116 exit(0);
117 } else {
118 usage();
119 exit(0);
120 }
121 }
122 }
123
124 /* Check to see what type of Mac we're on.*/
125 /* e.g. PowerBook1,1 */
126 macid = macMachineType();
127
128 sync.h_min = 0;
129 sync.h_max = 0;
130 sync.v_min = 0;
131 sync.v_max = 0;
132
133 /* Check to see if EDID is available */
134 if ( (sizeof(struct edid1_info) == 256) &&
135 (sizeof(struct edid_monitor_descriptor) == 18) &&
136 get_edid_supported())
137 {
138 edid_info = get_edid_info();
139
140 if ( !(edid_info == NULL) &&
141 !(edid_info->version == 0) &&
142 !(edid_info->version == 0xff && edid_info->revision == 0xff) &&
143 !(edid_info->version == 0 && edid_info->revision == 0))
144 {
145 manufacturer[0] = edid_info->manufacturer_name.char1 + 'A' - 1;
146 manufacturer[1] = edid_info->manufacturer_name.char2 + 'A' - 1;
147 manufacturer[2] = edid_info->manufacturer_name.char3 + 'A' - 1;
148 manufacturer[3] = '\0';
149 eisaid = malloc(sizeof(manufacturer) + sizeof(edid_info->product_code));
150 sprintf(eisaid, "%s%04x", manufacturer, edid_info->product_code);
151
152 for (i = 0; i < 4; i++)
153 {
154 struct edid_monitor_descriptor *monitor = NULL;
155
156 monitor = &edid_info->monitor_details.monitor_descriptor[i];
157
158 if (monitor->type == edid_monitor_descriptor_range)
159 {
160 sync.h_min = monitor->data.range_data.horizontal_min;
161 sync.h_max = monitor->data.range_data.horizontal_max;
162 sync.v_min = monitor->data.range_data.vertical_min;
163 sync.v_max = monitor->data.range_data.vertical_max;
164 }
165 }
166 /* On certain Apple LCD panels, set vertical min and max */
167 if ((eisaid) && (!strcmp(eisaid, "APP1592") ||
168 !strcmp(eisaid, "APPf401") ||
169 !strcmp(eisaid, "APP1792") ||
170 !strcmp(eisaid, "APP1992") ||
171 !strcmp(eisaid, "APP1692") ||
172 !strcmp(eisaid, "APP1892")))
173 sync.v_min = 50;
174 sync.v_max = 60;
175 }
176 }
177
178 /* More or less taken from Geert Uytterhoeven's fbset (GPL'ed as well..) */
179 htotal = var.left_margin+var.xres+var.right_margin+var.hsync_len;
180 vtotal = var.upper_margin+var.yres+var.lower_margin+var.vsync_len;
181 drate = (1E12/var.pixclock);
182 hrate = drate/htotal;
183 vrate = hrate/vtotal;
184 xsstart = var.xres+var.right_margin;
185 xsend = xsstart+var.hsync_len;
186 xtotal = xsend+var.left_margin;
187 ysstart = var.yres+var.lower_margin;
188 ysend = ysstart+var.vsync_len;
189 ytotal = ysend+var.upper_margin;
190 if (var.bits_per_pixel == 8)
191 depth = 8;
192 else
193 depth = var.red.length+var.green.length+var.blue.length;
194
195 /* look for nvidia and offb */
196 if (fix.id)
197 {
198 mem = strdup(fix.id);
199 while(((i = strlen(mem)) > 0) && isspace(mem[i - 1])) {
200 mem[i - 1] = '\0';
201 }
202 if (strstr(fix.id, "NVDA") != NULL)
203 nv = 1;
204
205 if (!strncmp(fix.id, "OFfb", 4))
206 offb = 1;
207 }
208
209 findVideoDevice(); /* For BusID */
210
211 /* Let's be nice and backup an existing xorg.conf */
212 if ((f = fopen("/etc/X11/xorg.conf", "r"))) {
213 system("mv /etc/X11/xorg.conf /etc/X11/xorg.conf.autobak");
214 printf("The current xorg.conf has been copied into /etc/X11/xorg.conf.autobak\n");
215 }
216
217 /* Check the permissions for writing */
218 if (!(f = fopen("/etc/X11/xorg.conf", "w"))) {
219 fprintf(stderr, "Can't write to /etc/X11/xorg.conf. Exiting.\n");
220 return -1;;
221 }
222
223 /*** BEGIN xorg.conf ***/
224 fprintf(f, "%s", header);
225
226 /* Set fontpaths */
227 DIR *fontdir = opendir(FONTPATH);
228 struct dirent *fontpath;
229
230 /* Add x fontserver note */
231 fprintf(f, "%s", fontpath_xfs);
232 if(fontdir != NULL) {
233 while ((fontpath = readdir (fontdir))) {
234 if((fontpath->d_name)[0] != '.') {
235 /* Only add directories */
236 strcpy(path, FONTPATH);
237 strcat(path, fontpath->d_name);
238 strcat(path, "/");
239 testdir = opendir(path);
240 if(testdir != NULL) {
241 fprintf(f, "\tFontPath\t\"%s\"\n",
242 path);
243 if(!strncmp(fontpath->d_name,"75dpi",5) ||
244 !strncmp(fontpath->d_name,"100dpi",6)){
245 fprintf(f, "\tFontPath\t\"%s:unscaled\"\n",
246 path);
247 }
248 closedir(testdir);
249 }
250 }
251 }
252 closedir(fontdir);
253 }
254 else {
255 fprintf(f, "%s", fontpath_gentoo);
256 }
257 fprintf(f, "EndSection\n\n");
258
259 /* modules section */
260 fprintf(f, "%s", modules_section);
261
262 /* extensions section */
263 fprintf(f, "%s", extensions_section);
264
265 /* serverflags section */
266 fprintf(f, "%s", serverflags_section);
267
268 /* input dev section */
269 fprintf(f, "%s", inputdev_section);
270
271 /* keyboard */
272
273 if(0 == keyboard_layout[0])
274 strcpy(keyboard_layout, "us\"\n");
275 fprintf(f, "%s", keyboard_linux_start);
276 fprintf(f, "%s", keyboard_layout);
277 fprintf(f, "%s", keyboard_linux_end);
278
279 /* mouse */
280 fprintf(f, "%s", mouse_section);
281
282 /* monitor section */
283 if (!offb)
284 fprintf(f, "%s", monitor_section);
285 else
286 fprintf(f, "%s", monitor_section_nomodes);
287
288 /* Use EDID's horizsync / vert refresh first, otherwise try to be sane */
289 if (sync.h_min && sync.h_max && sync.v_min && sync.v_max)
290 {
291 fprintf(f, "\tHorizSync %d-%d\n\tVertRefresh %d-%d\n",
292 sync.h_min, sync.h_max, sync.v_min, sync.v_max);
293 } else if (!strncmp(macid, "PowerMac4,2", 11)) {
294 fprintf(f, "\tHorizSync 28-49 \n\tVertRefresh 50-60\n");
295 } else if (sync.v_min && sync.v_max) {
296 /* Apple Studio/Cinema Display case */
297 fprintf(f, "\tHorizSync 30-130\n\tVertRefresh %d-%d\n",
298 sync.v_min, sync.v_max);
299 } else {
300 if(var.xres <= 640)
301 fprintf(f, " HorizSync 28-33\n VertRefresh 43-72\n");
302 else if(var.xres <= 800)
303 fprintf(f, " HorizSync 28-50\n VertRefresh 43-75\n");
304 else if(var.xres <= 1024)
305 fprintf(f, " HorizSync 30-70\n VertRefresh 50-160\n");
306 else if(var.xres <= 1152)
307 fprintf(f, " HorizSync 30-100\n VertRefresh 50-160\n");
308 else
309 fprintf(f, " HorizSync 30-130\n VertRefresh 50-160\n");
310 }
311
312 if (!offb)
313 {
314 fprintf(f, "%s", modes_section);
315 fprintf(f, " # D: %5.3f MHz, H: %5.3f kHz, V: %5.3f Hz\n", drate/1E6,
316 hrate/1E3, vrate);
317 fprintf(f, " Modeline \"%dx%d\" %5.3f %d %d %d %d %d %d %d %d ",
318 var.xres, var.yres, drate/1E6, var.xres, xsstart, xsend,
319 xtotal, var.yres, ysstart, ysend, ytotal);
320 if (var.vmode & FB_VMODE_INTERLACED)
321 fprintf(f, " Interlace");
322 if (var.vmode & FB_VMODE_DOUBLE)
323 fprintf(f, " DoubleScan");
324 if (var.sync & FB_SYNC_HOR_HIGH_ACT)
325 fprintf(f, " +HSync");
326 else
327 fprintf(f, " -HSync");
328 if (var.sync & FB_SYNC_VERT_HIGH_ACT)
329 fprintf(f, " +VSync");
330 else
331 fprintf(f, " -VSync");
332 if (var.sync & FB_SYNC_COMP_HIGH_ACT)
333 fprintf(f, " Composite");
334 if (var.sync & FB_SYNC_BROADCAST)
335 fprintf(f, " bcast");
336 }
337 fprintf(f, "%s", device_start);
338 depth = 16;
339
340 /* See if we know where the card is, if not... */
341 if (nv) {
342 if (!strncmp(macid, "PowerMac4,2", 11) || !strncmp(macid, "PowerMac4,5", 11))
343 fprintf(f, " Option \"FlatPanel\"\n");
344 fprintf(f, "%s", driver_nv);
345 fprintf(f, " Driver\t\"nv\"\n");
346 } else if((fbdev) || (bus == 0 && dev == 0 && func == 0)) {
347 if (!fbdev) {
348 fprintf(f, " # We couldn't determine the BusID of your ");
349 fprintf(f, "video card. So we will use\n# the fbdev driver\n");
350 }
351 fprintf(f, " #Option \"ShadowFB\" \"true\"\n");
352 fprintf(f, " #Option \"fbdev\" \"%s\"\n", name);
353 fprintf(f, " Driver\t\"fbdev\"\n");
354 fprintf(f, " #BusID \"0:0:0\"\n");
355 depth = 15;
356 } else {
357 /* Try and guess what video card is behind /dev/fb0 */
358 switch(fix.accel) {
359 /* ATI Mach64 Family cards */
360 case 6:
361 case 8:
362 case 9:
363 case 10:
364 fprintf(f, "%s", driver_on_opts);
365 fprintf(f, " Driver\t\"ati\"\n");
366 break;
367 /* IMS Twin Turbo */
368 case 14:
369 fprintf(f, "%s", driver_off_opts);
370 fprintf(f, " Driver\t\"imstt\"\n");
371 break;
372 /* Matrox Family */
373 case 16:
374 case 17:
375 case 18:
376 case 20:
377 case 21:
378 case 26:
379 fprintf(f, "%s", driver_off_opts);
380 fprintf(f, " Driver\t\"mga\"\n");
381 break;
382 /* Chips&Technology 6555x */
383 /* Apparently this is broken...
384 case 30:
385 fprintf(f, "%s", driver_off_opts);
386 fprintf(f, " Driver\t\"chips\"\n");
387 break;
388 */
389 /* VooDoo 3 / Banshee */
390 case 31:
391 fprintf(f, "%s", driver_off_opts);
392 fprintf(f, " Driver\t\"tdfx\"\n");
393 break;
394 /* ATI Rage 128 Family */
395 case 32:
396 fprintf(f, "%s", driver_r128);
397 fprintf(f, " Driver\t\"r128\"\n");
398 break;
399 /* ATI Radeon */
400 case 38:
401 fprintf(f, "%s", driver_radeon);
402 fprintf(f, " Driver\t\"radeon\"\n");
403 break;
404 default:
405 /* FIXME still broken? */
406 /* radeon seems to be broken on VE (aka 7000) */
407 if ((!strncmp(fix.id, "ATI Radeon", 10)) &&
408 (strncmp(fix.id, "ATI Radeon VE", 13))) {
409 fprintf(f, "%s", driver_radeon);
410 fprintf(f, " Driver\t\"radeon\"\n");
411 } else if (!strncmp(fix.id, "Rage128", 7)) {
412 fprintf(f, "%s", driver_r128);
413 fprintf(f, " Driver\t\"r128\"\n");
414 } else if (!strncmp(fix.id, "Rage Mobility M3", 16)) {
415 fprintf(f, "%s", driver_r128);
416 fprintf(f, " Driver\t\"r128\"\n");
417 } else {
418 fprintf(f, " #Option \"ShadowFB\"");
419 fprintf(f, " \"true\"\n");
420 fprintf(f, " Driver\t\"fbdev\"\n");
421 depth = 15;
422 }
423 break;
424 }
425 fprintf(f, " BusID \"PCI:%d:%d:%d\"\n", bus, dev, func);
426 }
427 fprintf(f, "%s", screen_section);
428 fprintf(f, "%d\n", depth);
429 if(fbdepth && depth != var.bits_per_pixel)
430 fprintf(f," DefaultFbBpp %d\n", var.bits_per_pixel);
431
432 /* Print this out for 8 / 16 / 24 */
433 for (i = 8;i <= 24;i = i + 8)
434 {
435 fprintf(f, " SubSection \"Display\"\n");
436 fprintf(f, " Depth %d\n", i);
437
438 /* If they've got 32bit use it, otherwise use 24 */
439 if (depth == 24 && depth != var.bits_per_pixel)
440 fprintf(f, " FbBpp %d\n",
441 var.bits_per_pixel);
442
443 fprintf(f, " Modes \"%dx%d\"\n", var.xres,
444 var.yres);
445 fprintf(f, " EndSubSection\n");
446 }
447
448 /* To catch depth = 15, common :( */
449 if(depth == 15) {
450 fprintf(f, " SubSection \"Display\"\n");
451 fprintf(f, " Depth %d\n", depth);
452
453 fprintf(f, " FbBpp %d\n",
454 var.bits_per_pixel);
455
456 fprintf(f, " Modes \"%dx%d\"\n", var.xres,
457 var.yres);
458 fprintf(f, " EndSubSection\n");
459 }
460
461 fprintf(f, "EndSection\n");
462 fprintf(f, "%s", dri_section);
463 /*** END Xorg.conf ***/
464
465 fclose(f);
466
467 fprintf(stdout, "Wrote /etc/X11/xorg.conf\n");
468 fprintf(stdout, "Please check the configuration file for additional options\n");
469
470 return 0;
471 }

  ViewVC Help
Powered by ViewVC 1.1.20