summaryrefslogtreecommitdiff
path: root/src/alloc.c
blob: 30af089e937ab9a2716ff585db7b93d1d4b05ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
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
documentation, and that the name of Silicon Graphics not be
used in advertising or publicity pertaining to distribution
of the software without specific prior written permission.
Silicon Graphics makes no representation about the suitability
of this software for any purpose. It is provided "as is"
without any express or implied warranty.

SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
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
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "utils.h"
#include "xkballoc.h"
#include "xkbcommon/xkbcommon.h"
#include "XKBcommonint.h"
#include <X11/extensions/XKB.h>

int
XkbcAllocCompatMap(struct xkb_desc * xkb, unsigned which, unsigned nSI)
{
    struct xkb_compat_map * compat;
    struct xkb_sym_interpret *prev_interpret;

    if (!xkb)
        return BadMatch;

    if (xkb->compat) {
        if (xkb->compat->size_si >= nSI)
            return Success;

        compat = xkb->compat;
        compat->size_si = nSI;
        if (!compat->sym_interpret)
            compat->num_si = 0;

        prev_interpret = compat->sym_interpret;
        compat->sym_interpret = uTypedRecalloc(compat->sym_interpret,
                                               compat->num_si, nSI,
                                               struct xkb_sym_interpret);
        if (!compat->sym_interpret) {
            free(prev_interpret);
            compat->size_si = compat->num_si = 0;
            return BadAlloc;
        }

        return Success;
    }

    compat = uTypedCalloc(1, struct xkb_compat_map);
    if (!compat)
        return BadAlloc;

    if (nSI > 0) {
        compat->sym_interpret = uTypedCalloc(nSI, struct xkb_sym_interpret);
        if (!compat->sym_interpret) {
            free(compat);
            return BadAlloc;
        }
    }
    compat->size_si = nSI;
    compat->num_si = 0;
    memset(&compat->groups[0], 0, XkbNumKbdGroups * sizeof(struct xkb_mods));
    xkb->compat = compat;

    return Success;
}


static void
XkbcFreeCompatMap(struct xkb_desc * xkb)
{
    struct xkb_compat_map * compat;

    if (!xkb || !xkb->compat)
        return;

    compat = xkb->compat;

    free(compat->sym_interpret);
    free(compat);
    xkb->compat = NULL;
}

int
XkbcAllocNames(struct xkb_desc * xkb, unsigned which, unsigned nTotalAliases)
{
    struct xkb_names * names;

    if (!xkb)
        return BadMatch;

    if (!xkb->names) {
        xkb->names = uTypedCalloc(1, struct xkb_names);
        if (!xkb->names)
            return BadAlloc;
    }
    names = xkb->names;

    if ((which & XkbKTLevelNamesMask) && xkb->map && xkb->map->types) {
        int i;
        struct xkb_key_type * type;

        type = xkb->map->types;
        for (i = 0; i < xkb->map->num_types; i++, type++) {
            if (!type->level_names) {
                type->level_names = uTypedCalloc(type->num_levels, const char *);
                if (!type->level_names)
                    return BadAlloc;
            }
        }
    }

    if ((which & XkbKeyNamesMask) && !names->keys) {
        if (!xkb_keymap_keycode_range_is_legal(xkb))
            return BadMatch;

        names->keys = uTypedCalloc(xkb->max_key_code + 1, struct xkb_key_name);
        if (!names->keys)
            return BadAlloc;
    }

    if ((which & XkbKeyAliasesMask) && (nTotalAliases > 0)) {
        if (!names->key_aliases)
            names->key_aliases = uTypedCalloc(nTotalAliases,
                                                 struct xkb_key_alias);
        else if (nTotalAliases > names->num_key_aliases) {
            struct xkb_key_alias *prev_aliases = names->key_aliases;

            names->key_aliases = uTypedRecalloc(names->key_aliases,
                                                names->num_key_aliases,
                                                nTotalAliases,
                                                struct xkb_key_alias);
            if (!names->key_aliases)
                free(prev_aliases);
        }

        if (!names->key_aliases) {
            names->num_key_aliases = 0;
            return BadAlloc;
        }

        names->num_key_aliases = nTotalAliases;
    }

    return Success;
}

static void
XkbcFreeNames(struct xkb_desc * xkb)
{
    struct xkb_names * names;
    struct xkb_client_map * map;
    int i;

    if (!xkb || !xkb->names)
        return;

    names = xkb->names;
    map = xkb->map;

    if (map && map->types) {
        struct xkb_key_type * type = map->types;

        for (i = 0; i < map->num_types; i++, type++) {
            int j;
            for (j = 0; j < type->num_levels; j++)
                free(UNCONSTIFY(type->level_names[i]));
            free(type->level_names);
            type->level_names = NULL;
        }
    }

    for (i = 0; i < XkbNumVirtualMods; i++)
        free(UNCONSTIFY(names->vmods[i]));
    for (i = 0; i < XkbNumIndicators; i++)
        free(UNCONSTIFY(names->indicators[i]));
    for (i = 0; i < XkbNumKbdGroups; i++)
        free(UNCONSTIFY(names->groups[i]));

    free(names->keys);
    free(names->key_aliases);
    free(names);
    xkb->names = NULL;
}

int
XkbcAllocControls(struct xkb_desc * xkb, unsigned which)
{
    if (!xkb)
        return BadMatch;

    if (!xkb->ctrls) {
        xkb->ctrls = uTypedCalloc(1, struct xkb_controls);
        if (!xkb->ctrls)
            return BadAlloc;
    }

    if (!xkb->ctrls->per_key_repeat) {
        xkb->ctrls->per_key_repeat = uTypedCalloc(xkb->max_key_code << 3,
                                                  unsigned char);
        if (!xkb->ctrls->per_key_repeat)
            return BadAlloc;
    }

    return Success;
}

static void
XkbcFreeControls(struct xkb_desc * xkb)
{
    if (xkb && xkb->ctrls) {
        free(xkb->ctrls->per_key_repeat);
        free(xkb->ctrls);
        xkb->ctrls = NULL;
    }
}

int
XkbcAllocIndicatorMaps(struct xkb_desc * xkb)
{
    if (!xkb)
        return BadMatch;

    if (!xkb->indicators) {
        xkb->indicators = uTypedCalloc(1, struct xkb_indicator);
        if (!xkb->indicators)
            return BadAlloc;
    }

    return Success;
}

static void
XkbcFreeIndicatorMaps(struct xkb_desc * xkb)
{
    if (xkb) {
        free(xkb->indicators);
        xkb->indicators = NULL;
    }
}

struct xkb_desc *
XkbcAllocKeyboard(struct xkb_context *context)
{
    struct xkb_desc *xkb;

    xkb = uTypedCalloc(1, struct xkb_desc);
    if (!xkb)
        return NULL;

    xkb->refcnt = 1;
    xkb_context_ref(context);
    xkb->context = context;
    xkb->device_spec = XkbUseCoreKbd;

    return xkb;
}

void
XkbcFreeKeyboard(struct xkb_desc * xkb)
{
    if (!xkb)
        return;

    XkbcFreeClientMap(xkb);
    XkbcFreeServerMap(xkb);
    XkbcFreeCompatMap(xkb);
    XkbcFreeIndicatorMaps(xkb);
    XkbcFreeNames(xkb);
    XkbcFreeControls(xkb);
    xkb_context_unref(xkb->context);
    free(xkb);
}