Debian Codegraph @

xkb/XKBMAlloc.c

raw symbols SCIP
1 /************************************************************
2 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
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.
15
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.
24
25 ********************************************************/
26
27 #ifdef HAVE_DIX_CONFIG_H
28 #include <dix-config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <X11/X.h>
33 #include <X11/Xproto.h>
34 #include "misc.h"
35 #include "inputstr.h"
36 #include <X11/keysym.h>
37 #include <xkbsrv.h>
38
39 /***====================================================================***/
40
41 Status
42 XkbAllocClientMap(XkbDescPtr xkb, unsigned which, unsigned nTotalTypes)
43 {
44 XkbClientMapPtr map;
45
46 if ((xkb == NULL) ||
47 ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
48 return BadValue;
49 if ((which & XkbKeySymsMask) &&
50 ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
51 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
52 (xkb->max_key_code < xkb->min_key_code))) {
53 DebugF("bad keycode (%d,%d) in XkbAllocClientMap\n",
54 xkb->min_key_code, xkb->max_key_code);
55 return BadValue;
56 }
57
58 if (xkb->map == NULL) {
59 map = calloc(1, sizeof(XkbClientMapRec));
60 if (map == NULL)
61 return BadAlloc;
62 xkb->map = map;
63 }
64 else
65 map = xkb->map;
66
67 if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
68 if (map->types == NULL) {
69 map->types = calloc(nTotalTypes, sizeof(XkbKeyTypeRec));
70 if (map->types == NULL)
71 return BadAlloc;
72 map->num_types = 0;
73 map->size_types = nTotalTypes;
74 }
75 else if (map->size_types < nTotalTypes) {
76 XkbKeyTypeRec *prev_types = map->types;
77
78 map->types =
79 reallocarray(map->types, nTotalTypes, sizeof(XkbKeyTypeRec));
80 if (map->types == NULL) {
81 free(prev_types);
82 map->num_types = map->size_types = 0;
83 return BadAlloc;
84 }
85 map->size_types = nTotalTypes;
86 memset(&map->types[map->num_types], 0,
87 ((map->size_types -
88 map->num_types) * sizeof(XkbKeyTypeRec)));
89 }
90 }
91 if (which & XkbKeySymsMask) {
92 int nKeys = XkbNumKeys(xkb);
93
94 if (map->syms == NULL) {
95 map->size_syms = (nKeys * 15) / 10;
96 map->syms = calloc(map->size_syms, sizeof(KeySym));
97 if (!map->syms) {
98 map->size_syms = 0;
99 return BadAlloc;
100 }
101 map->num_syms = 1;
102 map->syms[0] = NoSymbol;
103 }
104 if (map->key_sym_map == NULL) {
105 map->key_sym_map = calloc(MAP_LENGTH, sizeof(XkbSymMapRec));
106 if (map->key_sym_map == NULL)
107 return BadAlloc;
108 }
109 }
110 if (which & XkbModifierMapMask) {
111 if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
112 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
113 (xkb->max_key_code < xkb->min_key_code))
114 return BadMatch;
115 if (map->modmap == NULL) {
116 map->modmap = calloc(MAP_LENGTH, sizeof(unsigned char));
117 if (map->modmap == NULL)
118 return BadAlloc;
119 }
120 }
121 return Success;
122 }
123
124 Status
125 XkbAllocServerMap(XkbDescPtr xkb, unsigned which, unsigned nNewActions)
126 {
127 register int i;
128 XkbServerMapPtr map;
129
130 if (xkb == NULL)
131 return BadMatch;
132 if (xkb->server == NULL) {
133 map = calloc(1, sizeof(XkbServerMapRec));
134 if (map == NULL)
135 return BadAlloc;
136 for (i = 0; i < XkbNumVirtualMods; i++) {
137 map->vmods[i] = XkbNoModifierMask;
138 }
139 xkb->server = map;
140 }
141 else
142 map = xkb->server;
143 if (which & XkbExplicitComponentsMask) {
144 if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
145 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
146 (xkb->max_key_code < xkb->min_key_code))
147 return BadMatch;
148 if (map->explicit == NULL) {
149 map->explicit = calloc(MAP_LENGTH, sizeof(unsigned char));
150 if (map->explicit == NULL)
151 return BadAlloc;
152 }
153 }
154 if (which & XkbKeyActionsMask) {
155 if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
156 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
157 (xkb->max_key_code < xkb->min_key_code))
158 return BadMatch;
159 if (nNewActions < 1)
160 nNewActions = 1;
161 if (map->acts == NULL) {
162 map->acts = calloc((nNewActions + 1), sizeof(XkbAction));
163 if (map->acts == NULL)
164 return BadAlloc;
165 map->num_acts = 1;
166 map->size_acts = nNewActions + 1;
167 }
168 else if ((map->size_acts - map->num_acts) < nNewActions) {
169 unsigned need;
170 XkbAction *prev_acts = map->acts;
171
172 need = map->num_acts + nNewActions;
173 map->acts = reallocarray(map->acts, need, sizeof(XkbAction));
174 if (map->acts == NULL) {
175 free(prev_acts);
176 map->num_acts = map->size_acts = 0;
177 return BadAlloc;
178 }
179 map->size_acts = need;
180 memset(&map->acts[map->num_acts], 0,
181 ((map->size_acts - map->num_acts) * sizeof(XkbAction)));
182 }
183 if (map->key_acts == NULL) {
184 map->key_acts = calloc(MAP_LENGTH, sizeof(unsigned short));
185 if (map->key_acts == NULL)
186 return BadAlloc;
187 }
188 }
189 if (which & XkbKeyBehaviorsMask) {
190 if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
191 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
192 (xkb->max_key_code < xkb->min_key_code))
193 return BadMatch;
194 if (map->behaviors == NULL) {
195 map->behaviors = calloc(MAP_LENGTH, sizeof(XkbBehavior));
196 if (map->behaviors == NULL)
197 return BadAlloc;
198 }
199 }
200 if (which & XkbVirtualModMapMask) {
201 if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
202 (!XkbIsLegalKeycode(xkb->max_key_code)) ||
203 (xkb->max_key_code < xkb->min_key_code))
204 return BadMatch;
205 if (map->vmodmap == NULL) {
206 map->vmodmap = calloc(MAP_LENGTH, sizeof(unsigned short));
207 if (map->vmodmap == NULL)
208 return BadAlloc;
209 }
210 }
211 return Success;
212 }
213
214 /***====================================================================***/
215
216 static Status
217 XkbCopyKeyType(XkbKeyTypePtr from, XkbKeyTypePtr into)
218 {
219 if ((!from) || (!into))
220 return BadMatch;
221 free(into->map);
222 into->map = NULL;
223 free(into->preserve);
224 into->preserve = NULL;
225 free(into->level_names);
226 into->level_names = NULL;
227 *into = *from;
228 if ((from->map) && (into->map_count > 0)) {
229 into->map = calloc(into->map_count, sizeof(XkbKTMapEntryRec));
230 if (!into->map)
231 return BadAlloc;
232 memcpy(into->map, from->map,
233 into->map_count * sizeof(XkbKTMapEntryRec));
234 }
235 if ((from->preserve) && (into->map_count > 0)) {
236 into->preserve = calloc(into->map_count, sizeof(XkbModsRec));
237 if (!into->preserve)
238 return BadAlloc;
239 memcpy(into->preserve, from->preserve,
240 into->map_count * sizeof(XkbModsRec));
241 }
242 if ((from->level_names) && (into->num_levels > 0)) {
243 into->level_names = calloc(into->num_levels, sizeof(Atom));
244 if (!into->level_names)
245 return BadAlloc;
246 memcpy(into->level_names, from->level_names,
247 into->num_levels * sizeof(Atom));
248 }
249 return Success;
250 }
251
252 Status
253 XkbCopyKeyTypes(XkbKeyTypePtr from, XkbKeyTypePtr into, int num_types)
254 {
255 register int i, rtrn;
256
257 if ((!from) || (!into) || (num_types < 0))
258 return BadMatch;
259 for (i = 0; i < num_types; i++) {
260 if ((rtrn = XkbCopyKeyType(from++, into++)) != Success)
261 return rtrn;
262 }
263 return Success;
264 }
265
266 Status
267 XkbResizeKeyType(XkbDescPtr xkb,
268 int type_ndx,
269 int map_count, Bool want_preserve, int new_num_lvls)
270 {
271 XkbKeyTypePtr type;
272 KeyCode matchingKeys[XkbMaxKeyCount], nMatchingKeys;
273
274 if ((type_ndx < 0) || (type_ndx >= xkb->map->num_types) || (map_count < 0)
275 || (new_num_lvls < 1))
276 return BadValue;
277 switch (type_ndx) {
278 case XkbOneLevelIndex:
279 if (new_num_lvls != 1)
280 return BadMatch;
281 break;
282 case XkbTwoLevelIndex:
283 case XkbAlphabeticIndex:
284 case XkbKeypadIndex:
285 if (new_num_lvls != 2)
286 return BadMatch;
287 break;
288 }
289 type = &xkb->map->types[type_ndx];
290 if (map_count == 0) {
291 free(type->map);
292 type->map = NULL;
293 free(type->preserve);
294 type->preserve = NULL;
295 type->map_count = 0;
296 }
297 else {
298 XkbKTMapEntryRec *prev_map = type->map;
299
300 if ((map_count > type->map_count) || (type->map == NULL))
301 type->map =
302 reallocarray(type->map, map_count, sizeof(XkbKTMapEntryRec));
303 if (!type->map) {
304 free(prev_map);
305 return BadAlloc;
306 }
307 if (want_preserve) {
308 XkbModsRec *prev_preserve = type->preserve;
309
310 if ((map_count > type->map_count) || (type->preserve == NULL)) {
311 type->preserve = reallocarray(type->preserve,
312 map_count, sizeof(XkbModsRec));
313 }
314 if (!type->preserve) {
315 free(prev_preserve);
316 return BadAlloc;
317 }
318 }
319 else {
320 free(type->preserve);
321 type->preserve = NULL;
322 }
323 type->map_count = map_count;
324 }
325
326 if ((new_num_lvls > type->num_levels) || (type->level_names == NULL)) {
327 Atom *prev_level_names = type->level_names;
328
329 type->level_names = reallocarray(type->level_names,
330 new_num_lvls, sizeof(Atom));
331 if (!type->level_names) {
332 free(prev_level_names);
333 return BadAlloc;
334 }
335 }
336 /*
337 * Here's the theory:
338 * If the width of the type changed, we might have to resize the symbol
339 * maps for any keys that use the type for one or more groups. This is
340 * expensive, so we'll try to cull out any keys that are obviously okay:
341 * In any case:
342 * - keys that have a group width <= the old width are okay (because
343 * they could not possibly have been associated with the old type)
344 * If the key type increased in size:
345 * - keys that already have a group width >= to the new width are okay
346 * + keys that have a group width >= the old width but < the new width
347 * might have to be enlarged.
348 * If the key type decreased in size:
349 * - keys that have a group width > the old width don't have to be
350 * resized (because they must have some other wider type associated
351 * with some group).
352 * + keys that have a group width == the old width might have to be
353 * shrunk.
354 * The possibilities marked with '+' require us to examine the key types
355 * associated with each group for the key.
356 */
357 memset(matchingKeys, 0, XkbMaxKeyCount * sizeof(KeyCode));
358 nMatchingKeys = 0;
359 if (new_num_lvls > type->num_levels) {
360 int nTotal;
361 KeySym *newSyms;
362 int width, match, nResize;
363 register int i, g, nSyms;
364
365 nResize = 0;
366 for (nTotal = 1, i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
367 width = XkbKeyGroupsWidth(xkb, i);
368 if (width < type->num_levels || width >= new_num_lvls) {
369 nTotal += XkbKeyNumSyms(xkb,i);
370 continue;
371 }
372 for (match = 0, g = XkbKeyNumGroups(xkb, i) - 1;
373 (g >= 0) && (!match); g--) {
374 if (XkbKeyKeyTypeIndex(xkb, i, g) == type_ndx) {
375 matchingKeys[nMatchingKeys++] = i;
376 match = 1;
377 }
378 }
379 if (!match)
380 nTotal += XkbKeyNumSyms(xkb, i);
381 else {
382 nTotal += XkbKeyNumGroups(xkb, i) * new_num_lvls;
383 nResize++;
384 }
385 }
386 if (nResize > 0) {
387 int nextMatch;
388
389 xkb->map->size_syms = (nTotal * 15) / 10;
390 newSyms = calloc(xkb->map->size_syms, sizeof(KeySym));
391 if (newSyms == NULL)
392 return BadAlloc;
393 nextMatch = 0;
394 nSyms = 1;
395 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
396 if (matchingKeys[nextMatch] == i) {
397 KeySym *pOld;
398
399 nextMatch++;
400 width = XkbKeyGroupsWidth(xkb, i);
401 pOld = XkbKeySymsPtr(xkb, i);
402 for (g = XkbKeyNumGroups(xkb, i) - 1; g >= 0; g--) {
403 memcpy(&newSyms[nSyms + (new_num_lvls * g)],
404 &pOld[width * g], width * sizeof(KeySym));
405 }
406 xkb->map->key_sym_map[i].offset = nSyms;
407 nSyms += XkbKeyNumGroups(xkb, i) * new_num_lvls;
408 }
409 else {
410 memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
411 XkbKeyNumSyms(xkb, i) * sizeof(KeySym));
412 xkb->map->key_sym_map[i].offset = nSyms;
413 nSyms += XkbKeyNumSyms(xkb, i);
414 }
415 }
416 type->num_levels = new_num_lvls;
417 free(xkb->map->syms);
418 xkb->map->syms = newSyms;
419 xkb->map->num_syms = nSyms;
420 return Success;
421 }
422 }
423 else if (new_num_lvls < type->num_levels) {
424 int width, match;
425 register int g, i;
426
427 for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
428 width = XkbKeyGroupsWidth(xkb, i);
429 if (width < type->num_levels)
430 continue;
431 for (match = 0, g = XkbKeyNumGroups(xkb, i) - 1;
432 (g >= 0) && (!match); g--) {
433 if (XkbKeyKeyTypeIndex(xkb, i, g) == type_ndx) {
434 matchingKeys[nMatchingKeys++] = i;
435 match = 1;
436 }
437 }
438 }
439 }
440 if (nMatchingKeys > 0) {
441 int key, firstClear;
442 register int i, g;
443
444 if (new_num_lvls > type->num_levels)
445 firstClear = type->num_levels;
446 else
447 firstClear = new_num_lvls;
448 for (i = 0; i < nMatchingKeys; i++) {
449 KeySym *pSyms;
450 int width, nClear;
451
452 key = matchingKeys[i];
453 width = XkbKeyGroupsWidth(xkb, key);
454 nClear = width - firstClear;
455 pSyms = XkbKeySymsPtr(xkb, key);
456 for (g = XkbKeyNumGroups(xkb, key) - 1; g >= 0; g--) {
457 if (XkbKeyKeyTypeIndex(xkb, key, g) == type_ndx) {
458 if (nClear > 0)
459 memset(&pSyms[g * width + firstClear], 0,
460 nClear * sizeof(KeySym));
461 }
462 }
463 }
464 }
465 type->num_levels = new_num_lvls;
466 return Success;
467 }
468
469 KeySym *
470 XkbResizeKeySyms(XkbDescPtr xkb, int key, int needed)
471 {
472 register int i, nSyms, nKeySyms;
473 unsigned nOldSyms;
474 KeySym *newSyms;
475
476 if (needed == 0) {
477 xkb->map->key_sym_map[key].offset = 0;
478 return xkb->map->syms;
479 }
480 nOldSyms = XkbKeyNumSyms(xkb, key);
481 if (nOldSyms >= (unsigned) needed) {
482 return XkbKeySymsPtr(xkb, key);
483 }
484 if (xkb->map->size_syms - xkb->map->num_syms >= (unsigned) needed) {
485 if (nOldSyms > 0) {
486 memcpy(&xkb->map->syms[xkb->map->num_syms], XkbKeySymsPtr(xkb, key),
487 nOldSyms * sizeof(KeySym));
488 }
489 if ((needed - nOldSyms) > 0) {
490 memset(&xkb->map->
491 syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)], 0,
492 (needed - nOldSyms) * sizeof(KeySym));
493 }
494 xkb->map->key_sym_map[key].offset = xkb->map->num_syms;
495 xkb->map->num_syms += needed;
496 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
497 }
498 xkb->map->size_syms += (needed > 32 ? needed : 32);
499 newSyms = calloc(xkb->map->size_syms, sizeof(KeySym));
500 if (newSyms == NULL)
501 return NULL;
502 newSyms[0] = NoSymbol;
503 nSyms = 1;
504 for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
505 int nCopy;
506
507 nCopy = nKeySyms = XkbKeyNumSyms(xkb, i);
508 if ((nKeySyms == 0) && (i != key))
509 continue;
510 if (i == key)
511 nKeySyms = needed;
512 if (nCopy != 0)
513 memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
514 nCopy * sizeof(KeySym));
515 if (nKeySyms > nCopy)
516 memset(&newSyms[nSyms + nCopy], 0,
517 (nKeySyms - nCopy) * sizeof(KeySym));
518 xkb->map->key_sym_map[i].offset = nSyms;
519 nSyms += nKeySyms;
520 }
521 free(xkb->map->syms);
522 xkb->map->syms = newSyms;
523 xkb->map->num_syms = nSyms;
524 return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
525 }
526
527 static unsigned
528 _ExtendRange(unsigned int old_flags,
529 unsigned int flag,
530 KeyCode newKC, KeyCode *old_min, unsigned char *old_num)
531 {
532 if ((old_flags & flag) == 0) {
533 old_flags |= flag;
534 *old_min = newKC;
535 *old_num = 1;
536 }
537 else {
538 int last = (*old_min) + (*old_num) - 1;
539
540 if (newKC < *old_min) {
541 *old_min = newKC;
542 *old_num = (last - newKC) + 1;
543 }
544 else if (newKC > last) {
545 *old_num = (newKC - (*old_min)) + 1;
546 }
547 }
548 return old_flags;
549 }
550
551 Status
552 XkbChangeKeycodeRange(XkbDescPtr xkb,
553 int minKC, int maxKC, XkbChangesPtr changes)
554 {
555 int tmp;
556
557 if ((!xkb) || (minKC < XkbMinLegalKeyCode) || (maxKC > XkbMaxLegalKeyCode))
558 return BadValue;
559 if (minKC > maxKC)
560 return BadMatch;
561 if (minKC < xkb->min_key_code) {
562 if (changes)
563 changes->map.min_key_code = minKC;
564 tmp = xkb->min_key_code - minKC;
565 if (xkb->map) {
566 if (xkb->map->key_sym_map) {
567 memset((char *) &xkb->map->key_sym_map[minKC], 0,
568 tmp * sizeof(XkbSymMapRec));
569 if (changes) {
570 changes->map.changed = _ExtendRange(changes->map.changed,
571 XkbKeySymsMask, minKC,
572 &changes->map.
573 first_key_sym,
574 &changes->map.
575 num_key_syms);
576 }
577 }
578 if (xkb->map->modmap) {
579 memset((char *) &xkb->map->modmap[minKC], 0, tmp);
580 if (changes) {
581 changes->map.changed = _ExtendRange(changes->map.changed,
582 XkbModifierMapMask,
583 minKC,
584 &changes->map.
585 first_modmap_key,
586 &changes->map.
587 num_modmap_keys);
588 }
589 }
590 }
591 if (xkb->server) {
592 if (xkb->server->behaviors) {
593 memset((char *) &xkb->server->behaviors[minKC], 0,
594 tmp * sizeof(XkbBehavior));
595 if (changes) {
596 changes->map.changed = _ExtendRange(changes->map.changed,
597 XkbKeyBehaviorsMask,
598 minKC,
599 &changes->map.
600 first_key_behavior,
601 &changes->map.
602 num_key_behaviors);
603 }
604 }
605 if (xkb->server->key_acts) {
606 memset((char *) &xkb->server->key_acts[minKC], 0,
607 tmp * sizeof(unsigned short));
608 if (changes) {
609 changes->map.changed = _ExtendRange(changes->map.changed,
610 XkbKeyActionsMask,
611 minKC,
612 &changes->map.
613 first_key_act,
614 &changes->map.
615 num_key_acts);
616 }
617 }
618 if (xkb->server->vmodmap) {
619 memset((char *) &xkb->server->vmodmap[minKC], 0,
620 tmp * sizeof(unsigned short));
621 if (changes) {
622 changes->map.changed = _ExtendRange(changes->map.changed,
623 XkbVirtualModMapMask,
624 minKC,
625 &changes->map.
626 first_modmap_key,
627 &changes->map.
628 num_vmodmap_keys);
629 }
630 }
631 }
632 if ((xkb->names) && (xkb->names->keys)) {
633 memset((char *) &xkb->names->keys[minKC], 0,
634 tmp * sizeof(XkbKeyNameRec));
635 if (changes) {
636 changes->names.changed = _ExtendRange(changes->names.changed,
637 XkbKeyNamesMask, minKC,
638 &changes->names.first_key,
639 &changes->names.num_keys);
640 }
641 }
642 xkb->min_key_code = minKC;
643 }
644 if (maxKC > xkb->max_key_code) {
645 if (changes)
646 changes->map.max_key_code = maxKC;
647 tmp = MAP_LENGTH - xkb->max_key_code;
648 if (xkb->map) {
649 if (xkb->map->key_sym_map) {
650 memset((char *) &xkb->map->key_sym_map[xkb->max_key_code], 0,
651 tmp * sizeof(XkbSymMapRec));
652 if (changes) {
653 changes->map.changed = _ExtendRange(changes->map.changed,
654 XkbKeySymsMask, maxKC,
655 &changes->map.
656 first_key_sym,
657 &changes->map.
658 num_key_syms);
659 }
660 }
661 if (xkb->map->modmap) {
662 memset((char *) &xkb->map->modmap[xkb->max_key_code], 0, tmp);
663 if (changes) {
664 changes->map.changed = _ExtendRange(changes->map.changed,
665 XkbModifierMapMask,
666 maxKC,
667 &changes->map.
668 first_modmap_key,
669 &changes->map.
670 num_modmap_keys);
671 }
672 }
673 }
674 if (xkb->server) {
675 if (xkb->server->behaviors) {
676 memset((char *) &xkb->server->behaviors[xkb->max_key_code], 0,
677 tmp * sizeof(XkbBehavior));
678 if (changes) {
679 changes->map.changed = _ExtendRange(changes->map.changed,
680 XkbKeyBehaviorsMask,
681 maxKC,
682 &changes->map.
683 first_key_behavior,
684 &changes->map.
685 num_key_behaviors);
686 }
687 }
688 if (xkb->server->key_acts) {
689 memset((char *) &xkb->server->key_acts[xkb->max_key_code], 0,
690 tmp * sizeof(unsigned short));
691 if (changes) {
692 changes->map.changed = _ExtendRange(changes->map.changed,
693 XkbKeyActionsMask,
694 maxKC,
695 &changes->map.
696 first_key_act,
697 &changes->map.
698 num_key_acts);
699 }
700 }
701 if (xkb->server->vmodmap) {
702 memset((char *) &xkb->server->vmodmap[xkb->max_key_code], 0,
703 tmp * sizeof(unsigned short));
704 if (changes) {
705 changes->map.changed = _ExtendRange(changes->map.changed,
706 XkbVirtualModMapMask,
707 maxKC,
708 &changes->map.
709 first_modmap_key,
710 &changes->map.
711 num_vmodmap_keys);
712 }
713 }
714 }
715 if ((xkb->names) && (xkb->names->keys)) {
716 memset((char *) &xkb->names->keys[xkb->max_key_code], 0,
717 tmp * sizeof(XkbKeyNameRec));
718 if (changes) {
719 changes->names.changed = _ExtendRange(changes->names.changed,
720 XkbKeyNamesMask, maxKC,
721 &changes->names.first_key,
722 &changes->names.num_keys);
723 }
724 }
725 xkb->max_key_code = maxKC;
726 }
727 return Success;
728 }
729
730 XkbAction *
731 XkbResizeKeyActions(XkbDescPtr xkb, int key, int needed)
732 {
733 register int i, nActs;
734 XkbAction *newActs;
735
736 if (needed <= 0) {
737 xkb->server->key_acts[key] = 0;
738 return NULL;
739 }
740 if (XkbKeyHasActions(xkb, key) &&
741 (XkbKeyNumSyms(xkb, key) >= (unsigned) needed))
742 return XkbKeyActionsPtr(xkb, key);
743 if (xkb->server->size_acts - xkb->server->num_acts >= (unsigned) needed) {
744 xkb->server->key_acts[key] = xkb->server->num_acts;
745 xkb->server->num_acts += needed;
746 return &xkb->server->acts[xkb->server->key_acts[key]];
747 }
748 xkb->server->size_acts = xkb->server->num_acts + needed + 8;
749 newActs = calloc(xkb->server->size_acts, sizeof(XkbAction));
750 if (newActs == NULL)
751 return NULL;
752 newActs[0].type = XkbSA_NoAction;
753 nActs = 1;
754 for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
755 int nKeyActs, nCopy;
756
757 if ((xkb->server->key_acts[i] == 0) && (i != key))
758 continue;
759
760 nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
761 if (i == key) {
762 nKeyActs = needed;
763 if (needed < nCopy)
764 nCopy = needed;
765 }
766
767 if (nCopy > 0)
768 memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
769 nCopy * sizeof(XkbAction));
770 if (nCopy < nKeyActs)
771 memset(&newActs[nActs + nCopy], 0,
772 (nKeyActs - nCopy) * sizeof(XkbAction));
773 xkb->server->key_acts[i] = nActs;
774 nActs += nKeyActs;
775 }
776 free(xkb->server->acts);
777 xkb->server->acts = newActs;
778 xkb->server->num_acts = nActs;
779 return &xkb->server->acts[xkb->server->key_acts[key]];
780 }
781
782 void
783 XkbFreeClientMap(XkbDescPtr xkb, unsigned what, Bool freeMap)
784 {
785 XkbClientMapPtr map;
786
787 if ((xkb == NULL) || (xkb->map == NULL))
788 return;
789 if (freeMap)
790 what = XkbAllClientInfoMask;
791 map = xkb->map;
792 if (what & XkbKeyTypesMask) {
793 if (map->types != NULL) {
794 if (map->num_types > 0) {
795 register int i;
796 XkbKeyTypePtr type;
797
798 for (i = 0, type = map->types; i < map->num_types; i++, type++) {
799 free(type->map);
800 type->map = NULL;
801 free(type->preserve);
802 type->preserve = NULL;
803 type->map_count = 0;
804 free(type->level_names);
805 type->level_names = NULL;
806 }
807 }
808 free(map->types);
809 map->num_types = map->size_types = 0;
810 map->types = NULL;
811 }
812 }
813 if (what & XkbKeySymsMask) {
814 free(map->key_sym_map);
815 map->key_sym_map = NULL;
816 if (map->syms != NULL) {
817 free(map->syms);
818 map->size_syms = map->num_syms = 0;
819 map->syms = NULL;
820 }
821 }
822 if ((what & XkbModifierMapMask) && (map->modmap != NULL)) {
823 free(map->modmap);
824 map->modmap = NULL;
825 }
826 if (freeMap) {
827 free(xkb->map);
828 xkb->map = NULL;
829 }
830 return;
831 }
832
833 void
834 XkbFreeServerMap(XkbDescPtr xkb, unsigned what, Bool freeMap)
835 {
836 XkbServerMapPtr map;
837
838 if ((xkb == NULL) || (xkb->server == NULL))
839 return;
840 if (freeMap)
841 what = XkbAllServerInfoMask;
842 map = xkb->server;
843 if ((what & XkbExplicitComponentsMask) && (map->explicit != NULL)) {
844 free(map->explicit);
845 map->explicit = NULL;
846 }
847 if (what & XkbKeyActionsMask) {
848 free(map->key_acts);
849 map->key_acts = NULL;
850 if (map->acts != NULL) {
851 free(map->acts);
852 map->num_acts = map->size_acts = 0;
853 map->acts = NULL;
854 }
855 }
856 if ((what & XkbKeyBehaviorsMask) && (map->behaviors != NULL)) {
857 free(map->behaviors);
858 map->behaviors = NULL;
859 }
860 if ((what & XkbVirtualModMapMask) && (map->vmodmap != NULL)) {
861 free(map->vmodmap);
862 map->vmodmap = NULL;
863 }
864
865 if (freeMap) {
866 free(xkb->server);
867 xkb->server = NULL;
868 }
869 return;
870 }