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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download) (as text)
Fri Jun 10 23:19:44 2005 UTC (9 years, 3 months ago) by josejx
Branch: MAIN
Changes since 1.5: +116 -93 lines
File MIME type: text/x-csrc
Added DRI enabling code (needs testing) and added comment for Apple Panels.

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

  ViewVC Help
Powered by ViewVC 1.1.20