********************************************************/
#include "xkbcomp-priv.h"
+#include "text.h"
#include "expr.h"
-#include "parseutils.h"
+#include "include.h"
-typedef struct _AliasInfo {
+typedef struct {
enum merge_mode merge;
- unsigned file_id;
- struct list entry;
- char alias[XkbKeyNameLength + 1];
- char real[XkbKeyNameLength + 1];
+ xkb_atom_t alias;
+ xkb_atom_t real;
} AliasInfo;
-typedef struct _IndicatorNameInfo {
+typedef struct {
enum merge_mode merge;
- unsigned file_id;
- struct list entry;
- int ndx;
xkb_atom_t name;
- bool virtual;
-} IndicatorNameInfo;
+} LedNameInfo;
-typedef struct _KeyNamesInfo {
- char *name; /* e.g. evdev+aliases(qwerty) */
+typedef struct {
+ char *name;
int errorCount;
- unsigned file_id;
- enum merge_mode merge;
- xkb_keycode_t computedMin; /* lowest keycode stored */
- xkb_keycode_t computedMax; /* highest keycode stored */
- xkb_keycode_t explicitMin;
- xkb_keycode_t explicitMax;
- darray(unsigned long) names;
- darray(unsigned int) files;
- struct list leds;
- struct list aliases;
-
- struct xkb_keymap *keymap;
-} KeyNamesInfo;
-static void
-ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
-{
- if (newMax < darray_size(info->names))
- return;
+ xkb_keycode_t min_key_code;
+ xkb_keycode_t max_key_code;
+ darray(xkb_atom_t) key_names;
+ LedNameInfo led_names[XKB_MAX_LEDS];
+ unsigned int num_led_names;
+ darray(AliasInfo) aliases;
- darray_resize0(info->names, newMax + 1);
- darray_resize0(info->files, newMax + 1);
-}
+ struct xkb_context *ctx;
+} KeyNamesInfo;
+
+/***====================================================================***/
static void
-InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
- char *alias, char *real)
+InitAliasInfo(AliasInfo *info, enum merge_mode merge,
+ xkb_atom_t alias, xkb_atom_t real)
{
memset(info, 0, sizeof(*info));
info->merge = merge;
- info->file_id = file_id;
- strncpy(info->alias, alias, XkbKeyNameLength);
- strncpy(info->real, real, XkbKeyNameLength);
+ info->alias = alias;
+ info->real = real;
}
-static void
-InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
+static LedNameInfo *
+FindLedByName(KeyNamesInfo *info, xkb_atom_t name,
+ xkb_led_index_t *idx_out)
{
- ii->merge = info->merge;
- ii->file_id = info->file_id;
- ii->ndx = 0;
- ii->name = XKB_ATOM_NONE;
- ii->virtual = false;
-}
-
-static IndicatorNameInfo *
-NextIndicatorName(KeyNamesInfo * info)
-{
- IndicatorNameInfo *ii;
-
- ii = malloc(sizeof(*ii));
- if (!ii)
- return NULL;
-
- InitIndicatorNameInfo(ii, info);
- list_append(&ii->entry, &info->leds);
-
- return ii;
-}
-
-static IndicatorNameInfo *
-FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
-{
- IndicatorNameInfo *old;
-
- list_foreach(old, &info->leds, entry)
- if (old->ndx == ndx)
- return old;
-
- return NULL;
-}
-
-static IndicatorNameInfo *
-FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
-{
- IndicatorNameInfo *old;
-
- list_foreach(old, &info->leds, entry)
- if (old->name == name)
- return old;
+ for (xkb_led_index_t idx = 0; idx < info->num_led_names; idx++) {
+ LedNameInfo *ledi = &info->led_names[idx];
+ if (ledi->name == name) {
+ *idx_out = idx;
+ return ledi;
+ }
+ }
return NULL;
}
static bool
-AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
- IndicatorNameInfo *new)
+AddLedName(KeyNamesInfo *info, enum merge_mode merge, bool same_file,
+ LedNameInfo *new, xkb_led_index_t new_idx)
{
- IndicatorNameInfo *old;
- bool replace;
- int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
-
- replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
-
- old = FindIndicatorByName(info, new->name);
+ xkb_led_index_t old_idx;
+ LedNameInfo *old;
+ const int verbosity = xkb_context_get_log_verbosity(info->ctx);
+ const bool report = (same_file && verbosity > 0) || verbosity > 9;
+ const bool replace = (merge == MERGE_REPLACE || merge == MERGE_OVERRIDE);
+
+ /* LED with the same name already exists. */
+ old = FindLedByName(info, new->name, &old_idx);
if (old) {
- if ((old->file_id == new->file_id && verbosity > 0) ||
- verbosity > 9) {
- if (old->ndx == new->ndx) {
- if (old->virtual != new->virtual) {
- if (replace)
- old->virtual = new->virtual;
- log_warn(info->keymap->ctx, "Multiple indicators named %s; "
- "Using %s instead of %s\n",
- xkb_atom_text(info->keymap->ctx, new->name),
- (old->virtual ? "virtual" : "real"),
- (old->virtual ? "real" : "virtual"));
- }
- else {
- log_warn(info->keymap->ctx, "Multiple indicators named %s; "
- "Identical definitions ignored\n",
- xkb_atom_text(info->keymap->ctx, new->name));
- }
- return true;
- }
- else {
- log_warn(info->keymap->ctx, "Multiple indicators named %s; "
- "Using %d, ignoring %d\n",
- xkb_atom_text(info->keymap->ctx, new->name),
- (replace ? old->ndx : new->ndx),
- (replace ? new->ndx : old->ndx));
- }
+ if (old_idx == new_idx) {
+ log_warn(info->ctx,
+ "Multiple indicators named \"%s\"; "
+ "Identical definitions ignored\n",
+ xkb_atom_text(info->ctx, new->name));
+ return true;
+ }
- if (replace) {
- list_del(&old->entry);
- free(old);
- }
+ if (report) {
+ xkb_led_index_t use = (replace ? new_idx + 1 : old_idx + 1);
+ xkb_led_index_t ignore = (replace ? old_idx + 1 : new_idx + 1);
+ log_warn(info->ctx,
+ "Multiple indicators named %s; Using %d, ignoring %d\n",
+ xkb_atom_text(info->ctx, new->name), use, ignore);
}
+
+ if (replace)
+ *old = *new;
+
+ return true;
}
- old = FindIndicatorByIndex(info, new->ndx);
- if (old) {
- if ((old->file_id == new->file_id && verbosity > 0) ||
- verbosity > 9) {
- if (old->name == new->name && old->virtual == new->virtual) {
- log_warn(info->keymap->ctx,
- "Multiple names for indicator %d; "
- "Identical definitions ignored\n", new->ndx);
- } else {
- const char *oldType, *newType;
- xkb_atom_t using, ignoring;
- if (old->virtual)
- oldType = "virtual indicator";
- else
- oldType = "real indicator";
- if (new->virtual)
- newType = "virtual indicator";
- else
- newType = "real indicator";
- if (replace) {
- using = new->name;
- ignoring = old->name;
- }
- else {
- using = old->name;
- ignoring = new->name;
- }
- log_warn(info->keymap->ctx,
- "Multiple names for indicator %d; "
- "Using %s %s, ignoring %s %s\n",
- new->ndx,
- oldType, xkb_atom_text(info->keymap->ctx, using),
- newType, xkb_atom_text(info->keymap->ctx, ignoring));
- }
- }
- if (replace) {
- old->name = new->name;
- old->virtual = new->virtual;
+ if (new_idx >= info->num_led_names)
+ info->num_led_names = new_idx + 1;
+
+ /* LED with the same index already exists. */
+ old = &info->led_names[new_idx];
+ if (old->name != XKB_ATOM_NONE) {
+ if (report) {
+ const xkb_atom_t use = (replace ? new->name : old->name);
+ const xkb_atom_t ignore = (replace ? old->name : new->name);
+ log_warn(info->ctx, "Multiple names for indicator %d; "
+ "Using %s, ignoring %s\n", new_idx + 1,
+ xkb_atom_text(info->ctx, use),
+ xkb_atom_text(info->ctx, ignore));
}
+
+ if (replace)
+ *old = *new;
+
return true;
}
- old = new;
- new = NextIndicatorName(info);
- if (!new) {
- log_wsgo(info->keymap->ctx,
- "Couldn't allocate name for indicator %d; Ignored\n",
- old->ndx);
- return false;
- }
- new->name = old->name;
- new->ndx = old->ndx;
- new->virtual = old->virtual;
+
+ *old = *new;
return true;
}
static void
-ClearKeyNamesInfo(KeyNamesInfo * info)
+ClearKeyNamesInfo(KeyNamesInfo *info)
{
- AliasInfo *alias, *next_alias;
- IndicatorNameInfo *ii, *next_ii;
-
free(info->name);
- info->name = NULL;
- info->merge = MERGE_DEFAULT;
- info->computedMax = info->explicitMax = info->explicitMin = 0;
- info->computedMin = XKB_KEYCODE_MAX;
- darray_free(info->names);
- darray_free(info->files);
- list_foreach_safe(ii, next_ii, &info->leds, entry)
- free(ii);
- list_init(&info->leds);
- list_foreach_safe(alias, next_alias, &info->aliases, entry)
- free(alias);
- list_init(&info->aliases);
+ darray_free(info->key_names);
+ darray_free(info->aliases);
}
static void
-InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_keymap *keymap,
- unsigned file_id)
+InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx)
{
- info->name = NULL;
- info->merge = MERGE_DEFAULT;
- list_init(&info->leds);
- list_init(&info->aliases);
- info->file_id = file_id;
- darray_init(info->names);
- darray_init(info->files);
- ClearKeyNamesInfo(info);
- info->errorCount = 0;
- info->keymap = keymap;
+ memset(info, 0, sizeof(*info));
+ info->ctx = ctx;
+ info->min_key_code = XKB_KEYCODE_INVALID;
+#if XKB_KEYCODE_INVALID < XKB_KEYCODE_MAX
+#error "Hey, you can't be changing stuff like that."
+#endif
}
-static int
-FindKeyByLong(KeyNamesInfo * info, unsigned long name)
+static xkb_keycode_t
+FindKeyByName(KeyNamesInfo *info, xkb_atom_t name)
{
- uint64_t i;
+ xkb_keycode_t i;
- for (i = info->computedMin; i <= info->computedMax; i++)
- if (darray_item(info->names, i) == name)
+ for (i = info->min_key_code; i <= info->max_key_code; i++)
+ if (darray_item(info->key_names, i) == name)
return i;
- return 0;
+ return XKB_KEYCODE_INVALID;
}
-/**
- * Store the name of the key as a long in the info struct under the given
- * keycode. If the same keys is referred to twice, print a warning.
- * Note that the key's name is stored as a long, the keycode is the index.
- */
static bool
-AddKeyName(KeyNamesInfo * info,
- xkb_keycode_t kc, char *name, enum merge_mode merge,
- unsigned file_id, bool reportCollisions)
+AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name,
+ enum merge_mode merge, bool same_file, bool report)
{
- xkb_keycode_t old;
- unsigned long lval;
- int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
-
- ResizeKeyNameArrays(info, kc);
-
- if (kc < info->computedMin)
- info->computedMin = kc;
- if (kc > info->computedMax)
- info->computedMax = kc;
- lval = KeyNameToLong(name);
-
- if (reportCollisions)
- reportCollisions = (verbosity > 7 ||
- (verbosity > 0 &&
- file_id == darray_item(info->files, kc)));
-
- if (darray_item(info->names, kc) != 0) {
- char buf[6];
-
- LongToKeyName(darray_item(info->names, kc), buf);
- buf[4] = '\0';
- if (darray_item(info->names, kc) == lval && reportCollisions) {
- log_warn(info->keymap->ctx,
- "Multiple identical key name definitions; "
- "Later occurences of \"<%s> = %d\" ignored\n", buf, kc);
+ xkb_atom_t old_name;
+ xkb_keycode_t old_kc;
+ const int verbosity = xkb_context_get_log_verbosity(info->ctx);
+
+ report = report && ((same_file && verbosity > 0) || verbosity > 7);
+
+ if (kc >= darray_size(info->key_names))
+ darray_resize0(info->key_names, kc + 1);
+
+ info->min_key_code = MIN(info->min_key_code, kc);
+ info->max_key_code = MAX(info->max_key_code, kc);
+
+ /* There's already a key with this keycode. */
+ old_name = darray_item(info->key_names, kc);
+ if (old_name != XKB_ATOM_NONE) {
+ const char *lname = KeyNameText(info->ctx, old_name);
+ const char *kname = KeyNameText(info->ctx, name);
+
+ if (old_name == name) {
+ if (report)
+ log_warn(info->ctx,
+ "Multiple identical key name definitions; "
+ "Later occurrences of \"%s = %d\" ignored\n",
+ lname, kc);
return true;
}
-
- if (merge == MERGE_AUGMENT) {
- if (reportCollisions)
- log_warn(info->keymap->ctx,
+ else if (merge == MERGE_AUGMENT) {
+ if (report)
+ log_warn(info->ctx,
"Multiple names for keycode %d; "
- "Using <%s>, ignoring <%s>\n", kc, buf, name);
+ "Using %s, ignoring %s\n", kc, lname, kname);
return true;
}
else {
- if (reportCollisions)
- log_warn(info->keymap->ctx,
+ if (report)
+ log_warn(info->ctx,
"Multiple names for keycode %d; "
- "Using <%s>, ignoring <%s>\n", kc, name, buf);
- darray_item(info->names, kc) = 0;
- darray_item(info->files, kc) = 0;
+ "Using %s, ignoring %s\n", kc, kname, lname);
+ darray_item(info->key_names, kc) = XKB_ATOM_NONE;
}
}
- old = FindKeyByLong(info, lval);
- if ((old != 0) && (old != kc)) {
+ /* There's already a key with this name. */
+ old_kc = FindKeyByName(info, name);
+ if (old_kc != XKB_KEYCODE_INVALID && old_kc != kc) {
+ const char *kname = KeyNameText(info->ctx, name);
+
if (merge == MERGE_OVERRIDE) {
- darray_item(info->names, old) = 0;
- darray_item(info->files, old) = 0;
- if (reportCollisions)
- log_warn(info->keymap->ctx,
- "Key name <%s> assigned to multiple keys; "
- "Using %d, ignoring %d\n", name, kc, old);
+ darray_item(info->key_names, old_kc) = XKB_ATOM_NONE;
+ if (report)
+ log_warn(info->ctx,
+ "Key name %s assigned to multiple keys; "
+ "Using %d, ignoring %d\n", kname, kc, old_kc);
}
else {
- if (reportCollisions && verbosity > 3)
- log_warn(info->keymap->ctx,
- "Key name <%s> assigned to multiple keys; "
- "Using %d, ignoring %d\n", name, old, kc);
+ if (report)
+ log_vrb(info->ctx, 3,
+ "Key name %s assigned to multiple keys; "
+ "Using %d, ignoring %d\n", kname, old_kc, kc);
return true;
}
}
- darray_item(info->names, kc) = lval;
- darray_item(info->files, kc) = file_id;
+
+ darray_item(info->key_names, kc) = name;
return true;
}
/***====================================================================***/
-static int
-HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
- unsigned file_id);
-
static bool
-MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
-{
- AliasInfo *alias, *next;
- KeyAliasDef def;
-
- if (list_empty(&from->aliases))
- return true;
-
- if (list_empty(&into->aliases)) {
- list_replace(&from->aliases, &into->aliases);
- list_init(&from->aliases);
- return true;
- }
-
- memset(&def, 0, sizeof(def));
-
- list_foreach_safe(alias, next, &from->aliases, entry) {
- def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
- memcpy(def.alias, alias->alias, XkbKeyNameLength);
- memcpy(def.real, alias->real, XkbKeyNameLength);
-
- if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
- return false;
- }
-
- return true;
-}
+HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge);
static void
MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
enum merge_mode merge)
{
- uint64_t i;
- char buf[5];
- IndicatorNameInfo *led;
-
if (from->errorCount > 0) {
into->errorCount += from->errorCount;
return;
}
+
if (into->name == NULL) {
into->name = from->name;
from->name = NULL;
}
- ResizeKeyNameArrays(into, from->computedMax);
+ /* Merge key names. */
+ if (darray_empty(into->key_names)) {
+ into->key_names = from->key_names;
+ darray_init(from->key_names);
+ into->min_key_code = from->min_key_code;
+ into->max_key_code = from->max_key_code;
+ }
+ else {
+ if (darray_size(into->key_names) < darray_size(from->key_names))
+ darray_resize0(into->key_names, darray_size(from->key_names));
- for (i = from->computedMin; i <= from->computedMax; i++) {
- if (darray_item(from->names, i) == 0)
- continue;
- LongToKeyName(darray_item(from->names, i), buf);
- buf[4] = '\0';
- if (!AddKeyName(into, i, buf, merge, from->file_id, false))
- into->errorCount++;
+ for (unsigned i = from->min_key_code; i <= from->max_key_code; i++) {
+ xkb_atom_t name = darray_item(from->key_names, i);
+ if (name == XKB_ATOM_NONE)
+ continue;
+
+ if (!AddKeyName(into, i, name, merge, true, false))
+ into->errorCount++;
+ }
}
- list_foreach(led, &from->leds, entry) {
- led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
- if (!AddIndicatorName(into, led->merge, led))
- into->errorCount++;
+ /* Merge key aliases. */
+ if (darray_empty(into->aliases)) {
+ into->aliases = from->aliases;
+ darray_init(from->aliases);
+ }
+ else {
+ AliasInfo *alias;
+
+ darray_foreach(alias, from->aliases) {
+ KeyAliasDef def;
+
+ def.merge = (merge == MERGE_DEFAULT ? alias->merge : merge);
+ def.alias = alias->alias;
+ def.real = alias->real;
+
+ if (!HandleAliasDef(into, &def, def.merge))
+ into->errorCount++;
+ }
}
- if (!MergeAliases(into, from, merge))
- into->errorCount++;
- if (from->explicitMin != 0) {
- if ((into->explicitMin == 0)
- || (into->explicitMin > from->explicitMin))
- into->explicitMin = from->explicitMin;
+ /* Merge LED names. */
+ if (into->num_led_names == 0) {
+ memcpy(into->led_names, from->led_names,
+ sizeof(*from->led_names) * from->num_led_names);
+ into->num_led_names = from->num_led_names;
+ from->num_led_names = 0;
}
- if (from->explicitMax > 0) {
- if ((into->explicitMax == 0)
- || (into->explicitMax < from->explicitMax))
- into->explicitMax = from->explicitMax;
+ else {
+ for (xkb_led_index_t idx = 0; idx < from->num_led_names; idx++) {
+ LedNameInfo *ledi = &from->led_names[idx];
+
+ if (ledi->name == XKB_ATOM_NONE)
+ continue;
+
+ ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
+ if (!AddLedName(into, ledi->merge, false, ledi, idx))
+ into->errorCount++;
+ }
}
}
static void
HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
-/**
- * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
- *
- * @param info Struct to store the key info in.
- * @param stmt The include statement from the keymap file.
- */
static bool
-HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
+HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *include)
{
- enum merge_mode merge = MERGE_DEFAULT;
- XkbFile *rtrn;
- KeyNamesInfo included, next_incl;
-
- /* XXX: What's that? */
- if (stmt->file && streq(stmt->file, "computed")) {
- info->keymap->flags |= AutoKeyNames;
- info->explicitMin = 0;
- info->explicitMax = XKB_KEYCODE_MAX;
- return (info->errorCount == 0);
- }
+ KeyNamesInfo included;
- InitKeyNamesInfo(&included, info->keymap, info->file_id);
- if (stmt->stmt) {
- free(included.name);
- included.name = stmt->stmt;
- stmt->stmt = NULL;
- }
+ InitKeyNamesInfo(&included, info->ctx);
+ included.name = include->stmt;
+ include->stmt = NULL;
+
+ for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
+ KeyNamesInfo next_incl;
+ XkbFile *file;
- for (; stmt; stmt = stmt->next_incl) {
- if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_KEYCODES,
- &rtrn, &merge)) {
+ file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES);
+ if (!file) {
info->errorCount += 10;
ClearKeyNamesInfo(&included);
return false;
}
- InitKeyNamesInfo(&next_incl, info->keymap, rtrn->id);
+ InitKeyNamesInfo(&next_incl, info->ctx);
- HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
+ HandleKeycodesFile(&next_incl, file, MERGE_OVERRIDE);
- MergeIncludedKeycodes(&included, &next_incl, merge);
+ MergeIncludedKeycodes(&included, &next_incl, stmt->merge);
ClearKeyNamesInfo(&next_incl);
- FreeXKBFile(rtrn);
+ FreeXkbFile(file);
}
- MergeIncludedKeycodes(info, &included, merge);
+ MergeIncludedKeycodes(info, &included, include->merge);
ClearKeyNamesInfo(&included);
return (info->errorCount == 0);
}
-/**
- * Parse the given statement and store the output in the info struct.
- * e.g. <ESC> = 9
- */
-static int
+static bool
HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
{
- if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
- (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
- log_err(info->keymap->ctx,
- "Illegal keycode %lu for name <%s>; "
- "Must be in the range %d-%d inclusive\n",
- stmt->value, stmt->name, info->explicitMin,
- info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
- return 0;
- }
if (stmt->merge != MERGE_DEFAULT) {
if (stmt->merge == MERGE_REPLACE)
merge = MERGE_OVERRIDE;
else
merge = stmt->merge;
}
- return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
- true);
-}
-static void
-HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
-{
- int verbosity = xkb_get_log_verbosity(info->keymap->ctx);
-
- if (strncmp(new->real, old->real, XkbKeyNameLength) == 0) {
- if ((new->file_id == old->file_id && verbosity > 0) || verbosity > 9)
- log_warn(info->keymap->ctx,
- "Alias of %s for %s declared more than once; "
- "First definition ignored\n",
- KeyNameText(new->alias), KeyNameText(new->real));
+ if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) {
+ log_err(info->ctx,
+ "Illegal keycode %lld: must be between 0..%u; "
+ "Key ignored\n", (long long) stmt->value, XKB_KEYCODE_MAX);
+ return false;
}
- else {
- char *use, *ignore;
- if (new->merge == MERGE_AUGMENT) {
- use = old->real;
- ignore = new->real;
- }
- else {
- use = new->real;
- ignore = old->real;
- }
+ return AddKeyName(info, stmt->value, stmt->name, merge, false, true);
+}
- if ((old->file_id == new->file_id && verbosity > 0) || verbosity > 9)
- log_warn(info->keymap->ctx,
- "Multiple definitions for alias %s; "
- "Using %s, ignoring %s\n",
- KeyNameText(old->alias), KeyNameText(use),
- KeyNameText(ignore));
+static bool
+HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge)
+{
+ AliasInfo *old, new;
+
+ darray_foreach(old, info->aliases) {
+ if (old->alias == def->alias) {
+ if (def->real == old->real) {
+ log_vrb(info->ctx, 1,
+ "Alias of %s for %s declared more than once; "
+ "First definition ignored\n",
+ KeyNameText(info->ctx, def->alias),
+ KeyNameText(info->ctx, def->real));
+ }
+ else {
+ xkb_atom_t use, ignore;
- if (use != old->real)
- memcpy(old->real, use, XkbKeyNameLength);
- }
+ use = (merge == MERGE_AUGMENT ? old->real : def->real);
+ ignore = (merge == MERGE_AUGMENT ? def->real : old->real);
- old->file_id = new->file_id;
- old->merge = new->merge;
-}
+ log_warn(info->ctx,
+ "Multiple definitions for alias %s; "
+ "Using %s, ignoring %s\n",
+ KeyNameText(info->ctx, old->alias),
+ KeyNameText(info->ctx, use),
+ KeyNameText(info->ctx, ignore));
-static int
-HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
- unsigned file_id)
-{
- AliasInfo *alias;
+ old->real = use;
+ }
- list_foreach(alias, &info->aliases, entry) {
- if (strncmp(alias->alias, def->alias, XkbKeyNameLength) == 0) {
- AliasInfo new;
- InitAliasInfo(&new, merge, file_id, def->alias, def->real);
- HandleAliasCollision(info, alias, &new);
+ old->merge = merge;
return true;
}
}
- alias = calloc(1, sizeof(*alias));
- if (!alias) {
- log_wsgo(info->keymap->ctx, "Allocation failure in HandleAliasDef\n");
- return false;
- }
-
- alias->file_id = file_id;
- alias->merge = merge;
- memcpy(alias->alias, def->alias, XkbKeyNameLength);
- memcpy(alias->real, def->real, XkbKeyNameLength);
- list_append(&alias->entry, &info->aliases);
-
+ InitAliasInfo(&new, merge, def->alias, def->real);
+ darray_append(info->aliases, new);
return true;
}
-#define MIN_KEYCODE_DEF 0
-#define MAX_KEYCODE_DEF 1
-
-/**
- * Handle the minimum/maximum statement of the xkb file.
- * Sets explicitMin/Max of the info struct.
- *
- * @return 1 on success, 0 otherwise.
- */
-static int
+static bool
HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
{
const char *elem, *field;
- xkb_keycode_t kc;
ExprDef *arrayNdx;
- int which;
- if (!ExprResolveLhs(info->keymap->ctx, stmt->name, &elem, &field,
- &arrayNdx))
- return false; /* internal error, already reported */
+ if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
+ return false;
if (elem) {
- log_err(info->keymap->ctx,
- "Unknown element %s encountered; "
+ log_err(info->ctx, "Unknown element %s encountered; "
"Default for field %s ignored\n", elem, field);
return false;
}
- if (istreq(field, "minimum"))
- which = MIN_KEYCODE_DEF;
- else if (istreq(field, "maximum"))
- which = MAX_KEYCODE_DEF;
- else {
- log_err(info->keymap->ctx,
- "Unknown field encountered; "
- "Assigment to field %s ignored\n", field);
- return false;
- }
-
- if (arrayNdx != NULL) {
- log_err(info->keymap->ctx,
- "The %s setting is not an array; "
- "Illegal array reference ignored\n", field);
- return false;
- }
-
- if (ExprResolveKeyCode(info->keymap->ctx, stmt->value, &kc) == 0) {
- log_err(info->keymap->ctx,
- "Illegal keycode encountered; "
+ if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
+ log_err(info->ctx, "Unknown field encountered; "
"Assignment to field %s ignored\n", field);
return false;
}
- if (kc > XKB_KEYCODE_MAX) {
- log_err(info->keymap->ctx,
- "Illegal keycode %d (must be in the range %d-%d inclusive); "
- "Value of \"%s\" not changed\n",
- kc, 0, XKB_KEYCODE_MAX, field);
- return false;
- }
-
- if (which == MIN_KEYCODE_DEF) {
- if (info->explicitMax > 0 && info->explicitMax < kc) {
- log_err(info->keymap->ctx,
- "Minimum key code (%d) must be <= maximum key code (%d); "
- "Minimum key code value not changed\n",
- kc, info->explicitMax);
- return false;
- }
-
- if (info->computedMax > 0 && info->computedMin < kc) {
- log_err(info->keymap->ctx,
- "Minimum key code (%d) must be <= lowest defined key (%d); "
- "Minimum key code value not changed\n",
- kc, info->computedMin);
- return false;
- }
-
- info->explicitMin = kc;
- }
-
- if (which == MAX_KEYCODE_DEF) {
- if (info->explicitMin > 0 && info->explicitMin > kc) {
- log_err(info->keymap->ctx,
- "Maximum code (%d) must be >= minimum key code (%d); "
- "Maximum code value not changed\n",
- kc, info->explicitMin);
- return false;
- }
-
- if (info->computedMax > 0 && info->computedMax > kc) {
- log_err(info->keymap->ctx,
- "Maximum code (%d) must be >= highest defined key (%d); "
- "Maximum code value not changed\n",
- kc, info->computedMax);
- return false;
- }
-
- info->explicitMax = kc;
- }
-
+ /* We ignore explicit min/max statements, we always use computed. */
return true;
}
-static int
-HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
- enum merge_mode merge)
+static bool
+HandleLedNameDef(KeyNamesInfo *info, LedNameDef *def,
+ enum merge_mode merge)
{
- IndicatorNameInfo ii;
- const char *str;
+ LedNameInfo ledi;
+ xkb_atom_t name;
- if (def->ndx < 1 || def->ndx > XkbNumIndicators) {
+ if (def->ndx < 1 || def->ndx > XKB_MAX_LEDS) {
info->errorCount++;
- log_err(info->keymap->ctx,
- "Name specified for illegal indicator index %d\n; Ignored\n",
- def->ndx);
+ log_err(info->ctx,
+ "Illegal indicator index (%d) specified; must be between 1 .. %d; "
+ "Ignored\n", def->ndx, XKB_MAX_LEDS);
return false;
}
- InitIndicatorNameInfo(&ii, info);
- ii.ndx = def->ndx;
-
- if (!ExprResolveString(info->keymap->ctx, def->name, &str)) {
+ if (!ExprResolveString(info->ctx, def->name, &name)) {
char buf[20];
snprintf(buf, sizeof(buf), "%d", def->ndx);
info->errorCount++;
- return ReportBadType(info->keymap, "indicator", "name", buf,
- "string");
+ return ReportBadType(info->ctx, "indicator", "name", buf, "string");
}
- ii.name = xkb_atom_intern(info->keymap->ctx, str);
- ii.virtual = def->virtual;
-
- return AddIndicatorName(info, merge, &ii);
+ ledi.merge = merge;
+ ledi.name = name;
+ return AddLedName(info, merge, true, &ledi, def->ndx - 1);
}
-/**
- * Handle the xkb_keycodes section of a xkb file.
- * All information about parsed keys is stored in the info struct.
- *
- * Such a section may have include statements, in which case this function is
- * semi-recursive (it calls HandleIncludeKeycodes, which may call
- * HandleKeycodesFile again).
- *
- * @param info Struct to contain the fully parsed key information.
- * @param file The input file (parsed xkb_keycodes section)
- * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
- */
static void
HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
{
- ParseCommon *stmt;
+ bool ok;
free(info->name);
info->name = strdup_safe(file->name);
- stmt = file->defs;
- while (stmt)
- {
+
+ for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
switch (stmt->type) {
- case STMT_INCLUDE: /* e.g. include "evdev+aliases(qwerty)" */
- if (!HandleIncludeKeycodes(info, (IncludeStmt *) stmt))
- info->errorCount++;
+ case STMT_INCLUDE:
+ ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
break;
- case STMT_KEYCODE: /* e.g. <ESC> = 9; */
- if (!HandleKeycodeDef(info, (KeycodeDef *) stmt, merge))
- info->errorCount++;
+ case STMT_KEYCODE:
+ ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
break;
- case STMT_ALIAS: /* e.g. alias <MENU> = <COMP>; */
- if (!HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
- info->file_id))
- info->errorCount++;
+ case STMT_ALIAS:
+ ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge);
break;
- case STMT_VAR: /* e.g. minimum, maximum */
- if (!HandleKeyNameVar(info, (VarDef *) stmt))
- info->errorCount++;
+ case STMT_VAR:
+ ok = HandleKeyNameVar(info, (VarDef *) stmt);
break;
- case STMT_INDICATOR_NAME: /* e.g. indicator 1 = "Caps Lock"; */
- if (!HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
- merge))
- info->errorCount++;
- break;
- case STMT_INTERP:
- case STMT_VMOD:
- log_err(info->keymap->ctx,
- "Keycode files may define key and indicator names only; "
- "Ignoring definition of %s\n",
- (stmt->type == STMT_INTERP ?
- "a symbol interpretation" : "virtual modifiers"));
- info->errorCount++;
+ case STMT_LED_NAME:
+ ok = HandleLedNameDef(info, (LedNameDef *) stmt, merge);
break;
default:
- log_wsgo(info->keymap->ctx,
- "Unexpected statement type %d in HandleKeycodesFile\n",
- stmt->type);
+ log_err(info->ctx,
+ "Keycode files may define key and indicator names only; "
+ "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 keycodes file \"%s\"\n",
+ log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
file->topName);
break;
}
}
}
-static int
-ApplyAliases(KeyNamesInfo *info)
+/***====================================================================***/
+
+static bool
+CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
+{
+ struct xkb_key *keys;
+ xkb_keycode_t min_key_code, max_key_code, kc;
+
+ min_key_code = info->min_key_code;
+ max_key_code = info->max_key_code;
+ /* If the keymap has no keys, let's just use the safest pair we know. */
+ if (min_key_code == XKB_KEYCODE_INVALID) {
+ min_key_code = 8;
+ max_key_code = 255;
+ }
+
+ keys = calloc(max_key_code + 1, sizeof(*keys));
+ if (!keys)
+ return false;
+
+ for (kc = min_key_code; kc <= max_key_code; kc++)
+ keys[kc].keycode = kc;
+
+ for (kc = info->min_key_code; kc <= info->max_key_code; kc++)
+ keys[kc].name = darray_item(info->key_names, kc);
+
+ keymap->min_key_code = min_key_code;
+ keymap->max_key_code = max_key_code;
+ keymap->keys = keys;
+ return true;
+}
+
+static bool
+CopyKeyAliasesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
{
- int i;
- struct xkb_key *key;
- struct xkb_key_alias *old, *a;
- AliasInfo *alias, *next;
- int nNew = 0, nOld;
- struct xkb_keymap *keymap = info->keymap;
-
- nOld = darray_size(keymap->key_aliases);
- old = &darray_item(keymap->key_aliases, 0);
-
- list_foreach(alias, &info->aliases, entry) {
- unsigned long lname;
-
- lname = KeyNameToLong(alias->real);
- key = FindNamedKey(keymap, lname, false, CreateKeyNames(keymap), 0);
- if (!key) {
- log_lvl(info->keymap->ctx, 5,
+ AliasInfo *alias;
+ unsigned i, num_key_aliases;
+ struct xkb_key_alias *key_aliases;
+
+ /*
+ * Do some sanity checking on the aliases. We can't do it before
+ * because keys and their aliases may be added out-of-order.
+ */
+ num_key_aliases = 0;
+ darray_foreach(alias, info->aliases) {
+ /* Check that ->real is a key. */
+ if (!XkbKeyByName(keymap, alias->real, false)) {
+ log_vrb(info->ctx, 5,
"Attempt to alias %s to non-existent key %s; Ignored\n",
- KeyNameText(alias->alias), KeyNameText(alias->real));
- alias->alias[0] = '\0';
+ KeyNameText(info->ctx, alias->alias),
+ KeyNameText(info->ctx, alias->real));
+ alias->real = XKB_ATOM_NONE;
continue;
}
- lname = KeyNameToLong(alias->alias);
- key = FindNamedKey(keymap, lname, false, false, 0);
- if (key) {
- log_lvl(info->keymap->ctx, 5,
+ /* Check that ->alias is not a key. */
+ if (XkbKeyByName(keymap, alias->alias, false)) {
+ log_vrb(info->ctx, 5,
"Attempt to create alias with the name of a real key; "
"Alias \"%s = %s\" ignored\n",
- KeyNameText(alias->alias), KeyNameText(alias->real));
- alias->alias[0] = '\0';
+ KeyNameText(info->ctx, alias->alias),
+ KeyNameText(info->ctx, alias->real));
+ alias->real = XKB_ATOM_NONE;
continue;
}
- nNew++;
-
- if (!old)
- continue;
-
- for (i = 0, a = old; i < nOld; i++, a++) {
- AliasInfo old_alias;
+ num_key_aliases++;
+ }
- if (strncmp(a->alias, alias->alias, XkbKeyNameLength) != 0)
- continue;
+ /* Copy key aliases. */
+ key_aliases = NULL;
+ if (num_key_aliases > 0) {
+ key_aliases = calloc(num_key_aliases, sizeof(*key_aliases));
+ if (!key_aliases)
+ return false;
+ }
- InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
- HandleAliasCollision(info, &old_alias, alias);
- memcpy(old_alias.real, a->real, XkbKeyNameLength);
- alias->alias[0] = '\0';
- nNew--;
- break;
+ i = 0;
+ darray_foreach(alias, info->aliases) {
+ if (alias->real != XKB_ATOM_NONE) {
+ key_aliases[i].alias = alias->alias;
+ key_aliases[i].real = alias->real;
+ i++;
}
}
- if (nNew == 0)
- goto out;
+ keymap->num_key_aliases = num_key_aliases;
+ keymap->key_aliases = key_aliases;
+ return true;
+}
+
+static bool
+CopyLedNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
+{
+ keymap->num_leds = info->num_led_names;
+ for (xkb_led_index_t idx = 0; idx < info->num_led_names; idx++) {
+ LedNameInfo *ledi = &info->led_names[idx];
- darray_resize0(keymap->key_aliases, nOld + nNew);
+ if (ledi->name == XKB_ATOM_NONE)
+ continue;
- a = &darray_item(keymap->key_aliases, nOld);
- list_foreach(alias, &info->aliases, entry) {
- if (alias->alias[0] != '\0') {
- strncpy(a->alias, alias->alias, XkbKeyNameLength);
- strncpy(a->real, alias->real, XkbKeyNameLength);
- a++;
- }
+ keymap->leds[idx].name = ledi->name;
}
-out:
- list_foreach_safe(alias, next, &info->aliases, entry)
- free(alias);
- list_init(&info->aliases);
return true;
}
-/**
- * Compile the xkb_keycodes section, parse it's output, return the results.
- *
- * @param file The parsed XKB file (may have include statements requiring
- * further parsing)
- * @param result The effective keycodes, as gathered from the file.
- * @param merge Merge strategy.
- *
- * @return true on success, false otherwise.
- */
+static bool
+CopyKeyNamesInfoToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
+{
+ /* This function trashes keymap on error, but that's OK. */
+ if (!CopyKeyNamesToKeymap(keymap, info) ||
+ !CopyKeyAliasesToKeymap(keymap, info) ||
+ !CopyLedNamesToKeymap(keymap, info))
+ return false;
+
+ keymap->keycodes_section_name = strdup_safe(info->name);
+ XkbEscapeMapName(keymap->keycodes_section_name);
+ return true;
+}
+
+/***====================================================================***/
+
bool
CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
enum merge_mode merge)
{
- xkb_keycode_t kc;
- KeyNamesInfo info; /* contains all the info after parsing */
- IndicatorNameInfo *ii;
+ KeyNamesInfo info;
- InitKeyNamesInfo(&info, keymap, file->id);
+ InitKeyNamesInfo(&info, keymap->ctx);
HandleKeycodesFile(&info, file, merge);
-
- /* all the keys are now stored in info */
-
if (info.errorCount != 0)
goto err_info;
- if (info.explicitMin > 0) /* if "minimum" statement was present */
- keymap->min_key_code = info.explicitMin;
- else
- keymap->min_key_code = info.computedMin;
-
- if (info.explicitMax > 0) /* if "maximum" statement was present */
- keymap->max_key_code = info.explicitMax;
- else
- keymap->max_key_code = info.computedMax;
-
- darray_resize0(keymap->keys, keymap->max_key_code + 1);
- for (kc = info.computedMin; kc <= info.computedMax; kc++)
- LongToKeyName(darray_item(info.names, kc),
- XkbKey(keymap, kc)->name);
-
- if (info.name)
- keymap->keycodes_section_name = strdup(info.name);
-
- list_foreach(ii, &info.leds, entry) {
- keymap->indicator_names[ii->ndx - 1] =
- xkb_atom_text(keymap->ctx, ii->name);
- }
-
- ApplyAliases(&info);
+ if (!CopyKeyNamesInfoToKeymap(keymap, &info))
+ goto err_info;
ClearKeyNamesInfo(&info);
return true;