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 struct xkb_sym_map *sym_map = &darray_item(keymap->map->key_sym_map, key);
163 if (sym_map->size_syms >= needed)
166 sym_map->syms = uTypedRecalloc(sym_map->syms, sym_map->size_syms,
167 needed, xkb_keysym_t);
168 if (!sym_map->syms) {
169 sym_map->size_syms = 0;
173 sym_map->size_syms = needed;
178 XkbcResizeKeyActions(struct xkb_keymap *keymap, xkb_keycode_t key,
181 size_t old_ndx, old_num_acts, new_ndx;
184 darray_item(keymap->server->key_acts, key) = 0;
188 if (XkbKeyHasActions(keymap, key) &&
189 XkbKeyGroupsWidth(keymap, key) >= needed)
190 return XkbKeyActionsPtr(keymap, key);
193 * The key may already be in the array, but without enough space.
194 * This should not happen often, so in order to avoid moving and
195 * copying stuff from acts and key_acts, we just allocate new
196 * space for the key at the end, and leave the old space alone.
199 old_ndx = darray_item(keymap->server->key_acts, key);
200 old_num_acts = XkbKeyNumActions(keymap, key);
201 new_ndx = darray_size(keymap->server->acts);
203 darray_resize0(keymap->server->acts, new_ndx + needed);
204 darray_item(keymap->server->key_acts, key) = new_ndx;
207 * The key was already in the array, copy the old actions to the
211 memcpy(darray_mem(keymap->server->acts, new_ndx),
212 darray_mem(keymap->server->acts, old_ndx),
213 old_num_acts * sizeof(union xkb_action));
215 return XkbKeyActionsPtr(keymap, key);
219 XkbcFreeClientMap(struct xkb_keymap *keymap)
221 struct xkb_client_map * map;
222 struct xkb_key_type * type;
223 struct xkb_sym_map *sym_map;
225 if (!keymap || !keymap->map)
230 darray_foreach(type, map->types) {
232 darray_free(type->map);
233 free(type->preserve);
234 for (j = 0; j < type->num_levels; j++)
235 free(type->level_names[j]);
236 free(type->level_names);
239 darray_free(map->types);
241 darray_foreach(sym_map, map->key_sym_map) {
242 free(sym_map->sym_index);
243 free(sym_map->num_syms);
246 darray_free(map->key_sym_map);
254 XkbcFreeServerMap(struct xkb_keymap *keymap)
256 struct xkb_server_map * map;
258 if (!keymap || !keymap->server)
261 map = keymap->server;
264 darray_free(map->key_acts);
265 darray_free(map->acts);
266 free(map->behaviors);
268 free(keymap->server);
269 keymap->server = NULL;
273 XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
278 if (!keymap->compat) {
279 keymap->compat = calloc(1, sizeof(*keymap->compat));
282 darray_init(keymap->compat->sym_interpret);
285 darray_growalloc(keymap->compat->sym_interpret, nSI);
286 darray_resize(keymap->compat->sym_interpret, 0);
288 memset(keymap->compat->groups, 0,
289 XkbNumKbdGroups * sizeof(*keymap->compat->groups));
296 XkbcFreeCompatMap(struct xkb_keymap *keymap)
298 if (!keymap || !keymap->compat)
301 darray_free(keymap->compat->sym_interpret);
302 free(keymap->compat);
303 keymap->compat = NULL;
307 XkbcAllocNames(struct xkb_keymap *keymap, unsigned which, size_t nTotalAliases)
312 if (!keymap->names) {
313 keymap->names = calloc(1, sizeof(*keymap->names));
317 darray_init(keymap->names->keys);
318 darray_init(keymap->names->key_aliases);
321 if ((which & XkbKTLevelNamesMask) && keymap->map) {
322 struct xkb_key_type * type;
324 darray_foreach(type, keymap->map->types) {
325 if (!type->level_names) {
326 type->level_names = calloc(type->num_levels,
327 sizeof(*type->level_names));
328 if (!type->level_names)
334 if (which & XkbKeyNamesMask)
335 darray_resize0(keymap->names->keys, keymap->max_key_code + 1);
337 if (which & XkbKeyAliasesMask)
338 darray_resize0(keymap->names->key_aliases, nTotalAliases);
344 XkbcFreeNames(struct xkb_keymap *keymap)
346 struct xkb_names * names;
347 struct xkb_client_map * map;
348 struct xkb_key_type *type;
351 if (!keymap || !keymap->names)
354 names = keymap->names;
358 darray_foreach(type, map->types) {
360 for (j = 0; j < type->num_levels; j++)
361 free(type->level_names[j]);
362 free(type->level_names);
363 type->level_names = NULL;
367 for (i = 0; i < XkbNumVirtualMods; i++)
368 free(names->vmods[i]);
369 for (i = 0; i < XkbNumIndicators; i++)
370 free(names->indicators[i]);
371 for (i = 0; i < XkbNumKbdGroups; i++)
372 free(names->groups[i]);
374 darray_free(names->keys);
375 darray_free(names->key_aliases);
377 keymap->names = NULL;
381 XkbcAllocControls(struct xkb_keymap *keymap)
386 if (!keymap->ctrls) {
387 keymap->ctrls = uTypedCalloc(1, struct xkb_controls);
392 keymap->ctrls->per_key_repeat = uTypedCalloc(keymap->max_key_code >> 3,
394 if (!keymap->ctrls->per_key_repeat)
401 XkbcFreeControls(struct xkb_keymap *keymap)
403 if (keymap && keymap->ctrls) {
404 free(keymap->ctrls->per_key_repeat);
406 keymap->ctrls = NULL;
411 XkbcAllocIndicatorMaps(struct xkb_keymap *keymap)
416 if (!keymap->indicators) {
417 keymap->indicators = uTypedCalloc(1, struct xkb_indicator);
418 if (!keymap->indicators)
426 XkbcFreeIndicatorMaps(struct xkb_keymap *keymap)
429 free(keymap->indicators);
430 keymap->indicators = NULL;
435 XkbcAllocKeyboard(struct xkb_context *ctx)
437 struct xkb_keymap *keymap;
439 keymap = uTypedCalloc(1, struct xkb_keymap);
444 keymap->ctx = xkb_context_ref(ctx);
450 XkbcFreeKeyboard(struct xkb_keymap *keymap)
455 XkbcFreeClientMap(keymap);
456 XkbcFreeServerMap(keymap);
457 XkbcFreeCompatMap(keymap);
458 XkbcFreeIndicatorMaps(keymap);
459 XkbcFreeNames(keymap);
460 XkbcFreeControls(keymap);
461 xkb_context_unref(keymap->ctx);