2 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
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.
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.
30 XkbcAllocClientMap(struct xkb_keymap *keymap, unsigned which,
33 if (!keymap || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
37 keymap->map = calloc(1, sizeof(*keymap->map));
40 darray_init(keymap->map->types);
43 if (which & XkbKeyTypesMask)
44 darray_growalloc(keymap->map->types, nTotalTypes);
46 if (which & XkbKeySymsMask)
47 darray_resize0(keymap->map->key_sym_map, keymap->max_key_code + 1);
49 if (which & XkbModifierMapMask) {
50 if (!keymap->map->modmap) {
51 keymap->map->modmap = uTypedCalloc(keymap->max_key_code + 1,
53 if (!keymap->map->modmap)
62 XkbcAllocServerMap(struct xkb_keymap *keymap, unsigned which,
66 struct xkb_server_map * map;
71 if (!keymap->server) {
72 map = uTypedCalloc(1, struct xkb_server_map);
76 for (i = 0; i < XkbNumVirtualMods; i++)
77 map->vmods[i] = XkbNoModifierMask;
88 i = keymap->max_key_code + 1;
89 map->explicit = uTypedCalloc(i, unsigned char);
97 darray_resize0(map->acts, darray_size(map->acts) + nNewActions + 1);
98 darray_resize0(map->key_acts, keymap->max_key_code + 1);
100 if (!map->behaviors) {
101 i = keymap->max_key_code + 1;
102 map->behaviors = uTypedCalloc(i, struct xkb_behavior);
108 i = keymap->max_key_code + 1;
109 map->vmodmap = uTypedCalloc(i, uint32_t);
118 XkbcCopyKeyType(const struct xkb_key_type *from, struct xkb_key_type *into)
125 darray_free(into->map);
126 free(into->preserve);
127 for (i = 0; i < into->num_levels; i++)
128 free(into->level_names[i]);
129 free(into->level_names);
133 darray_copy(into->map, from->map);
135 if (from->preserve && !darray_empty(into->map)) {
136 into->preserve = calloc(darray_size(into->map),
137 sizeof(*into->preserve));
140 memcpy(into->preserve, from->preserve,
141 darray_size(into->map) * sizeof(*into->preserve));
144 if (from->level_names && into->num_levels > 0) {
145 into->level_names = calloc(into->num_levels,
146 sizeof(*into->level_names));
147 if (!into->level_names)
150 for (i = 0; i < into->num_levels; i++)
151 into->level_names[i] = strdup(from->level_names[i]);
158 XkbcResizeKeySyms(struct xkb_keymap *keymap, xkb_keycode_t key,
161 darray_resize0(darray_item(keymap->map->key_sym_map, key).syms, needed);
166 XkbcResizeKeyActions(struct xkb_keymap *keymap, xkb_keycode_t key,
169 size_t old_ndx, old_num_acts, new_ndx;
172 darray_item(keymap->server->key_acts, key) = 0;
176 if (XkbKeyHasActions(keymap, key) &&
177 XkbKeyGroupsWidth(keymap, key) >= needed)
178 return XkbKeyActionsPtr(keymap, key);
181 * The key may already be in the array, but without enough space.
182 * This should not happen often, so in order to avoid moving and
183 * copying stuff from acts and key_acts, we just allocate new
184 * space for the key at the end, and leave the old space alone.
187 old_ndx = darray_item(keymap->server->key_acts, key);
188 old_num_acts = XkbKeyNumActions(keymap, key);
189 new_ndx = darray_size(keymap->server->acts);
191 darray_resize0(keymap->server->acts, new_ndx + needed);
192 darray_item(keymap->server->key_acts, key) = new_ndx;
195 * The key was already in the array, copy the old actions to the
199 memcpy(darray_mem(keymap->server->acts, new_ndx),
200 darray_mem(keymap->server->acts, old_ndx),
201 old_num_acts * sizeof(union xkb_action));
203 return XkbKeyActionsPtr(keymap, key);
207 XkbcFreeClientMap(struct xkb_keymap *keymap)
209 struct xkb_client_map * map;
210 struct xkb_key_type * type;
211 struct xkb_sym_map *sym_map;
213 if (!keymap || !keymap->map)
218 darray_foreach(type, map->types) {
220 darray_free(type->map);
221 free(type->preserve);
222 for (j = 0; j < type->num_levels; j++)
223 free(type->level_names[j]);
224 free(type->level_names);
227 darray_free(map->types);
229 darray_foreach(sym_map, map->key_sym_map) {
230 free(sym_map->sym_index);
231 free(sym_map->num_syms);
232 darray_free(sym_map->syms);
234 darray_free(map->key_sym_map);
242 XkbcFreeServerMap(struct xkb_keymap *keymap)
244 struct xkb_server_map * map;
246 if (!keymap || !keymap->server)
249 map = keymap->server;
252 darray_free(map->key_acts);
253 darray_free(map->acts);
254 free(map->behaviors);
256 free(keymap->server);
257 keymap->server = NULL;
261 XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
266 if (!keymap->compat) {
267 keymap->compat = calloc(1, sizeof(*keymap->compat));
270 darray_init(keymap->compat->sym_interpret);
273 darray_growalloc(keymap->compat->sym_interpret, nSI);
274 darray_resize(keymap->compat->sym_interpret, 0);
276 memset(keymap->compat->groups, 0,
277 XkbNumKbdGroups * sizeof(*keymap->compat->groups));
284 XkbcFreeCompatMap(struct xkb_keymap *keymap)
286 if (!keymap || !keymap->compat)
289 darray_free(keymap->compat->sym_interpret);
290 free(keymap->compat);
291 keymap->compat = NULL;
295 XkbcAllocNames(struct xkb_keymap *keymap, unsigned which, size_t nTotalAliases)
300 if (!keymap->names) {
301 keymap->names = calloc(1, sizeof(*keymap->names));
305 darray_init(keymap->names->keys);
306 darray_init(keymap->names->key_aliases);
309 if ((which & XkbKTLevelNamesMask) && keymap->map) {
310 struct xkb_key_type * type;
312 darray_foreach(type, keymap->map->types) {
313 if (!type->level_names) {
314 type->level_names = calloc(type->num_levels,
315 sizeof(*type->level_names));
316 if (!type->level_names)
322 if (which & XkbKeyNamesMask)
323 darray_resize0(keymap->names->keys, keymap->max_key_code + 1);
325 if (which & XkbKeyAliasesMask)
326 darray_resize0(keymap->names->key_aliases, nTotalAliases);
332 XkbcFreeNames(struct xkb_keymap *keymap)
334 struct xkb_names * names;
335 struct xkb_client_map * map;
336 struct xkb_key_type *type;
339 if (!keymap || !keymap->names)
342 names = keymap->names;
346 darray_foreach(type, map->types) {
348 for (j = 0; j < type->num_levels; j++)
349 free(type->level_names[j]);
350 free(type->level_names);
351 type->level_names = NULL;
355 free(names->keycodes);
356 free(names->symbols);
357 free(names->keytypes);
360 for (i = 0; i < XkbNumVirtualMods; i++)
361 free(names->vmods[i]);
362 for (i = 0; i < XkbNumIndicators; i++)
363 free(names->indicators[i]);
364 for (i = 0; i < XkbNumKbdGroups; i++)
365 free(names->groups[i]);
367 darray_free(names->keys);
368 darray_free(names->key_aliases);
370 keymap->names = NULL;
374 XkbcAllocControls(struct xkb_keymap *keymap)
379 if (!keymap->ctrls) {
380 keymap->ctrls = uTypedCalloc(1, struct xkb_controls);
385 keymap->ctrls->per_key_repeat = uTypedCalloc(keymap->max_key_code >> 3,
387 if (!keymap->ctrls->per_key_repeat)
394 XkbcFreeControls(struct xkb_keymap *keymap)
396 if (keymap && keymap->ctrls) {
397 free(keymap->ctrls->per_key_repeat);
399 keymap->ctrls = NULL;
404 XkbcAllocIndicatorMaps(struct xkb_keymap *keymap)
409 if (!keymap->indicators) {
410 keymap->indicators = uTypedCalloc(1, struct xkb_indicator);
411 if (!keymap->indicators)
419 XkbcFreeIndicatorMaps(struct xkb_keymap *keymap)
422 free(keymap->indicators);
423 keymap->indicators = NULL;
428 XkbcAllocKeyboard(struct xkb_context *ctx)
430 struct xkb_keymap *keymap;
432 keymap = uTypedCalloc(1, struct xkb_keymap);
437 keymap->ctx = xkb_context_ref(ctx);
443 XkbcFreeKeyboard(struct xkb_keymap *keymap)
448 XkbcFreeClientMap(keymap);
449 XkbcFreeServerMap(keymap);
450 XkbcFreeCompatMap(keymap);
451 XkbcFreeIndicatorMaps(keymap);
452 XkbcFreeNames(keymap);
453 XkbcFreeControls(keymap);
454 xkb_context_unref(keymap->ctx);