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 bzero(&map->types[map->num_types],
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 bzero(&map->acts[map->num_acts],
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 bzero(&xkb->map->syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)],
280 (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 bzero(&newSyms[nSyms+nCopy], (nKeySyms - nCopy) * sizeof(uint32_t));
310 xkb->map->key_sym_map[i].offset = nSyms;
314 free(xkb->map->syms);
315 xkb->map->syms = newSyms;
316 xkb->map->num_syms = nSyms;
318 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
322 XkbcResizeKeyActions(struct xkb_desc * xkb, xkb_keycode_t key, int needed)
324 xkb_keycode_t i, nActs;
325 union xkb_action *newActs;
328 xkb->server->key_acts[key] = 0;
332 if (XkbKeyHasActions(xkb, key) &&
333 (XkbKeyNumSyms(xkb, key) >= (unsigned)needed))
334 return XkbKeyActionsPtr(xkb, key);
336 if (xkb->server->size_acts - xkb->server->num_acts >= (unsigned)needed) {
337 xkb->server->key_acts[key] = xkb->server->num_acts;
338 xkb->server->num_acts += needed;
340 return &xkb->server->acts[xkb->server->key_acts[key]];
343 xkb->server->size_acts = xkb->server->num_acts + needed + 8;
344 newActs = _XkbTypedCalloc(xkb->server->size_acts, union xkb_action);
347 newActs[0].type = XkbSA_NoAction;
350 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
351 xkb_keycode_t nKeyActs, nCopy;
353 if ((xkb->server->key_acts[i] == 0) && (i != key))
356 nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
364 memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
365 nCopy * sizeof(union xkb_action));
366 if (nCopy < nKeyActs)
367 bzero(&newActs[nActs + nCopy],
368 (nKeyActs - nCopy) * sizeof(union xkb_action));
370 xkb->server->key_acts[i] = nActs;
374 free(xkb->server->acts);
375 xkb->server->acts = newActs;
376 xkb->server->num_acts = nActs;
378 return &xkb->server->acts[xkb->server->key_acts[key]];
382 XkbcFreeClientMap(struct xkb_desc * xkb, unsigned what, Bool freeMap)
384 struct xkb_client_map * map;
386 if (!xkb || !xkb->map)
390 what = XkbAllClientInfoMask;
393 if (what & XkbKeyTypesMask) {
395 struct xkb_key_type * type;
397 for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
400 free(type->preserve);
401 type->preserve = NULL;
403 free(type->level_names);
404 type->level_names = NULL;
407 map->num_types = map->size_types = 0;
411 if (what & XkbKeySymsMask) {
412 free(map->key_sym_map);
413 map->key_sym_map = NULL;
415 map->size_syms = map->num_syms = 0;
419 if (what & XkbModifierMapMask) {
431 XkbcFreeServerMap(struct xkb_desc * xkb, unsigned what, Bool freeMap)
433 struct xkb_server_map * map;
435 if (!xkb || !xkb->server)
439 what = XkbAllServerInfoMask;
442 if (what & XkbExplicitComponentsMask) {
444 map->explicit = NULL;
447 if (what & XkbKeyActionsMask) {
449 map->key_acts = NULL;
451 map->num_acts = map->size_acts = 0;
455 if (what & XkbKeyBehaviorsMask) {
456 free(map->behaviors);
457 map->behaviors = NULL;
460 if (what & XkbVirtualModMapMask) {