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.
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 to the keyboard LED (a.k.a indicator) with the given index.
85 * The led may be referred by this name later in the compat section
88 * Effect on the keymap
89 * --------------------
90 * After all of the xkb_keycodes sections have been compiled, the
91 * following members of struct xkb_keymap are finalized:
92 * xkb_keycode_t min_key_code;
93 * xkb_keycode_t max_key_code;
94 * darray(struct xkb_key_alias) key_aliases;
95 * char *keycodes_section_name;
96 * The 'name' field of leds declared in xkb_keycodes:
97 * darray(struct xkb_led) leds;
98 * Further, the array of keys:
99 * struct xkb_key *keys;
100 * had been resized to its final size (i.e. all of the xkb_key objects are
101 * referable by their keycode). However the objects themselves do not
102 * contain any useful information besides the key name at this point.
106 enum merge_mode merge;
120 enum merge_mode merge;
129 enum merge_mode merge;
132 xkb_keycode_t min_key_code;
133 xkb_keycode_t max_key_code;
134 darray(KeyNameInfo) key_names;
135 darray(LedNameInfo) led_names;
136 darray(AliasInfo) aliases;
138 struct xkb_context *ctx;
141 /***====================================================================***/
144 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
145 xkb_atom_t alias, xkb_atom_t real)
147 memset(info, 0, sizeof(*info));
149 info->file_id = file_id;
155 FindLedByName(KeyNamesInfo *info, xkb_atom_t name,
156 xkb_led_index_t *idx_out)
161 darray_enumerate(idx, ledi, info->led_names) {
162 if (ledi->name == name) {
172 AddLedName(KeyNamesInfo *info, enum merge_mode merge,
173 LedNameInfo *new, xkb_led_index_t new_idx)
175 xkb_led_index_t old_idx;
177 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
178 const bool replace = (merge == MERGE_REPLACE || merge == MERGE_OVERRIDE);
180 /* Inidicator with the same name already exists. */
181 old = FindLedByName(info, new->name, &old_idx);
183 const bool report = ((old->file_id == new->file_id && verbosity > 0) ||
186 if (old_idx == new_idx) {
188 "Multiple indicators named \"%s\"; "
189 "Identical definitions ignored\n",
190 xkb_atom_text(info->ctx, new->name));
195 xkb_led_index_t use = (replace ? new_idx + 1 : old_idx + 1);
196 xkb_led_index_t ignore = (replace ? old_idx + 1 : new_idx + 1);
198 "Multiple indicators named %s; Using %d, ignoring %d\n",
199 xkb_atom_text(info->ctx, new->name), use, ignore);
208 if (new_idx >= darray_size(info->led_names))
209 darray_resize0(info->led_names, new_idx + 1);
211 /* Inidicator with the same index already exists. */
212 old = &darray_item(info->led_names, new_idx);
213 if (old->name != XKB_ATOM_NONE) {
214 const bool report = ((old->file_id == new->file_id && verbosity > 0) ||
217 /* Same name case already handled above. */
220 const xkb_atom_t use = (replace ? new->name : old->name);
221 const xkb_atom_t ignore = (replace ? old->name : new->name);
222 log_warn(info->ctx, "Multiple names for indicator %d; "
223 "Using %s, ignoring %s\n", new_idx + 1,
224 xkb_atom_text(info->ctx, use),
225 xkb_atom_text(info->ctx, ignore));
234 darray_item(info->led_names, new_idx) = *new;
239 ClearKeyNamesInfo(KeyNamesInfo *info)
242 darray_free(info->key_names);
243 darray_free(info->aliases);
244 darray_free(info->led_names);
248 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
251 memset(info, 0, sizeof(*info));
253 info->merge = MERGE_DEFAULT;
254 info->file_id = file_id;
255 info->min_key_code = XKB_KEYCODE_MAX;
259 FindKeyByName(KeyNamesInfo * info, xkb_atom_t name)
263 for (i = info->min_key_code; i <= info->max_key_code; i++)
264 if (darray_item(info->key_names, i).name == name)
267 return XKB_KEYCODE_INVALID;
271 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name,
272 enum merge_mode merge, unsigned file_id, bool report)
276 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
278 if (kc >= darray_size(info->key_names))
279 darray_resize0(info->key_names, kc + 1);
281 info->min_key_code = MIN(info->min_key_code, kc);
282 info->max_key_code = MAX(info->max_key_code, kc);
284 namei = &darray_item(info->key_names, kc);
286 report = report && ((verbosity > 0 && file_id == namei->file_id) ||
289 if (namei->name != 0) {
290 const char *lname = KeyNameText(info->ctx, namei->name);
291 const char *kname = KeyNameText(info->ctx, name);
293 if (namei->name == name) {
296 "Multiple identical key name definitions; "
297 "Later occurences of \"%s = %d\" ignored\n",
301 else if (merge == MERGE_AUGMENT) {
304 "Multiple names for keycode %d; "
305 "Using %s, ignoring %s\n", kc, lname, kname);
311 "Multiple names for keycode %d; "
312 "Using %s, ignoring %s\n", kc, kname, lname);
318 old = FindKeyByName(info, name);
319 if (old != XKB_KEYCODE_INVALID && old != kc) {
320 const char *kname = KeyNameText(info->ctx, name);
322 if (merge == MERGE_OVERRIDE) {
323 darray_item(info->key_names, old).name = 0;
324 darray_item(info->key_names, old).file_id = 0;
327 "Key name %s assigned to multiple keys; "
328 "Using %d, ignoring %d\n", kname, kc, old);
332 log_vrb(info->ctx, 3,
333 "Key name %s assigned to multiple keys; "
334 "Using %d, ignoring %d\n", kname, old, kc);
340 namei->file_id = file_id;
344 /***====================================================================***/
347 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
351 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
356 if (darray_empty(from->aliases))
359 if (darray_empty(into->aliases)) {
360 into->aliases = from->aliases;
361 darray_init(from->aliases);
365 memset(&def, 0, sizeof(def));
367 darray_foreach(alias, from->aliases) {
368 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
369 def.alias = alias->alias;
370 def.real = alias->real;
372 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
380 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
381 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 darray_enumerate(idx, ledi, from->led_names) {
410 if (ledi->name == XKB_ATOM_NONE)
413 ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
414 if (!AddLedName(into, ledi->merge, ledi, idx))
418 if (!MergeAliases(into, from, merge))
423 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
426 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
428 enum merge_mode merge = MERGE_DEFAULT;
430 KeyNamesInfo included, next_incl;
432 InitKeyNamesInfo(&included, info->ctx, info->file_id);
435 included.name = stmt->stmt;
439 for (; stmt; stmt = stmt->next_incl) {
440 if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
442 info->errorCount += 10;
443 ClearKeyNamesInfo(&included);
447 InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
449 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
451 MergeIncludedKeycodes(&included, &next_incl, merge);
453 ClearKeyNamesInfo(&next_incl);
457 MergeIncludedKeycodes(info, &included, merge);
458 ClearKeyNamesInfo(&included);
460 return (info->errorCount == 0);
464 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
466 if (stmt->merge != MERGE_DEFAULT) {
467 if (stmt->merge == MERGE_REPLACE)
468 merge = MERGE_OVERRIDE;
473 if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) {
475 "Illegal keycode %lld: must be between 0..%u; "
476 "Key ignored\n", (long long) stmt->value, XKB_KEYCODE_MAX);
480 return AddKeyName(info, stmt->value, stmt->name, merge,
481 info->file_id, true);
485 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
487 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
488 const bool report = ((new->file_id == old->file_id && verbosity > 0) ||
491 if (new->real == old->real) {
493 log_warn(info->ctx, "Alias of %s for %s declared more than once; "
494 "First definition ignored\n",
495 KeyNameText(info->ctx, new->alias),
496 KeyNameText(info->ctx, new->real));
499 xkb_atom_t use, ignore;
501 use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
502 ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
505 log_warn(info->ctx, "Multiple definitions for alias %s; "
506 "Using %s, ignoring %s\n",
507 KeyNameText(info->ctx, old->alias),
508 KeyNameText(info->ctx, use),
509 KeyNameText(info->ctx, ignore));
514 old->file_id = new->file_id;
515 old->merge = new->merge;
519 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
522 AliasInfo *alias, new;
524 darray_foreach(alias, info->aliases) {
525 if (alias->alias == def->alias) {
526 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
527 HandleAliasCollision(info, alias, &new);
532 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
533 darray_append(info->aliases, new);
538 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
540 const char *elem, *field;
543 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
547 log_err(info->ctx, "Unknown element %s encountered; "
548 "Default for field %s ignored\n", elem, field);
552 if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
553 log_err(info->ctx, "Unknown field encountered; "
554 "Assignment to field %s ignored\n", field);
558 /* We ignore explicit min/max statements, we always use computed. */
563 HandleLedNameDef(KeyNamesInfo *info, LedNameDef *def,
564 enum merge_mode merge)
569 if (def->ndx < 1 || def->ndx > XKB_MAX_LEDS) {
572 "Illegal indicator index (%d) specified; must be between 1 .. %d; "
573 "Ignored\n", def->ndx, XKB_MAX_LEDS);
577 if (!ExprResolveString(info->ctx, def->name, &name)) {
579 snprintf(buf, sizeof(buf), "%d", def->ndx);
581 return ReportBadType(info->ctx, "indicator", "name", buf, "string");
584 ledi.merge = info->merge;
585 ledi.file_id = info->file_id;
587 return AddLedName(info, merge, &ledi, def->ndx - 1);
591 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
596 info->name = strdup_safe(file->name);
598 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
599 switch (stmt->type) {
601 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
604 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
607 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
611 ok = HandleKeyNameVar(info, (VarDef *) stmt);
614 ok = HandleLedNameDef(info, (LedNameDef *) stmt, merge);
618 "Keycode files may define key and indicator names only; "
619 "Ignoring %s\n", stmt_type_to_string(stmt->type));
627 if (info->errorCount > 10) {
628 log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
635 /***====================================================================***/
638 CopyAliasesToKeymap(KeyNamesInfo *info, struct xkb_keymap *keymap)
641 struct xkb_key_alias *a, new;
644 darray_foreach(alias, info->aliases) {
645 /* Check that ->real is a key. */
646 key = FindNamedKey(keymap, alias->real, false);
648 log_vrb(info->ctx, 5,
649 "Attempt to alias %s to non-existent key %s; Ignored\n",
650 KeyNameText(info->ctx, alias->alias),
651 KeyNameText(info->ctx, alias->real));
655 /* Check that ->alias is not a key. */
656 key = FindNamedKey(keymap, alias->alias, false);
658 log_vrb(info->ctx, 5,
659 "Attempt to create alias with the name of a real key; "
660 "Alias \"%s = %s\" ignored\n",
661 KeyNameText(info->ctx, alias->alias),
662 KeyNameText(info->ctx, alias->real));
666 /* Check that ->alias in not already an alias, and if so handle it. */
667 darray_foreach(a, keymap->key_aliases) {
670 if (a->alias != alias->alias)
673 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
674 HandleAliasCollision(info, &old_alias, alias);
675 a->alias = old_alias.alias;
676 a->real = old_alias.real;
679 if (alias->alias == 0)
683 new.alias = alias->alias;
684 new.real = alias->real;
685 darray_append(keymap->key_aliases, new);
688 darray_free(info->aliases);
692 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
698 keymap->keys = calloc(info->max_key_code + 1, sizeof(*keymap->keys));
702 keymap->min_key_code = info->min_key_code;
703 keymap->max_key_code = info->max_key_code;
705 for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
706 keymap->keys[kc].keycode = kc;
707 keymap->keys[kc].name = darray_item(info->key_names, kc).name;
710 keymap->keycodes_section_name = strdup_safe(info->name);
712 darray_resize0(keymap->leds, darray_size(info->led_names));
713 darray_enumerate(idx, ledi, info->led_names) {
714 if (ledi->name == XKB_ATOM_NONE)
717 darray_item(keymap->leds, idx).name = ledi->name;
720 CopyAliasesToKeymap(info, keymap);
725 /***====================================================================***/
728 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
729 enum merge_mode merge)
733 InitKeyNamesInfo(&info, keymap->ctx, file->id);
735 HandleKeycodesFile(&info, file, merge);
736 if (info.errorCount != 0)
739 if (!CopyKeyNamesToKeymap(keymap, &info))
742 ClearKeyNamesInfo(&info);
746 ClearKeyNamesInfo(&info);
750 /***====================================================================***/
753 FindNamedKey(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
757 xkb_foreach_key(key, keymap)
758 if (key->name == name)
763 if (FindKeyNameForAlias(keymap, name, &new_name))
764 return FindNamedKey(keymap, new_name, false);
771 FindKeyNameForAlias(struct xkb_keymap *keymap, xkb_atom_t name,
772 xkb_atom_t *real_name)
774 const struct xkb_key_alias *a;
776 darray_foreach(a, keymap->key_aliases) {
777 if (name == a->alias) {
778 *real_name = a->real;