xf86Configure.c 27.7 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright 2000-2002 by Alan Hourihane, Flint Mountain, North Wales.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Alan Hourihane not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Alan Hourihane makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * ALAN HOURIHANE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL ALAN HOURIHANE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Alan Hourihane, alanh@fairlite.demon.co.uk
 *
 */

26
27
28
29
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
30
31
32
33
34
35
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
36
37
#include <X11/X.h>
#include <X11/Xmd.h>
38
39
#include <pciaccess.h>
#include "Pci.h"
40
#include "os.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
41
42
43
#include "loaderProcs.h"
#include "xf86.h"
#include "xf86Config.h"
44
#include "xf86_OSlib.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
45
46
47
48
49
50
51
#include "xf86Priv.h"
#define IN_XSERVER
#include "xf86Parser.h"
#include "xf86tokens.h"
#include "Configint.h"
#include "vbe.h"
#include "xf86DDC.h"
52
#if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
53
54
55
56
57
58
59
#include "xf86Bus.h"
#include "xf86Sbus.h"
#endif
#include "globals.h"

typedef struct _DevToConfig {
    GDevRec GDev;
60
    struct pci_device * pVideo;
61
#if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
62
63
64
65
66
67
68
69
    sbusDevicePtr sVideo;
#endif
    int iDriver;
} DevToConfigRec, *DevToConfigPtr;

static DevToConfigPtr DevToConfig = NULL;
static int nDevToConfig = 0, CurrentDriver;

70
_X_EXPORT xf86MonPtr ConfiguredMonitor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
71
Bool xf86DoConfigurePass1 = TRUE;
72
static Bool foundMouse = FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
73

74
#if defined(__SCO__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
75
static char *DFLT_MOUSE_PROTO = "OSMouse";
76
#elif defined(__UNIXWARE__)
77
static char *DFLT_MOUSE_PROTO = "OSMouse";
78
static char *DFLT_MOUSE_DEV = "/dev/mouse";
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
79
80
81
82
83
84
#elif defined(QNX4)
static char *DFLT_MOUSE_PROTO = "OSMouse";
static char *DFLT_MOUSE_DEV = "/dev/mouse";
#elif defined(__QNXNTO__)
static char *DFLT_MOUSE_PROTO = "OSMouse";
static char *DFLT_MOUSE_DEV = "/dev/devi/mouse0";
85
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
86
87
static char *DFLT_MOUSE_DEV = "/dev/sysmouse";
static char *DFLT_MOUSE_PROTO = "auto";
88
89
90
#elif defined(linux)
static char DFLT_MOUSE_DEV[] = "/dev/input/mice";
static char DFLT_MOUSE_PROTO[] = "auto";
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#else
static char *DFLT_MOUSE_DEV = "/dev/mouse";
static char *DFLT_MOUSE_PROTO = "auto";
#endif

/*
 * This is called by the driver, either through xf86Match???Instances() or
 * directly.  We allocate a GDevRec and fill it in as much as we can, letting
 * the caller fill in the rest and/or change it as it sees fit.
 */
GDevPtr
xf86AddBusDeviceToConfigure(const char *driver, BusType bus, void *busData, int chipset)
{
    int i, j;
105
    struct pci_device * pVideo = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
106
107
108
109
110
111
112
113
    Bool isPrimary = FALSE;

    if (xf86DoProbe || !xf86DoConfigure || !xf86DoConfigurePass1)
	return NULL;

    /* Check for duplicates */
    switch (bus) {
    case BUS_PCI:
114
	pVideo = (struct pci_device *) busData;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
115
116
	for (i = 0;  i < nDevToConfig;  i++)
	    if (DevToConfig[i].pVideo &&
117
		(DevToConfig[i].pVideo->domain == pVideo->domain) &&
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
118
		(DevToConfig[i].pVideo->bus == pVideo->bus) &&
119
		(DevToConfig[i].pVideo->dev == pVideo->dev) &&
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		(DevToConfig[i].pVideo->func == pVideo->func))
		return NULL;
	isPrimary = xf86IsPrimaryPci(pVideo);
	break;
    case BUS_ISA:
	/*
	 * This needs to be revisited as it doesn't allow for non-PCI
	 * multihead.
	 */
	if (!xf86IsPrimaryIsa())
	    return NULL;
	isPrimary = TRUE;
	for (i = 0;  i < nDevToConfig;  i++)
	    if (!DevToConfig[i].pVideo)
		return NULL;
	break;
136
#if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    case BUS_SBUS:
	for (i = 0;  i < nDevToConfig;  i++)
	    if (DevToConfig[i].sVideo &&
		DevToConfig[i].sVideo->fbNum == ((sbusDevicePtr) busData)->fbNum)
		return NULL;
	break;
#endif
    default:
	return NULL;
    }

    /* Allocate new structure occurrence */
    i = nDevToConfig++;
    DevToConfig =
	xnfrealloc(DevToConfig, nDevToConfig * sizeof(DevToConfigRec));
#if 1   /* Doesn't work when a driver detects more than one adapter */
    if ((i > 0) && isPrimary) {
        memmove(DevToConfig + 1,DevToConfig,
		(nDevToConfig - 1) * sizeof(DevToConfigRec));
	i = 0;
    } 
#endif
    memset(DevToConfig + i, 0, sizeof(DevToConfigRec));

#   define NewDevice DevToConfig[i]

    NewDevice.GDev.chipID = NewDevice.GDev.chipRev = NewDevice.GDev.irq = -1;

    NewDevice.iDriver = CurrentDriver;

    /* Fill in what we know, converting the driver name to lower case */
    NewDevice.GDev.driver = xnfalloc(strlen(driver) + 1);
    for (j = 0;  (NewDevice.GDev.driver[j] = tolower(driver[j]));  j++);

    switch (bus) {
    case BUS_PCI: {
	const char *VendorName;
	const char *CardName;
	char busnum[8];

	NewDevice.pVideo = pVideo;
178
179
180

	VendorName = pci_device_get_vendor_name( pVideo );
	CardName = pci_device_get_device_name( pVideo );
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

	if (!VendorName) {
	    VendorName = xnfalloc(15);
	    sprintf((char*)VendorName, "Unknown Vendor");
	}

	if (!CardName) {
	    CardName = xnfalloc(14);
	    sprintf((char*)CardName, "Unknown Board");
	}

	NewDevice.GDev.identifier =
	    xnfalloc(strlen(VendorName) + strlen(CardName) + 2);
	sprintf(NewDevice.GDev.identifier, "%s %s", VendorName, CardName);

	NewDevice.GDev.vendor = (char *)VendorName;
	NewDevice.GDev.board = (char *)CardName;

	NewDevice.GDev.busID = xnfalloc(16);
	xf86FormatPciBusNumber(pVideo->bus, busnum);
	sprintf(NewDevice.GDev.busID, "PCI:%s:%d:%d",
202
	    busnum, pVideo->dev, pVideo->func);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
203

204
205
	NewDevice.GDev.chipID = pVideo->device_id;
	NewDevice.GDev.chipRev = pVideo->revision;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
206
207

	if (chipset < 0)
208
	    chipset = (pVideo->vendor_id << 16) | pVideo->device_id;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
209
210
211
212
213
214
	}
	break;
    case BUS_ISA:
	NewDevice.GDev.identifier = "ISA Adapter";
	NewDevice.GDev.busID = "ISA";
	break;
215
#if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    case BUS_SBUS: {
	char *promPath = NULL;
	NewDevice.sVideo = (sbusDevicePtr) busData;
	NewDevice.GDev.identifier = NewDevice.sVideo->descr;
	if (sparcPromInit() >= 0) {
	    promPath = sparcPromNode2Pathname(&NewDevice.sVideo->node);
	    sparcPromClose();
	}
	if (promPath) {
	    NewDevice.GDev.busID = xnfalloc(strlen(promPath) + 6);
	    sprintf(NewDevice.GDev.busID, "SBUS:%s", promPath);
	    xfree(promPath);
	} else {
	    NewDevice.GDev.busID = xnfalloc(12);
	    sprintf(NewDevice.GDev.busID, "SBUS:fb%d", NewDevice.sVideo->fbNum);
	}
	}
	break;
#endif
    default:
	break;
    }

    /* Get driver's available options */
    if (xf86DriverList[CurrentDriver]->AvailableOptions)
	NewDevice.GDev.options = (OptionInfoPtr)
	    (*xf86DriverList[CurrentDriver]->AvailableOptions)(chipset,
							       bus);

    return &NewDevice.GDev;

#   undef NewDevice
}

/*
 * Backwards compatibility
 */
253
_X_EXPORT GDevPtr
254
255
xf86AddDeviceToConfigure(const char *driver, struct pci_device * pVideo, 
			 int chipset)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
256
257
258
259
260
261
262
263
264
265
266
267
{
    return xf86AddBusDeviceToConfigure(driver, pVideo ? BUS_PCI : BUS_ISA,
				       pVideo, chipset);
}

static XF86ConfInputPtr
configureInputSection (void)
{
    XF86ConfInputPtr mouse = NULL;
    parsePrologue (XF86ConfInputPtr, XF86ConfInputRec)

    ptr->inp_identifier = "Keyboard0";
268
    ptr->inp_driver = "kbd";
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
269
270
271
272
273
274
275
276
277
278
279
    ptr->list.next = NULL;

    /* Crude mechanism to auto-detect mouse (os dependent) */
    { 
	int fd;
#ifdef WSCONS_SUPPORT
	fd = open("/dev/wsmouse", 0);
	if (fd > 0) {
	    DFLT_MOUSE_DEV = "/dev/wsmouse";
	    DFLT_MOUSE_PROTO = "wsmouse";
	    close(fd);
280
281
	} else {
	    ErrorF("cannot open /dev/wsmouse\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
282
283
284
	}
#endif

285
#ifndef __SCO__
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	fd = open(DFLT_MOUSE_DEV, 0);
	if (fd != -1) {
	    foundMouse = TRUE;
	    close(fd);
	}
#else
	foundMouse = TRUE;
#endif
    }

    mouse = xf86confmalloc(sizeof(XF86ConfInputRec));
    memset((XF86ConfInputPtr)mouse,0,sizeof(XF86ConfInputRec));
    mouse->inp_identifier = "Mouse0";
    mouse->inp_driver = "mouse";
    mouse->inp_option_lst = 
301
302
		xf86addNewOption(mouse->inp_option_lst, xstrdup("Protocol"),
				xstrdup(DFLT_MOUSE_PROTO));
303
#ifndef __SCO__
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
304
    mouse->inp_option_lst = 
305
306
		xf86addNewOption(mouse->inp_option_lst, xstrdup("Device"),
				xstrdup(DFLT_MOUSE_DEV));
307
#endif
308
    mouse->inp_option_lst = 
309
310
		xf86addNewOption(mouse->inp_option_lst, xstrdup("ZAxisMapping"),
				xstrdup("4 5 6 7"));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    ptr = (XF86ConfInputPtr)xf86addListItem((glp)ptr, (glp)mouse);
    return ptr;
}

static XF86ConfScreenPtr
configureScreenSection (int screennum)
{
    int i;
    int depths[] = { 1, 4, 8, 15, 16, 24/*, 32*/ };
    parsePrologue (XF86ConfScreenPtr, XF86ConfScreenRec)

    ptr->scrn_identifier = xf86confmalloc(18);
    sprintf(ptr->scrn_identifier, "Screen%d", screennum);
    ptr->scrn_monitor_str = xf86confmalloc(19);
    sprintf(ptr->scrn_monitor_str, "Monitor%d", screennum);
    ptr->scrn_device_str = xf86confmalloc(16);
    sprintf(ptr->scrn_device_str, "Card%d", screennum);

    for (i=0; i<sizeof(depths)/sizeof(depths[0]); i++)
    {
	XF86ConfDisplayPtr display;

	display = xf86confmalloc(sizeof(XF86ConfDisplayRec));
    	memset((XF86ConfDisplayPtr)display,0,sizeof(XF86ConfDisplayRec));
	display->disp_depth = depths[i];
	display->disp_black.red = display->disp_white.red = -1;
	display->disp_black.green = display->disp_white.green = -1;
	display->disp_black.blue = display->disp_white.blue = -1;
	ptr->scrn_display_lst = (XF86ConfDisplayPtr)xf86addListItem(
				     (glp)ptr->scrn_display_lst, (glp)display);
    }

    return ptr;
}

static const char* 
optionTypeToSting(OptionValueType type)
{
    switch (type) {
    case OPTV_NONE:
        return "";
    case OPTV_INTEGER:
        return "<i>";
    case OPTV_STRING:
        return "<str>";
    case OPTV_ANYSTR:
       return "[<str>]";
    case OPTV_REAL:
        return "<f>";
    case OPTV_BOOLEAN:
        return "[<bool>]";
    case OPTV_FREQ:
        return "<freq>";
    default:
        return "";
    }
}

static XF86ConfDevicePtr
configureDeviceSection (int screennum)
{
    char identifier[16];
    OptionInfoPtr p;
    int i = 0;
#ifdef DO_FBDEV_PROBE
    Bool foundFBDEV = FALSE;
#endif
    parsePrologue (XF86ConfDevicePtr, XF86ConfDeviceRec)

    /* Move device info to parser structure */
    sprintf(identifier, "Card%d", screennum);
    ptr->dev_identifier = strdup(identifier);
/*    ptr->dev_identifier = DevToConfig[screennum].GDev.identifier;*/
    ptr->dev_vendor = DevToConfig[screennum].GDev.vendor;
    ptr->dev_board = DevToConfig[screennum].GDev.board;
    ptr->dev_chipset = DevToConfig[screennum].GDev.chipset;
    ptr->dev_busid = DevToConfig[screennum].GDev.busID;
    ptr->dev_driver = DevToConfig[screennum].GDev.driver;
    ptr->dev_ramdac = DevToConfig[screennum].GDev.ramdac;
    for (i = 0;  (i < MAXDACSPEEDS) && (i < CONF_MAXDACSPEEDS);  i++)
        ptr->dev_dacSpeeds[i] = DevToConfig[screennum].GDev.dacSpeeds[i];
    ptr->dev_videoram = DevToConfig[screennum].GDev.videoRam;
    ptr->dev_textclockfreq = DevToConfig[screennum].GDev.textClockFreq;
    ptr->dev_bios_base = DevToConfig[screennum].GDev.BiosBase;
    ptr->dev_mem_base = DevToConfig[screennum].GDev.MemBase;
    ptr->dev_io_base = DevToConfig[screennum].GDev.IOBase;
    ptr->dev_clockchip = DevToConfig[screennum].GDev.clockchip;
    for (i = 0;  (i < MAXCLOCKS) && (i < DevToConfig[screennum].GDev.numclocks);  i++)
        ptr->dev_clock[i] = DevToConfig[screennum].GDev.clock[i];
    ptr->dev_clocks = i;
    ptr->dev_chipid = DevToConfig[screennum].GDev.chipID;
    ptr->dev_chiprev = DevToConfig[screennum].GDev.chipRev;
    ptr->dev_irq = DevToConfig[screennum].GDev.irq;

    /* Make sure older drivers don't segv */
    if (DevToConfig[screennum].GDev.options) {
    	/* Fill in the available driver options for people to use */
	const char *descrip =
	    "        ### Available Driver options are:-\n"
	    "        ### Values: <i>: integer, <f>: float, "
			"<bool>: \"True\"/\"False\",\n"
	    "        ### <string>: \"String\", <freq>: \"<f> Hz/kHz/MHz\"\n"
	    "        ### [arg]: arg optional\n";
	ptr->dev_comment = xstrdup(descrip);
	if (ptr->dev_comment) {
    	    for (p = DevToConfig[screennum].GDev.options;
		 p->name != NULL; p++) {
		char *p_e;
		const char *prefix = "        #Option     ";
		const char *middle = " \t# ";
		const char *suffix = "\n";
		const char *opttype = optionTypeToSting(p->type);
		char *optname;
		int len = strlen(ptr->dev_comment) + strlen(prefix) +
			  strlen(middle) + strlen(suffix) + 1;
		
		optname = xalloc(strlen(p->name) + 2 + 1);
		if (!optname)
		    break;
		sprintf(optname, "\"%s\"", p->name);

		len += max(20, strlen(optname));
		len += strlen(opttype);

		ptr->dev_comment = xrealloc(ptr->dev_comment, len);
		if (!ptr->dev_comment)
		    break;
		p_e = ptr->dev_comment + strlen(ptr->dev_comment);
		sprintf(p_e, "%s%-20s%s%s%s", prefix, optname, middle,
			opttype, suffix);
		xfree(optname);
	    }
    	}
    }

#ifdef DO_FBDEV_PROBE
    /* Crude mechanism to auto-detect fbdev (os dependent) */
    /* Skip it for now. Options list it anyway, and we can't
     * determine which screen/driver this belongs too anyway. */
    {
	int fd;

	fd = open("/dev/fb0", 0);
	if (fd != -1) {
	    foundFBDEV = TRUE;
	    close(fd);
	}
    }

    if (foundFBDEV) {
	XF86OptionPtr fbdev;

    	fbdev = xf86confmalloc(sizeof(XF86OptionRec));
    	memset((XF86OptionPtr)fbdev,0,sizeof(XF86OptionRec));
    	fbdev->opt_name = "UseFBDev";
	fbdev->opt_val = "ON";
	ptr->dev_option_lst = (XF86OptionPtr)xf86addListItem(
					(glp)ptr->dev_option_lst, (glp)fbdev);
    }
#endif

    return ptr;
}

static XF86ConfLayoutPtr
configureLayoutSection (void)
{
    int scrnum = 0;
    parsePrologue (XF86ConfLayoutPtr, XF86ConfLayoutRec)

Egbert Eich's avatar
Egbert Eich committed
481
    ptr->lay_identifier = "X.org Configured";
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
482
483
484
485
486
487
488
489
490

    {
	XF86ConfInputrefPtr iptr;

	iptr = xf86confmalloc (sizeof (XF86ConfInputrefRec));
	iptr->list.next = NULL;
	iptr->iref_option_lst = NULL;
	iptr->iref_inputdev_str = "Mouse0";
	iptr->iref_option_lst =
491
		xf86addNewOption (iptr->iref_option_lst, xstrdup("CorePointer"), NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
492
493
494
495
496
497
498
499
500
501
502
503
	ptr->lay_input_lst = (XF86ConfInputrefPtr)
		xf86addListItem ((glp) ptr->lay_input_lst, (glp) iptr);
    }

    {
	XF86ConfInputrefPtr iptr;

	iptr = xf86confmalloc (sizeof (XF86ConfInputrefRec));
	iptr->list.next = NULL;
	iptr->iref_option_lst = NULL;
	iptr->iref_inputdev_str = "Keyboard0";
	iptr->iref_option_lst =
504
		xf86addNewOption (iptr->iref_option_lst, xstrdup("CoreKeyboard"), NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
	ptr->lay_input_lst = (XF86ConfInputrefPtr)
		xf86addListItem ((glp) ptr->lay_input_lst, (glp) iptr);
    }

    for (scrnum = 0;  scrnum < nDevToConfig;  scrnum++) {
	XF86ConfAdjacencyPtr aptr;

	aptr = xf86confmalloc (sizeof (XF86ConfAdjacencyRec));
	aptr->list.next = NULL;
	aptr->adj_x = 0;
	aptr->adj_y = 0;
	aptr->adj_scrnum = scrnum;
	aptr->adj_screen_str = xnfalloc(18);
	sprintf(aptr->adj_screen_str, "Screen%d", scrnum);
	if (scrnum == 0) {
	    aptr->adj_where = CONF_ADJ_ABSOLUTE;
	    aptr->adj_refscreen = NULL;
	}
	else {
	    aptr->adj_where = CONF_ADJ_RIGHTOF;
	    aptr->adj_refscreen = xnfalloc(18);
	    sprintf(aptr->adj_refscreen, "Screen%d", scrnum - 1);
	}
    	ptr->lay_adjacency_lst =
	    (XF86ConfAdjacencyPtr)xf86addListItem((glp)ptr->lay_adjacency_lst,
					      (glp)aptr);
    }

    return ptr;
}

static XF86ConfFlagsPtr
configureFlagsSection (void)
{
    parsePrologue (XF86ConfFlagsPtr, XF86ConfFlagsRec)

    return ptr;
}

static XF86ConfModulePtr
configureModuleSection (void)
{
    char **elist, **el;
    /* Find the list of extension modules. */
    const char *esubdirs[] = {
	"extensions",
	NULL
    };
    const char *fsubdirs[] = {
	"fonts",
	NULL
    };
    parsePrologue (XF86ConfModulePtr, XF86ConfModuleRec)

    elist = LoaderListDirs(esubdirs, NULL);
    if (elist) {
	for (el = elist; *el; el++) {
	    XF86LoadPtr module;

    	    module = xf86confmalloc(sizeof(XF86LoadRec));
    	    memset((XF86LoadPtr)module,0,sizeof(XF86LoadRec));
    	    module->load_name = *el;
567
568
            ptr->mod_load_lst = (XF86LoadPtr)xf86addListItem(
                                (glp)ptr->mod_load_lst, (glp)module);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    	}
	xfree(elist);
    }

    /* Process list of font backends separately to include only required ones */
    elist = LoaderListDirs(fsubdirs, NULL);
    if (elist) {
	for (el = elist; *el; el++) {
	    XF86LoadPtr module;

    	    module = xf86confmalloc(sizeof(XF86LoadRec));
    	    memset((XF86LoadPtr)module,0,sizeof(XF86LoadRec));
    	    module->load_name = *el;

            /* Add only those font backends which are referenced by fontpath */
            /* 'strstr(dFP,"/dir")' is meant as 'dFP =~ m(/dir\W)' */
    	    if (defaultFontPath && (
Egbert Eich's avatar
Egbert Eich committed
586
587
		(strcmp(*el, "freetype")  == 0 &&
		 strstr(defaultFontPath, "/TTF")) ||
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
588
    	        (strcmp(*el, "type1")  == 0 &&
589
		 strstr(defaultFontPath, "/Type1")))) 
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
	    	ptr->mod_load_lst = (XF86LoadPtr)xf86addListItem(
					(glp)ptr->mod_load_lst, (glp)module);
    	}
	xfree(elist);
    }

    return ptr;
}

static XF86ConfFilesPtr
configureFilesSection (void)
{
    parsePrologue (XF86ConfFilesPtr, XF86ConfFilesRec)

   if (xf86ModulePath)
       ptr->file_modulepath = strdup(xf86ModulePath);
   if (defaultFontPath)
       ptr->file_fontpath = strdup(defaultFontPath);
   
    return ptr;
}

static XF86ConfMonitorPtr
configureMonitorSection (int screennum)
{
    parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec)

    ptr->mon_identifier = xf86confmalloc(19);
    sprintf(ptr->mon_identifier, "Monitor%d", screennum);
    ptr->mon_vendor = strdup("Monitor Vendor");
    ptr->mon_modelname = strdup("Monitor Model");

    return ptr;
}

static XF86ConfMonitorPtr
configureDDCMonitorSection (int screennum)
{
    int i = 0;
    int len, mon_width, mon_height;
#define displaySizeMaxLen 80
    char displaySize_string[displaySizeMaxLen];
    int displaySizeLen;

    parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec)

    ptr->mon_identifier = xf86confmalloc(19);
    sprintf(ptr->mon_identifier, "Monitor%d", screennum);
    ptr->mon_vendor = strdup(ConfiguredMonitor->vendor.name);
    ptr->mon_modelname = xf86confmalloc(12);
    sprintf(ptr->mon_modelname, "%x", ConfiguredMonitor->vendor.prod_id);

    /* features in centimetres, we want millimetres */
    mon_width  = 10 * ConfiguredMonitor->features.hsize ;
    mon_height = 10 * ConfiguredMonitor->features.vsize ;

#ifdef CONFIGURE_DISPLAYSIZE
    ptr->mon_width  = mon_width;
    ptr->mon_height = mon_height;
#else
    if (mon_width && mon_height) {
      /* when values available add DisplaySize option AS A COMMENT */

      displaySizeLen = snprintf(displaySize_string, displaySizeMaxLen,
				"\t#DisplaySize\t%5d %5d\t# mm\n",
				mon_width, mon_height);

      if (displaySizeLen>0 && displaySizeLen<displaySizeMaxLen) {
	if (ptr->mon_comment) {
	  len = strlen(ptr->mon_comment);
	} else {
	  len = 0;
	}
	if ((ptr->mon_comment =
	     xf86confrealloc(ptr->mon_comment, 
			     len+strlen(displaySize_string)))) {
	  strcpy(ptr->mon_comment + len, displaySize_string);
	}
      }
    }
#endif /* def CONFIGURE_DISPLAYSIZE */

    for (i=0;i<4;i++) {
	switch (ConfiguredMonitor->det_mon[i].type) {
	    case DS_NAME:
		ptr->mon_modelname  = xf86confrealloc(ptr->mon_modelname, 
		  strlen((char*)(ConfiguredMonitor->det_mon[i].section.name))
		    + 1);
		strcpy(ptr->mon_modelname,
		       (char*)(ConfiguredMonitor->det_mon[i].section.name));
680
		break;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
681
	    case DS_RANGES:
Egbert Eich's avatar
Egbert Eich committed
682
683
684
685
686
687
688
689
		ptr->mon_hsync[ptr->mon_n_hsync].lo =
		    ConfiguredMonitor->det_mon[i].section.ranges.min_h;
		ptr->mon_hsync[ptr->mon_n_hsync].hi =
		    ConfiguredMonitor->det_mon[i].section.ranges.max_h;
		ptr->mon_n_vrefresh = 1;
		ptr->mon_vrefresh[ptr->mon_n_hsync].lo =
		    ConfiguredMonitor->det_mon[i].section.ranges.min_v;
		ptr->mon_vrefresh[ptr->mon_n_hsync].hi =
Egbert Eich's avatar
Egbert Eich committed
690
		    ConfiguredMonitor->det_mon[i].section.ranges.max_v;
Egbert Eich's avatar
Egbert Eich committed
691
		ptr->mon_n_hsync++;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
692
693
694
695
696
697
	    default:
		break;
	}
    }

    if (ConfiguredMonitor->features.dpms) {
698
      ptr->mon_option_lst = xf86addNewOption(ptr->mon_option_lst, xstrdup("DPMS"), NULL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
    }

    return ptr;
}

void
DoConfigure()
{
    int i,j, screennum = -1;
    char *home = NULL;
    char *filename = NULL;
    XF86ConfigPtr xf86config = NULL;
    char **vlist, **vl;
    int *dev2screen;

    vlist = xf86DriverlistFromCompile();

    if (!vlist) {
	ErrorF("Missing output drivers.  Configuration failed.\n");
	goto bail;
    }

    ErrorF("List of video drivers:\n");
    for (vl = vlist; *vl; vl++)
	ErrorF("\t%s\n", *vl);

    /* Load all the drivers that were found. */
    xf86LoadModules(vlist, NULL);

    xfree(vlist);

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
    for (i = 0; i < xf86NumDrivers; i++) {
	xorgHWFlags flags;
	if (!xf86DriverList[i]->driverFunc
	    || !xf86DriverList[i]->driverFunc(NULL,
					      GET_REQUIRED_HW_INTERFACES,
					      &flags)
	    || NEED_IO_ENABLED(flags)) {
	    xorgHWAccess = TRUE;
	    break;
	}
    }
    /* Enable full I/O access */
    if (xorgHWAccess) {
	if(!xf86EnableIO())
	    /* oops, we have failed */
	    xorgHWAccess = FALSE;
    }

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
    /* Disable PCI devices */
    xf86ResourceBrokerInit();
    xf86AccessInit();
    xf86FindPrimaryDevice();
 
    /* Create XF86Config file structure */
    xf86config = malloc(sizeof(XF86ConfigRec));
    memset ((XF86ConfigPtr)xf86config, 0, sizeof(XF86ConfigRec));
    xf86config->conf_device_lst = NULL;
    xf86config->conf_screen_lst = NULL;
    xf86config->conf_monitor_lst = NULL;

    /* Call all of the probe functions, reporting the results. */
    for (CurrentDriver = 0;  CurrentDriver < xf86NumDrivers;  CurrentDriver++) {
762
	xorgHWFlags flags;
763
764
765
	Bool found_screen;
	DriverRec * const drv = xf86DriverList[CurrentDriver];

766
	if (!xorgHWAccess) {
767
768
	    if (!drv->driverFunc
		|| !drv->driverFunc( NULL, GET_REQUIRED_HW_INTERFACES, &flags )
769
770
771
		|| NEED_IO_ENABLED(flags)) 
		continue;
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
772
	
773
774
775
776
	found_screen = xf86CallDriverProbe( drv, TRUE );
	if ( found_screen && drv->Identify ) {
	    (*drv->Identify)(0);
	}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
    }

    if (nDevToConfig <= 0) {
	ErrorF("No devices to configure.  Configuration failed.\n");
	goto bail;
    }

    /* Add device, monitor and screen sections for detected devices */
    for (screennum = 0;  screennum < nDevToConfig;  screennum++) {
    	XF86ConfDevicePtr DevicePtr;
	XF86ConfMonitorPtr MonitorPtr;
	XF86ConfScreenPtr ScreenPtr;

	DevicePtr = configureDeviceSection(screennum);
    	xf86config->conf_device_lst = (XF86ConfDevicePtr)xf86addListItem(
			    (glp)xf86config->conf_device_lst, (glp)DevicePtr);
	MonitorPtr = configureMonitorSection(screennum);
    	xf86config->conf_monitor_lst = (XF86ConfMonitorPtr)xf86addListItem(
			    (glp)xf86config->conf_monitor_lst, (glp)MonitorPtr);
	ScreenPtr = configureScreenSection(screennum);
    	xf86config->conf_screen_lst = (XF86ConfScreenPtr)xf86addListItem(
			    (glp)xf86config->conf_screen_lst, (glp)ScreenPtr);
    }

    xf86config->conf_files = configureFilesSection();
    xf86config->conf_modules = configureModuleSection();
    xf86config->conf_flags = configureFlagsSection();
Adam Jackson's avatar
Adam Jackson committed
804
805
806
807
    xf86config->conf_videoadaptor_lst = NULL;
    xf86config->conf_modes_lst = NULL;
    xf86config->conf_vendor_lst = NULL;
    xf86config->conf_dri = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
808
809
810
811
812
813
    xf86config->conf_input_lst = configureInputSection();
    xf86config->conf_layout_lst = configureLayoutSection();

    if (!(home = getenv("HOME")))
    	home = "/";
    {
814
#if !defined(PATH_MAX)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
815
816
817
818
819
820
821
822
823
#define PATH_MAX 1024
#endif
        const char* configfile = XF86CONFIGFILE".new";
    	char homebuf[PATH_MAX];
    	/* getenv might return R/O memory, as with OS/2 */
    	strncpy(homebuf,home,PATH_MAX-1);
    	homebuf[PATH_MAX-1] = '\0';
    	home = homebuf;
    	if (!(filename =
Daniel Stone's avatar
Daniel Stone committed
824
	     (char *)xalloc(strlen(home) + 
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
	  			 strlen(configfile) + 3)))

      	if (home[0] == '/' && home[1] == '\0')
            home[0] = '\0';
#ifndef QNX4
	sprintf(filename, "%s/%s", home,configfile);
#else
	sprintf(filename, "//%d%s/%s", getnid(),home,configfile);
#endif
	
    }

    xf86writeConfigFile(filename, xf86config);

    xf86DoConfigurePass1 = FALSE;
    /* Try to get DDC information filled in */
    xf86ConfigFile = filename;
842
    if (xf86HandleConfigFile(FALSE) != CONFIG_OK) {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
	goto bail;
    }

    xf86DoConfigurePass1 = FALSE;
    
    dev2screen = xnfcalloc(1,xf86NumDrivers*sizeof(int));

    {
	Bool *driverProbed = xnfcalloc(1,xf86NumDrivers*sizeof(Bool));
	for (screennum = 0;  screennum < nDevToConfig;  screennum++) {
	    int k,l,n,oldNumScreens;

	    i = DevToConfig[screennum].iDriver;

	    if (driverProbed[i]) continue;
	    driverProbed[i] = TRUE;
	    
	    oldNumScreens = xf86NumScreens;

862
	    xf86CallDriverProbe( xf86DriverList[i], FALSE );
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936

	    /* reorder */
	    k = screennum > 0 ? screennum : 1;
	    for (l = oldNumScreens; l < xf86NumScreens; l++) {
	        /* is screen primary? */
	        Bool primary = FALSE;
		for (n = 0; n<xf86Screens[l]->numEntities; n++) {
	            if (xf86IsEntityPrimary(xf86Screens[l]->entityList[n])) {
		        dev2screen[0] = l;
			primary = TRUE;
			break;
		    }
		}
		if (primary) continue;
		/* not primary: assign it to next device of same driver */
		/* 
		 * NOTE: we assume that devices in DevToConfig 
		 * and xf86Screens[] have the same order except
		 * for the primary device which always comes first.
		 */
		for (; k < nDevToConfig; k++) {
		    if (DevToConfig[k].iDriver == i) {
		        dev2screen[k++] = l;
			break;
		    }
		}
	    }
	}
	xfree(driverProbed);
    }
    

    if (nDevToConfig != xf86NumScreens) {
	ErrorF("Number of created screens does not match number of detected"
	       " devices.\n  Configuration failed.\n");
	goto bail;
    }

    xf86PostProbe();
    xf86EntityInit();

    for (j = 0; j < xf86NumScreens; j++) {
	xf86Screens[j]->scrnIndex = j;
    }

    xf86freeMonitorList(xf86config->conf_monitor_lst);
    xf86config->conf_monitor_lst = NULL;
    xf86freeScreenList(xf86config->conf_screen_lst);
    xf86config->conf_screen_lst = NULL;
    for (j = 0; j < xf86NumScreens; j++) {
	XF86ConfMonitorPtr MonitorPtr;
	XF86ConfScreenPtr ScreenPtr;

	ConfiguredMonitor = NULL;

	xf86EnableAccess(xf86Screens[dev2screen[j]]);
	if ((*xf86Screens[dev2screen[j]]->PreInit)(xf86Screens[dev2screen[j]], 
						   PROBE_DETECT) &&
	    ConfiguredMonitor) {
	    MonitorPtr = configureDDCMonitorSection(j);
	} else {
	    MonitorPtr = configureMonitorSection(j);
	}
	ScreenPtr = configureScreenSection(j);
	xf86config->conf_monitor_lst = (XF86ConfMonitorPtr)xf86addListItem(
		(glp)xf86config->conf_monitor_lst, (glp)MonitorPtr);
	xf86config->conf_screen_lst = (XF86ConfScreenPtr)xf86addListItem(
		(glp)xf86config->conf_screen_lst, (glp)ScreenPtr);
    }

    xf86writeConfigFile(filename, xf86config);

    ErrorF("\n");

937
#ifdef __SCO__
Egbert Eich's avatar
Egbert Eich committed
938
939
940
    ErrorF("\n"__XSERVERNAME__
	   " is using the kernel event driver to access the mouse.\n"
	    "If you wish to use the internal "__XSERVERNAME__
941
	   " mouse drivers, please\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
942
	    "edit the file and correct the Device.\n");
943
#else /* !__SCO__ */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
944
    if (!foundMouse) {
Egbert Eich's avatar
Egbert Eich committed
945
	ErrorF("\n"__XSERVERNAME__" is not able to detect your mouse.\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
946
947
		"Edit the file and correct the Device.\n");
    } else {
Egbert Eich's avatar
Egbert Eich committed
948
	ErrorF("\n"__XSERVERNAME__" detected your mouse at device %s.\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
949
		"Please check your config if the mouse is still not\n"
Egbert Eich's avatar
Egbert Eich committed
950
951
		"operational, as by default "__XSERVERNAME__
	       " tries to autodetect\n"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
952
953
		"the protocol.\n",DFLT_MOUSE_DEV);
    }
954
#endif /* !__SCO__ */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
955
956

    if (xf86NumScreens > 1) {
Egbert Eich's avatar
Egbert Eich committed
957
958
	ErrorF("\n"__XSERVERNAME__
	       " has configured a multihead system, please check your config.\n");
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
959
960
    }

Egbert Eich's avatar
Egbert Eich committed
961
    ErrorF("\nYour %s file is %s\n\n", XF86CONFIGFILE ,filename);
Egbert Eich's avatar
Egbert Eich committed
962
    ErrorF("To test the server, run 'X -config %s'\n\n", filename);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
963
964

bail:
965
    OsCleanup(TRUE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
966
967
968
969
    AbortDDX();
    fflush(stderr);
    exit(0);
}