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_BEHAVIOR = (1 << 3),
49 KEY_FIELD_TYPE_DFLT = (1 << 4),
50 KEY_FIELD_TYPES = (1 << 5),
51 KEY_FIELD_GROUPINFO = (1 << 6),
52 KEY_FIELD_VMODMAP = (1 << 7),
55 static inline const char *
56 longText(unsigned long val)
60 LongToKeyName(val, buf);
61 return KeyNameText(buf);
64 typedef struct _KeyInfo {
65 enum key_field defined;
67 enum merge_mode merge;
69 unsigned long name; /* the 4 chars of the key name, as long */
70 unsigned char typesDefined;
71 unsigned char symsDefined;
72 unsigned char actsDefined;
73 xkb_level_index_t numLevels[XkbNumKbdGroups];
75 /* syms[group] -> Single array for all the keysyms in the group. */
76 darray_xkb_keysym_t syms[XkbNumKbdGroups];
78 * symsMapIndex[group][level] -> The index from which the syms for
79 * the level begin in the syms[group] array. Remember each keycode
80 * can have multiple keysyms in each level (that is, each key press
81 * can result in multiple keysyms).
83 darray(int) symsMapIndex[XkbNumKbdGroups];
85 * symsMapNumEntries[group][level] -> How many syms are in
86 * syms[group][symsMapIndex[group][level]].
88 darray(size_t) symsMapNumEntries[XkbNumKbdGroups];
90 darray_xkb_action acts[XkbNumKbdGroups];
92 xkb_atom_t types[XkbNumKbdGroups];
93 enum key_repeat repeat;
94 struct xkb_behavior behavior;
95 unsigned short vmodmap;
98 uint8_t out_of_range_group_action;
99 xkb_group_index_t out_of_range_group_number;
103 * Init the given key info to sane values.
106 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
109 static const char dflt[4] = "*";
112 keyi->file_id = file_id;
113 keyi->merge = MERGE_OVERRIDE;
114 keyi->name = KeyNameToLong(dflt);
115 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
117 for (i = 0; i < XkbNumKbdGroups; i++) {
118 keyi->numLevels[i] = 0;
119 keyi->types[i] = XKB_ATOM_NONE;
120 darray_init(keyi->syms[i]);
121 darray_init(keyi->symsMapIndex[i]);
122 darray_init(keyi->symsMapNumEntries[i]);
123 darray_init(keyi->acts[i]);
126 keyi->dfltType = XKB_ATOM_NONE;
127 keyi->behavior.type = XkbKB_Default;
128 keyi->behavior.data = 0;
130 keyi->repeat = KEY_REPEAT_UNDEFINED;
131 keyi->out_of_range_group_action = 0;
132 keyi->out_of_range_group_number = 0;
136 FreeKeyInfo(KeyInfo *keyi)
140 for (i = 0; i < XkbNumKbdGroups; i++) {
141 darray_free(keyi->syms[i]);
142 darray_free(keyi->symsMapIndex[i]);
143 darray_free(keyi->symsMapNumEntries[i]);
144 darray_free(keyi->acts[i]);
149 * Copy old into new, optionally reset old to 0.
150 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
151 * newly allocated and new points to the new memory areas.
154 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
161 for (i = 0; i < XkbNumKbdGroups; i++) {
162 old->numLevels[i] = 0;
163 darray_init(old->symsMapIndex[i]);
164 darray_init(old->symsMapNumEntries[i]);
165 darray_init(old->syms[i]);
166 darray_init(old->acts[i]);
170 for (i = 0; i < XkbNumKbdGroups; i++) {
171 darray_copy(new->syms[i], old->syms[i]);
172 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
173 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
174 darray_copy(new->acts[i], old->acts[i]);
181 /***====================================================================***/
183 typedef struct _ModMapEntry {
185 enum merge_mode merge;
189 unsigned long keyName;
194 typedef struct _SymbolsInfo {
195 char *name; /* e.g. pc+us+inet(evdev) */
198 enum merge_mode merge;
199 xkb_group_index_t explicit_group;
200 darray(KeyInfo) keys;
204 xkb_atom_t groupNames[XkbNumKbdGroups];
208 struct xkb_keymap *keymap;
212 InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap,
218 info->explicit_group = 0;
219 info->errorCount = 0;
220 info->file_id = file_id;
221 info->merge = MERGE_OVERRIDE;
222 darray_init(info->keys);
223 darray_growalloc(info->keys, 110);
224 list_init(&info->modMaps);
225 for (i = 0; i < XkbNumKbdGroups; i++)
226 info->groupNames[i] = XKB_ATOM_NONE;
227 InitKeyInfo(&info->dflt, file_id);
228 InitVModInfo(&info->vmods, keymap);
230 info->keymap = keymap;
234 FreeSymbolsInfo(SymbolsInfo * info)
237 ModMapEntry *mm, *next;
240 darray_foreach(keyi, info->keys) {
243 darray_free(info->keys);
244 list_foreach_safe(mm, next, &info->modMaps, entry)
246 memset(info, 0, sizeof(SymbolsInfo));
250 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group,
251 xkb_level_index_t numLevels, unsigned sizeSyms,
256 if (darray_size(keyi->syms[group]) < sizeSyms)
257 darray_resize0(keyi->syms[group], sizeSyms);
259 if (darray_empty(keyi->symsMapIndex[group]) ||
260 keyi->numLevels[group] < numLevels) {
261 darray_resize(keyi->symsMapIndex[group], numLevels);
262 for (i = keyi->numLevels[group]; i < numLevels; i++)
263 darray_item(keyi->symsMapIndex[group], i) = -1;
266 if (darray_empty(keyi->symsMapNumEntries[group]) ||
267 keyi->numLevels[group] < numLevels)
268 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
270 if ((forceActions && (keyi->numLevels[group] < numLevels ||
271 darray_empty(keyi->acts[group]))) ||
272 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
273 darray_resize0(keyi->acts[group], numLevels);
275 if (keyi->numLevels[group] < numLevels)
276 keyi->numLevels[group] = numLevels;
281 enum key_group_selector {
288 MergeKeyGroups(SymbolsInfo * info,
289 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
291 darray_xkb_keysym_t resultSyms;
292 enum key_group_selector using = NONE;
293 darray_xkb_action resultActs;
294 xkb_level_index_t resultWidth;
295 unsigned int resultSize = 0;
298 bool report, clobber;
299 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
301 clobber = (from->merge != MERGE_AUGMENT);
303 report = (verbosity > 9) ||
304 (into->file_id == from->file_id && verbosity > 0);
306 darray_init(resultSyms);
308 if (into->numLevels[group] >= from->numLevels[group]) {
309 resultActs = into->acts[group];
310 resultWidth = into->numLevels[group];
313 resultActs = from->acts[group];
314 resultWidth = from->numLevels[group];
315 darray_resize(into->symsMapIndex[group],
316 from->numLevels[group]);
317 darray_resize0(into->symsMapNumEntries[group],
318 from->numLevels[group]);
320 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
321 darray_item(into->symsMapIndex[group], i) = -1;
324 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
325 !darray_empty(from->acts[group]))) {
326 darray_resize0(resultActs, resultWidth);
327 for (i = 0; i < resultWidth; i++) {
328 union xkb_action *fromAct = NULL, *toAct = NULL;
330 if (!darray_empty(from->acts[group]))
331 fromAct = &darray_item(from->acts[group], i);
333 if (!darray_empty(into->acts[group]))
334 toAct = &darray_item(into->acts[group], i);
336 if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction))
337 && (toAct != NULL)) {
338 darray_item(resultActs, i) = *toAct;
340 else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction))
341 && (fromAct != NULL)) {
342 darray_item(resultActs, i) = *fromAct;
345 union xkb_action *use, *ignore;
355 log_warn(info->keymap->ctx,
356 "Multiple actions for level %d/group %u on key %s; "
357 "Using %s, ignoring %s\n",
358 i + 1, group + 1, longText(into->name),
359 ActionTypeText(use->type),
360 ActionTypeText(ignore->type));
362 darray_item(resultActs, i) = *use;
367 for (i = 0; i < resultWidth; i++) {
368 unsigned int fromSize = 0;
371 if (!darray_empty(from->symsMapNumEntries[group]) &&
372 i < from->numLevels[group])
373 fromSize = darray_item(from->symsMapNumEntries[group], i);
375 if (!darray_empty(into->symsMapNumEntries[group]) &&
376 i < into->numLevels[group])
377 toSize = darray_item(into->symsMapNumEntries[group], i);
380 resultSize += toSize;
383 else if (toSize == 0 || clobber) {
384 resultSize += fromSize;
388 resultSize += toSize;
397 resultSyms = from->syms[group];
398 darray_free(into->symsMapNumEntries[group]);
399 darray_free(into->symsMapIndex[group]);
400 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
401 into->symsMapIndex[group] = from->symsMapIndex[group];
402 darray_init(from->symsMapNumEntries[group]);
403 darray_init(from->symsMapIndex[group]);
406 else if (using == TO) {
407 resultSyms = into->syms[group];
411 darray_resize0(resultSyms, resultSize);
413 for (i = 0; i < resultWidth; i++) {
414 enum key_group_selector use = NONE;
415 unsigned int fromSize = 0;
416 unsigned int toSize = 0;
418 if (i < from->numLevels[group])
419 fromSize = darray_item(from->symsMapNumEntries[group], i);
421 if (i < into->numLevels[group])
422 toSize = darray_item(into->symsMapNumEntries[group], i);
424 if (fromSize == 0 && toSize == 0) {
425 darray_item(into->symsMapIndex[group], i) = -1;
426 darray_item(into->symsMapNumEntries[group], i) = 0;
432 else if (toSize == 0 || clobber)
437 if (toSize && fromSize && report) {
438 log_info(info->keymap->ctx,
439 "Multiple symbols for group %u, level %d on key %s; "
440 "Using %s, ignoring %s\n",
441 group + 1, i + 1, longText(into->name),
442 (use == FROM ? "from" : "to"),
443 (use == FROM ? "to" : "from"));
447 memcpy(darray_mem(resultSyms, cur_idx),
448 darray_mem(from->syms[group],
449 darray_item(from->symsMapIndex[group], i)),
450 darray_item(from->symsMapNumEntries[group],
451 i) * sizeof(xkb_keysym_t));
452 darray_item(into->symsMapIndex[group], i) = cur_idx;
453 darray_item(into->symsMapNumEntries[group], i) =
454 darray_item(from->symsMapNumEntries[group], i);
457 memcpy(darray_mem(resultSyms, cur_idx),
458 darray_mem(into->syms[group],
459 darray_item(into->symsMapIndex[group], i)),
460 darray_item(into->symsMapNumEntries[group],
461 i) * sizeof(xkb_keysym_t));
462 darray_item(into->symsMapIndex[group], i) = cur_idx;
464 cur_idx += darray_item(into->symsMapNumEntries[group], i);
468 if (!darray_same(resultActs, into->acts[group]))
469 darray_free(into->acts[group]);
470 if (!darray_same(resultActs, from->acts[group]))
471 darray_free(from->acts[group]);
472 into->numLevels[group] = resultWidth;
473 if (!darray_same(resultSyms, into->syms[group]))
474 darray_free(into->syms[group]);
475 into->syms[group] = resultSyms;
476 if (!darray_same(resultSyms, from->syms[group]))
477 darray_free(from->syms[group]);
478 darray_init(from->syms[group]);
479 darray_free(from->symsMapIndex[group]);
480 darray_free(from->symsMapNumEntries[group]);
481 into->acts[group] = resultActs;
482 darray_init(from->acts[group]);
483 if (!darray_empty(into->syms[group]))
484 into->symsDefined |= (1 << group);
485 from->symsDefined &= ~(1 << group);
486 into->actsDefined |= (1 << group);
487 from->actsDefined &= ~(1 << group);
493 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
494 int verbosity, enum key_field *collide)
496 if (!(old->defined & field))
499 if (new->defined & field) {
500 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
503 if (new->merge != MERGE_AUGMENT)
512 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
515 enum key_field collide = 0;
517 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
519 if (from->merge == MERGE_REPLACE) {
520 for (i = 0; i < XkbNumKbdGroups; i++) {
521 if (into->numLevels[i] != 0) {
522 darray_free(into->syms[i]);
523 darray_free(into->acts[i]);
527 memset(from, 0, sizeof(KeyInfo));
531 report = (verbosity > 9 ||
532 (into->file_id == from->file_id && verbosity > 0));
534 for (i = 0; i < XkbNumKbdGroups; i++) {
535 if (from->numLevels[i] > 0) {
536 if (into->numLevels[i] == 0) {
537 into->numLevels[i] = from->numLevels[i];
538 into->syms[i] = from->syms[i];
539 into->symsMapIndex[i] = from->symsMapIndex[i];
540 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
541 into->acts[i] = from->acts[i];
542 into->symsDefined |= (1 << i);
543 darray_init(from->syms[i]);
544 darray_init(from->symsMapIndex[i]);
545 darray_init(from->symsMapNumEntries[i]);
546 darray_init(from->acts[i]);
547 from->numLevels[i] = 0;
548 from->symsDefined &= ~(1 << i);
549 if (!darray_empty(into->syms[i]))
550 into->defined |= KEY_FIELD_SYMS;
551 if (!darray_empty(into->acts[i]))
552 into->defined |= KEY_FIELD_ACTS;
556 if (!darray_empty(into->syms[i]))
557 collide |= KEY_FIELD_SYMS;
558 if (!darray_empty(into->acts[i]))
559 collide |= KEY_FIELD_ACTS;
561 MergeKeyGroups(info, into, from, (unsigned) i);
564 if (from->types[i] != XKB_ATOM_NONE) {
565 if ((into->types[i] != XKB_ATOM_NONE) && report &&
566 (into->types[i] != from->types[i])) {
567 xkb_atom_t use, ignore;
568 collide |= KEY_FIELD_TYPES;
569 if (from->merge != MERGE_AUGMENT) {
570 use = from->types[i];
571 ignore = into->types[i];
574 use = into->types[i];
575 ignore = from->types[i];
578 log_warn(info->keymap->ctx,
579 "Multiple definitions for group %d type of key %s; "
580 "Using %s, ignoring %s\n",
581 i, longText(into->name),
582 xkb_atom_text(info->keymap->ctx, use),
583 xkb_atom_text(info->keymap->ctx, ignore));
586 if (from->merge != MERGE_AUGMENT ||
587 into->types[i] == XKB_ATOM_NONE) {
588 into->types[i] = from->types[i];
593 if (UseNewKeyField(KEY_FIELD_BEHAVIOR, into, from, verbosity, &collide)) {
594 into->behavior = from->behavior;
595 into->defined |= KEY_FIELD_BEHAVIOR;
597 if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
598 into->vmodmap = from->vmodmap;
599 into->defined |= KEY_FIELD_VMODMAP;
601 if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
602 into->repeat = from->repeat;
603 into->defined |= KEY_FIELD_REPEAT;
605 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
606 into->dfltType = from->dfltType;
607 into->defined |= KEY_FIELD_TYPE_DFLT;
609 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
610 into->out_of_range_group_action = from->out_of_range_group_action;
611 into->out_of_range_group_number = from->out_of_range_group_number;
612 into->defined |= KEY_FIELD_GROUPINFO;
616 log_warn(info->keymap->ctx,
617 "Symbol map for key %s redefined; "
618 "Using %s definition for conflicting fields\n",
619 longText(into->name),
620 (from->merge == MERGE_AUGMENT ? "first" : "last"));
626 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
628 unsigned long real_name;
631 darray_foreach(iter, info->keys)
632 if (iter->name == keyi->name)
633 return MergeKeys(info, iter, keyi);
635 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
636 darray_foreach(iter, info->keys)
637 if (iter->name == real_name)
638 return MergeKeys(info, iter, keyi);
640 darray_resize0(info->keys, darray_size(info->keys) + 1);
641 new = &darray_item(info->keys, darray_size(info->keys) - 1);
642 return CopyKeyInfo(keyi, new, true);
646 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
651 clobber = (new->merge != MERGE_AUGMENT);
652 list_foreach(mm, &info->modMaps, entry) {
653 if (new->haveSymbol && mm->haveSymbol
654 && (new->u.keySym == mm->u.keySym)) {
655 unsigned use, ignore;
656 if (mm->modifier != new->modifier) {
659 ignore = mm->modifier;
663 ignore = new->modifier;
665 log_err(info->keymap->ctx,
666 "%s added to symbol map for multiple modifiers; "
667 "Using %s, ignoring %s.\n",
668 KeysymText(new->u.keySym), ModIndexText(use),
669 ModIndexText(ignore));
674 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
675 (new->u.keyName == mm->u.keyName)) {
676 unsigned use, ignore;
677 if (mm->modifier != new->modifier) {
680 ignore = mm->modifier;
684 ignore = new->modifier;
686 log_err(info->keymap->ctx,
687 "Key %s added to map for multiple modifiers; "
688 "Using %s, ignoring %s.\n",
689 longText(new->u.keyName), ModIndexText(use),
690 ModIndexText(ignore));
697 mm = malloc(sizeof(*mm));
699 log_wsgo(info->keymap->ctx,
700 "Could not allocate modifier map entry; "
701 "Modifier map for %s will be incomplete\n",
702 ModIndexText(new->modifier));
707 list_add(&mm->entry, &info->modMaps);
711 /***====================================================================***/
714 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
715 enum merge_mode merge)
719 ModMapEntry *mm, *next;
721 if (from->errorCount > 0) {
722 into->errorCount += from->errorCount;
725 if (into->name == NULL) {
726 into->name = from->name;
729 for (i = 0; i < XkbNumKbdGroups; i++) {
730 if (from->groupNames[i] != XKB_ATOM_NONE) {
731 if ((merge != MERGE_AUGMENT) ||
732 (into->groupNames[i] == XKB_ATOM_NONE))
733 into->groupNames[i] = from->groupNames[i];
737 darray_foreach(keyi, from->keys) {
738 if (merge != MERGE_DEFAULT)
741 if (!AddKeySymbols(into, keyi))
745 list_foreach_safe(mm, next, &from->modMaps, entry) {
746 if (merge != MERGE_DEFAULT)
748 if (!AddModMapEntry(into, mm))
752 list_init(&from->modMaps);
756 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
759 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
761 enum merge_mode merge = MERGE_DEFAULT;
763 SymbolsInfo included, next_incl;
765 InitSymbolsInfo(&included, info->keymap, info->file_id);
768 included.name = stmt->stmt;
772 for (; stmt; stmt = stmt->next_incl) {
773 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
775 info->errorCount += 10;
776 FreeSymbolsInfo(&included);
780 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id);
781 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
783 next_incl.explicit_group = atoi(stmt->modifier) - 1;
785 next_incl.explicit_group = info->explicit_group;
787 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
789 MergeIncludedSymbols(&included, &next_incl, merge);
791 FreeSymbolsInfo(&next_incl);
795 MergeIncludedSymbols(info, &included, merge);
796 FreeSymbolsInfo(&included);
798 return (info->errorCount == 0);
805 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
806 unsigned what, xkb_group_index_t *ndx_rtrn)
815 if (arrayNdx == NULL) {
819 defined = keyi->symsDefined;
821 defined = keyi->actsDefined;
823 for (i = 0; i < XkbNumKbdGroups; i++) {
824 if ((defined & (1 << i)) == 0) {
830 log_err(info->keymap->ctx,
831 "Too many groups of %s for key %s (max %u); "
832 "Ignoring %s defined for extra groups\n",
833 name, longText(keyi->name), XkbNumKbdGroups + 1, name);
837 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
838 log_err(info->keymap->ctx,
839 "Illegal group index for %s of key %s\n"
840 "Definition with non-integer array index ignored\n",
841 name, longText(keyi->name));
850 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
853 xkb_group_index_t ndx;
855 xkb_level_index_t nLevels;
859 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
862 keyi->symsDefined |= (1 << ndx);
865 if (value->op != EXPR_KEYSYM_LIST) {
866 log_err(info->keymap->ctx,
867 "Expected a list of symbols, found %s; "
868 "Ignoring symbols for group %u of %s\n",
869 exprOpText(value->op), ndx + 1, longText(keyi->name));
872 if (!darray_empty(keyi->syms[ndx])) {
873 log_err(info->keymap->ctx,
874 "Symbols for key %s, group %u already defined; "
875 "Ignoring duplicate definition\n",
876 longText(keyi->name), ndx + 1);
879 nSyms = darray_size(value->value.list.syms);
880 nLevels = darray_size(value->value.list.symsMapIndex);
881 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
882 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
883 log_wsgo(info->keymap->ctx,
884 "Could not resize group %u of key %s to contain %zu levels; "
886 ndx + 1, longText(keyi->name), nSyms);
889 keyi->symsDefined |= (1 << ndx);
890 for (i = 0; i < nLevels; i++) {
891 darray_item(keyi->symsMapIndex[ndx], i) =
892 darray_item(value->value.list.symsMapIndex, i);
893 darray_item(keyi->symsMapNumEntries[ndx], i) =
894 darray_item(value->value.list.symsNumEntries, i);
896 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
897 /* FIXME: What's abort() doing here? */
898 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
900 if (!LookupKeysym(darray_item(value->value.list.syms,
901 darray_item(value->value.list.symsMapIndex,
903 &darray_item(keyi->syms[ndx],
904 darray_item(keyi->symsMapIndex[ndx],
906 log_warn(info->keymap->ctx,
907 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
908 darray_item(value->value.list.syms, i),
909 longText(keyi->name),
911 xkb_atom_text(info->keymap->ctx,
912 info->groupNames[ndx]),
915 darray_item(keyi->syms[ndx],
916 darray_item(keyi->symsMapIndex[ndx],
917 i) + j) = XKB_KEY_NoSymbol;
918 darray_item(keyi->symsMapIndex[ndx], i) = -1;
919 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
922 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
923 darray_item(keyi->syms[ndx],
924 darray_item(keyi->symsMapIndex[ndx],
925 i) + j) == XKB_KEY_NoSymbol) {
926 darray_item(keyi->symsMapIndex[ndx], i) = -1;
927 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
931 for (j = keyi->numLevels[ndx] - 1;
932 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
933 keyi->numLevels[ndx]--;
938 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
942 xkb_group_index_t ndx;
945 struct xkb_any_action *toAct;
947 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
951 keyi->actsDefined |= (1 << ndx);
955 if (value->op != EXPR_ACTION_LIST) {
956 log_wsgo(info->keymap->ctx,
957 "Bad expression type (%d) for action list value; "
958 "Ignoring actions for group %u of %s\n",
959 value->op, ndx, longText(keyi->name));
963 if (!darray_empty(keyi->acts[ndx])) {
964 log_wsgo(info->keymap->ctx,
965 "Actions for key %s, group %u already defined\n",
966 longText(keyi->name), ndx);
970 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
971 act = (ExprDef *) act->common.next;
975 log_wsgo(info->keymap->ctx,
976 "Action list but not actions in AddActionsToKey\n");
980 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
981 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
982 log_wsgo(info->keymap->ctx,
983 "Could not resize group %u of key %s; "
985 ndx, longText(keyi->name));
988 keyi->actsDefined |= (1 << ndx);
990 toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0);
991 act = value->value.child;
992 for (i = 0; i < nActs; i++, toAct++) {
993 if (!HandleActionDef(act, info->keymap, toAct, info->action)) {
994 log_err(info->keymap->ctx,
995 "Illegal action definition for %s; "
996 "Action for group %u/level %zu ignored\n",
997 longText(keyi->name), ndx + 1, i + 1);
999 act = (ExprDef *) act->common.next;
1005 static const LookupEntry lockingEntries[] = {
1006 { "true", XkbKB_Lock },
1007 { "yes", XkbKB_Lock },
1008 { "on", XkbKB_Lock },
1009 { "false", XkbKB_Default },
1010 { "no", XkbKB_Default },
1011 { "off", XkbKB_Default },
1012 { "permanent", XkbKB_Lock | XkbKB_Permanent },
1016 static const LookupEntry repeatEntries[] = {
1017 { "true", KEY_REPEAT_YES },
1018 { "yes", KEY_REPEAT_YES },
1019 { "on", KEY_REPEAT_YES },
1020 { "false", KEY_REPEAT_NO },
1021 { "no", KEY_REPEAT_NO },
1022 { "off", KEY_REPEAT_NO },
1023 { "default", KEY_REPEAT_UNDEFINED },
1028 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1029 ExprDef *arrayNdx, ExprDef *value)
1032 struct xkb_context *ctx = info->keymap->ctx;
1034 if (istreq(field, "type")) {
1035 xkb_group_index_t ndx;
1038 if (!ExprResolveString(ctx, value, &str))
1039 log_lvl(info->keymap->ctx, 1,
1040 "The type field of a key symbol map must be a string; "
1041 "Ignoring illegal type definition\n");
1043 if (arrayNdx == NULL) {
1044 keyi->dfltType = xkb_atom_intern(ctx, str);
1045 keyi->defined |= KEY_FIELD_TYPE_DFLT;
1047 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1048 log_err(info->keymap->ctx,
1049 "Illegal group index for type of key %s; "
1050 "Definition with non-integer array index ignored\n",
1051 longText(keyi->name));
1056 keyi->types[ndx] = xkb_atom_intern(ctx, str);
1057 keyi->typesDefined |= (1 << ndx);
1060 else if (istreq(field, "symbols"))
1061 return AddSymbolsToKey(info, keyi, arrayNdx, value);
1062 else if (istreq(field, "actions"))
1063 return AddActionsToKey(info, keyi, arrayNdx, value);
1064 else if (istreq(field, "vmods") ||
1065 istreq(field, "virtualmods") ||
1066 istreq(field, "virtualmodifiers")) {
1067 xkb_mod_mask_t mask;
1069 ok = ExprResolveVModMask(info->keymap, value, &mask);
1071 keyi->vmodmap = (mask >> 8);
1072 keyi->defined |= KEY_FIELD_VMODMAP;
1075 log_err(info->keymap->ctx,
1076 "Expected a virtual modifier mask, found %s; "
1077 "Ignoring virtual modifiers definition for key %s\n",
1078 exprOpText(value->op), longText(keyi->name));
1081 else if (istreq(field, "locking") ||
1082 istreq(field, "lock") ||
1083 istreq(field, "locks")) {
1086 ok = ExprResolveEnum(ctx, value, &val, lockingEntries);
1088 keyi->behavior.type = val;
1089 keyi->defined |= KEY_FIELD_BEHAVIOR;
1091 else if (istreq(field, "radiogroup") ||
1092 istreq(field, "permanentradiogroup") ||
1093 istreq(field, "allownone")) {
1094 log_err(info->keymap->ctx,
1095 "Radio groups not supported; "
1096 "Ignoring radio group specification for key %s\n",
1097 longText(keyi->name));
1100 else if (istreq_prefix("overlay", field) ||
1101 istreq_prefix("permanentoverlay", field)) {
1102 log_err(info->keymap->ctx,
1103 "Overlays not supported; "
1104 "Ignoring overlay specification for key %s\n",
1105 longText(keyi->name));
1107 else if (istreq(field, "repeating") ||
1108 istreq(field, "repeats") ||
1109 istreq(field, "repeat")) {
1112 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1114 log_err(info->keymap->ctx,
1115 "Illegal repeat setting for %s; "
1116 "Non-boolean repeat setting ignored\n",
1117 longText(keyi->name));
1121 keyi->defined |= KEY_FIELD_REPEAT;
1123 else if (istreq(field, "groupswrap") ||
1124 istreq(field, "wrapgroups")) {
1127 if (!ExprResolveBoolean(ctx, value, &set)) {
1128 log_err(info->keymap->ctx,
1129 "Illegal groupsWrap setting for %s; "
1130 "Non-boolean value ignored\n",
1131 longText(keyi->name));
1136 keyi->out_of_range_group_action = XkbWrapIntoRange;
1138 keyi->out_of_range_group_action = XkbClampIntoRange;
1140 keyi->defined |= KEY_FIELD_GROUPINFO;
1142 else if (istreq(field, "groupsclamp") ||
1143 istreq(field, "clampgroups")) {
1146 if (!ExprResolveBoolean(ctx, value, &set)) {
1147 log_err(info->keymap->ctx,
1148 "Illegal groupsClamp setting for %s; "
1149 "Non-boolean value ignored\n",
1150 longText(keyi->name));
1155 keyi->out_of_range_group_action = XkbClampIntoRange;
1157 keyi->out_of_range_group_action = XkbWrapIntoRange;
1159 keyi->defined |= KEY_FIELD_GROUPINFO;
1161 else if (istreq(field, "groupsredirect") ||
1162 istreq(field, "redirectgroups")) {
1163 xkb_group_index_t grp;
1165 if (!ExprResolveGroup(ctx, value, &grp)) {
1166 log_err(info->keymap->ctx,
1167 "Illegal group index for redirect of key %s; "
1168 "Definition with non-integer group ignored\n",
1169 longText(keyi->name));
1173 keyi->out_of_range_group_action = XkbRedirectIntoRange;
1174 keyi->out_of_range_group_number = grp - 1;
1175 keyi->defined |= KEY_FIELD_GROUPINFO;
1178 log_err(info->keymap->ctx,
1179 "Unknown field %s in a symbol interpretation; "
1180 "Definition ignored\n",
1188 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1190 xkb_group_index_t grp;
1194 log_lvl(info->keymap->ctx, 1,
1195 "You must specify an index when specifying a group name; "
1196 "Group name definition without array subscript ignored\n");
1200 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1201 log_err(info->keymap->ctx,
1202 "Illegal index in group name definition; "
1203 "Definition with non-integer array index ignored\n");
1207 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1208 log_err(info->keymap->ctx,
1209 "Group name must be a string; "
1210 "Illegal name for group %d ignored\n", grp);
1214 info->groupNames[grp - 1 + info->explicit_group] =
1215 xkb_atom_intern(info->keymap->ctx, name);
1221 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1223 const char *elem, *field;
1227 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1229 return 0; /* internal error, already reported */
1230 if (elem && istreq(elem, "key")) {
1231 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1234 else if (!elem && (istreq(field, "name") ||
1235 istreq(field, "groupname"))) {
1236 ret = SetGroupName(info, arrayNdx, stmt->value);
1238 else if (!elem && (istreq(field, "groupswrap") ||
1239 istreq(field, "wrapgroups"))) {
1240 log_err(info->keymap->ctx,
1241 "Global \"groupswrap\" not supported; Ignored\n");
1244 else if (!elem && (istreq(field, "groupsclamp") ||
1245 istreq(field, "clampgroups"))) {
1246 log_err(info->keymap->ctx,
1247 "Global \"groupsclamp\" not supported; Ignored\n");
1250 else if (!elem && (istreq(field, "groupsredirect") ||
1251 istreq(field, "redirectgroups"))) {
1252 log_err(info->keymap->ctx,
1253 "Global \"groupsredirect\" not supported; Ignored\n");
1256 else if (!elem && istreq(field, "allownone")) {
1257 log_err(info->keymap->ctx,
1258 "Radio groups not supported; "
1259 "Ignoring \"allownone\" specification\n");
1263 ret = SetActionField(info->keymap, elem, field, arrayNdx,
1264 stmt->value, &info->action);
1271 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1274 const char *elem, *field;
1277 for (; def; def = (VarDef *) def->common.next) {
1278 if (def->name && def->name->op == EXPR_FIELD_REF) {
1279 ok = HandleSymbolsVar(info, def);
1284 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1291 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1296 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1303 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1305 xkb_group_index_t group = info->explicit_group;
1310 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1311 xkb_group_index_t i;
1312 log_warn(info->keymap->ctx,
1313 "For the map %s an explicit group specified\n",
1315 log_warn(info->keymap->ctx,
1316 "but key %s has more than one group defined; "
1317 "All groups except first one will be ignored\n",
1318 longText(keyi->name));
1319 for (i = 1; i < XkbNumKbdGroups; i++) {
1320 keyi->numLevels[i] = 0;
1321 darray_free(keyi->syms[i]);
1322 darray_free(keyi->acts[i]);
1326 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1328 keyi->numLevels[group] = keyi->numLevels[0];
1329 keyi->numLevels[0] = 0;
1330 keyi->syms[group] = keyi->syms[0];
1331 darray_init(keyi->syms[0]);
1332 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1333 darray_init(keyi->symsMapIndex[0]);
1334 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1335 darray_init(keyi->symsMapNumEntries[0]);
1336 keyi->acts[group] = keyi->acts[0];
1337 darray_init(keyi->acts[0]);
1338 keyi->types[group] = keyi->types[0];
1344 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1348 InitKeyInfo(&keyi, info->file_id);
1349 CopyKeyInfo(&info->dflt, &keyi, false);
1350 keyi.merge = stmt->merge;
1351 keyi.name = KeyNameToLong(stmt->keyName);
1352 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1357 if (!SetExplicitGroup(info, &keyi)) {
1362 if (!AddKeySymbols(info, &keyi)) {
1370 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1374 xkb_mod_index_t ndx;
1376 struct xkb_context *ctx = info->keymap->ctx;
1378 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1379 log_err(info->keymap->ctx,
1380 "Illegal modifier map definition; "
1381 "Ignoring map for non-modifier \"%s\"\n",
1382 xkb_atom_text(ctx, def->modifier));
1389 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1392 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1393 tmp.haveSymbol = false;
1394 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1396 else if (ExprResolveKeySym(ctx, key, &sym)) {
1397 tmp.haveSymbol = true;
1401 log_err(info->keymap->ctx,
1402 "Modmap entries may contain only key names or keysyms; "
1403 "Illegal definition for %s modifier ignored\n",
1404 ModIndexText(tmp.modifier));
1408 ok = AddModMapEntry(info, &tmp) && ok;
1414 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1420 info->name = strdup_safe(file->name);
1423 for (stmt = file->defs; stmt; stmt = stmt->next) {
1424 switch (stmt->type) {
1426 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1429 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1432 ok = HandleSymbolsVar(info, (VarDef *) stmt);
1435 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1439 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1442 log_err(info->keymap->ctx,
1443 "Interpretation files may not include other types; "
1444 "Ignoring %s\n", StmtTypeToString(stmt->type));
1452 if (info->errorCount > 10) {
1453 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1461 * Given a keysym @sym, return a key which generates it, or NULL.
1462 * This is used for example in a modifier map definition, such as:
1463 * modifier_map Lock { Caps_Lock };
1464 * where we want to add the Lock modifier to the modmap of the key
1465 * which matches the keysym Caps_Lock.
1466 * Since there can be many keys which generates the keysym, the key
1467 * is chosen first by lowest group in which the keysym appears, than
1468 * by lowest level and than by lowest key code.
1470 static struct xkb_key *
1471 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1473 struct xkb_key *key, *ret = NULL;
1474 xkb_group_index_t group, min_group = UINT32_MAX;
1475 xkb_level_index_t level, min_level = UINT16_MAX;
1477 xkb_foreach_key(key, keymap) {
1478 for (group = 0; group < key->num_groups; group++) {
1479 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1481 if (XkbKeyNumSyms(key, group, level) != 1 ||
1482 (XkbKeySymEntry(key, group, level))[0] != sym)
1486 * If the keysym was found in a group or level > 0, we must
1487 * keep looking since we might find a key in which the keysym
1488 * is in a lower group or level.
1490 if (group < min_group ||
1491 (group == min_group && level < min_level)) {
1493 if (group == 0 && level == 0) {
1509 * Find the given name in the keymap->map->types and return its index.
1511 * @param atom The atom to search for.
1512 * @param type_rtrn Set to the index of the name if found.
1514 * @return true if found, false otherwise.
1517 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
1520 const char *name = xkb_atom_text(keymap->ctx, atom);
1521 struct xkb_key_type *type;
1524 darray_foreach(type, keymap->types) {
1525 if (streq(type->name, name)) {
1536 * Assign a type to the given sym and return the Atom for the type assigned.
1539 * - ONE_LEVEL for width 0/1
1540 * - ALPHABETIC for 2 shift levels, with lower/upercase
1541 * - KEYPAD for keypad keys.
1542 * - TWO_LEVEL for other 2 shift level keys.
1543 * and the same for four level keys.
1545 * @param width Number of sysms in syms.
1546 * @param syms The keysyms for the given key (must be size width).
1547 * @param typeNameRtrn Set to the Atom of the type name.
1549 * @returns true if a type could be found, false otherwise.
1551 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1552 * all that fun stuff rather than just assuming there's always one
1556 FindAutomaticType(struct xkb_keymap *keymap, xkb_level_index_t width,
1557 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1561 if ((width == 1) || (width == 0)) {
1562 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1565 else if (width == 2) {
1566 if (syms && xkb_keysym_is_lower(syms[0]) &&
1567 xkb_keysym_is_upper(syms[1])) {
1568 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1570 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1571 xkb_keysym_is_keypad(syms[1]))) {
1572 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1576 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1580 else if (width <= 4) {
1581 if (syms && xkb_keysym_is_lower(syms[0]) &&
1582 xkb_keysym_is_upper(syms[1]))
1583 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1585 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1587 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1588 "FOUR_LEVEL_SEMIALPHABETIC");
1590 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1591 xkb_keysym_is_keypad(syms[1])))
1592 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1594 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1595 /* XXX: why not set autoType here? */
1597 return ((width >= 0) && (width <= 4));
1601 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1602 * groups, and reduce to one group if all groups are identical anyway.
1605 PrepareKeyDef(KeyInfo *keyi)
1607 xkb_group_index_t i, lastGroup;
1608 unsigned int defined;
1609 xkb_level_index_t j, width;
1612 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1613 /* get highest group number */
1614 for (i = XkbNumKbdGroups - 1; i > 0; i--) {
1615 if (defined & (1 << i))
1623 /* If there are empty groups between non-empty ones fill them with data */
1624 /* from the first group. */
1625 /* We can make a wrong assumption here. But leaving gaps is worse. */
1626 for (i = lastGroup; i > 0; i--) {
1627 if (defined & (1 << i))
1629 width = keyi->numLevels[0];
1630 if (keyi->typesDefined & 1) {
1631 for (j = 0; j < width; j++) {
1632 keyi->types[i] = keyi->types[0];
1634 keyi->typesDefined |= 1 << i;
1636 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1637 darray_copy(keyi->acts[i], keyi->acts[0]);
1638 keyi->actsDefined |= 1 << i;
1640 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1641 darray_copy(keyi->syms[i], keyi->syms[0]);
1642 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1643 darray_copy(keyi->symsMapNumEntries[i],
1644 keyi->symsMapNumEntries[0]);
1645 keyi->symsDefined |= 1 << i;
1648 keyi->numLevels[i] = keyi->numLevels[0];
1651 /* If all groups are completely identical remove them all */
1652 /* exept the first one. */
1654 for (i = lastGroup; i > 0; i--) {
1655 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1656 (keyi->types[i] != keyi->types[0])) {
1660 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1661 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1662 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1663 memcmp(darray_mem(keyi->syms[i], 0),
1664 darray_mem(keyi->syms[0], 0),
1665 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1669 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1670 (darray_empty(keyi->symsMapIndex[i]) ||
1671 darray_empty(keyi->symsMapIndex[0]) ||
1672 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1673 darray_mem(keyi->symsMapIndex[0], 0),
1674 keyi->numLevels[0] * sizeof(int)))) {
1678 if (!darray_same(keyi->symsMapNumEntries[i],
1679 keyi->symsMapNumEntries[0]) &&
1680 (darray_empty(keyi->symsMapNumEntries[i]) ||
1681 darray_empty(keyi->symsMapNumEntries[0]) ||
1682 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1683 darray_mem(keyi->symsMapNumEntries[0], 0),
1684 keyi->numLevels[0] * sizeof(size_t)))) {
1688 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1689 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1690 memcmp(darray_mem(keyi->acts[i], 0),
1691 darray_mem(keyi->acts[0], 0),
1692 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1698 for (i = lastGroup; i > 0; i--) {
1699 keyi->numLevels[i] = 0;
1700 darray_free(keyi->syms[i]);
1701 darray_free(keyi->symsMapIndex[i]);
1702 darray_free(keyi->symsMapNumEntries[i]);
1703 darray_free(keyi->acts[i]);
1706 keyi->symsDefined &= 1;
1707 keyi->actsDefined &= 1;
1708 keyi->typesDefined &= 1;
1713 * Copy the KeyInfo into the keyboard description.
1715 * This function recurses.
1718 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1719 xkb_keycode_t start_from)
1721 struct xkb_keymap *keymap = info->keymap;
1723 struct xkb_key *key;
1724 size_t sizeSyms = 0;
1725 xkb_group_index_t i, nGroups;
1726 xkb_level_index_t width, tmp;
1727 struct xkb_key_type * type;
1728 bool haveActions, autoType, useAlias;
1729 unsigned types[XkbNumKbdGroups];
1730 union xkb_action *outActs;
1731 unsigned int symIndex = 0;
1733 useAlias = (start_from == 0);
1735 key = FindNamedKey(keymap, keyi->name, useAlias,
1736 CreateKeyNames(keymap), start_from);
1738 if (start_from == 0)
1739 log_lvl(info->keymap->ctx, 5,
1740 "Key %s not found in keycodes; Symbols ignored\n",
1741 longText(keyi->name));
1744 kc = XkbKeyGetKeycode(keymap, key);
1746 haveActions = false;
1748 for (i = nGroups = 0; i < XkbNumKbdGroups; i++) {
1749 if (((i + 1) > nGroups)
1750 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1751 || (keyi->typesDefined) & (1 << i)))
1753 if (!darray_empty(keyi->acts[i]))
1756 /* Assign the type to the key, if it is missing. */
1757 if (keyi->types[i] == XKB_ATOM_NONE) {
1758 if (keyi->dfltType != XKB_ATOM_NONE)
1759 keyi->types[i] = keyi->dfltType;
1760 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1761 darray_mem(keyi->syms[i], 0),
1762 &keyi->types[i], &autoType)) { }
1764 log_lvl(info->keymap->ctx, 5,
1765 "No automatic type for %d symbols; "
1766 "Using %s for the %s key (keycode %d)\n",
1768 xkb_atom_text(keymap->ctx, keyi->types[i]),
1769 longText(keyi->name), kc);
1771 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1772 if (!autoType || keyi->numLevels[i] > 2)
1773 key->explicit |= (1 << i);
1776 log_lvl(info->keymap->ctx, 3,
1777 "Type \"%s\" is not defined; "
1778 "Using TWO_LEVEL for the %s key (keycode %d)\n",
1779 xkb_atom_text(keymap->ctx, keyi->types[i]),
1780 longText(keyi->name), kc);
1781 types[i] = XkbTwoLevelIndex;
1784 /* if the type specifies fewer levels than the key has, shrink the key */
1785 type = &darray_item(keymap->types, types[i]);
1786 if (type->num_levels < keyi->numLevels[i]) {
1787 log_lvl(info->keymap->ctx, 1,
1788 "Type \"%s\" has %d levels, but %s has %d symbols; "
1789 "Ignoring extra symbols\n",
1790 type->name, type->num_levels,
1791 longText(keyi->name),
1792 keyi->numLevels[i]);
1793 keyi->numLevels[i] = type->num_levels;
1795 if (keyi->numLevels[i] > width)
1796 width = keyi->numLevels[i];
1797 if (type->num_levels > width)
1798 width = type->num_levels;
1799 sizeSyms += darray_size(keyi->syms[i]);
1802 darray_resize0(key->syms, sizeSyms);
1805 outActs = ResizeKeyActions(keymap, key, width * nGroups);
1806 if (outActs == NULL) {
1807 log_wsgo(info->keymap->ctx,
1808 "Could not enlarge actions for %s (key %d)\n",
1809 longText(keyi->name), kc);
1812 key->explicit |= XkbExplicitInterpretMask;
1817 key->num_groups = nGroups;
1818 if (keyi->defined & KEY_FIELD_GROUPINFO) {
1819 key->out_of_range_group_number = keyi->out_of_range_group_number;
1820 key->out_of_range_group_action = keyi->out_of_range_group_action;
1823 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1824 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1826 for (i = 0; i < nGroups; i++) {
1827 /* assign kt_index[i] to the index of the type in map->types.
1828 * kt_index[i] may have been set by a previous run (if we have two
1829 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1830 * default group if we dont even have keys for this group anyway.
1832 * FIXME: There should be a better fix for this.
1834 if (keyi->numLevels[i])
1835 key->kt_index[i] = types[i];
1836 if (!darray_empty(keyi->syms[i])) {
1837 /* fill key to "width" symbols*/
1838 for (tmp = 0; tmp < width; tmp++) {
1839 if (tmp < keyi->numLevels[i] &&
1840 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1841 memcpy(darray_mem(key->syms, symIndex),
1842 darray_mem(keyi->syms[i],
1843 darray_item(keyi->symsMapIndex[i], tmp)),
1844 darray_item(keyi->symsMapNumEntries[i],
1845 tmp) * sizeof(xkb_keysym_t));
1846 key->sym_index[(i * width) + tmp] = symIndex;
1847 key->num_syms[(i * width) + tmp] =
1848 darray_item(keyi->symsMapNumEntries[i], tmp);
1849 symIndex += key->num_syms[(i * width) + tmp];
1852 key->sym_index[(i * width) + tmp] = -1;
1853 key->num_syms[(i * width) + tmp] = 0;
1855 if (outActs != NULL && !darray_empty(keyi->acts[i])) {
1856 if (tmp < keyi->numLevels[i])
1857 outActs[tmp] = darray_item(keyi->acts[i], tmp);
1859 outActs[tmp].type = XkbSA_NoAction;
1864 switch (keyi->behavior.type & XkbKB_OpMask) {
1869 key->behavior = keyi->behavior;
1870 key->explicit |= XkbExplicitBehaviorMask;
1873 if (keyi->defined & KEY_FIELD_VMODMAP) {
1874 key->vmodmap = keyi->vmodmap;
1875 key->explicit |= XkbExplicitVModMapMask;
1877 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1878 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1879 key->explicit |= XkbExplicitAutoRepeatMask;
1882 /* do the same thing for the next key */
1883 CopySymbolsDef(info, keyi, kc + 1);
1888 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1890 struct xkb_key *key;
1891 struct xkb_keymap *keymap = info->keymap;
1893 if (!entry->haveSymbol) {
1894 key = FindNamedKey(keymap, entry->u.keyName, true,
1895 CreateKeyNames(keymap), 0);
1897 log_lvl(info->keymap->ctx, 5,
1898 "Key %s not found in keycodes; "
1899 "Modifier map entry for %s not updated\n",
1900 longText(entry->u.keyName),
1901 ModIndexText(entry->modifier));
1906 key = FindKeyForSymbol(keymap, entry->u.keySym);
1908 log_lvl(info->keymap->ctx, 5,
1909 "Key \"%s\" not found in symbol map; "
1910 "Modifier map entry for %s not updated\n",
1911 KeysymText(entry->u.keySym),
1912 ModIndexText(entry->modifier));
1917 key->modmap |= (1 << entry->modifier);
1922 * Handle the xkb_symbols section of an xkb file.
1924 * @param file The parsed xkb_symbols section of the xkb file.
1925 * @param keymap Handle to the keyboard description to store the symbols in.
1926 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1929 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1930 enum merge_mode merge)
1932 xkb_group_index_t i;
1933 struct xkb_key *key;
1938 InitSymbolsInfo(&info, keymap, file->id);
1939 info.dflt.merge = merge;
1941 HandleSymbolsFile(&info, file, merge);
1943 if (darray_empty(info.keys))
1946 if (info.errorCount != 0)
1949 darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1);
1952 keymap->symbols_section_name = strdup(info.name);
1954 for (i = 0; i < XkbNumKbdGroups; i++) {
1955 if (info.groupNames[i] != XKB_ATOM_NONE) {
1956 keymap->group_names[i] = xkb_atom_text(keymap->ctx,
1957 info.groupNames[i]);
1962 darray_foreach(keyi, info.keys)
1963 PrepareKeyDef(keyi);
1966 darray_foreach(keyi, info.keys)
1967 if (!CopySymbolsDef(&info, keyi, 0))
1970 if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1971 xkb_foreach_key(key, keymap) {
1972 if (key->name[0] == '\0')
1975 if (key->num_groups < 1)
1976 log_warn(info.keymap->ctx,
1977 "No symbols defined for <%.4s> (keycode %d)\n",
1978 key->name, XkbKeyGetKeycode(keymap, key));
1982 list_foreach(mm, &info.modMaps, entry)
1983 if (!CopyModMapDef(&info, mm))
1986 FreeSymbolsInfo(&info);
1990 FreeSymbolsInfo(&info);