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"
29 #include "parseutils.h"
31 typedef struct _AliasInfo {
32 enum merge_mode merge;
36 char alias[XkbKeyNameLength + 1];
37 char real[XkbKeyNameLength + 1];
40 typedef struct _IndicatorNameInfo {
41 enum merge_mode merge;
50 typedef struct _KeyNamesInfo {
51 char *name; /* e.g. evdev+aliases(qwerty) */
54 enum merge_mode merge;
55 xkb_keycode_t computedMin; /* lowest keycode stored */
56 xkb_keycode_t computedMax; /* highest keycode stored */
57 xkb_keycode_t explicitMin;
58 xkb_keycode_t explicitMax;
59 darray(unsigned long) names;
60 darray(unsigned int) files;
64 struct xkb_keymap *keymap;
68 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
70 if (newMax < darray_size(info->names))
73 darray_resize0(info->names, newMax + 1);
74 darray_resize0(info->files, newMax + 1);
78 InitAliasInfo(AliasInfo *info, enum merge_mode merge, unsigned file_id,
79 char *alias, char *real)
81 memset(info, 0, sizeof(*info));
83 info->file_id = file_id;
84 strncpy(info->alias, alias, XkbKeyNameLength);
85 strncpy(info->real, real, XkbKeyNameLength);
89 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
91 ii->merge = info->merge;
92 ii->file_id = info->file_id;
94 ii->name = XKB_ATOM_NONE;
98 static IndicatorNameInfo *
99 NextIndicatorName(KeyNamesInfo * info)
101 IndicatorNameInfo *ii;
103 ii = malloc(sizeof(*ii));
107 InitIndicatorNameInfo(ii, info);
108 list_append(&ii->entry, &info->leds);
113 static IndicatorNameInfo *
114 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
116 IndicatorNameInfo *old;
118 list_foreach(old, &info->leds, entry)
125 static IndicatorNameInfo *
126 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
128 IndicatorNameInfo *old;
130 list_foreach(old, &info->leds, entry)
131 if (old->name == name)
138 AddIndicatorName(KeyNamesInfo *info, enum merge_mode merge,
139 IndicatorNameInfo *new)
141 IndicatorNameInfo *old;
144 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
146 old = FindIndicatorByName(info, new->name);
148 if ((old->file_id == new->file_id && warningLevel > 0) ||
150 WARN("Multiple indicators named %s\n",
151 xkb_atom_text(info->keymap->ctx, new->name));
152 if (old->ndx == new->ndx) {
153 if (old->virtual != new->virtual) {
155 old->virtual = new->virtual;
156 ACTION("Using %s instead of %s\n",
157 (old->virtual ? "virtual" : "real"),
158 (old->virtual ? "real" : "virtual"));
161 ACTION("Identical definitions ignored\n");
167 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
169 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
173 list_del(&old->entry);
179 old = FindIndicatorByIndex(info, new->ndx);
181 if ((old->file_id == new->file_id && warningLevel > 0) ||
183 WARN("Multiple names for indicator %d\n", new->ndx);
184 if ((old->name == new->name) && (old->virtual == new->virtual))
185 ACTION("Identical definitions ignored\n");
187 const char *oldType, *newType;
188 xkb_atom_t using, ignoring;
190 oldType = "virtual indicator";
192 oldType = "real indicator";
194 newType = "virtual indicator";
196 newType = "real indicator";
199 ignoring = old->name;
203 ignoring = new->name;
205 ACTION("Using %s %s, ignoring %s %s\n",
206 oldType, xkb_atom_text(info->keymap->ctx, using),
207 newType, xkb_atom_text(info->keymap->ctx, ignoring));
211 old->name = new->name;
212 old->virtual = new->virtual;
217 new = NextIndicatorName(info);
219 WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
223 new->name = old->name;
225 new->virtual = old->virtual;
230 ClearKeyNamesInfo(KeyNamesInfo * info)
232 AliasInfo *alias, *next_alias;
233 IndicatorNameInfo *ii, *next_ii;
237 info->computedMax = info->explicitMax = info->explicitMin = 0;
238 info->computedMin = XKB_KEYCODE_MAX;
239 darray_free(info->names);
240 darray_free(info->files);
241 list_foreach_safe(ii, next_ii, &info->leds, entry)
243 list_foreach_safe(alias, next_alias, &info->aliases, entry)
245 list_init(&info->aliases);
249 InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_keymap *keymap,
253 list_init(&info->leds);
254 list_init(&info->aliases);
255 info->file_id = file_id;
256 darray_init(info->names);
257 darray_init(info->files);
258 ClearKeyNamesInfo(info);
259 info->errorCount = 0;
260 info->keymap = keymap;
264 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
268 for (i = info->computedMin; i <= info->computedMax; i++)
269 if (darray_item(info->names, i) == name)
276 * Store the name of the key as a long in the info struct under the given
277 * keycode. If the same keys is referred to twice, print a warning.
278 * Note that the key's name is stored as a long, the keycode is the index.
281 AddKeyName(KeyNamesInfo * info,
282 xkb_keycode_t kc, char *name, enum merge_mode merge,
283 unsigned file_id, bool reportCollisions)
288 ResizeKeyNameArrays(info, kc);
290 if (kc < info->computedMin)
291 info->computedMin = kc;
292 if (kc > info->computedMax)
293 info->computedMax = kc;
294 lval = KeyNameToLong(name);
296 if (reportCollisions) {
297 reportCollisions = (warningLevel > 7 ||
299 file_id == darray_item(info->files, kc)));
302 if (darray_item(info->names, kc) != 0) {
305 LongToKeyName(darray_item(info->names, kc), buf);
307 if (darray_item(info->names, kc) == lval && reportCollisions) {
308 WARN("Multiple identical key name definitions\n");
309 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
313 if (merge == MERGE_AUGMENT) {
314 if (reportCollisions) {
315 WARN("Multiple names for keycode %d\n", kc);
316 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
321 if (reportCollisions) {
322 WARN("Multiple names for keycode %d\n", kc);
323 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
325 darray_item(info->names, kc) = 0;
326 darray_item(info->files, kc) = 0;
329 old = FindKeyByLong(info, lval);
330 if ((old != 0) && (old != kc)) {
331 if (merge == MERGE_OVERRIDE) {
332 darray_item(info->names, old) = 0;
333 darray_item(info->files, old) = 0;
334 if (reportCollisions) {
335 WARN("Key name <%s> assigned to multiple keys\n", name);
336 ACTION("Using %d, ignoring %d\n", kc, old);
340 if ((reportCollisions) && (warningLevel > 3)) {
341 WARN("Key name <%s> assigned to multiple keys\n", name);
342 ACTION("Using %d, ignoring %d\n", old, kc);
347 darray_item(info->names, kc) = lval;
348 darray_item(info->files, kc) = file_id;
352 /***====================================================================***/
355 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
359 MergeAliases(KeyNamesInfo *into, KeyNamesInfo *from, enum merge_mode merge)
361 AliasInfo *alias, *next;
364 if (list_empty(&from->aliases))
367 if (list_empty(&into->aliases)) {
368 list_replace(&from->aliases, &into->aliases);
369 list_init(&from->aliases);
373 memset(&def, 0, sizeof(def));
375 list_foreach_safe(alias, next, &from->aliases, entry) {
376 def.merge = (merge == MERGE_DEFAULT) ? alias->merge : merge;
377 memcpy(def.alias, alias->alias, XkbKeyNameLength);
378 memcpy(def.real, alias->real, XkbKeyNameLength);
380 if (!HandleAliasDef(into, &def, def.merge, alias->file_id))
388 MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from,
389 enum merge_mode merge)
393 IndicatorNameInfo *led;
395 if (from->errorCount > 0) {
396 into->errorCount += from->errorCount;
399 if (into->name == NULL) {
400 into->name = from->name;
404 ResizeKeyNameArrays(into, from->computedMax);
406 for (i = from->computedMin; i <= from->computedMax; i++) {
407 if (darray_item(from->names, i) == 0)
409 LongToKeyName(darray_item(from->names, i), buf);
411 if (!AddKeyName(into, i, buf, merge, from->file_id, false))
415 list_foreach(led, &from->leds, entry) {
416 led->merge = (merge == MERGE_DEFAULT ? led->merge : merge);
417 if (!AddIndicatorName(into, led->merge, led))
421 if (!MergeAliases(into, from, merge))
423 if (from->explicitMin != 0) {
424 if ((into->explicitMin == 0)
425 || (into->explicitMin > from->explicitMin))
426 into->explicitMin = from->explicitMin;
428 if (from->explicitMax > 0) {
429 if ((into->explicitMax == 0)
430 || (into->explicitMax < from->explicitMax))
431 into->explicitMax = from->explicitMax;
436 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge);
439 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
441 * @param info Struct to store the key info in.
442 * @param stmt The include statement from the keymap file.
445 HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *stmt)
447 enum merge_mode merge = MERGE_DEFAULT;
449 KeyNamesInfo included, next_incl;
451 /* XXX: What's that? */
452 if (stmt->file && strcmp(stmt->file, "computed") == 0) {
453 info->keymap->flags |= AutoKeyNames;
454 info->explicitMin = 0;
455 info->explicitMax = XKB_KEYCODE_MAX;
456 return (info->errorCount == 0);
459 InitKeyNamesInfo(&included, info->keymap, info->file_id);
462 included.name = stmt->stmt;
466 for (; stmt; stmt = stmt->next) {
467 if (!ProcessIncludeFile(info->keymap->ctx, stmt, FILE_TYPE_KEYCODES,
469 info->errorCount += 10;
470 ClearKeyNamesInfo(&included);
474 InitKeyNamesInfo(&next_incl, info->keymap, rtrn->id);
476 HandleKeycodesFile(&next_incl, rtrn, MERGE_OVERRIDE);
478 MergeIncludedKeycodes(&included, &next_incl, merge);
480 ClearKeyNamesInfo(&next_incl);
484 MergeIncludedKeycodes(info, &included, merge);
485 ClearKeyNamesInfo(&included);
487 return (info->errorCount == 0);
491 * Parse the given statement and store the output in the info struct.
495 HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, enum merge_mode merge)
497 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
498 (info->explicitMax != 0 && stmt->value > info->explicitMax)) {
499 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
500 ACTION("Must be in the range %d-%d inclusive\n",
502 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
505 if (stmt->merge != MERGE_DEFAULT) {
506 if (stmt->merge == MERGE_REPLACE)
507 merge = MERGE_OVERRIDE;
511 return AddKeyName(info, stmt->value, stmt->name, merge, info->file_id,
516 HandleAliasCollision(KeyNamesInfo *info, AliasInfo *old, AliasInfo *new)
518 if (strncmp(new->real, old->real, XkbKeyNameLength) == 0) {
519 if ((new->file_id == old->file_id && warningLevel > 0) ||
521 WARN("Alias of %s for %s declared more than once\n",
522 XkbcKeyNameText(new->alias), XkbcKeyNameText(new->real));
523 ACTION("First definition ignored\n");
529 if (new->merge == MERGE_AUGMENT) {
538 if ((old->file_id == new->file_id && warningLevel > 0) ||
540 WARN("Multiple definitions for alias %s\n",
541 XkbcKeyNameText(old->alias));
542 ACTION("Using %s, ignoring %s\n",
543 XkbcKeyNameText(use), XkbcKeyNameText(ignore));
546 if (use != old->real)
547 memcpy(old->real, use, XkbKeyNameLength);
550 old->file_id = new->file_id;
551 old->merge = new->merge;
555 HandleAliasDef(KeyNamesInfo *info, KeyAliasDef *def, enum merge_mode merge,
560 list_foreach(alias, &info->aliases, entry) {
561 if (strncmp(alias->alias, def->alias, XkbKeyNameLength) == 0) {
563 InitAliasInfo(&new, merge, file_id, def->alias, def->real);
564 HandleAliasCollision(info, alias, &new);
569 alias = calloc(1, sizeof(*alias));
571 WSGO("Allocation failure in HandleAliasDef\n");
575 alias->file_id = file_id;
576 alias->merge = merge;
577 memcpy(alias->alias, def->alias, XkbKeyNameLength);
578 memcpy(alias->real, def->real, XkbKeyNameLength);
579 list_append(&alias->entry, &info->aliases);
584 #define MIN_KEYCODE_DEF 0
585 #define MAX_KEYCODE_DEF 1
588 * Handle the minimum/maximum statement of the xkb file.
589 * Sets explicitMin/Max of the info struct.
591 * @return 1 on success, 0 otherwise.
594 HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt)
596 ExprResult tmp, field;
600 if (ExprResolveLhs(info->keymap, stmt->name, &tmp, &field,
602 return 0; /* internal error, already reported */
604 if (tmp.str != NULL) {
605 ERROR("Unknown element %s encountered\n", tmp.str);
606 ACTION("Default for field %s ignored\n", field.str);
609 if (strcasecmp(field.str, "minimum") == 0)
610 which = MIN_KEYCODE_DEF;
611 else if (strcasecmp(field.str, "maximum") == 0)
612 which = MAX_KEYCODE_DEF;
614 ERROR("Unknown field encountered\n");
615 ACTION("Assigment to field %s ignored\n", field.str);
618 if (arrayNdx != NULL) {
619 ERROR("The %s setting is not an array\n", field.str);
620 ACTION("Illegal array reference ignored\n");
624 if (ExprResolveKeyCode(info->keymap->ctx, stmt->value, &tmp) == 0) {
625 ACTION("Assignment to field %s ignored\n", field.str);
628 if (tmp.uval > XKB_KEYCODE_MAX) {
630 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
631 tmp.uval, 0, XKB_KEYCODE_MAX);
632 ACTION("Value of \"%s\" not changed\n", field.str);
635 if (which == MIN_KEYCODE_DEF) {
636 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval)) {
638 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
639 tmp.uval, info->explicitMax);
640 ACTION("Minimum key code value not changed\n");
643 if ((info->computedMax > 0) && (info->computedMin < tmp.uval)) {
645 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
646 tmp.uval, info->computedMin);
647 ACTION("Minimum key code value not changed\n");
650 info->explicitMin = tmp.uval;
652 if (which == MAX_KEYCODE_DEF) {
653 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval)) {
654 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
655 tmp.uval, info->explicitMin);
656 ACTION("Maximum code value not changed\n");
659 if ((info->computedMax > 0) && (info->computedMax > tmp.uval)) {
661 ("Maximum code (%d) must be >= highest defined key (%d)\n",
662 tmp.uval, info->computedMax);
663 ACTION("Maximum code value not changed\n");
666 info->explicitMax = tmp.uval;
678 HandleIndicatorNameDef(KeyNamesInfo *info, IndicatorNameDef *def,
679 enum merge_mode merge)
681 IndicatorNameInfo ii;
684 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators)) {
686 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
690 InitIndicatorNameInfo(&ii, info);
692 if (!ExprResolveString(info->keymap->ctx, def->name, &tmp)) {
694 snprintf(buf, sizeof(buf), "%d", def->ndx);
696 return ReportBadType("indicator", "name", buf, "string");
698 ii.name = xkb_atom_intern(info->keymap->ctx, tmp.str);
700 ii.virtual = def->virtual;
701 if (!AddIndicatorName(info, merge, &ii))
707 * Handle the xkb_keycodes section of a xkb file.
708 * All information about parsed keys is stored in the info struct.
710 * Such a section may have include statements, in which case this function is
711 * semi-recursive (it calls HandleIncludeKeycodes, which may call
712 * HandleKeycodesFile again).
714 * @param info Struct to contain the fully parsed key information.
715 * @param file The input file (parsed xkb_keycodes section)
716 * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
719 HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
724 info->name = uDupString(file->name);
728 switch (stmt->stmtType) {
729 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
730 if (!HandleIncludeKeycodes(info, (IncludeStmt *) stmt))
733 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
734 if (!HandleKeycodeDef(info, (KeycodeDef *) stmt, merge))
737 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
738 if (!HandleAliasDef(info, (KeyAliasDef *) stmt, merge,
742 case StmtVarDef: /* e.g. minimum, maximum */
743 if (!HandleKeyNameVar(info, (VarDef *) stmt))
746 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
747 if (!HandleIndicatorNameDef(info, (IndicatorNameDef *) stmt,
753 ERROR("Keycode files may define key and indicator names only\n");
754 ACTION("Ignoring definition of %s\n",
756 StmtInterpDef) ? "a symbol interpretation" :
757 "virtual modifiers"));
761 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
766 if (info->errorCount > 10) {
768 ERROR("Too many errors\n");
770 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
777 ApplyAliases(KeyNamesInfo *info)
781 struct xkb_key_alias *old, *a;
782 AliasInfo *alias, *next;
784 struct xkb_keymap *keymap = info->keymap;
786 nOld = darray_size(keymap->key_aliases);
787 old = &darray_item(keymap->key_aliases, 0);
789 list_foreach(alias, &info->aliases, entry) {
792 lname = KeyNameToLong(alias->real);
793 key = FindNamedKey(keymap, lname, false, CreateKeyNames(keymap), 0);
795 if (warningLevel > 4) {
796 WARN("Attempt to alias %s to non-existent key %s\n",
797 XkbcKeyNameText(alias->alias),
798 XkbcKeyNameText(alias->real));
801 alias->alias[0] = '\0';
805 lname = KeyNameToLong(alias->alias);
806 key = FindNamedKey(keymap, lname, false, false, 0);
808 if (warningLevel > 4) {
809 WARN("Attempt to create alias with the name of a real key\n");
810 ACTION("Alias \"%s = %s\" ignored\n",
811 XkbcKeyNameText(alias->alias),
812 XkbcKeyNameText(alias->real));
814 alias->alias[0] = '\0';
823 for (i = 0, a = old; i < nOld; i++, a++) {
826 if (strncmp(a->alias, alias->alias, XkbKeyNameLength) != 0)
829 InitAliasInfo(&old_alias, MERGE_AUGMENT, 0, a->alias, a->real);
830 HandleAliasCollision(info, &old_alias, alias);
831 memcpy(old_alias.real, a->real, XkbKeyNameLength);
832 alias->alias[0] = '\0';
841 darray_resize0(keymap->key_aliases, nOld + nNew);
843 a = &darray_item(keymap->key_aliases, nOld);
844 list_foreach(alias, &info->aliases, entry) {
845 if (alias->alias[0] != '\0') {
846 strncpy(a->alias, alias->alias, XkbKeyNameLength);
847 strncpy(a->real, alias->real, XkbKeyNameLength);
853 list_foreach_safe(alias, next, &info->aliases, entry)
855 list_init(&info->aliases);
860 * Compile the xkb_keycodes section, parse it's output, return the results.
862 * @param file The parsed XKB file (may have include statements requiring
864 * @param result The effective keycodes, as gathered from the file.
865 * @param merge Merge strategy.
867 * @return true on success, false otherwise.
870 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap,
871 enum merge_mode merge)
874 KeyNamesInfo info; /* contains all the info after parsing */
875 IndicatorNameInfo *ii;
877 InitKeyNamesInfo(&info, keymap, file->id);
879 HandleKeycodesFile(&info, file, merge);
881 /* all the keys are now stored in info */
883 if (info.errorCount != 0)
886 if (info.explicitMin > 0) /* if "minimum" statement was present */
887 keymap->min_key_code = info.explicitMin;
889 keymap->min_key_code = info.computedMin;
891 if (info.explicitMax > 0) /* if "maximum" statement was present */
892 keymap->max_key_code = info.explicitMax;
894 keymap->max_key_code = info.computedMax;
896 darray_resize0(keymap->keys, keymap->max_key_code + 1);
897 for (kc = info.computedMin; kc <= info.computedMax; kc++)
898 LongToKeyName(darray_item(info.names, kc),
899 XkbKey(keymap, kc)->name);
902 keymap->keycodes_section_name = strdup(info.name);
904 list_foreach(ii, &info.leds, entry) {
905 free(keymap->indicator_names[ii->ndx - 1]);
906 keymap->indicator_names[ii->ndx - 1] =
907 xkb_atom_strdup(keymap->ctx, ii->name);
912 ClearKeyNamesInfo(&info);
916 ClearKeyNamesInfo(&info);