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"
33 * The xkb_keycodes section
34 * ========================
36 * This is the simplest section type, and is the first one to be
37 * compiled. The purpose of this is mostly to map between the
38 * hardware/evdev scancodes and xkb keycodes. Each key is given a name
39 * by which it can be referred to later, e.g. in the symbols section.
43 * Statements of the form:
47 * The above would let 49 and 10 be valid keycodes in the keymap, and
48 * assign them the names TLDE and AE01 respectively. The format <WXYZ> is
49 * always used to refer to a key by name.
51 * [ The naming convention <AE01> just denoted the position of the key
52 * in the main alphanumric section of the keyboard, with the two letters
53 * specifying the row and the two digits specifying the column, from
56 * In the common case this just maps to the evdev scancodes from
57 * /usr/include/linux/input.h, e.g. the following definitions:
58 * #define KEY_GRAVE 41
60 * Similar definitions appear in the xf86-input-keyboard driver. Note
61 * that in all current keymaps there's a constant offset of 8 (for
62 * historical reasons).
64 * If there's a conflict, like the same name given to different keycodes,
65 * or same keycode given different names, it is resolved according to the
66 * merge mode which applies to the definitions.
70 * Statements of the form:
71 * alias <MENU> = <COMP>;
73 * Allows to refer to a previously defined key (here <COMP>) by another
74 * name (here <MENU>). Conflicts are handled similarly.
77 * -------------------------
78 * Statements of the form:
79 * indicator 1 = "Caps Lock";
80 * indicator 2 = "Num Lock";
81 * indicator 3 = "Scroll Lock";
83 * Assigns a name to the keyboard LED (a.k.a indicator) with the given index.
84 * The led may be referred by this name later in the compat section
87 * Effect on the keymap
88 * --------------------
89 * After all of the xkb_keycodes sections have been compiled, the
90 * following members of struct xkb_keymap are finalized:
91 * xkb_keycode_t min_key_code;
92 * xkb_keycode_t max_key_code;
93 * darray(struct xkb_key_alias) key_aliases;
94 * char *keycodes_section_name;
95 * The 'name' field of leds declared in xkb_keycodes:
96 * darray(struct xkb_led) leds;
97 * Further, the array of keys:
98 * struct xkb_key *keys;
99 * had been resized to its final size (i.e. all of the xkb_key objects are
100 * referable by their keycode). However the objects themselves do not
101 * contain any useful information besides the key name at this point.
105 enum merge_mode merge;
118 enum merge_mode merge;
127 enum merge_mode merge;
130 xkb_keycode_t min_key_code;
131 xkb_keycode_t max_key_code;
132 darray(KeyNameInfo) key_names;
133 darray(LedNameInfo) led_names;
134 darray(AliasInfo) aliases;
136 struct xkb_context *ctx;
139 /***====================================================================***/
142 InitAliasInfo(AliasInfo *info, enum merge_mode merge,
143 xkb_atom_t alias, xkb_atom_t real)
145 memset(info, 0, sizeof(*info));
152 FindLedByName(KeyNamesInfo *info, xkb_atom_t name,
153 xkb_led_index_t *idx_out)
158 darray_enumerate(idx, ledi, info->led_names) {
159 if (ledi->name == name) {
169 AddLedName(KeyNamesInfo *info, enum merge_mode merge,
170 LedNameInfo *new, xkb_led_index_t new_idx)
172 xkb_led_index_t old_idx;
174 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
175 const bool replace = (merge == MERGE_REPLACE || merge == MERGE_OVERRIDE);
177 /* Inidicator with the same name already exists. */
178 old = FindLedByName(info, new->name, &old_idx);
180 const bool report = ((old->file_id == new->file_id && verbosity > 0) ||
183 if (old_idx == new_idx) {
185 "Multiple indicators named \"%s\"; "
186 "Identical definitions ignored\n",
187 xkb_atom_text(info->ctx, new->name));
192 xkb_led_index_t use = (replace ? new_idx + 1 : old_idx + 1);
193 xkb_led_index_t ignore = (replace ? old_idx + 1 : new_idx + 1);
195 "Multiple indicators named %s; Using %d, ignoring %d\n",
196 xkb_atom_text(info->ctx, new->name), use, ignore);
205 if (new_idx >= darray_size(info->led_names))
206 darray_resize0(info->led_names, new_idx + 1);
208 /* Inidicator with the same index already exists. */
209 old = &darray_item(info->led_names, new_idx);
210 if (old->name != XKB_ATOM_NONE) {
211 const bool report = ((old->file_id == new->file_id && verbosity > 0) ||
214 /* Same name case already handled above. */
217 const xkb_atom_t use = (replace ? new->name : old->name);
218 const xkb_atom_t ignore = (replace ? old->name : new->name);
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, use),
222 xkb_atom_text(info->ctx, ignore));
231 darray_item(info->led_names, new_idx) = *new;
236 ClearKeyNamesInfo(KeyNamesInfo *info)
239 darray_free(info->key_names);
240 darray_free(info->aliases);
241 darray_free(info->led_names);
245 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx,
248 memset(info, 0, sizeof(*info));
250 info->merge = MERGE_DEFAULT;
251 info->file_id = file_id;
252 info->min_key_code = XKB_KEYCODE_MAX;
256 FindKeyByName(KeyNamesInfo * info, xkb_atom_t name)
260 for (i = info->min_key_code; i <= info->max_key_code; i++)
261 if (darray_item(info->key_names, i).name == name)
264 return XKB_KEYCODE_INVALID;
268 AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name,
269 enum merge_mode merge, unsigned file_id, bool report)
273 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
275 if (kc >= darray_size(info->key_names))
276 darray_resize0(info->key_names, kc + 1);
278 info->min_key_code = MIN(info->min_key_code, kc);
279 info->max_key_code = MAX(info->max_key_code, kc);
281 namei = &darray_item(info->key_names, kc);
283 report = report && ((verbosity > 0 && file_id == namei->file_id) ||
286 if (namei->name != 0) {
287 const char *lname = KeyNameText(info->ctx, namei->name);
288 const char *kname = KeyNameText(info->ctx, name);
290 if (namei->name == name) {
293 "Multiple identical key name definitions; "
294 "Later occurrences of \"%s = %d\" ignored\n",
298 else if (merge == MERGE_AUGMENT) {
301 "Multiple names for keycode %d; "
302 "Using %s, ignoring %s\n", kc, lname, kname);
308 "Multiple names for keycode %d; "
309 "Using %s, ignoring %s\n", kc, kname, lname);
315 old = FindKeyByName(info, name);
316 if (old != XKB_KEYCODE_INVALID && old != kc) {
317 const char *kname = KeyNameText(info->ctx, name);
319 if (merge == MERGE_OVERRIDE) {
320 darray_item(info->key_names, old).name = 0;
321 darray_item(info->key_names, old).file_id = 0;
324 "Key name %s assigned to multiple keys; "
325 "Using %d, ignoring %d\n", kname, kc, old);
329 log_vrb(info->ctx, 3,
330 "Key name %s assigned to multiple keys; "
331 "Using %d, ignoring %d\n", kname, old, kc);
337 namei->file_id = file_id;
341 /***====================================================================***/
344 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge);
347 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
348 enum merge_mode merge)
350 if (from->errorCount > 0) {
351 into->errorCount += from->errorCount;
355 if (into->name == NULL) {
356 into->name = from->name;
360 /* Merge key names. */
361 if (darray_empty(into->key_names)) {
362 into->key_names = from->key_names;
363 darray_init(from->key_names);
364 into->min_key_code = from->min_key_code;
365 into->max_key_code = from->max_key_code;
368 if (darray_size(into->key_names) < darray_size(from->key_names))
369 darray_resize0(into->key_names, darray_size(from->key_names));
371 for (unsigned i = from->min_key_code; i <= from->max_key_code; i++) {
372 xkb_atom_t name = darray_item(from->key_names, i).name;
373 if (name == XKB_ATOM_NONE)
376 if (!AddKeyName(into, i, name, merge, from->file_id, false))
381 /* Merge key aliases. */
382 if (darray_empty(into->aliases)) {
383 into->aliases = from->aliases;
384 darray_init(from->aliases);
389 darray_foreach(alias, from->aliases) {
392 def.merge = (merge == MERGE_DEFAULT ? alias->merge : merge);
393 def.alias = alias->alias;
394 def.real = alias->real;
396 if (!HandleAliasDef(into, &def, def.merge))
401 /* Merge LED names. */
402 if (darray_empty(into->led_names)) {
403 into->led_names = from->led_names;
404 darray_init(from->led_names);
410 darray_enumerate(idx, ledi, from->led_names) {
411 if (ledi->name == XKB_ATOM_NONE)
414 ledi->merge = (merge == MERGE_DEFAULT ? ledi->merge : merge);
415 if (!AddLedName(into, ledi->merge, ledi, idx))
422 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
425 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *include)
427 KeyNamesInfo included;
429 InitKeyNamesInfo(&included, info->ctx, info->file_id);
430 included.name = include->stmt;
431 include->stmt = NULL;
433 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
434 KeyNamesInfo next_incl;
437 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES);
439 info->errorCount += 10;
440 ClearKeyNamesInfo(&included);
444 InitKeyNamesInfo(&next_incl, info->ctx, file->id);
446 HandleKeycodesFile(&next_incl, file, MERGE_OVERRIDE);
448 MergeIncludedKeycodes(&included, &next_incl, stmt->merge);
450 ClearKeyNamesInfo(&next_incl);
454 MergeIncludedKeycodes(info, &included, include->merge);
455 ClearKeyNamesInfo(&included);
457 return (info->errorCount == 0);
461 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
463 if (stmt->merge != MERGE_DEFAULT) {
464 if (stmt->merge == MERGE_REPLACE)
465 merge = MERGE_OVERRIDE;
470 if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) {
472 "Illegal keycode %lld: must be between 0..%u; "
473 "Key ignored\n", (long long) stmt->value, XKB_KEYCODE_MAX);
477 return AddKeyName(info, stmt->value, stmt->name, merge,
478 info->file_id, true);
482 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge)
486 darray_foreach(old, info->aliases) {
487 if (old->alias == def->alias) {
488 if (def->real == old->real) {
489 log_vrb(info->ctx, 1,
490 "Alias of %s for %s declared more than once; "
491 "First definition ignored\n",
492 KeyNameText(info->ctx, def->alias),
493 KeyNameText(info->ctx, def->real));
496 xkb_atom_t use, ignore;
498 use = (merge == MERGE_AUGMENT ? old->real : def->real);
499 ignore = (merge == MERGE_AUGMENT ? def->real : old->real);
502 "Multiple definitions for alias %s; "
503 "Using %s, ignoring %s\n",
504 KeyNameText(info->ctx, old->alias),
505 KeyNameText(info->ctx, use),
506 KeyNameText(info->ctx, ignore));
516 InitAliasInfo(&new, merge, def->alias, def->real);
517 darray_append(info->aliases, new);
522 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
524 const char *elem, *field;
527 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
531 log_err(info->ctx, "Unknown element %s encountered; "
532 "Default for field %s ignored\n", elem, field);
536 if (!istreq(field, "minimum") && !istreq(field, "maximum")) {
537 log_err(info->ctx, "Unknown field encountered; "
538 "Assignment to field %s ignored\n", field);
542 /* We ignore explicit min/max statements, we always use computed. */
547 HandleLedNameDef(KeyNamesInfo *info, LedNameDef *def,
548 enum merge_mode merge)
553 if (def->ndx < 1 || def->ndx > XKB_MAX_LEDS) {
556 "Illegal indicator index (%d) specified; must be between 1 .. %d; "
557 "Ignored\n", def->ndx, XKB_MAX_LEDS);
561 if (!ExprResolveString(info->ctx, def->name, &name)) {
563 snprintf(buf, sizeof(buf), "%d", def->ndx);
565 return ReportBadType(info->ctx, "indicator", "name", buf, "string");
568 ledi.merge = info->merge;
569 ledi.file_id = info->file_id;
571 return AddLedName(info, merge, &ledi, def->ndx - 1);
575 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
580 info->name = strdup_safe(file->name);
582 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
583 switch (stmt->type) {
585 ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt);
588 ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, merge);
591 ok = HandleAliasDef(info, (KeyAliasDef *) stmt, merge);
594 ok = HandleKeyNameVar(info, (VarDef *) stmt);
597 ok = HandleLedNameDef(info, (LedNameDef *) stmt, merge);
601 "Keycode files may define key and indicator names only; "
602 "Ignoring %s\n", stmt_type_to_string(stmt->type));
610 if (info->errorCount > 10) {
611 log_err(info->ctx, "Abandoning keycodes file \"%s\"\n",
618 /***====================================================================***/
621 CopyAliasesToKeymap(KeyNamesInfo *info, struct xkb_keymap *keymap)
625 darray_foreach(alias, info->aliases) {
627 struct xkb_key_alias new;
629 /* Check that ->real is a key. */
630 key = XkbKeyByName(keymap, alias->real, false);
632 log_vrb(info->ctx, 5,
633 "Attempt to alias %s to non-existent key %s; Ignored\n",
634 KeyNameText(info->ctx, alias->alias),
635 KeyNameText(info->ctx, alias->real));
639 /* Check that ->alias is not a key. */
640 key = XkbKeyByName(keymap, alias->alias, false);
642 log_vrb(info->ctx, 5,
643 "Attempt to create alias with the name of a real key; "
644 "Alias \"%s = %s\" ignored\n",
645 KeyNameText(info->ctx, alias->alias),
646 KeyNameText(info->ctx, alias->real));
651 new.alias = alias->alias;
652 new.real = alias->real;
653 darray_append(keymap->key_aliases, new);
656 darray_free(info->aliases);
660 CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
666 keymap->keys = calloc(info->max_key_code + 1, sizeof(*keymap->keys));
670 keymap->min_key_code = info->min_key_code;
671 keymap->max_key_code = info->max_key_code;
673 for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
674 keymap->keys[kc].keycode = kc;
675 keymap->keys[kc].name = darray_item(info->key_names, kc).name;
678 keymap->keycodes_section_name = strdup_safe(info->name);
680 darray_resize0(keymap->leds, darray_size(info->led_names));
681 darray_enumerate(idx, ledi, info->led_names) {
682 if (ledi->name == XKB_ATOM_NONE)
685 darray_item(keymap->leds, idx).name = ledi->name;
688 CopyAliasesToKeymap(info, keymap);
693 /***====================================================================***/
696 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
697 enum merge_mode merge)
701 InitKeyNamesInfo(&info, keymap->ctx, file->id);
703 HandleKeycodesFile(&info, file, merge);
704 if (info.errorCount != 0)
707 if (!CopyKeyNamesToKeymap(keymap, &info))
710 ClearKeyNamesInfo(&info);
714 ClearKeyNamesInfo(&info);