XKBGAlloc.c 28.9 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
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

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

27
#ifdef HAVE_CONFIG_H
28 29 30 31 32 33 34
#include <config.h>
#endif


#include <stdio.h>
#include "Xlibint.h"
#include "XKBlibint.h"
35 36
#include <nx-X11/extensions/XKBgeom.h>
#include <nx-X11/extensions/XKBproto.h>
37 38 39

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

40
static void
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
_XkbFreeGeomLeafElems(Bool freeAll,
                      int first,
                      int count,
                      unsigned short *num_inout,
                      unsigned short *sz_inout,
                      char **elems,
                      unsigned int elem_sz)
{
    if ((freeAll) || (*elems == NULL)) {
        *num_inout = *sz_inout = 0;
        if (*elems != NULL) {
            _XkbFree(*elems);
            *elems = NULL;
        }
        return;
    }

    if ((first >= (*num_inout)) || (first < 0) || (count < 1))
        return;

    if (first + count >= (*num_inout)) {
        /* truncating the array is easy */
        (*num_inout) = first;
64 65
    }
    else {
66 67 68 69 70 71 72 73 74
        char *ptr;
        int extra;

        ptr = *elems;
        extra = ((*num_inout) - (first + count)) * elem_sz;
        if (extra > 0)
            memmove(&ptr[first * elem_sz], &ptr[(first + count) * elem_sz],
                    extra);
        (*num_inout) -= count;
75 76 77 78
    }
    return;
}

79 80
typedef void (*ContentsClearFunc) (
    char *       /* priv */
81 82
);

83
static void
84 85 86 87 88 89 90 91 92 93 94
_XkbFreeGeomNonLeafElems(Bool freeAll,
                         int first,
                         int count,
                         unsigned short *num_inout,
                         unsigned short *sz_inout,
                         char **elems,
                         unsigned int elem_sz,
                         ContentsClearFunc freeFunc)
{
    register int i;
    register char *ptr;
95 96

    if (freeAll) {
97 98
        first = 0;
        count = (*num_inout);
99
    }
100 101 102 103 104 105
    else if ((first >= (*num_inout)) || (first < 0) || (count < 1))
        return;
    else if (first + count > (*num_inout))
        count = (*num_inout) - first;
    if (*elems == NULL)
        return;
106 107

    if (freeFunc) {
108 109 110 111 112 113
        ptr = *elems;
        ptr += first * elem_sz;
        for (i = 0; i < count; i++) {
            (*freeFunc) (ptr);
            ptr += elem_sz;
        }
114 115
    }
    if (freeAll) {
116 117 118 119 120 121 122 123
        (*num_inout) = (*sz_inout) = 0;
        if (*elems) {
            _XkbFree(*elems);
            *elems = NULL;
        }
    }
    else if (first + count >= (*num_inout))
        *num_inout = first;
124
    else {
125 126 127 128
        i = ((*num_inout) - (first + count)) * elem_sz;
        ptr = *elems;
        memmove(&ptr[first * elem_sz], &ptr[(first + count) * elem_sz], i);
        (*num_inout) -= count;
129 130 131 132 133 134 135 136 137
    }
    return;
}

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

static void
_XkbClearProperty(char *prop_in)
{
138
    XkbPropertyPtr prop = (XkbPropertyPtr) prop_in;
139 140

    if (prop->name) {
141 142
        _XkbFree(prop->name);
        prop->name = NULL;
143 144
    }
    if (prop->value) {
145 146
        _XkbFree(prop->value);
        prop->value = NULL;
147 148 149 150 151
    }
    return;
}

void
152 153 154 155 156 157
XkbFreeGeomProperties(XkbGeometryPtr geom, int first, int count, Bool freeAll)
{
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &geom->num_properties, &geom->sz_properties,
                             (char **) &geom->properties,
                             sizeof(XkbPropertyRec), _XkbClearProperty);
158 159 160 161 162 163
    return;
}

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

void
164 165 166 167 168 169
XkbFreeGeomKeyAliases(XkbGeometryPtr geom, int first, int count, Bool freeAll)
{
    _XkbFreeGeomLeafElems(freeAll, first, count,
                          &geom->num_key_aliases, &geom->sz_key_aliases,
                          (char **) &geom->key_aliases,
                          sizeof(XkbKeyAliasRec));
170 171 172 173 174 175 176 177
    return;
}

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

static void
_XkbClearColor(char *color_in)
{
178
    XkbColorPtr color = (XkbColorPtr) color_in;
179

180
    _XkbFree(color->spec);
181 182 183 184
    return;
}

void
185
XkbFreeGeomColors(XkbGeometryPtr geom, int first, int count, Bool freeAll)
186
{
187 188 189 190
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &geom->num_colors, &geom->sz_colors,
                             (char **) &geom->colors,
                             sizeof(XkbColorRec), _XkbClearColor);
191 192 193 194 195 196
    return;
}

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

void
197
XkbFreeGeomPoints(XkbOutlinePtr outline, int first, int count, Bool freeAll)
198
{
199 200 201 202
    _XkbFreeGeomLeafElems(freeAll, first, count,
                          &outline->num_points, &outline->sz_points,
                          (char **) &outline->points,
                          sizeof(XkbPointRec));
203 204 205 206 207 208 209 210
    return;
}

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

static void
_XkbClearOutline(char *outline_in)
{
211
    XkbOutlinePtr outline = (XkbOutlinePtr) outline_in;
212

213 214
    if (outline->points != NULL)
        XkbFreeGeomPoints(outline, 0, outline->num_points, True);
215 216 217 218
    return;
}

void
219
XkbFreeGeomOutlines(XkbShapePtr shape, int first, int count, Bool freeAll)
220
{
221 222 223 224
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &shape->num_outlines, &shape->sz_outlines,
                             (char **) &shape->outlines,
                             sizeof(XkbOutlineRec), _XkbClearOutline);
225

226 227 228 229 230 231 232 233
    return;
}

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

static void
_XkbClearShape(char *shape_in)
{
234
    XkbShapePtr shape = (XkbShapePtr) shape_in;
235 236

    if (shape->outlines)
237
        XkbFreeGeomOutlines(shape, 0, shape->num_outlines, True);
238 239 240 241
    return;
}

void
242
XkbFreeGeomShapes(XkbGeometryPtr geom, int first, int count, Bool freeAll)
243
{
244 245 246 247
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &geom->num_shapes, &geom->sz_shapes,
                             (char **) &geom->shapes,
                             sizeof(XkbShapeRec), _XkbClearShape);
248 249 250 251 252
    return;
}

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

253
void
254
XkbFreeGeomOverlayKeys(XkbOverlayRowPtr row, int first, int count, Bool freeAll)
255
{
256 257 258 259
    _XkbFreeGeomLeafElems(freeAll, first, count,
                          &row->num_keys, &row->sz_keys,
                          (char **) &row->keys,
                          sizeof(XkbOverlayKeyRec));
260 261 262 263 264 265 266 267
    return;
}

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

static void
_XkbClearOverlayRow(char *row_in)
{
268
    XkbOverlayRowPtr row = (XkbOverlayRowPtr) row_in;
269

270 271
    if (row->keys != NULL)
        XkbFreeGeomOverlayKeys(row, 0, row->num_keys, True);
272 273 274 275
    return;
}

void
276 277
XkbFreeGeomOverlayRows(XkbOverlayPtr overlay, int first, int count,
                       Bool freeAll)
278
{
279 280 281 282
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &overlay->num_rows, &overlay->sz_rows,
                             (char **) &overlay->rows,
                             sizeof(XkbOverlayRowRec), _XkbClearOverlayRow);
283 284 285 286 287 288 289 290
    return;
}

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

static void
_XkbClearOverlay(char *overlay_in)
{
291
    XkbOverlayPtr overlay = (XkbOverlayPtr) overlay_in;
292

293 294
    if (overlay->rows != NULL)
        XkbFreeGeomOverlayRows(overlay, 0, overlay->num_rows, True);
295 296 297 298
    return;
}

void
299
XkbFreeGeomOverlays(XkbSectionPtr section, int first, int count, Bool freeAll)
300
{
301 302 303 304
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &section->num_overlays, &section->sz_overlays,
                             (char **) &section->overlays,
                             sizeof(XkbOverlayRec), _XkbClearOverlay);
305 306 307 308 309 310
    return;
}

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

void
311
XkbFreeGeomKeys(XkbRowPtr row, int first, int count, Bool freeAll)
312
{
313 314 315 316
    _XkbFreeGeomLeafElems(freeAll, first, count,
                          &row->num_keys, &row->sz_keys,
                          (char **) &row->keys,
                          sizeof(XkbKeyRec));
317 318 319 320 321 322 323 324
    return;
}

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

static void
_XkbClearRow(char *row_in)
{
325
    XkbRowPtr row = (XkbRowPtr) row_in;
326

327 328
    if (row->keys != NULL)
        XkbFreeGeomKeys(row, 0, row->num_keys, True);
329 330 331 332
    return;
}

void
333
XkbFreeGeomRows(XkbSectionPtr section, int first, int count, Bool freeAll)
334
{
335 336 337 338
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &section->num_rows, &section->sz_rows,
                             (char **) &section->rows,
                             sizeof(XkbRowRec), _XkbClearRow);
339 340 341 342 343 344 345
}

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

static void
_XkbClearSection(char *section_in)
{
346
    XkbSectionPtr section = (XkbSectionPtr) section_in;
347

348 349 350 351 352
    if (section->rows != NULL)
        XkbFreeGeomRows(section, 0, section->num_rows, True);
    if (section->doodads != NULL) {
        XkbFreeGeomDoodads(section->doodads, section->num_doodads, True);
        section->doodads = NULL;
353 354 355 356 357
    }
    return;
}

void
358
XkbFreeGeomSections(XkbGeometryPtr geom, int first, int count, Bool freeAll)
359
{
360 361 362 363
    _XkbFreeGeomNonLeafElems(freeAll, first, count,
                             &geom->num_sections, &geom->sz_sections,
                             (char **) &geom->sections,
                             sizeof(XkbSectionRec), _XkbClearSection);
364 365 366 367 368 369 370 371
    return;
}

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

static void
_XkbClearDoodad(char *doodad_in)
{
372
    XkbDoodadPtr doodad = (XkbDoodadPtr) doodad_in;
373 374

    switch (doodad->any.type) {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    case XkbTextDoodad:
    {
        if (doodad->text.text != NULL) {
            _XkbFree(doodad->text.text);
            doodad->text.text = NULL;
        }
        if (doodad->text.font != NULL) {
            _XkbFree(doodad->text.font);
            doodad->text.font = NULL;
        }
    }
        break;
    case XkbLogoDoodad:
    {
        if (doodad->logo.logo_name != NULL) {
            _XkbFree(doodad->logo.logo_name);
            doodad->logo.logo_name = NULL;
        }
    }
        break;
395 396 397 398 399
    }
    return;
}

void
400
XkbFreeGeomDoodads(XkbDoodadPtr doodads, int nDoodads, Bool freeAll)
401
{
402 403
    register int i;
    register XkbDoodadPtr doodad;
404 405

    if (doodads) {
406 407 408 409 410
        for (i = 0, doodad = doodads; i < nDoodads; i++, doodad++) {
            _XkbClearDoodad((char *) doodad);
        }
        if (freeAll)
            _XkbFree(doodads);
411 412 413 414 415
    }
    return;
}

void
416
XkbFreeGeometry(XkbGeometryPtr geom, unsigned which, Bool freeMap)
417
{
418 419
    if (geom == NULL)
        return;
420
    if (freeMap)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        which = XkbGeomAllMask;
    if ((which & XkbGeomPropertiesMask) && (geom->properties != NULL))
        XkbFreeGeomProperties(geom, 0, geom->num_properties, True);
    if ((which & XkbGeomColorsMask) && (geom->colors != NULL))
        XkbFreeGeomColors(geom, 0, geom->num_colors, True);
    if ((which & XkbGeomShapesMask) && (geom->shapes != NULL))
        XkbFreeGeomShapes(geom, 0, geom->num_shapes, True);
    if ((which & XkbGeomSectionsMask) && (geom->sections != NULL))
        XkbFreeGeomSections(geom, 0, geom->num_sections, True);
    if ((which & XkbGeomDoodadsMask) && (geom->doodads != NULL)) {
        XkbFreeGeomDoodads(geom->doodads, geom->num_doodads, True);
        geom->doodads = NULL;
        geom->num_doodads = geom->sz_doodads = 0;
    }
    if ((which & XkbGeomKeyAliasesMask) && (geom->key_aliases != NULL))
        XkbFreeGeomKeyAliases(geom, 0, geom->num_key_aliases, True);
437
    if (freeMap) {
438 439 440 441 442
        if (geom->label_font != NULL) {
            _XkbFree(geom->label_font);
            geom->label_font = NULL;
        }
        _XkbFree(geom);
443 444 445 446 447 448 449
    }
    return;
}

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

static Status
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
_XkbGeomAlloc(XPointer *old,
              unsigned short *num,
              unsigned short *total,
              int num_new,
              size_t sz_elem)
{
    if (num_new < 1)
        return Success;
    if ((*old) == NULL)
        *num = *total = 0;

    if ((*num) + num_new <= (*total))
        return Success;

    *total = (*num) + num_new;
    if ((*old) != NULL)
        (*old) = (XPointer) _XkbRealloc((*old), (*total) * sz_elem);
    else
        (*old) = (XPointer) _XkbCalloc((*total), sz_elem);
    if ((*old) == NULL) {
        *total = *num = 0;
        return BadAlloc;
    }

    if (*num > 0) {
        char *tmp = (char *) (*old);
        bzero(&tmp[sz_elem * (*num)], (num_new * sz_elem));
477 478 479 480
    }
    return Success;
}

481 482 483 484 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
#define _XkbAllocProps(g, n) _XkbGeomAlloc((XPointer *)&(g)->properties, \
                                &(g)->num_properties, &(g)->sz_properties, \
                                (n), sizeof(XkbPropertyRec))
#define _XkbAllocColors(g, n) _XkbGeomAlloc((XPointer *)&(g)->colors, \
                                &(g)->num_colors, &(g)->sz_colors, \
                                (n), sizeof(XkbColorRec))
#define _XkbAllocShapes(g, n) _XkbGeomAlloc((XPointer *)&(g)->shapes, \
                                &(g)->num_shapes, &(g)->sz_shapes, \
                                (n), sizeof(XkbShapeRec))
#define _XkbAllocSections(g, n) _XkbGeomAlloc((XPointer *)&(g)->sections, \
                                &(g)->num_sections, &(g)->sz_sections, \
                                (n), sizeof(XkbSectionRec))
#define _XkbAllocDoodads(g, n) _XkbGeomAlloc((XPointer *)&(g)->doodads, \
                                &(g)->num_doodads, &(g)->sz_doodads, \
                                (n), sizeof(XkbDoodadRec))
#define _XkbAllocKeyAliases(g, n) _XkbGeomAlloc((XPointer *)&(g)->key_aliases, \
                                &(g)->num_key_aliases, &(g)->sz_key_aliases, \
                                (n), sizeof(XkbKeyAliasRec))

#define _XkbAllocOutlines(s, n) _XkbGeomAlloc((XPointer *)&(s)->outlines, \
                                &(s)->num_outlines, &(s)->sz_outlines, \
                                (n), sizeof(XkbOutlineRec))
#define _XkbAllocRows(s, n) _XkbGeomAlloc((XPointer *)&(s)->rows, \
                                &(s)->num_rows, &(s)->sz_rows, \
                                (n), sizeof(XkbRowRec))
#define _XkbAllocPoints(o, n) _XkbGeomAlloc((XPointer *)&(o)->points, \
                                &(o)->num_points, &(o)->sz_points, \
                                (n), sizeof(XkbPointRec))
#define _XkbAllocKeys(r, n) _XkbGeomAlloc((XPointer *)&(r)->keys, \
                                &(r)->num_keys, &(r)->sz_keys, \
                                (n), sizeof(XkbKeyRec))
#define _XkbAllocOverlays(s, n) _XkbGeomAlloc((XPointer *)&(s)->overlays, \
                                &(s)->num_overlays, &(s)->sz_overlays, \
                                (n), sizeof(XkbOverlayRec))
#define _XkbAllocOverlayRows(o, n) _XkbGeomAlloc((XPointer *)&(o)->rows, \
                                &(o)->num_rows, &(o)->sz_rows, \
                                (n), sizeof(XkbOverlayRowRec))
#define _XkbAllocOverlayKeys(r, n) _XkbGeomAlloc((XPointer *)&(r)->keys, \
                                &(r)->num_keys, &(r)->sz_keys, \
                                (n), sizeof(XkbOverlayKeyRec))
521

522
Status
523
XkbAllocGeomProps(XkbGeometryPtr geom, int nProps)
524
{
525
    return _XkbAllocProps(geom, nProps);
526 527 528
}

Status
529
XkbAllocGeomColors(XkbGeometryPtr geom, int nColors)
530
{
531
    return _XkbAllocColors(geom, nColors);
532 533 534
}

Status
535
XkbAllocGeomKeyAliases(XkbGeometryPtr geom, int nKeyAliases)
536
{
537
    return _XkbAllocKeyAliases(geom, nKeyAliases);
538 539 540
}

Status
541
XkbAllocGeomShapes(XkbGeometryPtr geom, int nShapes)
542
{
543
    return _XkbAllocShapes(geom, nShapes);
544 545 546
}

Status
547
XkbAllocGeomSections(XkbGeometryPtr geom, int nSections)
548
{
549
    return _XkbAllocSections(geom, nSections);
550 551 552
}

Status
553
XkbAllocGeomOverlays(XkbSectionPtr section, int nOverlays)
554
{
555
    return _XkbAllocOverlays(section, nOverlays);
556 557 558
}

Status
559
XkbAllocGeomOverlayRows(XkbOverlayPtr overlay, int nRows)
560
{
561
    return _XkbAllocOverlayRows(overlay, nRows);
562 563 564
}

Status
565
XkbAllocGeomOverlayKeys(XkbOverlayRowPtr row, int nKeys)
566
{
567
    return _XkbAllocOverlayKeys(row, nKeys);
568 569 570
}

Status
571
XkbAllocGeomDoodads(XkbGeometryPtr geom, int nDoodads)
572
{
573
    return _XkbAllocDoodads(geom, nDoodads);
574 575 576
}

Status
577
XkbAllocGeomSectionDoodads(XkbSectionPtr section, int nDoodads)
578
{
579
    return _XkbAllocDoodads(section, nDoodads);
580 581 582
}

Status
583
XkbAllocGeomOutlines(XkbShapePtr shape, int nOL)
584
{
585
    return _XkbAllocOutlines(shape, nOL);
586 587 588
}

Status
589
XkbAllocGeomRows(XkbSectionPtr section, int nRows)
590
{
591
    return _XkbAllocRows(section, nRows);
592 593 594
}

Status
595
XkbAllocGeomPoints(XkbOutlinePtr ol, int nPts)
596
{
597
    return _XkbAllocPoints(ol, nPts);
598 599 600
}

Status
601
XkbAllocGeomKeys(XkbRowPtr row, int nKeys)
602
{
603
    return _XkbAllocKeys(row, nKeys);
604 605 606
}

Status
607
XkbAllocGeometry(XkbDescPtr xkb, XkbGeometrySizesPtr sizes)
608
{
609 610
    XkbGeometryPtr geom;
    Status rtrn;
611

612 613 614 615
    if (xkb->geom == NULL) {
        xkb->geom = _XkbTypedCalloc(1, XkbGeometryRec);
        if (!xkb->geom)
            return BadAlloc;
616
    }
617 618 619 620
    geom = xkb->geom;
    if ((sizes->which & XkbGeomPropertiesMask) &&
        ((rtrn = _XkbAllocProps(geom, sizes->num_properties)) != Success)) {
        goto BAIL;
621
    }
622 623 624
    if ((sizes->which & XkbGeomColorsMask) &&
        ((rtrn = _XkbAllocColors(geom, sizes->num_colors)) != Success)) {
        goto BAIL;
625
    }
626 627 628
    if ((sizes->which & XkbGeomShapesMask) &&
        ((rtrn = _XkbAllocShapes(geom, sizes->num_shapes)) != Success)) {
        goto BAIL;
629
    }
630 631 632
    if ((sizes->which & XkbGeomSectionsMask) &&
        ((rtrn = _XkbAllocSections(geom, sizes->num_sections)) != Success)) {
        goto BAIL;
633
    }
634 635 636
    if ((sizes->which & XkbGeomDoodadsMask) &&
        ((rtrn = _XkbAllocDoodads(geom, sizes->num_doodads)) != Success)) {
        goto BAIL;
637
    }
638 639 640 641
    if ((sizes->which & XkbGeomKeyAliasesMask) &&
        ((rtrn = _XkbAllocKeyAliases(geom, sizes->num_key_aliases))
         != Success)) {
        goto BAIL;
642 643
    }
    return Success;
644 645 646
 BAIL:
    XkbFreeGeometry(geom, XkbGeomAllMask, True);
    xkb->geom = NULL;
647 648 649 650 651 652
    return rtrn;
}

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

XkbPropertyPtr
653 654 655 656 657 658 659 660 661
XkbAddGeomProperty(XkbGeometryPtr geom, char *name, char *value)
{
    register int i;
    register XkbPropertyPtr prop;

    if ((!geom) || (!name) || (!value))
        return NULL;
    for (i = 0, prop = geom->properties; i < geom->num_properties; i++, prop++) {
        if ((prop->name) && (strcmp(name, prop->name) == 0)) {
662
            _XkbFree(prop->value);
663 664 665 666 667 668 669 670 671 672
            prop->value = strdup(value);
            return prop;
        }
    }
    if ((geom->num_properties >= geom->sz_properties) &&
        (_XkbAllocProps(geom, 1) != Success)) {
        return NULL;
    }
    prop = &geom->properties[geom->num_properties];
    prop->name = strdup(name);
673
    if (!prop->name)
674 675
        return NULL;
    prop->value = strdup(value);
676
    if (!prop->value) {
677 678 679
        _XkbFree(prop->name);
        prop->name = NULL;
        return NULL;
680 681 682 683 684 685
    }
    geom->num_properties++;
    return prop;
}

XkbKeyAliasPtr
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
{
    register int i;
    register XkbKeyAliasPtr alias;

    if ((!geom) || (!aliasStr) || (!realStr) || (!aliasStr[0]) || (!realStr[0]))
        return NULL;
    for (i = 0, alias = geom->key_aliases; i < geom->num_key_aliases;
         i++, alias++) {
        if (strncmp(alias->alias, aliasStr, XkbKeyNameLength) == 0) {
            bzero(alias->real, XkbKeyNameLength);
            strncpy(alias->real, realStr, XkbKeyNameLength);
            return alias;
        }
    }
    if ((geom->num_key_aliases >= geom->sz_key_aliases) &&
        (_XkbAllocKeyAliases(geom, 1) != Success)) {
        return NULL;
    }
    alias = &geom->key_aliases[geom->num_key_aliases];
    bzero(alias, sizeof(XkbKeyAliasRec));
    strncpy(alias->alias, aliasStr, XkbKeyNameLength);
    strncpy(alias->real, realStr, XkbKeyNameLength);
709 710 711 712 713
    geom->num_key_aliases++;
    return alias;
}

XkbColorPtr
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
XkbAddGeomColor(XkbGeometryPtr geom, char *spec, unsigned int pixel)
{
    register int i;
    register XkbColorPtr color;

    if ((!geom) || (!spec))
        return NULL;
    for (i = 0, color = geom->colors; i < geom->num_colors; i++, color++) {
        if ((color->spec) && (strcmp(color->spec, spec) == 0)) {
            color->pixel = pixel;
            return color;
        }
    }
    if ((geom->num_colors >= geom->sz_colors) &&
        (_XkbAllocColors(geom, 1) != Success)) {
        return NULL;
    }
    color = &geom->colors[geom->num_colors];
    color->pixel = pixel;
    color->spec = strdup(spec);
734
    if (!color->spec)
735
        return NULL;
736 737 738 739 740
    geom->num_colors++;
    return color;
}

XkbOutlinePtr
741
XkbAddGeomOutline(XkbShapePtr shape, int sz_points)
742
{
743
    XkbOutlinePtr outline;
744

745 746 747 748 749
    if ((!shape) || (sz_points < 0))
        return NULL;
    if ((shape->num_outlines >= shape->sz_outlines) &&
        (_XkbAllocOutlines(shape, 1) != Success)) {
        return NULL;
750
    }
751 752 753 754
    outline = &shape->outlines[shape->num_outlines];
    bzero(outline, sizeof(XkbOutlineRec));
    if ((sz_points > 0) && (_XkbAllocPoints(outline, sz_points) != Success))
        return NULL;
755 756 757 758 759
    shape->num_outlines++;
    return outline;
}

XkbShapePtr
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
XkbAddGeomShape(XkbGeometryPtr geom, Atom name, int sz_outlines)
{
    XkbShapePtr shape;
    register int i;

    if ((!geom) || (!name) || (sz_outlines < 0))
        return NULL;
    if (geom->num_shapes > 0) {
        for (shape = geom->shapes, i = 0; i < geom->num_shapes; i++, shape++) {
            if (name == shape->name)
                return shape;
        }
    }
    if ((geom->num_shapes >= geom->sz_shapes) &&
        (_XkbAllocShapes(geom, 1) != Success))
        return NULL;
    shape = &geom->shapes[geom->num_shapes];
    bzero(shape, sizeof(XkbShapeRec));
    if ((sz_outlines > 0) && (_XkbAllocOutlines(shape, sz_outlines) != Success))
        return NULL;
    shape->name = name;
    shape->primary = shape->approx = NULL;
782 783 784 785 786 787 788
    geom->num_shapes++;
    return shape;
}

XkbKeyPtr
XkbAddGeomKey(XkbRowPtr row)
{
789 790
    XkbKeyPtr key;

791
    if (!row)
792 793 794 795 796
        return NULL;
    if ((row->num_keys >= row->sz_keys) && (_XkbAllocKeys(row, 1) != Success))
        return NULL;
    key = &row->keys[row->num_keys++];
    bzero(key, sizeof(XkbKeyRec));
797 798 799 800
    return key;
}

XkbRowPtr
801 802 803 804 805 806 807 808 809 810 811 812 813
XkbAddGeomRow(XkbSectionPtr section, int sz_keys)
{
    XkbRowPtr row;

    if ((!section) || (sz_keys < 0))
        return NULL;
    if ((section->num_rows >= section->sz_rows) &&
        (_XkbAllocRows(section, 1) != Success))
        return NULL;
    row = &section->rows[section->num_rows];
    bzero(row, sizeof(XkbRowRec));
    if ((sz_keys > 0) && (_XkbAllocKeys(row, sz_keys) != Success))
        return NULL;
814 815 816 817 818
    section->num_rows++;
    return row;
}

XkbSectionPtr
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
XkbAddGeomSection(XkbGeometryPtr geom,
                  Atom name,
                  int sz_rows,
                  int sz_doodads,
                  int sz_over)
{
    register int i;
    XkbSectionPtr section;

    if ((!geom) || (name == None) || (sz_rows < 0))
        return NULL;
    for (i = 0, section = geom->sections; i < geom->num_sections;
         i++, section++) {
        if (section->name != name)
            continue;
        if (((sz_rows > 0) && (_XkbAllocRows(section, sz_rows) != Success)) ||
            ((sz_doodads > 0) &&
             (_XkbAllocDoodads(section, sz_doodads) != Success)) ||
            ((sz_over > 0) && (_XkbAllocOverlays(section, sz_over) != Success)))
            return NULL;
        return section;
    }
    if ((geom->num_sections >= geom->sz_sections) &&
        (_XkbAllocSections(geom, 1) != Success))
        return NULL;
    section = &geom->sections[geom->num_sections];
    if ((sz_rows > 0) && (_XkbAllocRows(section, sz_rows) != Success))
        return NULL;
    if ((sz_doodads > 0) && (_XkbAllocDoodads(section, sz_doodads) != Success)) {
        if (section->rows) {
            _XkbFree(section->rows);
            section->rows = NULL;
            section->sz_rows = section->num_rows = 0;
        }
        return NULL;
    }
    section->name = name;
856 857 858 859 860
    geom->num_sections++;
    return section;
}

XkbDoodadPtr
861
XkbAddGeomDoodad(XkbGeometryPtr geom, XkbSectionPtr section, Atom name)
862
{
863 864
    XkbDoodadPtr old, doodad;
    register int i, nDoodads;
865

866 867 868 869 870
    if ((!geom) || (name == None))
        return NULL;
    if ((section != NULL) && (section->num_doodads > 0)) {
        old = section->doodads;
        nDoodads = section->num_doodads;
871 872
    }
    else {
873 874
        old = geom->doodads;
        nDoodads = geom->num_doodads;
875
    }
876 877 878
    for (i = 0, doodad = old; i < nDoodads; i++, doodad++) {
        if (doodad->any.name == name)
            return doodad;
879 880
    }
    if (section) {
881 882 883 884 885
        if ((section->num_doodads >= geom->sz_doodads) &&
            (_XkbAllocDoodads(section, 1) != Success)) {
            return NULL;
        }
        doodad = &section->doodads[section->num_doodads++];
886 887
    }
    else {
888 889 890 891
        if ((geom->num_doodads >= geom->sz_doodads) &&
            (_XkbAllocDoodads(geom, 1) != Success))
            return NULL;
        doodad = &geom->doodads[geom->num_doodads++];
892
    }
893 894
    bzero(doodad, sizeof(XkbDoodadRec));
    doodad->any.name = name;
895 896 897 898
    return doodad;
}

XkbOverlayKeyPtr
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
XkbAddGeomOverlayKey(XkbOverlayPtr overlay,
                     XkbOverlayRowPtr row,
                     char *over,
                     char *under)
{
    register int i;
    XkbOverlayKeyPtr key;
    XkbSectionPtr section;
    XkbRowPtr row_under;
    Bool found;

    if ((!overlay) || (!row) || (!over) || (!under))
        return NULL;
    section = overlay->section_under;
    if (row->row_under >= section->num_rows)
        return NULL;
    row_under = &section->rows[row->row_under];
    for (i = 0, found = False; i < row_under->num_keys; i++) {
        if (strncmp(under, row_under->keys[i].name.name, XkbKeyNameLength) == 0) {
            found = True;
            break;
        }
921 922
    }
    if (!found)
923 924 925 926 927 928 929
        return NULL;
    if ((row->num_keys >= row->sz_keys) &&
        (_XkbAllocOverlayKeys(row, 1) != Success))
        return NULL;
    key = &row->keys[row->num_keys];
    strncpy(key->under.name, under, XkbKeyNameLength);
    strncpy(key->over.name, over, XkbKeyNameLength);
930 931 932 933 934
    row->num_keys++;
    return key;
}

XkbOverlayRowPtr
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
XkbAddGeomOverlayRow(XkbOverlayPtr overlay, int row_under, int sz_keys)
{
    register int i;
    XkbOverlayRowPtr row;

    if ((!overlay) || (sz_keys < 0))
        return NULL;
    if (row_under >= overlay->section_under->num_rows)
        return NULL;
    for (i = 0; i < overlay->num_rows; i++) {
        if (overlay->rows[i].row_under == row_under) {
            row = &overlay->rows[i];
            if ((row->sz_keys < sz_keys) &&
                (_XkbAllocOverlayKeys(row, sz_keys) != Success)) {
                return NULL;
            }
            return &overlay->rows[i];
        }
    }
    if ((overlay->num_rows >= overlay->sz_rows) &&
        (_XkbAllocOverlayRows(overlay, 1) != Success))
        return NULL;
    row = &overlay->rows[overlay->num_rows];
    bzero(row, sizeof(XkbOverlayRowRec));
    if ((sz_keys > 0) && (_XkbAllocOverlayKeys(row, sz_keys) != Success))
        return NULL;
    row->row_under = row_under;
962 963 964 965 966
    overlay->num_rows++;
    return row;
}

XkbOverlayPtr
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
XkbAddGeomOverlay(XkbSectionPtr section, Atom name, int sz_rows)
{
    register int i;
    XkbOverlayPtr overlay;

    if ((!section) || (name == None) || (sz_rows == 0))
        return NULL;

    for (i = 0, overlay = section->overlays; i < section->num_overlays;
         i++, overlay++) {
        if (overlay->name == name) {
            if ((sz_rows > 0) &&
                (_XkbAllocOverlayRows(overlay, sz_rows) != Success))
                return NULL;
            return overlay;
        }
    }
    if ((section->num_overlays >= section->sz_overlays) &&
        (_XkbAllocOverlays(section, 1) != Success))
        return NULL;
    overlay = &section->overlays[section->num_overlays];
    if ((sz_rows > 0) && (_XkbAllocOverlayRows(overlay, sz_rows) != Success))
        return NULL;
    overlay->name = name;
    overlay->section_under = section;
992 993 994
    section->num_overlays++;
    return overlay;
}