1 /************************************************************
2 * Copyright (c) 1994 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 ********************************************************/
27 #include "xkbcomp-priv.h"
28 #include "parseutils.h"
32 /***====================================================================***/
34 /* Needed to work with the typechecker. */
35 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
36 typedef darray(union xkb_action) darray_xkb_action;
41 KEY_REPEAT_UNDEFINED = -1
45 KEY_FIELD_SYMS = (1 << 0),
46 KEY_FIELD_ACTS = (1 << 1),
47 KEY_FIELD_REPEAT = (1 << 2),
48 KEY_FIELD_TYPE_DFLT = (1 << 3),
49 KEY_FIELD_TYPES = (1 << 4),
50 KEY_FIELD_GROUPINFO = (1 << 5),
51 KEY_FIELD_VMODMAP = (1 << 6),
54 typedef struct _KeyInfo {
55 enum key_field defined;
57 enum merge_mode merge;
59 unsigned long name; /* the 4 chars of the key name, as long */
60 unsigned char typesDefined;
61 unsigned char symsDefined;
62 unsigned char actsDefined;
63 xkb_level_index_t numLevels[XkbNumKbdGroups];
65 /* syms[group] -> Single array for all the keysyms in the group. */
66 darray_xkb_keysym_t syms[XkbNumKbdGroups];
68 * symsMapIndex[group][level] -> The index from which the syms for
69 * the level begin in the syms[group] array. Remember each keycode
70 * can have multiple keysyms in each level (that is, each key press
71 * can result in multiple keysyms).
73 darray(int) symsMapIndex[XkbNumKbdGroups];
75 * symsMapNumEntries[group][level] -> How many syms are in
76 * syms[group][symsMapIndex[group][level]].
78 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
80 darray_xkb_action acts[XkbNumKbdGroups];
82 xkb_atom_t types[XkbNumKbdGroups];
83 enum key_repeat repeat;
84 xkb_mod_mask_t vmodmap;
87 uint8_t out_of_range_group_action;
88 xkb_group_index_t out_of_range_group_number;
92 * Init the given key info to sane values.
95 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
98 static const char dflt[4] = "*";
101 keyi->file_id = file_id;
102 keyi->merge = MERGE_OVERRIDE;
103 keyi->name = KeyNameToLong(dflt);
104 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
106 for (i = 0; i < XkbNumKbdGroups; i++) {
107 keyi->numLevels[i] = 0;
108 keyi->types[i] = XKB_ATOM_NONE;
109 darray_init(keyi->syms[i]);
110 darray_init(keyi->symsMapIndex[i]);
111 darray_init(keyi->symsMapNumEntries[i]);
112 darray_init(keyi->acts[i]);
115 keyi->dfltType = XKB_ATOM_NONE;
117 keyi->repeat = KEY_REPEAT_UNDEFINED;
118 keyi->out_of_range_group_action = 0;
119 keyi->out_of_range_group_number = 0;
123 FreeKeyInfo(KeyInfo *keyi)
127 for (i = 0; i < XkbNumKbdGroups; i++) {
128 darray_free(keyi->syms[i]);
129 darray_free(keyi->symsMapIndex[i]);
130 darray_free(keyi->symsMapNumEntries[i]);
131 darray_free(keyi->acts[i]);
136 * Copy old into new, optionally reset old to 0.
137 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
138 * newly allocated and new points to the new memory areas.
141 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
148 for (i = 0; i < XkbNumKbdGroups; i++) {
149 old->numLevels[i] = 0;
150 darray_init(old->symsMapIndex[i]);
151 darray_init(old->symsMapNumEntries[i]);
152 darray_init(old->syms[i]);
153 darray_init(old->acts[i]);
157 for (i = 0; i < XkbNumKbdGroups; i++) {
158 darray_copy(new->syms[i], old->syms[i]);
159 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
160 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
161 darray_copy(new->acts[i], old->acts[i]);
168 /***====================================================================***/
170 typedef struct _ModMapEntry {
172 enum merge_mode merge;
176 unsigned long keyName;
181 typedef struct _SymbolsInfo {
182 char *name; /* e.g. pc+us+inet(evdev) */
185 enum merge_mode merge;
186 xkb_group_index_t explicit_group;
187 darray(KeyInfo) keys;
191 xkb_atom_t groupNames[XkbNumKbdGroups];
195 struct xkb_keymap *keymap;
199 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
205 info->explicit_group = 0;
206 info->errorCount = 0;
207 info->file_id = file_id;
208 info->merge = MERGE_OVERRIDE;
209 darray_init(info->keys);
210 darray_growalloc(info->keys, 110);
211 list_init(&info->modMaps);
212 for (i = 0; i < XkbNumKbdGroups; i++)
213 info->groupNames[i] = XKB_ATOM_NONE;
214 InitKeyInfo(&info->dflt, file_id);
215 InitVModInfo(&info->vmods, keymap);
217 info->keymap = keymap;
221 FreeSymbolsInfo(SymbolsInfo * info)
224 ModMapEntry *mm, *next;
227 darray_foreach(keyi, info->keys) {
230 darray_free(info->keys);
231 list_foreach_safe(mm, next, &info->modMaps, entry)
233 memset(info, 0, sizeof(SymbolsInfo));
237 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group,
238 xkb_level_index_t numLevels, unsigned sizeSyms,
243 if (darray_size(keyi->syms[group]) < sizeSyms)
244 darray_resize0(keyi->syms[group], sizeSyms);
246 if (darray_empty(keyi->symsMapIndex[group]) ||
247 keyi->numLevels[group] < numLevels) {
248 darray_resize(keyi->symsMapIndex[group], numLevels);
249 for (i = keyi->numLevels[group]; i < numLevels; i++)
250 darray_item(keyi->symsMapIndex[group], i) = -1;
253 if (darray_empty(keyi->symsMapNumEntries[group]) ||
254 keyi->numLevels[group] < numLevels)
255 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
257 if ((forceActions && (keyi->numLevels[group] < numLevels ||
258 darray_empty(keyi->acts[group]))) ||
259 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
260 darray_resize0(keyi->acts[group], numLevels);
262 if (keyi->numLevels[group] < numLevels)
263 keyi->numLevels[group] = numLevels;
268 enum key_group_selector {
275 MergeKeyGroups(SymbolsInfo * info,
276 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
278 darray_xkb_keysym_t resultSyms;
279 enum key_group_selector using = NONE;
280 darray_xkb_action resultActs;
281 xkb_level_index_t resultWidth;
282 unsigned int resultSize = 0;
285 bool report, clobber;
286 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
288 clobber = (from->merge != MERGE_AUGMENT);
290 report = (verbosity > 9) ||
291 (into->file_id == from->file_id && verbosity > 0);
293 darray_init(resultSyms);
295 if (into->numLevels[group] >= from->numLevels[group]) {
296 resultActs = into->acts[group];
297 resultWidth = into->numLevels[group];
300 resultActs = from->acts[group];
301 resultWidth = from->numLevels[group];
302 darray_resize(into->symsMapIndex[group],
303 from->numLevels[group]);
304 darray_resize0(into->symsMapNumEntries[group],
305 from->numLevels[group]);
307 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
308 darray_item(into->symsMapIndex[group], i) = -1;
311 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
312 !darray_empty(from->acts[group]))) {
313 darray_resize0(resultActs, resultWidth);
314 for (i = 0; i < resultWidth; i++) {
315 union xkb_action *fromAct = NULL, *toAct = NULL;
317 if (!darray_empty(from->acts[group]))
318 fromAct = &darray_item(from->acts[group], i);
320 if (!darray_empty(into->acts[group]))
321 toAct = &darray_item(into->acts[group], i);
323 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
324 && (toAct != NULL)) {
325 darray_item(resultActs, i) = *toAct;
327 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
328 && (fromAct != NULL)) {
329 darray_item(resultActs, i) = *fromAct;
332 union xkb_action *use, *ignore;
342 log_warn(info->keymap->ctx,
343 "Multiple actions for level %d/group %u on key %s; "
344 "Using %s, ignoring %s\n",
345 i + 1, group + 1, LongKeyNameText(into->name),
346 ActionTypeText(use->type),
347 ActionTypeText(ignore->type));
349 darray_item(resultActs, i) = *use;
354 for (i = 0; i < resultWidth; i++) {
355 unsigned int fromSize = 0;
358 if (!darray_empty(from->symsMapNumEntries[group]) &&
359 i < from->numLevels[group])
360 fromSize = darray_item(from->symsMapNumEntries[group], i);
362 if (!darray_empty(into->symsMapNumEntries[group]) &&
363 i < into->numLevels[group])
364 toSize = darray_item(into->symsMapNumEntries[group], i);
367 resultSize += toSize;
370 else if (toSize == 0 || clobber) {
371 resultSize += fromSize;
375 resultSize += toSize;
384 resultSyms = from->syms[group];
385 darray_free(into->symsMapNumEntries[group]);
386 darray_free(into->symsMapIndex[group]);
387 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
388 into->symsMapIndex[group] = from->symsMapIndex[group];
389 darray_init(from->symsMapNumEntries[group]);
390 darray_init(from->symsMapIndex[group]);
393 else if (using == TO) {
394 resultSyms = into->syms[group];
398 darray_resize0(resultSyms, resultSize);
400 for (i = 0; i < resultWidth; i++) {
401 enum key_group_selector use = NONE;
402 unsigned int fromSize = 0;
403 unsigned int toSize = 0;
405 if (i < from->numLevels[group])
406 fromSize = darray_item(from->symsMapNumEntries[group], i);
408 if (i < into->numLevels[group])
409 toSize = darray_item(into->symsMapNumEntries[group], i);
411 if (fromSize == 0 && toSize == 0) {
412 darray_item(into->symsMapIndex[group], i) = -1;
413 darray_item(into->symsMapNumEntries[group], i) = 0;
419 else if (toSize == 0 || clobber)
424 if (toSize && fromSize && report) {
425 log_info(info->keymap->ctx,
426 "Multiple symbols for group %u, level %d on key %s; "
427 "Using %s, ignoring %s\n",
428 group + 1, i + 1, LongKeyNameText(into->name),
429 (use == FROM ? "from" : "to"),
430 (use == FROM ? "to" : "from"));
434 memcpy(darray_mem(resultSyms, cur_idx),
435 darray_mem(from->syms[group],
436 darray_item(from->symsMapIndex[group], i)),
437 darray_item(from->symsMapNumEntries[group],
438 i) * sizeof(xkb_keysym_t));
439 darray_item(into->symsMapIndex[group], i) = cur_idx;
440 darray_item(into->symsMapNumEntries[group], i) =
441 darray_item(from->symsMapNumEntries[group], i);
444 memcpy(darray_mem(resultSyms, cur_idx),
445 darray_mem(into->syms[group],
446 darray_item(into->symsMapIndex[group], i)),
447 darray_item(into->symsMapNumEntries[group],
448 i) * sizeof(xkb_keysym_t));
449 darray_item(into->symsMapIndex[group], i) = cur_idx;
451 cur_idx += darray_item(into->symsMapNumEntries[group], i);
455 if (!darray_same(resultActs, into->acts[group]))
456 darray_free(into->acts[group]);
457 if (!darray_same(resultActs, from->acts[group]))
458 darray_free(from->acts[group]);
459 into->numLevels[group] = resultWidth;
460 if (!darray_same(resultSyms, into->syms[group]))
461 darray_free(into->syms[group]);
462 into->syms[group] = resultSyms;
463 if (!darray_same(resultSyms, from->syms[group]))
464 darray_free(from->syms[group]);
465 darray_init(from->syms[group]);
466 darray_free(from->symsMapIndex[group]);
467 darray_free(from->symsMapNumEntries[group]);
468 into->acts[group] = resultActs;
469 darray_init(from->acts[group]);
470 if (!darray_empty(into->syms[group]))
471 into->symsDefined |= (1 << group);
472 from->symsDefined &= ~(1 << group);
473 into->actsDefined |= (1 << group);
474 from->actsDefined &= ~(1 << group);
480 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
481 int verbosity, enum key_field *collide)
483 if (!(old->defined & field))
486 if (new->defined & field) {
487 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
490 if (new->merge != MERGE_AUGMENT)
499 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
502 enum key_field collide = 0;
504 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
506 if (from->merge == MERGE_REPLACE) {
507 for (i = 0; i < XkbNumKbdGroups; i++) {
508 if (into->numLevels[i] != 0) {
509 darray_free(into->syms[i]);
510 darray_free(into->acts[i]);
514 memset(from, 0, sizeof(KeyInfo));
518 report = (verbosity > 9 ||
519 (into->file_id == from->file_id && verbosity > 0));
521 for (i = 0; i < XkbNumKbdGroups; i++) {
522 if (from->numLevels[i] > 0) {
523 if (into->numLevels[i] == 0) {
524 into->numLevels[i] = from->numLevels[i];
525 into->syms[i] = from->syms[i];
526 into->symsMapIndex[i] = from->symsMapIndex[i];
527 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
528 into->acts[i] = from->acts[i];
529 into->symsDefined |= (1 << i);
530 darray_init(from->syms[i]);
531 darray_init(from->symsMapIndex[i]);
532 darray_init(from->symsMapNumEntries[i]);
533 darray_init(from->acts[i]);
534 from->numLevels[i] = 0;
535 from->symsDefined &= ~(1 << i);
536 if (!darray_empty(into->syms[i]))
537 into->defined |= KEY_FIELD_SYMS;
538 if (!darray_empty(into->acts[i]))
539 into->defined |= KEY_FIELD_ACTS;
543 if (!darray_empty(into->syms[i]))
544 collide |= KEY_FIELD_SYMS;
545 if (!darray_empty(into->acts[i]))
546 collide |= KEY_FIELD_ACTS;
548 MergeKeyGroups(info, into, from, (unsigned) i);
551 if (from->types[i] != XKB_ATOM_NONE) {
552 if ((into->types[i] != XKB_ATOM_NONE) && report &&
553 (into->types[i] != from->types[i])) {
554 xkb_atom_t use, ignore;
555 collide |= KEY_FIELD_TYPES;
556 if (from->merge != MERGE_AUGMENT) {
557 use = from->types[i];
558 ignore = into->types[i];
561 use = into->types[i];
562 ignore = from->types[i];
565 log_warn(info->keymap->ctx,
566 "Multiple definitions for group %d type of key %s; "
567 "Using %s, ignoring %s\n",
568 i, LongKeyNameText(into->name),
569 xkb_atom_text(info->keymap->ctx, use),
570 xkb_atom_text(info->keymap->ctx, ignore));
573 if (from->merge != MERGE_AUGMENT ||
574 into->types[i] == XKB_ATOM_NONE) {
575 into->types[i] = from->types[i];
580 if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
581 into->vmodmap = from->vmodmap;
582 into->defined |= KEY_FIELD_VMODMAP;
584 if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
585 into->repeat = from->repeat;
586 into->defined |= KEY_FIELD_REPEAT;
588 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
589 into->dfltType = from->dfltType;
590 into->defined |= KEY_FIELD_TYPE_DFLT;
592 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
593 into->out_of_range_group_action = from->out_of_range_group_action;
594 into->out_of_range_group_number = from->out_of_range_group_number;
595 into->defined |= KEY_FIELD_GROUPINFO;
599 log_warn(info->keymap->ctx,
600 "Symbol map for key %s redefined; "
601 "Using %s definition for conflicting fields\n",
602 LongKeyNameText(into->name),
603 (from->merge == MERGE_AUGMENT ? "first" : "last"));
609 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
611 unsigned long real_name;
614 darray_foreach(iter, info->keys)
615 if (iter->name == keyi->name)
616 return MergeKeys(info, iter, keyi);
618 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
619 darray_foreach(iter, info->keys)
620 if (iter->name == real_name)
621 return MergeKeys(info, iter, keyi);
623 darray_resize0(info->keys, darray_size(info->keys) + 1);
624 new = &darray_item(info->keys, darray_size(info->keys) - 1);
625 return CopyKeyInfo(keyi, new, true);
629 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
634 clobber = (new->merge != MERGE_AUGMENT);
635 list_foreach(mm, &info->modMaps, entry) {
636 if (new->haveSymbol && mm->haveSymbol
637 && (new->u.keySym == mm->u.keySym)) {
638 unsigned use, ignore;
639 if (mm->modifier != new->modifier) {
642 ignore = mm->modifier;
646 ignore = new->modifier;
648 log_err(info->keymap->ctx,
649 "%s added to symbol map for multiple modifiers; "
650 "Using %s, ignoring %s.\n",
651 KeysymText(new->u.keySym), ModIndexText(use),
652 ModIndexText(ignore));
657 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
658 (new->u.keyName == mm->u.keyName)) {
659 unsigned use, ignore;
660 if (mm->modifier != new->modifier) {
663 ignore = mm->modifier;
667 ignore = new->modifier;
669 log_err(info->keymap->ctx,
670 "Key %s added to map for multiple modifiers; "
671 "Using %s, ignoring %s.\n",
672 LongKeyNameText(new->u.keyName), ModIndexText(use),
673 ModIndexText(ignore));
680 mm = malloc(sizeof(*mm));
682 log_wsgo(info->keymap->ctx,
683 "Could not allocate modifier map entry; "
684 "Modifier map for %s will be incomplete\n",
685 ModIndexText(new->modifier));
690 list_add(&mm->entry, &info->modMaps);
694 /***====================================================================***/
697 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
698 enum merge_mode merge)
702 ModMapEntry *mm, *next;
704 if (from->errorCount > 0) {
705 into->errorCount += from->errorCount;
708 if (into->name == NULL) {
709 into->name = from->name;
712 for (i = 0; i < XkbNumKbdGroups; i++) {
713 if (from->groupNames[i] != XKB_ATOM_NONE) {
714 if ((merge != MERGE_AUGMENT) ||
715 (into->groupNames[i] == XKB_ATOM_NONE))
716 into->groupNames[i] = from->groupNames[i];
720 darray_foreach(keyi, from->keys) {
721 if (merge != MERGE_DEFAULT)
724 if (!AddKeySymbols(into, keyi))
728 list_foreach_safe(mm, next, &from->modMaps, entry) {
729 if (merge != MERGE_DEFAULT)
731 if (!AddModMapEntry(into, mm))
735 list_init(&from->modMaps);
739 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
742 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
744 enum merge_mode merge = MERGE_DEFAULT;
746 SymbolsInfo included, next_incl;
748 InitSymbolsInfo(&included, info->keymap, info->file_id);
751 included.name = stmt->stmt;
755 for (; stmt; stmt = stmt->next_incl) {
756 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
758 info->errorCount += 10;
759 FreeSymbolsInfo(&included);
763 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id);
764 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
766 next_incl.explicit_group = atoi(stmt->modifier) - 1;
768 next_incl.explicit_group = info->explicit_group;
770 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
772 MergeIncludedSymbols(&included, &next_incl, merge);
774 FreeSymbolsInfo(&next_incl);
778 MergeIncludedSymbols(info, &included, merge);
779 FreeSymbolsInfo(&included);
781 return (info->errorCount == 0);
788 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
789 unsigned what, xkb_group_index_t *ndx_rtrn)
798 if (arrayNdx == NULL) {
802 defined = keyi->symsDefined;
804 defined = keyi->actsDefined;
806 for (i = 0; i < XkbNumKbdGroups; i++) {
807 if ((defined & (1 << i)) == 0) {
813 log_err(info->keymap->ctx,
814 "Too many groups of %s for key %s (max %u); "
815 "Ignoring %s defined for extra groups\n",
816 name, LongKeyNameText(keyi->name), XkbNumKbdGroups + 1, name);
820 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
821 log_err(info->keymap->ctx,
822 "Illegal group index for %s of key %s\n"
823 "Definition with non-integer array index ignored\n",
824 name, LongKeyNameText(keyi->name));
833 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
836 xkb_group_index_t ndx;
838 xkb_level_index_t nLevels;
842 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
845 keyi->symsDefined |= (1 << ndx);
848 if (value->op != EXPR_KEYSYM_LIST) {
849 log_err(info->keymap->ctx,
850 "Expected a list of symbols, found %s; "
851 "Ignoring symbols for group %u of %s\n",
852 exprOpText(value->op), ndx + 1, LongKeyNameText(keyi->name));
855 if (!darray_empty(keyi->syms[ndx])) {
856 log_err(info->keymap->ctx,
857 "Symbols for key %s, group %u already defined; "
858 "Ignoring duplicate definition\n",
859 LongKeyNameText(keyi->name), ndx + 1);
862 nSyms = darray_size(value->value.list.syms);
863 nLevels = darray_size(value->value.list.symsMapIndex);
864 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
865 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
866 log_wsgo(info->keymap->ctx,
867 "Could not resize group %u of key %s to contain %zu levels; "
869 ndx + 1, LongKeyNameText(keyi->name), nSyms);
872 keyi->symsDefined |= (1 << ndx);
873 for (i = 0; i < nLevels; i++) {
874 darray_item(keyi->symsMapIndex[ndx], i) =
875 darray_item(value->value.list.symsMapIndex, i);
876 darray_item(keyi->symsMapNumEntries[ndx], i) =
877 darray_item(value->value.list.symsNumEntries, i);
879 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
880 /* FIXME: What's abort() doing here? */
881 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
883 if (!LookupKeysym(darray_item(value->value.list.syms,
884 darray_item(value->value.list.symsMapIndex,
886 &darray_item(keyi->syms[ndx],
887 darray_item(keyi->symsMapIndex[ndx],
889 log_warn(info->keymap->ctx,
890 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
891 darray_item(value->value.list.syms, i),
892 LongKeyNameText(keyi->name),
894 xkb_atom_text(info->keymap->ctx,
895 info->groupNames[ndx]),
898 darray_item(keyi->syms[ndx],
899 darray_item(keyi->symsMapIndex[ndx],
900 i) + j) = XKB_KEY_NoSymbol;
901 darray_item(keyi->symsMapIndex[ndx], i) = -1;
902 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
905 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
906 darray_item(keyi->syms[ndx],
907 darray_item(keyi->symsMapIndex[ndx],
908 i) + j) == XKB_KEY_NoSymbol) {
909 darray_item(keyi->symsMapIndex[ndx], i) = -1;
910 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
914 for (j = keyi->numLevels[ndx] - 1;
915 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
916 keyi->numLevels[ndx]--;
921 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
925 xkb_group_index_t ndx;
928 struct xkb_any_action *toAct;
930 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
934 keyi->actsDefined |= (1 << ndx);
938 if (value->op != EXPR_ACTION_LIST) {
939 log_wsgo(info->keymap->ctx,
940 "Bad expression type (%d) for action list value; "
941 "Ignoring actions for group %u of %s\n",
942 value->op, ndx, LongKeyNameText(keyi->name));
946 if (!darray_empty(keyi->acts[ndx])) {
947 log_wsgo(info->keymap->ctx,
948 "Actions for key %s, group %u already defined\n",
949 LongKeyNameText(keyi->name), ndx);
953 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
954 act = (ExprDef *) act->common.next;
958 log_wsgo(info->keymap->ctx,
959 "Action list but not actions in AddActionsToKey\n");
963 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
964 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
965 log_wsgo(info->keymap->ctx,
966 "Could not resize group %u of key %s; "
968 ndx, LongKeyNameText(keyi->name));
971 keyi->actsDefined |= (1 << ndx);
973 toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
974 act = value->value.child;
975 for (i = 0; i < nActs; i++, toAct++) {
976 if (!HandleActionDef(act, info->keymap, toAct, info->action)) {
977 log_err(info->keymap->ctx,
978 "Illegal action definition for %s; "
979 "Action for group %u/level %zu ignored\n",
980 LongKeyNameText(keyi->name), ndx + 1, i + 1);
982 act = (ExprDef *) act->common.next;
988 static const LookupEntry repeatEntries[] = {
989 { "true", KEY_REPEAT_YES },
990 { "yes", KEY_REPEAT_YES },
991 { "on", KEY_REPEAT_YES },
992 { "false", KEY_REPEAT_NO },
993 { "no", KEY_REPEAT_NO },
994 { "off", KEY_REPEAT_NO },
995 { "default", KEY_REPEAT_UNDEFINED },
1000 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1001 ExprDef *arrayNdx, ExprDef *value)
1004 struct xkb_context *ctx = info->keymap->ctx;
1006 if (istreq(field, "type")) {
1007 xkb_group_index_t ndx;
1010 if (!ExprResolveString(ctx, value, &str))
1011 log_lvl(info->keymap->ctx, 1,
1012 "The type field of a key symbol map must be a string; "
1013 "Ignoring illegal type definition\n");
1015 if (arrayNdx == NULL) {
1016 keyi->dfltType = xkb_atom_intern(ctx, str);
1017 keyi->defined |= KEY_FIELD_TYPE_DFLT;
1019 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1020 log_err(info->keymap->ctx,
1021 "Illegal group index for type of key %s; "
1022 "Definition with non-integer array index ignored\n",
1023 LongKeyNameText(keyi->name));
1028 keyi->types[ndx] = xkb_atom_intern(ctx, str);
1029 keyi->typesDefined |= (1 << ndx);
1032 else if (istreq(field, "symbols"))
1033 return AddSymbolsToKey(info, keyi, arrayNdx, value);
1034 else if (istreq(field, "actions"))
1035 return AddActionsToKey(info, keyi, arrayNdx, value);
1036 else if (istreq(field, "vmods") ||
1037 istreq(field, "virtualmods") ||
1038 istreq(field, "virtualmodifiers")) {
1039 xkb_mod_mask_t mask;
1041 ok = ExprResolveVModMask(info->keymap, value, &mask);
1043 keyi->vmodmap = (mask >> XkbNumModifiers) & 0xffff;
1044 keyi->defined |= KEY_FIELD_VMODMAP;
1047 log_err(info->keymap->ctx,
1048 "Expected a virtual modifier mask, found %s; "
1049 "Ignoring virtual modifiers definition for key %s\n",
1050 exprOpText(value->op), LongKeyNameText(keyi->name));
1053 else if (istreq(field, "locking") ||
1054 istreq(field, "lock") ||
1055 istreq(field, "locks")) {
1056 log_err(info->keymap->ctx,
1057 "Key behaviors not supported; "
1058 "Ignoring locking specification for key %s\n",
1059 LongKeyNameText(keyi->name));
1061 else if (istreq(field, "radiogroup") ||
1062 istreq(field, "permanentradiogroup") ||
1063 istreq(field, "allownone")) {
1064 log_err(info->keymap->ctx,
1065 "Radio groups not supported; "
1066 "Ignoring radio group specification for key %s\n",
1067 LongKeyNameText(keyi->name));
1069 else if (istreq_prefix("overlay", field) ||
1070 istreq_prefix("permanentoverlay", field)) {
1071 log_err(info->keymap->ctx,
1072 "Overlays not supported; "
1073 "Ignoring overlay specification for key %s\n",
1074 LongKeyNameText(keyi->name));
1076 else if (istreq(field, "repeating") ||
1077 istreq(field, "repeats") ||
1078 istreq(field, "repeat")) {
1081 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1083 log_err(info->keymap->ctx,
1084 "Illegal repeat setting for %s; "
1085 "Non-boolean repeat setting ignored\n",
1086 LongKeyNameText(keyi->name));
1090 keyi->defined |= KEY_FIELD_REPEAT;
1092 else if (istreq(field, "groupswrap") ||
1093 istreq(field, "wrapgroups")) {
1096 if (!ExprResolveBoolean(ctx, value, &set)) {
1097 log_err(info->keymap->ctx,
1098 "Illegal groupsWrap setting for %s; "
1099 "Non-boolean value ignored\n",
1100 LongKeyNameText(keyi->name));
1105 keyi->out_of_range_group_action = XkbWrapIntoRange;
1107 keyi->out_of_range_group_action = XkbClampIntoRange;
1109 keyi->defined |= KEY_FIELD_GROUPINFO;
1111 else if (istreq(field, "groupsclamp") ||
1112 istreq(field, "clampgroups")) {
1115 if (!ExprResolveBoolean(ctx, value, &set)) {
1116 log_err(info->keymap->ctx,
1117 "Illegal groupsClamp setting for %s; "
1118 "Non-boolean value ignored\n",
1119 LongKeyNameText(keyi->name));
1124 keyi->out_of_range_group_action = XkbClampIntoRange;
1126 keyi->out_of_range_group_action = XkbWrapIntoRange;
1128 keyi->defined |= KEY_FIELD_GROUPINFO;
1130 else if (istreq(field, "groupsredirect") ||
1131 istreq(field, "redirectgroups")) {
1132 xkb_group_index_t grp;
1134 if (!ExprResolveGroup(ctx, value, &grp)) {
1135 log_err(info->keymap->ctx,
1136 "Illegal group index for redirect of key %s; "
1137 "Definition with non-integer group ignored\n",
1138 LongKeyNameText(keyi->name));
1142 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1143 keyi->out_of_range_group_number = grp - 1;
1144 keyi->defined |= KEY_FIELD_GROUPINFO;
1147 log_err(info->keymap->ctx,
1148 "Unknown field %s in a symbol interpretation; "
1149 "Definition ignored\n",
1158 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1160 xkb_group_index_t grp;
1164 log_lvl(info->keymap->ctx, 1,
1165 "You must specify an index when specifying a group name; "
1166 "Group name definition without array subscript ignored\n");
1170 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1171 log_err(info->keymap->ctx,
1172 "Illegal index in group name definition; "
1173 "Definition with non-integer array index ignored\n");
1177 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1178 log_err(info->keymap->ctx,
1179 "Group name must be a string; "
1180 "Illegal name for group %d ignored\n", grp);
1184 info->groupNames[grp - 1 + info->explicit_group] =
1185 xkb_atom_intern(info->keymap->ctx, name);
1191 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1193 const char *elem, *field;
1197 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1199 return 0; /* internal error, already reported */
1200 if (elem && istreq(elem, "key")) {
1201 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1204 else if (!elem && (istreq(field, "name") ||
1205 istreq(field, "groupname"))) {
1206 ret = SetGroupName(info, arrayNdx, stmt->value);
1208 else if (!elem && (istreq(field, "groupswrap") ||
1209 istreq(field, "wrapgroups"))) {
1210 log_err(info->keymap->ctx,
1211 "Global \"groupswrap\" not supported; Ignored\n");
1214 else if (!elem && (istreq(field, "groupsclamp") ||
1215 istreq(field, "clampgroups"))) {
1216 log_err(info->keymap->ctx,
1217 "Global \"groupsclamp\" not supported; Ignored\n");
1220 else if (!elem && (istreq(field, "groupsredirect") ||
1221 istreq(field, "redirectgroups"))) {
1222 log_err(info->keymap->ctx,
1223 "Global \"groupsredirect\" not supported; Ignored\n");
1226 else if (!elem && istreq(field, "allownone")) {
1227 log_err(info->keymap->ctx,
1228 "Radio groups not supported; "
1229 "Ignoring \"allownone\" specification\n");
1233 ret = SetActionField(info->keymap, elem, field, arrayNdx,
1234 stmt->value, &info->action);
1241 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1244 const char *elem, *field;
1247 for (; def; def = (VarDef *) def->common.next) {
1248 if (def->name && def->name->op == EXPR_FIELD_REF) {
1249 ok = HandleSymbolsVar(info, def);
1254 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1261 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1266 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1273 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1275 xkb_group_index_t group = info->explicit_group;
1280 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1281 xkb_group_index_t i;
1282 log_warn(info->keymap->ctx,
1283 "For the map %s an explicit group specified, "
1284 "but key %s has more than one group defined; "
1285 "All groups except first one will be ignored\n",
1286 info->name, LongKeyNameText(keyi->name));
1287 for (i = 1; i < XkbNumKbdGroups; i++) {
1288 keyi->numLevels[i] = 0;
1289 darray_free(keyi->syms[i]);
1290 darray_free(keyi->acts[i]);
1294 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1296 keyi->numLevels[group] = keyi->numLevels[0];
1297 keyi->numLevels[0] = 0;
1298 keyi->syms[group] = keyi->syms[0];
1299 darray_init(keyi->syms[0]);
1300 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1301 darray_init(keyi->symsMapIndex[0]);
1302 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1303 darray_init(keyi->symsMapNumEntries[0]);
1304 keyi->acts[group] = keyi->acts[0];
1305 darray_init(keyi->acts[0]);
1306 keyi->types[group] = keyi->types[0];
1312 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1316 InitKeyInfo(&keyi, info->file_id);
1317 CopyKeyInfo(&info->dflt, &keyi, false);
1318 keyi.merge = stmt->merge;
1319 keyi.name = KeyNameToLong(stmt->keyName);
1320 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1325 if (!SetExplicitGroup(info, &keyi)) {
1330 if (!AddKeySymbols(info, &keyi)) {
1338 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1342 xkb_mod_index_t ndx;
1344 struct xkb_context *ctx = info->keymap->ctx;
1346 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1347 log_err(info->keymap->ctx,
1348 "Illegal modifier map definition; "
1349 "Ignoring map for non-modifier \"%s\"\n",
1350 xkb_atom_text(ctx, def->modifier));
1357 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1360 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1361 tmp.haveSymbol = false;
1362 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1364 else if (ExprResolveKeySym(ctx, key, &sym)) {
1365 tmp.haveSymbol = true;
1369 log_err(info->keymap->ctx,
1370 "Modmap entries may contain only key names or keysyms; "
1371 "Illegal definition for %s modifier ignored\n",
1372 ModIndexText(tmp.modifier));
1376 ok = AddModMapEntry(info, &tmp) && ok;
1382 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1388 info->name = strdup_safe(file->name);
1391 for (stmt = file->defs; stmt; stmt = stmt->next) {
1392 switch (stmt->type) {
1394 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1397 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1400 ok = HandleSymbolsVar(info, (VarDef *) stmt);
1403 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1407 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1410 log_err(info->keymap->ctx,
1411 "Interpretation files may not include other types; "
1412 "Ignoring %s\n", StmtTypeToString(stmt->type));
1420 if (info->errorCount > 10) {
1421 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1429 * Given a keysym @sym, return a key which generates it, or NULL.
1430 * This is used for example in a modifier map definition, such as:
1431 * modifier_map Lock { Caps_Lock };
1432 * where we want to add the Lock modifier to the modmap of the key
1433 * which matches the keysym Caps_Lock.
1434 * Since there can be many keys which generates the keysym, the key
1435 * is chosen first by lowest group in which the keysym appears, than
1436 * by lowest level and than by lowest key code.
1438 static struct xkb_key *
1439 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1441 struct xkb_key *key, *ret = NULL;
1442 xkb_group_index_t group, min_group = UINT32_MAX;
1443 xkb_level_index_t level, min_level = UINT16_MAX;
1445 xkb_foreach_key(key, keymap) {
1446 for (group = 0; group < key->num_groups; group++) {
1447 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1449 if (XkbKeyNumSyms(key, group, level) != 1 ||
1450 (XkbKeySymEntry(key, group, level))[0] != sym)
1454 * If the keysym was found in a group or level > 0, we must
1455 * keep looking since we might find a key in which the keysym
1456 * is in a lower group or level.
1458 if (group < min_group ||
1459 (group == min_group && level < min_level)) {
1461 if (group == 0 && level == 0) {
1477 * Find the given name in the keymap->map->types and return its index.
1479 * @param name The name to search for.
1480 * @param type_rtrn Set to the index of the name if found.
1482 * @return true if found, false otherwise.
1485 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn)
1489 for (i = 0; i < keymap->num_types; i++) {
1490 if (keymap->types[i].name == name) {
1500 * Assign a type to the given sym and return the Atom for the type assigned.
1503 * - ONE_LEVEL for width 0/1
1504 * - ALPHABETIC for 2 shift levels, with lower/upercase
1505 * - KEYPAD for keypad keys.
1506 * - TWO_LEVEL for other 2 shift level keys.
1507 * and the same for four level keys.
1509 * @param width Number of sysms in syms.
1510 * @param syms The keysyms for the given key (must be size width).
1511 * @param typeNameRtrn Set to the Atom of the type name.
1513 * @returns true if a type could be found, false otherwise.
1515 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1516 * all that fun stuff rather than just assuming there's always one
1520 FindAutomaticType(struct xkb_keymap *keymap, xkb_level_index_t width,
1521 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1525 if ((width == 1) || (width == 0)) {
1526 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1529 else if (width == 2) {
1530 if (syms && xkb_keysym_is_lower(syms[0]) &&
1531 xkb_keysym_is_upper(syms[1])) {
1532 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1534 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1535 xkb_keysym_is_keypad(syms[1]))) {
1536 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1540 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1544 else if (width <= 4) {
1545 if (syms && xkb_keysym_is_lower(syms[0]) &&
1546 xkb_keysym_is_upper(syms[1]))
1547 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1549 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1551 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1552 "FOUR_LEVEL_SEMIALPHABETIC");
1554 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1555 xkb_keysym_is_keypad(syms[1])))
1556 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1558 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1559 /* XXX: why not set autoType here? */
1561 return ((width >= 0) && (width <= 4));
1565 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1566 * groups, and reduce to one group if all groups are identical anyway.
1569 PrepareKeyDef(KeyInfo *keyi)
1571 xkb_group_index_t i, lastGroup;
1572 unsigned int defined;
1573 xkb_level_index_t j, width;
1576 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1577 /* get highest group number */
1578 for (i = XkbNumKbdGroups - 1; i > 0; i--) {
1579 if (defined & (1 << i))
1587 /* If there are empty groups between non-empty ones fill them with data */
1588 /* from the first group. */
1589 /* We can make a wrong assumption here. But leaving gaps is worse. */
1590 for (i = lastGroup; i > 0; i--) {
1591 if (defined & (1 << i))
1593 width = keyi->numLevels[0];
1594 if (keyi->typesDefined & 1) {
1595 for (j = 0; j < width; j++) {
1596 keyi->types[i] = keyi->types[0];
1598 keyi->typesDefined |= 1 << i;
1600 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1601 darray_copy(keyi->acts[i], keyi->acts[0]);
1602 keyi->actsDefined |= 1 << i;
1604 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1605 darray_copy(keyi->syms[i], keyi->syms[0]);
1606 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1607 darray_copy(keyi->symsMapNumEntries[i],
1608 keyi->symsMapNumEntries[0]);
1609 keyi->symsDefined |= 1 << i;
1612 keyi->numLevels[i] = keyi->numLevels[0];
1615 /* If all groups are completely identical remove them all */
1616 /* exept the first one. */
1618 for (i = lastGroup; i > 0; i--) {
1619 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1620 (keyi->types[i] != keyi->types[0])) {
1624 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1625 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1626 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1627 memcmp(darray_mem(keyi->syms[i], 0),
1628 darray_mem(keyi->syms[0], 0),
1629 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1633 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1634 (darray_empty(keyi->symsMapIndex[i]) ||
1635 darray_empty(keyi->symsMapIndex[0]) ||
1636 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1637 darray_mem(keyi->symsMapIndex[0], 0),
1638 keyi->numLevels[0] * sizeof(int)))) {
1642 if (!darray_same(keyi->symsMapNumEntries[i],
1643 keyi->symsMapNumEntries[0]) &&
1644 (darray_empty(keyi->symsMapNumEntries[i]) ||
1645 darray_empty(keyi->symsMapNumEntries[0]) ||
1646 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1647 darray_mem(keyi->symsMapNumEntries[0], 0),
1648 keyi->numLevels[0] * sizeof(size_t)))) {
1652 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1653 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1654 memcmp(darray_mem(keyi->acts[i], 0),
1655 darray_mem(keyi->acts[0], 0),
1656 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1662 for (i = lastGroup; i > 0; i--) {
1663 keyi->numLevels[i] = 0;
1664 darray_free(keyi->syms[i]);
1665 darray_free(keyi->symsMapIndex[i]);
1666 darray_free(keyi->symsMapNumEntries[i]);
1667 darray_free(keyi->acts[i]);
1670 keyi->symsDefined &= 1;
1671 keyi->actsDefined &= 1;
1672 keyi->typesDefined &= 1;
1677 * Copy the KeyInfo into the keyboard description.
1679 * This function recurses.
1682 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1683 xkb_keycode_t start_from)
1685 struct xkb_keymap *keymap = info->keymap;
1687 struct xkb_key *key;
1688 size_t sizeSyms = 0;
1689 xkb_group_index_t i, nGroups;
1690 xkb_level_index_t width, tmp;
1691 struct xkb_key_type * type;
1692 bool haveActions, autoType, useAlias;
1693 unsigned types[XkbNumKbdGroups];
1694 union xkb_action *outActs;
1695 unsigned int symIndex = 0;
1697 useAlias = (start_from == 0);
1699 key = FindNamedKey(keymap, keyi->name, useAlias,
1700 CreateKeyNames(keymap), start_from);
1702 if (start_from == 0)
1703 log_lvl(info->keymap->ctx, 5,
1704 "Key %s not found in keycodes; Symbols ignored\n",
1705 LongKeyNameText(keyi->name));
1708 kc = XkbKeyGetKeycode(keymap, key);
1710 haveActions = false;
1712 for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1713 if (((i + 1) > nGroups)
1714 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1715 || (keyi->typesDefined) & (1 << i)))
1717 if (!darray_empty(keyi->acts[i]))
1720 /* Assign the type to the key, if it is missing. */
1721 if (keyi->types[i] == XKB_ATOM_NONE) {
1722 if (keyi->dfltType != XKB_ATOM_NONE)
1723 keyi->types[i] = keyi->dfltType;
1724 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1725 darray_mem(keyi->syms[i], 0),
1726 &keyi->types[i], &autoType)) { }
1728 log_lvl(info->keymap->ctx, 5,
1729 "No automatic type for %d symbols; "
1730 "Using %s for the %s key (keycode %d)\n",
1732 xkb_atom_text(keymap->ctx, keyi->types[i]),
1733 LongKeyNameText(keyi->name), kc);
1735 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1736 if (!autoType || keyi->numLevels[i] > 2)
1737 key->explicit |= (1 << i);
1740 log_lvl(info->keymap->ctx, 3,
1741 "Type \"%s\" is not defined; "
1742 "Using default type for the %s key (keycode %d)\n",
1743 xkb_atom_text(keymap->ctx, keyi->types[i]),
1744 LongKeyNameText(keyi->name), kc);
1746 * Index 0 is guaranteed to contain something, usually
1747 * ONE_LEVEL or at least some default one-level type.
1752 /* if the type specifies fewer levels than the key has, shrink the key */
1753 type = &keymap->types[types[i]];
1754 if (type->num_levels < keyi->numLevels[i]) {
1755 log_lvl(info->keymap->ctx, 1,
1756 "Type \"%s\" has %d levels, but %s has %d symbols; "
1757 "Ignoring extra symbols\n",
1758 xkb_atom_text(keymap->ctx, type->name),
1760 LongKeyNameText(keyi->name),
1761 keyi->numLevels[i]);
1762 keyi->numLevels[i] = type->num_levels;
1764 if (keyi->numLevels[i] > width)
1765 width = keyi->numLevels[i];
1766 if (type->num_levels > width)
1767 width = type->num_levels;
1768 sizeSyms += darray_size(keyi->syms[i]);
1771 darray_resize0(key->syms, sizeSyms);
1774 outActs = ResizeKeyActions(keymap, key, width * nGroups);
1775 if (outActs == NULL) {
1776 log_wsgo(info->keymap->ctx,
1777 "Could not enlarge actions for %s (key %d)\n",
1778 LongKeyNameText(keyi->name), kc);
1781 key->explicit |= XkbExplicitInterpretMask;
1786 key->num_groups = nGroups;
1787 if (keyi->defined & KEY_FIELD_GROUPINFO) {
1788 key->out_of_range_group_number = keyi->out_of_range_group_number;
1789 key->out_of_range_group_action = keyi->out_of_range_group_action;
1792 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1793 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1795 for (i = 0; i < nGroups; i++) {
1796 /* assign kt_index[i] to the index of the type in map->types.
1797 * kt_index[i] may have been set by a previous run (if we have two
1798 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1799 * default group if we dont even have keys for this group anyway.
1801 * FIXME: There should be a better fix for this.
1803 if (keyi->numLevels[i])
1804 key->kt_index[i] = types[i];
1805 if (!darray_empty(keyi->syms[i])) {
1806 /* fill key to "width" symbols*/
1807 for (tmp = 0; tmp < width; tmp++) {
1808 if (tmp < keyi->numLevels[i] &&
1809 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1810 memcpy(darray_mem(key->syms, symIndex),
1811 darray_mem(keyi->syms[i],
1812 darray_item(keyi->symsMapIndex[i], tmp)),
1813 darray_item(keyi->symsMapNumEntries[i],
1814 tmp) * sizeof(xkb_keysym_t));
1815 key->sym_index[(i * width) + tmp] = symIndex;
1816 key->num_syms[(i * width) + tmp] =
1817 darray_item(keyi->symsMapNumEntries[i], tmp);
1818 symIndex += key->num_syms[(i * width) + tmp];
1821 key->sym_index[(i * width) + tmp] = -1;
1822 key->num_syms[(i * width) + tmp] = 0;
1824 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1825 if (tmp < keyi->numLevels[i])
1826 outActs[tmp] = darray_item(keyi->acts[i], tmp);
1828 outActs[tmp].type = XkbSA_NoAction;
1834 if (keyi->defined & KEY_FIELD_VMODMAP) {
1835 key->vmodmap = keyi->vmodmap;
1836 key->explicit |= XkbExplicitVModMapMask;
1839 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1840 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1841 key->explicit |= XkbExplicitAutoRepeatMask;
1844 /* do the same thing for the next key */
1845 CopySymbolsDef(info, keyi, kc + 1);
1850 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1852 struct xkb_key *key;
1853 struct xkb_keymap *keymap = info->keymap;
1855 if (!entry->haveSymbol) {
1856 key = FindNamedKey(keymap, entry->u.keyName, true,
1857 CreateKeyNames(keymap), 0);
1859 log_lvl(info->keymap->ctx, 5,
1860 "Key %s not found in keycodes; "
1861 "Modifier map entry for %s not updated\n",
1862 LongKeyNameText(entry->u.keyName),
1863 ModIndexText(entry->modifier));
1868 key = FindKeyForSymbol(keymap, entry->u.keySym);
1870 log_lvl(info->keymap->ctx, 5,
1871 "Key \"%s\" not found in symbol map; "
1872 "Modifier map entry for %s not updated\n",
1873 KeysymText(entry->u.keySym),
1874 ModIndexText(entry->modifier));
1879 key->modmap |= (1 << entry->modifier);
1884 * Handle the xkb_symbols section of an xkb file.
1886 * @param file The parsed xkb_symbols section of the xkb file.
1887 * @param keymap Handle to the keyboard description to store the symbols in.
1888 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1891 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1892 enum merge_mode merge)
1894 xkb_group_index_t i;
1895 struct xkb_key *key;
1900 InitSymbolsInfo(&info, keymap, file->id);
1901 info.dflt.merge = merge;
1903 HandleSymbolsFile(&info, file, merge);
1905 if (darray_empty(info.keys))
1908 if (info.errorCount != 0)
1911 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1914 keymap->symbols_section_name = strdup(info.name);
1916 for (i = 0; i < XkbNumKbdGroups; i++) {
1917 if (info.groupNames[i] != XKB_ATOM_NONE) {
1918 keymap->group_names[i] = xkb_atom_text(keymap->ctx,
1919 info.groupNames[i]);
1924 darray_foreach(keyi, info.keys)
1925 PrepareKeyDef(keyi);
1928 darray_foreach(keyi, info.keys)
1929 if (!CopySymbolsDef(&info, keyi, 0))
1932 if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1933 xkb_foreach_key(key, keymap) {
1934 if (key->name[0] == '\0')
1937 if (key->num_groups < 1)
1938 log_warn(info.keymap->ctx,
1939 "No symbols defined for %s (keycode %d)\n",
1940 KeyNameText(key->name),
1941 XkbKeyGetKeycode(keymap, key));
1945 list_foreach(mm, &info.modMaps, entry)
1946 if (!CopyModMapDef(&info, mm))
1949 FreeSymbolsInfo(&info);
1953 FreeSymbolsInfo(&info);