1/************************************************************
2 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.
3
4 Permission to use, copy, modify, and distribute this
5 software and its documentation for any purpose and without
6 fee is hereby granted, provided that the above copyright
7 notice appear in all copies and that both that copyright
8 notice and this permission notice appear in supporting
9 documentation, and that the name of Silicon Graphics not be
10 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 of this software for any purpose. It is provided "as is"
14 without any express or implied warranty.
15
16 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23 THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25 ********************************************************/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <stdio.h>
31#include <ctype.h>
32#include <stdlib.h>
33
34#include <X11/Xos.h>
35#include <X11/Xfuncs.h>
36#include <X11/Xlib.h>
37#include <X11/keysym.h>
38#include <X11/XKBlib.h>
39#include <X11/extensions/XKBgeom.h>
40#include "XKMformat.h"
41#include "XKBfileInt.h"
42
43static void
44_XkbAddDrawable(XkbDrawablePtr *pfirst, XkbDrawablePtr *plast,
45                XkbDrawablePtr tmp)
46{
47    XkbDrawablePtr old;
48
49    if (*pfirst == NULL) {
50        *pfirst = *plast = tmp;
51    }
52    else if (tmp->priority >= (*plast)->priority) {
53        (*plast)->next = tmp;
54        *plast = tmp;
55    }
56    else if (tmp->priority < (*pfirst)->priority) {
57        tmp->next = (*pfirst);
58        (*pfirst) = tmp;
59    }
60    else {
61        old = *pfirst;
62        while ((old->next) && (old->next->priority <= tmp->priority)) {
63            old = old->next;
64        }
65        tmp->next = old->next;
66        old->next = tmp;
67    }
68    return;
69}
70
71XkbDrawablePtr
72XkbGetOrderedDrawables(XkbGeometryPtr geom, XkbSectionPtr section)
73{
74    XkbDrawablePtr first, last, tmp;
75    int i;
76
77    first = last = NULL;
78    if (geom != NULL) {
79        XkbSectionPtr section;
80        XkbDoodadPtr doodad;
81
82        for (i = 0, section = geom->sections; i < geom->num_sections;
83             i++, section++) {
84            tmp = _XkbTypedCalloc(1, XkbDrawableRec);
85            if (!tmp) {
86                XkbFreeOrderedDrawables(first);
87                return NULL;
88            }
89            tmp->type = XkbDW_Section;
90            tmp->priority = section->priority;
91            tmp->u.section = section;
92            tmp->next = NULL;
93            _XkbAddDrawable(&first, &last, tmp);
94        }
95        for (i = 0, doodad = geom->doodads; i < geom->num_doodads;
96             i++, doodad++) {
97            tmp = _XkbTypedCalloc(1, XkbDrawableRec);
98            if (!tmp) {
99                XkbFreeOrderedDrawables(first);
100                return NULL;
101            }
102            tmp->type = XkbDW_Doodad;
103            tmp->priority = doodad->any.priority;
104            tmp->u.doodad = doodad;
105            tmp->next = NULL;
106            _XkbAddDrawable(&first, &last, tmp);
107        }
108    }
109    if (section != NULL) {
110        XkbDoodadPtr doodad;
111
112        for (i = 0, doodad = section->doodads; i < section->num_doodads;
113             i++, doodad++) {
114            tmp = _XkbTypedCalloc(1, XkbDrawableRec);
115            if (!tmp) {
116                XkbFreeOrderedDrawables(first);
117                return NULL;
118            }
119            tmp->type = XkbDW_Doodad;
120            tmp->priority = doodad->any.priority;
121            tmp->u.doodad = doodad;
122            tmp->next = NULL;
123            _XkbAddDrawable(&first, &last, tmp);
124        }
125    }
126    return first;
127}
128
129void
130XkbFreeOrderedDrawables(XkbDrawablePtr draw)
131{
132    XkbDrawablePtr tmp;
133
134    for (; draw != NULL; draw = tmp) {
135        tmp = draw->next;
136        _XkbFree(draw);
137    }
138    return;
139}
140