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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations) (download) (as text)
Tue Jun 14 03:13:55 2005 UTC (9 years, 1 month ago) by josejx
Branch: MAIN
Changes since 1.10: +31 -7 lines
File MIME type: text/x-csrc
Added init script, fixed Makefile and added dump function.

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.11 char dump = FALSE; /* Dump debugging info? */
82     char quiet = FALSE; /* Be quiet */
83 josejx 1.3
84 pylon 1.1 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 josejx 1.11
95     if (rc) {
96     fprintf(stderr, "Can't access %s. Exiting.\n", name);
97     return -1;
98     }
99 pylon 1.1
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 josejx 1.3 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 josejx 1.11 else if (!strcmp(argv[args], "--dump"))
115     dump = TRUE;
116 josejx 1.3 else if (!strcmp(argv[args], "--safe"))
117     fbdev = fbdepth = 1;
118 josejx 1.11 else if (!strcmp(argv[args], "--quiet"))
119     quiet = TRUE;
120 josejx 1.3 else if (!strncmp(argv[args], "--keymap=", 9)) {
121     strncpy(keyboard_layout, argv[args] + 9 , 29);
122     strcat(keyboard_layout, "\"\n");
123 josejx 1.4 }
124     else if (!strcmp(argv[args], "--list-keymaps")) {
125     printf("%s", keymap_list);
126     exit(0);
127 josejx 1.3 } else {
128     usage();
129     exit(0);
130     }
131 pylon 1.1 }
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, "NVDA") != NULL)
213     nv = 1;
214    
215     if (!strncmp(fix.id, "OFfb", 4))
216     offb = 1;
217     }
218    
219     findVideoDevice(); /* For BusID */
220    
221 josejx 1.6 /* Work out the card specifics to figure out if we need DRI, etc. */
222     depth = 16;
223 josejx 1.9 memset(videobuf, 0, MAX_VIDEO_LEN);
224 josejx 1.6 if (nv) {
225     if (!strncmp(macid, "PowerMac4,2", 11) || !strncmp(macid, "PowerMac4,5", 11))
226     fprintf(f, " Option \"FlatPanel\"\n");
227 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_nv);
228     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"nv\"\n");
229 josejx 1.6 dri = FALSE;
230     } else if((fbdev) || (bus == 0 && dev == 0 && func == 0)) {
231     if (!fbdev) {
232 josejx 1.10 videolen -= snprintf(videobuf + strlen(videobuf),
233     videolen, "\t# We couldn't determine the BusID of your ");
234     videolen -= snprintf(videobuf + strlen(videobuf),
235     videolen, "video card. So we will use\n#\tthe fbdev driver\n");
236 josejx 1.6 }
237 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"ShadowFB\" \"true\"\n");
238     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"fbdev\" \"%s\"\n", name);
239     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"fbdev\"\n");
240     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #BusID \"0:0:0\"\n");
241 josejx 1.6 depth = 15;
242     dri = FALSE;
243     } else {
244     /* Try and guess what video card is behind /dev/fb0 */
245     switch(fix.accel) {
246     /* ATI Mach64 Family cards */
247     case 6:
248     case 8:
249     case 9:
250     case 10:
251 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_on_opts);
252     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"ati\"\n");
253 josejx 1.6 dri = FALSE; /* Can this be enabled? */
254     break;
255     /* IMS Twin Turbo */
256     case 14:
257 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
258     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"imstt\"\n");
259 josejx 1.6 dri = FALSE; /* Can this be enabled? */
260     break;
261     /* Matrox Family */
262     case 16:
263     case 17:
264     case 18:
265     case 20:
266     case 21:
267     case 26:
268 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
269     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"mga\"\n");
270 josejx 1.6 dri = FALSE; /* Can this be enabled? */
271     break;
272     /* Chips&Technology 6555x */
273     /* Is this still broken?
274     case 30:
275 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
276     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"chips\"\n");
277 josejx 1.6 dri = FALSE;
278     break;
279     */
280     /* VooDoo 3 / Banshee */
281     case 31:
282 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_off_opts);
283     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"tdfx\"\n");
284 josejx 1.6 dri = FALSE; /* Can this be enabled? */
285     break;
286     /* ATI Rage 128 Family */
287     case 32:
288 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
289     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
290 josejx 1.6 dri = TRUE;
291     break;
292     /* ATI Radeon */
293     /* FIXME what are the values for radeon > 9200? */
294     case 38:
295 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_radeon);
296 josejx 1.6 if(!strncmp(macid + 5, "Pegasos", 7))
297 josejx 1.8 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", "\tOption\t\"BusType\"\t\"PCI\"\n");
298 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"radeon\"\n");
299 josejx 1.6 dri = TRUE;
300     break;
301     default:
302     /* FIXME still broken? */
303     /* radeon seems to be broken on VE (aka 7000) */
304     if ((!strncmp(fix.id, "ATI Radeon", 10)) &&
305     (strncmp(fix.id, "ATI Radeon VE", 13))) {
306 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_radeon);
307 josejx 1.6 if(!strncmp(macid + 5, "Pegasos", 7))
308 josejx 1.8 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", "\tOption\t\"BusType\"\t\"PCI\"\n");
309 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"radeon\"\n");
310 josejx 1.6 dri = TRUE;
311     } else if (!strncmp(fix.id, "Rage128", 7)) {
312 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
313     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
314 josejx 1.6 dri = TRUE;
315     } else if (!strncmp(fix.id, "Rage Mobility M3", 16)) {
316 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, "%s", driver_r128);
317     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"r128\"\n");
318 josejx 1.6 dri = TRUE;
319     } else {
320 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " #Option \"ShadowFB\" \"true\"\n");
321     videolen -= snprintf(videobuf + strlen(videobuf), videolen, " Driver\t\"fbdev\"\n");
322 josejx 1.6 dri = FALSE;
323     depth = 15;
324     }
325     break;
326     }
327 josejx 1.7 videolen -= snprintf(videobuf + strlen(videobuf), videolen, " BusID \"PCI:%d:%d:%d\"\n", bus, dev, func);
328 josejx 1.6 }
329    
330 josejx 1.11 if(dump) {
331     printf("Dumping Debugging Information:\n");
332     printf("\tMachine ID:\t%s\n", macid);
333     if(edid_info == NULL)
334     printf("\tNo EDID Information present\n");
335     else
336     printf("\tEDID Tag:\t%s\n", eisaid);
337     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);
338     printf("\tCard Dump:\n");
339     printf("\t\tFramebuffer Id:\t\t%s\n", fix.id);
340     printf("\t\tFramebuffer Type:\t%i\n", fix.accel);
341     printf("\t\tFramebuffer Depth:\t%i\n", depth);
342     return 0;
343     }
344    
345 pylon 1.1 /* Let's be nice and backup an existing xorg.conf */
346     if ((f = fopen("/etc/X11/xorg.conf", "r"))) {
347 pylon 1.5 system("mv /etc/X11/xorg.conf /etc/X11/xorg.conf.autobak");
348 josejx 1.11 if(!quiet)
349 pylon 1.5 printf("The current xorg.conf has been copied into /etc/X11/xorg.conf.autobak\n");
350 pylon 1.1 }
351    
352 pylon 1.5 /* Check the permissions for writing */
353 pylon 1.1 if (!(f = fopen("/etc/X11/xorg.conf", "w"))) {
354     fprintf(stderr, "Can't write to /etc/X11/xorg.conf. Exiting.\n");
355     return -1;;
356     }
357    
358     /*** BEGIN xorg.conf ***/
359     fprintf(f, "%s", header);
360    
361 pylon 1.2 /* Set fontpaths */
362     DIR *fontdir = opendir(FONTPATH);
363     struct dirent *fontpath;
364    
365     /* Add x fontserver note */
366     fprintf(f, "%s", fontpath_xfs);
367     if(fontdir != NULL) {
368     while ((fontpath = readdir (fontdir))) {
369     if((fontpath->d_name)[0] != '.') {
370     /* Only add directories */
371     strcpy(path, FONTPATH);
372     strcat(path, fontpath->d_name);
373     strcat(path, "/");
374     testdir = opendir(path);
375     if(testdir != NULL) {
376     fprintf(f, "\tFontPath\t\"%s\"\n",
377     path);
378     if(!strncmp(fontpath->d_name,"75dpi",5) ||
379     !strncmp(fontpath->d_name,"100dpi",6)){
380     fprintf(f, "\tFontPath\t\"%s:unscaled\"\n",
381     path);
382     }
383     closedir(testdir);
384     }
385     }
386     }
387     closedir(fontdir);
388     }
389     else {
390     fprintf(f, "%s", fontpath_gentoo);
391     }
392     fprintf(f, "EndSection\n\n");
393 pylon 1.1
394     /* modules section */
395 josejx 1.6 fprintf(f, "%s", modules_section_start);
396     if(!dri) fprintf(f, "%s", "#");
397     fprintf(f, "%s", modules_section_end);
398 pylon 1.1
399     /* extensions section */
400     fprintf(f, "%s", extensions_section);
401    
402     /* serverflags section */
403     fprintf(f, "%s", serverflags_section);
404    
405     /* input dev section */
406     fprintf(f, "%s", inputdev_section);
407    
408     /* keyboard */
409 josejx 1.3 if(0 == keyboard_layout[0])
410     strcpy(keyboard_layout, "us\"\n");
411     fprintf(f, "%s", keyboard_linux_start);
412     fprintf(f, "%s", keyboard_layout);
413     fprintf(f, "%s", keyboard_linux_end);
414    
415 pylon 1.1 /* mouse */
416     fprintf(f, "%s", mouse_section);
417    
418     /* monitor section */
419     if (!offb)
420     fprintf(f, "%s", monitor_section);
421     else
422     fprintf(f, "%s", monitor_section_nomodes);
423    
424     /* Use EDID's horizsync / vert refresh first, otherwise try to be sane */
425     if (sync.h_min && sync.h_max && sync.v_min && sync.v_max)
426     {
427     fprintf(f, "\tHorizSync %d-%d\n\tVertRefresh %d-%d\n",
428     sync.h_min, sync.h_max, sync.v_min, sync.v_max);
429     } else if (!strncmp(macid, "PowerMac4,2", 11)) {
430     fprintf(f, "\tHorizSync 28-49 \n\tVertRefresh 50-60\n");
431     } else if (sync.v_min && sync.v_max) {
432     /* Apple Studio/Cinema Display case */
433     fprintf(f, "\tHorizSync 30-130\n\tVertRefresh %d-%d\n",
434     sync.v_min, sync.v_max);
435     } else {
436     if(var.xres <= 640)
437     fprintf(f, " HorizSync 28-33\n VertRefresh 43-72\n");
438     else if(var.xres <= 800)
439     fprintf(f, " HorizSync 28-50\n VertRefresh 43-75\n");
440     else if(var.xres <= 1024)
441     fprintf(f, " HorizSync 30-70\n VertRefresh 50-160\n");
442     else if(var.xres <= 1152)
443     fprintf(f, " HorizSync 30-100\n VertRefresh 50-160\n");
444     else
445     fprintf(f, " HorizSync 30-130\n VertRefresh 50-160\n");
446     }
447    
448     if (!offb)
449     {
450     fprintf(f, "%s", modes_section);
451     fprintf(f, " # D: %5.3f MHz, H: %5.3f kHz, V: %5.3f Hz\n", drate/1E6,
452     hrate/1E3, vrate);
453     fprintf(f, " Modeline \"%dx%d\" %5.3f %d %d %d %d %d %d %d %d ",
454     var.xres, var.yres, drate/1E6, var.xres, xsstart, xsend,
455     xtotal, var.yres, ysstart, ysend, ytotal);
456     if (var.vmode & FB_VMODE_INTERLACED)
457     fprintf(f, " Interlace");
458     if (var.vmode & FB_VMODE_DOUBLE)
459     fprintf(f, " DoubleScan");
460     if (var.sync & FB_SYNC_HOR_HIGH_ACT)
461     fprintf(f, " +HSync");
462     else
463     fprintf(f, " -HSync");
464     if (var.sync & FB_SYNC_VERT_HIGH_ACT)
465     fprintf(f, " +VSync");
466     else
467     fprintf(f, " -VSync");
468     if (var.sync & FB_SYNC_COMP_HIGH_ACT)
469     fprintf(f, " Composite");
470     if (var.sync & FB_SYNC_BROADCAST)
471     fprintf(f, " bcast");
472     }
473     fprintf(f, "%s", device_start);
474 josejx 1.6 fprintf(f, "%s", videobuf);
475 pylon 1.1 fprintf(f, "%s", screen_section);
476     fprintf(f, "%d\n", depth);
477     if(fbdepth && depth != var.bits_per_pixel)
478     fprintf(f," DefaultFbBpp %d\n", var.bits_per_pixel);
479    
480     /* Print this out for 8 / 16 / 24 */
481     for (i = 8;i <= 24;i = i + 8)
482     {
483     fprintf(f, " SubSection \"Display\"\n");
484     fprintf(f, " Depth %d\n", i);
485    
486     /* If they've got 32bit use it, otherwise use 24 */
487     if (depth == 24 && depth != var.bits_per_pixel)
488     fprintf(f, " FbBpp %d\n",
489     var.bits_per_pixel);
490    
491     fprintf(f, " Modes \"%dx%d\"\n", var.xres,
492     var.yres);
493     fprintf(f, " EndSubSection\n");
494     }
495    
496     /* To catch depth = 15, common :( */
497     if(depth == 15) {
498     fprintf(f, " SubSection \"Display\"\n");
499     fprintf(f, " Depth %d\n", depth);
500    
501     fprintf(f, " FbBpp %d\n",
502     var.bits_per_pixel);
503    
504     fprintf(f, " Modes \"%dx%d\"\n", var.xres,
505     var.yres);
506     fprintf(f, " EndSubSection\n");
507     }
508    
509     fprintf(f, "EndSection\n");
510     fprintf(f, "%s", dri_section);
511 pylon 1.2 /*** END Xorg.conf ***/
512 pylon 1.1
513     fclose(f);
514    
515 josejx 1.11 if(!quiet) {
516     fprintf(stdout, "Wrote /etc/X11/xorg.conf\n");
517     fprintf(stdout, "Please check the configuration file for additional options\n");
518     }
519 pylon 1.1
520     return 0;
521     }

  ViewVC Help
Powered by ViewVC 1.1.20