1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
27 #include "xkbcomp-priv.h"
34 * The xkb_keycodes section
35 * ========================
37 * This is the simplest section type, and is the first one to be
38 * compiled. The purpose of this is mostly to map between the
39 * hardware/evdev scancodes and xkb keycodes. Each key is given a name
40 * by which it can be referred to later, e.g. in the symbols section.
44 * Statements of the form:
48 * The above would let 49 and 10 be valid keycodes in the keymap, and
49 * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
50 * always used to refer to a key by name.
52 * [ The naming convention <AE01> just denoted the position of the key
53 * in the main alphanumric section of the keyboard, with the two letters
54 * specifying the row and the two digits specifying the column, from
57 * In the common case this just maps to the evdev scancodes from
58 * /usr/include/linux/input.h, e.g. the following definitions:
59 * #define KEY_GRAVE 41
61 * Similar definitions appear in the xf86-input-keyboard driver. Note
62 * that in all current keymaps there's a constant offset of 8 (for
63 * historical reasons).
65 * If there's a conflict, like the same name given to different keycodes,
66 * or same keycode given different names, it is resolved according to the
67 * merge mode which applies to the definitions.
71 * Statements of the form:
72 * alias <MENU> = <COMP>;
74 * Allows to refer to a previously defined key (here <COMP>) by another
75 * name (here <MENU>). Conflicts are handled similarly.
77 * Indicator name statements
78 * -------------------------
79 * Statements of the form:
80 * indicator 1 = "Caps Lock";
81 * indicator 2 = "Num Lock";
82 * indicator 3 = "Scroll Lock";
84 * Assigns a name the indicator (i.e. keyboard LED) with the given index.
85 * The amount of possible indicators is predetermined (XKB_NUM_INDICATORS).
86 * The indicator may be referred by this name later in the compat section
89 * Effect on the keymap
90 * --------------------
91 * After all of the xkb_keycodes sections have been compiled, the
92 * following members of struct xkb_keymap are finalized:
93 * xkb_keycode_t min_key_code;
94 * xkb_keycode_t max_key_code;
95 * darray(struct xkb_key_alias) key_aliases;
96 * char *keycodes_section_name;
97 * The 'name' field of indicators declared in xkb_keycodes:
98 * struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
99 * Further, the array of keys:
100 * darray(struct xkb_key) keys;
101 * had been resized to its final size (i.e. all of the xkb_key objects are
102 * referable by their keycode). However the objects themselves do not
103 * contain any useful information besides the key name at this point.
106 typedef struct _AliasInfo {
107 enum merge_mode merge;
115 unsigned int file_id;
119 typedef struct _IndicatorNameInfo {
120 enum merge_mode merge;
126 typedef struct _KeyNamesInfo {
127 char *name; /* e.g. evdev+aliases(qwerty) */
130 enum merge_mode merge;
132 xkb_keycode_t min_key_code;
133 xkb_keycode_t max_key_code;
134 darray(KeyNameInfo) key_names;
135 IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
136 darray(AliasInfo) aliases;
138 struct xkb_context *ctx;
142 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
143 xkb_atom_t alias, xkb_atom_t real)
145 memset(info, 0, sizeof(*info));
147 info->file_id = file_id;
152 static IndicatorNameInfo *
153 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
154 xkb_led_index_t *idx_out)
158 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
159 if (info->indicator_names[idx].name == name) {
161 return &info->indicator_names[idx];
169 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
170 IndicatorNameInfo *new, xkb_led_index_t new_idx)
172 xkb_led_index_t old_idx;
173 IndicatorNameInfo *old;
174 bool replace, report;
175 int verbosity = xkb_context_get_log_verbosity(info->ctx);
177 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
179 old = FindIndicatorByName(info, new->name, &old_idx);
181 report = ((old->file_id == new->file_id && verbosity > 0) ||
184 if (old_idx == new_idx) {
186 log_warn(info->ctx, "Multiple indicators named %s; "
187 "Identical definitions ignored\n",
188 xkb_atom_text(info->ctx, new->name));
193 log_warn(info->ctx, "Multiple indicators named %s; "
194 "Using %d, ignoring %d\n",
195 xkb_atom_text(info->ctx, new->name),
196 (replace ? old_idx + 1 : new_idx + 1),
197 (replace ? new_idx + 1 : old_idx + 1));
200 * XXX: If in the next check we ignore new, than we will have
201 * deleted this old for nothing!
204 memset(old, 0, sizeof(*old));
207 old = &info->indicator_names[new_idx];
208 if (old->name != XKB_ATOM_NONE) {
209 report = ((old->file_id == new->file_id && verbosity > 0) ||
212 if (old->name == new->name) {
214 log_warn(info->ctx, "Multiple names for indicator %d; "
215 "Identical definitions ignored\n", new_idx + 1);
219 log_warn(info->ctx, "Multiple names for indicator %d; "
220 "Using %s, ignoring %s\n", new_idx + 1,
221 xkb_atom_text(info->ctx, new->name),
222 xkb_atom_text(info->ctx, old->name));
223 old->name = new->name;
227 log_warn(info->ctx, "Multiple names for indicator %d; "
228 "Using %s, ignoring %s\n", new_idx + 1,
229 xkb_atom_text(info->ctx, old->name),
230 xkb_atom_text(info->ctx, new->name));
236 info->indicator_names[new_idx] = *new;
241 ClearKeyNamesInfo(KeyNamesInfo *info)
244 darray_free(info->key_names);
245 darray_free(info->aliases);
249 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
252 memset(info, 0, sizeof(*info));
254 info->merge = MERGE_DEFAULT;
255 info->file_id = file_id;
256 info->min_key_code = XKB_KEYCODE_MAX;
260 FindKeyByName(KeyNamesInfo * info, xkb_atom_t name)
264 for (i = info->min_key_code; i <= info->max_key_code; i++)
265 if (darray_item(info->key_names, i).name == name)
268 return XKB_KEYCODE_INVALID;
272 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name,
273 enum merge_mode merge, unsigned file_id, bool report)
277 int verbosity = xkb_context_get_log_verbosity(info->ctx);
279 if (kc >= darray_size(info->key_names))
280 darray_resize0(info->key_names, kc + 1);
282 info->min_key_code = MIN(info->min_key_code, kc);
283 info->max_key_code = MAX(info->max_key_code, kc);
285 namei = &darray_item(info->key_names, kc);
287 report = report && ((verbosity > 0 && file_id == namei->file_id) ||
290 if (namei->name != 0) {
291 const char *lname = KeyNameText(info->ctx, namei->name);
292 const char *kname = KeyNameText(info->ctx, name);
294 if (namei->name == name) {
297 "Multiple identical key name definitions; "
298 "Later occurences of \"%s = %d\" ignored\n",
302 else if (merge == MERGE_AUGMENT) {
305 "Multiple names for keycode %d; "
306 "Using %s, ignoring %s\n", kc, lname, kname);
312 "Multiple names for keycode %d; "
313 "Using %s, ignoring %s\n", kc, kname, lname);
319 old = FindKeyByName(info, name);
320 if (old != XKB_KEYCODE_INVALID && old != kc) {
321 const char *kname = KeyNameText(info->ctx, name);
323 if (merge == MERGE_OVERRIDE) {
324 darray_item(info->key_names, old).name = 0;
325 darray_item(info->key_names, old).file_id = 0;
328 "Key name %s assigned to multiple keys; "
329 "Using %d, ignoring %d\n", kname, kc, old);
333 log_vrb(info->ctx, 3,
334 "Key name %s assigned to multiple keys; "
335 "Using %d, ignoring %d\n", kname, old, kc);
341 namei->file_id = file_id;
345 /***====================================================================***/
348 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
352 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
357 if (darray_empty(from->aliases))
360 if (darray_empty(into->aliases)) {
361 into->aliases = from->aliases;
362 darray_init(from->aliases);
366 memset(&def, 0, sizeof(def));
368 darray_foreach(alias, from->aliases) {
369 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
370 def.alias = alias->alias;
371 def.real = alias->real;
373 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
381 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
382 enum merge_mode merge)
387 if (from->errorCount > 0) {
388 into->errorCount += from->errorCount;
392 if (into->name == NULL) {
393 into->name = from->name;
397 if (darray_size(into->key_names) < darray_size(from->key_names))
398 darray_resize0(into->key_names, darray_size(from->key_names));
400 for (i = from->min_key_code; i <= from->max_key_code; i++) {
401 xkb_atom_t name = darray_item(from->key_names, i).name;
402 if (name == XKB_ATOM_NONE)
405 if (!AddKeyName(into, i, name, merge, from->file_id, false))
409 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
410 IndicatorNameInfo *led = &from->indicator_names[idx];
411 if (led->name == XKB_ATOM_NONE)
414 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
415 if (!AddIndicatorName(into, led->merge, led, idx))
419 if (!MergeAliases(into, from, merge))
424 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
427 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
429 enum merge_mode merge = MERGE_DEFAULT;
431 KeyNamesInfo included, next_incl;
433 InitKeyNamesInfo(&included, info->ctx, info->file_id);
436 included.name = stmt->stmt;
440 for (; stmt; stmt = stmt->next_incl) {
441 if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
443 info->errorCount += 10;
444 ClearKeyNamesInfo(&included);
448 InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
450 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
452 MergeIncludedKeycodes(&included, &next_incl, merge);
454 ClearKeyNamesInfo(&next_incl);
458 MergeIncludedKeycodes(info, &included, merge);
459 ClearKeyNamesInfo(&included);
461 return (info->errorCount == 0);
465 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
467 if (stmt->merge != MERGE_DEFAULT) {
468 if (stmt->merge == MERGE_REPLACE)
469 merge = MERGE_OVERRIDE;
474 if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) {
476 "Illegal keycode %lld: must be between 0..%u; "
477 "Key ignored\n", stmt->value, XKB_KEYCODE_MAX);
481 return AddKeyName(info, stmt->value, stmt->name, merge,
482 info->file_id, true);
486 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
488 int verbosity = xkb_context_get_log_verbosity(info->ctx);
489 bool report = ((new->file_id == old->file_id && verbosity > 0) ||
492 if (new->real == old->real) {
494 log_warn(info->ctx, "Alias of %s for %s declared more than once; "
495 "First definition ignored\n",
496 KeyNameText(info->ctx, new->alias),
497 KeyNameText(info->ctx, new->real));
500 xkb_atom_t use, ignore;
502 use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
503 ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
506 log_warn(info->ctx, "Multiple definitions for alias %s; "
507 "Using %s, ignoring %s\n",
508 KeyNameText(info->ctx, old->alias),
509 KeyNameText(info->ctx, use),
510 KeyNameText(info->ctx, ignore));
515 old->file_id = new->file_id;
516 old->merge = new->merge;
520 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
523 AliasInfo *alias, new;
525 darray_foreach(alias, info->aliases) {
526 if (alias->alias == def->alias) {
527 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
528 HandleAliasCollision(info, alias, &new);
533 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
534 darray_append(info->aliases, new);
539 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
541 const char *elem, *field;
544 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
548 log_err(info->ctx, "Unknown element %s encountered; "
549 "Default for field %s ignored\n", elem, field);
553 if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
554 log_err(info->ctx, "Unknown field encountered; "
555 "Assigment to field %s ignored\n", field);
559 /* We ignore explicit min/max statements, we always use computed. */
564 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
565 enum merge_mode merge)
567 IndicatorNameInfo ii;
570 if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
573 "Name specified for illegal indicator index %d\n; Ignored\n",
578 if (!ExprResolveString(info->ctx, def->name, &name)) {
580 snprintf(buf, sizeof(buf), "%d", def->ndx);
582 return ReportBadType(info->ctx, "indicator", "name", buf,
586 ii.merge = info->merge;
587 ii.file_id = info->file_id;
589 return AddIndicatorName(info, merge, &ii, def->ndx - 1);
593 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
599 info->name = strdup_safe(file->name);
601 for (stmt = file->defs; stmt; stmt = stmt->next) {
602 switch (stmt->type) {
604 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
607 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
610 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
614 ok = HandleKeyNameVar(info, (VarDef *) stmt);
616 case STMT_INDICATOR_NAME:
617 ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
622 "Keycode files may define key and indicator names only; "
623 "Ignoring %s\n", stmt_type_to_string(stmt->type));
631 if (info->errorCount > 10) {
632 log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
640 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
643 struct xkb_key_alias *a, new;
646 darray_foreach(alias, info->aliases) {
647 /* Check that ->real is a key. */
648 key = FindNamedKey(keymap, alias->real, false);
650 log_vrb(info->ctx, 5,
651 "Attempt to alias %s to non-existent key %s; Ignored\n",
652 KeyNameText(info->ctx, alias->alias),
653 KeyNameText(info->ctx, alias->real));
657 /* Check that ->alias is not a key. */
658 key = FindNamedKey(keymap, alias->alias, false);
660 log_vrb(info->ctx, 5,
661 "Attempt to create alias with the name of a real key; "
662 "Alias \"%s = %s\" ignored\n",
663 KeyNameText(info->ctx, alias->alias),
664 KeyNameText(info->ctx, alias->real));
668 /* Check that ->alias in not already an alias, and if so handle it. */
669 darray_foreach(a, keymap->key_aliases) {
672 if (a->alias != alias->alias)
675 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
676 HandleAliasCollision(info, &old_alias, alias);
677 a->alias = old_alias.alias;
678 a->real = old_alias.real;
681 if (alias->alias == 0)
685 new.alias = alias->alias;
686 new.real = alias->real;
687 darray_append(keymap->key_aliases, new);
690 darray_free(info->aliases);
694 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
699 keymap->min_key_code = info->min_key_code;
700 keymap->max_key_code = info->max_key_code;
702 darray_resize0(keymap->keys, keymap->max_key_code + 1);
703 for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
704 struct xkb_key *key = &darray_item(keymap->keys, kc);
706 key->name = darray_item(info->key_names, kc).name;
709 keymap->keycodes_section_name = strdup_safe(info->name);
711 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
712 IndicatorNameInfo *led = &info->indicator_names[idx];
713 if (led->name == XKB_ATOM_NONE)
716 keymap->indicators[idx].name = led->name;
719 ApplyAliases(info, keymap);
725 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
726 enum merge_mode merge)
730 InitKeyNamesInfo(&info, keymap->ctx, file->id);
732 HandleKeycodesFile(&info, file, merge);
733 if (info.errorCount != 0)
736 if (!CopyKeyNamesToKeymap(keymap, &info))
739 ClearKeyNamesInfo(&info);
743 ClearKeyNamesInfo(&info);
748 FindNamedKey(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
752 xkb_foreach_key(key, keymap)
753 if (key->name == name)
758 if (FindKeyNameForAlias(keymap, name, &new_name))
759 return FindNamedKey(keymap, new_name, false);
766 FindKeyNameForAlias(struct xkb_keymap *keymap, xkb_atom_t name,
767 xkb_atom_t *real_name)
769 struct xkb_key_alias *a;
771 darray_foreach(a, keymap->key_aliases) {
772 if (name == a->alias) {
773 *real_name = a->real;