~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/xfree86/common/xf86Helper.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86Helper.c,v 1.136 2004/01/27 01:31:45 dawes Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
 
5
 *
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a
 
7
 * copy of this software and associated documentation files (the "Software"),
 
8
 * to deal in the Software without restriction, including without limitation
 
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
 * and/or sell copies of the Software, and to permit persons to whom the
 
11
 * Software is furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice shall be included in
 
14
 * all copies or substantial portions of the Software.
 
15
 *
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
19
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
20
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
21
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
22
 * OTHER DEALINGS IN THE SOFTWARE.
 
23
 *
 
24
 * Except as contained in this notice, the name of the copyright holder(s)
 
25
 * and author(s) shall not be used in advertising or otherwise to promote
 
26
 * the sale, use or other dealings in this Software without prior written
 
27
 * authorization from the copyright holder(s) and author(s).
 
28
 */
 
29
 
 
30
/*
 
31
 * Authors: Dirk Hohndel <hohndel@XFree86.Org>
 
32
 *          David Dawes <dawes@XFree86.Org>
 
33
 *          ... and others
 
34
 *
 
35
 * This file includes the helper functions that the server provides for
 
36
 * different drivers.
 
37
 */
 
38
 
 
39
#ifdef HAVE_XORG_CONFIG_H
 
40
#include <xorg-config.h>
 
41
#endif
 
42
 
 
43
#include <X11/X.h>
 
44
#include "os.h"
 
45
#include "servermd.h"
 
46
#include "pixmapstr.h"
 
47
#include "windowstr.h"
 
48
#include "propertyst.h"
 
49
#include "gcstruct.h"
 
50
#include "loaderProcs.h"
 
51
#include "xf86.h"
 
52
#include "xf86Priv.h"
 
53
#include "xf86_OSlib.h"
 
54
#include "micmap.h"
 
55
#include "xf86PciInfo.h"
 
56
#include "xf86DDC.h"
 
57
#include "xf86Xinput.h"
 
58
#include "xf86InPriv.h"
 
59
#include "mivalidate.h"
 
60
#include "xf86RAC.h"
 
61
#include "xf86Bus.h"
 
62
#include "xf86Version.h"
 
63
 
 
64
/* For xf86GetClocks */
 
65
#if defined(CSRG_BASED) || defined(__GNU__)
 
66
#define HAS_SETPRIORITY
 
67
#include <sys/resource.h>
 
68
#endif
 
69
 
 
70
static int xf86ScrnInfoPrivateCount = 0;
 
71
 
 
72
 
 
73
#ifdef XFree86LOADER
 
74
/* Add a pointer to a new DriverRec to xf86DriverList */
 
75
 
 
76
void
 
77
xf86AddDriver(DriverPtr driver, pointer module, int flags)
 
78
{
 
79
    /* Don't add null entries */
 
80
    if (!driver)
 
81
        return;
 
82
 
 
83
    if (xf86DriverList == NULL)
 
84
        xf86NumDrivers = 0;
 
85
 
 
86
    xf86NumDrivers++;
 
87
    xf86DriverList = xnfrealloc(xf86DriverList,
 
88
                                xf86NumDrivers * sizeof(DriverPtr));
 
89
    xf86DriverList[xf86NumDrivers - 1] = xnfalloc(sizeof(DriverRec));
 
90
    if (flags & HaveDriverFuncs)
 
91
        *xf86DriverList[xf86NumDrivers - 1] = *driver;
 
92
    else {
 
93
        memcpy(xf86DriverList[xf86NumDrivers - 1], driver, sizeof(DriverRec1));
 
94
        xf86DriverList[xf86NumDrivers - 1]->driverFunc = NULL;
 
95
    }
 
96
    xf86DriverList[xf86NumDrivers - 1]->module = module;
 
97
    xf86DriverList[xf86NumDrivers - 1]->refCount = 0;
 
98
}
 
99
 
 
100
void
 
101
xf86DeleteDriver(int drvIndex)
 
102
{
 
103
    if (xf86DriverList[drvIndex]
 
104
        && (!xf86DriverHasEntities(xf86DriverList[drvIndex]))) {
 
105
        if (xf86DriverList[drvIndex]->module)
 
106
            UnloadModule(xf86DriverList[drvIndex]->module);
 
107
        xfree(xf86DriverList[drvIndex]);
 
108
        xf86DriverList[drvIndex] = NULL;
 
109
    }
 
110
}
 
111
 
 
112
/* Add a pointer to a new InputDriverRec to xf86InputDriverList */
 
113
 
 
114
void
 
115
xf86AddInputDriver(InputDriverPtr driver, pointer module, int flags)
 
116
{
 
117
    /* Don't add null entries */
 
118
    if (!driver)
 
119
        return;
 
120
 
 
121
    if (xf86InputDriverList == NULL)
 
122
        xf86NumInputDrivers = 0;
 
123
 
 
124
    xf86NumInputDrivers++;
 
125
    xf86InputDriverList = xnfrealloc(xf86InputDriverList,
 
126
                                xf86NumInputDrivers * sizeof(InputDriverPtr));
 
127
    xf86InputDriverList[xf86NumInputDrivers - 1] =
 
128
                                xnfalloc(sizeof(InputDriverRec));
 
129
    *xf86InputDriverList[xf86NumInputDrivers - 1] = *driver;
 
130
    xf86InputDriverList[xf86NumInputDrivers - 1]->module = module;
 
131
    xf86InputDriverList[xf86NumInputDrivers - 1]->refCount = 0;
 
132
}
 
133
 
 
134
void
 
135
xf86DeleteInputDriver(int drvIndex)
 
136
{
 
137
    if (xf86InputDriverList[drvIndex] && xf86InputDriverList[drvIndex]->module)
 
138
        UnloadModule(xf86InputDriverList[drvIndex]->module);
 
139
    xfree(xf86InputDriverList[drvIndex]);
 
140
    xf86InputDriverList[drvIndex] = NULL;
 
141
}
 
142
 
 
143
void
 
144
xf86AddModuleInfo(ModuleInfoPtr info, pointer module)
 
145
{
 
146
    /* Don't add null entries */
 
147
    if (!module)
 
148
        return;
 
149
 
 
150
    if (xf86ModuleInfoList == NULL)
 
151
        xf86NumModuleInfos = 0;
 
152
 
 
153
    xf86NumModuleInfos++;
 
154
    xf86ModuleInfoList = xnfrealloc(xf86ModuleInfoList,
 
155
                                    xf86NumModuleInfos * sizeof(ModuleInfoPtr));
 
156
    xf86ModuleInfoList[xf86NumModuleInfos - 1] = xnfalloc(sizeof(ModuleInfoRec));
 
157
    *xf86ModuleInfoList[xf86NumModuleInfos - 1] = *info;
 
158
    xf86ModuleInfoList[xf86NumModuleInfos - 1]->module = module;
 
159
    xf86ModuleInfoList[xf86NumModuleInfos - 1]->refCount = 0;
 
160
}
 
161
 
 
162
void
 
163
xf86DeleteModuleInfo(int idx)
 
164
{
 
165
    if (xf86ModuleInfoList[idx]) {
 
166
        if (xf86ModuleInfoList[idx]->module)
 
167
            UnloadModule(xf86ModuleInfoList[idx]->module);
 
168
        xfree(xf86ModuleInfoList[idx]);
 
169
        xf86ModuleInfoList[idx] = NULL;
 
170
    }
 
171
}
 
172
#endif
 
173
 
 
174
 
 
175
/* Allocate a new ScrnInfoRec in xf86Screens */
 
176
 
 
177
ScrnInfoPtr
 
178
xf86AllocateScreen(DriverPtr drv, int flags)
 
179
{
 
180
    int i;
 
181
 
 
182
    if (xf86Screens == NULL)
 
183
        xf86NumScreens = 0;
 
184
 
 
185
    i = xf86NumScreens++;
 
186
    xf86Screens = xnfrealloc(xf86Screens, xf86NumScreens * sizeof(ScrnInfoPtr));
 
187
    xf86Screens[i] = xnfcalloc(sizeof(ScrnInfoRec), 1);
 
188
    xf86Screens[i]->scrnIndex = i;      /* Changes when a screen is removed */
 
189
    xf86Screens[i]->origIndex = i;      /* This never changes */
 
190
    xf86Screens[i]->privates = xnfcalloc(sizeof(DevUnion),
 
191
                                         xf86ScrnInfoPrivateCount);
 
192
    /*
 
193
     * EnableDisableFBAccess now gets initialized in InitOutput()
 
194
     * xf86Screens[i]->EnableDisableFBAccess = xf86EnableDisableFBAccess;
 
195
     */
 
196
 
 
197
    xf86Screens[i]->drv = drv;
 
198
    drv->refCount++;
 
199
#ifdef XFree86LOADER
 
200
    xf86Screens[i]->module = DuplicateModule(drv->module, NULL);
 
201
#else
 
202
    xf86Screens[i]->module = NULL;
 
203
#endif
 
204
    /*
 
205
     * set the initial access state. This will be modified after PreInit.
 
206
     * XXX Or should we do it some other place?
 
207
     */
 
208
    xf86Screens[i]->CurrentAccess = &xf86CurrentAccess;
 
209
    xf86Screens[i]->resourceType = MEM_IO;
 
210
 
 
211
#ifdef DEBUG
 
212
    /* OOps -- What's this ? */
 
213
    ErrorF("xf86AllocateScreen - xf86Screens[%d]->pScreen = %p\n",
 
214
           i, xf86Screens[i]->pScreen );
 
215
    if ( NULL != xf86Screens[i]->pScreen ) {
 
216
      ErrorF("xf86Screens[%d]->pScreen->CreateWindow = %p\n",
 
217
             i, xf86Screens[i]->pScreen->CreateWindow );
 
218
    }
 
219
#endif
 
220
 
 
221
    xf86Screens[i]->DriverFunc = drv->driverFunc;
 
222
    
 
223
    return xf86Screens[i];
 
224
}
 
225
 
 
226
 
 
227
/*
 
228
 * Remove an entry from xf86Screens.  Ideally it should free all allocated
 
229
 * data.  To do this properly may require a driver hook.
 
230
 */
 
231
 
 
232
void
 
233
xf86DeleteScreen(int scrnIndex, int flags)
 
234
{
 
235
    ScrnInfoPtr pScrn;
 
236
    int i;
 
237
 
 
238
    /* First check if the screen is valid */
 
239
    if (xf86NumScreens == 0 || xf86Screens == NULL)
 
240
        return;
 
241
 
 
242
    if (scrnIndex > xf86NumScreens - 1)
 
243
        return;
 
244
 
 
245
    if (!(pScrn = xf86Screens[scrnIndex]))
 
246
        return;
 
247
 
 
248
    /* If a FreeScreen function is defined, call it here */
 
249
    if (pScrn->FreeScreen != NULL)
 
250
        pScrn->FreeScreen(scrnIndex, 0);
 
251
 
 
252
    while (pScrn->modes)
 
253
        xf86DeleteMode(&pScrn->modes, pScrn->modes);
 
254
 
 
255
    while (pScrn->modePool)
 
256
        xf86DeleteMode(&pScrn->modePool, pScrn->modePool);
 
257
 
 
258
    xf86OptionListFree(pScrn->options);
 
259
 
 
260
#ifdef XFree86LOADER
 
261
    if (pScrn->module)
 
262
        UnloadModule(pScrn->module);
 
263
#endif
 
264
 
 
265
    if (pScrn->drv)
 
266
        pScrn->drv->refCount--;
 
267
 
 
268
    if (pScrn->privates)
 
269
        xfree(pScrn->privates);
 
270
 
 
271
    xf86ClearEntityListForScreen(scrnIndex);
 
272
 
 
273
    xfree(pScrn);
 
274
 
 
275
    /* Move the other entries down, updating their scrnIndex fields */
 
276
    
 
277
    xf86NumScreens--;
 
278
 
 
279
    for (i = scrnIndex; i < xf86NumScreens; i++) {
 
280
        xf86Screens[i] = xf86Screens[i + 1];
 
281
        xf86Screens[i]->scrnIndex = i;
 
282
        /* Also need to take care of the screen layout settings */
 
283
    }
 
284
}
 
285
 
 
286
/*
 
287
 * Allocate a private in ScrnInfoRec.
 
288
 */
 
289
 
 
290
int
 
291
xf86AllocateScrnInfoPrivateIndex(void)
 
292
{
 
293
    int idx, i;
 
294
    ScrnInfoPtr pScr;
 
295
    DevUnion *nprivs;
 
296
 
 
297
    idx = xf86ScrnInfoPrivateCount++;
 
298
    for (i = 0; i < xf86NumScreens; i++) {
 
299
        pScr = xf86Screens[i];
 
300
        nprivs = xnfrealloc(pScr->privates,
 
301
                            xf86ScrnInfoPrivateCount * sizeof(DevUnion));
 
302
        /* Zero the new private */
 
303
        bzero(&nprivs[idx], sizeof(DevUnion));
 
304
        pScr->privates = nprivs;
 
305
    }
 
306
    return idx;
 
307
}
 
308
 
 
309
/* Allocate a new InputInfoRec and add it to the head xf86InputDevs. */
 
310
 
 
311
InputInfoPtr
 
312
xf86AllocateInput(InputDriverPtr drv, int flags)
 
313
{
 
314
    InputInfoPtr new;
 
315
 
 
316
    if (!(new = xcalloc(sizeof(InputInfoRec), 1)))
 
317
        return NULL;
 
318
 
 
319
    new->drv = drv;
 
320
    drv->refCount++;
 
321
#ifdef XFree86LOADER
 
322
    new->module = DuplicateModule(drv->module, NULL);
 
323
#else
 
324
    new->module = NULL;
 
325
#endif
 
326
    new->next = xf86InputDevs;
 
327
    xf86InputDevs = new;
 
328
    return new;
 
329
}
 
330
 
 
331
 
 
332
/*
 
333
 * Remove an entry from xf86InputDevs.  Ideally it should free all allocated
 
334
 * data.  To do this properly may require a driver hook.
 
335
 */
 
336
 
 
337
void
 
338
xf86DeleteInput(InputInfoPtr pInp, int flags)
 
339
{
 
340
    InputInfoPtr p;
 
341
 
 
342
    /* First check if the inputdev is valid. */
 
343
    if (pInp == NULL)
 
344
        return;
 
345
 
 
346
#if 0
 
347
    /* If a free function is defined, call it here. */
 
348
    if (pInp->free)
 
349
        pInp->free(pInp, 0);
 
350
#endif
 
351
 
 
352
#ifdef XFree86LOADER
 
353
    if (pInp->module)
 
354
        UnloadModule(pInp->module);
 
355
#endif
 
356
 
 
357
    if (pInp->drv)
 
358
        pInp->drv->refCount--;
 
359
 
 
360
    if (pInp->private)
 
361
        xfree(pInp->private);
 
362
 
 
363
    /* Remove the entry from the list. */
 
364
    if (pInp == xf86InputDevs)
 
365
        xf86InputDevs = pInp->next;
 
366
    else {
 
367
        p = xf86InputDevs;
 
368
        while (p && p->next != pInp)
 
369
            p = p->next;
 
370
        if (p)
 
371
            p->next = pInp->next;
 
372
        /* Else the entry wasn't in the xf86InputDevs list (ignore this). */
 
373
    }
 
374
    xfree(pInp);
 
375
}
 
376
 
 
377
Bool
 
378
xf86AddPixFormat(ScrnInfoPtr pScrn, int depth, int bpp, int pad)
 
379
{
 
380
    int i;
 
381
 
 
382
    if (pScrn->numFormats >= MAXFORMATS)
 
383
        return FALSE;
 
384
 
 
385
    if (bpp <= 0) {
 
386
        if (depth == 1)
 
387
            bpp = 1;
 
388
        else if (depth <= 8)
 
389
            bpp = 8;
 
390
        else if (depth <= 16)
 
391
            bpp = 16;
 
392
        else if (depth <= 32)
 
393
            bpp = 32;
 
394
        else
 
395
            return FALSE;
 
396
    }
 
397
    if (pad <= 0)
 
398
        pad = BITMAP_SCANLINE_PAD;
 
399
 
 
400
    i = pScrn->numFormats++;
 
401
    pScrn->formats[i].depth = depth;
 
402
    pScrn->formats[i].bitsPerPixel = bpp;
 
403
    pScrn->formats[i].scanlinePad = pad;
 
404
    return TRUE;
 
405
}
 
406
 
 
407
/*
 
408
 * Set the depth we are using based on (in the following order of preference):
 
409
 *  - values given on the command line
 
410
 *  - values given in the config file
 
411
 *  - values provided by the driver
 
412
 *  - an overall default when nothing else is given
 
413
 *
 
414
 * Also find a Display subsection matching the depth/bpp found.
 
415
 *
 
416
 * Sets the following ScrnInfoRec fields:
 
417
 *     bitsPerPixel, pixmap24, depth, display, imageByteOrder,
 
418
 *     bitmapScanlinePad, bitmapScanlineUnit, bitmapBitOrder, numFormats,
 
419
 *     formats, fbFormat.
 
420
 */
 
421
 
 
422
/* Can the screen handle 24 bpp pixmaps */
 
423
#define DO_PIX24(f) ((f & Support24bppFb) || \
 
424
                     ((f & Support32bppFb) && (f & SupportConvert24to32)))
 
425
 
 
426
/* Can the screen handle 32 bpp pixmaps */
 
427
#define DO_PIX32(f) ((f & Support32bppFb) || \
 
428
                     ((f & Support24bppFb) && (f & SupportConvert32to24)))
 
429
 
 
430
/* Does the screen prefer 32bpp fb for 24bpp pixmaps */
 
431
#define CHOOSE32FOR24(f) ((f & Support32bppFb) && (f & SupportConvert24to32) \
 
432
                          && (f & PreferConvert24to32))
 
433
 
 
434
/* Does the screen prefer 24bpp fb for 32bpp pixmaps */
 
435
#define CHOOSE24FOR32(f) ((f & Support24bppFb) && (f & SupportConvert32to24) \
 
436
                          && (f & PreferConvert32to24))
 
437
 
 
438
/* Can the screen handle 32bpp pixmaps for 24bpp fb */
 
439
#define DO_PIX32FOR24(f) ((f & Support24bppFb) && (f & SupportConvert32to24))
 
440
 
 
441
/* Can the screen handle 24bpp pixmaps for 32bpp fb */
 
442
#define DO_PIX24FOR32(f) ((f & Support32bppFb) && (f & SupportConvert24to32))
 
443
 
 
444
#ifndef GLOBAL_DEFAULT_DEPTH
 
445
#define GLOBAL_DEFAULT_DEPTH 16
 
446
#endif
 
447
 
 
448
#ifndef GLOBAL_DEFAULT_FBBPP
 
449
#define GLOBAL_DEFAULT_FBBPP 16
 
450
#endif
 
451
 
 
452
Bool
 
453
xf86SetDepthBpp(ScrnInfoPtr scrp, int depth, int dummy, int fbbpp,
 
454
                int depth24flags)
 
455
{
 
456
    int i;
 
457
    DispPtr disp;
 
458
    Pix24Flags pix24 = xf86Info.pixmap24;
 
459
    Bool nomatch = FALSE;
 
460
 
 
461
    scrp->bitsPerPixel = -1;
 
462
    scrp->depth = -1;
 
463
    scrp->pixmap24 = Pix24DontCare;
 
464
    scrp->bitsPerPixelFrom = X_DEFAULT;
 
465
    scrp->depthFrom = X_DEFAULT;
 
466
 
 
467
#if BITMAP_SCANLINE_UNIT == 64
 
468
    /*
 
469
     * For platforms with 64-bit scanlines, modify the driver's depth24flags
 
470
     * to remove preferences for packed 24bpp modes, which are not currently
 
471
     * supported on these platforms.
 
472
     */
 
473
    depth24flags &= ~(SupportConvert32to24 | SupportConvert32to24 |
 
474
                      PreferConvert24to32 | PreferConvert32to24);
 
475
#endif
 
476
     
 
477
    if (xf86FbBpp > 0) {
 
478
        scrp->bitsPerPixel = xf86FbBpp;
 
479
        scrp->bitsPerPixelFrom = X_CMDLINE;
 
480
    }
 
481
 
 
482
    if (xf86Depth > 0) {
 
483
        scrp->depth = xf86Depth;
 
484
        scrp->depthFrom = X_CMDLINE;
 
485
    }
 
486
 
 
487
    if (xf86FbBpp < 0 && xf86Depth < 0) {
 
488
        if (scrp->confScreen->defaultfbbpp > 0) {
 
489
            scrp->bitsPerPixel = scrp->confScreen->defaultfbbpp;
 
490
            scrp->bitsPerPixelFrom = X_CONFIG;
 
491
        }
 
492
        if (scrp->confScreen->defaultdepth > 0) {
 
493
            scrp->depth = scrp->confScreen->defaultdepth;
 
494
            scrp->depthFrom = X_CONFIG;
 
495
        }
 
496
 
 
497
        if (scrp->confScreen->defaultfbbpp <= 0 &&
 
498
            scrp->confScreen->defaultdepth <= 0) {
 
499
            /*
 
500
             * Check for DefaultDepth and DefaultFbBpp options in the
 
501
             * Device sections.
 
502
             */
 
503
            int i;
 
504
            GDevPtr device;
 
505
            Bool found = FALSE;
 
506
 
 
507
            for (i = 0; i < scrp->numEntities; i++) {
 
508
                device = xf86GetDevFromEntity(scrp->entityList[i],
 
509
                                              scrp->entityInstanceList[i]);
 
510
                if (device && device->options) {
 
511
                    if (xf86FindOption(device->options, "DefaultDepth")) {
 
512
                        scrp->depth = xf86SetIntOption(device->options,
 
513
                                                       "DefaultDepth", -1);
 
514
                        scrp->depthFrom = X_CONFIG;
 
515
                        found = TRUE;
 
516
                    }
 
517
                    if (xf86FindOption(device->options, "DefaultFbBpp")) {
 
518
                        scrp->bitsPerPixel = xf86SetIntOption(device->options,
 
519
                                                              "DefaultFbBpp",
 
520
                                                              -1);
 
521
                        scrp->bitsPerPixelFrom = X_CONFIG;
 
522
                        found = TRUE;
 
523
                    }
 
524
                }
 
525
                if (found)
 
526
                    break;
 
527
            }
 
528
        }
 
529
    }
 
530
 
 
531
    /* If none of these is set, pick a default */
 
532
    if (scrp->bitsPerPixel < 0 && scrp->depth < 0) {
 
533
        if (fbbpp > 0 || depth > 0) {
 
534
            if (fbbpp > 0)
 
535
                scrp->bitsPerPixel = fbbpp;
 
536
            if (depth > 0)
 
537
                scrp->depth = depth;
 
538
        } else {
 
539
            scrp->bitsPerPixel = GLOBAL_DEFAULT_FBBPP;
 
540
            scrp->depth = GLOBAL_DEFAULT_DEPTH;
 
541
        }
 
542
    }
 
543
 
 
544
    /* If any are not given, determine a default for the others */
 
545
 
 
546
    if (scrp->bitsPerPixel < 0) {
 
547
        /* The depth must be set */
 
548
        if (scrp->depth > -1) {
 
549
            if (scrp->depth == 1)
 
550
                scrp->bitsPerPixel = 1;
 
551
            else if (scrp->depth <= 4)
 
552
                scrp->bitsPerPixel = 4;
 
553
            else if (scrp->depth <= 8)
 
554
                scrp->bitsPerPixel = 8;
 
555
            else if (scrp->depth <= 16)
 
556
                scrp->bitsPerPixel = 16;
 
557
            else if (scrp->depth <= 24) {
 
558
                /*
 
559
                 * Figure out if a choice is possible based on the depth24
 
560
                 * and pix24 flags.
 
561
                 */
 
562
                /* Check pix24 first */
 
563
                if (pix24 != Pix24DontCare) {
 
564
                    if (pix24 == Pix24Use32) {
 
565
                        if (DO_PIX32(depth24flags)) {
 
566
                            if (CHOOSE24FOR32(depth24flags))
 
567
                                scrp->bitsPerPixel = 24;
 
568
                            else
 
569
                                scrp->bitsPerPixel = 32;
 
570
                        } else {
 
571
                            nomatch = TRUE;
 
572
                        }
 
573
                    } else if (pix24 == Pix24Use24) {
 
574
                        if (DO_PIX24(depth24flags)) {
 
575
                            if (CHOOSE32FOR24(depth24flags))
 
576
                                scrp->bitsPerPixel = 32;
 
577
                            else
 
578
                                scrp->bitsPerPixel = 24;
 
579
                        } else {
 
580
                            nomatch = TRUE;
 
581
                        }
 
582
                    }
 
583
                } else {
 
584
                    if (DO_PIX32(depth24flags)) {
 
585
                        if (CHOOSE24FOR32(depth24flags))
 
586
                            scrp->bitsPerPixel = 24;
 
587
                        else
 
588
                            scrp->bitsPerPixel = 32;
 
589
                    } else if (DO_PIX24(depth24flags)) {
 
590
                        if (CHOOSE32FOR24(depth24flags))
 
591
                            scrp->bitsPerPixel = 32;
 
592
                        else
 
593
                            scrp->bitsPerPixel = 24;
 
594
                    }
 
595
                }
 
596
            } else if (scrp->depth <= 32)
 
597
                scrp->bitsPerPixel = 32;
 
598
            else {
 
599
                xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
600
                           "Specified depth (%d) is greater than 32\n",
 
601
                           scrp->depth);
 
602
                return FALSE;
 
603
            }
 
604
        } else {
 
605
            xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
606
                        "xf86SetDepthBpp: internal error: depth and fbbpp"
 
607
                        " are both not set\n");
 
608
            return FALSE;
 
609
        }
 
610
        if (scrp->bitsPerPixel < 0) {
 
611
            if (nomatch)
 
612
                xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
613
                        "Driver can't support depth 24 pixmap format (%d)\n",
 
614
                        PIX24TOBPP(pix24));
 
615
            else if ((depth24flags & (Support24bppFb | Support32bppFb)) ==
 
616
                     NoDepth24Support)
 
617
                xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
618
                        "Driver can't support depth 24\n");
 
619
            else
 
620
                xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
621
                        "Can't find fbbpp for depth 24\n");
 
622
            return FALSE;
 
623
        }
 
624
        scrp->bitsPerPixelFrom = X_PROBED;
 
625
    }
 
626
 
 
627
    if (scrp->depth <= 0) {
 
628
        /* bitsPerPixel is already set */
 
629
        switch (scrp->bitsPerPixel) {
 
630
        case 32:
 
631
            scrp->depth = 24;
 
632
            break;
 
633
        default:
 
634
            /* 1, 4, 8, 16 and 24 */
 
635
            scrp->depth = scrp->bitsPerPixel;
 
636
            break;
 
637
        }
 
638
        scrp->depthFrom = X_PROBED;
 
639
    }
 
640
 
 
641
    /* Sanity checks */
 
642
    if (scrp->depth < 1 || scrp->depth > 32) {
 
643
        xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
644
                   "Specified depth (%d) is not in the range 1-32\n",
 
645
                    scrp->depth);
 
646
        return FALSE;
 
647
    }
 
648
    switch (scrp->bitsPerPixel) {
 
649
    case 1:
 
650
    case 4:
 
651
    case 8:
 
652
    case 16:
 
653
    case 24:
 
654
    case 32:
 
655
        break;
 
656
    default:
 
657
        xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
658
                   "Specified fbbpp (%d) is not a permitted value\n",
 
659
                   scrp->bitsPerPixel);
 
660
        return FALSE;
 
661
    }
 
662
    if (scrp->depth > scrp->bitsPerPixel) {
 
663
        xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
664
                   "Specified depth (%d) is greater than the fbbpp (%d)\n",
 
665
                   scrp->depth, scrp->bitsPerPixel);
 
666
        return FALSE;
 
667
    }
 
668
 
 
669
    /* set scrp->pixmap24 if the driver isn't flexible */
 
670
    if (scrp->bitsPerPixel == 24 && !DO_PIX32FOR24(depth24flags)) {
 
671
        scrp->pixmap24 = Pix24Use24;
 
672
    }
 
673
    if (scrp->bitsPerPixel == 32 && !DO_PIX24FOR32(depth24flags)) {
 
674
        scrp->pixmap24 = Pix24Use32;
 
675
    }
 
676
 
 
677
    /*
 
678
     * Find the Display subsection matching the depth/fbbpp and initialise
 
679
     * scrp->display with it.
 
680
     */
 
681
    for (i = 0, disp = scrp->confScreen->displays;
 
682
         i < scrp->confScreen->numdisplays; i++, disp++) {
 
683
        if ((disp->depth == scrp->depth && disp->fbbpp == scrp->bitsPerPixel)
 
684
            || (disp->depth == scrp->depth && disp->fbbpp <= 0)
 
685
            || (disp->fbbpp == scrp->bitsPerPixel && disp->depth <= 0)) {
 
686
            scrp->display = disp;
 
687
            break;
 
688
        }
 
689
    }
 
690
 
 
691
    /*
 
692
     * If an exact match can't be found, see if there is one with no
 
693
     * depth or fbbpp specified.
 
694
     */
 
695
    if (i == scrp->confScreen->numdisplays) {
 
696
        for (i = 0, disp = scrp->confScreen->displays;
 
697
             i < scrp->confScreen->numdisplays; i++, disp++) {
 
698
            if (disp->depth <= 0 && disp->fbbpp <= 0) {
 
699
                scrp->display = disp;
 
700
                break;
 
701
            }
 
702
        }
 
703
    }
 
704
 
 
705
    /*
 
706
     * If all else fails, create a default one.
 
707
     */
 
708
    if (i == scrp->confScreen->numdisplays) {
 
709
        scrp->confScreen->numdisplays++;
 
710
        scrp->confScreen->displays =
 
711
                xnfrealloc(scrp->confScreen->displays,
 
712
                           scrp->confScreen->numdisplays * sizeof(DispRec));
 
713
        xf86DrvMsg(scrp->scrnIndex, X_INFO,
 
714
                   "Creating default Display subsection in Screen section\n"
 
715
                   "\t\"%s\" for depth/fbbpp %d/%d\n",
 
716
                   scrp->confScreen->id, scrp->depth, scrp->bitsPerPixel);
 
717
        memset(&scrp->confScreen->displays[i], 0, sizeof(DispRec));
 
718
        scrp->confScreen->displays[i].blackColour.red = -1;
 
719
        scrp->confScreen->displays[i].blackColour.green = -1;
 
720
        scrp->confScreen->displays[i].blackColour.blue = -1;
 
721
        scrp->confScreen->displays[i].whiteColour.red = -1;
 
722
        scrp->confScreen->displays[i].whiteColour.green = -1;
 
723
        scrp->confScreen->displays[i].whiteColour.blue = -1;
 
724
        scrp->confScreen->displays[i].defaultVisual = -1;
 
725
        scrp->confScreen->displays[i].modes = xnfalloc(sizeof(char *));
 
726
        scrp->confScreen->displays[i].modes[0] = NULL;
 
727
        scrp->confScreen->displays[i].depth = depth;
 
728
        scrp->confScreen->displays[i].fbbpp = fbbpp;
 
729
        scrp->display = &scrp->confScreen->displays[i];
 
730
    }
 
731
 
 
732
    /*
 
733
     * Setup defaults for the display-wide attributes the framebuffer will
 
734
     * need.  These defaults should eventually be set globally, and not
 
735
     * dependent on the screens.
 
736
     */
 
737
    scrp->imageByteOrder = IMAGE_BYTE_ORDER;
 
738
    scrp->bitmapScanlinePad = BITMAP_SCANLINE_PAD;
 
739
    if (scrp->depth < 8) {
 
740
        /* Planar modes need these settings */
 
741
        scrp->bitmapScanlineUnit = 8;
 
742
        scrp->bitmapBitOrder = MSBFirst;
 
743
    } else {
 
744
        scrp->bitmapScanlineUnit = BITMAP_SCANLINE_UNIT;
 
745
        scrp->bitmapBitOrder = BITMAP_BIT_ORDER;
 
746
    }
 
747
 
 
748
    /*
 
749
     * If an unusual depth is required, add it to scrp->formats.  The formats
 
750
     * for the common depths are handled globally in InitOutput
 
751
     */
 
752
    switch (scrp->depth) {
 
753
    case 1:
 
754
    case 4:
 
755
    case 8:
 
756
    case 15:
 
757
    case 16:
 
758
    case 24:
 
759
        /* Common depths.  Nothing to do for them */
 
760
        break;
 
761
    default:
 
762
        if (!xf86AddPixFormat(scrp, scrp->depth, 0, 0)) {
 
763
            xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
764
                       "Can't add pixmap format for depth %d\n", scrp->depth);
 
765
            return FALSE;
 
766
        }
 
767
    }
 
768
 
 
769
    /* Initialise the framebuffer format for this screen */
 
770
    scrp->fbFormat.depth = scrp->depth;
 
771
    scrp->fbFormat.bitsPerPixel = scrp->bitsPerPixel;
 
772
    scrp->fbFormat.scanlinePad = BITMAP_SCANLINE_PAD;
 
773
 
 
774
    return TRUE;
 
775
}
 
776
 
 
777
/*
 
778
 * Print out the selected depth and bpp.
 
779
 */
 
780
void
 
781
xf86PrintDepthBpp(ScrnInfoPtr scrp)
 
782
{
 
783
    xf86DrvMsg(scrp->scrnIndex, scrp->depthFrom, "Depth %d, ", scrp->depth);
 
784
    xf86Msg(scrp->bitsPerPixelFrom, "framebuffer bpp %d\n", scrp->bitsPerPixel);
 
785
}
 
786
 
 
787
/*
 
788
 * xf86SetWeight sets scrp->weight, scrp->mask, scrp->offset, and for depths
 
789
 * greater than MAX_PSEUDO_DEPTH also scrp->rgbBits.
 
790
 */
 
791
Bool
 
792
xf86SetWeight(ScrnInfoPtr scrp, rgb weight, rgb mask)
 
793
{
 
794
    MessageType weightFrom = X_DEFAULT;
 
795
 
 
796
    scrp->weight.red = 0;
 
797
    scrp->weight.green = 0;
 
798
    scrp->weight.blue = 0;
 
799
 
 
800
    if (xf86Weight.red > 0 && xf86Weight.green > 0 && xf86Weight.blue > 0) {
 
801
        scrp->weight = xf86Weight;
 
802
        weightFrom = X_CMDLINE;
 
803
    } else if (scrp->display->weight.red > 0 && scrp->display->weight.green > 0
 
804
               && scrp->display->weight.blue > 0) {
 
805
        scrp->weight = scrp->display->weight;
 
806
        weightFrom = X_CONFIG;
 
807
    } else if (weight.red > 0 && weight.green > 0 && weight.blue > 0) {
 
808
        scrp->weight = weight;
 
809
    } else {
 
810
        switch (scrp->depth) {
 
811
        case 1:
 
812
        case 4:
 
813
        case 8:
 
814
            scrp->weight.red = scrp->weight.green =
 
815
                scrp->weight.blue = scrp->rgbBits;
 
816
            break;
 
817
        case 15:
 
818
            scrp->weight.red = scrp->weight.green = scrp->weight.blue = 5;
 
819
            break;
 
820
        case 16:
 
821
            scrp->weight.red = scrp->weight.blue = 5;
 
822
            scrp->weight.green = 6;
 
823
            break;
 
824
        case 24:
 
825
            scrp->weight.red = scrp->weight.green = scrp->weight.blue = 8;
 
826
            break;
 
827
        case 30:
 
828
            scrp->weight.red = scrp->weight.green = scrp->weight.blue = 10;
 
829
            break;
 
830
        }
 
831
    }
 
832
 
 
833
    if (scrp->weight.red)
 
834
        xf86DrvMsg(scrp->scrnIndex, weightFrom, "RGB weight %d%d%d\n",
 
835
                   (int)scrp->weight.red, (int)scrp->weight.green,
 
836
                   (int)scrp->weight.blue);
 
837
 
 
838
    if (scrp->depth > MAX_PSEUDO_DEPTH &&
 
839
        (scrp->depth != scrp->weight.red + scrp->weight.green +
 
840
                        scrp->weight.blue)) {
 
841
        xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
842
                   "Weight given (%d%d%d) is inconsistent with the "
 
843
                   "depth (%d)\n",
 
844
                   (int)scrp->weight.red, (int)scrp->weight.green,
 
845
                   (int)scrp->weight.blue, scrp->depth);
 
846
        return FALSE;
 
847
    }
 
848
    if (scrp->depth > MAX_PSEUDO_DEPTH && scrp->weight.red) {
 
849
        /*
 
850
         * XXX Does this even mean anything for TrueColor visuals?
 
851
         * If not, we shouldn't even be setting it here.  However, this
 
852
         * matches the behaviour of 3.x versions of XFree86.
 
853
         */
 
854
        scrp->rgbBits = scrp->weight.red;
 
855
        if (scrp->weight.green > scrp->rgbBits)
 
856
            scrp->rgbBits = scrp->weight.green;
 
857
        if (scrp->weight.blue > scrp->rgbBits)
 
858
            scrp->rgbBits = scrp->weight.blue;
 
859
    }
 
860
 
 
861
    /* Set the mask and offsets */
 
862
    if (mask.red == 0 || mask.green == 0 || mask.blue == 0) {
 
863
        /* Default to a setting common to PC hardware */
 
864
        scrp->offset.red = scrp->weight.green + scrp->weight.blue;
 
865
        scrp->offset.green = scrp->weight.blue;
 
866
        scrp->offset.blue = 0;
 
867
        scrp->mask.red = ((1 << scrp->weight.red) - 1) << scrp->offset.red;
 
868
        scrp->mask.green = ((1 << scrp->weight.green) - 1)
 
869
                                << scrp->offset.green;
 
870
        scrp->mask.blue = (1 << scrp->weight.blue) - 1;
 
871
    } else {
 
872
        /* Initialise to the values passed */
 
873
        scrp->mask.red = mask.red;
 
874
        scrp->mask.green = mask.green;
 
875
        scrp->mask.blue = mask.blue;
 
876
        scrp->offset.red = ffs(mask.red);
 
877
        scrp->offset.green = ffs(mask.green);
 
878
        scrp->offset.blue = ffs(mask.blue);
 
879
    }
 
880
    return TRUE;
 
881
}
 
882
 
 
883
Bool
 
884
xf86SetDefaultVisual(ScrnInfoPtr scrp, int visual)
 
885
{
 
886
    MessageType visualFrom = X_DEFAULT;
 
887
 
 
888
    if (defaultColorVisualClass >= 0) {
 
889
        scrp->defaultVisual = defaultColorVisualClass;
 
890
        visualFrom = X_CMDLINE;
 
891
    } else if (scrp->display->defaultVisual >= 0) {
 
892
        scrp->defaultVisual = scrp->display->defaultVisual;
 
893
        visualFrom = X_CONFIG;
 
894
    } else if (visual >= 0) {
 
895
        scrp->defaultVisual = visual;
 
896
    } else {
 
897
        if (scrp->depth == 1)
 
898
            scrp->defaultVisual = StaticGray;
 
899
        else if (scrp->depth == 4)
 
900
            scrp->defaultVisual = StaticColor;
 
901
        else if (scrp->depth <= MAX_PSEUDO_DEPTH)
 
902
            scrp->defaultVisual = PseudoColor;
 
903
        else
 
904
            scrp->defaultVisual = TrueColor;
 
905
    }
 
906
    switch (scrp->defaultVisual) {
 
907
    case StaticGray:
 
908
    case GrayScale:
 
909
    case StaticColor:
 
910
    case PseudoColor:
 
911
    case TrueColor:
 
912
    case DirectColor:
 
913
        xf86DrvMsg(scrp->scrnIndex, visualFrom, "Default visual is %s\n",
 
914
                   xf86VisualNames[scrp->defaultVisual]);
 
915
            return TRUE;
 
916
    default:
 
917
 
 
918
        xf86DrvMsg(scrp->scrnIndex, X_ERROR,
 
919
                   "Invalid default visual class (%d)\n", scrp->defaultVisual);
 
920
        return FALSE;
 
921
    }
 
922
}
 
923
 
 
924
#define TEST_GAMMA(g) \
 
925
        (g).red > GAMMA_ZERO || (g).green > GAMMA_ZERO || (g).blue > GAMMA_ZERO
 
926
 
 
927
#define SET_GAMMA(g) \
 
928
        (g) > GAMMA_ZERO ? (g) : 1.0
 
929
 
 
930
Bool
 
931
xf86SetGamma(ScrnInfoPtr scrp, Gamma gamma)
 
932
{
 
933
    MessageType from = X_DEFAULT;
 
934
#if 0
 
935
    xf86MonPtr DDC = (xf86MonPtr)(scrp->monitor->DDC); 
 
936
#endif
 
937
    if (TEST_GAMMA(xf86Gamma)) {
 
938
        from = X_CMDLINE;
 
939
        scrp->gamma.red = SET_GAMMA(xf86Gamma.red);
 
940
        scrp->gamma.green = SET_GAMMA(xf86Gamma.green);
 
941
        scrp->gamma.blue = SET_GAMMA(xf86Gamma.blue);
 
942
    } else if (TEST_GAMMA(scrp->monitor->gamma)) {
 
943
        from = X_CONFIG;
 
944
        scrp->gamma.red = SET_GAMMA(scrp->monitor->gamma.red);
 
945
        scrp->gamma.green = SET_GAMMA(scrp->monitor->gamma.green);
 
946
        scrp->gamma.blue = SET_GAMMA(scrp->monitor->gamma.blue);
 
947
#if 0
 
948
    } else if ( DDC && DDC->features.gamma > GAMMA_ZERO ) {
 
949
        from = X_PROBED;
 
950
        scrp->gamma.red = SET_GAMMA(DDC->features.gamma);
 
951
        scrp->gamma.green = SET_GAMMA(DDC->features.gamma);
 
952
        scrp->gamma.blue = SET_GAMMA(DDC->features.gamma);
 
953
        /* EDID structure version 2 gives optional seperate red, green & blue gamma values
 
954
         * in bytes 0x57-0x59 */
 
955
#endif
 
956
    } else if (TEST_GAMMA(gamma)) {
 
957
        scrp->gamma.red = SET_GAMMA(gamma.red);
 
958
        scrp->gamma.green = SET_GAMMA(gamma.green);
 
959
        scrp->gamma.blue = SET_GAMMA(gamma.blue);
 
960
    } else {
 
961
        scrp->gamma.red = 1.0;
 
962
        scrp->gamma.green = 1.0;
 
963
        scrp->gamma.blue = 1.0;
 
964
    }
 
965
    xf86DrvMsg(scrp->scrnIndex, from,
 
966
               "Using gamma correction (%.1f, %.1f, %.1f)\n",
 
967
               scrp->gamma.red, scrp->gamma.green, scrp->gamma.blue);
 
968
 
 
969
    return TRUE;
 
970
}
 
971
 
 
972
#undef TEST_GAMMA
 
973
#undef SET_GAMMA
 
974
 
 
975
 
 
976
/*
 
977
 * Set the DPI from the command line option.  XXX should allow it to be
 
978
 * calculated from the widthmm/heightmm values.
 
979
 */
 
980
 
 
981
#undef MMPERINCH
 
982
#define MMPERINCH 25.4
 
983
 
 
984
void
 
985
xf86SetDpi(ScrnInfoPtr pScrn, int x, int y)
 
986
{
 
987
    MessageType from = X_DEFAULT;
 
988
    xf86MonPtr DDC = (xf86MonPtr)(pScrn->monitor->DDC); 
 
989
    int ddcWidthmm, ddcHeightmm;
 
990
    int widthErr, heightErr;
 
991
 
 
992
    /* XXX Maybe there is no need for widthmm/heightmm in ScrnInfoRec */
 
993
    pScrn->widthmm = pScrn->monitor->widthmm;
 
994
    pScrn->heightmm = pScrn->monitor->heightmm;
 
995
 
 
996
    if (DDC && (DDC->features.hsize > 0 && DDC->features.vsize > 0) ) {
 
997
      /* DDC gives display size in mm for individual modes,
 
998
       * but cm for monitor 
 
999
       */
 
1000
      ddcWidthmm = DDC->features.hsize * 10; /* 10mm in 1cm */
 
1001
      ddcHeightmm = DDC->features.vsize * 10; /* 10mm in 1cm */
 
1002
    } else {
 
1003
      ddcWidthmm = ddcHeightmm = 0;
 
1004
    }
 
1005
 
 
1006
    if (monitorResolution > 0) {
 
1007
        pScrn->xDpi = monitorResolution;
 
1008
        pScrn->yDpi = monitorResolution;
 
1009
        from = X_CMDLINE;
 
1010
    } else if (pScrn->widthmm > 0 || pScrn->heightmm > 0) {
 
1011
        from = X_CONFIG;
 
1012
        if (pScrn->widthmm > 0) {
 
1013
           pScrn->xDpi =
 
1014
                (int)((double)pScrn->virtualX * MMPERINCH / pScrn->widthmm);
 
1015
        }
 
1016
        if (pScrn->heightmm > 0) {
 
1017
           pScrn->yDpi =
 
1018
                (int)((double)pScrn->virtualY * MMPERINCH / pScrn->heightmm);
 
1019
        }
 
1020
        if (pScrn->xDpi > 0 && pScrn->yDpi <= 0)
 
1021
            pScrn->yDpi = pScrn->xDpi;
 
1022
        if (pScrn->yDpi > 0 && pScrn->xDpi <= 0)
 
1023
            pScrn->xDpi = pScrn->yDpi;
 
1024
        xf86DrvMsg(pScrn->scrnIndex, from, "Display dimensions: (%d, %d) mm\n",
 
1025
                   pScrn->widthmm, pScrn->heightmm);
 
1026
 
 
1027
        /* Warn if config and probe disagree about display size */
 
1028
        if ( ddcWidthmm && ddcHeightmm ) {
 
1029
          if (pScrn->widthmm > 0) {
 
1030
            widthErr  = abs(ddcWidthmm  - pScrn->widthmm);
 
1031
          } else {
 
1032
            widthErr  = 0;
 
1033
          }
 
1034
          if (pScrn->heightmm > 0) {
 
1035
            heightErr = abs(ddcHeightmm - pScrn->heightmm);
 
1036
          } else {
 
1037
            heightErr = 0;
 
1038
          }
 
1039
          if (widthErr>10 || heightErr>10) {
 
1040
            /* Should include config file name for monitor here */
 
1041
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
 
1042
                       "Probed monitor is %dx%d mm, using Displaysize %dx%d mm\n", 
 
1043
                       ddcWidthmm,ddcHeightmm, pScrn->widthmm,pScrn->heightmm);
 
1044
          }
 
1045
        }
 
1046
    } else if ( ddcWidthmm && ddcHeightmm ) {
 
1047
        from = X_PROBED;
 
1048
        xf86DrvMsg(pScrn->scrnIndex, from, "Display dimensions: (%d, %d) mm\n",
 
1049
                   ddcWidthmm, ddcHeightmm );
 
1050
        pScrn->widthmm = ddcWidthmm;
 
1051
        pScrn->heightmm = ddcHeightmm;
 
1052
        if (pScrn->widthmm > 0) {
 
1053
           pScrn->xDpi =
 
1054
                (int)((double)pScrn->virtualX * MMPERINCH / pScrn->widthmm);
 
1055
        }
 
1056
        if (pScrn->heightmm > 0) {
 
1057
           pScrn->yDpi =
 
1058
                (int)((double)pScrn->virtualY * MMPERINCH / pScrn->heightmm);
 
1059
        }
 
1060
        if (pScrn->xDpi > 0 && pScrn->yDpi <= 0)
 
1061
            pScrn->yDpi = pScrn->xDpi;
 
1062
        if (pScrn->yDpi > 0 && pScrn->xDpi <= 0)
 
1063
            pScrn->xDpi = pScrn->yDpi;
 
1064
    } else {
 
1065
        if (x > 0)
 
1066
            pScrn->xDpi = x;
 
1067
        else
 
1068
            pScrn->xDpi = DEFAULT_DPI;
 
1069
        if (y > 0)
 
1070
            pScrn->yDpi = y;
 
1071
        else
 
1072
            pScrn->yDpi = DEFAULT_DPI;
 
1073
    }
 
1074
    xf86DrvMsg(pScrn->scrnIndex, from, "DPI set to (%d, %d)\n",
 
1075
               pScrn->xDpi, pScrn->yDpi);
 
1076
}
 
1077
 
 
1078
#undef MMPERINCH
 
1079
 
 
1080
 
 
1081
void
 
1082
xf86SetBlackWhitePixels(ScreenPtr pScreen)
 
1083
{
 
1084
    if (xf86FlipPixels) {
 
1085
        pScreen->whitePixel = 0;
 
1086
        pScreen->blackPixel = 1;
 
1087
    } else {
 
1088
        pScreen->whitePixel = 1;
 
1089
        pScreen->blackPixel = 0;
 
1090
    }
 
1091
}
 
1092
 
 
1093
/*
 
1094
 * xf86SetRootClip --
 
1095
 *      Enable or disable rendering to the screen by
 
1096
 *      setting the root clip list and revalidating
 
1097
 *      all of the windows
 
1098
 */
 
1099
 
 
1100
static void
 
1101
xf86SetRootClip (ScreenPtr pScreen, Bool enable)
 
1102
{
 
1103
    WindowPtr   pWin = WindowTable[pScreen->myNum];
 
1104
    WindowPtr   pChild;
 
1105
    Bool        WasViewable = (Bool)(pWin->viewable);
 
1106
    Bool        anyMarked = FALSE;
 
1107
    RegionPtr   pOldClip = NULL, bsExposed;
 
1108
#ifdef DO_SAVE_UNDERS
 
1109
    Bool        dosave = FALSE;
 
1110
#endif
 
1111
    WindowPtr   pLayerWin;
 
1112
    BoxRec      box;
 
1113
 
 
1114
    if (WasViewable)
 
1115
    {
 
1116
        for (pChild = pWin->firstChild; pChild; pChild = pChild->nextSib)
 
1117
        {
 
1118
            (void) (*pScreen->MarkOverlappedWindows)(pChild,
 
1119
                                                     pChild,
 
1120
                                                     &pLayerWin);
 
1121
        }
 
1122
        (*pScreen->MarkWindow) (pWin);
 
1123
        anyMarked = TRUE;
 
1124
        if (pWin->valdata)
 
1125
        {
 
1126
            if (HasBorder (pWin))
 
1127
            {
 
1128
                RegionPtr       borderVisible;
 
1129
 
 
1130
                borderVisible = REGION_CREATE(pScreen, NullBox, 1);
 
1131
                REGION_SUBTRACT(pScreen, borderVisible,
 
1132
                                &pWin->borderClip, &pWin->winSize);
 
1133
                pWin->valdata->before.borderVisible = borderVisible;
 
1134
            }
 
1135
            pWin->valdata->before.resized = TRUE;
 
1136
        }
 
1137
    }
 
1138
    
 
1139
    /*
 
1140
     * Use REGION_BREAK to avoid optimizations in ValidateTree
 
1141
     * that assume the root borderClip can't change well, normally
 
1142
     * it doesn't...)
 
1143
     */
 
1144
    if (enable)
 
1145
    {
 
1146
        box.x1 = 0;
 
1147
        box.y1 = 0;
 
1148
        box.x2 = pScreen->width;
 
1149
        box.y2 = pScreen->height;
 
1150
        REGION_INIT (pScreen, &pWin->winSize, &box, 1);
 
1151
        REGION_INIT (pScreen, &pWin->borderSize, &box, 1);
 
1152
        if (WasViewable)
 
1153
            REGION_RESET(pScreen, &pWin->borderClip, &box);
 
1154
        pWin->drawable.width = pScreen->width;
 
1155
        pWin->drawable.height = pScreen->height;
 
1156
        REGION_BREAK (pWin->drawable.pScreen, &pWin->clipList);
 
1157
    }
 
1158
    else
 
1159
    {
 
1160
        REGION_EMPTY(pScreen, &pWin->borderClip);
 
1161
        REGION_BREAK (pWin->drawable.pScreen, &pWin->clipList);
 
1162
    }
 
1163
    
 
1164
    ResizeChildrenWinSize (pWin, 0, 0, 0, 0);
 
1165
    
 
1166
    if (WasViewable)
 
1167
    {
 
1168
        if (pWin->backStorage)
 
1169
        {
 
1170
            pOldClip = REGION_CREATE(pScreen, NullBox, 1);
 
1171
            REGION_COPY(pScreen, pOldClip, &pWin->clipList);
 
1172
        }
 
1173
 
 
1174
        if (pWin->firstChild)
 
1175
        {
 
1176
            anyMarked |= (*pScreen->MarkOverlappedWindows)(pWin->firstChild,
 
1177
                                                           pWin->firstChild,
 
1178
                                                           (WindowPtr *)NULL);
 
1179
        }
 
1180
        else
 
1181
        {
 
1182
            (*pScreen->MarkWindow) (pWin);
 
1183
            anyMarked = TRUE;
 
1184
        }
 
1185
 
 
1186
#ifdef DO_SAVE_UNDERS
 
1187
        if (DO_SAVE_UNDERS(pWin))
 
1188
        {
 
1189
            dosave = (*pScreen->ChangeSaveUnder)(pLayerWin, pLayerWin);
 
1190
        }
 
1191
#endif /* DO_SAVE_UNDERS */
 
1192
 
 
1193
        if (anyMarked)
 
1194
            (*pScreen->ValidateTree)(pWin, NullWindow, VTOther);
 
1195
    }
 
1196
 
 
1197
    if (pWin->backStorage &&
 
1198
        ((pWin->backingStore == Always) || WasViewable))
 
1199
    {
 
1200
        if (!WasViewable)
 
1201
            pOldClip = &pWin->clipList; /* a convenient empty region */
 
1202
        bsExposed = (*pScreen->TranslateBackingStore)
 
1203
                             (pWin, 0, 0, pOldClip,
 
1204
                              pWin->drawable.x, pWin->drawable.y);
 
1205
        if (WasViewable)
 
1206
            REGION_DESTROY(pScreen, pOldClip);
 
1207
        if (bsExposed)
 
1208
        {
 
1209
            RegionPtr   valExposed = NullRegion;
 
1210
    
 
1211
            if (pWin->valdata)
 
1212
                valExposed = &pWin->valdata->after.exposed;
 
1213
            (*pScreen->WindowExposures) (pWin, valExposed, bsExposed);
 
1214
            if (valExposed)
 
1215
                REGION_EMPTY(pScreen, valExposed);
 
1216
            REGION_DESTROY(pScreen, bsExposed);
 
1217
        }
 
1218
    }
 
1219
    if (WasViewable)
 
1220
    {
 
1221
        if (anyMarked)
 
1222
            (*pScreen->HandleExposures)(pWin);
 
1223
#ifdef DO_SAVE_UNDERS
 
1224
        if (dosave)
 
1225
            (*pScreen->PostChangeSaveUnder)(pLayerWin, pLayerWin);
 
1226
#endif /* DO_SAVE_UNDERS */
 
1227
        if (anyMarked && pScreen->PostValidateTree)
 
1228
            (*pScreen->PostValidateTree)(pWin, NullWindow, VTOther);
 
1229
    }
 
1230
    if (pWin->realized)
 
1231
        WindowsRestructured ();
 
1232
    FlushAllOutput ();
 
1233
}
 
1234
 
 
1235
/*
 
1236
 * Function to enable/disable access to the frame buffer
 
1237
 *
 
1238
 * This is used when VT switching and when entering/leaving DGA direct mode.
 
1239
 *
 
1240
 * This has been rewritten again to eliminate the saved pixmap.  The
 
1241
 * devPrivate field in the screen pixmap is set to NULL to catch code
 
1242
 * accidentally referencing the frame buffer while the X server is not
 
1243
 * supposed to touch it.
 
1244
 *
 
1245
 * Here, we exchange the pixmap private data, rather than the pixmaps
 
1246
 * themselves to avoid having to find and change any references to the screen
 
1247
 * pixmap such as GC's, window privates etc.  This also means that this code
 
1248
 * does not need to know exactly how the pixmap pixels are accessed.  Further,
 
1249
 * this exchange is >not< done through the screen's ModifyPixmapHeader()
 
1250
 * vector.  This means the called frame buffer code layers can determine
 
1251
 * whether they are switched in or out by keeping track of the root pixmap's
 
1252
 * private data, and therefore don't need to access pScrnInfo->vtSema.
 
1253
 */
 
1254
void
 
1255
xf86EnableDisableFBAccess(int scrnIndex, Bool enable)
 
1256
{
 
1257
    ScrnInfoPtr pScrnInfo = xf86Screens[scrnIndex];
 
1258
    ScreenPtr pScreen = pScrnInfo->pScreen;
 
1259
    PixmapPtr pspix;
 
1260
 
 
1261
    pspix = (*pScreen->GetScreenPixmap) (pScreen);
 
1262
    if (enable)
 
1263
    {
 
1264
        /*
 
1265
         * Restore the screen pixmap devPrivate field
 
1266
         */
 
1267
        pspix->devPrivate = pScrnInfo->pixmapPrivate;
 
1268
        /*
 
1269
         * Restore all of the clip lists on the screen 
 
1270
         */
 
1271
        if (!xf86Resetting)
 
1272
            xf86SetRootClip (pScreen, TRUE);
 
1273
 
 
1274
    }
 
1275
    else
 
1276
    {
 
1277
        /*
 
1278
         * Empty all of the clip lists on the screen 
 
1279
         */
 
1280
        xf86SetRootClip (pScreen, FALSE);
 
1281
        /*
 
1282
         * save the screen pixmap devPrivate field and
 
1283
         * replace it with NULL so accidental references
 
1284
         * to the frame buffer are caught
 
1285
         */
 
1286
        pScrnInfo->pixmapPrivate = pspix->devPrivate;
 
1287
        pspix->devPrivate.ptr = NULL;
 
1288
    }
 
1289
}
 
1290
 
 
1291
/* Print driver messages in the standard format */
 
1292
 
 
1293
#undef PREFIX_SIZE
 
1294
#define PREFIX_SIZE 14
 
1295
 
 
1296
void
 
1297
xf86VDrvMsgVerb(int scrnIndex, MessageType type, int verb, const char *format,
 
1298
                va_list args)
 
1299
{
 
1300
    char *tmpFormat;
 
1301
 
 
1302
    /* Prefix the scrnIndex name to the format string. */
 
1303
    if (scrnIndex >= 0 && scrnIndex < xf86NumScreens &&
 
1304
        xf86Screens[scrnIndex]->name) {
 
1305
        tmpFormat = xalloc(strlen(format) +
 
1306
                           strlen(xf86Screens[scrnIndex]->name) +
 
1307
                           PREFIX_SIZE + 1);
 
1308
        if (!tmpFormat)
 
1309
            return;
 
1310
 
 
1311
        snprintf(tmpFormat, PREFIX_SIZE + 1, "%s(%d): ",
 
1312
                 xf86Screens[scrnIndex]->name, scrnIndex);
 
1313
 
 
1314
        strcat(tmpFormat, format);
 
1315
        LogVMessageVerb(type, verb, tmpFormat, args);
 
1316
        xfree(tmpFormat);
 
1317
    } else
 
1318
        LogVMessageVerb(type, verb, format, args);
 
1319
}
 
1320
#undef PREFIX_SIZE
 
1321
 
 
1322
/* Print driver messages, with verbose level specified directly */
 
1323
void
 
1324
xf86DrvMsgVerb(int scrnIndex, MessageType type, int verb, const char *format,
 
1325
               ...)
 
1326
{
 
1327
    va_list ap;
 
1328
 
 
1329
    va_start(ap, format);
 
1330
    xf86VDrvMsgVerb(scrnIndex, type, verb, format, ap);
 
1331
    va_end(ap);
 
1332
}
 
1333
 
 
1334
/* Print driver messages, with verbose level of 1 (default) */
 
1335
void
 
1336
xf86DrvMsg(int scrnIndex, MessageType type, const char *format, ...)
 
1337
{
 
1338
    va_list ap;
 
1339
 
 
1340
    va_start(ap, format);
 
1341
    xf86VDrvMsgVerb(scrnIndex, type, 1, format, ap);
 
1342
    va_end(ap);
 
1343
}
 
1344
 
 
1345
/* Print non-driver messages with verbose level specified directly */
 
1346
void
 
1347
xf86MsgVerb(MessageType type, int verb, const char *format, ...)
 
1348
{
 
1349
    va_list ap;
 
1350
 
 
1351
    va_start(ap, format);
 
1352
    xf86VDrvMsgVerb(-1, type, verb, format, ap);
 
1353
    va_end(ap);
 
1354
}
 
1355
 
 
1356
/* Print non-driver messages with verbose level of 1 (default) */
 
1357
void
 
1358
xf86Msg(MessageType type, const char *format, ...)
 
1359
{
 
1360
    va_list ap;
 
1361
 
 
1362
    va_start(ap, format);
 
1363
    xf86VDrvMsgVerb(-1, type, 1, format, ap);
 
1364
    va_end(ap);
 
1365
}
 
1366
 
 
1367
/* Just like ErrorF, but with the verbose level checked */
 
1368
void
 
1369
xf86ErrorFVerb(int verb, const char *format, ...)
 
1370
{
 
1371
    va_list ap;
 
1372
 
 
1373
    va_start(ap, format);
 
1374
    if (xf86Verbose >= verb || xf86LogVerbose >= verb)
 
1375
        LogVWrite(verb, format, ap);
 
1376
    va_end(ap);
 
1377
}
 
1378
 
 
1379
/* Like xf86ErrorFVerb, but with an implied verbose level of 1 */
 
1380
void
 
1381
xf86ErrorF(const char *format, ...)
 
1382
{
 
1383
    va_list ap;
 
1384
 
 
1385
    va_start(ap, format);
 
1386
    if (xf86Verbose >= 1 || xf86LogVerbose >= 1)
 
1387
        LogVWrite(1, format, ap);
 
1388
    va_end(ap);
 
1389
}
 
1390
 
 
1391
 
 
1392
void
 
1393
xf86LogInit()
 
1394
{
 
1395
    char *lf;
 
1396
 
 
1397
#define LOGSUFFIX ".log"
 
1398
#define LOGOLDSUFFIX ".old"
 
1399
    
 
1400
    /* Get the log file name */
 
1401
    if (xf86LogFileFrom == X_DEFAULT) {
 
1402
        /* Append the display number and ".log" */
 
1403
        lf = malloc(strlen(xf86LogFile) + strlen("%s") +
 
1404
                    strlen(LOGSUFFIX) + 1);
 
1405
        if (!lf)
 
1406
            FatalError("Cannot allocate space for the log file name\n");
 
1407
        sprintf(lf, "%s%%s" LOGSUFFIX, xf86LogFile);
 
1408
        xf86LogFile = lf;
 
1409
    }
 
1410
 
 
1411
    xf86LogFile = LogInit(xf86LogFile, LOGOLDSUFFIX);
 
1412
    xf86LogFileWasOpened = TRUE;
 
1413
 
 
1414
    xf86SetVerbosity(xf86Verbose);
 
1415
    xf86SetLogVerbosity(xf86LogVerbose);
 
1416
 
 
1417
#undef LOGSUFFIX
 
1418
#undef LOGOLDSUFFIX
 
1419
}
 
1420
 
 
1421
void
 
1422
xf86CloseLog()
 
1423
{
 
1424
    LogClose();
 
1425
}
 
1426
 
 
1427
 
 
1428
/*
 
1429
 * Drivers can use these for using their own SymTabRecs.
 
1430
 */
 
1431
 
 
1432
const char *
 
1433
xf86TokenToString(SymTabPtr table, int token)
 
1434
{
 
1435
    int i;
 
1436
 
 
1437
    for (i = 0; table[i].token >= 0 && table[i].token != token; i++)
 
1438
        ;
 
1439
 
 
1440
    if (table[i].token < 0)
 
1441
        return NULL;
 
1442
    else
 
1443
        return(table[i].name);
 
1444
}
 
1445
 
 
1446
int
 
1447
xf86StringToToken(SymTabPtr table, const char *string)
 
1448
{
 
1449
    int i;
 
1450
 
 
1451
    if (string == NULL)
 
1452
        return -1;
 
1453
 
 
1454
    for (i = 0; table[i].token >= 0 && xf86NameCmp(string, table[i].name); i++)
 
1455
        ;
 
1456
 
 
1457
    return(table[i].token);
 
1458
}
 
1459
 
 
1460
/*
 
1461
 * helper to display the clocks found on a card
 
1462
 */
 
1463
void
 
1464
xf86ShowClocks(ScrnInfoPtr scrp, MessageType from)
 
1465
{
 
1466
    int j;
 
1467
 
 
1468
    xf86DrvMsg(scrp->scrnIndex, from, "Pixel clocks available:");
 
1469
    for (j=0; j < scrp->numClocks; j++) {
 
1470
        if ((j % 4) == 0) {
 
1471
            xf86ErrorF("\n");
 
1472
            xf86DrvMsg(scrp->scrnIndex, from, "pixel clocks:");
 
1473
        }
 
1474
        xf86ErrorF(" %7.3f", (double)scrp->clock[j] / 1000.0);
 
1475
    }
 
1476
    xf86ErrorF("\n");
 
1477
}
 
1478
 
 
1479
 
 
1480
/*
 
1481
 * This prints out the driver identify message, including the names of
 
1482
 * the supported chipsets.
 
1483
 *
 
1484
 * XXX This makes assumptions about the line width, etc.  Maybe we could
 
1485
 * use a more general "pretty print" function for messages.
 
1486
 */
 
1487
void
 
1488
xf86PrintChipsets(const char *drvname, const char *drvmsg, SymTabPtr chips)
 
1489
{
 
1490
    int len, i;
 
1491
 
 
1492
    len = 6 + strlen(drvname) + 2 + strlen(drvmsg) + 2;
 
1493
    xf86Msg(X_INFO, "%s: %s:", drvname, drvmsg);
 
1494
    for (i = 0; chips[i].name != NULL; i++) {
 
1495
        if (i != 0) {
 
1496
            xf86ErrorF(",");
 
1497
            len++;
 
1498
        }
 
1499
        if (len + 2 + strlen(chips[i].name) < 78) {
 
1500
            xf86ErrorF(" ");
 
1501
            len++;
 
1502
        } else {
 
1503
            xf86ErrorF("\n\t");
 
1504
            len = 8;
 
1505
        }
 
1506
        xf86ErrorF("%s", chips[i].name);
 
1507
        len += strlen(chips[i].name);
 
1508
    }
 
1509
    xf86ErrorF("\n");
 
1510
}
 
1511
 
 
1512
 
 
1513
#define MAXDRIVERS 64   /* A >hack<, to be sure ... */
 
1514
 
 
1515
 
 
1516
int
 
1517
xf86MatchDevice(const char *drivername, GDevPtr **sectlist)
 
1518
{
 
1519
    GDevPtr       gdp, *pgdp = NULL;
 
1520
    confScreenPtr screensecptr;
 
1521
    int i,j;
 
1522
 
 
1523
    if (sectlist)
 
1524
        *sectlist = NULL;
 
1525
 
 
1526
    if (xf86DoProbe) return 1;
 
1527
  
 
1528
    if (xf86DoConfigure && xf86DoConfigurePass1) return 1;
 
1529
 
 
1530
    /*
 
1531
     * This is a very important function that matches the device sections
 
1532
     * as they show up in the config file with the drivers that the server
 
1533
     * loads at run time.
 
1534
     *
 
1535
     * ChipProbe can call 
 
1536
     * int xf86MatchDevice(char * drivername, GDevPtr ** sectlist) 
 
1537
     * with its driver name. The function allocates an array of GDevPtr and
 
1538
     * returns this via sectlist and returns the number of elements in
 
1539
     * this list as return value. 0 means none found, -1 means fatal error.
 
1540
     * 
 
1541
     * It can figure out which of the Device sections to use for which card
 
1542
     * (using things like the Card statement, etc). For single headed servers
 
1543
     * there will of course be just one such Device section.
 
1544
     */
 
1545
    i = 0;
 
1546
    
 
1547
    /*
 
1548
     * first we need to loop over all the Screens sections to get to all
 
1549
     * 'active' device sections
 
1550
     */
 
1551
    for (j=0; xf86ConfigLayout.screens[j].screen != NULL; j++) {
 
1552
        screensecptr = xf86ConfigLayout.screens[j].screen;
 
1553
        if ((screensecptr->device->driver != NULL)
 
1554
            && (xf86NameCmp( screensecptr->device->driver,drivername) == 0)
 
1555
            && (! screensecptr->device->claimed)) {
 
1556
            /*
 
1557
             * we have a matching driver that wasn't claimed, yet
 
1558
             */
 
1559
            pgdp = xnfrealloc(pgdp, (i + 2) * sizeof(GDevPtr));
 
1560
            pgdp[i++] = screensecptr->device;
 
1561
        }
 
1562
    }
 
1563
 
 
1564
    /* Then handle the inactive devices */
 
1565
    j = 0;
 
1566
    while (xf86ConfigLayout.inactives[j].identifier) {
 
1567
        gdp = &xf86ConfigLayout.inactives[j];
 
1568
        if (gdp->driver && !gdp->claimed &&
 
1569
            !xf86NameCmp(gdp->driver,drivername)) {
 
1570
            /* we have a matching driver that wasn't claimed yet */
 
1571
            pgdp = xnfrealloc(pgdp, (i + 2) * sizeof(GDevPtr));
 
1572
            pgdp[i++] = gdp;
 
1573
        }
 
1574
        j++;
 
1575
    }
 
1576
    
 
1577
    /*
 
1578
     * make the array NULL terminated and return its address
 
1579
     */
 
1580
    if (i)
 
1581
        pgdp[i] = NULL;
 
1582
 
 
1583
    if (sectlist)
 
1584
        *sectlist = pgdp;
 
1585
    else
 
1586
        xfree(pgdp);
 
1587
    return i;
 
1588
}
 
1589
 
 
1590
struct Inst {
 
1591
    pciVideoPtr pci;
 
1592
    GDevPtr             dev;
 
1593
    Bool                foundHW;  /* PCIid in list of supported chipsets */
 
1594
    Bool                claimed;  /* BusID matches with a device section */
 
1595
    int                 chip;
 
1596
    int                 screen;
 
1597
};
 
1598
 
 
1599
int
 
1600
xf86MatchPciInstances(const char *driverName, int vendorID, 
 
1601
                      SymTabPtr chipsets, PciChipsets *PCIchipsets,
 
1602
                      GDevPtr *devList, int numDevs, DriverPtr drvp,
 
1603
                      int **foundEntities)
 
1604
{
 
1605
    int i,j;
 
1606
    MessageType from;
 
1607
    pciVideoPtr pPci, *ppPci;
 
1608
    struct Inst {
 
1609
        pciVideoPtr     pci;
 
1610
        GDevPtr         dev;
 
1611
        Bool            foundHW;  /* PCIid in list of supported chipsets */
 
1612
        Bool            claimed;  /* BusID matches with a device section */
 
1613
        int             chip;
 
1614
        int             screen;
 
1615
    } *instances = NULL;
 
1616
    int numClaimedInstances = 0;
 
1617
    int allocatedInstances = 0;
 
1618
    int numFound = 0;
 
1619
    SymTabRec *c;
 
1620
    PciChipsets *id;
 
1621
    GDevPtr devBus = NULL;
 
1622
    GDevPtr dev = NULL;
 
1623
    int *retEntities = NULL;
 
1624
 
 
1625
    *foundEntities = NULL;
 
1626
 
 
1627
    if (vendorID == 0) {
 
1628
        for (ppPci = xf86PciVideoInfo; *ppPci != NULL; ppPci++) {
 
1629
            Bool foundVendor = FALSE;
 
1630
            for (id = PCIchipsets; id->PCIid != -1; id++) {
 
1631
                if ( (((id->PCIid & 0xFFFF0000) >> 16) == (*ppPci)->vendor)) {
 
1632
                    if (!foundVendor) {
 
1633
                        ++allocatedInstances;
 
1634
                        instances = xnfrealloc(instances,
 
1635
                                     allocatedInstances * sizeof(struct Inst));
 
1636
                        instances[allocatedInstances - 1].pci = *ppPci;
 
1637
                        instances[allocatedInstances - 1].dev = NULL;
 
1638
                        instances[allocatedInstances - 1].claimed = FALSE;
 
1639
                        instances[allocatedInstances - 1].foundHW = FALSE;
 
1640
                        instances[allocatedInstances - 1].screen = 0;
 
1641
                        foundVendor = TRUE;
 
1642
                    } 
 
1643
                    if ((id->PCIid & 0x0000FFFF) == (*ppPci)->chipType) {
 
1644
                       instances[allocatedInstances - 1].foundHW = TRUE;
 
1645
                       instances[allocatedInstances - 1].chip = id->numChipset;
 
1646
                       numFound++;
 
1647
                    }
 
1648
                }
 
1649
            }
 
1650
        }
 
1651
    } else if (vendorID == PCI_VENDOR_GENERIC) {
 
1652
        for (ppPci = xf86PciVideoInfo; *ppPci != NULL; ppPci++) {
 
1653
            for (id = PCIchipsets; id->PCIid != -1; id++) {
 
1654
                if (id->PCIid == xf86CheckPciGAType(*ppPci)) {
 
1655
                    ++allocatedInstances;
 
1656
                    instances = xnfrealloc(instances,
 
1657
                                  allocatedInstances * sizeof(struct Inst));
 
1658
                    instances[allocatedInstances - 1].pci = *ppPci;
 
1659
                    instances[allocatedInstances - 1].dev = NULL;
 
1660
                    instances[allocatedInstances - 1].claimed = FALSE;
 
1661
                    instances[allocatedInstances - 1].foundHW = TRUE;
 
1662
                    instances[allocatedInstances - 1].chip = id->numChipset;
 
1663
                    instances[allocatedInstances - 1].screen = 0;
 
1664
                    numFound++;
 
1665
                }
 
1666
            }
 
1667
        }
 
1668
    } else {
 
1669
        /* Find PCI devices that match the given vendor ID */
 
1670
        for (ppPci = xf86PciVideoInfo; (ppPci != NULL)
 
1671
               && (*ppPci != NULL); ppPci++) {
 
1672
            if ((*ppPci)->vendor == vendorID) {
 
1673
                ++allocatedInstances;
 
1674
                instances = xnfrealloc(instances,
 
1675
                              allocatedInstances * sizeof(struct Inst));
 
1676
                instances[allocatedInstances - 1].pci = *ppPci;
 
1677
                instances[allocatedInstances - 1].dev = NULL;
 
1678
                instances[allocatedInstances - 1].claimed = FALSE;
 
1679
                instances[allocatedInstances - 1].foundHW = FALSE;
 
1680
                instances[allocatedInstances - 1].screen = 0;
 
1681
 
 
1682
                /* Check if the chip type is listed in the chipsets table */
 
1683
                for (id = PCIchipsets; id->PCIid != -1; id++) {
 
1684
                    if (id->PCIid == (*ppPci)->chipType) {
 
1685
                        instances[allocatedInstances - 1].chip
 
1686
                            = id->numChipset;
 
1687
                        instances[allocatedInstances - 1].foundHW = TRUE;
 
1688
                        numFound++;
 
1689
                        break;
 
1690
                    }
 
1691
                }
 
1692
            }
 
1693
        }
 
1694
    }
 
1695
 
 
1696
    /*
 
1697
     * This may be debatable, but if no PCI devices with a matching vendor
 
1698
     * type is found, return zero now.  It is probably not desirable to
 
1699
     * allow the config file to override this.
 
1700
     */
 
1701
    if (allocatedInstances <= 0) {
 
1702
        xfree(instances);
 
1703
        return 0;
 
1704
    }
 
1705
 
 
1706
    if (xf86DoProbe) {
 
1707
        xfree(instances);
 
1708
        return numFound;
 
1709
    }
 
1710
 
 
1711
    if (xf86DoConfigure && xf86DoConfigurePass1) {
 
1712
        GDevPtr pGDev;
 
1713
        int actualcards = 0;
 
1714
        for (i = 0; i < allocatedInstances; i++) {
 
1715
            pPci = instances[i].pci;
 
1716
            if (instances[i].foundHW) {
 
1717
                if (!xf86CheckPciSlot(pPci->bus, pPci->device, pPci->func))
 
1718
                    continue;
 
1719
                actualcards++;
 
1720
                pGDev = xf86AddDeviceToConfigure(drvp->driverName,
 
1721
                                                 instances[i].pci, -1);
 
1722
                if (pGDev) {
 
1723
                   /*
 
1724
                    * XF86Match???Instances() treat chipID and chipRev as
 
1725
                    * overrides, so clobber them here.
 
1726
                    */
 
1727
                   pGDev->chipID = pGDev->chipRev = -1;
 
1728
                }
 
1729
            }
 
1730
        }
 
1731
        xfree(instances);
 
1732
        return actualcards;
 
1733
    }
 
1734
 
 
1735
#ifdef DEBUG
 
1736
    ErrorF("%s instances found: %d\n", driverName, allocatedInstances);
 
1737
#endif
 
1738
 
 
1739
   /*
 
1740
    * Check for devices that need duplicated instances.  This is required
 
1741
    * when there is more than one screen per entity.
 
1742
    *
 
1743
    * XXX This currently doesn't work for cases where the BusID isn't
 
1744
    * specified explicitly in the config file.
 
1745
    */
 
1746
 
 
1747
    for (j = 0; j < numDevs; j++) {
 
1748
        if (devList[j]->screen > 0 && devList[j]->busID 
 
1749
            && *devList[j]->busID) {
 
1750
            for (i = 0; i < allocatedInstances; i++) {
 
1751
                pPci = instances[i].pci;
 
1752
                if (xf86ComparePciBusString(devList[j]->busID, pPci->bus,
 
1753
                                            pPci->device,
 
1754
                                            pPci->func)) {
 
1755
                    allocatedInstances++;
 
1756
                    instances = xnfrealloc(instances,
 
1757
                                           allocatedInstances * 
 
1758
                                           sizeof(struct Inst));
 
1759
                    instances[allocatedInstances - 1] = instances[i];
 
1760
                    instances[allocatedInstances - 1].screen =
 
1761
                                                devList[j]->screen;
 
1762
                    numFound++;
 
1763
                    break;
 
1764
                }
 
1765
            }
 
1766
        }
 
1767
    }
 
1768
 
 
1769
    for (i = 0; i < allocatedInstances; i++) {
 
1770
        pPci = instances[i].pci;
 
1771
        devBus = NULL;
 
1772
        dev = NULL;
 
1773
        for (j = 0; j < numDevs; j++) {
 
1774
            if (devList[j]->busID && *devList[j]->busID) {
 
1775
                if (xf86ComparePciBusString(devList[j]->busID, pPci->bus,
 
1776
                                           pPci->device,
 
1777
                                           pPci->func) &&
 
1778
                    devList[j]->screen == instances[i].screen) {
 
1779
                   
 
1780
                    if (devBus)
 
1781
                        xf86MsgVerb(X_WARNING,0,
 
1782
                            "%s: More than one matching Device section for "
 
1783
                            "instances\n\t(BusID: %s) found: %s\n",
 
1784
                            driverName, devList[j]->busID,
 
1785
                            devList[j]->identifier);
 
1786
                    else
 
1787
                        devBus = devList[j];
 
1788
                } 
 
1789
            } else {
 
1790
                /* 
 
1791
                 * if device section without BusID is found 
 
1792
                 * only assign to it to the primary device.
 
1793
                 */
 
1794
                if (xf86IsPrimaryPci(pPci)) {
 
1795
                    xf86Msg(X_PROBED, "Assigning device section with no busID"
 
1796
                            " to primary device\n");
 
1797
                    if (dev || devBus)
 
1798
                        xf86MsgVerb(X_WARNING, 0,
 
1799
                            "%s: More than one matching Device section "
 
1800
                            "found: %s\n", driverName, devList[j]->identifier);
 
1801
                    else
 
1802
                        dev = devList[j];
 
1803
                }
 
1804
            }
 
1805
        }
 
1806
        if (devBus) dev = devBus;  /* busID preferred */ 
 
1807
        if (!dev) {
 
1808
            if (xf86CheckPciSlot(pPci->bus, pPci->device, pPci->func)) {
 
1809
                xf86MsgVerb(X_WARNING, 0, "%s: No matching Device section "
 
1810
                            "for instance (BusID PCI:%i:%i:%i) found\n",
 
1811
                            driverName, pPci->bus, pPci->device, pPci->func);
 
1812
            }
 
1813
        } else {
 
1814
            numClaimedInstances++;
 
1815
            instances[i].claimed = TRUE;
 
1816
            instances[i].dev = dev;
 
1817
        }
 
1818
    }
 
1819
#ifdef DEBUG
 
1820
    ErrorF("%s instances found: %d\n", driverName, numClaimedInstances);
 
1821
#endif
 
1822
    /*
 
1823
     * Now check that a chipset or chipID override in the device section
 
1824
     * is valid.  Chipset has precedence over chipID.
 
1825
     * If chipset is not valid ignore BusSlot completely.
 
1826
     */
 
1827
    for (i = 0; i < allocatedInstances && numClaimedInstances > 0; i++) {
 
1828
        if (!instances[i].claimed) {
 
1829
            continue;
 
1830
        }
 
1831
        from = X_PROBED;
 
1832
        if (instances[i].dev->chipset) {
 
1833
            for (c = chipsets; c->token >= 0; c++) {
 
1834
                if (xf86NameCmp(c->name, instances[i].dev->chipset) == 0)
 
1835
                    break;
 
1836
            }
 
1837
            if (c->token == -1) {
 
1838
                instances[i].claimed = FALSE;
 
1839
                numClaimedInstances--;
 
1840
                xf86MsgVerb(X_WARNING, 0, "%s: Chipset \"%s\" in Device "
 
1841
                            "section \"%s\" isn't valid for this driver\n",
 
1842
                            driverName, instances[i].dev->chipset,
 
1843
                            instances[i].dev->identifier);
 
1844
            } else {
 
1845
                instances[i].chip = c->token;
 
1846
 
 
1847
                for (id = PCIchipsets; id->numChipset >= 0; id++) {
 
1848
                    if (id->numChipset == instances[i].chip)
 
1849
                        break;
 
1850
                }
 
1851
                if(id->numChipset >=0){
 
1852
                    xf86Msg(X_CONFIG,"Chipset override: %s\n",
 
1853
                             instances[i].dev->chipset);
 
1854
                    from = X_CONFIG;
 
1855
                } else {
 
1856
                    instances[i].claimed = FALSE;
 
1857
                    numClaimedInstances--;
 
1858
                    xf86MsgVerb(X_WARNING, 0, "%s: Chipset \"%s\" in Device "
 
1859
                                "section \"%s\" isn't a valid PCI chipset\n",
 
1860
                                driverName, instances[i].dev->chipset,
 
1861
                                instances[i].dev->identifier);
 
1862
                }
 
1863
            }
 
1864
        } else if (instances[i].dev->chipID > 0) {
 
1865
            for (id = PCIchipsets; id->numChipset >= 0; id++) {
 
1866
                if (id->PCIid == instances[i].dev->chipID)
 
1867
                    break;
 
1868
            }
 
1869
            if (id->numChipset == -1) {
 
1870
                instances[i].claimed = FALSE;
 
1871
                numClaimedInstances--;
 
1872
                xf86MsgVerb(X_WARNING, 0, "%s: ChipID 0x%04X in Device "
 
1873
                            "section \"%s\" isn't valid for this driver\n",
 
1874
                            driverName, instances[i].dev->chipID,
 
1875
                            instances[i].dev->identifier);
 
1876
            } else {
 
1877
                instances[i].chip = id->numChipset;
 
1878
 
 
1879
                xf86Msg( X_CONFIG,"ChipID override: 0x%04X\n",
 
1880
                         instances[i].dev->chipID);
 
1881
                from = X_CONFIG;
 
1882
            }
 
1883
        } else if (!instances[i].foundHW) {
 
1884
            /*
 
1885
             * This means that there was no override and the PCI chipType
 
1886
             * doesn't match one that is supported
 
1887
             */
 
1888
            instances[i].claimed = FALSE;
 
1889
            numClaimedInstances--;
 
1890
        }
 
1891
        if (instances[i].claimed == TRUE){
 
1892
            for (c = chipsets; c->token >= 0; c++) {
 
1893
                if (c->token == instances[i].chip)
 
1894
                    break;
 
1895
            }
 
1896
            xf86Msg(from,"Chipset %s found\n",
 
1897
                    c->name);
 
1898
        }
 
1899
    }
 
1900
 
 
1901
    /*
 
1902
     * Of the claimed instances, check that another driver hasn't already
 
1903
     * claimed its slot.
 
1904
     */
 
1905
    numFound = 0;
 
1906
    for (i = 0; i < allocatedInstances && numClaimedInstances > 0; i++) {
 
1907
        
 
1908
        if (!instances[i].claimed)
 
1909
            continue;
 
1910
        pPci = instances[i].pci;
 
1911
 
 
1912
 
 
1913
        /*
 
1914
         * Allow the same entity to be used more than once for devices with
 
1915
         * multiple screens per entity.  This assumes implicitly that there
 
1916
         * will be a screen == 0 instance.
 
1917
         *
 
1918
         * XXX Need to make sure that two different drivers don't claim
 
1919
         * the same screen > 0 instance.
 
1920
         */
 
1921
        if (instances[i].screen == 0 &&
 
1922
            !xf86CheckPciSlot(pPci->bus, pPci->device, pPci->func))
 
1923
            continue;
 
1924
 
 
1925
#ifdef DEBUG
 
1926
        ErrorF("%s: card at %d:%d:%d is claimed by a Device section\n",
 
1927
               driverName, pPci->bus, pPci->device, pPci->func);
 
1928
#endif
 
1929
        
 
1930
        /* Allocate an entry in the lists to be returned */
 
1931
        numFound++;
 
1932
        retEntities = xnfrealloc(retEntities, numFound * sizeof(int));
 
1933
        retEntities[numFound - 1]
 
1934
            = xf86ClaimPciSlot(pPci->bus, pPci->device,
 
1935
                               pPci->func,drvp, instances[i].chip,
 
1936
                               instances[i].dev,instances[i].dev->active ?
 
1937
                               TRUE : FALSE);
 
1938
        if (retEntities[numFound - 1] == -1 && instances[i].screen > 0) {
 
1939
            for (j = 0; j < xf86NumEntities; j++) {
 
1940
                EntityPtr pEnt = xf86Entities[j];
 
1941
                if (pEnt->busType != BUS_PCI)
 
1942
                    continue;
 
1943
                if (pEnt->pciBusId.bus == pPci->bus &&
 
1944
                    pEnt->pciBusId.device == pPci->device &&
 
1945
                    pEnt->pciBusId.func == pPci->func) {
 
1946
                    retEntities[numFound - 1] = j;
 
1947
                    xf86AddDevToEntity(j, instances[i].dev);
 
1948
                    break;
 
1949
                }
 
1950
            }
 
1951
        }
 
1952
    }
 
1953
    xfree(instances);
 
1954
    if (numFound > 0) {
 
1955
        *foundEntities = retEntities;
 
1956
    }
 
1957
        
 
1958
    return numFound;
 
1959
}
 
1960
 
 
1961
int
 
1962
xf86MatchIsaInstances(const char *driverName, SymTabPtr chipsets,
 
1963
                      IsaChipsets *ISAchipsets, DriverPtr drvp,
 
1964
                      FindIsaDevProc FindIsaDevice, GDevPtr *devList,
 
1965
                      int numDevs, int **foundEntities)
 
1966
{
 
1967
    SymTabRec *c;
 
1968
    IsaChipsets *Chips;
 
1969
    int i;
 
1970
    int numFound = 0;
 
1971
    int foundChip = -1;
 
1972
    int *retEntities = NULL;
 
1973
 
 
1974
    *foundEntities = NULL;
 
1975
 
 
1976
#if defined(__sparc__) || defined(__powerpc__)
 
1977
    FindIsaDevice = NULL;       /* Temporary */
 
1978
#endif
 
1979
 
 
1980
    if (xf86DoProbe || (xf86DoConfigure && xf86DoConfigurePass1)) {
 
1981
        if (FindIsaDevice &&
 
1982
            ((foundChip = (*FindIsaDevice)(NULL)) != -1)) {
 
1983
            xf86AddDeviceToConfigure(drvp->driverName, NULL, foundChip);
 
1984
            return 1;
 
1985
        }
 
1986
        return 0;
 
1987
    }
 
1988
 
 
1989
    for (i = 0; i < numDevs; i++) {
 
1990
        MessageType from = X_CONFIG;
 
1991
        GDevPtr dev = NULL;
 
1992
        GDevPtr devBus = NULL;
 
1993
 
 
1994
        if (devList[i]->busID && *devList[i]->busID) {
 
1995
            if (xf86ParseIsaBusString(devList[i]->busID)) {
 
1996
                if (devBus) xf86MsgVerb(X_WARNING,0,
 
1997
                                        "%s: More than one matching Device "
 
1998
                                        "section for ISA-Bus found: %s\n",
 
1999
                                        driverName,devList[i]->identifier);
 
2000
                else devBus = devList[i];
 
2001
            }
 
2002
        } else {
 
2003
            if (xf86IsPrimaryIsa()) {
 
2004
                if (dev) xf86MsgVerb(X_WARNING,0,
 
2005
                                     "%s: More than one matching "
 
2006
                                     "Device section found: %s\n",
 
2007
                                     driverName,devList[i]->identifier);
 
2008
                else dev = devList[i];
 
2009
            }
 
2010
        }
 
2011
        if (devBus) dev = devBus;
 
2012
        if (dev) {
 
2013
            if (dev->chipset) {
 
2014
                for (c = chipsets; c->token >= 0; c++) {
 
2015
                    if (xf86NameCmp(c->name, dev->chipset) == 0)
 
2016
                        break;
 
2017
                }
 
2018
                if (c->token == -1) {
 
2019
                    xf86MsgVerb(X_WARNING, 0, "%s: Chipset \"%s\" in Device "
 
2020
                                "section \"%s\" isn't valid for this driver\n",
 
2021
                                driverName, dev->chipset,
 
2022
                                dev->identifier);
 
2023
                } else
 
2024
                    foundChip = c->token;
 
2025
            } else { 
 
2026
                if (FindIsaDevice) foundChip = (*FindIsaDevice)(dev);
 
2027
                                                        /* Probe it */
 
2028
                from = X_PROBED;
 
2029
            }
 
2030
        }
 
2031
        
 
2032
        /* Check if the chip type is listed in the chipset table - for sanity*/
 
2033
 
 
2034
        if (foundChip >= 0){
 
2035
            for (Chips = ISAchipsets; Chips->numChipset >= 0; Chips++) {
 
2036
                if (Chips->numChipset == foundChip) 
 
2037
                    break;
 
2038
            }
 
2039
            if (Chips->numChipset == -1){
 
2040
                foundChip = -1;
 
2041
                xf86MsgVerb(X_WARNING,0,
 
2042
                            "%s: Driver detected unknown ISA-Bus Chipset\n",
 
2043
                            driverName);
 
2044
            }
 
2045
        }
 
2046
        if (foundChip != -1) {
 
2047
            numFound++;
 
2048
            retEntities = xnfrealloc(retEntities,numFound * sizeof(int));
 
2049
            retEntities[numFound - 1] =
 
2050
            xf86ClaimIsaSlot(drvp,foundChip,dev, dev->active ? TRUE : FALSE);
 
2051
            for (c = chipsets; c->token >= 0; c++) {
 
2052
                if (c->token == foundChip)
 
2053
                    break;
 
2054
            }
 
2055
            xf86Msg(from, "Chipset %s found\n", c->name);
 
2056
        }
 
2057
    }
 
2058
    *foundEntities = retEntities;
 
2059
    
 
2060
    return numFound;
 
2061
}
 
2062
 
 
2063
/*
 
2064
 * xf86GetClocks -- get the dot-clocks via a BIG BAD hack ...
 
2065
 */
 
2066
void
 
2067
xf86GetClocks(ScrnInfoPtr pScrn, int num, Bool (*ClockFunc)(ScrnInfoPtr, int),
 
2068
              void (*ProtectRegs)(ScrnInfoPtr, Bool),
 
2069
              void (*BlankScreen)(ScrnInfoPtr, Bool), IOADDRESS vertsyncreg,
 
2070
              int maskval, int knownclkindex, int knownclkvalue)
 
2071
{
 
2072
    register int status = vertsyncreg;
 
2073
    unsigned long i, cnt, rcnt, sync;
 
2074
 
 
2075
    /* First save registers that get written on */
 
2076
    (*ClockFunc)(pScrn, CLK_REG_SAVE);
 
2077
 
 
2078
    xf86SetPriority(TRUE);
 
2079
 
 
2080
    if (num > MAXCLOCKS)
 
2081
        num = MAXCLOCKS;
 
2082
 
 
2083
    for (i = 0; i < num; i++) 
 
2084
    {
 
2085
        if (ProtectRegs)
 
2086
            (*ProtectRegs)(pScrn, TRUE);
 
2087
        if (!(*ClockFunc)(pScrn, i))
 
2088
        {
 
2089
            pScrn->clock[i] = -1;
 
2090
            continue;
 
2091
        }
 
2092
        if (ProtectRegs)
 
2093
            (*ProtectRegs)(pScrn, FALSE);
 
2094
        if (BlankScreen)
 
2095
            (*BlankScreen)(pScrn, FALSE);
 
2096
            
 
2097
        usleep(50000);     /* let VCO stabilise */
 
2098
 
 
2099
        cnt  = 0;
 
2100
        sync = 200000;
 
2101
 
 
2102
        /* XXX How critical is this? */
 
2103
        if (!xf86DisableInterrupts())
 
2104
        {
 
2105
            (*ClockFunc)(pScrn, CLK_REG_RESTORE);
 
2106
            ErrorF("Failed to disable interrupts during clock probe.  If\n");
 
2107
            ErrorF("your OS does not support disabling interrupts, then you\n");
 
2108
            FatalError("must specify a Clocks line in the XF86Config file.\n");
 
2109
        }
 
2110
        while ((inb(status) & maskval) == 0x00) 
 
2111
            if (sync-- == 0) goto finish;
 
2112
        /* Something appears to be happening, so reset sync count */
 
2113
        sync = 200000;
 
2114
        while ((inb(status) & maskval) == maskval) 
 
2115
            if (sync-- == 0) goto finish;
 
2116
        /* Something appears to be happening, so reset sync count */
 
2117
        sync = 200000;
 
2118
        while ((inb(status) & maskval) == 0x00) 
 
2119
            if (sync-- == 0) goto finish;
 
2120
    
 
2121
        for (rcnt = 0; rcnt < 5; rcnt++) 
 
2122
        {
 
2123
            while (!(inb(status) & maskval)) 
 
2124
                cnt++;
 
2125
            while ((inb(status) & maskval)) 
 
2126
                cnt++;
 
2127
        }
 
2128
    
 
2129
finish:
 
2130
        xf86EnableInterrupts();
 
2131
 
 
2132
        pScrn->clock[i] = cnt ? cnt : -1;
 
2133
        if (BlankScreen)
 
2134
            (*BlankScreen)(pScrn, TRUE);
 
2135
    }
 
2136
 
 
2137
    xf86SetPriority(FALSE);
 
2138
 
 
2139
    for (i = 0; i < num; i++) 
 
2140
    {
 
2141
        if (i != knownclkindex)
 
2142
        {
 
2143
            if (pScrn->clock[i] == -1)
 
2144
            {
 
2145
                pScrn->clock[i] = 0;
 
2146
            }
 
2147
            else 
 
2148
            {
 
2149
                pScrn->clock[i] = (int)(0.5 +
 
2150
                    (((float)knownclkvalue) * pScrn->clock[knownclkindex]) / 
 
2151
                    (pScrn->clock[i]));
 
2152
                /* Round to nearest 10KHz */
 
2153
                pScrn->clock[i] += 5;
 
2154
                pScrn->clock[i] /= 10;
 
2155
                pScrn->clock[i] *= 10;
 
2156
            }
 
2157
        }
 
2158
    }
 
2159
 
 
2160
    pScrn->clock[knownclkindex] = knownclkvalue;
 
2161
    pScrn->numClocks = num; 
 
2162
 
 
2163
    /* Restore registers that were written on */
 
2164
    (*ClockFunc)(pScrn, CLK_REG_RESTORE);
 
2165
}
 
2166
 
 
2167
void
 
2168
xf86SetPriority(Bool up)
 
2169
{
 
2170
    static int saved_nice;
 
2171
 
 
2172
    if (up) {
 
2173
#ifdef HAS_SETPRIORITY
 
2174
        saved_nice = getpriority(PRIO_PROCESS, 0);
 
2175
        setpriority(PRIO_PROCESS, 0, -20);
 
2176
#endif
 
2177
#if defined(SYSV) || defined(SVR4) || defined(linux)
 
2178
        saved_nice = nice(0);
 
2179
        nice(-20 - saved_nice);
 
2180
#endif
 
2181
    } else {
 
2182
#ifdef HAS_SETPRIORITY
 
2183
        setpriority(PRIO_PROCESS, 0, saved_nice);
 
2184
#endif
 
2185
#if defined(SYSV) || defined(SVR4) || defined(linux)
 
2186
        nice(20 + saved_nice);
 
2187
#endif
 
2188
    }
 
2189
}
 
2190
 
 
2191
const char *
 
2192
xf86GetVisualName(int visual)
 
2193
{
 
2194
    if (visual < 0 || visual > DirectColor)
 
2195
        return NULL;
 
2196
 
 
2197
    return xf86VisualNames[visual];
 
2198
}
 
2199
 
 
2200
 
 
2201
int
 
2202
xf86GetVerbosity()
 
2203
{
 
2204
    return max(xf86Verbose, xf86LogVerbose);
 
2205
}
 
2206
 
 
2207
Pix24Flags
 
2208
xf86GetPix24()
 
2209
{
 
2210
    return xf86Info.pixmap24;
 
2211
}
 
2212
 
 
2213
 
 
2214
int
 
2215
xf86GetDepth()
 
2216
{
 
2217
    return xf86Depth;
 
2218
}
 
2219
 
 
2220
 
 
2221
rgb
 
2222
xf86GetWeight()
 
2223
{
 
2224
    return xf86Weight;
 
2225
}
 
2226
 
 
2227
 
 
2228
Gamma
 
2229
xf86GetGamma()
 
2230
{
 
2231
    return xf86Gamma;
 
2232
}
 
2233
 
 
2234
 
 
2235
Bool
 
2236
xf86GetFlipPixels()
 
2237
{
 
2238
    return xf86FlipPixels;
 
2239
}
 
2240
 
 
2241
 
 
2242
const char *
 
2243
xf86GetServerName()
 
2244
{
 
2245
    return xf86ServerName;
 
2246
}
 
2247
 
 
2248
 
 
2249
Bool
 
2250
xf86ServerIsExiting()
 
2251
{
 
2252
    return (dispatchException & DE_TERMINATE) == DE_TERMINATE;
 
2253
}
 
2254
 
 
2255
 
 
2256
Bool
 
2257
xf86ServerIsResetting()
 
2258
{
 
2259
    return xf86Resetting;
 
2260
}
 
2261
 
 
2262
 
 
2263
Bool
 
2264
xf86ServerIsInitialising()
 
2265
{
 
2266
    return xf86Initialising;
 
2267
}
 
2268
 
 
2269
 
 
2270
Bool
 
2271
xf86ServerIsOnlyDetecting(void)
 
2272
{
 
2273
    return xf86DoProbe || xf86DoConfigure;
 
2274
}
 
2275
 
 
2276
 
 
2277
Bool
 
2278
xf86ServerIsOnlyProbing(void)
 
2279
{
 
2280
    return xf86ProbeOnly;
 
2281
}
 
2282
 
 
2283
 
 
2284
Bool
 
2285
xf86CaughtSignal()
 
2286
{
 
2287
    return xf86Info.caughtSignal;
 
2288
}
 
2289
 
 
2290
 
 
2291
Bool
 
2292
xf86GetVidModeAllowNonLocal()
 
2293
{
 
2294
    return xf86Info.vidModeAllowNonLocal;
 
2295
}
 
2296
 
 
2297
 
 
2298
Bool
 
2299
xf86GetVidModeEnabled()
 
2300
{
 
2301
    return xf86Info.vidModeEnabled;
 
2302
}
 
2303
 
 
2304
Bool
 
2305
xf86GetModInDevAllowNonLocal()
 
2306
{
 
2307
    return xf86Info.miscModInDevAllowNonLocal;
 
2308
}
 
2309
 
 
2310
 
 
2311
Bool
 
2312
xf86GetModInDevEnabled()
 
2313
{
 
2314
    return xf86Info.miscModInDevEnabled;
 
2315
}
 
2316
 
 
2317
 
 
2318
Bool
 
2319
xf86GetAllowMouseOpenFail()
 
2320
{
 
2321
    return xf86Info.allowMouseOpenFail;
 
2322
}
 
2323
 
 
2324
 
 
2325
Bool
 
2326
xf86IsPc98()
 
2327
{
 
2328
#if defined(i386) || defined(__i386__)
 
2329
    return xf86Info.pc98;
 
2330
#else
 
2331
    return FALSE;
 
2332
#endif
 
2333
}
 
2334
 
 
2335
void
 
2336
xf86DisableRandR()
 
2337
{
 
2338
    xf86Info.disableRandR = TRUE;
 
2339
    xf86Info.randRFrom = X_PROBED;
 
2340
}
 
2341
 
 
2342
CARD32
 
2343
xf86GetVersion()
 
2344
{
 
2345
    return XF86_VERSION_CURRENT;
 
2346
}
 
2347
 
 
2348
CARD32
 
2349
xf86GetModuleVersion(pointer module)
 
2350
{
 
2351
#ifdef XFree86LOADER
 
2352
    return (CARD32)LoaderGetModuleVersion(module);
 
2353
#else
 
2354
    return 0;
 
2355
#endif
 
2356
}
 
2357
 
 
2358
pointer
 
2359
xf86LoadDrvSubModule(DriverPtr drv, const char *name)
 
2360
{
 
2361
#ifdef XFree86LOADER
 
2362
    pointer ret;
 
2363
    int errmaj = 0, errmin = 0;
 
2364
 
 
2365
    ret = LoadSubModule(drv->module, name, NULL, NULL, NULL, NULL,
 
2366
                        &errmaj, &errmin);
 
2367
    if (!ret)
 
2368
        LoaderErrorMsg(NULL, name, errmaj, errmin);
 
2369
    return ret;
 
2370
#else
 
2371
    return (pointer)1;
 
2372
#endif
 
2373
}
 
2374
 
 
2375
pointer
 
2376
xf86LoadSubModule(ScrnInfoPtr pScrn, const char *name)
 
2377
{
 
2378
#ifdef XFree86LOADER
 
2379
    pointer ret;
 
2380
    int errmaj = 0, errmin = 0;
 
2381
 
 
2382
    ret = LoadSubModule(pScrn->module, name, NULL, NULL, NULL, NULL,
 
2383
                        &errmaj, &errmin);
 
2384
    if (!ret)
 
2385
        LoaderErrorMsg(pScrn->name, name, errmaj, errmin);
 
2386
    return ret;
 
2387
#else
 
2388
    return (pointer)1;
 
2389
#endif
 
2390
}
 
2391
 
 
2392
/*
 
2393
 * xf86LoadOneModule loads a single module.
 
2394
 */             
 
2395
pointer
 
2396
xf86LoadOneModule(char *name, pointer opt)
 
2397
{
 
2398
#ifdef XFree86LOADER
 
2399
    int errmaj, errmin;
 
2400
#endif
 
2401
    char *Name;
 
2402
    pointer mod;
 
2403
    
 
2404
    if (!name)
 
2405
        return NULL;
 
2406
    
 
2407
#ifndef NORMALISE_MODULE_NAME
 
2408
    Name = xstrdup(name);
 
2409
#else
 
2410
    /* Normalise the module name */
 
2411
    Name = xf86NormalizeName(name);
 
2412
#endif
 
2413
 
 
2414
    /* Skip empty names */
 
2415
    if (Name == NULL)
 
2416
        return NULL;
 
2417
    if (*Name == '\0') {
 
2418
        xfree(Name);
 
2419
        return NULL;
 
2420
    }
 
2421
 
 
2422
#ifdef XFree86LOADER
 
2423
    mod = LoadModule(Name, NULL, NULL, NULL, opt, NULL, &errmaj, &errmin);
 
2424
    if (!mod)
 
2425
        LoaderErrorMsg(NULL, Name, errmaj, errmin);
 
2426
#else
 
2427
    mod = (pointer)1;
 
2428
#endif
 
2429
    xfree(Name);
 
2430
    return mod;
 
2431
}
 
2432
 
 
2433
void
 
2434
xf86UnloadSubModule(pointer mod)
 
2435
{
 
2436
    /*
 
2437
     * This is disabled for now.  The loader isn't smart enough yet to undo
 
2438
     * relocations.
 
2439
     */
 
2440
#if defined(XFree86LOADER) && 0
 
2441
    UnloadSubModule(mod);
 
2442
#endif
 
2443
}
 
2444
 
 
2445
Bool
 
2446
xf86LoaderCheckSymbol(const char *name)
 
2447
{
 
2448
#ifdef XFree86LOADER
 
2449
    return LoaderSymbol(name) != NULL;
 
2450
#else
 
2451
    return TRUE;
 
2452
#endif
 
2453
}
 
2454
 
 
2455
void
 
2456
xf86LoaderReqSymLists(const char **list0, ...)
 
2457
{
 
2458
#ifdef XFree86LOADER
 
2459
    va_list ap;
 
2460
 
 
2461
    va_start(ap, list0);
 
2462
    LoaderVReqSymLists(list0, ap);
 
2463
    va_end(ap);
 
2464
#endif
 
2465
}
 
2466
 
 
2467
void
 
2468
xf86LoaderReqSymbols(const char *sym0, ...)
 
2469
{
 
2470
#ifdef XFree86LOADER
 
2471
    va_list ap;
 
2472
 
 
2473
    va_start(ap, sym0);
 
2474
    LoaderVReqSymbols(sym0, ap);
 
2475
    va_end(ap);
 
2476
#endif
 
2477
}
 
2478
 
 
2479
void
 
2480
xf86LoaderRefSymLists(const char **list0, ...)
 
2481
{
 
2482
#ifdef XFree86LOADER
 
2483
    va_list ap;
 
2484
 
 
2485
    va_start(ap, list0);
 
2486
    LoaderVRefSymLists(list0, ap);
 
2487
    va_end(ap);
 
2488
#endif
 
2489
}
 
2490
 
 
2491
void
 
2492
xf86LoaderRefSymbols(const char *sym0, ...)
 
2493
{
 
2494
#ifdef XFree86LOADER
 
2495
    va_list ap;
 
2496
 
 
2497
    va_start(ap, sym0);
 
2498
    LoaderVRefSymbols(sym0, ap);
 
2499
    va_end(ap);
 
2500
#endif
 
2501
}
 
2502
 
 
2503
 
 
2504
typedef enum {
 
2505
   OPTION_BACKING_STORE
 
2506
} BSOpts;
 
2507
 
 
2508
static const OptionInfoRec BSOptions[] = {
 
2509
   { OPTION_BACKING_STORE, "BackingStore", OPTV_BOOLEAN, {0}, FALSE },
 
2510
   { -1,                   NULL,           OPTV_NONE,    {0}, FALSE }
 
2511
};
 
2512
 
 
2513
void 
 
2514
xf86SetBackingStore(ScreenPtr pScreen)
 
2515
{
 
2516
    Bool useBS = FALSE;
 
2517
    MessageType from = X_DEFAULT;
 
2518
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
 
2519
    OptionInfoPtr options;
 
2520
 
 
2521
    options = xnfalloc(sizeof(BSOptions));
 
2522
    (void)memcpy(options, BSOptions, sizeof(BSOptions));
 
2523
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
 
2524
 
 
2525
    /* check for commandline option here */
 
2526
    if (xf86bsEnableFlag) {
 
2527
        from = X_CMDLINE;
 
2528
        useBS = TRUE;
 
2529
    } else if (xf86bsDisableFlag) {
 
2530
        from = X_CMDLINE;
 
2531
        useBS = FALSE;
 
2532
    } else {
 
2533
        if (xf86GetOptValBool(options, OPTION_BACKING_STORE, &useBS))
 
2534
            from = X_CONFIG;
 
2535
    }
 
2536
    xfree(options);
 
2537
    pScreen->backingStoreSupport = useBS ? Always : NotUseful;
 
2538
    if (serverGeneration == 1)
 
2539
        xf86DrvMsg(pScreen->myNum, from, "Backing store %s\n",
 
2540
                   useBS ? "enabled" : "disabled");
 
2541
}
 
2542
 
 
2543
 
 
2544
typedef enum {
 
2545
   OPTION_SILKEN_MOUSE
 
2546
} SMOpts;
 
2547
 
 
2548
static const OptionInfoRec SMOptions[] = {
 
2549
   { OPTION_SILKEN_MOUSE, "SilkenMouse",   OPTV_BOOLEAN, {0}, FALSE },
 
2550
   { -1,                   NULL,           OPTV_NONE,    {0}, FALSE }
 
2551
};
 
2552
 
 
2553
void 
 
2554
xf86SetSilkenMouse (ScreenPtr pScreen)
 
2555
{
 
2556
    Bool useSM = TRUE;
 
2557
    MessageType from = X_DEFAULT;
 
2558
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
 
2559
    OptionInfoPtr options;
 
2560
 
 
2561
    options = xnfalloc(sizeof(SMOptions));
 
2562
    (void)memcpy(options, SMOptions, sizeof(SMOptions));
 
2563
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
 
2564
    
 
2565
    /* check for commandline option here */
 
2566
    /* disable if screen shares resources */
 
2567
    if (((pScrn->racMemFlags & RAC_CURSOR) && 
 
2568
         !xf86NoSharedResources(pScrn->scrnIndex,MEM)) ||
 
2569
        ((pScrn->racIoFlags & RAC_CURSOR) && 
 
2570
         !xf86NoSharedResources(pScrn->scrnIndex,IO))) {
 
2571
        useSM = FALSE;
 
2572
        from = X_PROBED;
 
2573
    } else if (xf86silkenMouseDisableFlag) {
 
2574
        from = X_CMDLINE;
 
2575
        useSM = FALSE;
 
2576
    } else {
 
2577
        if (xf86GetOptValBool(options, OPTION_SILKEN_MOUSE, &useSM))
 
2578
            from = X_CONFIG;
 
2579
    }
 
2580
    xfree(options);
 
2581
    /*
 
2582
     * XXX quick hack to report correctly for OSs that can't do SilkenMouse
 
2583
     * yet.  Should handle this differently so that alternate async methods
 
2584
     * like Xqueue work correctly with this too.
 
2585
     */
 
2586
    pScrn->silkenMouse = useSM && xf86SIGIOSupported();
 
2587
    if (serverGeneration == 1)
 
2588
        xf86DrvMsg(pScreen->myNum, from, "Silken mouse %s\n",
 
2589
                   pScrn->silkenMouse ? "enabled" : "disabled");
 
2590
}
 
2591
 
 
2592
/* Wrote this function for the PM2 Xv driver, preliminary. */
 
2593
 
 
2594
pointer
 
2595
xf86FindXvOptions(int scrnIndex, int adaptor_index, char *port_name,
 
2596
                  char **adaptor_name, pointer *adaptor_options)
 
2597
{
 
2598
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
 
2599
    confXvAdaptorPtr adaptor;
 
2600
    int i;
 
2601
 
 
2602
    if (adaptor_index >= pScrn->confScreen->numxvadaptors) {
 
2603
        if (adaptor_name) *adaptor_name = NULL;
 
2604
        if (adaptor_options) *adaptor_options = NULL;
 
2605
        return NULL;
 
2606
    }
 
2607
 
 
2608
    adaptor = &pScrn->confScreen->xvadaptors[adaptor_index];
 
2609
    if (adaptor_name) *adaptor_name = adaptor->identifier;
 
2610
    if (adaptor_options) *adaptor_options = adaptor->options;
 
2611
 
 
2612
    for (i = 0; i < adaptor->numports; i++)
 
2613
        if (!xf86NameCmp(adaptor->ports[i].identifier, port_name))
 
2614
            return adaptor->ports[i].options;
 
2615
 
 
2616
    return NULL;
 
2617
}
 
2618
 
 
2619
/* Rather than duplicate loader's get OS function, just include it directly */
 
2620
#define LoaderGetOS xf86GetOS
 
2621
#include "loader/os.c"
 
2622
 
 
2623
/* new RAC */
 
2624
/*
 
2625
 * xf86ConfigIsa/PciEntity() -- These helper functions assign an
 
2626
 * active entity to a screen, registers its fixed resources, assign
 
2627
 * special enter/leave functions and their private scratch area to
 
2628
 * this entity, take the dog for a walk...
 
2629
 */
 
2630
ScrnInfoPtr
 
2631
xf86ConfigIsaEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex,
 
2632
                          IsaChipsets *i_chip, resList res, EntityProc init,
 
2633
                          EntityProc enter, EntityProc leave, pointer private)
 
2634
{
 
2635
    IsaChipsets *i_id;
 
2636
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
 
2637
    if (!pEnt) return pScrn;
 
2638
    
 
2639
    if (!(pEnt->location.type == BUS_ISA)) {
 
2640
        xfree(pEnt);
 
2641
        return pScrn;
 
2642
    }
 
2643
 
 
2644
    if (!pEnt->active) {
 
2645
        xf86ConfigIsaEntityInactive(pEnt, i_chip, res, init,  enter,
 
2646
                                    leave,  private);
 
2647
        return pScrn;
 
2648
    }
 
2649
 
 
2650
    if (!pScrn)
 
2651
        pScrn = xf86AllocateScreen(pEnt->driver,scrnFlag); 
 
2652
    xf86AddEntityToScreen(pScrn,entityIndex);
 
2653
 
 
2654
    if (i_chip) {
 
2655
        for (i_id = i_chip; i_id->numChipset != -1; i_id++) {
 
2656
            if (pEnt->chipset == i_id->numChipset) break;
 
2657
        }
 
2658
        xf86ClaimFixedResources(i_id->resList,entityIndex);
 
2659
    }
 
2660
    xfree(pEnt);
 
2661
    xf86ClaimFixedResources(res,entityIndex);
 
2662
    xf86SetEntityFuncs(entityIndex,init,enter,leave,private);
 
2663
 
 
2664
    return pScrn;
 
2665
}
 
2666
 
 
2667
ScrnInfoPtr
 
2668
xf86ConfigPciEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex,
 
2669
                          PciChipsets *p_chip, resList res, EntityProc init,
 
2670
                          EntityProc enter, EntityProc leave, pointer private)
 
2671
{
 
2672
    PciChipsets *p_id;
 
2673
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
 
2674
    if (!pEnt) return pScrn;
 
2675
 
 
2676
    if (!(pEnt->location.type == BUS_PCI) 
 
2677
        || !xf86GetPciInfoForEntity(entityIndex)) {
 
2678
        xfree(pEnt);
 
2679
        return pScrn;
 
2680
    }
 
2681
    if (!pEnt->active) {
 
2682
        xf86ConfigPciEntityInactive(pEnt, p_chip, res, init,  enter,
 
2683
                                    leave,  private);
 
2684
        return pScrn;
 
2685
    }
 
2686
 
 
2687
    if (!pScrn)
 
2688
        pScrn = xf86AllocateScreen(pEnt->driver,scrnFlag);
 
2689
    if (xf86IsEntitySharable(entityIndex)) {
 
2690
        xf86SetEntityShared(entityIndex);
 
2691
    }
 
2692
    xf86AddEntityToScreen(pScrn,entityIndex);
 
2693
    if (xf86IsEntityShared(entityIndex)) {
 
2694
        return pScrn;
 
2695
    }
 
2696
    if (p_chip) {
 
2697
        for (p_id = p_chip; p_id->numChipset != -1; p_id++) {
 
2698
            if (pEnt->chipset == p_id->numChipset) break;
 
2699
        }
 
2700
        xf86ClaimFixedResources(p_id->resList,entityIndex);
 
2701
    }
 
2702
    xfree(pEnt);
 
2703
 
 
2704
    xf86ClaimFixedResources(res,entityIndex);
 
2705
    xf86SetEntityFuncs(entityIndex,init,enter,leave,private);
 
2706
 
 
2707
    return pScrn;
 
2708
}
 
2709
 
 
2710
ScrnInfoPtr
 
2711
xf86ConfigFbEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex,
 
2712
                   EntityProc init, EntityProc enter, EntityProc leave, 
 
2713
                   pointer private)
 
2714
{
 
2715
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
 
2716
    if (!pEnt) return pScrn;
 
2717
    
 
2718
    if (!(pEnt->location.type == BUS_NONE)) {
 
2719
        xfree(pEnt);
 
2720
        return pScrn;
 
2721
    }
 
2722
 
 
2723
    if (!pEnt->active) {
 
2724
        xf86ConfigFbEntityInactive(pEnt, init,  enter, leave,  private);
 
2725
        return pScrn;
 
2726
    }
 
2727
 
 
2728
    if (!pScrn)
 
2729
        pScrn = xf86AllocateScreen(pEnt->driver,scrnFlag); 
 
2730
    xf86AddEntityToScreen(pScrn,entityIndex);
 
2731
 
 
2732
    xf86SetEntityFuncs(entityIndex,init,enter,leave,private);
 
2733
 
 
2734
    return pScrn;
 
2735
}
 
2736
 
 
2737
/*
 
2738
 *
 
2739
 *  OBSOLETE ! xf86ConfigActiveIsaEntity() and xf86ConfigActivePciEntity()
 
2740
 *             are obsolete functions. They the are likely to be removed
 
2741
 *             Don't use!
 
2742
 */
 
2743
Bool
 
2744
xf86ConfigActiveIsaEntity(ScrnInfoPtr pScrn, int entityIndex,
 
2745
                          IsaChipsets *i_chip, resList res, EntityProc init,
 
2746
                          EntityProc enter, EntityProc leave, pointer private)
 
2747
{
 
2748
    IsaChipsets *i_id;
 
2749
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
 
2750
    if (!pEnt) return FALSE;
 
2751
 
 
2752
    if (!pEnt->active || !(pEnt->location.type == BUS_ISA)) {
 
2753
        xfree(pEnt);
 
2754
        return FALSE;
 
2755
    }
 
2756
 
 
2757
    xf86AddEntityToScreen(pScrn,entityIndex);
 
2758
 
 
2759
    if (i_chip) {
 
2760
        for (i_id = i_chip; i_id->numChipset != -1; i_id++) {
 
2761
            if (pEnt->chipset == i_id->numChipset) break;
 
2762
        }
 
2763
        xf86ClaimFixedResources(i_id->resList,entityIndex);
 
2764
    }
 
2765
    xfree(pEnt);
 
2766
    xf86ClaimFixedResources(res,entityIndex);
 
2767
    if (!xf86SetEntityFuncs(entityIndex,init,enter,leave,private))
 
2768
        return FALSE;
 
2769
 
 
2770
    return TRUE;
 
2771
}
 
2772
 
 
2773
Bool
 
2774
xf86ConfigActivePciEntity(ScrnInfoPtr pScrn, int entityIndex,
 
2775
                          PciChipsets *p_chip, resList res, EntityProc init,
 
2776
                          EntityProc enter, EntityProc leave, pointer private)
 
2777
{
 
2778
    PciChipsets *p_id;
 
2779
    EntityInfoPtr pEnt = xf86GetEntityInfo(entityIndex);
 
2780
    if (!pEnt) return FALSE;
 
2781
 
 
2782
    if (!pEnt->active || !(pEnt->location.type == BUS_PCI)) {
 
2783
        xfree(pEnt);
 
2784
        return FALSE;
 
2785
    }
 
2786
    xf86AddEntityToScreen(pScrn,entityIndex);
 
2787
 
 
2788
    if (p_chip) {
 
2789
        for (p_id = p_chip; p_id->numChipset != -1; p_id++) {
 
2790
            if (pEnt->chipset == p_id->numChipset) break;
 
2791
        }
 
2792
        xf86ClaimFixedResources(p_id->resList,entityIndex);
 
2793
    }
 
2794
    xfree(pEnt);
 
2795
 
 
2796
    xf86ClaimFixedResources(res,entityIndex);
 
2797
    if (!xf86SetEntityFuncs(entityIndex,init,enter,leave,private))
 
2798
        return FALSE;
 
2799
 
 
2800
    return TRUE;
 
2801
}
 
2802
 
 
2803
/*
 
2804
 * xf86ConfigPci/IsaEntityInactive() -- These functions can be used
 
2805
 * to configure an inactive entity as well as to reconfigure an
 
2806
 * previously active entity inactive. If the entity has been
 
2807
 * assigned to a screen before it will be removed. If p_pci(p_isa) is
 
2808
 * non-NULL all static resources listed there will be registered.
 
2809
 */
 
2810
void
 
2811
xf86ConfigPciEntityInactive(EntityInfoPtr pEnt, PciChipsets *p_chip,
 
2812
                            resList res, EntityProc init, EntityProc enter,
 
2813
                            EntityProc leave, pointer private)
 
2814
{
 
2815
    PciChipsets *p_id;
 
2816
    ScrnInfoPtr pScrn;
 
2817
 
 
2818
    if ((pScrn = xf86FindScreenForEntity(pEnt->index)))
 
2819
        xf86RemoveEntityFromScreen(pScrn,pEnt->index);
 
2820
    else if (p_chip) {
 
2821
        for (p_id = p_chip; p_id->numChipset != -1; p_id++) {
 
2822
            if (pEnt->chipset == p_id->numChipset) break;
 
2823
        }
 
2824
        xf86ClaimFixedResources(p_id->resList,pEnt->index);
 
2825
    }
 
2826
    xf86ClaimFixedResources(res,pEnt->index);
 
2827
    /* shared resources are only needed when entity is active: remove */
 
2828
    xf86DeallocateResourcesForEntity(pEnt->index, ResShared);
 
2829
    xf86SetEntityFuncs(pEnt->index,init,enter,leave,private);
 
2830
}
 
2831
 
 
2832
void
 
2833
xf86ConfigIsaEntityInactive(EntityInfoPtr pEnt, IsaChipsets *i_chip,
 
2834
                            resList res, EntityProc init, EntityProc enter,
 
2835
                            EntityProc leave, pointer private)
 
2836
{
 
2837
    IsaChipsets *i_id;
 
2838
    ScrnInfoPtr pScrn;
 
2839
 
 
2840
    if ((pScrn = xf86FindScreenForEntity(pEnt->index)))
 
2841
        xf86RemoveEntityFromScreen(pScrn,pEnt->index);
 
2842
    else if (i_chip) {
 
2843
        for (i_id = i_chip; i_id->numChipset != -1; i_id++) {
 
2844
            if (pEnt->chipset == i_id->numChipset) break;
 
2845
        }
 
2846
        xf86ClaimFixedResources(i_id->resList,pEnt->index);
 
2847
    }
 
2848
    xf86ClaimFixedResources(res,pEnt->index);
 
2849
    /* shared resources are only needed when entity is active: remove */
 
2850
    xf86DeallocateResourcesForEntity(pEnt->index, ResShared);
 
2851
    xf86SetEntityFuncs(pEnt->index,init,enter,leave,private);
 
2852
}
 
2853
 
 
2854
void
 
2855
xf86ConfigFbEntityInactive(EntityInfoPtr pEnt, EntityProc init, 
 
2856
                           EntityProc enter, EntityProc leave, pointer private)
 
2857
{
 
2858
    ScrnInfoPtr pScrn;
 
2859
 
 
2860
    if ((pScrn = xf86FindScreenForEntity(pEnt->index)))
 
2861
        xf86RemoveEntityFromScreen(pScrn,pEnt->index);
 
2862
    xf86SetEntityFuncs(pEnt->index,init,enter,leave,private);
 
2863
}
 
2864
 
 
2865
Bool
 
2866
xf86IsScreenPrimary(int scrnIndex)
 
2867
{
 
2868
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
 
2869
    int i;
 
2870
 
 
2871
    for (i=0 ; i < pScrn->numEntities; i++) {
 
2872
        if (xf86IsEntityPrimary(i))
 
2873
            return TRUE;
 
2874
    }
 
2875
    return FALSE;
 
2876
}
 
2877
 
 
2878
int
 
2879
xf86RegisterRootWindowProperty(int ScrnIndex, Atom property, Atom type,
 
2880
                               int format, unsigned long len, pointer value )
 
2881
{
 
2882
    RootWinPropPtr pNewProp = NULL, pRegProp;
 
2883
    int i;
 
2884
    Bool existing = FALSE;
 
2885
 
 
2886
#ifdef DEBUG
 
2887
    ErrorF("xf86RegisterRootWindowProperty(%d, %ld, %ld, %d, %ld, %p)\n",
 
2888
           ScrnIndex, property, type, format, len, value);
 
2889
#endif
 
2890
 
 
2891
    if (ScrnIndex<0 || ScrnIndex>=xf86NumScreens) {
 
2892
      return(BadMatch);
 
2893
    }
 
2894
 
 
2895
    if (xf86RegisteredPropertiesTable &&
 
2896
        xf86RegisteredPropertiesTable[ScrnIndex]) {
 
2897
      for (pNewProp = xf86RegisteredPropertiesTable[ScrnIndex];
 
2898
           pNewProp; pNewProp = pNewProp->next) {
 
2899
        if (strcmp(pNewProp->name, NameForAtom(property)) == 0)
 
2900
          break;
 
2901
      }
 
2902
    }
 
2903
 
 
2904
    if (!pNewProp) {
 
2905
      if ((pNewProp = (RootWinPropPtr)xalloc(sizeof(RootWinProp))) == NULL) {
 
2906
        return(BadAlloc);
 
2907
      }
 
2908
      /*
 
2909
       * We will put this property at the end of the list so that
 
2910
       * the changes are made in the order they were requested.
 
2911
       */
 
2912
      pNewProp->next = NULL;
 
2913
    } else {
 
2914
      if (pNewProp->name)
 
2915
        xfree(pNewProp->name);
 
2916
      existing = TRUE;
 
2917
    }
 
2918
 
 
2919
    pNewProp->name = xnfstrdup(NameForAtom(property));
 
2920
    pNewProp->type = type;
 
2921
    pNewProp->format = format;
 
2922
    pNewProp->size = len;
 
2923
    pNewProp->data = value;
 
2924
 
 
2925
#ifdef DEBUG
 
2926
    ErrorF("new property filled\n");
 
2927
#endif
 
2928
 
 
2929
    if (NULL==xf86RegisteredPropertiesTable) {
 
2930
#ifdef DEBUG
 
2931
      ErrorF("creating xf86RegisteredPropertiesTable[] size %d\n",
 
2932
             xf86NumScreens);
 
2933
#endif
 
2934
      if ( NULL==(xf86RegisteredPropertiesTable=(RootWinPropPtr*)xnfcalloc(sizeof(RootWinProp),xf86NumScreens) )) {
 
2935
        return(BadAlloc);
 
2936
      }
 
2937
      for (i=0; i<xf86NumScreens; i++) {
 
2938
        xf86RegisteredPropertiesTable[i] = NULL;
 
2939
      }
 
2940
    }
 
2941
 
 
2942
#ifdef DEBUG
 
2943
    ErrorF("xf86RegisteredPropertiesTable %p\n",
 
2944
           (void *)xf86RegisteredPropertiesTable);
 
2945
    ErrorF("xf86RegisteredPropertiesTable[%d] %p\n",
 
2946
           ScrnIndex, (void *)xf86RegisteredPropertiesTable[ScrnIndex]);
 
2947
#endif
 
2948
 
 
2949
    if (!existing) {
 
2950
      if ( xf86RegisteredPropertiesTable[ScrnIndex] == NULL) {
 
2951
        xf86RegisteredPropertiesTable[ScrnIndex] = pNewProp;
 
2952
      } else {
 
2953
        pRegProp = xf86RegisteredPropertiesTable[ScrnIndex];
 
2954
        while (pRegProp->next != NULL) {
 
2955
#ifdef DEBUG
 
2956
          ErrorF("- next %p\n", (void *)pRegProp);
 
2957
#endif
 
2958
          pRegProp = pRegProp->next;
 
2959
        }
 
2960
        pRegProp->next = pNewProp;
 
2961
      }
 
2962
    }
 
2963
#ifdef DEBUG
 
2964
    ErrorF("xf86RegisterRootWindowProperty succeeded\n");
 
2965
#endif
 
2966
    return(Success);    
 
2967
}
 
2968
 
 
2969
Bool
 
2970
xf86IsUnblank(int mode)
 
2971
{
 
2972
    switch(mode) {
 
2973
    case SCREEN_SAVER_OFF:
 
2974
    case SCREEN_SAVER_FORCER:
 
2975
        return TRUE;
 
2976
    case SCREEN_SAVER_ON:
 
2977
    case SCREEN_SAVER_CYCLE:
 
2978
        return FALSE;
 
2979
    default:
 
2980
        xf86MsgVerb(X_WARNING, 0, "Unexpected save screen mode: %d\n", mode);
 
2981
        return TRUE;
 
2982
    }
 
2983
}