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

Contents of /Xorgautoconfig/Xorgautoconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download) (as text)
Tue Apr 19 19:25:11 2005 UTC (9 years, 5 months ago) by josejx
Branch: MAIN
Changes since 1.3: +6 -2 lines
File MIME type: text/x-csrc
Added list of keymaps

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

  ViewVC Help
Powered by ViewVC 1.1.20