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
43 * Minimum/Maximum keycode
44 * -----------------------
45 * Statements of the form:
49 * The file may explicitly declare the minimum and/or maximum keycode
50 * contained therein (traditionally 8-255, inherited from old xfree86
51 * keycodes). If these are stated explicitly, they are enforced. If
52 * they are not stated, they are computed automatically.
56 * Statements of the form:
60 * The above would let 49 and 10 be valid keycodes in the keymap, and
61 * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
62 * always used to refer to a key by name.
64 * [ The naming convention <AE01> just denoted the position of the key
65 * in the main alphanumric section of the keyboard, with the top left key
66 * (usually "~") acting as origin. <AE01> is the key in the first row
67 * second column (which is usually "1"). ]
69 * In the common case this just maps to the evdev scancodes from
70 * /usr/include/linux/input.h, e.g. the following definitions:
71 * #define KEY_GRAVE 41
73 * Similar definitions appear in the xf86-input-keyboard driver. Note
74 * that in all current keymaps there's a constant offset of 8 (for
75 * historical reasons).
77 * If there's a conflict, like the same name given to different keycodes,
78 * or same keycode given different names, it is resolved according to the
79 * merge mode which applies to the definitions.
81 * The reason for the 4 characters limit is that the name is sometimes
82 * converted to an unsigned long (in a direct mapping), instead of a char
83 * array (see KeyNameToLong, LongToKeyName).
87 * Statements of the form:
88 * alias <MENU> = <COMP>;
90 * Allows to refer to a previously defined key (here <COMP>) by another
91 * name (here <MENU>). Conflicts are handled similarly.
93 * Indicator name statements
94 * -------------------------
95 * Statements of the form:
96 * indicator 1 = "Caps Lock";
97 * indicator 2 = "Num Lock";
98 * indicator 3 = "Scroll Lock";
100 * Assigns a name the indicator (i.e. keyboard LED) with the given index.
101 * The amount of possible indicators is predetermined (XKB_NUM_INDICATORS).
102 * The indicator may be referred by this name later in the compat section
105 * Effect on the keymap
106 * --------------------
107 * After all of the xkb_keycodes sections have been compiled, the
108 * following members of struct xkb_keymap are finalized:
109 * xkb_keycode_t min_key_code;
110 * xkb_keycode_t max_key_code;
111 * darray(struct xkb_key_alias) key_aliases;
112 * char *keycodes_section_name;
113 * The 'name' field of indicators declared in xkb_keycodes:
114 * struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
115 * Further, the array of keys:
116 * darray(struct xkb_key) keys;
117 * had been resized to its final size (i.e. all of the xkb_key objects are
118 * referable by their keycode). However the objects themselves do not
119 * contain any useful information besides the key name at this point.
122 typedef struct _AliasInfo {
123 enum merge_mode merge;
130 typedef struct _IndicatorNameInfo {
131 enum merge_mode merge;
137 typedef struct _KeyNamesInfo {
138 char *name; /* e.g. evdev+aliases(qwerty) */
141 enum merge_mode merge;
143 xkb_keycode_t computedMin; /* lowest keycode stored */
144 xkb_keycode_t computedMax; /* highest keycode stored */
145 xkb_keycode_t explicitMin;
146 xkb_keycode_t explicitMax;
147 darray(unsigned long) names;
148 darray(unsigned int) files;
149 IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
150 darray(AliasInfo) aliases;
152 struct xkb_context *ctx;
156 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
158 if (newMax < darray_size(info->names))
161 darray_resize0(info->names, newMax + 1);
162 darray_resize0(info->files, newMax + 1);
166 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
167 char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
169 memset(info, 0, sizeof(*info));
171 info->file_id = file_id;
172 info->alias = KeyNameToLong(alias);
173 info->real = KeyNameToLong(real);
176 static IndicatorNameInfo *
177 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
178 xkb_led_index_t *idx_out)
182 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
183 if (info->indicator_names[idx].name == name) {
185 return &info->indicator_names[idx];
193 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
194 IndicatorNameInfo *new, xkb_led_index_t new_idx)
196 xkb_led_index_t old_idx;
197 IndicatorNameInfo *old;
198 bool replace, report;
199 int verbosity = xkb_get_log_verbosity(info->ctx);
201 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
203 old = FindIndicatorByName(info, new->name, &old_idx);
205 report = ((old->file_id == new->file_id && verbosity > 0) ||
208 if (old_idx == new_idx) {
210 log_warn(info->ctx, "Multiple indicators named %s; "
211 "Identical definitions ignored\n",
212 xkb_atom_text(info->ctx, new->name));
217 log_warn(info->ctx, "Multiple indicators named %s; "
218 "Using %d, ignoring %d\n",
219 xkb_atom_text(info->ctx, new->name),
220 (replace ? old_idx + 1 : new_idx + 1),
221 (replace ? new_idx + 1 : old_idx + 1));
224 * XXX: If in the next check we ignore new, than we will have
225 * deleted this old for nothing!
228 memset(old, 0, sizeof(*old));
231 old = &info->indicator_names[new_idx];
232 if (old->name != XKB_ATOM_NONE) {
233 report = ((old->file_id == new->file_id && verbosity > 0) ||
236 if (old->name == new->name) {
238 log_warn(info->ctx, "Multiple names for indicator %d; "
239 "Identical definitions ignored\n", new_idx + 1);
243 log_warn(info->ctx, "Multiple names for indicator %d; "
244 "Using %s, ignoring %s\n", new_idx + 1,
245 xkb_atom_text(info->ctx, new->name),
246 xkb_atom_text(info->ctx, old->name));
247 old->name = new->name;
251 log_warn(info->ctx, "Multiple names for indicator %d; "
252 "Using %s, ignoring %s\n", new_idx + 1,
253 xkb_atom_text(info->ctx, old->name),
254 xkb_atom_text(info->ctx, new->name));
260 info->indicator_names[new_idx] = *new;
265 ClearKeyNamesInfo(KeyNamesInfo *info)
268 darray_free(info->names);
269 darray_free(info->files);
270 darray_free(info->aliases);
274 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
277 memset(info, 0, sizeof(*info));
279 info->merge = MERGE_DEFAULT;
280 info->file_id = file_id;
281 info->computedMin = XKB_KEYCODE_MAX;
285 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
289 for (i = info->computedMin; i <= info->computedMax; i++)
290 if (darray_item(info->names, i) == name)
297 * Store the name of the key as a long in the info struct under the given
298 * keycode. If the same keys is referred to twice, print a warning.
299 * Note that the key's name is stored as a long, the keycode is the index.
302 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, unsigned long name,
303 enum merge_mode merge, unsigned file_id, bool reportCollisions)
306 int verbosity = xkb_get_log_verbosity(info->ctx);
308 ResizeKeyNameArrays(info, kc);
310 if (kc < info->computedMin)
311 info->computedMin = kc;
312 if (kc > info->computedMax)
313 info->computedMax = kc;
315 if (reportCollisions)
316 reportCollisions = (verbosity > 7 ||
318 file_id == darray_item(info->files, kc)));
320 if (darray_item(info->names, kc) != 0) {
321 const char *lname = LongKeyNameText(darray_item(info->names, kc));
322 const char *kname = LongKeyNameText(name);
324 if (darray_item(info->names, kc) == name && reportCollisions) {
325 log_warn(info->ctx, "Multiple identical key name definitions; "
326 "Later occurences of \"%s = %d\" ignored\n", lname, kc);
330 if (merge == MERGE_AUGMENT) {
331 if (reportCollisions)
332 log_warn(info->ctx, "Multiple names for keycode %d; "
333 "Using %s, ignoring %s\n", kc, lname, kname);
337 if (reportCollisions)
338 log_warn(info->ctx, "Multiple names for keycode %d; "
339 "Using %s, ignoring %s\n", kc, kname, lname);
340 darray_item(info->names, kc) = 0;
341 darray_item(info->files, kc) = 0;
345 old = FindKeyByLong(info, name);
346 if (old != 0 && old != kc) {
347 const char *kname = LongKeyNameText(name);
349 if (merge == MERGE_OVERRIDE) {
350 darray_item(info->names, old) = 0;
351 darray_item(info->files, old) = 0;
352 if (reportCollisions)
353 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
354 "Using %d, ignoring %d\n", kname, kc, old);
357 if (reportCollisions && verbosity > 3)
358 log_warn(info->ctx, "Key name %s assigned to multiple keys; "
359 "Using %d, ignoring %d\n", kname, old, kc);
364 darray_item(info->names, kc) = name;
365 darray_item(info->files, kc) = file_id;
369 /***====================================================================***/
372 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
376 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
381 if (darray_empty(from->aliases))
384 if (darray_empty(into->aliases)) {
385 into->aliases = from->aliases;
386 darray_init(from->aliases);
390 memset(&def, 0, sizeof(def));
392 darray_foreach(alias, from->aliases) {
393 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
394 LongToKeyName(alias->alias, def.alias);
395 LongToKeyName(alias->real, def.real);
397 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
405 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
406 enum merge_mode merge)
411 if (from->errorCount > 0) {
412 into->errorCount += from->errorCount;
416 if (into->name == NULL) {
417 into->name = from->name;
421 ResizeKeyNameArrays(into, from->computedMax);
423 for (i = from->computedMin; i <= from->computedMax; i++) {
424 unsigned long name = darray_item(from->names, i);
428 if (!AddKeyName(into, i, name, merge, from->file_id, false))
432 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
433 IndicatorNameInfo *led = &from->indicator_names[idx];
434 if (led->name == XKB_ATOM_NONE)
437 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
438 if (!AddIndicatorName(into, led->merge, led, idx))
442 if (!MergeAliases(into, from, merge))
445 if (from->explicitMin != 0)
446 if (into->explicitMin == 0 || into->explicitMin > from->explicitMin)
447 into->explicitMin = from->explicitMin;
449 if (from->explicitMax > 0)
450 if (into->explicitMax == 0 || into->explicitMax < from->explicitMax)
451 into->explicitMax = from->explicitMax;
455 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
458 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
460 enum merge_mode merge = MERGE_DEFAULT;
462 KeyNamesInfo included, next_incl;
464 InitKeyNamesInfo(&included, info->ctx, info->file_id);
467 included.name = stmt->stmt;
471 for (; stmt; stmt = stmt->next_incl) {
472 if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
474 info->errorCount += 10;
475 ClearKeyNamesInfo(&included);
479 InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
481 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
483 MergeIncludedKeycodes(&included, &next_incl, merge);
485 ClearKeyNamesInfo(&next_incl);
489 MergeIncludedKeycodes(info, &included, merge);
490 ClearKeyNamesInfo(&included);
492 return (info->errorCount == 0);
496 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
498 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
499 (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
500 log_err(info->ctx, "Illegal keycode %lu for name %s; "
501 "Must be in the range %d-%d inclusive\n",
502 stmt->value, KeyNameText(stmt->name), info->explicitMin,
503 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
507 if (stmt->merge != MERGE_DEFAULT) {
508 if (stmt->merge == MERGE_REPLACE)
509 merge = MERGE_OVERRIDE;
514 return AddKeyName(info, stmt->value, KeyNameToLong(stmt->name), merge,
515 info->file_id, true);
519 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
521 int verbosity = xkb_get_log_verbosity(info->ctx);
522 bool report = ((new->file_id == old->file_id && verbosity > 0) ||
525 if (new->real == old->real) {
527 log_warn(info->ctx, "Alias of %s for %s declared more than once; "
528 "First definition ignored\n",
529 LongKeyNameText(new->alias), LongKeyNameText(new->real));
532 unsigned long use, ignore;
534 use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
535 ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
538 log_warn(info->ctx, "Multiple definitions for alias %s; "
539 "Using %s, ignoring %s\n",
540 LongKeyNameText(old->alias), LongKeyNameText(use),
541 LongKeyNameText(ignore));
546 old->file_id = new->file_id;
547 old->merge = new->merge;
551 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
554 AliasInfo *alias, new;
556 darray_foreach(alias, info->aliases) {
557 if (alias->alias == KeyNameToLong(def->alias)) {
558 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
559 HandleAliasCollision(info, alias, &new);
564 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
565 darray_append(info->aliases, new);
569 #define MIN_KEYCODE_DEF 0
570 #define MAX_KEYCODE_DEF 1
573 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
575 const char *elem, *field;
580 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
584 log_err(info->ctx, "Unknown element %s encountered; "
585 "Default for field %s ignored\n", elem, field);
589 if (istreq(field, "minimum")) {
590 which = MIN_KEYCODE_DEF;
592 else if (istreq(field, "maximum")) {
593 which = MAX_KEYCODE_DEF;
596 log_err(info->ctx, "Unknown field encountered; "
597 "Assigment to field %s ignored\n", field);
601 if (arrayNdx != NULL) {
602 log_err(info->ctx, "The %s setting is not an array; "
603 "Illegal array reference ignored\n", field);
607 if (!ExprResolveKeyCode(info->ctx, stmt->value, &kc)) {
608 log_err(info->ctx, "Illegal keycode encountered; "
609 "Assignment to field %s ignored\n", field);
613 if (kc > XKB_KEYCODE_MAX) {
615 "Illegal keycode %d (must be in the range %d-%d inclusive); "
616 "Value of \"%s\" not changed\n",
617 kc, 0, XKB_KEYCODE_MAX, field);
621 if (which == MIN_KEYCODE_DEF) {
622 if (info->explicitMax > 0 && info->explicitMax < kc) {
624 "Minimum key code (%d) must be <= maximum key code (%d); "
625 "Minimum key code value not changed\n",
626 kc, info->explicitMax);
630 if (info->computedMax > 0 && info->computedMin < kc) {
632 "Minimum key code (%d) must be <= lowest defined key (%d); "
633 "Minimum key code value not changed\n",
634 kc, info->computedMin);
638 info->explicitMin = kc;
640 else if (which == MAX_KEYCODE_DEF) {
641 if (info->explicitMin > 0 && info->explicitMin > kc) {
643 "Maximum code (%d) must be >= minimum key code (%d); "
644 "Maximum code value not changed\n",
645 kc, info->explicitMin);
649 if (info->computedMax > 0 && info->computedMax > kc) {
651 "Maximum code (%d) must be >= highest defined key (%d); "
652 "Maximum code value not changed\n",
653 kc, info->computedMax);
657 info->explicitMax = kc;
664 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
665 enum merge_mode merge)
667 IndicatorNameInfo ii;
670 if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
673 "Name specified for illegal indicator index %d\n; Ignored\n",
678 if (!ExprResolveString(info->ctx, def->name, &name)) {
680 snprintf(buf, sizeof(buf), "%d", def->ndx);
682 return ReportBadType(info->ctx, "indicator", "name", buf,
686 ii.merge = info->merge;
687 ii.file_id = info->file_id;
689 return AddIndicatorName(info, merge, &ii, def->ndx - 1);
693 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
699 info->name = strdup_safe(file->name);
701 for (stmt = file->defs; stmt; stmt = stmt->next) {
702 switch (stmt->type) {
704 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
707 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
710 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
714 ok = HandleKeyNameVar(info, (VarDef *) stmt);
716 case STMT_INDICATOR_NAME:
717 ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
722 "Keycode files may define key and indicator names only; "
723 "Ignoring %s\n", stmt_type_to_string(stmt->type));
731 if (info->errorCount > 10) {
732 log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
740 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
743 struct xkb_key_alias *a, new;
746 darray_foreach(alias, info->aliases) {
747 /* Check that ->real is a key. */
748 key = FindNamedKey(keymap, alias->real, false);
750 log_vrb(info->ctx, 5,
751 "Attempt to alias %s to non-existent key %s; Ignored\n",
752 LongKeyNameText(alias->alias),
753 LongKeyNameText(alias->real));
757 /* Check that ->alias is not a key. */
758 key = FindNamedKey(keymap, alias->alias, false);
760 log_vrb(info->ctx, 5,
761 "Attempt to create alias with the name of a real key; "
762 "Alias \"%s = %s\" ignored\n",
763 LongKeyNameText(alias->alias),
764 LongKeyNameText(alias->real));
768 /* Check that ->alias in not already an alias, and if so handle it. */
769 darray_foreach(a, keymap->key_aliases) {
772 if (KeyNameToLong(a->alias) != alias->alias)
775 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
776 HandleAliasCollision(info, &old_alias, alias);
777 LongToKeyName(old_alias.alias, a->alias);
778 LongToKeyName(old_alias.real, a->real);
781 if (alias->alias == 0)
785 LongToKeyName(alias->alias, new.alias);
786 LongToKeyName(alias->real, new.real);
787 darray_append(keymap->key_aliases, new);
790 darray_free(info->aliases);
794 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
799 if (info->explicitMin > 0)
800 keymap->min_key_code = info->explicitMin;
802 keymap->min_key_code = info->computedMin;
804 if (info->explicitMax > 0)
805 keymap->max_key_code = info->explicitMax;
807 keymap->max_key_code = info->computedMax;
809 darray_resize0(keymap->keys, keymap->max_key_code + 1);
810 for (kc = info->computedMin; kc <= info->computedMax; kc++)
811 LongToKeyName(darray_item(info->names, kc),
812 XkbKey(keymap, kc)->name);
814 keymap->keycodes_section_name = strdup_safe(info->name);
816 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
817 IndicatorNameInfo *led = &info->indicator_names[idx];
818 if (led->name == XKB_ATOM_NONE)
821 keymap->indicators[idx].name = led->name;
824 ApplyAliases(info, keymap);
830 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
831 enum merge_mode merge)
835 InitKeyNamesInfo(&info, keymap->ctx, file->id);
837 HandleKeycodesFile(&info, file, merge);
838 if (info.errorCount != 0)
841 if (!CopyKeyNamesToKeymap(keymap, &info))
844 ClearKeyNamesInfo(&info);
848 ClearKeyNamesInfo(&info);
853 FindNamedKey(struct xkb_keymap *keymap, unsigned long name, bool use_aliases)
857 xkb_foreach_key(key, keymap)
858 if (KeyNameToLong(key->name) == name)
862 unsigned long new_name;
863 if (FindKeyNameForAlias(keymap, name, &new_name))
864 return FindNamedKey(keymap, new_name, false);
871 FindKeyNameForAlias(struct xkb_keymap *keymap, unsigned long lname,
872 unsigned long *real_name)
874 char name[XKB_KEY_NAME_LENGTH];
875 struct xkb_key_alias *a;
877 LongToKeyName(lname, name);
878 darray_foreach(a, keymap->key_aliases) {
879 if (strncmp(name, a->alias, XKB_KEY_NAME_LENGTH) == 0) {
880 *real_name = KeyNameToLong(a->real);