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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download) (as text)
Mon Apr 18 05:34:46 2005 UTC (9 years, 8 months ago) by josejx
Branch: MAIN
Changes since 1.2: +40 -38 lines
File MIME type: text/x-csrc
Fixed a few issues from last commit and added keymap switching support.

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

  ViewVC Help
Powered by ViewVC 1.1.20