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 ********************************************************/
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
54 #include "xkbcomp-priv.h"
62 /* Needed to work with the typechecker. */
63 typedef darray(xkb_keysym_t) darray_xkb_keysym_t;
64 typedef darray(union xkb_action) darray_xkb_action;
69 KEY_REPEAT_UNDEFINED = -1
73 KEY_FIELD_SYMS = (1 << 0),
74 KEY_FIELD_ACTS = (1 << 1),
75 KEY_FIELD_REPEAT = (1 << 2),
76 KEY_FIELD_TYPE_DFLT = (1 << 3),
77 KEY_FIELD_TYPES = (1 << 4),
78 KEY_FIELD_GROUPINFO = (1 << 5),
79 KEY_FIELD_VMODMAP = (1 << 6),
82 typedef struct _KeyInfo {
83 enum key_field defined;
85 enum merge_mode merge;
87 unsigned long name; /* the 4 chars of the key name, as long */
88 unsigned char typesDefined;
89 unsigned char symsDefined;
90 unsigned char actsDefined;
91 xkb_level_index_t numLevels[XKB_NUM_GROUPS];
93 /* syms[group] -> Single array for all the keysyms in the group. */
94 darray_xkb_keysym_t syms[XKB_NUM_GROUPS];
96 * symsMapIndex[group][level] -> The index from which the syms for
97 * the level begin in the syms[group] array. Remember each keycode
98 * can have multiple keysyms in each level (that is, each key press
99 * can result in multiple keysyms).
101 darray(int) symsMapIndex[XKB_NUM_GROUPS];
103 * symsMapNumEntries[group][level] -> How many syms are in
104 * syms[group][symsMapIndex[group][level]].
106 darray(size_t) symsMapNumEntries[XKB_NUM_GROUPS];
108 darray_xkb_action acts[XKB_NUM_GROUPS];
110 xkb_atom_t types[XKB_NUM_GROUPS];
111 enum key_repeat repeat;
112 xkb_mod_mask_t vmodmap;
115 enum xkb_range_exceed_type out_of_range_group_action;
116 xkb_group_index_t out_of_range_group_number;
120 * Init the given key info to sane values.
123 InitKeyInfo(KeyInfo *keyi, unsigned file_id)
126 static const char dflt[4] = "*";
129 keyi->file_id = file_id;
130 keyi->merge = MERGE_OVERRIDE;
131 keyi->name = KeyNameToLong(dflt);
132 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0;
134 for (i = 0; i < XKB_NUM_GROUPS; i++) {
135 keyi->numLevels[i] = 0;
136 keyi->types[i] = XKB_ATOM_NONE;
137 darray_init(keyi->syms[i]);
138 darray_init(keyi->symsMapIndex[i]);
139 darray_init(keyi->symsMapNumEntries[i]);
140 darray_init(keyi->acts[i]);
143 keyi->dfltType = XKB_ATOM_NONE;
145 keyi->repeat = KEY_REPEAT_UNDEFINED;
146 keyi->out_of_range_group_action = RANGE_WRAP;
147 keyi->out_of_range_group_number = 0;
151 ClearKeyInfo(KeyInfo *keyi)
155 for (i = 0; i < XKB_NUM_GROUPS; i++) {
156 darray_free(keyi->syms[i]);
157 darray_free(keyi->symsMapIndex[i]);
158 darray_free(keyi->symsMapNumEntries[i]);
159 darray_free(keyi->acts[i]);
164 * Copy old into new, optionally reset old to 0.
165 * If old is reset, new simply re-uses old's memory. Otherwise, the memory is
166 * newly allocated and new points to the new memory areas.
169 CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld)
176 for (i = 0; i < XKB_NUM_GROUPS; i++) {
177 old->numLevels[i] = 0;
178 darray_init(old->symsMapIndex[i]);
179 darray_init(old->symsMapNumEntries[i]);
180 darray_init(old->syms[i]);
181 darray_init(old->acts[i]);
185 for (i = 0; i < XKB_NUM_GROUPS; i++) {
186 darray_copy(new->syms[i], old->syms[i]);
187 darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]);
188 darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]);
189 darray_copy(new->acts[i], old->acts[i]);
196 /***====================================================================***/
198 typedef struct _ModMapEntry {
200 enum merge_mode merge;
204 unsigned long keyName;
209 typedef struct _SymbolsInfo {
210 char *name; /* e.g. pc+us+inet(evdev) */
213 enum merge_mode merge;
214 xkb_group_index_t explicit_group;
215 darray(KeyInfo) keys;
218 ActionsInfo *actions;
219 xkb_atom_t groupNames[XKB_NUM_GROUPS];
223 struct xkb_keymap *keymap;
227 InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap,
228 unsigned file_id, ActionsInfo *actions)
233 info->explicit_group = 0;
234 info->errorCount = 0;
235 info->file_id = file_id;
236 info->merge = MERGE_OVERRIDE;
237 darray_init(info->keys);
238 darray_growalloc(info->keys, 110);
239 list_init(&info->modMaps);
240 for (i = 0; i < XKB_NUM_GROUPS; i++)
241 info->groupNames[i] = XKB_ATOM_NONE;
242 InitKeyInfo(&info->dflt, file_id);
243 InitVModInfo(&info->vmods, keymap);
244 info->actions = actions;
245 info->keymap = keymap;
249 ClearSymbolsInfo(SymbolsInfo * info)
252 ModMapEntry *mm, *next;
255 darray_foreach(keyi, info->keys)
257 darray_free(info->keys);
258 list_foreach_safe(mm, next, &info->modMaps, entry)
260 memset(info, 0, sizeof(SymbolsInfo));
264 ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group,
265 xkb_level_index_t numLevels, unsigned sizeSyms,
270 if (darray_size(keyi->syms[group]) < sizeSyms)
271 darray_resize0(keyi->syms[group], sizeSyms);
273 if (darray_empty(keyi->symsMapIndex[group]) ||
274 keyi->numLevels[group] < numLevels) {
275 darray_resize(keyi->symsMapIndex[group], numLevels);
276 for (i = keyi->numLevels[group]; i < numLevels; i++)
277 darray_item(keyi->symsMapIndex[group], i) = -1;
280 if (darray_empty(keyi->symsMapNumEntries[group]) ||
281 keyi->numLevels[group] < numLevels)
282 darray_resize0(keyi->symsMapNumEntries[group], numLevels);
284 if ((forceActions && (keyi->numLevels[group] < numLevels ||
285 darray_empty(keyi->acts[group]))) ||
286 (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group])))
287 darray_resize0(keyi->acts[group], numLevels);
289 if (keyi->numLevels[group] < numLevels)
290 keyi->numLevels[group] = numLevels;
295 enum key_group_selector {
302 MergeKeyGroups(SymbolsInfo * info,
303 KeyInfo * into, KeyInfo * from, xkb_group_index_t group)
305 darray_xkb_keysym_t resultSyms;
306 enum key_group_selector using = NONE;
307 darray_xkb_action resultActs;
308 xkb_level_index_t resultWidth;
309 unsigned int resultSize = 0;
312 bool report, clobber;
313 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
315 clobber = (from->merge != MERGE_AUGMENT);
317 report = (verbosity > 9) ||
318 (into->file_id == from->file_id && verbosity > 0);
320 darray_init(resultSyms);
322 if (into->numLevels[group] >= from->numLevels[group]) {
323 resultActs = into->acts[group];
324 resultWidth = into->numLevels[group];
327 resultActs = from->acts[group];
328 resultWidth = from->numLevels[group];
329 darray_resize(into->symsMapIndex[group],
330 from->numLevels[group]);
331 darray_resize0(into->symsMapNumEntries[group],
332 from->numLevels[group]);
334 for (i = into->numLevels[group]; i < from->numLevels[group]; i++)
335 darray_item(into->symsMapIndex[group], i) = -1;
338 if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) ||
339 !darray_empty(from->acts[group]))) {
340 darray_resize0(resultActs, resultWidth);
341 for (i = 0; i < resultWidth; i++) {
342 union xkb_action *fromAct = NULL, *toAct = NULL;
344 if (!darray_empty(from->acts[group]))
345 fromAct = &darray_item(from->acts[group], i);
347 if (!darray_empty(into->acts[group]))
348 toAct = &darray_item(into->acts[group], i);
350 if (((fromAct == NULL) || (fromAct->type == ACTION_TYPE_NONE))
351 && (toAct != NULL)) {
352 darray_item(resultActs, i) = *toAct;
354 else if (((toAct == NULL) || (toAct->type == ACTION_TYPE_NONE))
355 && (fromAct != NULL)) {
356 darray_item(resultActs, i) = *fromAct;
359 union xkb_action *use, *ignore;
369 log_warn(info->keymap->ctx,
370 "Multiple actions for level %d/group %u on key %s; "
371 "Using %s, ignoring %s\n",
372 i + 1, group + 1, LongKeyNameText(into->name),
373 ActionTypeText(use->type),
374 ActionTypeText(ignore->type));
376 darray_item(resultActs, i) = *use;
381 for (i = 0; i < resultWidth; i++) {
382 unsigned int fromSize = 0;
385 if (!darray_empty(from->symsMapNumEntries[group]) &&
386 i < from->numLevels[group])
387 fromSize = darray_item(from->symsMapNumEntries[group], i);
389 if (!darray_empty(into->symsMapNumEntries[group]) &&
390 i < into->numLevels[group])
391 toSize = darray_item(into->symsMapNumEntries[group], i);
394 resultSize += toSize;
397 else if (toSize == 0 || clobber) {
398 resultSize += fromSize;
402 resultSize += toSize;
411 resultSyms = from->syms[group];
412 darray_free(into->symsMapNumEntries[group]);
413 darray_free(into->symsMapIndex[group]);
414 into->symsMapNumEntries[group] = from->symsMapNumEntries[group];
415 into->symsMapIndex[group] = from->symsMapIndex[group];
416 darray_init(from->symsMapNumEntries[group]);
417 darray_init(from->symsMapIndex[group]);
420 else if (using == TO) {
421 resultSyms = into->syms[group];
425 darray_resize0(resultSyms, resultSize);
427 for (i = 0; i < resultWidth; i++) {
428 enum key_group_selector use = NONE;
429 unsigned int fromSize = 0;
430 unsigned int toSize = 0;
432 if (i < from->numLevels[group])
433 fromSize = darray_item(from->symsMapNumEntries[group], i);
435 if (i < into->numLevels[group])
436 toSize = darray_item(into->symsMapNumEntries[group], i);
438 if (fromSize == 0 && toSize == 0) {
439 darray_item(into->symsMapIndex[group], i) = -1;
440 darray_item(into->symsMapNumEntries[group], i) = 0;
446 else if (toSize == 0 || clobber)
451 if (toSize && fromSize && report) {
452 log_info(info->keymap->ctx,
453 "Multiple symbols for group %u, level %d on key %s; "
454 "Using %s, ignoring %s\n",
455 group + 1, i + 1, LongKeyNameText(into->name),
456 (use == FROM ? "from" : "to"),
457 (use == FROM ? "to" : "from"));
461 memcpy(darray_mem(resultSyms, cur_idx),
462 darray_mem(from->syms[group],
463 darray_item(from->symsMapIndex[group], i)),
464 darray_item(from->symsMapNumEntries[group],
465 i) * sizeof(xkb_keysym_t));
466 darray_item(into->symsMapIndex[group], i) = cur_idx;
467 darray_item(into->symsMapNumEntries[group], i) =
468 darray_item(from->symsMapNumEntries[group], i);
471 memcpy(darray_mem(resultSyms, cur_idx),
472 darray_mem(into->syms[group],
473 darray_item(into->symsMapIndex[group], i)),
474 darray_item(into->symsMapNumEntries[group],
475 i) * sizeof(xkb_keysym_t));
476 darray_item(into->symsMapIndex[group], i) = cur_idx;
478 cur_idx += darray_item(into->symsMapNumEntries[group], i);
482 if (!darray_same(resultActs, into->acts[group]))
483 darray_free(into->acts[group]);
484 if (!darray_same(resultActs, from->acts[group]))
485 darray_free(from->acts[group]);
486 into->numLevels[group] = resultWidth;
487 if (!darray_same(resultSyms, into->syms[group]))
488 darray_free(into->syms[group]);
489 into->syms[group] = resultSyms;
490 if (!darray_same(resultSyms, from->syms[group]))
491 darray_free(from->syms[group]);
492 darray_init(from->syms[group]);
493 darray_free(from->symsMapIndex[group]);
494 darray_free(from->symsMapNumEntries[group]);
495 into->acts[group] = resultActs;
496 darray_init(from->acts[group]);
497 if (!darray_empty(into->syms[group]))
498 into->symsDefined |= (1 << group);
499 from->symsDefined &= ~(1 << group);
500 into->actsDefined |= (1 << group);
501 from->actsDefined &= ~(1 << group);
507 UseNewKeyField(enum key_field field, KeyInfo *old, KeyInfo *new,
508 int verbosity, enum key_field *collide)
510 if (!(old->defined & field))
513 if (new->defined & field) {
514 if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
517 if (new->merge != MERGE_AUGMENT)
526 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from)
529 enum key_field collide = 0;
531 int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
533 if (from->merge == MERGE_REPLACE) {
534 for (i = 0; i < XKB_NUM_GROUPS; i++) {
535 if (into->numLevels[i] != 0) {
536 darray_free(into->syms[i]);
537 darray_free(into->acts[i]);
541 memset(from, 0, sizeof(KeyInfo));
545 report = (verbosity > 9 ||
546 (into->file_id == from->file_id && verbosity > 0));
548 for (i = 0; i < XKB_NUM_GROUPS; i++) {
549 if (from->numLevels[i] > 0) {
550 if (into->numLevels[i] == 0) {
551 into->numLevels[i] = from->numLevels[i];
552 into->syms[i] = from->syms[i];
553 into->symsMapIndex[i] = from->symsMapIndex[i];
554 into->symsMapNumEntries[i] = from->symsMapNumEntries[i];
555 into->acts[i] = from->acts[i];
556 into->symsDefined |= (1 << i);
557 darray_init(from->syms[i]);
558 darray_init(from->symsMapIndex[i]);
559 darray_init(from->symsMapNumEntries[i]);
560 darray_init(from->acts[i]);
561 from->numLevels[i] = 0;
562 from->symsDefined &= ~(1 << i);
563 if (!darray_empty(into->syms[i]))
564 into->defined |= KEY_FIELD_SYMS;
565 if (!darray_empty(into->acts[i]))
566 into->defined |= KEY_FIELD_ACTS;
570 if (!darray_empty(into->syms[i]))
571 collide |= KEY_FIELD_SYMS;
572 if (!darray_empty(into->acts[i]))
573 collide |= KEY_FIELD_ACTS;
575 MergeKeyGroups(info, into, from, (unsigned) i);
578 if (from->types[i] != XKB_ATOM_NONE) {
579 if ((into->types[i] != XKB_ATOM_NONE) && report &&
580 (into->types[i] != from->types[i])) {
581 xkb_atom_t use, ignore;
582 collide |= KEY_FIELD_TYPES;
583 if (from->merge != MERGE_AUGMENT) {
584 use = from->types[i];
585 ignore = into->types[i];
588 use = into->types[i];
589 ignore = from->types[i];
592 log_warn(info->keymap->ctx,
593 "Multiple definitions for group %d type of key %s; "
594 "Using %s, ignoring %s\n",
595 i, LongKeyNameText(into->name),
596 xkb_atom_text(info->keymap->ctx, use),
597 xkb_atom_text(info->keymap->ctx, ignore));
600 if (from->merge != MERGE_AUGMENT ||
601 into->types[i] == XKB_ATOM_NONE) {
602 into->types[i] = from->types[i];
607 if (UseNewKeyField(KEY_FIELD_VMODMAP, into, from, verbosity, &collide)) {
608 into->vmodmap = from->vmodmap;
609 into->defined |= KEY_FIELD_VMODMAP;
611 if (UseNewKeyField(KEY_FIELD_REPEAT, into, from, verbosity, &collide)) {
612 into->repeat = from->repeat;
613 into->defined |= KEY_FIELD_REPEAT;
615 if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into, from, verbosity, &collide)) {
616 into->dfltType = from->dfltType;
617 into->defined |= KEY_FIELD_TYPE_DFLT;
619 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into, from, verbosity, &collide)) {
620 into->out_of_range_group_action = from->out_of_range_group_action;
621 into->out_of_range_group_number = from->out_of_range_group_number;
622 into->defined |= KEY_FIELD_GROUPINFO;
626 log_warn(info->keymap->ctx,
627 "Symbol map for key %s redefined; "
628 "Using %s definition for conflicting fields\n",
629 LongKeyNameText(into->name),
630 (from->merge == MERGE_AUGMENT ? "first" : "last"));
636 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi)
638 unsigned long real_name;
641 darray_foreach(iter, info->keys)
642 if (iter->name == keyi->name)
643 return MergeKeys(info, iter, keyi);
645 if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name))
646 darray_foreach(iter, info->keys)
647 if (iter->name == real_name)
648 return MergeKeys(info, iter, keyi);
650 darray_resize0(info->keys, darray_size(info->keys) + 1);
651 new = &darray_item(info->keys, darray_size(info->keys) - 1);
652 return CopyKeyInfo(keyi, new, true);
656 AddModMapEntry(SymbolsInfo * info, ModMapEntry * new)
661 clobber = (new->merge != MERGE_AUGMENT);
662 list_foreach(mm, &info->modMaps, entry) {
663 if (new->haveSymbol && mm->haveSymbol
664 && (new->u.keySym == mm->u.keySym)) {
665 unsigned use, ignore;
666 if (mm->modifier != new->modifier) {
669 ignore = mm->modifier;
673 ignore = new->modifier;
675 log_err(info->keymap->ctx,
676 "%s added to symbol map for multiple modifiers; "
677 "Using %s, ignoring %s.\n",
678 KeysymText(new->u.keySym), ModIndexText(use),
679 ModIndexText(ignore));
684 if ((!new->haveSymbol) && (!mm->haveSymbol) &&
685 (new->u.keyName == mm->u.keyName)) {
686 unsigned use, ignore;
687 if (mm->modifier != new->modifier) {
690 ignore = mm->modifier;
694 ignore = new->modifier;
696 log_err(info->keymap->ctx,
697 "Key %s added to map for multiple modifiers; "
698 "Using %s, ignoring %s.\n",
699 LongKeyNameText(new->u.keyName), ModIndexText(use),
700 ModIndexText(ignore));
707 mm = malloc(sizeof(*mm));
709 log_wsgo(info->keymap->ctx,
710 "Could not allocate modifier map entry; "
711 "Modifier map for %s will be incomplete\n",
712 ModIndexText(new->modifier));
717 list_add(&mm->entry, &info->modMaps);
721 /***====================================================================***/
724 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
725 enum merge_mode merge)
729 ModMapEntry *mm, *next;
731 if (from->errorCount > 0) {
732 into->errorCount += from->errorCount;
735 if (into->name == NULL) {
736 into->name = from->name;
739 for (i = 0; i < XKB_NUM_GROUPS; i++) {
740 if (from->groupNames[i] != XKB_ATOM_NONE) {
741 if ((merge != MERGE_AUGMENT) ||
742 (into->groupNames[i] == XKB_ATOM_NONE))
743 into->groupNames[i] = from->groupNames[i];
747 darray_foreach(keyi, from->keys) {
748 if (merge != MERGE_DEFAULT)
751 if (!AddKeySymbols(into, keyi))
755 list_foreach_safe(mm, next, &from->modMaps, entry) {
756 if (merge != MERGE_DEFAULT)
758 if (!AddModMapEntry(into, mm))
762 list_init(&from->modMaps);
766 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
769 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt)
771 enum merge_mode merge = MERGE_DEFAULT;
773 SymbolsInfo included, next_incl;
775 InitSymbolsInfo(&included, info->keymap, info->file_id, info->actions);
778 included.name = stmt->stmt;
782 for (; stmt; stmt = stmt->next_incl) {
783 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS,
785 info->errorCount += 10;
786 ClearSymbolsInfo(&included);
790 InitSymbolsInfo(&next_incl, info->keymap, rtrn->id, info->actions);
791 next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE;
793 next_incl.explicit_group = atoi(stmt->modifier) - 1;
795 next_incl.explicit_group = info->explicit_group;
797 HandleSymbolsFile(&next_incl, rtrn, MERGE_OVERRIDE);
799 MergeIncludedSymbols(&included, &next_incl, merge);
801 ClearSymbolsInfo(&next_incl);
805 MergeIncludedSymbols(info, &included, merge);
806 ClearSymbolsInfo(&included);
808 return (info->errorCount == 0);
815 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
816 unsigned what, xkb_group_index_t *ndx_rtrn)
825 if (arrayNdx == NULL) {
829 defined = keyi->symsDefined;
831 defined = keyi->actsDefined;
833 for (i = 0; i < XKB_NUM_GROUPS; i++) {
834 if ((defined & (1 << i)) == 0) {
840 log_err(info->keymap->ctx,
841 "Too many groups of %s for key %s (max %u); "
842 "Ignoring %s defined for extra groups\n",
843 name, LongKeyNameText(keyi->name), XKB_NUM_GROUPS + 1, name);
847 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) {
848 log_err(info->keymap->ctx,
849 "Illegal group index for %s of key %s\n"
850 "Definition with non-integer array index ignored\n",
851 name, LongKeyNameText(keyi->name));
860 LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn)
864 if (!str || istreq(str, "any") || istreq(str, "nosymbol")) {
865 *sym_rtrn = XKB_KEY_NoSymbol;
869 if (istreq(str, "none") || istreq(str, "voidsymbol")) {
870 *sym_rtrn = XKB_KEY_VoidSymbol;
874 sym = xkb_keysym_from_name(str);
875 if (sym != XKB_KEY_NoSymbol) {
884 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
887 xkb_group_index_t ndx;
889 xkb_level_index_t nLevels;
893 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
896 keyi->symsDefined |= (1 << ndx);
899 if (value->op != EXPR_KEYSYM_LIST) {
900 log_err(info->keymap->ctx,
901 "Expected a list of symbols, found %s; "
902 "Ignoring symbols for group %u of %s\n",
903 expr_op_type_to_string(value->op), ndx + 1,
904 LongKeyNameText(keyi->name));
907 if (!darray_empty(keyi->syms[ndx])) {
908 log_err(info->keymap->ctx,
909 "Symbols for key %s, group %u already defined; "
910 "Ignoring duplicate definition\n",
911 LongKeyNameText(keyi->name), ndx + 1);
914 nSyms = darray_size(value->value.list.syms);
915 nLevels = darray_size(value->value.list.symsMapIndex);
916 if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) &&
917 (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) {
918 log_wsgo(info->keymap->ctx,
919 "Could not resize group %u of key %s to contain %zu levels; "
921 ndx + 1, LongKeyNameText(keyi->name), nSyms);
924 keyi->symsDefined |= (1 << ndx);
925 for (i = 0; i < nLevels; i++) {
926 darray_item(keyi->symsMapIndex[ndx], i) =
927 darray_item(value->value.list.symsMapIndex, i);
928 darray_item(keyi->symsMapNumEntries[ndx], i) =
929 darray_item(value->value.list.symsNumEntries, i);
931 for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) {
932 /* FIXME: What's abort() doing here? */
933 if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms)
935 if (!LookupKeysym(darray_item(value->value.list.syms,
936 darray_item(value->value.list.symsMapIndex,
938 &darray_item(keyi->syms[ndx],
939 darray_item(keyi->symsMapIndex[ndx],
941 log_warn(info->keymap->ctx,
942 "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n",
943 darray_item(value->value.list.syms, i),
944 LongKeyNameText(keyi->name),
946 xkb_atom_text(info->keymap->ctx,
947 info->groupNames[ndx]),
950 darray_item(keyi->syms[ndx],
951 darray_item(keyi->symsMapIndex[ndx],
952 i) + j) = XKB_KEY_NoSymbol;
953 darray_item(keyi->symsMapIndex[ndx], i) = -1;
954 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
957 if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 &&
958 darray_item(keyi->syms[ndx],
959 darray_item(keyi->symsMapIndex[ndx],
960 i) + j) == XKB_KEY_NoSymbol) {
961 darray_item(keyi->symsMapIndex[ndx], i) = -1;
962 darray_item(keyi->symsMapNumEntries[ndx], i) = 0;
966 for (j = keyi->numLevels[ndx] - 1;
967 j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--)
968 keyi->numLevels[ndx]--;
973 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
977 xkb_group_index_t ndx;
980 union xkb_action *toAct;
982 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
986 keyi->actsDefined |= (1 << ndx);
990 if (value->op != EXPR_ACTION_LIST) {
991 log_wsgo(info->keymap->ctx,
992 "Bad expression type (%d) for action list value; "
993 "Ignoring actions for group %u of %s\n",
994 value->op, ndx, LongKeyNameText(keyi->name));
998 if (!darray_empty(keyi->acts[ndx])) {
999 log_wsgo(info->keymap->ctx,
1000 "Actions for key %s, group %u already defined\n",
1001 LongKeyNameText(keyi->name), ndx);
1005 for (nActs = 0, act = value->value.child; act != NULL; nActs++) {
1006 act = (ExprDef *) act->common.next;
1010 log_wsgo(info->keymap->ctx,
1011 "Action list but not actions in AddActionsToKey\n");
1015 if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) &&
1016 !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) {
1017 log_wsgo(info->keymap->ctx,
1018 "Could not resize group %u of key %s; "
1020 ndx, LongKeyNameText(keyi->name));
1023 keyi->actsDefined |= (1 << ndx);
1025 toAct = darray_mem(keyi->acts[ndx], 0);
1026 act = value->value.child;
1027 for (i = 0; i < nActs; i++, toAct++) {
1028 if (!HandleActionDef(act, info->keymap, toAct, info->actions)) {
1029 log_err(info->keymap->ctx,
1030 "Illegal action definition for %s; "
1031 "Action for group %u/level %zu ignored\n",
1032 LongKeyNameText(keyi->name), ndx + 1, i + 1);
1034 act = (ExprDef *) act->common.next;
1040 static const LookupEntry repeatEntries[] = {
1041 { "true", KEY_REPEAT_YES },
1042 { "yes", KEY_REPEAT_YES },
1043 { "on", KEY_REPEAT_YES },
1044 { "false", KEY_REPEAT_NO },
1045 { "no", KEY_REPEAT_NO },
1046 { "off", KEY_REPEAT_NO },
1047 { "default", KEY_REPEAT_UNDEFINED },
1052 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
1053 ExprDef *arrayNdx, ExprDef *value)
1056 struct xkb_context *ctx = info->keymap->ctx;
1058 if (istreq(field, "type")) {
1059 xkb_group_index_t ndx;
1062 if (!ExprResolveString(ctx, value, &val))
1064 "The type field of a key symbol map must be a string; "
1065 "Ignoring illegal type definition\n");
1067 if (arrayNdx == NULL) {
1068 keyi->dfltType = val;
1069 keyi->defined |= KEY_FIELD_TYPE_DFLT;
1071 else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) {
1073 "Illegal group index for type of key %s; "
1074 "Definition with non-integer array index ignored\n",
1075 LongKeyNameText(keyi->name));
1080 keyi->types[ndx] = val;
1081 keyi->typesDefined |= (1 << ndx);
1084 else if (istreq(field, "symbols"))
1085 return AddSymbolsToKey(info, keyi, arrayNdx, value);
1086 else if (istreq(field, "actions"))
1087 return AddActionsToKey(info, keyi, arrayNdx, value);
1088 else if (istreq(field, "vmods") ||
1089 istreq(field, "virtualmods") ||
1090 istreq(field, "virtualmodifiers")) {
1091 xkb_mod_mask_t mask;
1093 ok = ExprResolveVModMask(info->keymap, value, &mask);
1095 keyi->vmodmap = (mask >> XKB_NUM_CORE_MODS) & 0xffff;
1096 keyi->defined |= KEY_FIELD_VMODMAP;
1099 log_err(info->keymap->ctx,
1100 "Expected a virtual modifier mask, found %s; "
1101 "Ignoring virtual modifiers definition for key %s\n",
1102 expr_op_type_to_string(value->op),
1103 LongKeyNameText(keyi->name));
1106 else if (istreq(field, "locking") ||
1107 istreq(field, "lock") ||
1108 istreq(field, "locks")) {
1109 log_err(info->keymap->ctx,
1110 "Key behaviors not supported; "
1111 "Ignoring locking specification for key %s\n",
1112 LongKeyNameText(keyi->name));
1114 else if (istreq(field, "radiogroup") ||
1115 istreq(field, "permanentradiogroup") ||
1116 istreq(field, "allownone")) {
1117 log_err(info->keymap->ctx,
1118 "Radio groups not supported; "
1119 "Ignoring radio group specification for key %s\n",
1120 LongKeyNameText(keyi->name));
1122 else if (istreq_prefix("overlay", field) ||
1123 istreq_prefix("permanentoverlay", field)) {
1124 log_err(info->keymap->ctx,
1125 "Overlays not supported; "
1126 "Ignoring overlay specification for key %s\n",
1127 LongKeyNameText(keyi->name));
1129 else if (istreq(field, "repeating") ||
1130 istreq(field, "repeats") ||
1131 istreq(field, "repeat")) {
1134 ok = ExprResolveEnum(ctx, value, &val, repeatEntries);
1136 log_err(info->keymap->ctx,
1137 "Illegal repeat setting for %s; "
1138 "Non-boolean repeat setting ignored\n",
1139 LongKeyNameText(keyi->name));
1143 keyi->defined |= KEY_FIELD_REPEAT;
1145 else if (istreq(field, "groupswrap") ||
1146 istreq(field, "wrapgroups")) {
1149 if (!ExprResolveBoolean(ctx, value, &set)) {
1150 log_err(info->keymap->ctx,
1151 "Illegal groupsWrap setting for %s; "
1152 "Non-boolean value ignored\n",
1153 LongKeyNameText(keyi->name));
1158 keyi->out_of_range_group_action = RANGE_WRAP;
1160 keyi->out_of_range_group_action = RANGE_SATURATE;
1162 keyi->defined |= KEY_FIELD_GROUPINFO;
1164 else if (istreq(field, "groupsclamp") ||
1165 istreq(field, "clampgroups")) {
1168 if (!ExprResolveBoolean(ctx, value, &set)) {
1169 log_err(info->keymap->ctx,
1170 "Illegal groupsClamp setting for %s; "
1171 "Non-boolean value ignored\n",
1172 LongKeyNameText(keyi->name));
1177 keyi->out_of_range_group_action = RANGE_SATURATE;
1179 keyi->out_of_range_group_action = RANGE_WRAP;
1181 keyi->defined |= KEY_FIELD_GROUPINFO;
1183 else if (istreq(field, "groupsredirect") ||
1184 istreq(field, "redirectgroups")) {
1185 xkb_group_index_t grp;
1187 if (!ExprResolveGroup(ctx, value, &grp)) {
1188 log_err(info->keymap->ctx,
1189 "Illegal group index for redirect of key %s; "
1190 "Definition with non-integer group ignored\n",
1191 LongKeyNameText(keyi->name));
1195 keyi->out_of_range_group_action = RANGE_REDIRECT;
1196 keyi->out_of_range_group_number = grp - 1;
1197 keyi->defined |= KEY_FIELD_GROUPINFO;
1200 log_err(info->keymap->ctx,
1201 "Unknown field %s in a symbol interpretation; "
1202 "Definition ignored\n",
1211 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
1213 xkb_group_index_t grp;
1217 log_vrb(info->keymap->ctx, 1,
1218 "You must specify an index when specifying a group name; "
1219 "Group name definition without array subscript ignored\n");
1223 if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) {
1224 log_err(info->keymap->ctx,
1225 "Illegal index in group name definition; "
1226 "Definition with non-integer array index ignored\n");
1230 if (!ExprResolveString(info->keymap->ctx, value, &name)) {
1231 log_err(info->keymap->ctx,
1232 "Group name must be a string; "
1233 "Illegal name for group %d ignored\n", grp);
1237 info->groupNames[grp - 1 + info->explicit_group] = name;
1242 HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt)
1244 const char *elem, *field;
1248 if (ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
1250 return 0; /* internal error, already reported */
1251 if (elem && istreq(elem, "key")) {
1252 ret = SetSymbolsField(info, &info->dflt, field, arrayNdx,
1255 else if (!elem && (istreq(field, "name") ||
1256 istreq(field, "groupname"))) {
1257 ret = SetGroupName(info, arrayNdx, stmt->value);
1259 else if (!elem && (istreq(field, "groupswrap") ||
1260 istreq(field, "wrapgroups"))) {
1261 log_err(info->keymap->ctx,
1262 "Global \"groupswrap\" not supported; Ignored\n");
1265 else if (!elem && (istreq(field, "groupsclamp") ||
1266 istreq(field, "clampgroups"))) {
1267 log_err(info->keymap->ctx,
1268 "Global \"groupsclamp\" not supported; Ignored\n");
1271 else if (!elem && (istreq(field, "groupsredirect") ||
1272 istreq(field, "redirectgroups"))) {
1273 log_err(info->keymap->ctx,
1274 "Global \"groupsredirect\" not supported; Ignored\n");
1277 else if (!elem && istreq(field, "allownone")) {
1278 log_err(info->keymap->ctx,
1279 "Radio groups not supported; "
1280 "Ignoring \"allownone\" specification\n");
1284 ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value,
1292 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1295 const char *elem, *field;
1298 for (; def; def = (VarDef *) def->common.next) {
1299 if (def->name && def->name->op == EXPR_FIELD_REF) {
1300 ok = HandleSymbolsVar(info, def);
1305 if (!def->value || def->value->op == EXPR_KEYSYM_LIST)
1312 ok = ExprResolveLhs(info->keymap->ctx, def->name, &elem, &field,
1317 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1324 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1326 xkb_group_index_t group = info->explicit_group;
1331 if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) {
1332 xkb_group_index_t i;
1333 log_warn(info->keymap->ctx,
1334 "For the map %s an explicit group specified, "
1335 "but key %s has more than one group defined; "
1336 "All groups except first one will be ignored\n",
1337 info->name, LongKeyNameText(keyi->name));
1338 for (i = 1; i < XKB_NUM_GROUPS; i++) {
1339 keyi->numLevels[i] = 0;
1340 darray_free(keyi->syms[i]);
1341 darray_free(keyi->acts[i]);
1345 keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group;
1347 keyi->numLevels[group] = keyi->numLevels[0];
1348 keyi->numLevels[0] = 0;
1349 keyi->syms[group] = keyi->syms[0];
1350 darray_init(keyi->syms[0]);
1351 keyi->symsMapIndex[group] = keyi->symsMapIndex[0];
1352 darray_init(keyi->symsMapIndex[0]);
1353 keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0];
1354 darray_init(keyi->symsMapNumEntries[0]);
1355 keyi->acts[group] = keyi->acts[0];
1356 darray_init(keyi->acts[0]);
1357 keyi->types[group] = keyi->types[0];
1363 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1367 InitKeyInfo(&keyi, info->file_id);
1368 CopyKeyInfo(&info->dflt, &keyi, false);
1369 keyi.merge = stmt->merge;
1370 keyi.name = KeyNameToLong(stmt->keyName);
1371 if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) {
1376 if (!SetExplicitGroup(info, &keyi)) {
1381 if (!AddKeySymbols(info, &keyi)) {
1389 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1393 xkb_mod_index_t ndx;
1395 struct xkb_context *ctx = info->keymap->ctx;
1397 if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) {
1398 log_err(info->keymap->ctx,
1399 "Illegal modifier map definition; "
1400 "Ignoring map for non-modifier \"%s\"\n",
1401 xkb_atom_text(ctx, def->modifier));
1408 for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) {
1411 if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) {
1412 tmp.haveSymbol = false;
1413 tmp.u.keyName = KeyNameToLong(key->value.keyName);
1415 else if (ExprResolveKeySym(ctx, key, &sym)) {
1416 tmp.haveSymbol = true;
1420 log_err(info->keymap->ctx,
1421 "Modmap entries may contain only key names or keysyms; "
1422 "Illegal definition for %s modifier ignored\n",
1423 ModIndexText(tmp.modifier));
1427 ok = AddModMapEntry(info, &tmp) && ok;
1433 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1439 info->name = strdup_safe(file->name);
1442 for (stmt = file->defs; stmt; stmt = stmt->next) {
1443 switch (stmt->type) {
1445 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1448 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1451 ok = HandleSymbolsVar(info, (VarDef *) stmt);
1454 ok = HandleVModDef((VModDef *) stmt, info->keymap, merge,
1458 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1461 log_err(info->keymap->ctx,
1462 "Interpretation files may not include other types; "
1463 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1471 if (info->errorCount > 10) {
1472 log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n",
1480 * Given a keysym @sym, return a key which generates it, or NULL.
1481 * This is used for example in a modifier map definition, such as:
1482 * modifier_map Lock { Caps_Lock };
1483 * where we want to add the Lock modifier to the modmap of the key
1484 * which matches the keysym Caps_Lock.
1485 * Since there can be many keys which generates the keysym, the key
1486 * is chosen first by lowest group in which the keysym appears, than
1487 * by lowest level and than by lowest key code.
1489 static struct xkb_key *
1490 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1492 struct xkb_key *key, *ret = NULL;
1493 xkb_group_index_t group, min_group = UINT32_MAX;
1494 xkb_level_index_t level, min_level = UINT16_MAX;
1496 xkb_foreach_key(key, keymap) {
1497 for (group = 0; group < key->num_groups; group++) {
1498 for (level = 0; level < XkbKeyGroupWidth(keymap, key, group);
1500 if (XkbKeyNumSyms(key, group, level) != 1 ||
1501 (XkbKeySymEntry(key, group, level))[0] != sym)
1505 * If the keysym was found in a group or level > 0, we must
1506 * keep looking since we might find a key in which the keysym
1507 * is in a lower group or level.
1509 if (group < min_group ||
1510 (group == min_group && level < min_level)) {
1512 if (group == 0 && level == 0) {
1528 * Find the given name in the keymap->map->types and return its index.
1530 * @param name The name to search for.
1531 * @param type_rtrn Set to the index of the name if found.
1533 * @return true if found, false otherwise.
1536 FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn)
1540 for (i = 0; i < keymap->num_types; i++) {
1541 if (keymap->types[i].name == name) {
1551 * Assign a type to the given sym and return the Atom for the type assigned.
1554 * - ONE_LEVEL for width 0/1
1555 * - ALPHABETIC for 2 shift levels, with lower/upercase
1556 * - KEYPAD for keypad keys.
1557 * - TWO_LEVEL for other 2 shift level keys.
1558 * and the same for four level keys.
1560 * @param width Number of sysms in syms.
1561 * @param syms The keysyms for the given key (must be size width).
1562 * @param typeNameRtrn Set to the Atom of the type name.
1564 * @returns true if a type could be found, false otherwise.
1566 * FIXME: I need to take the KeyInfo so I can look at symsMapIndex and
1567 * all that fun stuff rather than just assuming there's always one
1571 FindAutomaticType(struct xkb_keymap *keymap, xkb_level_index_t width,
1572 const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn,
1576 if ((width == 1) || (width == 0)) {
1577 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL");
1580 else if (width == 2) {
1581 if (syms && xkb_keysym_is_lower(syms[0]) &&
1582 xkb_keysym_is_upper(syms[1])) {
1583 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC");
1585 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1586 xkb_keysym_is_keypad(syms[1]))) {
1587 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD");
1591 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL");
1595 else if (width <= 4) {
1596 if (syms && xkb_keysym_is_lower(syms[0]) &&
1597 xkb_keysym_is_upper(syms[1]))
1598 if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3]))
1600 xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC");
1602 *typeNameRtrn = xkb_atom_intern(keymap->ctx,
1603 "FOUR_LEVEL_SEMIALPHABETIC");
1605 else if (syms && (xkb_keysym_is_keypad(syms[0]) ||
1606 xkb_keysym_is_keypad(syms[1])))
1607 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD");
1609 *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL");
1610 /* XXX: why not set autoType here? */
1616 * Ensure the given KeyInfo is in a coherent state, i.e. no gaps between the
1617 * groups, and reduce to one group if all groups are identical anyway.
1620 PrepareKeyDef(KeyInfo *keyi)
1622 xkb_group_index_t i, lastGroup;
1623 unsigned int defined;
1624 xkb_level_index_t j, width;
1627 defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined;
1628 /* get highest group number */
1629 for (i = XKB_NUM_GROUPS - 1; i > 0; i--) {
1630 if (defined & (1 << i))
1638 /* If there are empty groups between non-empty ones fill them with data */
1639 /* from the first group. */
1640 /* We can make a wrong assumption here. But leaving gaps is worse. */
1641 for (i = lastGroup; i > 0; i--) {
1642 if (defined & (1 << i))
1644 width = keyi->numLevels[0];
1645 if (keyi->typesDefined & 1) {
1646 for (j = 0; j < width; j++) {
1647 keyi->types[i] = keyi->types[0];
1649 keyi->typesDefined |= 1 << i;
1651 if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) {
1652 darray_copy(keyi->acts[i], keyi->acts[0]);
1653 keyi->actsDefined |= 1 << i;
1655 if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) {
1656 darray_copy(keyi->syms[i], keyi->syms[0]);
1657 darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]);
1658 darray_copy(keyi->symsMapNumEntries[i],
1659 keyi->symsMapNumEntries[0]);
1660 keyi->symsDefined |= 1 << i;
1663 keyi->numLevels[i] = keyi->numLevels[0];
1666 /* If all groups are completely identical remove them all */
1667 /* exept the first one. */
1669 for (i = lastGroup; i > 0; i--) {
1670 if ((keyi->numLevels[i] != keyi->numLevels[0]) ||
1671 (keyi->types[i] != keyi->types[0])) {
1675 if (!darray_same(keyi->syms[i], keyi->syms[0]) &&
1676 (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) ||
1677 darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) ||
1678 memcmp(darray_mem(keyi->syms[i], 0),
1679 darray_mem(keyi->syms[0], 0),
1680 sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) {
1684 if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) &&
1685 (darray_empty(keyi->symsMapIndex[i]) ||
1686 darray_empty(keyi->symsMapIndex[0]) ||
1687 memcmp(darray_mem(keyi->symsMapIndex[i], 0),
1688 darray_mem(keyi->symsMapIndex[0], 0),
1689 keyi->numLevels[0] * sizeof(int)))) {
1693 if (!darray_same(keyi->symsMapNumEntries[i],
1694 keyi->symsMapNumEntries[0]) &&
1695 (darray_empty(keyi->symsMapNumEntries[i]) ||
1696 darray_empty(keyi->symsMapNumEntries[0]) ||
1697 memcmp(darray_mem(keyi->symsMapNumEntries[i], 0),
1698 darray_mem(keyi->symsMapNumEntries[0], 0),
1699 keyi->numLevels[0] * sizeof(size_t)))) {
1703 if (!darray_same(keyi->acts[i], keyi->acts[0]) &&
1704 (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) ||
1705 memcmp(darray_mem(keyi->acts[i], 0),
1706 darray_mem(keyi->acts[0], 0),
1707 keyi->numLevels[0] * sizeof(union xkb_action)))) {
1713 for (i = lastGroup; i > 0; i--) {
1714 keyi->numLevels[i] = 0;
1715 darray_free(keyi->syms[i]);
1716 darray_free(keyi->symsMapIndex[i]);
1717 darray_free(keyi->symsMapNumEntries[i]);
1718 darray_free(keyi->acts[i]);
1721 keyi->symsDefined &= 1;
1722 keyi->actsDefined &= 1;
1723 keyi->typesDefined &= 1;
1728 * Copy the KeyInfo into the keyboard description.
1730 * This function recurses.
1733 CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
1734 xkb_keycode_t start_from)
1736 struct xkb_keymap *keymap = info->keymap;
1738 struct xkb_key *key;
1739 size_t sizeSyms = 0;
1740 xkb_group_index_t i, nGroups;
1741 xkb_level_index_t width, tmp;
1742 struct xkb_key_type * type;
1743 bool haveActions, autoType, useAlias;
1744 unsigned types[XKB_NUM_GROUPS];
1745 unsigned int symIndex = 0;
1747 useAlias = (start_from == 0);
1749 key = FindNamedKey(keymap, keyi->name, useAlias, start_from);
1751 if (start_from == 0)
1752 log_vrb(info->keymap->ctx, 5,
1753 "Key %s not found in keycodes; Symbols ignored\n",
1754 LongKeyNameText(keyi->name));
1757 kc = XkbKeyGetKeycode(keymap, key);
1759 haveActions = false;
1761 for (i = nGroups = 0; i < XKB_NUM_GROUPS; i++) {
1762 if (((i + 1) > nGroups)
1763 && (((keyi->symsDefined | keyi->actsDefined) & (1 << i))
1764 || (keyi->typesDefined) & (1 << i)))
1766 if (!darray_empty(keyi->acts[i]))
1769 /* Assign the type to the key, if it is missing. */
1770 if (keyi->types[i] == XKB_ATOM_NONE) {
1771 if (keyi->dfltType != XKB_ATOM_NONE)
1772 keyi->types[i] = keyi->dfltType;
1773 else if (FindAutomaticType(keymap, keyi->numLevels[i],
1774 darray_mem(keyi->syms[i], 0),
1775 &keyi->types[i], &autoType)) { }
1777 log_vrb(info->keymap->ctx, 5,
1778 "No automatic type for %d symbols; "
1779 "Using %s for the %s key (keycode %d)\n",
1781 xkb_atom_text(keymap->ctx, keyi->types[i]),
1782 LongKeyNameText(keyi->name), kc);
1784 if (FindNamedType(keymap, keyi->types[i], &types[i])) {
1785 if (!autoType || keyi->numLevels[i] > 2)
1786 key->explicit_groups |= (1 << i);
1789 log_vrb(info->keymap->ctx, 3,
1790 "Type \"%s\" is not defined; "
1791 "Using default type for the %s key (keycode %d)\n",
1792 xkb_atom_text(keymap->ctx, keyi->types[i]),
1793 LongKeyNameText(keyi->name), kc);
1795 * Index 0 is guaranteed to contain something, usually
1796 * ONE_LEVEL or at least some default one-level type.
1801 /* if the type specifies fewer levels than the key has, shrink the key */
1802 type = &keymap->types[types[i]];
1803 if (type->num_levels < keyi->numLevels[i]) {
1804 log_vrb(info->keymap->ctx, 1,
1805 "Type \"%s\" has %d levels, but %s has %d symbols; "
1806 "Ignoring extra symbols\n",
1807 xkb_atom_text(keymap->ctx, type->name),
1809 LongKeyNameText(keyi->name),
1810 keyi->numLevels[i]);
1811 keyi->numLevels[i] = type->num_levels;
1813 if (keyi->numLevels[i] > width)
1814 width = keyi->numLevels[i];
1815 if (type->num_levels > width)
1816 width = type->num_levels;
1817 sizeSyms += darray_size(keyi->syms[i]);
1820 darray_resize0(key->syms, sizeSyms);
1822 key->num_groups = nGroups;
1826 key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index));
1828 key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms));
1831 key->actions = calloc(nGroups * width, sizeof(*key->actions));
1832 key->explicit |= EXPLICIT_INTERP;
1835 key->out_of_range_group_number = keyi->out_of_range_group_number;
1836 key->out_of_range_group_action = keyi->out_of_range_group_action;
1838 for (i = 0; i < nGroups; i++) {
1839 /* assign kt_index[i] to the index of the type in map->types.
1840 * kt_index[i] may have been set by a previous run (if we have two
1841 * layouts specified). Let's not overwrite it with the ONE_LEVEL
1842 * default group if we dont even have keys for this group anyway.
1844 * FIXME: There should be a better fix for this.
1846 if (keyi->numLevels[i])
1847 key->kt_index[i] = types[i];
1848 if (!darray_empty(keyi->syms[i])) {
1849 /* fill key to "width" symbols*/
1850 for (tmp = 0; tmp < width; tmp++) {
1851 if (tmp < keyi->numLevels[i] &&
1852 darray_item(keyi->symsMapNumEntries[i], tmp) != 0) {
1853 memcpy(darray_mem(key->syms, symIndex),
1854 darray_mem(keyi->syms[i],
1855 darray_item(keyi->symsMapIndex[i], tmp)),
1856 darray_item(keyi->symsMapNumEntries[i],
1857 tmp) * sizeof(xkb_keysym_t));
1858 key->sym_index[(i * width) + tmp] = symIndex;
1859 key->num_syms[(i * width) + tmp] =
1860 darray_item(keyi->symsMapNumEntries[i], tmp);
1861 symIndex += key->num_syms[(i * width) + tmp];
1864 key->sym_index[(i * width) + tmp] = -1;
1865 key->num_syms[(i * width) + tmp] = 0;
1867 if (key->actions && !darray_empty(keyi->acts[i])) {
1868 if (tmp < keyi->numLevels[i])
1869 key->actions[tmp] = darray_item(keyi->acts[i], tmp);
1871 key->actions[tmp].type = ACTION_TYPE_NONE;
1877 if (keyi->defined & KEY_FIELD_VMODMAP) {
1878 key->vmodmap = keyi->vmodmap;
1879 key->explicit |= EXPLICIT_VMODMAP;
1882 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1883 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1884 key->explicit |= EXPLICIT_REPEAT;
1887 /* do the same thing for the next key */
1888 CopySymbolsDef(info, keyi, kc + 1);
1893 CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
1895 struct xkb_key *key;
1896 struct xkb_keymap *keymap = info->keymap;
1898 if (!entry->haveSymbol) {
1899 key = FindNamedKey(keymap, entry->u.keyName, true, 0);
1901 log_vrb(info->keymap->ctx, 5,
1902 "Key %s not found in keycodes; "
1903 "Modifier map entry for %s not updated\n",
1904 LongKeyNameText(entry->u.keyName),
1905 ModIndexText(entry->modifier));
1910 key = FindKeyForSymbol(keymap, entry->u.keySym);
1912 log_vrb(info->keymap->ctx, 5,
1913 "Key \"%s\" not found in symbol map; "
1914 "Modifier map entry for %s not updated\n",
1915 KeysymText(entry->u.keySym),
1916 ModIndexText(entry->modifier));
1921 key->modmap |= (1 << entry->modifier);
1926 * Handle the xkb_symbols section of an xkb file.
1928 * @param file The parsed xkb_symbols section of the xkb file.
1929 * @param keymap Handle to the keyboard description to store the symbols in.
1930 * @param merge Merge strategy (e.g. MERGE_OVERRIDE).
1933 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1934 enum merge_mode merge)
1936 xkb_group_index_t i;
1937 struct xkb_key *key;
1939 ActionsInfo *actions;
1943 actions = NewActionsInfo();
1947 InitSymbolsInfo(&info, keymap, file->id, actions);
1948 info.dflt.merge = merge;
1950 HandleSymbolsFile(&info, file, merge);
1952 if (darray_empty(info.keys))
1955 if (info.errorCount != 0)
1959 keymap->symbols_section_name = strdup(info.name);
1961 for (i = 0; i < XKB_NUM_GROUPS; i++)
1962 if (info.groupNames[i] != XKB_ATOM_NONE)
1963 keymap->group_names[i] = info.groupNames[i];
1966 darray_foreach(keyi, info.keys)
1967 PrepareKeyDef(keyi);
1970 darray_foreach(keyi, info.keys)
1971 if (!CopySymbolsDef(&info, keyi, 0))
1974 if (xkb_get_log_verbosity(keymap->ctx) > 3) {
1975 xkb_foreach_key(key, keymap) {
1976 if (key->name[0] == '\0')
1979 if (key->num_groups < 1)
1980 log_info(info.keymap->ctx,
1981 "No symbols defined for %s (keycode %d)\n",
1982 KeyNameText(key->name),
1983 XkbKeyGetKeycode(keymap, key));
1987 list_foreach(mm, &info.modMaps, entry)
1988 if (!CopyModMapDef(&info, mm))
1991 ClearSymbolsInfo(&info);
1992 FreeActionsInfo(actions);
1996 FreeActionsInfo(actions);
1997 ClearSymbolsInfo(&info);