micmap.c 17.2 KB
Newer Older
1
/*
2
 * Copyright (c) 1987, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
Kaleb Keithley's avatar
Kaleb Keithley committed
23 24 25 26 27 28 29

/*
 * This is based on cfbcmap.c.  The functions here are useful independently
 * of cfb, which is the reason for including them here.  How "mi" these
 * are may be debatable.
 */

30 31 32 33
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

34 35
#include <X11/X.h>
#include <X11/Xproto.h>
Kaleb Keithley's avatar
Kaleb Keithley committed
36 37 38 39 40 41
#include "scrnintstr.h"
#include "colormapst.h"
#include "resource.h"
#include "globals.h"
#include "micmap.h"

42
DevPrivateKeyRec micmapScrPrivateKeyRec;
Kaleb Keithley's avatar
Kaleb Keithley committed
43

44
int
45
miListInstalledColormaps(ScreenPtr pScreen, Colormap * pmaps)
Kaleb Keithley's avatar
Kaleb Keithley committed
46
{
47
    if (GetInstalledmiColormap(pScreen)) {
48 49
        *pmaps = GetInstalledmiColormap(pScreen)->mid;
        return 1;
50 51
    }
    return 0;
Kaleb Keithley's avatar
Kaleb Keithley committed
52 53
}

54
void
Kaleb Keithley's avatar
Kaleb Keithley committed
55 56
miInstallColormap(ColormapPtr pmap)
{
57
    ColormapPtr oldpmap = GetInstalledmiColormap(pmap->pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
58

59 60 61 62 63 64 65 66
    if (pmap != oldpmap) {
        /* Uninstall pInstalledMap. No hardware changes required, just
         * notify all interested parties. */
        if (oldpmap != (ColormapPtr) None)
            WalkTree(pmap->pScreen, TellLostMap, (char *) &oldpmap->mid);
        /* Install pmap */
        SetInstalledmiColormap(pmap->pScreen, pmap);
        WalkTree(pmap->pScreen, TellGainedMap, (char *) &pmap->mid);
Kaleb Keithley's avatar
Kaleb Keithley committed
67 68 69 70

    }
}

71
void
Kaleb Keithley's avatar
Kaleb Keithley committed
72 73
miUninstallColormap(ColormapPtr pmap)
{
74
    ColormapPtr curpmap = GetInstalledmiColormap(pmap->pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
75

76 77
    if (pmap == curpmap) {
        if (pmap->mid != pmap->pScreen->defColormap) {
78
            dixLookupResourceByType((void **) &curpmap,
79 80 81 82
                                    pmap->pScreen->defColormap,
                                    RT_COLORMAP, serverClient, DixUseAccess);
            (*pmap->pScreen->InstallColormap) (curpmap);
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
83 84 85
    }
}

86
void
Kaleb Keithley's avatar
Kaleb Keithley committed
87
miResolveColor(unsigned short *pred, unsigned short *pgreen,
88
               unsigned short *pblue, VisualPtr pVisual)
Kaleb Keithley's avatar
Kaleb Keithley committed
89 90 91 92
{
    int shift = 16 - pVisual->bitsPerRGBValue;
    unsigned lim = (1 << pVisual->bitsPerRGBValue) - 1;

93 94 95 96
    if ((pVisual->class | DynamicClass) == GrayScale) {
        /* rescale to gray then rgb bits */
        *pred = (30L * *pred + 59L * *pgreen + 11L * *pblue) / 100;
        *pblue = *pgreen = *pred = ((*pred >> shift) * 65535) / lim;
Kaleb Keithley's avatar
Kaleb Keithley committed
97
    }
98 99 100 101 102
    else {
        /* rescale to rgb bits */
        *pred = ((*pred >> shift) * 65535) / lim;
        *pgreen = ((*pgreen >> shift) * 65535) / lim;
        *pblue = ((*pblue >> shift) * 65535) / lim;
Kaleb Keithley's avatar
Kaleb Keithley committed
103 104 105
    }
}

106
Bool
Kaleb Keithley's avatar
Kaleb Keithley committed
107 108
miInitializeColormap(ColormapPtr pmap)
{
109 110
    unsigned i;
    VisualPtr pVisual;
Kaleb Keithley's avatar
Kaleb Keithley committed
111 112 113 114 115 116
    unsigned lim, maxent, shift;

    pVisual = pmap->pVisual;
    lim = (1 << pVisual->bitsPerRGBValue) - 1;
    shift = 16 - pVisual->bitsPerRGBValue;
    maxent = pVisual->ColormapEntries - 1;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    if (pVisual->class == TrueColor) {
        unsigned limr, limg, limb;

        limr = pVisual->redMask >> pVisual->offsetRed;
        limg = pVisual->greenMask >> pVisual->offsetGreen;
        limb = pVisual->blueMask >> pVisual->offsetBlue;
        for (i = 0; i <= maxent; i++) {
            /* rescale to [0..65535] then rgb bits */
            pmap->red[i].co.local.red =
                ((((i * 65535) / limr) >> shift) * 65535) / lim;
            pmap->green[i].co.local.green =
                ((((i * 65535) / limg) >> shift) * 65535) / lim;
            pmap->blue[i].co.local.blue =
                ((((i * 65535) / limb) >> shift) * 65535) / lim;
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
132
    }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    else if (pVisual->class == StaticColor) {
        unsigned limr, limg, limb;

        limr = pVisual->redMask >> pVisual->offsetRed;
        limg = pVisual->greenMask >> pVisual->offsetGreen;
        limb = pVisual->blueMask >> pVisual->offsetBlue;
        for (i = 0; i <= maxent; i++) {
            /* rescale to [0..65535] then rgb bits */
            pmap->red[i].co.local.red =
                ((((((i & pVisual->redMask) >> pVisual->offsetRed)
                    * 65535) / limr) >> shift) * 65535) / lim;
            pmap->red[i].co.local.green =
                ((((((i & pVisual->greenMask) >> pVisual->offsetGreen)
                    * 65535) / limg) >> shift) * 65535) / lim;
            pmap->red[i].co.local.blue =
                ((((((i & pVisual->blueMask) >> pVisual->offsetBlue)
                    * 65535) / limb) >> shift) * 65535) / lim;
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
151
    }
152 153 154 155 156 157 158 159
    else if (pVisual->class == StaticGray) {
        for (i = 0; i <= maxent; i++) {
            /* rescale to [0..65535] then rgb bits */
            pmap->red[i].co.local.red = ((((i * 65535) / maxent) >> shift)
                                         * 65535) / lim;
            pmap->red[i].co.local.green = pmap->red[i].co.local.red;
            pmap->red[i].co.local.blue = pmap->red[i].co.local.red;
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    }
    return TRUE;
}

/* When simulating DirectColor on PseudoColor hardware, multiple
   entries of the colormap must be updated
 */

#define AddElement(mask) { \
    pixel = red | green | blue; \
    for (i = 0; i < nresult; i++) \
  	if (outdefs[i].pixel == pixel) \
    	    break; \
    if (i == nresult) \
    { \
   	nresult++; \
	outdefs[i].pixel = pixel; \
	outdefs[i].flags = 0; \
    } \
    outdefs[i].flags |= (mask); \
    outdefs[i].red = pmap->red[red >> pVisual->offsetRed].co.local.red; \
    outdefs[i].green = pmap->green[green >> pVisual->offsetGreen].co.local.green; \
    outdefs[i].blue = pmap->blue[blue >> pVisual->offsetBlue].co.local.blue; \
}

185
int
186 187
miExpandDirectColors(ColormapPtr pmap, int ndef, xColorItem * indefs,
                     xColorItem * outdefs)
Kaleb Keithley's avatar
Kaleb Keithley committed
188
{
189 190 191 192 193 194 195
    int red, green, blue;
    int maxred, maxgreen, maxblue;
    int stepred, stepgreen, stepblue;
    VisualPtr pVisual;
    int pixel;
    int nresult;
    int i;
Kaleb Keithley's avatar
Kaleb Keithley committed
196 197 198 199 200 201 202 203 204 205

    pVisual = pmap->pVisual;

    stepred = 1 << pVisual->offsetRed;
    stepgreen = 1 << pVisual->offsetGreen;
    stepblue = 1 << pVisual->offsetBlue;
    maxred = pVisual->redMask;
    maxgreen = pVisual->greenMask;
    maxblue = pVisual->blueMask;
    nresult = 0;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    for (; ndef--; indefs++) {
        if (indefs->flags & DoRed) {
            red = indefs->pixel & pVisual->redMask;
            for (green = 0; green <= maxgreen; green += stepgreen) {
                for (blue = 0; blue <= maxblue; blue += stepblue) {
                    AddElement(DoRed)
                }
            }
        }
        if (indefs->flags & DoGreen) {
            green = indefs->pixel & pVisual->greenMask;
            for (red = 0; red <= maxred; red += stepred) {
                for (blue = 0; blue <= maxblue; blue += stepblue) {
                    AddElement(DoGreen)
                }
            }
        }
        if (indefs->flags & DoBlue) {
            blue = indefs->pixel & pVisual->blueMask;
            for (red = 0; red <= maxred; red += stepred) {
                for (green = 0; green <= maxgreen; green += stepgreen) {
                    AddElement(DoBlue)
                }
            }
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
231 232 233 234
    }
    return nresult;
}

235
Bool
Kaleb Keithley's avatar
Kaleb Keithley committed
236 237
miCreateDefColormap(ScreenPtr pScreen)
{
238
    unsigned short zero = 0, ones = 0xFFFF;
Kaleb Keithley's avatar
Kaleb Keithley committed
239
    Pixel wp, bp;
240 241
    VisualPtr pVisual;
    ColormapPtr cmap;
Kaleb Keithley's avatar
Kaleb Keithley committed
242
    int alloctype;
243

244
    if (!dixRegisterPrivateKey(&micmapScrPrivateKeyRec, PRIVATE_SCREEN, 0))
245
        return FALSE;
246

Kaleb Keithley's avatar
Kaleb Keithley committed
247
    for (pVisual = pScreen->visuals;
248
         pVisual->vid != pScreen->rootVisual; pVisual++);
Kaleb Keithley's avatar
Kaleb Keithley committed
249 250

    if (pScreen->rootDepth == 1 || (pVisual->class & DynamicClass))
251
        alloctype = AllocNone;
Kaleb Keithley's avatar
Kaleb Keithley committed
252
    else
253
        alloctype = AllocAll;
Kaleb Keithley's avatar
Kaleb Keithley committed
254 255

    if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap,
256 257
                       alloctype, 0) != Success)
        return FALSE;
Kaleb Keithley's avatar
Kaleb Keithley committed
258 259

    if (pScreen->rootDepth > 1) {
260 261 262 263 264 265 266 267
        wp = pScreen->whitePixel;
        bp = pScreen->blackPixel;
        if ((AllocColor(cmap, &ones, &ones, &ones, &wp, 0) !=
             Success) ||
            (AllocColor(cmap, &zero, &zero, &zero, &bp, 0) != Success))
            return FALSE;
        pScreen->whitePixel = wp;
        pScreen->blackPixel = bp;
Kaleb Keithley's avatar
Kaleb Keithley committed
268 269
    }

270
    (*pScreen->InstallColormap) (cmap);
Kaleb Keithley's avatar
Kaleb Keithley committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    return TRUE;
}

/*
 * Default true color bitmasks, should be overridden by
 * driver
 */

#define _RZ(d) ((d + 2) / 3)
#define _RS(d) 0
#define _RM(d) ((1 << _RZ(d)) - 1)
#define _GZ(d) ((d - _RZ(d) + 1) / 2)
#define _GS(d) _RZ(d)
#define _GM(d) (((1 << _GZ(d)) - 1) << _GS(d))
#define _BZ(d) (d - _RZ(d) - _GZ(d))
#define _BS(d) (_RZ(d) + _GZ(d))
#define _BM(d) (((1 << _BZ(d)) - 1) << _BS(d))
#define _CE(d) (1 << _RZ(d))

typedef struct _miVisuals {
291 292 293 294 295 296 297
    struct _miVisuals *next;
    int depth;
    int bitsPerRGB;
    int visuals;
    int count;
    int preferredCVC;
    Pixel redMask, greenMask, blueMask;
Kaleb Keithley's avatar
Kaleb Keithley committed
298 299
} miVisualsRec, *miVisualsPtr;

300
static int miVisualPriority[] = {
Kaleb Keithley's avatar
Kaleb Keithley committed
301 302 303 304 305
    PseudoColor, GrayScale, StaticColor, TrueColor, DirectColor, StaticGray
};

#define NUM_PRIORITY	6

306
static miVisualsPtr miVisuals;
Kaleb Keithley's avatar
Kaleb Keithley committed
307

308
void
309
miClearVisualTypes(void)
Kaleb Keithley's avatar
Kaleb Keithley committed
310 311 312 313
{
    miVisualsPtr v;

    while ((v = miVisuals)) {
314 315
        miVisuals = v->next;
        free(v);
Kaleb Keithley's avatar
Kaleb Keithley committed
316 317 318
    }
}

319
Bool
320 321 322
miSetVisualTypesAndMasks(int depth, int visuals, int bitsPerRGB,
                         int preferredCVC,
                         Pixel redMask, Pixel greenMask, Pixel blueMask)
Kaleb Keithley's avatar
Kaleb Keithley committed
323
{
324 325
    miVisualsPtr new, *prev, v;
    int count;
Kaleb Keithley's avatar
Kaleb Keithley committed
326

327
    new = malloc(sizeof *new);
Kaleb Keithley's avatar
Kaleb Keithley committed
328
    if (!new)
329 330 331 332 333
        return FALSE;
    if (!redMask || !greenMask || !blueMask) {
        redMask = _RM(depth);
        greenMask = _GM(depth);
        blueMask = _BM(depth);
Kaleb Keithley's avatar
Kaleb Keithley committed
334 335 336 337 338 339 340 341 342 343 344
    }
    new->next = 0;
    new->depth = depth;
    new->visuals = visuals;
    new->bitsPerRGB = bitsPerRGB;
    new->preferredCVC = preferredCVC;
    new->redMask = redMask;
    new->greenMask = greenMask;
    new->blueMask = blueMask;
    count = (visuals >> 1) & 033333333333;
    count = visuals - count - ((count >> 1) & 033333333333);
345
    count = (((count + (count >> 3)) & 030707070707) % 077);    /* HAKMEM 169 */
Kaleb Keithley's avatar
Kaleb Keithley committed
346 347 348 349 350 351
    new->count = count;
    for (prev = &miVisuals; (v = *prev); prev = &v->next);
    *prev = new;
    return TRUE;
}

352
Bool
Kaleb Keithley's avatar
Kaleb Keithley committed
353 354
miSetVisualTypes(int depth, int visuals, int bitsPerRGB, int preferredCVC)
{
355 356
    return miSetVisualTypesAndMasks(depth, visuals, bitsPerRGB,
                                    preferredCVC, 0, 0, 0);
Kaleb Keithley's avatar
Kaleb Keithley committed
357 358
}

359
int
Kaleb Keithley's avatar
Kaleb Keithley committed
360 361 362
miGetDefaultVisualMask(int depth)
{
    if (depth > MAX_PSEUDO_DEPTH)
363
        return LARGE_VISUALS;
Kaleb Keithley's avatar
Kaleb Keithley committed
364
    else if (depth >= MIN_TRUE_DEPTH)
365
        return ALL_VISUALS;
Kaleb Keithley's avatar
Kaleb Keithley committed
366
    else if (depth == 1)
367
        return StaticGrayMask;
Kaleb Keithley's avatar
Kaleb Keithley committed
368
    else
369
        return SMALL_VISUALS;
Kaleb Keithley's avatar
Kaleb Keithley committed
370 371 372
}

static Bool
373
miVisualTypesSet(int depth)
Kaleb Keithley's avatar
Kaleb Keithley committed
374
{
375
    miVisualsPtr visuals;
Kaleb Keithley's avatar
Kaleb Keithley committed
376 377

    for (visuals = miVisuals; visuals; visuals = visuals->next)
378 379
        if (visuals->depth == depth)
            return TRUE;
Kaleb Keithley's avatar
Kaleb Keithley committed
380 381 382
    return FALSE;
}

383
Bool
384
miSetPixmapDepths(void)
Kaleb Keithley's avatar
Kaleb Keithley committed
385
{
386 387
    int d, f;

Kaleb Keithley's avatar
Kaleb Keithley committed
388
    /* Add any unlisted depths from the pixmap formats */
389 390 391 392 393 394
    for (f = 0; f < screenInfo.numPixmapFormats; f++) {
        d = screenInfo.formats[f].depth;
        if (!miVisualTypesSet(d)) {
            if (!miSetVisualTypes(d, 0, 0, -1))
                return FALSE;
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
395 396 397 398 399 400 401 402
    }
    return TRUE;
}

/*
 * Distance to least significant one bit
 */
static int
403
maskShift(Pixel p)
Kaleb Keithley's avatar
Kaleb Keithley committed
404
{
405
    int s;
Kaleb Keithley's avatar
Kaleb Keithley committed
406

407 408
    if (!p)
        return 0;
Kaleb Keithley's avatar
Kaleb Keithley committed
409
    s = 0;
410 411 412
    while (!(p & 1)) {
        s++;
        p >>= 1;
Kaleb Keithley's avatar
Kaleb Keithley committed
413 414 415 416 417 418 419 420 421 422
    }
    return s;
}

/*
 * Given a list of formats for a screen, create a list
 * of visuals and depths for the screen which corespond to
 * the set which can be used with this version of cfb.
 */

423
Bool
424 425 426
miInitVisuals(VisualPtr * visualp, DepthPtr * depthp, int *nvisualp,
              int *ndepthp, int *rootDepthp, VisualID * defaultVisp,
              unsigned long sizes, int bitsPerRGB, int preferredVis)
Kaleb Keithley's avatar
Kaleb Keithley committed
427
{
428 429 430 431 432 433 434 435 436 437 438 439
    int i, j = 0, k;
    VisualPtr visual;
    DepthPtr depth;
    VisualID *vid;
    int d, b;
    int f;
    int ndepth, nvisual;
    int nvtype;
    int vtype;
    miVisualsPtr visuals, nextVisuals;
    int *preferredCVCs, *prefp;
    int first_depth;
Kaleb Keithley's avatar
Kaleb Keithley committed
440 441

    /* none specified, we'll guess from pixmap formats */
442 443 444 445 446 447 448 449 450 451 452
    if (!miVisuals) {
        for (f = 0; f < screenInfo.numPixmapFormats; f++) {
            d = screenInfo.formats[f].depth;
            b = screenInfo.formats[f].bitsPerPixel;
            if (sizes & (1 << (b - 1)))
                vtype = miGetDefaultVisualMask(d);
            else
                vtype = 0;
            if (!miSetVisualTypes(d, vtype, bitsPerRGB, -1))
                return FALSE;
        }
Kaleb Keithley's avatar
Kaleb Keithley committed
453 454 455
    }
    nvisual = 0;
    ndepth = 0;
456 457 458 459
    for (visuals = miVisuals; visuals; visuals = nextVisuals) {
        nextVisuals = visuals->next;
        ndepth++;
        nvisual += visuals->count;
Kaleb Keithley's avatar
Kaleb Keithley committed
460
    }
461 462 463
    depth = xallocarray(ndepth, sizeof(DepthRec));
    visual = xallocarray(nvisual, sizeof(VisualRec));
    preferredCVCs = xallocarray(ndepth, sizeof(int));
464 465 466 467 468
    if (!depth || !visual || !preferredCVCs) {
        free(depth);
        free(visual);
        free(preferredCVCs);
        return FALSE;
Kaleb Keithley's avatar
Kaleb Keithley committed
469 470 471 472 473 474
    }
    *depthp = depth;
    *visualp = visual;
    *ndepthp = ndepth;
    *nvisualp = nvisual;
    prefp = preferredCVCs;
475 476 477 478 479 480 481 482 483
    for (visuals = miVisuals; visuals; visuals = nextVisuals) {
        nextVisuals = visuals->next;
        d = visuals->depth;
        vtype = visuals->visuals;
        nvtype = visuals->count;
        *prefp = visuals->preferredCVC;
        prefp++;
        vid = NULL;
        if (nvtype) {
484
            vid = xallocarray(nvtype, sizeof(VisualID));
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            if (!vid) {
                free(depth);
                free(visual);
                free(preferredCVCs);
                return FALSE;
            }
        }
        depth->depth = d;
        depth->numVids = nvtype;
        depth->vids = vid;
        depth++;
        for (i = 0; i < NUM_PRIORITY; i++) {
            if (!(vtype & (1 << miVisualPriority[i])))
                continue;
            visual->class = miVisualPriority[i];
            visual->bitsPerRGBValue = visuals->bitsPerRGB;
            visual->ColormapEntries = 1 << d;
            visual->nplanes = d;
            visual->vid = *vid = FakeClientID(0);
            switch (visual->class) {
            case PseudoColor:
            case GrayScale:
            case StaticGray:
                visual->redMask = 0;
                visual->greenMask = 0;
                visual->blueMask = 0;
                visual->offsetRed = 0;
                visual->offsetGreen = 0;
                visual->offsetBlue = 0;
                break;
            case DirectColor:
            case TrueColor:
                visual->ColormapEntries = _CE(d);
                /* fall through */
            case StaticColor:
                visual->redMask = visuals->redMask;
                visual->greenMask = visuals->greenMask;
                visual->blueMask = visuals->blueMask;
                visual->offsetRed = maskShift(visuals->redMask);
                visual->offsetGreen = maskShift(visuals->greenMask);
                visual->offsetBlue = maskShift(visuals->blueMask);
            }
            vid++;
            visual++;
        }
        free(visuals);
Kaleb Keithley's avatar
Kaleb Keithley committed
531 532 533 534
    }
    miVisuals = NULL;
    visual = *visualp;
    depth = *depthp;
535 536 537 538 539 540 541 542

    /*
     * if we did not supplyied by a preferred visual class
     * check if there is a preferred class in one of the depth
     * structures - if there is, we want to start looking for the
     * default visual/depth from that depth.
     */
    first_depth = 0;
543 544 545 546 547 548 549
    if (preferredVis < 0 && defaultColorVisualClass < 0) {
        for (i = 0; i < ndepth; i++) {
            if (preferredCVCs[i] >= 0) {
                first_depth = i;
                break;
            }
        }
550 551
    }

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    for (i = first_depth; i < ndepth; i++) {
        int prefColorVisualClass = -1;

        if (defaultColorVisualClass >= 0)
            prefColorVisualClass = defaultColorVisualClass;
        else if (preferredVis >= 0)
            prefColorVisualClass = preferredVis;
        else if (preferredCVCs[i] >= 0)
            prefColorVisualClass = preferredCVCs[i];

        if (*rootDepthp && *rootDepthp != depth[i].depth)
            continue;

        for (j = 0; j < depth[i].numVids; j++) {
            for (k = 0; k < nvisual; k++)
                if (visual[k].vid == depth[i].vids[j])
                    break;
            if (k == nvisual)
                continue;
            if (prefColorVisualClass < 0 ||
                visual[k].class == prefColorVisualClass)
                break;
        }
        if (j != depth[i].numVids)
            break;
Kaleb Keithley's avatar
Kaleb Keithley committed
577 578
    }
    if (i == ndepth) {
579 580
        i = 0;
        j = 0;
Kaleb Keithley's avatar
Kaleb Keithley committed
581 582 583
    }
    *rootDepthp = depth[i].depth;
    *defaultVisp = depth[i].vids[j];
584
    free(preferredCVCs);
Kaleb Keithley's avatar
Kaleb Keithley committed
585 586 587

    return TRUE;
}