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 two letters
55 * specifying the row and the two digits specifying the column, from
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;
120 unsigned int file_id;
124 typedef struct _IndicatorNameInfo {
125 enum merge_mode merge;
131 typedef struct _KeyNamesInfo {
132 char *name; /* e.g. evdev+aliases(qwerty) */
135 enum merge_mode merge;
137 xkb_keycode_t min_key_code;
138 xkb_keycode_t max_key_code;
139 darray(KeyNameInfo) key_names;
140 IndicatorNameInfo indicator_names[XKB_NUM_INDICATORS];
141 darray(AliasInfo) aliases;
143 struct xkb_context *ctx;
147 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
148 char alias[XKB_KEY_NAME_LENGTH], char real[XKB_KEY_NAME_LENGTH])
150 memset(info, 0, sizeof(*info));
152 info->file_id = file_id;
153 info->alias = KeyNameToLong(alias);
154 info->real = KeyNameToLong(real);
157 static IndicatorNameInfo *
158 FindIndicatorByName(KeyNamesInfo *info, xkb_atom_t name,
159 xkb_led_index_t *idx_out)
163 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
164 if (info->indicator_names[idx].name == name) {
166 return &info->indicator_names[idx];
174 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
175 IndicatorNameInfo *new, xkb_led_index_t new_idx)
177 xkb_led_index_t old_idx;
178 IndicatorNameInfo *old;
179 bool replace, report;
180 int verbosity = xkb_get_log_verbosity(info->ctx);
182 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
184 old = FindIndicatorByName(info, new->name, &old_idx);
186 report = ((old->file_id == new->file_id && verbosity > 0) ||
189 if (old_idx == new_idx) {
191 log_warn(info->ctx, "Multiple indicators named %s; "
192 "Identical definitions ignored\n",
193 xkb_atom_text(info->ctx, new->name));
198 log_warn(info->ctx, "Multiple indicators named %s; "
199 "Using %d, ignoring %d\n",
200 xkb_atom_text(info->ctx, new->name),
201 (replace ? old_idx + 1 : new_idx + 1),
202 (replace ? new_idx + 1 : old_idx + 1));
205 * XXX: If in the next check we ignore new, than we will have
206 * deleted this old for nothing!
209 memset(old, 0, sizeof(*old));
212 old = &info->indicator_names[new_idx];
213 if (old->name != XKB_ATOM_NONE) {
214 report = ((old->file_id == new->file_id && verbosity > 0) ||
217 if (old->name == new->name) {
219 log_warn(info->ctx, "Multiple names for indicator %d; "
220 "Identical definitions ignored\n", new_idx + 1);
224 log_warn(info->ctx, "Multiple names for indicator %d; "
225 "Using %s, ignoring %s\n", new_idx + 1,
226 xkb_atom_text(info->ctx, new->name),
227 xkb_atom_text(info->ctx, old->name));
228 old->name = new->name;
232 log_warn(info->ctx, "Multiple names for indicator %d; "
233 "Using %s, ignoring %s\n", new_idx + 1,
234 xkb_atom_text(info->ctx, old->name),
235 xkb_atom_text(info->ctx, new->name));
241 info->indicator_names[new_idx] = *new;
246 ClearKeyNamesInfo(KeyNamesInfo *info)
249 darray_free(info->key_names);
250 darray_free(info->aliases);
254 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
257 memset(info, 0, sizeof(*info));
259 info->merge = MERGE_DEFAULT;
260 info->file_id = file_id;
261 info->min_key_code = XKB_KEYCODE_MAX;
265 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
269 for (i = info->min_key_code; i <= info->max_key_code; i++)
270 if (darray_item(info->key_names, i).name == name)
273 return XKB_KEYCODE_INVALID;
277 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, unsigned long name,
278 enum merge_mode merge, unsigned file_id, bool report)
282 int verbosity = xkb_get_log_verbosity(info->ctx);
284 if (kc >= darray_size(info->key_names))
285 darray_resize0(info->key_names, kc + 1);
287 info->min_key_code = MIN(info->min_key_code, kc);
288 info->max_key_code = MAX(info->max_key_code, kc);
290 namei = &darray_item(info->key_names, kc);
292 report = report && ((verbosity > 0 && file_id == namei->file_id) ||
295 if (namei->name != 0) {
296 const char *lname = LongKeyNameText(namei->name);
297 const char *kname = LongKeyNameText(name);
299 if (namei->name == name) {
302 "Multiple identical key name definitions; "
303 "Later occurences of \"%s = %d\" ignored\n",
307 else if (merge == MERGE_AUGMENT) {
310 "Multiple names for keycode %d; "
311 "Using %s, ignoring %s\n", kc, lname, kname);
317 "Multiple names for keycode %d; "
318 "Using %s, ignoring %s\n", kc, kname, lname);
324 old = FindKeyByLong(info, name);
325 if (old != XKB_KEYCODE_INVALID && old != kc) {
326 const char *kname = LongKeyNameText(name);
328 if (merge == MERGE_OVERRIDE) {
329 darray_item(info->key_names, old).name = 0;
330 darray_item(info->key_names, old).file_id = 0;
333 "Key name %s assigned to multiple keys; "
334 "Using %d, ignoring %d\n", kname, kc, old);
338 log_vrb(info->ctx, 3,
339 "Key name %s assigned to multiple keys; "
340 "Using %d, ignoring %d\n", kname, old, kc);
346 namei->file_id = file_id;
350 /***====================================================================***/
353 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
357 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
362 if (darray_empty(from->aliases))
365 if (darray_empty(into->aliases)) {
366 into->aliases = from->aliases;
367 darray_init(from->aliases);
371 memset(&def, 0, sizeof(def));
373 darray_foreach(alias, from->aliases) {
374 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
375 LongToKeyName(alias->alias, def.alias);
376 LongToKeyName(alias->real, def.real);
378 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
386 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
387 enum merge_mode merge)
392 if (from->errorCount > 0) {
393 into->errorCount += from->errorCount;
397 if (into->name == NULL) {
398 into->name = from->name;
402 if (darray_size(into->key_names) < darray_size(from->key_names))
403 darray_resize0(into->key_names, darray_size(from->key_names));
405 for (i = from->min_key_code; i <= from->max_key_code; i++) {
406 unsigned long name = darray_item(from->key_names, i).name;
410 if (!AddKeyName(into, i, name, merge, from->file_id, false))
414 for (idx = 0; idx < XKB_NUM_INDICATORS; idx++) {
415 IndicatorNameInfo *led = &from->indicator_names[idx];
416 if (led->name == XKB_ATOM_NONE)
419 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
420 if (!AddIndicatorName(into, led->merge, led, idx))
424 if (!MergeAliases(into, from, merge))
429 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
432 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
434 enum merge_mode merge = MERGE_DEFAULT;
436 KeyNamesInfo included, next_incl;
438 InitKeyNamesInfo(&included, info->ctx, info->file_id);
441 included.name = stmt->stmt;
445 for (; stmt; stmt = stmt->next_incl) {
446 if (!ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES,
448 info->errorCount += 10;
449 ClearKeyNamesInfo(&included);
453 InitKeyNamesInfo(&next_incl, info->ctx, rtrn->id);
455 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
457 MergeIncludedKeycodes(&included, &next_incl, merge);
459 ClearKeyNamesInfo(&next_incl);
463 MergeIncludedKeycodes(info, &included, merge);
464 ClearKeyNamesInfo(&included);
466 return (info->errorCount == 0);
470 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
472 if (stmt->merge != MERGE_DEFAULT) {
473 if (stmt->merge == MERGE_REPLACE)
474 merge = MERGE_OVERRIDE;
479 return AddKeyName(info, stmt->value, KeyNameToLong(stmt->name), merge,
480 info->file_id, true);
484 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
486 int verbosity = xkb_get_log_verbosity(info->ctx);
487 bool report = ((new->file_id == old->file_id && verbosity > 0) ||
490 if (new->real == old->real) {
492 log_warn(info->ctx, "Alias of %s for %s declared more than once; "
493 "First definition ignored\n",
494 LongKeyNameText(new->alias), LongKeyNameText(new->real));
497 unsigned long use, ignore;
499 use = (new->merge == MERGE_AUGMENT ? old->real : new->real);
500 ignore = (new->merge == MERGE_AUGMENT ? new->real : old->real);
503 log_warn(info->ctx, "Multiple definitions for alias %s; "
504 "Using %s, ignoring %s\n",
505 LongKeyNameText(old->alias), LongKeyNameText(use),
506 LongKeyNameText(ignore));
511 old->file_id = new->file_id;
512 old->merge = new->merge;
516 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
519 AliasInfo *alias, new;
521 darray_foreach(alias, info->aliases) {
522 if (alias->alias == KeyNameToLong(def->alias)) {
523 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
524 HandleAliasCollision(info, alias, &new);
529 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
530 darray_append(info->aliases, new);
535 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
537 const char *elem, *field;
540 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
544 log_err(info->ctx, "Unknown element %s encountered; "
545 "Default for field %s ignored\n", elem, field);
549 if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
550 log_err(info->ctx, "Unknown field encountered; "
551 "Assigment to field %s ignored\n", field);
555 /* We ignore explicit min/max statements, we always use computed. */
560 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
561 enum merge_mode merge)
563 IndicatorNameInfo ii;
566 if (def->ndx < 1 || def->ndx > XKB_NUM_INDICATORS) {
569 "Name specified for illegal indicator index %d\n; Ignored\n",
574 if (!ExprResolveString(info->ctx, def->name, &name)) {
576 snprintf(buf, sizeof(buf), "%d", def->ndx);
578 return ReportBadType(info->ctx, "indicator", "name", buf,
582 ii.merge = info->merge;
583 ii.file_id = info->file_id;
585 return AddIndicatorName(info, merge, &ii, def->ndx - 1);
589 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
595 info->name = strdup_safe(file->name);
597 for (stmt = file->defs; stmt; stmt = stmt->next) {
598 switch (stmt->type) {
600 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
603 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
606 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
610 ok = HandleKeyNameVar(info, (VarDef *) stmt);
612 case STMT_INDICATOR_NAME:
613 ok = HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
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",
636 ApplyAliases(KeyNamesInfo *info, struct xkb_keymap *keymap)
639 struct xkb_key_alias *a, new;
642 darray_foreach(alias, info->aliases) {
643 /* Check that ->real is a key. */
644 key = FindNamedKey(keymap, alias->real, false);
646 log_vrb(info->ctx, 5,
647 "Attempt to alias %s to non-existent key %s; Ignored\n",
648 LongKeyNameText(alias->alias),
649 LongKeyNameText(alias->real));
653 /* Check that ->alias is not a key. */
654 key = FindNamedKey(keymap, alias->alias, false);
656 log_vrb(info->ctx, 5,
657 "Attempt to create alias with the name of a real key; "
658 "Alias \"%s = %s\" ignored\n",
659 LongKeyNameText(alias->alias),
660 LongKeyNameText(alias->real));
664 /* Check that ->alias in not already an alias, and if so handle it. */
665 darray_foreach(a, keymap->key_aliases) {
668 if (KeyNameToLong(a->alias) != alias->alias)
671 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
672 HandleAliasCollision(info, &old_alias, alias);
673 LongToKeyName(old_alias.alias, a->alias);
674 LongToKeyName(old_alias.real, a->real);
677 if (alias->alias == 0)
681 LongToKeyName(alias->alias, new.alias);
682 LongToKeyName(alias->real, new.real);
683 darray_append(keymap->key_aliases, new);
686 darray_free(info->aliases);
690 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
695 keymap->min_key_code = info->min_key_code;
696 keymap->max_key_code = info->max_key_code;
698 darray_resize0(keymap->keys, keymap->max_key_code + 1);
699 for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
700 struct xkb_key *key = &darray_item(keymap->keys, kc);
702 LongToKeyName(darray_item(info->key_names, 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);