X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fxkbcomp%2Fsymbols.c;h=9c4fb1c9c39d67f414d999c755d96a6ecc6b1444;hb=b21107056e0540489669bb35fcb3abe1b5b46ecd;hp=f196f0b746a742a3cda40337898dd997fa9e46f8;hpb=27f9492969a4f9e2b31ecc1053233dfe0726dfcf;p=platform%2Fupstream%2Flibxkbcommon.git diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c index f196f0b..9c4fb1c 100644 --- a/src/xkbcomp/symbols.c +++ b/src/xkbcomp/symbols.c @@ -24,162 +24,129 @@ * ********************************************************/ -#include +/* + * Copyright © 2012 Intel Corporation + * Copyright © 2012 Ran Benita + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Author: Daniel Stone + * Ran Benita + */ #include "xkbcomp-priv.h" -#include "parseutils.h" +#include "text.h" +#include "expr.h" #include "action.h" #include "vmod.h" +#include "keycodes.h" +#include "include.h" +#include "keysym.h" + +enum key_repeat { + KEY_REPEAT_UNDEFINED = 0, + KEY_REPEAT_YES = 1, + KEY_REPEAT_NO = 2, +}; -/***====================================================================***/ +enum group_field { + GROUP_FIELD_SYMS = (1 << 0), + GROUP_FIELD_ACTS = (1 << 1), + GROUP_FIELD_TYPE = (1 << 2), +}; + +enum key_field { + KEY_FIELD_REPEAT = (1 << 0), + KEY_FIELD_TYPE_DFLT = (1 << 1), + KEY_FIELD_GROUPINFO = (1 << 2), + KEY_FIELD_VMODMAP = (1 << 3), +}; + +typedef struct { + unsigned int num_syms; + unsigned int sym_index; + union xkb_action act; +} LevelInfo; -/* Needed to work with the typechecker. */ typedef darray(xkb_keysym_t) darray_xkb_keysym_t; -typedef darray(union xkb_action) darray_xkb_action; - -#define RepeatYes 1 -#define RepeatNo 0 -#define RepeatUndefined ~((unsigned) 0) - -#define _Key_Syms (1 << 0) -#define _Key_Acts (1 << 1) -#define _Key_Repeat (1 << 2) -#define _Key_Behavior (1 << 3) -#define _Key_Type_Dflt (1 << 4) -#define _Key_Types (1 << 5) -#define _Key_GroupInfo (1 << 6) -#define _Key_VModMap (1 << 7) - -static inline const char * -longText(unsigned long val) -{ - char buf[4]; - LongToKeyName(val, buf); - return KeyNameText(buf); -} +typedef struct { + enum group_field defined; + darray_xkb_keysym_t syms; + darray(LevelInfo) levels; + xkb_atom_t type; +} GroupInfo; typedef struct _KeyInfo { - unsigned short defined; + enum key_field defined; unsigned file_id; enum merge_mode merge; unsigned long name; /* the 4 chars of the key name, as long */ - unsigned char typesDefined; - unsigned char symsDefined; - unsigned char actsDefined; - unsigned int numLevels[XkbNumKbdGroups]; - - /* syms[group] -> Single array for all the keysyms in the group. */ - darray_xkb_keysym_t syms[XkbNumKbdGroups]; - /* - * symsMapIndex[group][level] -> The index from which the syms for - * the level begin in the syms[group] array. Remember each keycode - * can have multiple keysyms in each level (that is, each key press - * can result in multiple keysyms). - */ - darray(int) symsMapIndex[XkbNumKbdGroups]; - /* - * symsMapNumEntries[group][level] -> How many syms are in - * syms[group][symsMapIndex[group][level]]. - */ - darray(size_t) symsMapNumEntries[XkbNumKbdGroups]; - darray_xkb_action acts[XkbNumKbdGroups]; + GroupInfo groups[XKB_NUM_GROUPS]; - xkb_atom_t types[XkbNumKbdGroups]; - unsigned repeat; - struct xkb_behavior behavior; - unsigned short vmodmap; + enum key_repeat repeat; + xkb_mod_mask_t vmodmap; xkb_atom_t dfltType; - uint8_t out_of_range_group_action; + enum xkb_range_exceed_type out_of_range_group_action; xkb_group_index_t out_of_range_group_number; } KeyInfo; -/** - * Init the given key info to sane values. - */ static void -InitKeyInfo(KeyInfo *keyi, unsigned file_id) +InitGroupInfo(GroupInfo *groupi) { - xkb_group_index_t i; - static const char dflt[4] = "*"; + memset(groupi, 0, sizeof(*groupi)); +} - keyi->defined = 0; - keyi->file_id = file_id; - keyi->merge = MERGE_OVERRIDE; - keyi->name = KeyNameToLong(dflt); - keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 0; - - for (i = 0; i < XkbNumKbdGroups; i++) { - keyi->numLevels[i] = 0; - keyi->types[i] = XKB_ATOM_NONE; - darray_init(keyi->syms[i]); - darray_init(keyi->symsMapIndex[i]); - darray_init(keyi->symsMapNumEntries[i]); - darray_init(keyi->acts[i]); - } - - keyi->dfltType = XKB_ATOM_NONE; - keyi->behavior.type = XkbKB_Default; - keyi->behavior.data = 0; - keyi->vmodmap = 0; - keyi->repeat = RepeatUndefined; - keyi->out_of_range_group_action = 0; - keyi->out_of_range_group_number = 0; +static void +ClearGroupInfo(GroupInfo *groupi) +{ + darray_free(groupi->syms); + darray_free(groupi->levels); } static void -FreeKeyInfo(KeyInfo *keyi) +InitKeyInfo(KeyInfo *keyi, unsigned file_id) { - xkb_group_index_t i; + static const char dflt_key_name[XKB_KEY_NAME_LENGTH] = "*"; - for (i = 0; i < XkbNumKbdGroups; i++) { - darray_free(keyi->syms[i]); - darray_free(keyi->symsMapIndex[i]); - darray_free(keyi->symsMapNumEntries[i]); - darray_free(keyi->acts[i]); - } + memset(keyi, 0, sizeof(*keyi)); + keyi->file_id = file_id; + keyi->merge = MERGE_OVERRIDE; + keyi->name = KeyNameToLong(dflt_key_name); + keyi->out_of_range_group_action = RANGE_WRAP; } -/** - * Copy old into new, optionally reset old to 0. - * If old is reset, new simply re-uses old's memory. Otherwise, the memory is - * newly allocated and new points to the new memory areas. - */ -static bool -CopyKeyInfo(KeyInfo * old, KeyInfo * new, bool clearOld) +static void +ClearKeyInfo(KeyInfo *keyi) { xkb_group_index_t i; - - *new = *old; - - if (clearOld) { - for (i = 0; i < XkbNumKbdGroups; i++) { - old->numLevels[i] = 0; - darray_init(old->symsMapIndex[i]); - darray_init(old->symsMapNumEntries[i]); - darray_init(old->syms[i]); - darray_init(old->acts[i]); - } - } - else { - for (i = 0; i < XkbNumKbdGroups; i++) { - darray_copy(new->syms[i], old->syms[i]); - darray_copy(new->symsMapIndex[i], old->symsMapIndex[i]); - darray_copy(new->symsMapNumEntries[i], old->symsMapNumEntries[i]); - darray_copy(new->acts[i], old->acts[i]); - } - } - - return true; + for (i = 0; i < XKB_NUM_GROUPS; i++) + ClearGroupInfo(&keyi->groups[i]); } /***====================================================================***/ typedef struct _ModMapEntry { - struct list entry; enum merge_mode merge; bool haveSymbol; int modifier; @@ -198,306 +165,224 @@ typedef struct _SymbolsInfo { darray(KeyInfo) keys; KeyInfo dflt; VModInfo vmods; - ActionInfo *action; - xkb_atom_t groupNames[XkbNumKbdGroups]; - - struct list modMaps; + ActionsInfo *actions; + xkb_atom_t groupNames[XKB_NUM_GROUPS]; + darray(ModMapEntry) modMaps; struct xkb_keymap *keymap; } SymbolsInfo; static void -InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap, - unsigned file_id) +InitSymbolsInfo(SymbolsInfo *info, struct xkb_keymap *keymap, + unsigned file_id, ActionsInfo *actions) { - xkb_group_index_t i; - - info->name = NULL; - info->explicit_group = 0; - info->errorCount = 0; + memset(info, 0, sizeof(*info)); + info->keymap = keymap; info->file_id = file_id; info->merge = MERGE_OVERRIDE; - darray_init(info->keys); - darray_growalloc(info->keys, 110); - list_init(&info->modMaps); - for (i = 0; i < XkbNumKbdGroups; i++) - info->groupNames[i] = XKB_ATOM_NONE; InitKeyInfo(&info->dflt, file_id); InitVModInfo(&info->vmods, keymap); - info->action = NULL; - info->keymap = keymap; + info->actions = actions; } static void -FreeSymbolsInfo(SymbolsInfo * info) +ClearSymbolsInfo(SymbolsInfo * info) { KeyInfo *keyi; - ModMapEntry *mm, *next; - free(info->name); - darray_foreach(keyi, info->keys) { - FreeKeyInfo(keyi); - } + darray_foreach(keyi, info->keys) + ClearKeyInfo(keyi); darray_free(info->keys); - list_foreach_safe(mm, next, &info->modMaps, entry) - free(mm); - memset(info, 0, sizeof(SymbolsInfo)); + darray_free(info->modMaps); } static bool -ResizeKeyGroup(KeyInfo *keyi, xkb_group_index_t group, unsigned int numLevels, - unsigned sizeSyms, bool forceActions) +MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber, + bool report, xkb_group_index_t group, unsigned long key_name) { - int i; - - if (darray_size(keyi->syms[group]) < sizeSyms) - darray_resize0(keyi->syms[group], sizeSyms); - - if (darray_empty(keyi->symsMapIndex[group]) || - keyi->numLevels[group] < numLevels) { - darray_resize(keyi->symsMapIndex[group], numLevels); - for (i = keyi->numLevels[group]; i < numLevels; i++) - darray_item(keyi->symsMapIndex[group], i) = -1; - } - - if (darray_empty(keyi->symsMapNumEntries[group]) || - keyi->numLevels[group] < numLevels) - darray_resize0(keyi->symsMapNumEntries[group], numLevels); - - if ((forceActions && (keyi->numLevels[group] < numLevels || - darray_empty(keyi->acts[group]))) || - (keyi->numLevels[group] < numLevels && !darray_empty(keyi->acts[group]))) - darray_resize0(keyi->acts[group], numLevels); - - if (keyi->numLevels[group] < numLevels) - keyi->numLevels[group] = numLevels; - - return true; -} - -enum key_group_selector { - NONE = 0, - FROM = (1 << 0), - TO = (1 << 1), -}; + xkb_level_index_t i, numLevels; + enum { INTO = (1 << 0), FROM = (1 << 1) } using; -static bool -MergeKeyGroups(SymbolsInfo * info, - KeyInfo * into, KeyInfo * from, xkb_group_index_t group) -{ - darray_xkb_keysym_t resultSyms; - enum key_group_selector using = NONE; - darray_xkb_action resultActs; - unsigned int resultWidth; - unsigned int resultSize = 0; - int cur_idx = 0; - int i; - bool report, clobber; - int verbosity = xkb_get_log_verbosity(info->keymap->ctx); + /* First find the type of the merged group. */ + if (into->type != from->type) { + if (from->type == XKB_ATOM_NONE) { + } + else if (into->type == XKB_ATOM_NONE) { + into->type = from->type; + } + else { + xkb_atom_t use = (clobber ? from->type : into->type); + xkb_atom_t ignore = (clobber ? into->type : from->type); - clobber = (from->merge != MERGE_AUGMENT); + if (report) + log_warn(info->keymap->ctx, + "Multiple definitions for group %d type of key %s; " + "Using %s, ignoring %s\n", + group + 1, LongKeyNameText(key_name), + xkb_atom_text(info->keymap->ctx, use), + xkb_atom_text(info->keymap->ctx, ignore)); - report = (verbosity > 9) || - (into->file_id == from->file_id && verbosity > 0); + into->type = use; + } + } + into->defined |= (from->defined & GROUP_FIELD_TYPE); - darray_init(resultSyms); + /* Now look at the levels. */ - if (into->numLevels[group] >= from->numLevels[group]) { - resultActs = into->acts[group]; - resultWidth = into->numLevels[group]; + if (darray_empty(from->levels)) { + InitGroupInfo(from); + return true; } - else { - resultActs = from->acts[group]; - resultWidth = from->numLevels[group]; - darray_resize(into->symsMapIndex[group], - from->numLevels[group]); - darray_resize0(into->symsMapNumEntries[group], - from->numLevels[group]); - for (i = into->numLevels[group]; i < from->numLevels[group]; i++) - darray_item(into->symsMapIndex[group], i) = -1; + if (darray_empty(into->levels)) { + from->type = into->type; + *into = *from; + InitGroupInfo(from); + return true; } - if (darray_empty(resultActs) && (!darray_empty(into->acts[group]) || - !darray_empty(from->acts[group]))) { - darray_resize0(resultActs, resultWidth); - for (i = 0; i < resultWidth; i++) { - union xkb_action *fromAct = NULL, *toAct = NULL; - - if (!darray_empty(from->acts[group])) - fromAct = &darray_item(from->acts[group], i); + /* First we merge the actions and ensure @into has all the levels. */ + numLevels = MAX(darray_size(into->levels), darray_size(from->levels)); + for (i = 0; i < numLevels; i++) { + union xkb_action *intoAct, *fromAct; - if (!darray_empty(into->acts[group])) - toAct = &darray_item(into->acts[group], i); + if (i >= darray_size(from->levels)) + continue; - if (((fromAct == NULL) || (fromAct->type == XkbSA_NoAction)) - && (toAct != NULL)) { - darray_item(resultActs, i) = *toAct; - } - else if (((toAct == NULL) || (toAct->type == XkbSA_NoAction)) - && (fromAct != NULL)) { - darray_item(resultActs, i) = *fromAct; - } - else { - union xkb_action *use, *ignore; - if (clobber) { - use = fromAct; - ignore = toAct; - } - else { - use = toAct; - ignore = fromAct; - } - if (report) - log_warn(info->keymap->ctx, - "Multiple actions for level %d/group %u on key %s; " - "Using %s, ignoring %s\n", - i + 1, group + 1, longText(into->name), - ActionTypeText(use->type), - ActionTypeText(ignore->type)); - if (use) - darray_item(resultActs, i) = *use; - } + if (i >= darray_size(into->levels)) { + darray_append(into->levels, darray_item(from->levels, i)); + darray_item(into->levels, i).num_syms = 0; + darray_item(into->levels, i).sym_index = 0; + continue; } - } - - for (i = 0; i < resultWidth; i++) { - unsigned int fromSize = 0; - unsigned toSize = 0; - - if (!darray_empty(from->symsMapNumEntries[group]) && - i < from->numLevels[group]) - fromSize = darray_item(from->symsMapNumEntries[group], i); - if (!darray_empty(into->symsMapNumEntries[group]) && - i < into->numLevels[group]) - toSize = darray_item(into->symsMapNumEntries[group], i); + intoAct = &darray_item(into->levels, i).act; + fromAct = &darray_item(from->levels, i).act; - if (fromSize == 0) { - resultSize += toSize; - using |= TO; + if (fromAct->type == ACTION_TYPE_NONE) { } - else if (toSize == 0 || clobber) { - resultSize += fromSize; - using |= FROM; + else if (intoAct->type == ACTION_TYPE_NONE) { + *intoAct = *fromAct; } else { - resultSize += toSize; - using |= TO; - } - } + union xkb_action *use = (clobber ? fromAct : intoAct); + union xkb_action *ignore = (clobber ? intoAct : fromAct); - if (resultSize == 0) - goto out; + if (report) + log_warn(info->keymap->ctx, + "Multiple actions for level %d/group %u on key %s; " + "Using %s, ignoring %s\n", + i + 1, group + 1, LongKeyNameText(key_name), + ActionTypeText(use->type), + ActionTypeText(ignore->type)); - if (using == FROM) { - resultSyms = from->syms[group]; - darray_free(into->symsMapNumEntries[group]); - darray_free(into->symsMapIndex[group]); - into->symsMapNumEntries[group] = from->symsMapNumEntries[group]; - into->symsMapIndex[group] = from->symsMapIndex[group]; - darray_init(from->symsMapNumEntries[group]); - darray_init(from->symsMapIndex[group]); - goto out; - } - else if (using == TO) { - resultSyms = into->syms[group]; - goto out; + *intoAct = *use; + } } + into->defined |= (from->defined & GROUP_FIELD_ACTS); - darray_resize0(resultSyms, resultSize); + /* Then merge the keysyms. */ - for (i = 0; i < resultWidth; i++) { - enum key_group_selector use = NONE; - unsigned int fromSize = 0; - unsigned int toSize = 0; + /* + * We want to avoid copying and allocating if not necessary. So + * here we do a pre-scan of the levels to check if we'll only use + * @into's or @from's keysyms, and if so we'll just assign them. + * However if one level uses @into's and another uses @from's, we + * will need to construct a new syms array. + */ + using = 0; + for (i = 0; i < numLevels; i++) { + unsigned int intoSize, fromSize; - if (i < from->numLevels[group]) - fromSize = darray_item(from->symsMapNumEntries[group], i); + intoSize = darray_item(into->levels, i).num_syms; + if (i < darray_size(from->levels)) + fromSize = darray_item(from->levels, i).num_syms; + else + fromSize = 0; - if (i < into->numLevels[group]) - toSize = darray_item(into->symsMapNumEntries[group], i); + if (intoSize == 0 && fromSize == 0) + using |= 0; + else if (intoSize == 0) + using |= FROM; + else if (fromSize == 0) + using |= INTO; + else + using |= (clobber ? FROM : INTO); + } - if (fromSize == 0 && toSize == 0) { - darray_item(into->symsMapIndex[group], i) = -1; - darray_item(into->symsMapNumEntries[group], i) = 0; - continue; + if (using == 0 || using == INTO) { + } + else if (using == FROM) { + darray_free(into->syms); + into->syms = from->syms; + darray_init(from->syms); + for (i = 0; i < darray_size(from->levels); i++) { + darray_item(into->levels, i).num_syms = + darray_item(from->levels, i).num_syms; + darray_item(into->levels, i).sym_index = + darray_item(from->levels, i).sym_index; } + } + else { + darray_xkb_keysym_t syms = darray_new(); - if (fromSize == 0) - use = TO; - else if (toSize == 0 || clobber) - use = FROM; - else - use = TO; - - if (toSize && fromSize && report) { - log_info(info->keymap->ctx, - "Multiple symbols for group %u, level %d on key %s; " - "Using %s, ignoring %s\n", - group + 1, i + 1, longText(into->name), - (use == FROM ? "from" : "to"), - (use == FROM ? "to" : "from")); - } + for (i = 0; i < numLevels; i++) { + unsigned int intoSize, fromSize; - if (use == FROM) { - memcpy(darray_mem(resultSyms, cur_idx), - darray_mem(from->syms[group], - darray_item(from->symsMapIndex[group], i)), - darray_item(from->symsMapNumEntries[group], - i) * sizeof(xkb_keysym_t)); - darray_item(into->symsMapIndex[group], i) = cur_idx; - darray_item(into->symsMapNumEntries[group], i) = - darray_item(from->symsMapNumEntries[group], i); - } - else { - memcpy(darray_mem(resultSyms, cur_idx), - darray_mem(into->syms[group], - darray_item(into->symsMapIndex[group], i)), - darray_item(into->symsMapNumEntries[group], - i) * sizeof(xkb_keysym_t)); - darray_item(into->symsMapIndex[group], i) = cur_idx; + intoSize = darray_item(into->levels, i).num_syms; + if (i < darray_size(from->levels)) + fromSize = darray_item(from->levels, i).num_syms; + else + fromSize = 0; + + /* Empty level. */ + if (intoSize == 0 && fromSize == 0) + continue; + + if (intoSize != 0 && fromSize != 0 && report) + log_info(info->keymap->ctx, + "Multiple symbols for group %u, level %d on key %s; " + "Using %s, ignoring %s\n", + group + 1, i + 1, LongKeyNameText(key_name), + (clobber ? "from" : "to"), + (clobber ? "to" : "from")); + + if (intoSize == 0 || (fromSize != 0 && clobber)) { + unsigned sym_index = darray_item(from->levels, i).sym_index; + darray_item(into->levels, i).sym_index = darray_size(syms); + darray_item(into->levels, i).num_syms = fromSize; + darray_append_items(syms, &darray_item(from->syms, sym_index), + fromSize); + } + else + { + unsigned sym_index = darray_item(into->levels, i).sym_index; + darray_item(into->levels, i).sym_index = darray_size(syms); + darray_item(into->levels, i).num_syms = intoSize; + darray_append_items(syms, &darray_item(into->syms, sym_index), + intoSize); + } } - cur_idx += darray_item(into->symsMapNumEntries[group], i); - } - -out: - if (!darray_same(resultActs, into->acts[group])) - darray_free(into->acts[group]); - if (!darray_same(resultActs, from->acts[group])) - darray_free(from->acts[group]); - into->numLevels[group] = resultWidth; - if (!darray_same(resultSyms, into->syms[group])) - darray_free(into->syms[group]); - into->syms[group] = resultSyms; - if (!darray_same(resultSyms, from->syms[group])) - darray_free(from->syms[group]); - darray_init(from->syms[group]); - darray_free(from->symsMapIndex[group]); - darray_free(from->symsMapNumEntries[group]); - into->acts[group] = resultActs; - darray_init(from->acts[group]); - if (!darray_empty(into->syms[group])) - into->symsDefined |= (1 << group); - from->symsDefined &= ~(1 << group); - into->actsDefined |= (1 << group); - from->actsDefined &= ~(1 << group); + darray_free(into->syms); + into->syms = syms; + } + into->defined |= (from->defined & GROUP_FIELD_SYMS); return true; } static bool -UseNewKeyField(unsigned field, KeyInfo *old, KeyInfo *new, int verbosity, - unsigned *collide) +UseNewKeyField(enum key_field field, enum key_field old, enum key_field new, + bool clobber, bool report, enum key_field *collide) { - if (!(old->defined & field)) - return true; + if (!(old & field)) + return (new & field); - if (new->defined & field) { - if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9) + if (new & field) { + if (report) *collide |= field; - if (new->merge != MERGE_AUGMENT) + if (clobber) return true; } @@ -509,113 +394,56 @@ static bool MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from) { xkb_group_index_t i; - unsigned collide = 0; - bool report; + enum key_field collide = 0; + bool clobber, report; int verbosity = xkb_get_log_verbosity(info->keymap->ctx); if (from->merge == MERGE_REPLACE) { - for (i = 0; i < XkbNumKbdGroups; i++) { - if (into->numLevels[i] != 0) { - darray_free(into->syms[i]); - darray_free(into->acts[i]); - } - } + ClearKeyInfo(into); *into = *from; - memset(from, 0, sizeof(KeyInfo)); + InitKeyInfo(from, info->file_id); return true; } + clobber = (from->merge != MERGE_AUGMENT); report = (verbosity > 9 || (into->file_id == from->file_id && verbosity > 0)); - for (i = 0; i < XkbNumKbdGroups; i++) { - if (from->numLevels[i] > 0) { - if (into->numLevels[i] == 0) { - into->numLevels[i] = from->numLevels[i]; - into->syms[i] = from->syms[i]; - into->symsMapIndex[i] = from->symsMapIndex[i]; - into->symsMapNumEntries[i] = from->symsMapNumEntries[i]; - into->acts[i] = from->acts[i]; - into->symsDefined |= (1 << i); - darray_init(from->syms[i]); - darray_init(from->symsMapIndex[i]); - darray_init(from->symsMapNumEntries[i]); - darray_init(from->acts[i]); - from->numLevels[i] = 0; - from->symsDefined &= ~(1 << i); - if (!darray_empty(into->syms[i])) - into->defined |= _Key_Syms; - if (!darray_empty(into->acts[i])) - into->defined |= _Key_Acts; - } - else { - if (report) { - if (!darray_empty(into->syms[i])) - collide |= _Key_Syms; - if (!darray_empty(into->acts[i])) - collide |= _Key_Acts; - } - MergeKeyGroups(info, into, from, (unsigned) i); - } - } - if (from->types[i] != XKB_ATOM_NONE) { - if ((into->types[i] != XKB_ATOM_NONE) && report && - (into->types[i] != from->types[i])) { - xkb_atom_t use, ignore; - collide |= _Key_Types; - if (from->merge != MERGE_AUGMENT) { - use = from->types[i]; - ignore = into->types[i]; - } - else { - use = into->types[i]; - ignore = from->types[i]; - } - - log_warn(info->keymap->ctx, - "Multiple definitions for group %d type of key %s; " - "Using %s, ignoring %s\n", - i, longText(into->name), - xkb_atom_text(info->keymap->ctx, use), - xkb_atom_text(info->keymap->ctx, ignore)); - } - - if (from->merge != MERGE_AUGMENT || - into->types[i] == XKB_ATOM_NONE) { - into->types[i] = from->types[i]; - } - } - } + for (i = 0; i < XKB_NUM_GROUPS; i++) + MergeGroups(info, &into->groups[i], &from->groups[i], clobber, + report, i, into->name); - if (UseNewKeyField(_Key_Behavior, into, from, verbosity, &collide)) { - into->behavior = from->behavior; - into->defined |= _Key_Behavior; - } - if (UseNewKeyField(_Key_VModMap, into, from, verbosity, &collide)) { + if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined, + clobber, report, &collide)) { into->vmodmap = from->vmodmap; - into->defined |= _Key_VModMap; + into->defined |= KEY_FIELD_VMODMAP; } - if (UseNewKeyField(_Key_Repeat, into, from, verbosity, &collide)) { + if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined, + clobber, report, &collide)) { into->repeat = from->repeat; - into->defined |= _Key_Repeat; + into->defined |= KEY_FIELD_REPEAT; } - if (UseNewKeyField(_Key_Type_Dflt, into, from, verbosity, &collide)) { + if (UseNewKeyField(KEY_FIELD_TYPE_DFLT, into->defined, from->defined, + clobber, report, &collide)) { into->dfltType = from->dfltType; - into->defined |= _Key_Type_Dflt; + into->defined |= KEY_FIELD_TYPE_DFLT; } - if (UseNewKeyField(_Key_GroupInfo, into, from, verbosity, &collide)) { + if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined, + clobber, report, &collide)) { into->out_of_range_group_action = from->out_of_range_group_action; into->out_of_range_group_number = from->out_of_range_group_number; - into->defined |= _Key_GroupInfo; + into->defined |= KEY_FIELD_GROUPINFO; } if (collide) log_warn(info->keymap->ctx, "Symbol map for key %s redefined; " "Using %s definition for conflicting fields\n", - longText(into->name), - (from->merge == MERGE_AUGMENT ? "first" : "last")); + LongKeyNameText(into->name), + (clobber ? "first" : "last")); + ClearKeyInfo(from); + InitKeyInfo(from, info->file_id); return true; } @@ -623,20 +451,24 @@ static bool AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi) { unsigned long real_name; - KeyInfo *iter, *new; + KeyInfo *iter; + + /* + * Don't keep aliases in the keys array; this guarantees that + * searching for keys to merge with by straight comparison (see the + * following loop) is enough, and we won't get multiple KeyInfo's + * for the same key because of aliases. + */ + if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name)) + keyi->name = real_name; darray_foreach(iter, info->keys) if (iter->name == keyi->name) return MergeKeys(info, iter, keyi); - if (FindKeyNameForAlias(info->keymap, keyi->name, &real_name)) - darray_foreach(iter, info->keys) - if (iter->name == real_name) - return MergeKeys(info, iter, keyi); - - darray_resize0(info->keys, darray_size(info->keys) + 1); - new = &darray_item(info->keys, darray_size(info->keys) - 1); - return CopyKeyInfo(keyi, new, true); + darray_append(info->keys, *keyi); + InitKeyInfo(keyi, info->file_id); + return true; } static bool @@ -646,7 +478,7 @@ AddModMapEntry(SymbolsInfo * info, ModMapEntry * new) bool clobber; clobber = (new->merge != MERGE_AUGMENT); - list_foreach(mm, &info->modMaps, entry) { + darray_foreach(mm, info->modMaps) { if (new->haveSymbol && mm->haveSymbol && (new->u.keySym == mm->u.keySym)) { unsigned use, ignore; @@ -683,7 +515,7 @@ AddModMapEntry(SymbolsInfo * info, ModMapEntry * new) log_err(info->keymap->ctx, "Key %s added to map for multiple modifiers; " "Using %s, ignoring %s.\n", - longText(new->u.keyName), ModIndexText(use), + LongKeyNameText(new->u.keyName), ModIndexText(use), ModIndexText(ignore)); mm->modifier = use; } @@ -691,17 +523,7 @@ AddModMapEntry(SymbolsInfo * info, ModMapEntry * new) } } - mm = malloc(sizeof(*mm)); - if (!mm) { - log_wsgo(info->keymap->ctx, - "Could not allocate modifier map entry; " - "Modifier map for %s will be incomplete\n", - ModIndexText(new->modifier)); - return false; - } - - *mm = *new; - list_add(&mm->entry, &info->modMaps); + darray_append(info->modMaps, *new); return true; } @@ -713,7 +535,7 @@ MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from, { unsigned int i; KeyInfo *keyi; - ModMapEntry *mm, *next; + ModMapEntry *mm; if (from->errorCount > 0) { into->errorCount += from->errorCount; @@ -723,7 +545,7 @@ MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from, into->name = from->name; from->name = NULL; } - for (i = 0; i < XkbNumKbdGroups; i++) { + for (i = 0; i < XKB_NUM_GROUPS; i++) { if (from->groupNames[i] != XKB_ATOM_NONE) { if ((merge != MERGE_AUGMENT) || (into->groupNames[i] == XKB_ATOM_NONE)) @@ -732,21 +554,16 @@ MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from, } darray_foreach(keyi, from->keys) { - if (merge != MERGE_DEFAULT) - keyi->merge = merge; - + merge = (merge == MERGE_DEFAULT ? keyi->merge : merge); if (!AddKeySymbols(into, keyi)) into->errorCount++; } - list_foreach_safe(mm, next, &from->modMaps, entry) { - if (merge != MERGE_DEFAULT) - mm->merge = merge; + darray_foreach(mm, from->modMaps) { + mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge); if (!AddModMapEntry(into, mm)) into->errorCount++; - free(mm); } - list_init(&from->modMaps); } static void @@ -759,7 +576,7 @@ HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt) XkbFile *rtrn; SymbolsInfo included, next_incl; - InitSymbolsInfo(&included, info->keymap, info->file_id); + InitSymbolsInfo(&included, info->keymap, info->file_id, info->actions); if (stmt->stmt) { free(included.name); included.name = stmt->stmt; @@ -770,11 +587,11 @@ HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt) if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_SYMBOLS, &rtrn, &merge)) { info->errorCount += 10; - FreeSymbolsInfo(&included); + ClearSymbolsInfo(&included); return false; } - InitSymbolsInfo(&next_incl, info->keymap, rtrn->id); + InitSymbolsInfo(&next_incl, info->keymap, rtrn->id, info->actions); next_incl.merge = next_incl.dflt.merge = MERGE_OVERRIDE; if (stmt->modifier) next_incl.explicit_group = atoi(stmt->modifier) - 1; @@ -785,12 +602,12 @@ HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *stmt) MergeIncludedSymbols(&included, &next_incl, merge); - FreeSymbolsInfo(&next_incl); - FreeXKBFile(rtrn); + ClearSymbolsInfo(&next_incl); + FreeXkbFile(rtrn); } MergeIncludedSymbols(info, &included, merge); - FreeSymbolsInfo(&included); + ClearSymbolsInfo(&included); return (info->errorCount == 0); } @@ -802,129 +619,149 @@ static bool GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx, unsigned what, xkb_group_index_t *ndx_rtrn) { - const char *name; - ExprResult tmp; - - if (what == SYMBOLS) - name = "symbols"; - else - name = "actions"; + const char *name = (what == SYMBOLS ? "symbols" : "actions"); if (arrayNdx == NULL) { xkb_group_index_t i; - unsigned defined; - if (what == SYMBOLS) - defined = keyi->symsDefined; - else - defined = keyi->actsDefined; + enum group_field field = (what == SYMBOLS ? + GROUP_FIELD_SYMS : GROUP_FIELD_ACTS); - for (i = 0; i < XkbNumKbdGroups; i++) { - if ((defined & (1 << i)) == 0) { + for (i = 0; i < XKB_NUM_GROUPS; i++) { + if (!(keyi->groups[i].defined & field)) { *ndx_rtrn = i; return true; } } + log_err(info->keymap->ctx, "Too many groups of %s for key %s (max %u); " "Ignoring %s defined for extra groups\n", - name, longText(keyi->name), XkbNumKbdGroups + 1, name); + name, LongKeyNameText(keyi->name), XKB_NUM_GROUPS + 1, name); return false; } - if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &tmp)) { + + if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, ndx_rtrn)) { log_err(info->keymap->ctx, "Illegal group index for %s of key %s\n" "Definition with non-integer array index ignored\n", - name, longText(keyi->name)); + name, LongKeyNameText(keyi->name)); return false; } - *ndx_rtrn = tmp.uval - 1; + + (*ndx_rtrn)--; return true; } +bool +LookupKeysym(const char *str, xkb_keysym_t *sym_rtrn) +{ + xkb_keysym_t sym; + + if (!str || istreq(str, "any") || istreq(str, "nosymbol")) { + *sym_rtrn = XKB_KEY_NoSymbol; + return 1; + } + + if (istreq(str, "none") || istreq(str, "voidsymbol")) { + *sym_rtrn = XKB_KEY_VoidSymbol; + return 1; + } + + sym = xkb_keysym_from_name(str); + if (sym != XKB_KEY_NoSymbol) { + *sym_rtrn = sym; + return 1; + } + + return 0; +} + static bool AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx, ExprDef *value) { xkb_group_index_t ndx; - size_t nSyms, nLevels; - size_t i; + GroupInfo *groupi; + unsigned int nSyms; + xkb_level_index_t nLevels; + xkb_level_index_t i; int j; if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx)) return false; + + groupi = &keyi->groups[ndx]; + if (value == NULL) { - keyi->symsDefined |= (1 << ndx); + groupi->defined |= GROUP_FIELD_SYMS; return true; } + if (value->op != EXPR_KEYSYM_LIST) { log_err(info->keymap->ctx, "Expected a list of symbols, found %s; " "Ignoring symbols for group %u of %s\n", - exprOpText(value->op), ndx + 1, longText(keyi->name)); + expr_op_type_to_string(value->op), ndx + 1, + LongKeyNameText(keyi->name)); return false; } - if (!darray_empty(keyi->syms[ndx])) { + + if (groupi->defined & GROUP_FIELD_SYMS) { log_err(info->keymap->ctx, "Symbols for key %s, group %u already defined; " "Ignoring duplicate definition\n", - longText(keyi->name), ndx + 1); + LongKeyNameText(keyi->name), ndx + 1); return false; } + nSyms = darray_size(value->value.list.syms); nLevels = darray_size(value->value.list.symsMapIndex); - if ((keyi->numLevels[ndx] < nSyms || darray_empty(keyi->syms[ndx])) && - (!ResizeKeyGroup(keyi, ndx, nLevels, nSyms, false))) { - log_wsgo(info->keymap->ctx, - "Could not resize group %u of key %s to contain %zu levels; " - "Symbols lost\n", - ndx + 1, longText(keyi->name), nSyms); - return false; - } - keyi->symsDefined |= (1 << ndx); + + if (darray_size(groupi->syms) < nSyms) + darray_resize0(groupi->syms, nSyms); + + if (darray_size(groupi->levels) < nLevels) + darray_resize0(groupi->levels, nLevels); + + groupi->defined |= GROUP_FIELD_SYMS; + for (i = 0; i < nLevels; i++) { - darray_item(keyi->symsMapIndex[ndx], i) = - darray_item(value->value.list.symsMapIndex, i); - darray_item(keyi->symsMapNumEntries[ndx], i) = - darray_item(value->value.list.symsNumEntries, i); - - for (j = 0; j < darray_item(keyi->symsMapNumEntries[ndx], i); j++) { - /* FIXME: What's abort() doing here? */ - if (darray_item(keyi->symsMapIndex[ndx], i) + j >= nSyms) - abort(); + LevelInfo *leveli = &darray_item(groupi->levels, i); + + leveli->sym_index = darray_item(value->value.list.symsMapIndex, i); + leveli->num_syms = darray_item(value->value.list.symsNumEntries, i); + + for (j = 0; j < leveli->num_syms; j++) { if (!LookupKeysym(darray_item(value->value.list.syms, - darray_item(value->value.list.symsMapIndex, - i) + j), - &darray_item(keyi->syms[ndx], - darray_item(keyi->symsMapIndex[ndx], - i) + j))) { + leveli->sym_index + j), + &darray_item(groupi->syms, + leveli->sym_index + j))) { log_warn(info->keymap->ctx, - "Could not resolve keysym %s for key %s, group %u (%s), level %zu\n", + "Could not resolve keysym %s for key %s, group %u (%s), level %u\n", darray_item(value->value.list.syms, i), - longText(keyi->name), + LongKeyNameText(keyi->name), ndx + 1, xkb_atom_text(info->keymap->ctx, info->groupNames[ndx]), nSyms); - while (--j >= 0) - darray_item(keyi->syms[ndx], - darray_item(keyi->symsMapIndex[ndx], - i) + j) = XKB_KEY_NoSymbol; - darray_item(keyi->symsMapIndex[ndx], i) = -1; - darray_item(keyi->symsMapNumEntries[ndx], i) = 0; + leveli->sym_index = 0; + leveli->num_syms = 0; break; } - if (darray_item(keyi->symsMapNumEntries[ndx], i) == 1 && - darray_item(keyi->syms[ndx], - darray_item(keyi->symsMapIndex[ndx], - i) + j) == XKB_KEY_NoSymbol) { - darray_item(keyi->symsMapIndex[ndx], i) = -1; - darray_item(keyi->symsMapNumEntries[ndx], i) = 0; + + if (leveli->num_syms == 1 && + darray_item(groupi->syms, + leveli->sym_index + j) == XKB_KEY_NoSymbol) { + leveli->sym_index = 0; + leveli->num_syms = 0; } } } - for (j = keyi->numLevels[ndx] - 1; - j >= 0 && darray_item(keyi->symsMapNumEntries[ndx], j) == 0; j--) - keyi->numLevels[ndx]--; + + for (j = darray_size(groupi->levels) - 1; + j >= 0 && darray_item(groupi->levels, j).num_syms == 0; j--) + (void) darray_pop(groupi->levels); + return true; } @@ -932,17 +769,20 @@ static bool AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx, ExprDef *value) { - size_t i; + unsigned int i; xkb_group_index_t ndx; - size_t nActs; + GroupInfo *groupi; + unsigned int nActs; ExprDef *act; - struct xkb_any_action *toAct; + union xkb_action *toAct; if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx)) return false; + groupi = &keyi->groups[ndx]; + if (value == NULL) { - keyi->actsDefined |= (1 << ndx); + groupi->defined |= GROUP_FIELD_ACTS; return true; } @@ -950,71 +790,50 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx, log_wsgo(info->keymap->ctx, "Bad expression type (%d) for action list value; " "Ignoring actions for group %u of %s\n", - value->op, ndx, longText(keyi->name)); + value->op, ndx, LongKeyNameText(keyi->name)); return false; } - if (!darray_empty(keyi->acts[ndx])) { + if (groupi->defined & GROUP_FIELD_ACTS) { log_wsgo(info->keymap->ctx, "Actions for key %s, group %u already defined\n", - longText(keyi->name), ndx); + LongKeyNameText(keyi->name), ndx); return false; } - for (nActs = 0, act = value->value.child; act != NULL; nActs++) { - act = (ExprDef *) act->common.next; - } + nActs = 0; + for (act = value->value.child; act; act = (ExprDef *) act->common.next) + nActs++; - if (nActs < 1) { - log_wsgo(info->keymap->ctx, - "Action list but not actions in AddActionsToKey\n"); - return false; - } + if (darray_size(groupi->levels) < nActs) + darray_resize0(groupi->levels, nActs); - if ((keyi->numLevels[ndx] < nActs || darray_empty(keyi->acts[ndx])) && - !ResizeKeyGroup(keyi, ndx, nActs, nActs, true)) { - log_wsgo(info->keymap->ctx, - "Could not resize group %u of key %s; " - "Actions lost\n", - ndx, longText(keyi->name)); - return false; - } - keyi->actsDefined |= (1 << ndx); + groupi->defined |= GROUP_FIELD_ACTS; - toAct = (struct xkb_any_action *) darray_mem(keyi->acts[ndx], 0); act = value->value.child; - for (i = 0; i < nActs; i++, toAct++) { - if (!HandleActionDef(act, info->keymap, toAct, info->action)) { + for (i = 0; i < nActs; i++) { + toAct = &darray_item(groupi->levels, i).act; + + if (!HandleActionDef(act, info->keymap, toAct, info->actions)) log_err(info->keymap->ctx, "Illegal action definition for %s; " - "Action for group %u/level %zu ignored\n", - longText(keyi->name), ndx + 1, i + 1); - } + "Action for group %u/level %u ignored\n", + LongKeyNameText(keyi->name), ndx + 1, i + 1); + act = (ExprDef *) act->common.next; } return true; } -static const LookupEntry lockingEntries[] = { - { "true", XkbKB_Lock }, - { "yes", XkbKB_Lock }, - { "on", XkbKB_Lock }, - { "false", XkbKB_Default }, - { "no", XkbKB_Default }, - { "off", XkbKB_Default }, - { "permanent", XkbKB_Lock | XkbKB_Permanent }, - { NULL, 0 } -}; - static const LookupEntry repeatEntries[] = { - { "true", RepeatYes }, - { "yes", RepeatYes }, - { "on", RepeatYes }, - { "false", RepeatNo }, - { "no", RepeatNo }, - { "off", RepeatNo }, - { "default", RepeatUndefined }, + { "true", KEY_REPEAT_YES }, + { "yes", KEY_REPEAT_YES }, + { "on", KEY_REPEAT_YES }, + { "false", KEY_REPEAT_NO }, + { "no", KEY_REPEAT_NO }, + { "off", KEY_REPEAT_NO }, + { "default", KEY_REPEAT_UNDEFINED }, { NULL, 0 } }; @@ -1023,32 +842,32 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field, ExprDef *arrayNdx, ExprDef *value) { bool ok = true; - ExprResult tmp; struct xkb_context *ctx = info->keymap->ctx; if (istreq(field, "type")) { - ExprResult ndx; - const char *str; + xkb_group_index_t ndx; + xkb_atom_t val; - if (!ExprResolveString(ctx, value, &str)) - log_lvl(info->keymap->ctx, 1, + if (!ExprResolveString(ctx, value, &val)) + log_vrb(ctx, 1, "The type field of a key symbol map must be a string; " "Ignoring illegal type definition\n"); if (arrayNdx == NULL) { - keyi->dfltType = xkb_atom_intern(ctx, str); - keyi->defined |= _Key_Type_Dflt; + keyi->dfltType = val; + keyi->defined |= KEY_FIELD_TYPE_DFLT; } else if (!ExprResolveGroup(ctx, arrayNdx, &ndx)) { - log_err(info->keymap->ctx, + log_err(ctx, "Illegal group index for type of key %s; " "Definition with non-integer array index ignored\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); return false; } else { - keyi->types[ndx.uval - 1] = xkb_atom_intern(ctx, str); - keyi->typesDefined |= (1 << (ndx.uval - 1)); + ndx--; + keyi->groups[ndx].type = val; + keyi->groups[ndx].defined |= GROUP_FIELD_TYPE; } } else if (istreq(field, "symbols")) @@ -1058,25 +877,28 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field, else if (istreq(field, "vmods") || istreq(field, "virtualmods") || istreq(field, "virtualmodifiers")) { - ok = ExprResolveVModMask(info->keymap, value, &tmp); + xkb_mod_mask_t mask; + + ok = ExprResolveVModMask(info->keymap, value, &mask); if (ok) { - keyi->vmodmap = (tmp.uval >> 8); - keyi->defined |= _Key_VModMap; + keyi->vmodmap = (mask >> XKB_NUM_CORE_MODS) & 0xffff; + keyi->defined |= KEY_FIELD_VMODMAP; } else { log_err(info->keymap->ctx, "Expected a virtual modifier mask, found %s; " "Ignoring virtual modifiers definition for key %s\n", - exprOpText(value->op), longText(keyi->name)); + expr_op_type_to_string(value->op), + LongKeyNameText(keyi->name)); } } else if (istreq(field, "locking") || istreq(field, "lock") || istreq(field, "locks")) { - ok = ExprResolveEnum(ctx, value, &tmp, lockingEntries); - if (ok) - keyi->behavior.type = tmp.uval; - keyi->defined |= _Key_Behavior; + log_err(info->keymap->ctx, + "Key behaviors not supported; " + "Ignoring locking specification for key %s\n", + LongKeyNameText(keyi->name)); } else if (istreq(field, "radiogroup") || istreq(field, "permanentradiogroup") || @@ -1084,72 +906,84 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field, log_err(info->keymap->ctx, "Radio groups not supported; " "Ignoring radio group specification for key %s\n", - longText(keyi->name)); - return false; + LongKeyNameText(keyi->name)); } else if (istreq_prefix("overlay", field) || istreq_prefix("permanentoverlay", field)) { log_err(info->keymap->ctx, "Overlays not supported; " "Ignoring overlay specification for key %s\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); } else if (istreq(field, "repeating") || istreq(field, "repeats") || istreq(field, "repeat")) { - ok = ExprResolveEnum(ctx, value, &tmp, repeatEntries); + unsigned int val; + + ok = ExprResolveEnum(ctx, value, &val, repeatEntries); if (!ok) { log_err(info->keymap->ctx, "Illegal repeat setting for %s; " "Non-boolean repeat setting ignored\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); return false; } - keyi->repeat = tmp.uval; - keyi->defined |= _Key_Repeat; + keyi->repeat = val; + keyi->defined |= KEY_FIELD_REPEAT; } else if (istreq(field, "groupswrap") || istreq(field, "wrapgroups")) { - if (!ExprResolveBoolean(ctx, value, &tmp)) { + bool set; + + if (!ExprResolveBoolean(ctx, value, &set)) { log_err(info->keymap->ctx, "Illegal groupsWrap setting for %s; " "Non-boolean value ignored\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); return false; } - if (tmp.uval) - keyi->out_of_range_group_action = XkbWrapIntoRange; + + if (set) + keyi->out_of_range_group_action = RANGE_WRAP; else - keyi->out_of_range_group_action = XkbClampIntoRange; - keyi->defined |= _Key_GroupInfo; + keyi->out_of_range_group_action = RANGE_SATURATE; + + keyi->defined |= KEY_FIELD_GROUPINFO; } else if (istreq(field, "groupsclamp") || istreq(field, "clampgroups")) { - if (!ExprResolveBoolean(ctx, value, &tmp)) { + bool set; + + if (!ExprResolveBoolean(ctx, value, &set)) { log_err(info->keymap->ctx, "Illegal groupsClamp setting for %s; " "Non-boolean value ignored\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); return false; } - if (tmp.uval) - keyi->out_of_range_group_action = XkbClampIntoRange; + + if (set) + keyi->out_of_range_group_action = RANGE_SATURATE; else - keyi->out_of_range_group_action = XkbWrapIntoRange; - keyi->defined |= _Key_GroupInfo; + keyi->out_of_range_group_action = RANGE_WRAP; + + keyi->defined |= KEY_FIELD_GROUPINFO; } else if (istreq(field, "groupsredirect") || istreq(field, "redirectgroups")) { - if (!ExprResolveGroup(ctx, value, &tmp)) { + xkb_group_index_t grp; + + if (!ExprResolveGroup(ctx, value, &grp)) { log_err(info->keymap->ctx, "Illegal group index for redirect of key %s; " "Definition with non-integer group ignored\n", - longText(keyi->name)); + LongKeyNameText(keyi->name)); return false; } - keyi->out_of_range_group_action = XkbRedirectIntoRange; - keyi->out_of_range_group_number = tmp.uval - 1; - keyi->defined |= _Key_GroupInfo; + + keyi->out_of_range_group_action = RANGE_REDIRECT; + keyi->out_of_range_group_number = grp - 1; + keyi->defined |= KEY_FIELD_GROUPINFO; } else { log_err(info->keymap->ctx, @@ -1158,23 +992,24 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field, field); ok = false; } + return ok; } static int SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value) { - ExprResult tmp; - const char *name; + xkb_group_index_t grp; + xkb_atom_t name; if (!arrayNdx) { - log_lvl(info->keymap->ctx, 1, + log_vrb(info->keymap->ctx, 1, "You must specify an index when specifying a group name; " "Group name definition without array subscript ignored\n"); return false; } - if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &tmp)) { + if (!ExprResolveGroup(info->keymap->ctx, arrayNdx, &grp)) { log_err(info->keymap->ctx, "Illegal index in group name definition; " "Definition with non-integer array index ignored\n"); @@ -1184,13 +1019,11 @@ SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value) if (!ExprResolveString(info->keymap->ctx, value, &name)) { log_err(info->keymap->ctx, "Group name must be a string; " - "Illegal name for group %d ignored\n", tmp.uval); + "Illegal name for group %d ignored\n", grp); return false; } - info->groupNames[tmp.uval - 1 + info->explicit_group] = - xkb_atom_intern(info->keymap->ctx, name); - + info->groupNames[grp - 1 + info->explicit_group] = name; return true; } @@ -1237,8 +1070,8 @@ HandleSymbolsVar(SymbolsInfo *info, VarDef *stmt) ret = true; } else { - ret = SetActionField(info->keymap, elem, field, arrayNdx, - stmt->value, &info->action); + ret = SetActionField(info->keymap, elem, field, arrayNdx, stmt->value, + info->actions); } return ret; @@ -1279,41 +1112,27 @@ HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi) static bool SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi) { - xkb_group_index_t group = info->explicit_group; + xkb_group_index_t i; - if (group == 0) + if (info->explicit_group == 0) return true; - if ((keyi->typesDefined | keyi->symsDefined | keyi->actsDefined) & ~1) { - xkb_group_index_t i; - log_warn(info->keymap->ctx, - "For the map %s an explicit group specified\n", - info->name); - log_warn(info->keymap->ctx, - "but key %s has more than one group defined; " - "All groups except first one will be ignored\n", - longText(keyi->name)); - for (i = 1; i < XkbNumKbdGroups; i++) { - keyi->numLevels[i] = 0; - darray_free(keyi->syms[i]); - darray_free(keyi->acts[i]); - keyi->types[i] = 0; + for (i = 1; i < XKB_NUM_GROUPS; i++) { + if (keyi->groups[i].defined) { + log_warn(info->keymap->ctx, + "For the map %s an explicit group specified, " + "but key %s has more than one group defined; " + "All groups except first one will be ignored\n", + info->name, LongKeyNameText(keyi->name)); + break; } } - keyi->typesDefined = keyi->symsDefined = keyi->actsDefined = 1 << group; - - keyi->numLevels[group] = keyi->numLevels[0]; - keyi->numLevels[0] = 0; - keyi->syms[group] = keyi->syms[0]; - darray_init(keyi->syms[0]); - keyi->symsMapIndex[group] = keyi->symsMapIndex[0]; - darray_init(keyi->symsMapIndex[0]); - keyi->symsMapNumEntries[group] = keyi->symsMapNumEntries[0]; - darray_init(keyi->symsMapNumEntries[0]); - keyi->acts[group] = keyi->acts[0]; - darray_init(keyi->acts[0]); - keyi->types[group] = keyi->types[0]; - keyi->types[0] = 0; + if (i < XKB_NUM_GROUPS) + for (i = 1; i < XKB_NUM_GROUPS; i++) + ClearGroupInfo(&keyi->groups[i]); + + keyi->groups[info->explicit_group] = keyi->groups[0]; + InitGroupInfo(&keyi->groups[0]); return true; } @@ -1321,11 +1140,16 @@ static int HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt) { KeyInfo keyi; + xkb_group_index_t i; - InitKeyInfo(&keyi, info->file_id); - CopyKeyInfo(&info->dflt, &keyi, false); + keyi = info->dflt; + for (i = 0; i < XKB_NUM_GROUPS; i++) { + darray_copy(keyi.groups[i].syms, info->dflt.groups[i].syms); + darray_copy(keyi.groups[i].levels, info->dflt.groups[i].levels); + } keyi.merge = stmt->merge; keyi.name = KeyNameToLong(stmt->keyName); + if (!HandleSymbolsBody(info, (VarDef *) stmt->symbols, &keyi)) { info->errorCount++; return false; @@ -1340,6 +1164,7 @@ HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt) info->errorCount++; return false; } + return true; } @@ -1348,27 +1173,31 @@ HandleModMapDef(SymbolsInfo *info, ModMapDef *def) { ExprDef *key; ModMapEntry tmp; - ExprResult rtrn; + xkb_mod_index_t ndx; bool ok; struct xkb_context *ctx = info->keymap->ctx; - if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &rtrn)) { + if (!LookupModIndex(ctx, NULL, def->modifier, EXPR_TYPE_INT, &ndx)) { log_err(info->keymap->ctx, "Illegal modifier map definition; " "Ignoring map for non-modifier \"%s\"\n", xkb_atom_text(ctx, def->modifier)); return false; } + ok = true; - tmp.modifier = rtrn.uval; + tmp.modifier = ndx; + for (key = def->keys; key != NULL; key = (ExprDef *) key->common.next) { + xkb_keysym_t sym; + if (key->op == EXPR_VALUE && key->value_type == EXPR_TYPE_KEYNAME) { tmp.haveSymbol = false; tmp.u.keyName = KeyNameToLong(key->value.keyName); } - else if (ExprResolveKeySym(ctx, key, &rtrn)) { + else if (ExprResolveKeySym(ctx, key, &sym)) { tmp.haveSymbol = true; - tmp.u.keySym = rtrn.uval; + tmp.u.keySym = sym; } else { log_err(info->keymap->ctx, @@ -1386,54 +1215,42 @@ HandleModMapDef(SymbolsInfo *info, ModMapDef *def) static void HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge) { + bool ok; ParseCommon *stmt; free(info->name); info->name = strdup_safe(file->name); + stmt = file->defs; - while (stmt) - { + for (stmt = file->defs; stmt; stmt = stmt->next) { switch (stmt->type) { case STMT_INCLUDE: - if (!HandleIncludeSymbols(info, (IncludeStmt *) stmt)) - info->errorCount++; + ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt); break; case STMT_SYMBOLS: - if (!HandleSymbolsDef(info, (SymbolsDef *) stmt)) - info->errorCount++; + ok = HandleSymbolsDef(info, (SymbolsDef *) stmt); break; case STMT_VAR: - if (!HandleSymbolsVar(info, (VarDef *) stmt)) - info->errorCount++; + ok = HandleSymbolsVar(info, (VarDef *) stmt); break; case STMT_VMOD: - if (!HandleVModDef((VModDef *) stmt, info->keymap, merge, - &info->vmods)) - info->errorCount++; - break; - case STMT_INTERP: - log_err(info->keymap->ctx, - "Interpretation files may not include other types; " - "Ignoring definition of symbol interpretation\n"); - info->errorCount++; - break; - case STMT_KEYCODE: - log_err(info->keymap->ctx, - "Interpretation files may not include other types; " - "Ignoring definition of key name\n"); - info->errorCount++; + ok = HandleVModDef((VModDef *) stmt, info->keymap, merge, + &info->vmods); break; case STMT_MODMAP: - if (!HandleModMapDef(info, (ModMapDef *) stmt)) - info->errorCount++; + ok = HandleModMapDef(info, (ModMapDef *) stmt); break; default: - log_wsgo(info->keymap->ctx, - "Unexpected statement type %d in HandleSymbolsFile\n", - stmt->type); + log_err(info->keymap->ctx, + "Interpretation files may not include other types; " + "Ignoring %s\n", stmt_type_to_string(stmt->type)); + ok = false; break; } - stmt = stmt->next; + + if (!ok) + info->errorCount++; + if (info->errorCount > 10) { log_err(info->keymap->ctx, "Abandoning symbols file \"%s\"\n", file->topName); @@ -1456,8 +1273,8 @@ static struct xkb_key * FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym) { struct xkb_key *key, *ret = NULL; - xkb_group_index_t group, min_group = UINT_MAX; - unsigned int level, min_level = UINT_MAX; + xkb_group_index_t group, min_group = UINT32_MAX; + xkb_level_index_t level, min_level = UINT16_MAX; xkb_foreach_key(key, keymap) { for (group = 0; group < key->num_groups; group++) { @@ -1490,30 +1307,18 @@ FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym) return ret; } -/** - * Find the given name in the keymap->map->types and return its index. - * - * @param atom The atom to search for. - * @param type_rtrn Set to the index of the name if found. - * - * @return true if found, false otherwise. - */ static bool -FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn) +FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn) { - unsigned n = 0; - const char *name = xkb_atom_text(keymap->ctx, atom); - struct xkb_key_type *type; - - if (keymap) { - darray_foreach(type, keymap->types) { - if (streq(type->name, name)) { - *type_rtrn = n; - return true; - } - n++; + unsigned int i; + + for (i = 0; i < keymap->num_types; i++) { + if (keymap->types[i].name == name) { + *type_rtrn = i; + return true; } } + return false; } @@ -1538,27 +1343,27 @@ FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn) * symbol per level. */ static bool -FindAutomaticType(struct xkb_keymap *keymap, int width, +FindAutomaticType(struct xkb_context *ctx, xkb_level_index_t width, const xkb_keysym_t *syms, xkb_atom_t *typeNameRtrn, bool *autoType) { *autoType = false; if ((width == 1) || (width == 0)) { - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ONE_LEVEL"); + *typeNameRtrn = xkb_atom_intern(ctx, "ONE_LEVEL"); *autoType = true; } else if (width == 2) { if (syms && xkb_keysym_is_lower(syms[0]) && xkb_keysym_is_upper(syms[1])) { - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "ALPHABETIC"); + *typeNameRtrn = xkb_atom_intern(ctx, "ALPHABETIC"); } else if (syms && (xkb_keysym_is_keypad(syms[0]) || xkb_keysym_is_keypad(syms[1]))) { - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "KEYPAD"); + *typeNameRtrn = xkb_atom_intern(ctx, "KEYPAD"); *autoType = true; } else { - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "TWO_LEVEL"); + *typeNameRtrn = xkb_atom_intern(ctx, "TWO_LEVEL"); *autoType = true; } } @@ -1567,19 +1372,19 @@ FindAutomaticType(struct xkb_keymap *keymap, int width, xkb_keysym_is_upper(syms[1])) if (xkb_keysym_is_lower(syms[2]) && xkb_keysym_is_upper(syms[3])) *typeNameRtrn = - xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_ALPHABETIC"); + xkb_atom_intern(ctx, "FOUR_LEVEL_ALPHABETIC"); else - *typeNameRtrn = xkb_atom_intern(keymap->ctx, + *typeNameRtrn = xkb_atom_intern(ctx, "FOUR_LEVEL_SEMIALPHABETIC"); else if (syms && (xkb_keysym_is_keypad(syms[0]) || xkb_keysym_is_keypad(syms[1]))) - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL_KEYPAD"); + *typeNameRtrn = xkb_atom_intern(ctx, "FOUR_LEVEL_KEYPAD"); else - *typeNameRtrn = xkb_atom_intern(keymap->ctx, "FOUR_LEVEL"); + *typeNameRtrn = xkb_atom_intern(ctx, "FOUR_LEVEL"); /* XXX: why not set autoType here? */ } - return ((width >= 0) && (width <= 4)); + return width <= 4; } /** @@ -1590,281 +1395,237 @@ static void PrepareKeyDef(KeyInfo *keyi) { xkb_group_index_t i, lastGroup; - int j, width, defined; + const GroupInfo *group0; bool identical; - defined = keyi->symsDefined | keyi->actsDefined | keyi->typesDefined; /* get highest group number */ - for (i = XkbNumKbdGroups - 1; i > 0; i--) { - if (defined & (1 << i)) + for (i = XKB_NUM_GROUPS - 1; i > 0; i--) + if (keyi->groups[i].defined) break; - } lastGroup = i; if (lastGroup == 0) return; + group0 = &keyi->groups[0]; + /* If there are empty groups between non-empty ones fill them with data */ /* from the first group. */ /* We can make a wrong assumption here. But leaving gaps is worse. */ for (i = lastGroup; i > 0; i--) { - if (defined & (1 << i)) + GroupInfo *groupi = &keyi->groups[i]; + + if (groupi->defined) continue; - width = keyi->numLevels[0]; - if (keyi->typesDefined & 1) { - for (j = 0; j < width; j++) { - keyi->types[i] = keyi->types[0]; - } - keyi->typesDefined |= 1 << i; - } - if ((keyi->actsDefined & 1) && !darray_empty(keyi->acts[0])) { - darray_copy(keyi->acts[i], keyi->acts[0]); - keyi->actsDefined |= 1 << i; - } - if ((keyi->symsDefined & 1) && !darray_empty(keyi->syms[0])) { - darray_copy(keyi->syms[i], keyi->syms[0]); - darray_copy(keyi->symsMapIndex[i], keyi->symsMapIndex[0]); - darray_copy(keyi->symsMapNumEntries[i], - keyi->symsMapNumEntries[0]); - keyi->symsDefined |= 1 << i; - } - if (defined & 1) { - keyi->numLevels[i] = keyi->numLevels[0]; - } + + groupi->type = group0->type; + darray_copy(groupi->syms, group0->syms); + darray_copy(groupi->levels, group0->levels); + groupi->defined = group0->defined; } + /* If all groups are completely identical remove them all */ /* exept the first one. */ + /* XXX: This code needs testing... or removal. */ identical = true; for (i = lastGroup; i > 0; i--) { - if ((keyi->numLevels[i] != keyi->numLevels[0]) || - (keyi->types[i] != keyi->types[0])) { + GroupInfo *groupi = &keyi->groups[i]; + + if (groupi->type != group0->type) { identical = false; break; } - if (!darray_same(keyi->syms[i], keyi->syms[0]) && - (darray_empty(keyi->syms[i]) || darray_empty(keyi->syms[0]) || - darray_size(keyi->syms[i]) != darray_size(keyi->syms[0]) || - memcmp(darray_mem(keyi->syms[i], 0), - darray_mem(keyi->syms[0], 0), - sizeof(xkb_keysym_t) * darray_size(keyi->syms[0])))) { + if (!darray_same(groupi->levels, group0->levels) && + (darray_empty(groupi->levels) || darray_empty(group0->levels) || + darray_size(groupi->levels) != darray_size(group0->levels) || + memcmp(darray_mem(groupi->levels, 0), + darray_mem(group0->levels, 0), + darray_size(group0->levels) * sizeof(LevelInfo)))) { identical = false; break; } - if (!darray_same(keyi->symsMapIndex[i], keyi->symsMapIndex[0]) && - (darray_empty(keyi->symsMapIndex[i]) || - darray_empty(keyi->symsMapIndex[0]) || - memcmp(darray_mem(keyi->symsMapIndex[i], 0), - darray_mem(keyi->symsMapIndex[0], 0), - keyi->numLevels[0] * sizeof(int)))) { - identical = false; - continue; - } - if (!darray_same(keyi->symsMapNumEntries[i], - keyi->symsMapNumEntries[0]) && - (darray_empty(keyi->symsMapNumEntries[i]) || - darray_empty(keyi->symsMapNumEntries[0]) || - memcmp(darray_mem(keyi->symsMapNumEntries[i], 0), - darray_mem(keyi->symsMapNumEntries[0], 0), - keyi->numLevels[0] * sizeof(size_t)))) { - identical = false; - continue; - } - if (!darray_same(keyi->acts[i], keyi->acts[0]) && - (darray_empty(keyi->acts[i]) || darray_empty(keyi->acts[0]) || - memcmp(darray_mem(keyi->acts[i], 0), - darray_mem(keyi->acts[0], 0), - keyi->numLevels[0] * sizeof(union xkb_action)))) { + if (!darray_same(groupi->syms, group0->syms) && + (darray_empty(groupi->syms) || darray_empty(group0->syms) || + darray_size(groupi->syms) != darray_size(group0->syms) || + memcmp(darray_mem(groupi->syms, 0), + darray_mem(group0->syms, 0), + darray_size(group0->syms) * sizeof(xkb_keysym_t)))) { identical = false; break; } } - if (identical) { - for (i = lastGroup; i > 0; i--) { - keyi->numLevels[i] = 0; - darray_free(keyi->syms[i]); - darray_free(keyi->symsMapIndex[i]); - darray_free(keyi->symsMapNumEntries[i]); - darray_free(keyi->acts[i]); - keyi->types[i] = 0; - } - keyi->symsDefined &= 1; - keyi->actsDefined &= 1; - keyi->typesDefined &= 1; - } + + if (identical) + for (i = lastGroup; i > 0; i--) + ClearGroupInfo(&keyi->groups[i]); } -/** - * Copy the KeyInfo into the keyboard description. - * - * This function recurses. - */ static bool -CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi, - xkb_keycode_t start_from) +CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi) { struct xkb_keymap *keymap = info->keymap; - xkb_keycode_t kc; struct xkb_key *key; - size_t sizeSyms = 0; - xkb_group_index_t i, nGroups; - unsigned width, tmp; - struct xkb_key_type * type; - bool haveActions, autoType, useAlias; - unsigned types[XkbNumKbdGroups]; - union xkb_action *outActs; - unsigned int symIndex = 0; - - useAlias = (start_from == 0); - - key = FindNamedKey(keymap, keyi->name, useAlias, - CreateKeyNames(keymap), start_from); + xkb_group_index_t i; + bool haveActions; + unsigned int sizeSyms; + unsigned int symIndex; + + /* + * The name is guaranteed to be real and not an alias (see + * AddKeySymbols), so 'false' is safe here. + */ + key = FindNamedKey(keymap, keyi->name, false); if (!key) { - if (start_from == 0) - log_lvl(info->keymap->ctx, 5, - "Key %s not found in keycodes; Symbols ignored\n", - longText(keyi->name)); + log_vrb(info->keymap->ctx, 5, + "Key %s not found in keycodes; Symbols ignored\n", + LongKeyNameText(keyi->name)); return false; } - kc = XkbKeyGetKeycode(keymap, key); + /* + * Find the range of groups we need. (There shouldn't be any gaps, + * see PrepareKeyDef). + */ + key->num_groups = 0; + for (i = 0; i < XKB_NUM_GROUPS; i++) + if (keyi->groups[i].defined) + key->num_groups = i + 1; + + /* See if we need to allocate an actions array. */ haveActions = false; - width = 0; - for (i = nGroups = 0; i < XkbNumKbdGroups; i++) { - if (((i + 1) > nGroups) - && (((keyi->symsDefined | keyi->actsDefined) & (1 << i)) - || (keyi->typesDefined) & (1 << i))) - nGroups = i + 1; - if (!darray_empty(keyi->acts[i])) - haveActions = true; - autoType = false; - /* Assign the type to the key, if it is missing. */ - if (keyi->types[i] == XKB_ATOM_NONE) { + for (i = 0; i < key->num_groups; i++) { + LevelInfo *leveli; + darray_foreach(leveli, keyi->groups[i].levels) { + if (leveli->act.type != ACTION_TYPE_NONE) { + haveActions = true; + goto out_of_loops; + } + } + } +out_of_loops: + + /* + * Find and assign the groups' types in the keymap. Also find the + * key width according to the largest type. + */ + key->width = 0; + for (i = 0; i < key->num_groups; i++) { + struct xkb_key_type *type; + GroupInfo *groupi = &keyi->groups[i]; + bool autoType = false; + + /* Find the type of the group, if it is missing. */ + if (groupi->type == XKB_ATOM_NONE) { if (keyi->dfltType != XKB_ATOM_NONE) - keyi->types[i] = keyi->dfltType; - else if (FindAutomaticType(keymap, keyi->numLevels[i], - darray_mem(keyi->syms[i], 0), - &keyi->types[i], &autoType)) { } + groupi->type = keyi->dfltType; + else if (FindAutomaticType(keymap->ctx, + darray_size(groupi->levels), + darray_mem(groupi->syms, 0), + &groupi->type, &autoType)) { } else - log_lvl(info->keymap->ctx, 5, - "No automatic type for %d symbols; " - "Using %s for the %s key (keycode %d)\n", - keyi->numLevels[i], - xkb_atom_text(keymap->ctx, keyi->types[i]), - longText(keyi->name), kc); + log_vrb(info->keymap->ctx, 5, + "No automatic type for %d levels; " + "Using %s for the %s key\n", + darray_size(groupi->levels), + xkb_atom_text(keymap->ctx, groupi->type), + LongKeyNameText(keyi->name)); } - if (FindNamedType(keymap, keyi->types[i], &types[i])) { - if (!autoType || keyi->numLevels[i] > 2) - key->explicit |= (1 << i); + + /* Find the type in the keymap, if it was defined in xkb_types. */ + if (FindNamedType(keymap, groupi->type, &key->kt_index[i])) { + if (!autoType || darray_size(groupi->levels) > 2) + key->explicit_groups |= (1 << i); } else { - log_lvl(info->keymap->ctx, 3, + log_vrb(info->keymap->ctx, 3, "Type \"%s\" is not defined; " - "Using TWO_LEVEL for the %s key (keycode %d)\n", - xkb_atom_text(keymap->ctx, keyi->types[i]), - longText(keyi->name), kc); - types[i] = XkbTwoLevelIndex; + "Using default type for the %s key\n", + xkb_atom_text(keymap->ctx, groupi->type), + LongKeyNameText(keyi->name)); + /* + * Index 0 is guaranteed to contain something, usually + * ONE_LEVEL or at least some default one-level type. + */ + key->kt_index[i] = 0; } - /* if the type specifies fewer levels than the key has, shrink the key */ - type = &darray_item(keymap->types, types[i]); - if (type->num_levels < keyi->numLevels[i]) { - log_lvl(info->keymap->ctx, 1, - "Type \"%s\" has %d levels, but %s has %d symbols; " + /* If the type specifies fewer levels than the key has, shrink the key. */ + type = &keymap->types[key->kt_index[i]]; + if (type->num_levels < darray_size(groupi->levels)) { + log_vrb(info->keymap->ctx, 1, + "Type \"%s\" has %d levels, but %s has %d levels; " "Ignoring extra symbols\n", - type->name, type->num_levels, - longText(keyi->name), - keyi->numLevels[i]); - keyi->numLevels[i] = type->num_levels; + xkb_atom_text(keymap->ctx, type->name), + type->num_levels, + LongKeyNameText(keyi->name), + darray_size(groupi->levels)); + darray_resize(groupi->levels, type->num_levels); } - if (keyi->numLevels[i] > width) - width = keyi->numLevels[i]; - if (type->num_levels > width) - width = type->num_levels; - sizeSyms += darray_size(keyi->syms[i]); - } - - darray_resize0(key->syms, sizeSyms); - if (haveActions) { - outActs = ResizeKeyActions(keymap, key, width * nGroups); - if (outActs == NULL) { - log_wsgo(info->keymap->ctx, - "Could not enlarge actions for %s (key %d)\n", - longText(keyi->name), kc); - return false; - } - key->explicit |= XkbExplicitInterpretMask; - } - else - outActs = NULL; - - key->num_groups = nGroups; - if (keyi->defined & _Key_GroupInfo) { - key->out_of_range_group_number = keyi->out_of_range_group_number; - key->out_of_range_group_action = keyi->out_of_range_group_action; - } - key->width = width; - key->sym_index = calloc(nGroups * width, sizeof(*key->sym_index)); - key->num_syms = calloc(nGroups * width, sizeof(*key->num_syms)); - - for (i = 0; i < nGroups; i++) { - /* assign kt_index[i] to the index of the type in map->types. - * kt_index[i] may have been set by a previous run (if we have two - * layouts specified). Let's not overwrite it with the ONE_LEVEL - * default group if we dont even have keys for this group anyway. - * - * FIXME: There should be a better fix for this. + /* + * Why type->num_levels and not darray_size(groupi->levels)? + * Because the type may have more levels, and each group must + * have at least as many levels as its type. Because the + * key->syms array is indexed by (group * width + level), we + * must take the largest one. + * Maybe we can change it to save some space. */ - if (keyi->numLevels[i]) - key->kt_index[i] = types[i]; - if (!darray_empty(keyi->syms[i])) { - /* fill key to "width" symbols*/ - for (tmp = 0; tmp < width; tmp++) { - if (tmp < keyi->numLevels[i] && - darray_item(keyi->symsMapNumEntries[i], tmp) != 0) { - memcpy(darray_mem(key->syms, symIndex), - darray_mem(keyi->syms[i], - darray_item(keyi->symsMapIndex[i], tmp)), - darray_item(keyi->symsMapNumEntries[i], - tmp) * sizeof(xkb_keysym_t)); - key->sym_index[(i * width) + tmp] = symIndex; - key->num_syms[(i * width) + tmp] = - darray_item(keyi->symsMapNumEntries[i], tmp); - symIndex += key->num_syms[(i * width) + tmp]; - } - else { - key->sym_index[(i * width) + tmp] = -1; - key->num_syms[(i * width) + tmp] = 0; - } - if (outActs != NULL && !darray_empty(keyi->acts[i])) { - if (tmp < keyi->numLevels[i]) - outActs[tmp] = darray_item(keyi->acts[i], tmp); - else - outActs[tmp].type = XkbSA_NoAction; - } - } - } - } - switch (keyi->behavior.type & XkbKB_OpMask) { - case XkbKB_Default: - break; - - default: - key->behavior = keyi->behavior; - key->explicit |= XkbExplicitBehaviorMask; - break; + key->width = MAX(key->width, type->num_levels); + } + + /* Find the size of the syms array. */ + sizeSyms = 0; + for (i = 0; i < key->num_groups; i++) + sizeSyms += darray_size(keyi->groups[i].syms); + + /* Initialize the xkb_key, now that we know the sizes. */ + key->syms = calloc(sizeSyms, sizeof(*key->syms)); + key->sym_index = calloc(key->num_groups * key->width, + sizeof(*key->sym_index)); + key->num_syms = calloc(key->num_groups * key->width, + sizeof(*key->num_syms)); + key->out_of_range_group_number = keyi->out_of_range_group_number; + key->out_of_range_group_action = keyi->out_of_range_group_action; + if (haveActions) { + key->actions = calloc(key->num_groups * key->width, + sizeof(*key->actions)); + key->explicit |= EXPLICIT_INTERP; } - if (keyi->defined & _Key_VModMap) { + if (keyi->defined & KEY_FIELD_VMODMAP) { key->vmodmap = keyi->vmodmap; - key->explicit |= XkbExplicitVModMapMask; + key->explicit |= EXPLICIT_VMODMAP; } - if (keyi->repeat != RepeatUndefined) { - key->repeats = keyi->repeat == RepeatYes; - key->explicit |= XkbExplicitAutoRepeatMask; + + if (keyi->repeat != KEY_REPEAT_UNDEFINED) { + key->repeats = (keyi->repeat == KEY_REPEAT_YES); + key->explicit |= EXPLICIT_REPEAT; + } + + /* Copy keysyms and actions. */ + symIndex = 0; + for (i = 0; i < key->num_groups; i++) { + GroupInfo *groupi = &keyi->groups[i]; + xkb_level_index_t j; + + /* We rely on calloc having zeroized the arrays up to key->width. */ + for (j = 0; j < darray_size(groupi->levels); j++) { + LevelInfo *leveli = &darray_item(groupi->levels, j); + + if (leveli->act.type != ACTION_TYPE_NONE) + key->actions[i * key->width + j] = leveli->act; + + if (leveli->num_syms <= 0) + continue; + + memcpy(&key->syms[symIndex], + &darray_item(groupi->syms, leveli->sym_index), + leveli->num_syms * sizeof(*key->syms)); + key->sym_index[i * key->width + j] = symIndex; + key->num_syms[i * key->width + j] = leveli->num_syms; + symIndex += key->num_syms[i * key->width + j]; + } } - /* do the same thing for the next key */ - CopySymbolsDef(info, keyi, kc + 1); return true; } @@ -1875,13 +1636,12 @@ CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry) struct xkb_keymap *keymap = info->keymap; if (!entry->haveSymbol) { - key = FindNamedKey(keymap, entry->u.keyName, true, - CreateKeyNames(keymap), 0); + key = FindNamedKey(keymap, entry->u.keyName, true); if (!key) { - log_lvl(info->keymap->ctx, 5, + log_vrb(info->keymap->ctx, 5, "Key %s not found in keycodes; " "Modifier map entry for %s not updated\n", - longText(entry->u.keyName), + LongKeyNameText(entry->u.keyName), ModIndexText(entry->modifier)); return false; } @@ -1889,7 +1649,7 @@ CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry) else { key = FindKeyForSymbol(keymap, entry->u.keySym); if (!key) { - log_lvl(info->keymap->ctx, 5, + log_vrb(info->keymap->ctx, 5, "Key \"%s\" not found in symbol map; " "Modifier map entry for %s not updated\n", KeysymText(entry->u.keySym), @@ -1902,54 +1662,25 @@ CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry) return true; } -/** - * Handle the xkb_symbols section of an xkb file. - * - * @param file The parsed xkb_symbols section of the xkb file. - * @param keymap Handle to the keyboard description to store the symbols in. - * @param merge Merge strategy (e.g. MERGE_OVERRIDE). - */ -bool -CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, - enum merge_mode merge) +static bool +CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info) { - xkb_group_index_t i; - struct xkb_key *key; - SymbolsInfo info; KeyInfo *keyi; ModMapEntry *mm; + xkb_group_index_t i; + struct xkb_key *key; - InitSymbolsInfo(&info, keymap, file->id); - info.dflt.merge = merge; - - HandleSymbolsFile(&info, file, merge); - - if (darray_empty(info.keys)) - goto err_info; - - if (info.errorCount != 0) - goto err_info; - - darray_resize0(keymap->acts, darray_size(keymap->acts) + 32 + 1); - - if (info.name) - keymap->symbols_section_name = strdup(info.name); + keymap->symbols_section_name = strdup_safe(info->name); - for (i = 0; i < XkbNumKbdGroups; i++) { - if (info.groupNames[i] != XKB_ATOM_NONE) { - keymap->group_names[i] = xkb_atom_text(keymap->ctx, - info.groupNames[i]); - } - } + for (i = 0; i < XKB_NUM_GROUPS; i++) + if (info->groupNames[i] != XKB_ATOM_NONE) + keymap->group_names[i] = info->groupNames[i]; - /* sanitize keys */ - darray_foreach(keyi, info.keys) + darray_foreach(keyi, info->keys) { PrepareKeyDef(keyi); - - /* copy! */ - darray_foreach(keyi, info.keys) - if (!CopySymbolsDef(&info, keyi, 0)) - info.errorCount++; + if (!CopySymbolsDef(info, keyi)) + info->errorCount++; + } if (xkb_get_log_verbosity(keymap->ctx) > 3) { xkb_foreach_key(key, keymap) { @@ -1957,20 +1688,51 @@ CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, continue; if (key->num_groups < 1) - log_warn(info.keymap->ctx, - "No symbols defined for <%.4s> (keycode %d)\n", - key->name, XkbKeyGetKeycode(keymap, key)); + log_info(keymap->ctx, + "No symbols defined for %s\n", + KeyNameText(key->name)); } } - list_foreach(mm, &info.modMaps, entry) - if (!CopyModMapDef(&info, mm)) - info.errorCount++; + darray_foreach(mm, info->modMaps) + if (!CopyModMapDef(info, mm)) + info->errorCount++; + + /* XXX: If we don't ignore errorCount, things break. */ + return true; +} + +bool +CompileSymbols(XkbFile *file, struct xkb_keymap *keymap, + enum merge_mode merge) +{ + SymbolsInfo info; + ActionsInfo *actions; + + actions = NewActionsInfo(); + if (!actions) + return false; + + InitSymbolsInfo(&info, keymap, file->id, actions); + info.dflt.merge = merge; + + HandleSymbolsFile(&info, file, merge); + + if (darray_empty(info.keys)) + goto err_info; + + if (info.errorCount != 0) + goto err_info; + + if (!CopySymbolsToKeymap(keymap, &info)) + goto err_info; - FreeSymbolsInfo(&info); + ClearSymbolsInfo(&info); + FreeActionsInfo(actions); return true; err_info: - FreeSymbolsInfo(&info); + FreeActionsInfo(actions); + ClearSymbolsInfo(&info); return false; }