XKBGeom.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8
/************************************************************
Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.

Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
9 10
documentation, and that the name of Silicon Graphics not be
used in advertising or publicity pertaining to distribution
11
of the software without specific prior written permission.
12
Silicon Graphics makes no representation about the suitability
13 14 15
of this software for any purpose. It is provided "as is"
without any express or implied warranty.

16 17
SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 20 21
GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 23 24 25 26 27 28 29 30 31 32 33 34 35
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

********************************************************/

#ifdef DEBUG
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#endif

#define NEED_MAP_READERS
#include "Xlibint.h"
36 37
#include <nx-X11/extensions/XKBgeom.h>
#include <nx-X11/extensions/XKBproto.h>
38 39 40 41 42 43 44 45 46 47 48 49
#include "XKBlibint.h"

#ifndef MINSHORT
#define	MINSHORT	-32768
#endif
#ifndef MAXSHORT
#define	MAXSHORT	32767
#endif

/***====================================================================***/

static void
50
_XkbCheckBounds(XkbBoundsPtr bounds, int x, int y)
51
{
52 53 54 55 56 57 58 59
    if (x < bounds->x1)
        bounds->x1 = x;
    if (x > bounds->x2)
        bounds->x2 = x;
    if (y < bounds->y1)
        bounds->y1 = y;
    if (y > bounds->y2)
        bounds->y2 = y;
60 61 62 63 64 65
    return;
}

Bool
XkbComputeShapeBounds(XkbShapePtr shape)
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    register int o, p;
    XkbOutlinePtr outline;
    XkbPointPtr pt;

    if ((!shape) || (shape->num_outlines < 1))
        return False;
    shape->bounds.x1 = shape->bounds.y1 = MAXSHORT;
    shape->bounds.x2 = shape->bounds.y2 = MINSHORT;
    for (outline = shape->outlines, o = 0; o < shape->num_outlines;
         o++, outline++) {
        for (pt = outline->points, p = 0; p < outline->num_points; p++, pt++) {
            _XkbCheckBounds(&shape->bounds, pt->x, pt->y);
        }
        if (outline->num_points < 2) {
            _XkbCheckBounds(&shape->bounds, 0, 0);
81
        }
82 83 84 85 86
    }
    return True;
}

Bool
87
XkbComputeShapeTop(XkbShapePtr shape, XkbBoundsPtr bounds)
88
{
89 90 91 92 93 94 95 96 97 98 99 100 101
    register int p;
    XkbOutlinePtr outline;
    XkbPointPtr pt;

    if ((!shape) || (shape->num_outlines < 1))
        return False;
    if (shape->approx)
        outline = shape->approx;
    else
        outline = &shape->outlines[shape->num_outlines - 1];
    if (outline->num_points < 2) {
        bounds->x1 = bounds->y1 = 0;
        bounds->x2 = bounds->y2 = 0;
102 103
    }
    else {
104 105
        bounds->x1 = bounds->y1 = MAXSHORT;
        bounds->x2 = bounds->y2 = MINSHORT;
106
    }
107 108
    for (pt = outline->points, p = 0; p < outline->num_points; p++, pt++) {
        _XkbCheckBounds(bounds, pt->x, pt->y);
109 110 111 112 113
    }
    return True;
}

Bool
114
XkbComputeRowBounds(XkbGeometryPtr geom, XkbSectionPtr section, XkbRowPtr row)
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    register int k, pos;
    XkbKeyPtr key;
    XkbBoundsPtr bounds, sbounds;

    if ((!geom) || (!section) || (!row))
        return False;
    bounds = &row->bounds;
    bzero(bounds, sizeof(XkbBoundsRec));
    for (key = row->keys, pos = k = 0; k < row->num_keys; k++, key++) {
        sbounds = &XkbKeyShape(geom, key)->bounds;
        _XkbCheckBounds(bounds, pos, 0);
        if (!row->vertical) {
            if (key->gap != 0) {
                pos += key->gap;
                _XkbCheckBounds(bounds, pos, 0);
            }
            _XkbCheckBounds(bounds, pos + sbounds->x1, sbounds->y1);
            _XkbCheckBounds(bounds, pos + sbounds->x2, sbounds->y2);
            pos += sbounds->x2;
        }
        else {
            if (key->gap != 0) {
                pos += key->gap;
                _XkbCheckBounds(bounds, 0, pos);
            }
            _XkbCheckBounds(bounds, pos + sbounds->x1, sbounds->y1);
            _XkbCheckBounds(bounds, pos + sbounds->x2, sbounds->y2);
            pos += sbounds->y2;
        }
145 146 147 148 149
    }
    return True;
}

Bool
150
XkbComputeSectionBounds(XkbGeometryPtr geom, XkbSectionPtr section)
151
{
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    register int i;
    XkbShapePtr shape;
    XkbRowPtr row;
    XkbDoodadPtr doodad;
    XkbBoundsPtr bounds, rbounds;

    if ((!geom) || (!section))
        return False;
    bounds = &section->bounds;
    bzero(bounds, sizeof(XkbBoundsRec));
    for (i = 0, row = section->rows; i < section->num_rows; i++, row++) {
        if (!XkbComputeRowBounds(geom, section, row))
            return False;
        rbounds = &row->bounds;
        _XkbCheckBounds(bounds, row->left + rbounds->x1,
                        row->top + rbounds->y1);
        _XkbCheckBounds(bounds, row->left + rbounds->x2,
                        row->top + rbounds->y2);
170
    }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    for (i = 0, doodad = section->doodads; i < section->num_doodads;
         i++, doodad++) {
        static XkbBoundsRec tbounds;

        switch (doodad->any.type) {
        case XkbOutlineDoodad:
        case XkbSolidDoodad:
            shape = XkbShapeDoodadShape(geom, &doodad->shape);
            rbounds = &shape->bounds;
            break;
        case XkbTextDoodad:
            tbounds.x1 = doodad->text.left;
            tbounds.y1 = doodad->text.top;
            tbounds.x2 = tbounds.x1 + doodad->text.width;
            tbounds.y2 = tbounds.y1 + doodad->text.height;
            rbounds = &tbounds;
            break;
        case XkbIndicatorDoodad:
            shape = XkbIndicatorDoodadShape(geom, &doodad->indicator);
            rbounds = &shape->bounds;
            break;
        case XkbLogoDoodad:
            shape = XkbLogoDoodadShape(geom, &doodad->logo);
            rbounds = &shape->bounds;
            break;
        default:
            tbounds.x1 = tbounds.x2 = doodad->any.left;
            tbounds.y1 = tbounds.y2 = doodad->any.top;
            rbounds = &tbounds;
            break;
        }
        _XkbCheckBounds(bounds, rbounds->x1, rbounds->y1);
        _XkbCheckBounds(bounds, rbounds->x2, rbounds->y2);
204 205 206 207 208 209 210
    }
    return True;
}

/***====================================================================***/

char *
211
XkbFindOverlayForKey(XkbGeometryPtr geom, XkbSectionPtr wanted, char *under)
212
{
213 214
    int s;
    XkbSectionPtr section;
215

216 217
    if ((geom == NULL) || (under == NULL) || (geom->num_sections < 1))
        return NULL;
218 219

    if (wanted)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        section = wanted;
    else
        section = geom->sections;

    for (s = 0; s < geom->num_sections; s++, section++) {
        XkbOverlayPtr ol;
        int o;

        if (section->num_overlays < 1)
            continue;
        for (o = 0, ol = section->overlays; o < section->num_overlays;
             o++, ol++) {
            XkbOverlayRowPtr row;
            int r;

            for (r = 0, row = ol->rows; r < ol->num_rows; r++, row++) {
                XkbOverlayKeyPtr key;
                int k;

                for (k = 0, key = row->keys; k < row->num_keys; k++, key++) {
                    if (strncmp(under, key->under.name, XkbKeyNameLength) == 0)
                        return key->over.name;
                }
            }
        }
        if (wanted != NULL)
            break;
247 248 249 250 251 252 253
    }
    return NULL;
}

/***====================================================================***/

static Status
254 255 256
_XkbReadGeomProperties(XkbReadBufferPtr buf,
                       XkbGeometryPtr geom,
                       xkbGetGeometryReply *rep)
257
{
258 259 260 261 262 263
    Status rtrn;

    if (rep->nProperties < 1)
        return Success;
    if ((rtrn = XkbAllocGeomProps(geom, rep->nProperties)) == Success) {
        register int i;
264
        register Bool ok = True;
265 266

        for (i = 0; (i < rep->nProperties) && ok; i++) {
267 268
            char *name = NULL;
            char *value = NULL;
269 270 271
            ok = _XkbGetReadBufferCountedString(buf, &name) && ok;
            ok = _XkbGetReadBufferCountedString(buf, &value) && ok;
            ok = ok && (XkbAddGeomProperty(geom, name, value) != NULL);
272 273 274

	    _XkbFree(name);
	    _XkbFree(value);
275 276 277 278 279
        }
        if (ok)
            rtrn = Success;
        else
            rtrn = BadLength;
280 281 282 283 284
    }
    return rtrn;
}

static Status
285 286 287
_XkbReadGeomKeyAliases(XkbReadBufferPtr buf,
                       XkbGeometryPtr geom,
                       xkbGetGeometryReply *rep)
288
{
289 290 291 292 293 294 295 296 297 298
    Status rtrn;

    if (rep->nKeyAliases < 1)
        return Success;
    if ((rtrn = XkbAllocGeomKeyAliases(geom, rep->nKeyAliases)) == Success) {
        if (!_XkbCopyFromReadBuffer(buf, (char *) geom->key_aliases,
                                    (rep->nKeyAliases * XkbKeyNameLength * 2)))
            return BadLength;
        geom->num_key_aliases = rep->nKeyAliases;
        return Success;
299
    }
300 301
    else {                      /* alloc failed, just skip the aliases */
        _XkbSkipReadBufferData(buf, (rep->nKeyAliases * XkbKeyNameLength * 2));
302 303 304 305 306
    }
    return rtrn;
}

static Status
307 308 309
_XkbReadGeomColors(XkbReadBufferPtr buf,
                   XkbGeometryPtr geom,
                   xkbGetGeometryReply *rep)
310
{
311 312 313 314 315 316 317 318
    Status rtrn;

    if (rep->nColors < 1)
        return Success;
    if ((rtrn = XkbAllocGeomColors(geom, rep->nColors)) == Success) {
        register int i;

        for (i = 0; i < rep->nColors; i++) {
319
            char *spec = NULL;
320 321 322 323
            if (!_XkbGetReadBufferCountedString(buf, &spec))
                rtrn = BadLength;
            else if (XkbAddGeomColor(geom, spec, geom->num_colors) == NULL)
                rtrn = BadAlloc;
324 325

            _XkbFree(spec);
326 327 328 329
            if (rtrn != Success)
                return rtrn;
        }
        return Success;
330 331 332 333 334
    }
    return rtrn;
}

static Status
335 336 337
_XkbReadGeomShapes(XkbReadBufferPtr buf,
                   XkbGeometryPtr geom,
                   xkbGetGeometryReply *rep)
338
{
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    register int i;
    Status rtrn;

    if (rep->nShapes < 1)
        return Success;
    if ((rtrn = XkbAllocGeomShapes(geom, rep->nShapes)) != Success)
        return rtrn;
    for (i = 0; i < rep->nShapes; i++) {
        xkbShapeWireDesc *shapeWire;
        XkbShapePtr shape;
        register int o;

        shapeWire = (xkbShapeWireDesc *)
            _XkbGetReadBufferPtr(buf, SIZEOF(xkbShapeWireDesc));
        if (!shapeWire)
            return BadLength;
        shape = XkbAddGeomShape(geom, shapeWire->name, shapeWire->nOutlines);
        if (!shape)
            return BadAlloc;
        for (o = 0; o < shapeWire->nOutlines; o++) {
            xkbOutlineWireDesc *olWire;
            XkbOutlinePtr ol;
            register int p;
            XkbPointPtr pt;

            olWire = (xkbOutlineWireDesc *)
                _XkbGetReadBufferPtr(buf, SIZEOF(xkbOutlineWireDesc));
            if (!olWire)
                return BadLength;
            ol = XkbAddGeomOutline(shape, olWire->nPoints);
            if (!ol)
                return BadAlloc;
            ol->corner_radius = olWire->cornerRadius;
            for (p = 0, pt = ol->points; p < olWire->nPoints; p++, pt++) {
                xkbPointWireDesc *ptWire;

                ptWire = (xkbPointWireDesc *)
                    _XkbGetReadBufferPtr(buf, SIZEOF(xkbPointWireDesc));
                if (!ptWire)
                    return BadLength;
                pt->x = ptWire->x;
                pt->y = ptWire->y;
            }
            ol->num_points = olWire->nPoints;
        }
        if ((shapeWire->primaryNdx != XkbNoShape) &&
            (shapeWire->primaryNdx < shapeWire->nOutlines))
            shape->primary = &shape->outlines[shapeWire->primaryNdx];
        else
            shape->primary = NULL;
        if ((shapeWire->approxNdx != XkbNoShape) &&
            (shapeWire->approxNdx < shapeWire->nOutlines))
            shape->approx = &shape->outlines[shapeWire->approxNdx];
        else
            shape->approx = NULL;
        XkbComputeShapeBounds(shape);
395 396 397 398 399
    }
    return Success;
}

static Status
400 401 402
_XkbReadGeomDoodad(XkbReadBufferPtr buf,
                   XkbGeometryPtr geom,
                   XkbSectionPtr section)
403
{
404 405
    XkbDoodadPtr doodad;
    xkbDoodadWireDesc *doodadWire;
406

407 408
    doodadWire = (xkbDoodadWireDesc *)
        _XkbGetReadBufferPtr(buf, SIZEOF(xkbDoodadWireDesc));
409
    if (!doodadWire)
410 411
        return BadLength;
    doodad = XkbAddGeomDoodad(geom, section, doodadWire->any.name);
412
    if (!doodad)
413 414 415 416 417 418
        return BadAlloc;
    doodad->any.type = doodadWire->any.type;
    doodad->any.priority = doodadWire->any.priority;
    doodad->any.top = doodadWire->any.top;
    doodad->any.left = doodadWire->any.left;
    doodad->any.angle = doodadWire->any.angle;
419
    switch (doodad->any.type) {
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    case XkbOutlineDoodad:
    case XkbSolidDoodad:
        doodad->shape.color_ndx = doodadWire->shape.colorNdx;
        doodad->shape.shape_ndx = doodadWire->shape.shapeNdx;
        break;
    case XkbTextDoodad:
        doodad->text.width = doodadWire->text.width;
        doodad->text.height = doodadWire->text.height;
        doodad->text.color_ndx = doodadWire->text.colorNdx;
        if (!_XkbGetReadBufferCountedString(buf, &doodad->text.text))
            return BadLength;
        if (!_XkbGetReadBufferCountedString(buf, &doodad->text.font))
            return BadLength;
        break;
    case XkbIndicatorDoodad:
        doodad->indicator.shape_ndx = doodadWire->indicator.shapeNdx;
        doodad->indicator.on_color_ndx = doodadWire->indicator.onColorNdx;
        doodad->indicator.off_color_ndx = doodadWire->indicator.offColorNdx;
        break;
    case XkbLogoDoodad:
        doodad->logo.color_ndx = doodadWire->logo.colorNdx;
        doodad->logo.shape_ndx = doodadWire->logo.shapeNdx;
        if (!_XkbGetReadBufferCountedString(buf, &doodad->logo.logo_name))
            return BadLength;
        break;
    default:
        return BadValue;
447 448 449 450 451
    }
    return Success;
}

static Status
452 453 454
_XkbReadGeomOverlay(XkbReadBufferPtr buf,
                    XkbGeometryPtr geom,
                    XkbSectionPtr section)
455
{
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    XkbOverlayPtr ol;
    xkbOverlayWireDesc *olWire;
    register int r;

    olWire = (xkbOverlayWireDesc *)
        _XkbGetReadBufferPtr(buf, SIZEOF(xkbOverlayWireDesc));
    if (olWire == NULL)
        return BadLength;
    ol = XkbAddGeomOverlay(section, olWire->name, olWire->nRows);
    if (ol == NULL)
        return BadLength;
    for (r = 0; r < olWire->nRows; r++) {
        register int k;
        XkbOverlayRowPtr row;
        xkbOverlayRowWireDesc *rowWire;
        xkbOverlayKeyWireDesc *keyWire;

        rowWire = (xkbOverlayRowWireDesc *)
            _XkbGetReadBufferPtr(buf, SIZEOF(xkbOverlayRowWireDesc));
        if (rowWire == NULL)
            return BadLength;
        row = XkbAddGeomOverlayRow(ol, rowWire->rowUnder, rowWire->nKeys);
        if (!row)
            return BadAlloc;
480
        row->row_under = rowWire->rowUnder;
481 482 483 484 485 486 487 488 489 490 491 492 493
        if (rowWire->nKeys < 1)
            continue;
        keyWire = (xkbOverlayKeyWireDesc *)
            _XkbGetReadBufferPtr(buf,
                             SIZEOF(xkbOverlayKeyWireDesc) * rowWire->nKeys);
        if (keyWire == NULL)
            return BadLength;
        for (k = 0; k < rowWire->nKeys; k++, keyWire++, row->num_keys++) {
            memcpy(row->keys[row->num_keys].over.name, keyWire->over,
                   XkbKeyNameLength);
            memcpy(row->keys[row->num_keys].under.name, keyWire->under,
                   XkbKeyNameLength);
        }
494 495 496 497 498
    }
    return Success;
}

static Status
499 500 501
_XkbReadGeomSections(XkbReadBufferPtr buf,
                     XkbGeometryPtr geom,
                     xkbGetGeometryReply *rep)
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 531
    register int s;
    XkbSectionPtr section;
    xkbSectionWireDesc *sectionWire;
    Status rtrn;

    if (rep->nSections < 1)
        return Success;
    if ((rtrn = XkbAllocGeomSections(geom, rep->nSections)) != Success)
        return rtrn;
    for (s = 0; s < rep->nSections; s++) {
        sectionWire = (xkbSectionWireDesc *)
            _XkbGetReadBufferPtr(buf, SIZEOF(xkbSectionWireDesc));
        if (!sectionWire)
            return BadLength;
        section = XkbAddGeomSection(geom, sectionWire->name, sectionWire->nRows,
                                    sectionWire->nDoodads,
                                    sectionWire->nOverlays);
        if (!section)
            return BadAlloc;
        section->top = sectionWire->top;
        section->left = sectionWire->left;
        section->width = sectionWire->width;
        section->height = sectionWire->height;
        section->angle = sectionWire->angle;
        section->priority = sectionWire->priority;
        if (sectionWire->nRows > 0) {
            register int r;

            for (r = 0; r < sectionWire->nRows; r++) {
532 533 534
                XkbRowPtr row;
                xkbRowWireDesc *rowWire;

535 536 537 538 539 540 541 542 543 544 545 546 547 548
                rowWire = (xkbRowWireDesc *)
                    _XkbGetReadBufferPtr(buf, SIZEOF(xkbRowWireDesc));
                if (!rowWire)
                    return BadLength;
                row = XkbAddGeomRow(section, rowWire->nKeys);
                if (!row)
                    return BadAlloc;
                row->top = rowWire->top;
                row->left = rowWire->left;
                row->vertical = rowWire->vertical;
                if (rowWire->nKeys > 0) {
                    register int k;

                    for (k = 0; k < rowWire->nKeys; k++) {
549 550 551
                        XkbKeyPtr key;
                        xkbKeyWireDesc *keyWire;

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 577 578 579 580 581 582
                        keyWire = (xkbKeyWireDesc *)
                            _XkbGetReadBufferPtr(buf, SIZEOF(xkbKeyWireDesc));
                        if (!keyWire)
                            return BadLength;
                        key = XkbAddGeomKey(row);
                        if (!key)
                            return BadAlloc;
                        memcpy(key->name.name, keyWire->name, XkbKeyNameLength);
                        key->gap = keyWire->gap;
                        key->shape_ndx = keyWire->shapeNdx;
                        key->color_ndx = keyWire->colorNdx;
                    }
                }
            }
        }
        if (sectionWire->nDoodads > 0) {
            register int d;

            for (d = 0; d < sectionWire->nDoodads; d++) {
                if ((rtrn = _XkbReadGeomDoodad(buf, geom, section)) != Success)
                    return rtrn;
            }
        }
        if (sectionWire->nOverlays > 0) {
            register int o;

            for (o = 0; o < sectionWire->nOverlays; o++) {
                if ((rtrn = _XkbReadGeomOverlay(buf, geom, section)) != Success)
                    return rtrn;
            }
        }
583 584 585 586 587
    }
    return Success;
}

static Status
588 589 590
_XkbReadGeomDoodads(XkbReadBufferPtr buf,
                    XkbGeometryPtr geom,
                    xkbGetGeometryReply *rep)
591
{
592 593 594 595 596 597 598 599 600 601
    register int d;
    Status rtrn;

    if (rep->nDoodads < 1)
        return Success;
    if ((rtrn = XkbAllocGeomDoodads(geom, rep->nDoodads)) != Success)
        return rtrn;
    for (d = 0; d < rep->nDoodads; d++) {
        if ((rtrn = _XkbReadGeomDoodad(buf, geom, NULL)) != Success)
            return rtrn;
602 603 604 605 606
    }
    return Success;
}

Status
607 608 609 610
_XkbReadGetGeometryReply(Display *dpy,
                         xkbGetGeometryReply *rep,
                         XkbDescPtr xkb,
                         int *nread_rtrn)
611
{
612
    XkbGeometryPtr geom;
613

614
    geom = _XkbTypedCalloc(1, XkbGeometryRec);
615
    if (!geom)
616
        return BadAlloc;
617
    if (xkb->geom)
618 619
        XkbFreeGeometry(xkb->geom, XkbGeomAllMask, True);
    xkb->geom = geom;
620

621 622 623
    geom->name = rep->name;
    geom->width_mm = rep->widthMM;
    geom->height_mm = rep->heightMM;
624
    if (rep->length) {
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
        XkbReadBufferRec buf;
        int left;

        if (_XkbInitReadBuffer(dpy, &buf, (int) rep->length * 4)) {
            Status status = Success;

            if (nread_rtrn)
                *nread_rtrn = (int) rep->length * 4;
            if (!_XkbGetReadBufferCountedString(&buf, &geom->label_font))
                status = BadLength;
            if (status == Success)
                status = _XkbReadGeomProperties(&buf, geom, rep);
            if (status == Success)
                status = _XkbReadGeomColors(&buf, geom, rep);
            if (status == Success)
                status = _XkbReadGeomShapes(&buf, geom, rep);
            if (status == Success)
                status = _XkbReadGeomSections(&buf, geom, rep);
            if (status == Success)
                status = _XkbReadGeomDoodads(&buf, geom, rep);
            if (status == Success)
                status = _XkbReadGeomKeyAliases(&buf, geom, rep);
            left = _XkbFreeReadBuffer(&buf);
            if ((rep->baseColorNdx > geom->num_colors) ||
                (rep->labelColorNdx > geom->num_colors))
                status = BadLength;
            if ((status != Success) || left || buf.error) {
                if (status == Success)
                    status = BadLength;
                XkbFreeGeometry(geom, XkbGeomAllMask, True);
                xkb->geom = NULL;
                return status;
            }
            geom->base_color = &geom->colors[rep->baseColorNdx];
            geom->label_color = &geom->colors[rep->labelColorNdx];
        }
        else {
            XkbFreeGeometry(geom, XkbGeomAllMask, True);
            xkb->geom = NULL;
            return BadAlloc;
        }
666 667 668 669 670
    }
    return Success;
}

Status
671
XkbGetGeometry(Display *dpy, XkbDescPtr xkb)
672
{
673 674 675
    xkbGetGeometryReq *req;
    xkbGetGeometryReply rep;
    Status status;
676

677 678 679
    if ((!xkb) || (dpy->flags & XlibDisplayNoXkb) ||
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return BadAccess;
680 681

    LockDisplay(dpy);
682 683 684 685
    GetReq(kbGetGeometry, req);
    req->reqType = dpy->xkb_info->codes->major_opcode;
    req->xkbReqType = X_kbGetGeometry;
    req->deviceSpec = xkb->device_spec;
686 687 688
    req->name = None;
    if (!_XReply(dpy, (xReply *) &rep, 0, xFalse))
        status = BadImplementation;
689
    else if (!rep.found)
690
        status = BadName;
691
    else
692
        status = _XkbReadGetGeometryReply(dpy, &rep, xkb, NULL);
693 694 695
    UnlockDisplay(dpy);
    SyncHandle();
    return status;
696 697 698
}

Status
699
XkbGetNamedGeometry(Display *dpy, XkbDescPtr xkb, Atom name)
700
{
701 702 703
    xkbGetGeometryReq *req;
    xkbGetGeometryReply rep;
    Status status;
704

705 706 707
    if ((name == None) || (dpy->flags & XlibDisplayNoXkb) ||
        (!dpy->xkb_info && !XkbUseExtension(dpy, NULL, NULL)))
        return BadAccess;
708 709

    LockDisplay(dpy);
710 711 712 713
    GetReq(kbGetGeometry, req);
    req->reqType = dpy->xkb_info->codes->major_opcode;
    req->xkbReqType = X_kbGetGeometry;
    req->deviceSpec = xkb->device_spec;
714 715 716
    req->name = (CARD32) name;
    if ((!_XReply(dpy, (xReply *) &rep, 0, xFalse)) || (!rep.found))
        status = BadImplementation;
717
    else if (!rep.found)
718
        status = BadName;
719
    else
720
        status = _XkbReadGetGeometryReply(dpy, &rep, xkb, NULL);
721 722 723
    UnlockDisplay(dpy);
    SyncHandle();
    return status;
724
}