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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations) (download) (as text)
Sun Jun 12 12:16:26 2005 UTC (9 years, 6 months ago) by josejx
Branch: MAIN
Changes since 1.9: +4 -2 lines
File MIME type: text/x-csrc
Added EDID vsync/hsync sanity check.

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

  ViewVC Help
Powered by ViewVC 1.1.20