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 * of up to 4 letters, by which it can be referred to later, e.g. in the
45 * Statements of the form:
49 * The above would let 49 and 10 be valid keycodes in the keymap, and
50 * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
51 * always used to refer to a key by name.
53 * [ The naming convention <AE01> just denoted the position of the key
54 * in the main alphanumric section of the keyboard, with the top left key
55 * (usually "~") acting as origin. <AE01> is the key in the first row
56 * second column (which is usually "1"). ]
58 * In the common case this just maps to the evdev scancodes from
59 * /usr/include/linux/input.h, e.g. the following definitions:
60 * #define KEY_GRAVE 41
62 * Similar definitions appear in the xf86-input-keyboard driver. Note
63 * that in all current keymaps there's a constant offset of 8 (for
64 * historical reasons).
66 * If there's a conflict, like the same name given to different keycodes,
67 * or same keycode given different names, it is resolved according to the
68 * merge mode which applies to the definitions.
70 * The reason for the 4 characters limit is that the name is sometimes
71 * converted to an unsigned long (in a direct mapping), instead of a char
72 * array (see KeyNameToLong, LongToKeyName).
76 * Statements of the form:
77 * alias <MENU> = <COMP>;
79 * Allows to refer to a previously defined key (here <COMP>) by another
80 * name (here <MENU>). Conflicts are handled similarly.
82 * Indicator name statements
83 * -------------------------
84 * Statements of the form:
85 * indicator 1 = "Caps Lock";
86 * indicator 2 = "Num Lock";
87 * indicator 3 = "Scroll Lock";
89 * Assigns a name the indicator (i.e. keyboard LED) with the given index.
90 * The amount of possible indicators is predetermined (XKB_NUM_INDICATORS).
91 * The indicator may be referred by this name later in the compat section
94 * Effect on the keymap
95 * --------------------
96 * After all of the xkb_keycodes sections have been compiled, the
97 * following members of struct xkb_keymap are finalized:
98 * xkb_keycode_t min_key_code;
99 * xkb_keycode_t max_key_code;
100 * darray(struct xkb_key_alias) key_aliases;
101 * char *keycodes_section_name;
102 * The 'name' field of indicators declared in xkb_keycodes:
103 * struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
104 * Further, the array of keys:
105 * darray(struct xkb_key) keys;
106 * had been resized to its final size (i.e. all of the xkb_key objects are
107 * referable by their keycode). However the objects themselves do not
108 * contain any useful information besides the key name at this point.
111 typedef struct _AliasInfo {
112 enum merge_mode merge;
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(unsigned long) names;
135 darray(unsigned int) files;
136 IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
137 darray(AliasInfo) aliases;
139 struct xkb_context *ctx;
143 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
145 if (newMax < darray_size(info->names))
148 darray_resize0(info->names, newMax + 1);
149 darray_resize0(info->files, newMax + 1);
153 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
154 char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
156 memset(info, 0, sizeof(*info));
158 info->file_id = file_id;
159 info->alias = KeyNameToLong(alias);
160 info->real = KeyNameToLong(real);
163 static IndicatorNameInfo *
164 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
165 xkb_led_index_t *idx_out)
169 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
170 if (info->indicator_names[idx].name == name) {
172 return &info->indicator_names[idx];
180 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
181 IndicatorNameInfo *new, xkb_led_index_t new_idx)
183 xkb_led_index_t old_idx;
184 IndicatorNameInfo *old;
185 bool replace, report;
186 int verbosity = xkb_get_log_verbosity(info->ctx);
188 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
190 old = FindIndicatorByName(info, new->name, &old_idx);
192 report = ((old->file_id == new->file_id && verbosity > 0) ||
195 if (old_idx == new_idx) {
197 log_warn(info->ctx, "Multiple indicators named %s; "
198 "Identical definitions ignored\n",
199 xkb_atom_text(info->ctx, new->name));
204 log_warn(info->ctx, "Multiple indicators named %s; "
205 "Using %d, ignoring %d\n",
206 xkb_atom_text(info->ctx, new->name),
207 (replace ? old_idx + 1 : new_idx + 1),
208 (replace ? new_idx + 1 : old_idx + 1));
211 * XXX: If in the next check we ignore new, than we will have
212 * deleted this old for nothing!
215 memset(old, 0, sizeof(*old));
218 old = &info->indicator_names[new_idx];
219 if (old->name != XKB_ATOM_NONE) {
220 report = ((old->file_id == new->file_id && verbosity > 0) ||
223 if (old->name == new->name) {
225 log_warn(info->ctx, "Multiple names for indicator %d; "
226 "Identical definitions ignored\n", new_idx + 1);
230 log_warn(info->ctx, "Multiple names for indicator %d; "
231 "Using %s, ignoring %s\n", new_idx + 1,
232 xkb_atom_text(info->ctx, new->name),
233 xkb_atom_text(info->ctx, old->name));
234 old->name = new->name;
238 log_warn(info->ctx, "Multiple names for indicator %d; "
239 "Using %s, ignoring %s\n", new_idx + 1,
240 xkb_atom_text(info->ctx, old->name),
241 xkb_atom_text(info->ctx, new->name));
247 info->indicator_names[new_idx] = *new;
252 ClearKeyNamesInfo(KeyNamesInfo *info)
255 darray_free(info->names);
256 darray_free(info->files);
257 darray_free(info->aliases);
261 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
264 memset(info, 0, sizeof(*info));
266 info->merge = MERGE_DEFAULT;
267 info->file_id = file_id;
268 info->min_key_code = XKB_KEYCODE_MAX;
272 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
276 for (i = info->min_key_code; i <= info->max_key_code; i++)
277 if (darray_item(info->names, i) == name)
284 * Store the name of the key as a long in the info struct under the given
285 * keycode. If the same keys is referred to twice, print a warning.
286 * Note that the key's name is stored as a long, the keycode is the index.
289 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, unsigned long name,
290 enum merge_mode merge, unsigned file_id, bool reportCollisions)
293 int verbosity = xkb_get_log_verbosity(info->ctx);
295 ResizeKeyNameArrays(info, kc);
297 info->min_key_code = MIN(info->min_key_code, kc);
298 info->max_key_code = MAX(info->max_key_code, kc);
300 if (reportCollisions)
301 reportCollisions = (verbosity > 7 ||
303 file_id == darray_item(info->files, kc)));
305 if (darray_item(info->names, kc) != 0) {
306 const char *lname = LongKeyNameText(darray_item(info->names, kc));
307 const char *kname = LongKeyNameText(name);
309 if (darray_item(info->names, kc) == name && reportCollisions) {
310 log_warn(info->ctx, "Multiple identical key name definitions; "
311 "Later occurences of \"%s = %d\" ignored\n", lname, kc);
315 if (merge == MERGE_AUGMENT) {
316 if (reportCollisions)
317 log_warn(info->ctx, "Multiple names for keycode %d; "
318 "Using %s, ignoring %s\n", kc, lname, kname);
322 if (reportCollisions)
323 log_warn(info->ctx, "Multiple names for keycode %d; "
324 "Using %s, ignoring %s\n", kc, kname, lname);
325 darray_item(info->names, kc) = 0;
326 darray_item(info->files, kc) = 0;
330 old = FindKeyByLong(info, name);
331 if (old != 0 && old != kc) {
332 const char *kname = LongKeyNameText(name);
334 if (merge == MERGE_OVERRIDE) {
335 darray_item(info->names, old) = 0;
336 darray_item(info->files, old) = 0;
337 if (reportCollisions)
338 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
339 "Using %d, ignoring %d\n", kname, kc, old);
342 if (reportCollisions && verbosity > 3)
343 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
344 "Using %d, ignoring %d\n", kname, old, kc);
349 darray_item(info->names, kc) = name;
350 darray_item(info->files, kc) = file_id;
354 /***====================================================================***/
357 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
361 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
366 if (darray_empty(from->aliases))
369 if (darray_empty(into->aliases)) {
370 into->aliases = from->aliases;
371 darray_init(from->aliases);
375 memset(&def, 0, sizeof(def));
377 darray_foreach(alias, from->aliases) {
378 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
379 LongToKeyName(alias->alias, def.alias);
380 LongToKeyName(alias->real, def.real);
382 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
390 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
391 enum merge_mode merge)
396 if (from->errorCount > 0) {
397 into->errorCount += from->errorCount;
401 if (into->name == NULL) {
402 into->name = from->name;
406 ResizeKeyNameArrays(into, from->max_key_code);
408 for (i = from->min_key_code; i <= from->max_key_code; i++) {
409 unsigned long name = darray_item(from->names, i);
413 if (!AddKeyName(into, i, name, merge, from->file_id, false))
417 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
418 IndicatorNameInfo *led = &from->indicator_names[idx];
419 if (led->name == XKB_ATOM_NONE)
422 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
423 if (!AddIndicatorName(into, led->merge, led, idx))
427 if (!MergeAliases(into, from, merge))
432 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
435 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
437 enum merge_mode merge = MERGE_DEFAULT;
439 KeyNamesInfo included, next_incl;
441 InitKeyNamesInfo(&included, info->ctx, info->file_id);
444 included.name = stmt->stmt;
448 for (; stmt; stmt = stmt->next_incl) {
449 if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
451 info->errorCount += 10;
452 ClearKeyNamesInfo(&included);
456 InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
458 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
460 MergeIncludedKeycodes(&included, &next_incl, merge);
462 ClearKeyNamesInfo(&next_incl);
466 MergeIncludedKeycodes(info, &included, merge);
467 ClearKeyNamesInfo(&included);
469 return (info->errorCount == 0);
473 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
475 if (stmt->merge != MERGE_DEFAULT) {
476 if (stmt->merge == MERGE_REPLACE)
477 merge = MERGE_OVERRIDE;
482 return AddKeyName(info, stmt->value, KeyNameToLong(stmt->name), merge,
483 info->file_id, true);
487 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
489 int verbosity = xkb_get_log_verbosity(info->ctx);
490 bool report = ((new->file_id == old->file_id && verbosity > 0) ||
493 if (new->real == old->real) {
495 log_warn(info->ctx, "Alias of %s for %s declared more than once; "
496 "First definition ignored\n",
497 LongKeyNameText(new->alias), LongKeyNameText(new->real));
500 unsigned long 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 LongKeyNameText(old->alias), LongKeyNameText(use),
509 LongKeyNameText(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 == KeyNameToLong(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 "Assigment to field %s ignored\n", field);
558 /* We ignore explicit min/max statements, we always use computed. */
563 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
564 enum merge_mode merge)
566 IndicatorNameInfo ii;
569 if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
572 "Name specified for illegal indicator index %d\n; Ignored\n",
577 if (!ExprResolveString(info->ctx, def->name, &name)) {
579 snprintf(buf, sizeof(buf), "%d", def->ndx);
581 return ReportBadType(info->ctx, "indicator", "name", buf,
585 ii.merge = info->merge;
586 ii.file_id = info->file_id;
588 return AddIndicatorName(info, merge, &ii, def->ndx - 1);
592 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
598 info->name = strdup_safe(file->name);
600 for (stmt = file->defs; stmt; stmt = stmt->next) {
601 switch (stmt->type) {
603 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
606 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
609 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
613 ok = HandleKeyNameVar(info, (VarDef *) stmt);
615 case STMT_INDICATOR_NAME:
616 ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
621 "Keycode files may define key and indicator names only; "
622 "Ignoring %s\n", stmt_type_to_string(stmt->type));
630 if (info->errorCount > 10) {
631 log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
639 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
642 struct xkb_key_alias *a, new;
645 darray_foreach(alias, info->aliases) {
646 /* Check that ->real is a key. */
647 key = FindNamedKey(keymap, alias->real, false);
649 log_vrb(info->ctx, 5,
650 "Attempt to alias %s to non-existent key %s; Ignored\n",
651 LongKeyNameText(alias->alias),
652 LongKeyNameText(alias->real));
656 /* Check that ->alias is not a key. */
657 key = FindNamedKey(keymap, alias->alias, false);
659 log_vrb(info->ctx, 5,
660 "Attempt to create alias with the name of a real key; "
661 "Alias \"%s = %s\" ignored\n",
662 LongKeyNameText(alias->alias),
663 LongKeyNameText(alias->real));
667 /* Check that ->alias in not already an alias, and if so handle it. */
668 darray_foreach(a, keymap->key_aliases) {
671 if (KeyNameToLong(a->alias) != alias->alias)
674 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
675 HandleAliasCollision(info, &old_alias, alias);
676 LongToKeyName(old_alias.alias, a->alias);
677 LongToKeyName(old_alias.real, a->real);
680 if (alias->alias == 0)
684 LongToKeyName(alias->alias, new.alias);
685 LongToKeyName(alias->real, new.real);
686 darray_append(keymap->key_aliases, new);
689 darray_free(info->aliases);
693 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
698 keymap->min_key_code = info->min_key_code;
699 keymap->max_key_code = info->max_key_code;
701 darray_resize0(keymap->keys, keymap->max_key_code + 1);
702 for (kc = info->min_key_code; kc <= info->max_key_code; kc++)
703 LongToKeyName(darray_item(info->names, kc),
704 darray_item(keymap->keys, kc).name);
706 keymap->keycodes_section_name = strdup_safe(info->name);
708 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
709 IndicatorNameInfo *led = &info->indicator_names[idx];
710 if (led->name == XKB_ATOM_NONE)
713 keymap->indicators[idx].name = led->name;
716 ApplyAliases(info, keymap);
722 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
723 enum merge_mode merge)
727 InitKeyNamesInfo(&info, keymap->ctx, file->id);
729 HandleKeycodesFile(&info, file, merge);
730 if (info.errorCount != 0)
733 if (!CopyKeyNamesToKeymap(keymap, &info))
736 ClearKeyNamesInfo(&info);
740 ClearKeyNamesInfo(&info);
745 FindNamedKey(struct xkb_keymap *keymap, unsigned long name, bool use_aliases)
749 xkb_foreach_key(key, keymap)
750 if (KeyNameToLong(key->name) == name)
754 unsigned long new_name;
755 if (FindKeyNameForAlias(keymap, name, &new_name))
756 return FindNamedKey(keymap, new_name, false);
763 FindKeyNameForAlias(struct xkb_keymap *keymap, unsigned long lname,
764 unsigned long *real_name)
766 char name[XKB_KEY_NAME_LENGTH];
767 struct xkb_key_alias *a;
769 LongToKeyName(lname, name);
770 darray_foreach(a, keymap->key_aliases) {
771 if (strncmp(name, a->alias, XKB_KEY_NAME_LENGTH) == 0) {
772 *real_name = KeyNameToLong(a->real);