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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations) (download) (as text)
Wed Jun 15 20:46:20 2005 UTC (9 years, 3 months ago) by pylon
Branch: MAIN
Changes since 1.12: +1 -1 lines
File MIME type: text/x-csrc
Fix for nv by bambam.

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
76 DIR *testdir; /* Test if path is a directory */
77 char path[256]; /* Font Path */
78 char videobuf[MAX_VIDEO_LEN]; /* Video Card Device section */
79 int videolen = MAX_VIDEO_LEN - 1; /* How many chars we have left */
80 char dri = FALSE; /* DRI on or off */
81 char dump = FALSE; /* Dump debugging info? */
82 char quiet = FALSE; /* Be quiet */
83
84 if (!(fd = open(name, O_RDONLY)))
85 rc = 2; /* Failure */
86
87 if (!rc && ioctl(fd, FBIOGET_FSCREENINFO, &fix))
88 rc = 2; /* Failure */
89
90 if (!rc && ioctl(fd, FBIOGET_VSCREENINFO, &var))
91 rc = 2; /* Failure */
92
93 close(fd);
94
95 if (rc) {
96 fprintf(stderr, "Can't access %s. Exiting.\n", name);
97 return -1;
98 }
99
100 /* 640x480 framebuffer typically implies a CRT, so lets bump */
101 /* the resolution up to 800x600 to be a bit more sane */
102 if (var.xres == 640 && var.yres == 480) {
103 var.xres = 800;
104 var.yres = 600;
105 }
106
107 if (argc > 1) {
108 int args;
109 for(args = 1; args<argc; args++) {
110 if (!strcmp(argv[args], "--fbdepth"))
111 fbdepth = 1;
112 else if (!strcmp(argv[args], "--fbdev"))
113 fbdev = 1;
114 else if (!strcmp(argv[args], "--dump"))
115 dump = TRUE;
116 else if (!strcmp(argv[args], "--safe"))
117 fbdev = fbdepth = 1;
118 else if (!strcmp(argv[args], "--quiet"))
119 quiet = TRUE;
120 else if (!strncmp(argv[args], "--keymap=", 9)) {
121 strncpy(keyboard_layout, argv[args] + 9 , 29);
122 strcat(keyboard_layout, "\"\n");
123 }
124 else if (!strcmp(argv[args], "--list-keymaps")) {
125 printf("%s", keymap_list);
126 exit(0);
127 } else {
128 usage();
129 exit(0);
130 }
131 }
132 }
133
134 /* Check to see what type of Mac we're on.*/
135 /* e.g. PowerBook1,1 */
136 macid = macMachineType();
137
138 sync.h_min = 0;
139 sync.h_max = 0;
140 sync.v_min = 0;
141 sync.v_max = 0;
142
143 /* Check to see if EDID is available */
144 if ( (sizeof(struct edid1_info) == 256) &&
145 (sizeof(struct edid_monitor_descriptor) == 18) &&
146 get_edid_supported())
147 {
148 edid_info = get_edid_info();
149
150 if ( !(edid_info == NULL) &&
151 !(edid_info->version == 0) &&
152 !(edid_info->version == 0xff && edid_info->revision == 0xff) &&
153 !(edid_info->version == 0 && edid_info->revision == 0))
154 {
155 manufacturer[0] = edid_info->manufacturer_name.char1 + 'A' - 1;
156 manufacturer[1] = edid_info->manufacturer_name.char2 + 'A' - 1;
157 manufacturer[2] = edid_info->manufacturer_name.char3 + 'A' - 1;
158 manufacturer[3] = '\0';
159 eisaid = malloc(sizeof(manufacturer) + sizeof(edid_info->product_code));
160 sprintf(eisaid, "%s%04x", manufacturer, edid_info->product_code);
161
162 for (i = 0; i < 4; i++)
163 {
164 struct edid_monitor_descriptor *monitor = NULL;
165
166 monitor = &edid_info->monitor_details.monitor_descriptor[i];
167
168 if (monitor->type == edid_monitor_descriptor_range)
169 {
170 sync.h_min = monitor->data.range_data.horizontal_min;
171 sync.h_max = monitor->data.range_data.horizontal_max;
172 sync.v_min = monitor->data.range_data.vertical_min;
173 sync.v_max = monitor->data.range_data.vertical_max;
174 }
175 }
176 /* On certain Apple LCD panels, set vertical min and max */
177 if ((eisaid) && (!strcmp(eisaid, "APP1592") ||
178 !strcmp(eisaid, "APPf401") ||
179 !strcmp(eisaid, "APP1792") ||
180 !strcmp(eisaid, "APP1992") ||
181 !strcmp(eisaid, "APP1692") ||
182 !strcmp(eisaid, "APP1892")))
183 sync.v_min = 50;
184 sync.v_max = 60;
185 }
186 }
187
188 /* More or less taken from Geert Uytterhoeven's fbset (GPL'ed as well..) */
189 htotal = var.left_margin+var.xres+var.right_margin+var.hsync_len;
190 vtotal = var.upper_margin+var.yres+var.lower_margin+var.vsync_len;
191 drate = (1E12/var.pixclock);
192 hrate = drate/htotal;
193 vrate = hrate/vtotal;
194 xsstart = var.xres+var.right_margin;
195 xsend = xsstart+var.hsync_len;
196 xtotal = xsend+var.left_margin;
197 ysstart = var.yres+var.lower_margin;
198 ysend = ysstart+var.vsync_len;
199 ytotal = ysend+var.upper_margin;
200 if (var.bits_per_pixel == 8)
201 depth = 8;
202 else
203 depth = var.red.length+var.green.length+var.blue.length;
204
205 /* look for nvidia and offb */
206 if (fix.id)
207 {
208 mem = strdup(fix.id);
209 while(((i = strlen(mem)) > 0) && isspace(mem[i - 1])) {
210 mem[i - 1] = '\0';
211 }
212 if (strstr(fix.id, "NV") != NULL)
213 nv = 1;
214 if (!strncmp(fix.id, "OFfb", 4))
215 offb = 1;
216 }
217
218 findVideoDevice(); /* For BusID */
219
220 /* Work out the card specifics to figure out if we need DRI, etc. */
221 depth = 16;
222 memset(videobuf, 0, MAX_VIDEO_LEN);
223 if (nv) {
224 if (!strncmp(macid, "PowerMac4,2", 11) || !strncmp(macid, "PowerMac4,5", 11))
225 fprintf(f, " Option \"FlatPanel\"\n");
226 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_nv);
227 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"nv\"\n");
228 dri = FALSE;
229 } else if((fbdev) || (bus == 0 && dev == 0 && func == 0)) {
230 if (!fbdev) {
231 videolen -= snprintf(videobuf + strlen(videobuf),
232 videolen, "\t# We couldn't determine the BusID of your ");
233 videolen -= snprintf(videobuf + strlen(videobuf),
234 videolen, "video card. So we will use\n#\tthe fbdev driver\n");
235 }
236 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"ShadowFB\" \"true\"\n");
237 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"fbdev\" \"%s\"\n", name);
238 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"fbdev\"\n");
239 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #BusID \"0:0:0\"\n");
240 depth = 15;
241 dri = FALSE;
242 } else {
243 /* Try and guess what video card is behind /dev/fb0 */
244 switch(fix.accel) {
245 /* ATI Mach64 Family cards */
246 case 6:
247 case 8:
248 case 9:
249 case 10:
250 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_on_opts);
251 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"ati\"\n");
252 dri = FALSE; /* Can this be enabled? */
253 break;
254 /* IMS Twin Turbo */
255 case 14:
256 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
257 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"imstt\"\n");
258 dri = FALSE; /* Can this be enabled? */
259 break;
260 /* Matrox Family */
261 case 16:
262 case 17:
263 case 18:
264 case 20:
265 case 21:
266 case 26:
267 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
268 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"mga\"\n");
269 dri = FALSE; /* Can this be enabled? */
270 break;
271 /* Chips&Technology 6555x */
272 /* Is this still broken?
273 case 30:
274 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
275 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"chips\"\n");
276 dri = FALSE;
277 break;
278 */
279 /* VooDoo 3 / Banshee */
280 case 31:
281 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
282 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"tdfx\"\n");
283 dri = FALSE; /* Can this be enabled? */
284 break;
285 /* ATI Rage 128 Family */
286 case 32:
287 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
288 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
289 dri = TRUE;
290 break;
291 /* ATI Radeon */
292 /* FIXME what are the values for radeon > 9200? */
293 case 38:
294 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_radeon);
295 if(!strncmp(macid + 5, "Pegasos", 7))
296 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", "\tOption\t\"BusType\"\t\"PCI\"\n");
297 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"radeon\"\n");
298 dri = TRUE;
299 break;
300 default:
301 if (!strncmp(fix.id, "ATI Radeon", 10)) {
302 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_radeon);
303 if(!strncmp(macid + 5, "Pegasos", 7))
304 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", "\tOption\t\"BusType\"\t\"PCI\"\n");
305 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"radeon\"\n");
306 dri = TRUE;
307 } else if (!strncmp(fix.id, "Rage128", 7)) {
308 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
309 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
310 dri = TRUE;
311 } else if (!strncmp(fix.id, "Rage Mobility M3", 16)) {
312 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
313 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
314 dri = TRUE;
315 } else {
316 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"ShadowFB\" \"true\"\n");
317 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"fbdev\"\n");
318 dri = FALSE;
319 depth = 15;
320 }
321 break;
322 }
323 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " BusID \"PCI:%d:%d:%d\"\n", bus, dev, func);
324 }
325
326 if(dump) {
327 printf("Dumping Debugging Information:\n");
328 printf("\tMachine ID:\t%s\n", macid);
329 if(edid_info == NULL)
330 printf("\tNo EDID Information present\n");
331 else
332 printf("\tEDID Tag:\t%s\n", eisaid);
333 printf("\tH-Min: %i H-Max: %i V-Min: %i V-Max: %i\n", sync.h_min, sync.h_max, sync.v_min, sync.v_max);
334 printf("\tCard Dump:\n");
335 printf("\t\tFramebuffer Id:\t\t%s\n", fix.id);
336 printf("\t\tFramebuffer Type:\t%i\n", fix.accel);
337 printf("\t\tFramebuffer Depth:\t%i\n", depth);
338 return 0;
339 }
340
341 /* Let's be nice and backup an existing xorg.conf */
342 if ((f = fopen("/etc/X11/xorg.conf", "r"))) {
343 system("mv /etc/X11/xorg.conf /etc/X11/xorg.conf.autobak");
344 if(!quiet)
345 printf("The current xorg.conf has been copied into /etc/X11/xorg.conf.autobak\n");
346 }
347
348 /* Check the permissions for writing */
349 if (!(f = fopen("/etc/X11/xorg.conf", "w"))) {
350 fprintf(stderr, "Can't write to /etc/X11/xorg.conf. Exiting.\n");
351 return -1;;
352 }
353
354 /*** BEGIN xorg.conf ***/
355 fprintf(f, "%s", header);
356
357 /* Set fontpaths */
358 DIR *fontdir = opendir(FONTPATH);
359 struct dirent *fontpath;
360
361 /* Add x fontserver note */
362 fprintf(f, "%s", fontpath_xfs);
363 if(fontdir != NULL) {
364 while ((fontpath = readdir (fontdir))) {
365 if((fontpath->d_name)[0] != '.') {
366 /* Only add directories */
367 strcpy(path, FONTPATH);
368 strcat(path, fontpath->d_name);
369 strcat(path, "/");
370 testdir = opendir(path);
371 if(testdir != NULL) {
372 fprintf(f, "\tFontPath\t\"%s\"\n",
373 path);
374 if(!strncmp(fontpath->d_name,"75dpi",5) ||
375 !strncmp(fontpath->d_name,"100dpi",6)){
376 fprintf(f, "\tFontPath\t\"%s:unscaled\"\n",
377 path);
378 }
379 closedir(testdir);
380 }
381 }
382 }
383 closedir(fontdir);
384 }
385 else {
386 fprintf(f, "%s", fontpath_gentoo);
387 }
388 fprintf(f, "EndSection\n\n");
389
390 /* modules section */
391 fprintf(f, "%s", modules_section_start);
392 if(!dri) fprintf(f, "%s", "#");
393 fprintf(f, "%s", modules_section_end);
394
395 /* extensions section */
396 fprintf(f, "%s", extensions_section);
397
398 /* serverflags section */
399 fprintf(f, "%s", serverflags_section);
400
401 /* input dev section */
402 fprintf(f, "%s", inputdev_section);
403
404 /* keyboard */
405 if(0 == keyboard_layout[0])
406 strcpy(keyboard_layout, "us\"\n");
407 fprintf(f, "%s", keyboard_linux_start);
408 fprintf(f, "%s", keyboard_layout);
409 fprintf(f, "%s", keyboard_linux_end);
410
411 /* mouse */
412 fprintf(f, "%s", mouse_section);
413
414 /* monitor section */
415 if (!offb)
416 fprintf(f, "%s", monitor_section);
417 else
418 fprintf(f, "%s", monitor_section_nomodes);
419
420 /* Use EDID's horizsync / vert refresh first, otherwise try to be sane */
421 if (sync.h_min && sync.h_max && sync.v_min && sync.v_max)
422 {
423 fprintf(f, "\tHorizSync %d-%d\n\tVertRefresh %d-%d\n",
424 sync.h_min, sync.h_max, sync.v_min, sync.v_max);
425 } else if (!strncmp(macid, "PowerMac4,2", 11)) {
426 fprintf(f, "\tHorizSync 28-49 \n\tVertRefresh 50-60\n");
427 } else if (sync.v_min && sync.v_max) {
428 /* Apple Studio/Cinema Display case */
429 fprintf(f, "\tHorizSync 30-130\n\tVertRefresh %d-%d\n",
430 sync.v_min, sync.v_max);
431 } else {
432 if(var.xres <= 640)
433 fprintf(f, " HorizSync 28-33\n VertRefresh 43-72\n");
434 else if(var.xres <= 800)
435 fprintf(f, " HorizSync 28-50\n VertRefresh 43-75\n");
436 else if(var.xres <= 1024)
437 fprintf(f, " HorizSync 30-70\n VertRefresh 50-160\n");
438 else if(var.xres <= 1152)
439 fprintf(f, " HorizSync 30-100\n VertRefresh 50-160\n");
440 else
441 fprintf(f, " HorizSync 30-130\n VertRefresh 50-160\n");
442 }
443
444 if (!offb)
445 {
446 fprintf(f, "%s", modes_section);
447 fprintf(f, " # D: %5.3f MHz, H: %5.3f kHz, V: %5.3f Hz\n", drate/1E6,
448 hrate/1E3, vrate);
449 fprintf(f, " Modeline \"%dx%d\" %5.3f %d %d %d %d %d %d %d %d ",
450 var.xres, var.yres, drate/1E6, var.xres, xsstart, xsend,
451 xtotal, var.yres, ysstart, ysend, ytotal);
452 if (var.vmode & FB_VMODE_INTERLACED)
453 fprintf(f, " Interlace");
454 if (var.vmode & FB_VMODE_DOUBLE)
455 fprintf(f, " DoubleScan");
456 if (var.sync & FB_SYNC_HOR_HIGH_ACT)
457 fprintf(f, " +HSync");
458 else
459 fprintf(f, " -HSync");
460 if (var.sync & FB_SYNC_VERT_HIGH_ACT)
461 fprintf(f, " +VSync");
462 else
463 fprintf(f, " -VSync");
464 if (var.sync & FB_SYNC_COMP_HIGH_ACT)
465 fprintf(f, " Composite");
466 if (var.sync & FB_SYNC_BROADCAST)
467 fprintf(f, " bcast");
468 }
469 fprintf(f, "%s", device_start);
470 fprintf(f, "%s", videobuf);
471 fprintf(f, "%s", screen_section);
472 fprintf(f, "%d\n", depth);
473 if(fbdepth && depth != var.bits_per_pixel)
474 fprintf(f," DefaultFbBpp %d\n", var.bits_per_pixel);
475
476 /* Print this out for 8 / 16 / 24 */
477 for (i = 8;i <= 24;i = i + 8)
478 {
479 fprintf(f, " SubSection \"Display\"\n");
480 fprintf(f, " Depth %d\n", i);
481
482 /* If they've got 32bit use it, otherwise use 24 */
483 if (depth == 24 && depth != var.bits_per_pixel)
484 fprintf(f, " FbBpp %d\n",
485 var.bits_per_pixel);
486
487 fprintf(f, " Modes \"%dx%d\"\n", var.xres,
488 var.yres);
489 fprintf(f, " EndSubSection\n");
490 }
491
492 /* To catch depth = 15, common :( */
493 if(depth == 15) {
494 fprintf(f, " SubSection \"Display\"\n");
495 fprintf(f, " Depth %d\n", depth);
496
497 fprintf(f, " FbBpp %d\n",
498 var.bits_per_pixel);
499
500 fprintf(f, " Modes \"%dx%d\"\n", var.xres,
501 var.yres);
502 fprintf(f, " EndSubSection\n");
503 }
504
505 fprintf(f, "EndSection\n");
506 fprintf(f, "%s", dri_section);
507 /*** END Xorg.conf ***/
508
509 fclose(f);
510
511 if(!quiet) {
512 fprintf(stdout, "Wrote /etc/X11/xorg.conf\n");
513 fprintf(stdout, "Please check the configuration file for additional options\n");
514 }
515
516 return 0;
517 }

  ViewVC Help
Powered by ViewVC 1.1.20