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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (download) (as text) (vendor branch)
Mon Apr 18 01:25:26 2005 UTC (9 years, 5 months ago) by pylon
Branch: Xorgautoconfig
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/x-csrc
Autoconfigure X.Org on ppc machines

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

  ViewVC Help
Powered by ViewVC 1.1.20