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)
224 free(into->preserve);
225 into->preserve= NULL;
226 for (i = 0; i < into->num_levels; i++)
227 free((char *) into->level_names[i]);
228 free(into->level_names);
229 into->level_names = NULL;
233 if (from->map && (into->map_count > 0)) {
234 into->map = _XkbTypedCalloc(into->map_count, struct xkb_kt_map_entry);
237 memcpy(into->map, from->map,
238 into->map_count * sizeof(struct xkb_kt_map_entry));
241 if (from->preserve && (into->map_count > 0)) {
242 into->preserve = _XkbTypedCalloc(into->map_count, struct xkb_mods);
245 memcpy(into->preserve, from->preserve,
246 into->map_count * sizeof(struct xkb_mods));
249 if (from->level_names && (into->num_levels > 0)) {
250 into->level_names = _XkbTypedCalloc(into->num_levels, const char *);
251 if (!into->level_names)
253 for (i = 0; i < into->num_levels; i++)
254 into->level_names[i] = strdup(from->level_names[i]);
261 XkbcResizeKeySyms(struct xkb_desc * xkb, xkb_keycode_t key,
264 uint32_t i, nSyms, nKeySyms;
269 xkb->map->key_sym_map[key].offset = 0;
270 return xkb->map->syms;
273 nOldSyms = XkbKeyNumSyms(xkb, key);
274 if (nOldSyms >= needed)
275 return XkbKeySymsPtr(xkb, key);
277 if (xkb->map->size_syms - xkb->map->num_syms >= needed) {
279 memcpy(&xkb->map->syms[xkb->map->num_syms],
280 XkbKeySymsPtr(xkb, key), nOldSyms * sizeof(uint32_t));
282 if ((needed - nOldSyms) > 0)
283 memset(&xkb->map->syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)],
284 0, (needed - nOldSyms) * sizeof(uint32_t));
286 xkb->map->key_sym_map[key].offset = xkb->map->num_syms;
287 xkb->map->num_syms += needed;
289 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
292 xkb->map->size_syms += (needed > 32 ? needed : 32);
293 newSyms = _XkbTypedCalloc(xkb->map->size_syms, uint32_t);
297 newSyms[0] = NoSymbol;
299 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
302 nCopy = nKeySyms = XkbKeyNumSyms(xkb, i);
303 if ((nKeySyms == 0) && (i != key))
309 memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
310 nCopy * sizeof(uint32_t));
311 if (nKeySyms > nCopy)
312 memset(&newSyms[nSyms + nCopy], 0,
313 (nKeySyms - nCopy) * sizeof(uint32_t));
315 xkb->map->key_sym_map[i].offset = nSyms;
319 free(xkb->map->syms);
320 xkb->map->syms = newSyms;
321 xkb->map->num_syms = nSyms;
323 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
327 XkbcResizeKeyActions(struct xkb_desc * xkb, xkb_keycode_t key, int needed)
329 xkb_keycode_t i, nActs;
330 union xkb_action *newActs;
333 xkb->server->key_acts[key] = 0;
337 if (XkbKeyHasActions(xkb, key) &&
338 (XkbKeyNumSyms(xkb, key) >= (unsigned)needed))
339 return XkbKeyActionsPtr(xkb, key);
341 if (xkb->server->size_acts - xkb->server->num_acts >= (unsigned)needed) {
342 xkb->server->key_acts[key] = xkb->server->num_acts;
343 xkb->server->num_acts += needed;
345 return &xkb->server->acts[xkb->server->key_acts[key]];
348 xkb->server->size_acts = xkb->server->num_acts + needed + 8;
349 newActs = _XkbTypedCalloc(xkb->server->size_acts, union xkb_action);
352 newActs[0].type = XkbSA_NoAction;
355 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
356 xkb_keycode_t nKeyActs, nCopy;
358 if ((xkb->server->key_acts[i] == 0) && (i != key))
361 nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
369 memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
370 nCopy * sizeof(union xkb_action));
371 if (nCopy < nKeyActs)
372 memset(&newActs[nActs + nCopy], 0,
373 (nKeyActs - nCopy) * sizeof(union xkb_action));
375 xkb->server->key_acts[i] = nActs;
379 free(xkb->server->acts);
380 xkb->server->acts = newActs;
381 xkb->server->num_acts = nActs;
383 return &xkb->server->acts[xkb->server->key_acts[key]];
387 XkbcFreeClientMap(struct xkb_desc * xkb)
390 struct xkb_client_map * map;
391 struct xkb_key_type * type;
393 if (!xkb || !xkb->map)
398 for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
401 free(type->preserve);
402 for (j = 0; j < type->num_levels; j++)
403 free((char *) type->level_names[j]);
404 free(type->level_names);
405 free((char *) type->name);
408 free(map->key_sym_map);
416 XkbcFreeServerMap(struct xkb_desc * xkb)
418 struct xkb_server_map * map;
420 if (!xkb || !xkb->server)
428 free(map->behaviors);