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 struct xkb_client_map * map;
35 if (!keymap || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
39 map = uTypedCalloc(1, struct xkb_client_map);
47 if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
49 map->types = uTypedCalloc(nTotalTypes, struct xkb_key_type);
54 map->size_types = nTotalTypes;
56 else if (map->size_types < nTotalTypes) {
57 struct xkb_key_type *prev_types = map->types;
59 map->types = uTypedRealloc(map->types, nTotalTypes,
63 map->num_types = map->size_types = 0;
67 map->size_types = nTotalTypes;
68 memset(&map->types[map->num_types], 0,
69 (map->size_types - map->num_types) * sizeof(struct xkb_key_type));
73 if (which & XkbKeySymsMask) {
74 if (!map->key_sym_map) {
75 map->key_sym_map = uTypedCalloc(keymap->max_key_code + 1,
77 if (!map->key_sym_map)
82 if (which & XkbModifierMapMask) {
84 map->modmap = uTypedCalloc(keymap->max_key_code + 1,
95 XkbcAllocServerMap(struct xkb_keymap *keymap, unsigned which,
99 struct xkb_server_map * map;
104 if (!keymap->server) {
105 map = uTypedCalloc(1, struct xkb_server_map);
109 for (i = 0; i < XkbNumVirtualMods; i++)
110 map->vmods[i] = XkbNoModifierMask;
112 keymap->server = map;
115 map = keymap->server;
120 if (!map->explicit) {
121 i = keymap->max_key_code + 1;
122 map->explicit = uTypedCalloc(i, unsigned char);
131 map->acts = uTypedCalloc(nNewActions + 1, union xkb_action);
135 map->size_acts = nNewActions + 1;
137 else if ((map->size_acts - map->num_acts) < (int)nNewActions) {
139 union xkb_action *prev_acts = map->acts;
141 need = map->num_acts + nNewActions;
142 map->acts = uTypedRealloc(map->acts, need, union xkb_action);
145 map->num_acts = map->size_acts = 0;
149 map->size_acts = need;
150 memset(&map->acts[map->num_acts], 0,
151 (map->size_acts - map->num_acts) * sizeof(union xkb_action));
154 if (!map->key_acts) {
155 i = keymap->max_key_code + 1;
156 map->key_acts = uTypedCalloc(i, unsigned short);
161 if (!map->behaviors) {
162 i = keymap->max_key_code + 1;
163 map->behaviors = uTypedCalloc(i, struct xkb_behavior);
169 i = keymap->max_key_code + 1;
170 map->vmodmap = uTypedCalloc(i, uint32_t);
179 XkbcCopyKeyType(struct xkb_key_type * from, struct xkb_key_type * into)
188 free(into->preserve);
189 into->preserve= NULL;
190 for (i = 0; i < into->num_levels; i++)
191 free(UNCONSTIFY(into->level_names[i]));
192 free(into->level_names);
193 into->level_names = NULL;
197 if (from->map && (into->map_count > 0)) {
198 into->map = uTypedCalloc(into->map_count, struct xkb_kt_map_entry);
201 memcpy(into->map, from->map,
202 into->map_count * sizeof(struct xkb_kt_map_entry));
205 if (from->preserve && (into->map_count > 0)) {
206 into->preserve = uTypedCalloc(into->map_count, struct xkb_mods);
209 memcpy(into->preserve, from->preserve,
210 into->map_count * sizeof(struct xkb_mods));
213 if (from->level_names && (into->num_levels > 0)) {
214 into->level_names = uTypedCalloc(into->num_levels, const char *);
215 if (!into->level_names)
217 for (i = 0; i < into->num_levels; i++)
218 into->level_names[i] = strdup(from->level_names[i]);
225 XkbcResizeKeySyms(struct xkb_keymap *keymap, xkb_keycode_t key,
228 if (keymap->map->key_sym_map[key].size_syms >= needed)
231 keymap->map->key_sym_map[key].syms =
232 uTypedRecalloc(keymap->map->key_sym_map[key].syms,
233 keymap->map->key_sym_map[key].size_syms,
236 if (!keymap->map->key_sym_map[key].syms) {
237 keymap->map->key_sym_map[key].size_syms = 0;
240 keymap->map->key_sym_map[key].size_syms = needed;
246 XkbcResizeKeyActions(struct xkb_keymap *keymap, xkb_keycode_t key,
249 xkb_keycode_t i, nActs;
250 union xkb_action *newActs;
253 keymap->server->key_acts[key] = 0;
257 if (XkbKeyHasActions(keymap, key) &&
258 (XkbKeyGroupsWidth(keymap, key) >= needed))
259 return XkbKeyActionsPtr(keymap, key);
261 if (keymap->server->size_acts - keymap->server->num_acts >= (int)needed) {
262 keymap->server->key_acts[key] = keymap->server->num_acts;
263 keymap->server->num_acts += needed;
265 return &keymap->server->acts[keymap->server->key_acts[key]];
268 keymap->server->size_acts = keymap->server->num_acts + needed + 8;
269 newActs = uTypedCalloc(keymap->server->size_acts, union xkb_action);
272 newActs[0].type = XkbSA_NoAction;
275 for (i = keymap->min_key_code; i <= keymap->max_key_code; i++) {
276 xkb_keycode_t nKeyActs, nCopy;
278 if ((keymap->server->key_acts[i] == 0) && (i != key))
281 nCopy = nKeyActs = XkbKeyNumActions(keymap, i);
289 memcpy(&newActs[nActs], XkbKeyActionsPtr(keymap, i),
290 nCopy * sizeof(union xkb_action));
291 if (nCopy < nKeyActs)
292 memset(&newActs[nActs + nCopy], 0,
293 (nKeyActs - nCopy) * sizeof(union xkb_action));
295 keymap->server->key_acts[i] = nActs;
299 free(keymap->server->acts);
300 keymap->server->acts = newActs;
301 keymap->server->num_acts = nActs;
303 return &keymap->server->acts[keymap->server->key_acts[key]];
307 XkbcFreeClientMap(struct xkb_keymap *keymap)
309 struct xkb_client_map * map;
310 struct xkb_key_type * type;
314 if (!keymap || !keymap->map)
319 for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
322 free(type->preserve);
323 for (j = 0; j < type->num_levels; j++)
324 free(UNCONSTIFY(type->level_names[j]));
325 free(type->level_names);
326 free(UNCONSTIFY(type->name));
330 if (map->key_sym_map) {
331 for (key = keymap->min_key_code; key < keymap->max_key_code; key++) {
332 free(map->key_sym_map[key].sym_index);
333 free(map->key_sym_map[key].num_syms);
334 free(map->key_sym_map[key].syms);
337 free(map->key_sym_map);
345 XkbcFreeServerMap(struct xkb_keymap *keymap)
347 struct xkb_server_map * map;
349 if (keymap || keymap->server)
352 map = keymap->server;
357 free(map->behaviors);
359 free(keymap->server);
360 keymap->server = NULL;
364 XkbcAllocCompatMap(struct xkb_keymap *keymap, unsigned nSI)
366 struct xkb_compat_map * compat;
367 struct xkb_sym_interpret *prev_interpret;
372 if (keymap->compat) {
373 if (keymap->compat->size_si >= nSI)
376 compat = keymap->compat;
377 compat->size_si = nSI;
378 if (!compat->sym_interpret)
381 prev_interpret = compat->sym_interpret;
382 compat->sym_interpret = uTypedRecalloc(compat->sym_interpret,
384 struct xkb_sym_interpret);
385 if (!compat->sym_interpret) {
386 free(prev_interpret);
387 compat->size_si = compat->num_si = 0;
394 compat = uTypedCalloc(1, struct xkb_compat_map);
399 compat->sym_interpret = uTypedCalloc(nSI, struct xkb_sym_interpret);
400 if (!compat->sym_interpret) {
405 compat->size_si = nSI;
407 memset(&compat->groups[0], 0, XkbNumKbdGroups * sizeof(struct xkb_mods));
408 keymap->compat = compat;
415 XkbcFreeCompatMap(struct xkb_keymap *keymap)
417 struct xkb_compat_map * compat;
419 if (!keymap || !keymap->compat)
422 compat = keymap->compat;
424 free(compat->sym_interpret);
426 keymap->compat = NULL;
430 XkbcAllocNames(struct xkb_keymap *keymap, unsigned which,
431 unsigned nTotalAliases)
433 struct xkb_names * names;
438 if (!keymap->names) {
439 keymap->names = uTypedCalloc(1, struct xkb_names);
443 names = keymap->names;
445 if ((which & XkbKTLevelNamesMask) && keymap->map && keymap->map->types) {
447 struct xkb_key_type * type;
449 type = keymap->map->types;
450 for (i = 0; i < keymap->map->num_types; i++, type++) {
451 if (!type->level_names) {
452 type->level_names = uTypedCalloc(type->num_levels, const char *);
453 if (!type->level_names)
459 if ((which & XkbKeyNamesMask) && !names->keys) {
460 names->keys = uTypedCalloc(keymap->max_key_code + 1,
461 struct xkb_key_name);
466 if ((which & XkbKeyAliasesMask) && (nTotalAliases > 0)) {
467 if (!names->key_aliases)
468 names->key_aliases = uTypedCalloc(nTotalAliases,
469 struct xkb_key_alias);
470 else if (nTotalAliases > names->num_key_aliases) {
471 struct xkb_key_alias *prev_aliases = names->key_aliases;
473 names->key_aliases = uTypedRecalloc(names->key_aliases,
474 names->num_key_aliases,
476 struct xkb_key_alias);
477 if (!names->key_aliases)
481 if (!names->key_aliases) {
482 names->num_key_aliases = 0;
486 names->num_key_aliases = nTotalAliases;
493 XkbcFreeNames(struct xkb_keymap *keymap)
495 struct xkb_names * names;
496 struct xkb_client_map * map;
499 if (!keymap || !keymap->names)
502 names = keymap->names;
505 if (map && map->types) {
506 struct xkb_key_type * type = map->types;
508 for (i = 0; i < map->num_types; i++, type++) {
510 for (j = 0; j < type->num_levels; j++)
511 free(UNCONSTIFY(type->level_names[i]));
512 free(type->level_names);
513 type->level_names = NULL;
517 for (i = 0; i < XkbNumVirtualMods; i++)
518 free(UNCONSTIFY(names->vmods[i]));
519 for (i = 0; i < XkbNumIndicators; i++)
520 free(UNCONSTIFY(names->indicators[i]));
521 for (i = 0; i < XkbNumKbdGroups; i++)
522 free(UNCONSTIFY(names->groups[i]));
525 free(names->key_aliases);
527 keymap->names = NULL;
531 XkbcAllocControls(struct xkb_keymap *keymap)
536 if (!keymap->ctrls) {
537 keymap->ctrls = uTypedCalloc(1, struct xkb_controls);
542 keymap->ctrls->per_key_repeat = uTypedCalloc(keymap->max_key_code >> 3,
544 if (!keymap->ctrls->per_key_repeat)
551 XkbcFreeControls(struct xkb_keymap *keymap)
553 if (keymap && keymap->ctrls) {
554 free(keymap->ctrls->per_key_repeat);
556 keymap->ctrls = NULL;
561 XkbcAllocIndicatorMaps(struct xkb_keymap *keymap)
566 if (!keymap->indicators) {
567 keymap->indicators = uTypedCalloc(1, struct xkb_indicator);
568 if (!keymap->indicators)
576 XkbcFreeIndicatorMaps(struct xkb_keymap *keymap)
579 free(keymap->indicators);
580 keymap->indicators = NULL;
585 XkbcAllocKeyboard(struct xkb_context *ctx)
587 struct xkb_keymap *keymap;
589 keymap = uTypedCalloc(1, struct xkb_keymap);
594 keymap->ctx = xkb_context_ref(ctx);
600 XkbcFreeKeyboard(struct xkb_keymap *keymap)
605 XkbcFreeClientMap(keymap);
606 XkbcFreeServerMap(keymap);
607 XkbcFreeCompatMap(keymap);
608 XkbcFreeIndicatorMaps(keymap);
609 XkbcFreeNames(keymap);
610 XkbcFreeControls(keymap);
611 xkb_context_unref(keymap->ctx);