1 /************************************************************
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.
25 ********************************************************/
28 #include "xkbcommon/xkbcommon.h"
29 #include "XKBcommonint.h"
32 XkbcAllocClientMap(struct xkb_desc * xkb, unsigned which, unsigned nTotalTypes)
35 struct xkb_client_map * map;
37 if (!xkb || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
40 if ((which & XkbKeySymsMask) && !xkb_keymap_keycode_range_is_legal(xkb)) {
42 fprintf(stderr, "bad keycode (%d,%d) in XkbAllocClientMap\n",
43 xkb->min_key_code, xkb->max_key_code);
49 map = _XkbTypedCalloc(1, struct xkb_client_map);
57 if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
59 map->types = _XkbTypedCalloc(nTotalTypes, struct xkb_key_type);
64 map->size_types = nTotalTypes;
66 else if (map->size_types < nTotalTypes) {
67 struct xkb_key_type *prev_types = map->types;
69 map->types = _XkbTypedRealloc(map->types, nTotalTypes,
73 map->num_types = map->size_types = 0;
77 map->size_types = nTotalTypes;
78 memset(&map->types[map->num_types], 0,
79 (map->size_types - map->num_types) * sizeof(struct xkb_key_type));
83 if (which & XkbKeySymsMask) {
84 int nKeys = XkbNumKeys(xkb);
87 map->size_syms = (nKeys * 15) / 10;
88 map->syms = _XkbTypedCalloc(map->size_syms, uint32_t);
94 map->syms[0] = NoSymbol;
97 if (!map->key_sym_map) {
98 i = xkb->max_key_code + 1;
99 map->key_sym_map = _XkbTypedCalloc(i, struct xkb_sym_map);
100 if (!map->key_sym_map)
105 if (which & XkbModifierMapMask) {
106 if (!xkb_keymap_keycode_range_is_legal(xkb))
110 i = xkb->max_key_code + 1;
111 map->modmap = _XkbTypedCalloc(i, unsigned char);
121 XkbcAllocServerMap(struct xkb_desc * xkb, unsigned which, unsigned nNewActions)
124 struct xkb_server_map * map;
130 map = _XkbTypedCalloc(1, struct xkb_server_map);
134 for (i = 0; i < XkbNumVirtualMods; i++)
135 map->vmods[i] = XkbNoModifierMask;
145 if (!xkb_keymap_keycode_range_is_legal(xkb))
148 if (which & XkbExplicitComponentsMask) {
149 if (!map->explicit) {
150 i = xkb->max_key_code + 1;
151 map->explicit = _XkbTypedCalloc(i, unsigned char);
157 if (which&XkbKeyActionsMask) {
162 map->acts = _XkbTypedCalloc(nNewActions + 1, union xkb_action);
166 map->size_acts = nNewActions + 1;
168 else if ((map->size_acts - map->num_acts) < nNewActions) {
170 union xkb_action *prev_acts = map->acts;
172 need = map->num_acts + nNewActions;
173 map->acts = _XkbTypedRealloc(map->acts, need, union xkb_action);
176 map->num_acts = map->size_acts = 0;
180 map->size_acts = need;
181 memset(&map->acts[map->num_acts], 0,
182 (map->size_acts - map->num_acts) * sizeof(union xkb_action));
185 if (!map->key_acts) {
186 i = xkb->max_key_code + 1;
187 map->key_acts = _XkbTypedCalloc(i, unsigned short);
193 if (which & XkbKeyBehaviorsMask) {
194 if (!map->behaviors) {
195 i = xkb->max_key_code + 1;
196 map->behaviors = _XkbTypedCalloc(i, struct xkb_behavior);
202 if (which & XkbVirtualModMapMask) {
204 i = xkb->max_key_code + 1;
205 map->vmodmap = _XkbTypedCalloc(i, uint32_t);
215 XkbcCopyKeyType(struct xkb_key_type * from, struct xkb_key_type * into)
222 free(into->preserve);
223 into->preserve= NULL;
224 free(into->level_names);
225 into->level_names = NULL;
229 if (from->map && (into->map_count > 0)) {
230 into->map = _XkbTypedCalloc(into->map_count, struct xkb_kt_map_entry);
233 memcpy(into->map, from->map,
234 into->map_count * sizeof(struct xkb_kt_map_entry));
237 if (from->preserve && (into->map_count > 0)) {
238 into->preserve = _XkbTypedCalloc(into->map_count, struct xkb_mods);
241 memcpy(into->preserve, from->preserve,
242 into->map_count * sizeof(struct xkb_mods));
245 if (from->level_names && (into->num_levels > 0)) {
246 into->level_names = _XkbTypedCalloc(into->num_levels, uint32_t);
247 if (!into->level_names)
249 memcpy(into->level_names, from->level_names,
250 into->num_levels * sizeof(uint32_t));
257 XkbcResizeKeySyms(struct xkb_desc * xkb, xkb_keycode_t key,
260 uint32_t i, nSyms, nKeySyms;
265 xkb->map->key_sym_map[key].offset = 0;
266 return xkb->map->syms;
269 nOldSyms = XkbKeyNumSyms(xkb, key);
270 if (nOldSyms >= needed)
271 return XkbKeySymsPtr(xkb, key);
273 if (xkb->map->size_syms - xkb->map->num_syms >= needed) {
275 memcpy(&xkb->map->syms[xkb->map->num_syms],
276 XkbKeySymsPtr(xkb, key), nOldSyms * sizeof(uint32_t));
278 if ((needed - nOldSyms) > 0)
279 memset(&xkb->map->syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)],
280 0, (needed - nOldSyms) * sizeof(uint32_t));
282 xkb->map->key_sym_map[key].offset = xkb->map->num_syms;
283 xkb->map->num_syms += needed;
285 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
288 xkb->map->size_syms += (needed > 32 ? needed : 32);
289 newSyms = _XkbTypedCalloc(xkb->map->size_syms, uint32_t);
293 newSyms[0] = NoSymbol;
295 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
298 nCopy = nKeySyms = XkbKeyNumSyms(xkb, i);
299 if ((nKeySyms == 0) && (i != key))
305 memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
306 nCopy * sizeof(uint32_t));
307 if (nKeySyms > nCopy)
308 memset(&newSyms[nSyms + nCopy], 0,
309 (nKeySyms - nCopy) * sizeof(uint32_t));
311 xkb->map->key_sym_map[i].offset = nSyms;
315 free(xkb->map->syms);
316 xkb->map->syms = newSyms;
317 xkb->map->num_syms = nSyms;
319 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
323 XkbcResizeKeyActions(struct xkb_desc * xkb, xkb_keycode_t key, int needed)
325 xkb_keycode_t i, nActs;
326 union xkb_action *newActs;
329 xkb->server->key_acts[key] = 0;
333 if (XkbKeyHasActions(xkb, key) &&
334 (XkbKeyNumSyms(xkb, key) >= (unsigned)needed))
335 return XkbKeyActionsPtr(xkb, key);
337 if (xkb->server->size_acts - xkb->server->num_acts >= (unsigned)needed) {
338 xkb->server->key_acts[key] = xkb->server->num_acts;
339 xkb->server->num_acts += needed;
341 return &xkb->server->acts[xkb->server->key_acts[key]];
344 xkb->server->size_acts = xkb->server->num_acts + needed + 8;
345 newActs = _XkbTypedCalloc(xkb->server->size_acts, union xkb_action);
348 newActs[0].type = XkbSA_NoAction;
351 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
352 xkb_keycode_t nKeyActs, nCopy;
354 if ((xkb->server->key_acts[i] == 0) && (i != key))
357 nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
365 memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
366 nCopy * sizeof(union xkb_action));
367 if (nCopy < nKeyActs)
368 memset(&newActs[nActs + nCopy], 0,
369 (nKeyActs - nCopy) * sizeof(union xkb_action));
371 xkb->server->key_acts[i] = nActs;
375 free(xkb->server->acts);
376 xkb->server->acts = newActs;
377 xkb->server->num_acts = nActs;
379 return &xkb->server->acts[xkb->server->key_acts[key]];
383 XkbcFreeClientMap(struct xkb_desc * xkb, unsigned what, Bool freeMap)
385 struct xkb_client_map * map;
387 if (!xkb || !xkb->map)
391 what = XkbAllClientInfoMask;
394 if (what & XkbKeyTypesMask) {
396 struct xkb_key_type * type;
398 for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
401 free(type->preserve);
402 type->preserve = NULL;
404 free(type->level_names);
405 type->level_names = NULL;
408 map->num_types = map->size_types = 0;
412 if (what & XkbKeySymsMask) {
413 free(map->key_sym_map);
414 map->key_sym_map = NULL;
416 map->size_syms = map->num_syms = 0;
420 if (what & XkbModifierMapMask) {
432 XkbcFreeServerMap(struct xkb_desc * xkb, unsigned what, Bool freeMap)
434 struct xkb_server_map * map;
436 if (!xkb || !xkb->server)
440 what = XkbAllServerInfoMask;
443 if (what & XkbExplicitComponentsMask) {
445 map->explicit = NULL;
448 if (what & XkbKeyActionsMask) {
450 map->key_acts = NULL;
452 map->num_acts = map->size_acts = 0;
456 if (what & XkbKeyBehaviorsMask) {
457 free(map->behaviors);
458 map->behaviors = NULL;
461 if (what & XkbVirtualModMapMask) {