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 ********************************************************/
29 #include "xkbcommon/xkbcommon.h"
30 #include "XKBcommonint.h"
33 XkbcAllocClientMap(struct xkb_desc * xkb, unsigned which, unsigned nTotalTypes)
36 struct xkb_client_map * map;
38 if (!xkb || ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
41 if ((which & XkbKeySymsMask) && !xkb_keymap_keycode_range_is_legal(xkb)) {
43 fprintf(stderr, "bad keycode (%d,%d) in XkbAllocClientMap\n",
44 xkb->min_key_code, xkb->max_key_code);
50 map = uTypedCalloc(1, struct xkb_client_map);
58 if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
60 map->types = uTypedCalloc(nTotalTypes, struct xkb_key_type);
65 map->size_types = nTotalTypes;
67 else if (map->size_types < nTotalTypes) {
68 struct xkb_key_type *prev_types = map->types;
70 map->types = uTypedRealloc(map->types, nTotalTypes,
74 map->num_types = map->size_types = 0;
78 map->size_types = nTotalTypes;
79 memset(&map->types[map->num_types], 0,
80 (map->size_types - map->num_types) * sizeof(struct xkb_key_type));
84 if (which & XkbKeySymsMask) {
85 int nKeys = XkbNumKeys(xkb);
88 map->size_syms = (nKeys * 15) / 10;
89 map->syms = uTypedCalloc(map->size_syms, xkb_keysym_t);
95 map->syms[0] = NoSymbol;
98 if (!map->key_sym_map) {
99 i = xkb->max_key_code + 1;
100 map->key_sym_map = uTypedCalloc(i, struct xkb_sym_map);
101 if (!map->key_sym_map)
106 if (which & XkbModifierMapMask) {
107 if (!xkb_keymap_keycode_range_is_legal(xkb))
111 i = xkb->max_key_code + 1;
112 map->modmap = uTypedCalloc(i, unsigned char);
122 XkbcAllocServerMap(struct xkb_desc * xkb, unsigned which, unsigned nNewActions)
125 struct xkb_server_map * map;
131 map = uTypedCalloc(1, struct xkb_server_map);
135 for (i = 0; i < XkbNumVirtualMods; i++)
136 map->vmods[i] = XkbNoModifierMask;
146 if (!xkb_keymap_keycode_range_is_legal(xkb))
149 if (which & XkbExplicitComponentsMask) {
150 if (!map->explicit) {
151 i = xkb->max_key_code + 1;
152 map->explicit = uTypedCalloc(i, unsigned char);
158 if (which&XkbKeyActionsMask) {
163 map->acts = uTypedCalloc(nNewActions + 1, union xkb_action);
167 map->size_acts = nNewActions + 1;
169 else if ((map->size_acts - map->num_acts) < (int)nNewActions) {
171 union xkb_action *prev_acts = map->acts;
173 need = map->num_acts + nNewActions;
174 map->acts = uTypedRealloc(map->acts, need, union xkb_action);
177 map->num_acts = map->size_acts = 0;
181 map->size_acts = need;
182 memset(&map->acts[map->num_acts], 0,
183 (map->size_acts - map->num_acts) * sizeof(union xkb_action));
186 if (!map->key_acts) {
187 i = xkb->max_key_code + 1;
188 map->key_acts = uTypedCalloc(i, unsigned short);
194 if (which & XkbKeyBehaviorsMask) {
195 if (!map->behaviors) {
196 i = xkb->max_key_code + 1;
197 map->behaviors = uTypedCalloc(i, struct xkb_behavior);
203 if (which & XkbVirtualModMapMask) {
205 i = xkb->max_key_code + 1;
206 map->vmodmap = uTypedCalloc(i, uint32_t);
216 XkbcCopyKeyType(struct xkb_key_type * from, struct xkb_key_type * into)
225 free(into->preserve);
226 into->preserve= NULL;
227 for (i = 0; i < into->num_levels; i++)
228 free(UNCONSTIFY(into->level_names[i]));
229 free(into->level_names);
230 into->level_names = NULL;
234 if (from->map && (into->map_count > 0)) {
235 into->map = uTypedCalloc(into->map_count, struct xkb_kt_map_entry);
238 memcpy(into->map, from->map,
239 into->map_count * sizeof(struct xkb_kt_map_entry));
242 if (from->preserve && (into->map_count > 0)) {
243 into->preserve = uTypedCalloc(into->map_count, struct xkb_mods);
246 memcpy(into->preserve, from->preserve,
247 into->map_count * sizeof(struct xkb_mods));
250 if (from->level_names && (into->num_levels > 0)) {
251 into->level_names = uTypedCalloc(into->num_levels, const char *);
252 if (!into->level_names)
254 for (i = 0; i < into->num_levels; i++)
255 into->level_names[i] = strdup(from->level_names[i]);
262 XkbcResizeKeySyms(struct xkb_desc * xkb, xkb_keycode_t key,
265 uint32_t i, nSyms, nKeySyms;
267 xkb_keysym_t *newSyms;
270 xkb->map->key_sym_map[key].offset = 0;
271 return xkb->map->syms;
274 nOldSyms = XkbKeyNumSyms(xkb, key);
275 if (nOldSyms >= needed)
276 return XkbKeySymsPtr(xkb, key);
278 if (xkb->map->size_syms - xkb->map->num_syms >= needed) {
280 memcpy(&xkb->map->syms[xkb->map->num_syms],
281 XkbKeySymsPtr(xkb, key), nOldSyms * sizeof(xkb_keysym_t));
283 if ((needed - nOldSyms) > 0)
284 memset(&xkb->map->syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)],
285 0, (needed - nOldSyms) * sizeof(xkb_keysym_t));
287 xkb->map->key_sym_map[key].offset = xkb->map->num_syms;
288 xkb->map->num_syms += needed;
290 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
293 xkb->map->size_syms += (needed > 32 ? needed : 32);
294 newSyms = uTypedCalloc(xkb->map->size_syms, xkb_keysym_t);
298 newSyms[0] = NoSymbol;
300 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
303 nCopy = nKeySyms = XkbKeyNumSyms(xkb, i);
304 if ((nKeySyms == 0) && (i != key))
310 memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
311 nCopy * sizeof(xkb_keysym_t));
312 if (nKeySyms > nCopy)
313 memset(&newSyms[nSyms + nCopy], 0,
314 (nKeySyms - nCopy) * sizeof(xkb_keysym_t));
316 xkb->map->key_sym_map[i].offset = nSyms;
320 free(xkb->map->syms);
321 xkb->map->syms = newSyms;
322 xkb->map->num_syms = nSyms;
324 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
328 XkbcResizeKeyActions(struct xkb_desc * xkb, xkb_keycode_t key, uint32_t needed)
330 xkb_keycode_t i, nActs;
331 union xkb_action *newActs;
334 xkb->server->key_acts[key] = 0;
338 if (XkbKeyHasActions(xkb, key) &&
339 (XkbKeyNumSyms(xkb, key) >= (int)needed))
340 return XkbKeyActionsPtr(xkb, key);
342 if (xkb->server->size_acts - xkb->server->num_acts >= (int)needed) {
343 xkb->server->key_acts[key] = xkb->server->num_acts;
344 xkb->server->num_acts += needed;
346 return &xkb->server->acts[xkb->server->key_acts[key]];
349 xkb->server->size_acts = xkb->server->num_acts + needed + 8;
350 newActs = uTypedCalloc(xkb->server->size_acts, union xkb_action);
353 newActs[0].type = XkbSA_NoAction;
356 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
357 xkb_keycode_t nKeyActs, nCopy;
359 if ((xkb->server->key_acts[i] == 0) && (i != key))
362 nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
370 memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
371 nCopy * sizeof(union xkb_action));
372 if (nCopy < nKeyActs)
373 memset(&newActs[nActs + nCopy], 0,
374 (nKeyActs - nCopy) * sizeof(union xkb_action));
376 xkb->server->key_acts[i] = nActs;
380 free(xkb->server->acts);
381 xkb->server->acts = newActs;
382 xkb->server->num_acts = nActs;
384 return &xkb->server->acts[xkb->server->key_acts[key]];
388 XkbcFreeClientMap(struct xkb_desc * xkb)
391 struct xkb_client_map * map;
392 struct xkb_key_type * type;
394 if (!xkb || !xkb->map)
399 for (i = 0, type = map->types; i < map->num_types && type; i++, type++) {
402 free(type->preserve);
403 for (j = 0; j < type->num_levels; j++)
404 free(UNCONSTIFY(type->level_names[j]));
405 free(type->level_names);
406 free(UNCONSTIFY(type->name));
409 free(map->key_sym_map);
417 XkbcFreeServerMap(struct xkb_desc * xkb)
419 struct xkb_server_map * map;
421 if (!xkb || !xkb->server)
429 free(map->behaviors);