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 ********************************************************/
29 #include "parseutils.h"
33 longText(unsigned long val)
37 LongToKeyName(val, buf);
38 return XkbcKeyNameText(buf);
41 /***====================================================================***/
44 LongToKeyName(unsigned long val, char *name)
46 name[0] = ((val >> 24) & 0xff);
47 name[1] = ((val >> 16) & 0xff);
48 name[2] = ((val >> 8) & 0xff);
49 name[3] = (val & 0xff);
52 /***====================================================================***/
54 typedef struct _IndicatorNameInfo
62 typedef struct _KeyNamesInfo
64 char *name; /* e.g. evdev+aliases(qwerty) */
67 enum merge_mode merge;
68 xkb_keycode_t computedMin; /* lowest keycode stored */
69 xkb_keycode_t computedMax; /* highest keycode stored */
70 xkb_keycode_t explicitMin;
71 xkb_keycode_t explicitMax;
72 darray(unsigned long) names;
73 darray(unsigned int) files;
74 darray(unsigned char) has_alt_forms;
75 IndicatorNameInfo *leds;
79 static void HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
80 enum merge_mode merge, KeyNamesInfo *info);
83 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
85 if (newMax < darray_size(info->names))
88 darray_resize0(info->names, newMax + 1);
89 darray_resize0(info->files, newMax + 1);
90 darray_resize0(info->has_alt_forms, newMax + 1);
94 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
97 ii->defs.merge = info->merge;
98 ii->defs.fileID = info->fileID;
101 ii->name = XKB_ATOM_NONE;
106 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
108 if (ii == info->leds)
110 ClearCommonInfo(&ii->defs);
115 static IndicatorNameInfo *
116 NextIndicatorName(KeyNamesInfo * info)
118 IndicatorNameInfo *ii;
120 ii = uTypedAlloc(IndicatorNameInfo);
123 InitIndicatorNameInfo(ii, info);
124 info->leds = AddCommonInfo(&info->leds->defs, &ii->defs);
129 static IndicatorNameInfo *
130 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
132 IndicatorNameInfo *old;
134 for (old = info->leds; old != NULL;
135 old = (IndicatorNameInfo *) old->defs.next)
143 static IndicatorNameInfo *
144 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
146 IndicatorNameInfo *old;
148 for (old = info->leds; old != NULL;
149 old = (IndicatorNameInfo *) old->defs.next)
151 if (old->name == name)
158 AddIndicatorName(KeyNamesInfo *info, struct xkb_keymap *keymap, enum merge_mode merge,
159 IndicatorNameInfo *new)
161 IndicatorNameInfo *old;
164 replace = (merge == MERGE_REPLACE) || (merge == MERGE_OVERRIDE);
165 old = FindIndicatorByName(info, new->name);
168 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
169 || (warningLevel > 9))
171 WARN("Multiple indicators named %s\n",
172 xkb_atom_text(keymap->ctx, new->name));
173 if (old->ndx == new->ndx)
175 if (old->virtual != new->virtual)
178 old->virtual = new->virtual;
179 ACTION("Using %s instead of %s\n",
180 (old->virtual ? "virtual" : "real"),
181 (old->virtual ? "real" : "virtual"));
185 ACTION("Identical definitions ignored\n");
192 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
194 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
198 if (info->leds == old)
199 info->leds = (IndicatorNameInfo *) old->defs.next;
202 IndicatorNameInfo *tmp;
205 tmp = (IndicatorNameInfo *) tmp->defs.next)
207 if (tmp->defs.next == (CommonInfo *) old)
209 tmp->defs.next = old->defs.next;
218 old = FindIndicatorByIndex(info, new->ndx);
221 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
222 || (warningLevel > 9))
224 WARN("Multiple names for indicator %d\n", new->ndx);
225 if ((old->name == new->name) && (old->virtual == new->virtual))
226 ACTION("Identical definitions ignored\n");
229 const char *oldType, *newType;
230 xkb_atom_t using, ignoring;
232 oldType = "virtual indicator";
234 oldType = "real indicator";
236 newType = "virtual indicator";
238 newType = "real indicator";
242 ignoring = old->name;
247 ignoring = new->name;
249 ACTION("Using %s %s, ignoring %s %s\n",
250 oldType, xkb_atom_text(keymap->ctx, using),
251 newType, xkb_atom_text(keymap->ctx, ignoring));
256 old->name = new->name;
257 old->virtual = new->virtual;
262 new = NextIndicatorName(info);
265 WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
269 new->name = old->name;
271 new->virtual = old->virtual;
276 ClearKeyNamesInfo(KeyNamesInfo * info)
280 info->computedMax = info->explicitMax = info->explicitMin = 0;
281 info->computedMin = XKB_KEYCODE_MAX;
282 darray_free(info->names);
283 darray_free(info->files);
284 darray_free(info->has_alt_forms);
286 ClearIndicatorNameInfo(info->leds, info);
288 ClearAliases(&info->aliases);
292 InitKeyNamesInfo(KeyNamesInfo * info)
296 info->aliases = NULL;
297 darray_init(info->names);
298 darray_init(info->files);
299 darray_init(info->has_alt_forms);
300 ClearKeyNamesInfo(info);
301 info->errorCount = 0;
305 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
309 for (i = info->computedMin; i <= info->computedMax; i++)
310 if (darray_item(info->names, i) == name)
317 * Store the name of the key as a long in the info struct under the given
318 * keycode. If the same keys is referred to twice, print a warning.
319 * Note that the key's name is stored as a long, the keycode is the index.
322 AddKeyName(KeyNamesInfo * info,
324 char *name, enum merge_mode merge, unsigned fileID, bool reportCollisions)
329 ResizeKeyNameArrays(info, kc);
331 if (kc < info->computedMin)
332 info->computedMin = kc;
333 if (kc > info->computedMax)
334 info->computedMax = kc;
335 lval = KeyNameToLong(name);
337 if (reportCollisions)
339 reportCollisions = (warningLevel > 7 ||
341 fileID == darray_item(info->files, kc)));
344 if (darray_item(info->names, kc) != 0)
348 LongToKeyName(darray_item(info->names, kc), buf);
350 if (darray_item(info->names, kc) == lval)
352 if (darray_item(info->has_alt_forms, kc) || (merge == MERGE_ALT_FORM)) {
353 darray_item(info->has_alt_forms, kc) = true;
355 else if (reportCollisions) {
356 WARN("Multiple identical key name definitions\n");
357 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
362 if (merge == MERGE_AUGMENT)
364 if (reportCollisions)
366 WARN("Multiple names for keycode %d\n", kc);
367 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
373 if (reportCollisions)
375 WARN("Multiple names for keycode %d\n", kc);
376 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
378 darray_item(info->names, kc) = 0;
379 darray_item(info->files, kc) = 0;
382 old = FindKeyByLong(info, lval);
383 if ((old != 0) && (old != kc))
385 if (merge == MERGE_OVERRIDE)
387 darray_item(info->names, old) = 0;
388 darray_item(info->files, old) = 0;
389 darray_item(info->has_alt_forms, old) = true;
390 if (reportCollisions)
392 WARN("Key name <%s> assigned to multiple keys\n", name);
393 ACTION("Using %d, ignoring %d\n", kc, old);
396 else if (merge != MERGE_ALT_FORM)
398 if ((reportCollisions) && (warningLevel > 3))
400 WARN("Key name <%s> assigned to multiple keys\n", name);
401 ACTION("Using %d, ignoring %d\n", old, kc);
403 ("Use 'alternate' keyword to assign the same name to multiple keys\n");
409 darray_item(info->has_alt_forms, old) = true;
412 darray_item(info->names, kc) = lval;
413 darray_item(info->files, kc) = fileID;
414 darray_item(info->has_alt_forms, kc) = (merge == MERGE_ALT_FORM);
418 /***====================================================================***/
421 MergeIncludedKeycodes(KeyNamesInfo *into, struct xkb_keymap *keymap,
422 KeyNamesInfo *from, enum merge_mode merge)
427 if (from->errorCount > 0)
429 into->errorCount += from->errorCount;
432 if (into->name == NULL)
434 into->name = from->name;
438 ResizeKeyNameArrays(into, from->computedMax);
440 for (i = from->computedMin; i <= from->computedMax; i++)
443 if (darray_item(from->names, i) == 0)
445 LongToKeyName(darray_item(from->names, i), buf);
447 if (darray_item(from->has_alt_forms, i))
448 thisMerge = MERGE_ALT_FORM;
451 if (!AddKeyName(into, i, buf, thisMerge, from->fileID, false))
456 IndicatorNameInfo *led, *next;
457 for (led = from->leds; led != NULL; led = next)
459 if (merge != MERGE_DEFAULT)
460 led->defs.merge = merge;
461 if (!AddIndicatorName(into, keymap, led->defs.merge, led))
463 next = (IndicatorNameInfo *) led->defs.next;
466 if (!MergeAliases(&into->aliases, &from->aliases, merge))
468 if (from->explicitMin != 0)
470 if ((into->explicitMin == 0)
471 || (into->explicitMin > from->explicitMin))
472 into->explicitMin = from->explicitMin;
474 if (from->explicitMax > 0)
476 if ((into->explicitMax == 0)
477 || (into->explicitMax < from->explicitMax))
478 into->explicitMax = from->explicitMax;
483 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
485 * @param stmt The include statement from the keymap file.
486 * @param keymap Unused for all but the keymap->flags.
487 * @param info Struct to store the key info in.
490 HandleIncludeKeycodes(IncludeStmt *stmt, struct xkb_keymap *keymap,
495 KeyNamesInfo included;
498 memset(&included, 0, sizeof(included));
501 if ((stmt->file == NULL) && (stmt->map == NULL))
505 memset(info, 0, sizeof(KeyNamesInfo));
507 else if (stmt->file && strcmp(stmt->file, "computed") == 0)
509 keymap->flags |= AutoKeyNames;
510 info->explicitMin = 0;
511 info->explicitMax = XKB_KEYCODE_MAX;
512 return (info->errorCount == 0);
513 } /* parse file, store returned info in the xkb struct */
514 else if (ProcessIncludeFile(keymap->ctx, stmt, FILE_TYPE_KEYCODES, &rtrn,
517 InitKeyNamesInfo(&included);
518 HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &included);
519 if (stmt->stmt != NULL)
522 included.name = stmt->stmt;
529 info->errorCount += 10; /* XXX: why 10?? */
532 /* Do we have more than one include statement? */
533 if ((stmt->next != NULL) && (included.errorCount < 1))
537 KeyNamesInfo next_incl;
539 for (next = stmt->next; next != NULL; next = next->next)
541 if ((next->file == NULL) && (next->map == NULL))
544 MergeIncludedKeycodes(&included, keymap, info, next->merge);
545 ClearKeyNamesInfo(info);
547 else if (ProcessIncludeFile(keymap->ctx, next, FILE_TYPE_KEYCODES,
550 InitKeyNamesInfo(&next_incl);
551 HandleKeycodesFile(rtrn, keymap, MERGE_OVERRIDE, &next_incl);
552 MergeIncludedKeycodes(&included, keymap, &next_incl, op);
553 ClearKeyNamesInfo(&next_incl);
558 info->errorCount += 10; /* XXX: Why 10?? */
559 ClearKeyNamesInfo(&included);
568 MergeIncludedKeycodes(info, keymap, &included, newMerge);
569 ClearKeyNamesInfo(&included);
571 return (info->errorCount == 0);
575 * Parse the given statement and store the output in the info struct.
579 HandleKeycodeDef(KeycodeDef *stmt, enum merge_mode merge, KeyNamesInfo *info)
581 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
582 (info->explicitMax != 0 && stmt->value > info->explicitMax))
584 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
585 ACTION("Must be in the range %d-%d inclusive\n",
587 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
590 if (stmt->merge != MERGE_DEFAULT)
592 if (stmt->merge == MERGE_REPLACE)
593 merge = MERGE_OVERRIDE;
597 return AddKeyName(info, stmt->value, stmt->name, merge, info->fileID,
601 #define MIN_KEYCODE_DEF 0
602 #define MAX_KEYCODE_DEF 1
605 * Handle the minimum/maximum statement of the xkb file.
606 * Sets explicitMin/Max of the info struct.
608 * @return 1 on success, 0 otherwise.
611 HandleKeyNameVar(VarDef *stmt, struct xkb_keymap *keymap, KeyNamesInfo *info)
613 ExprResult tmp, field;
617 if (ExprResolveLhs(keymap, stmt->name, &tmp, &field, &arrayNdx) == 0)
618 return 0; /* internal error, already reported */
622 ERROR("Unknown element %s encountered\n", tmp.str);
623 ACTION("Default for field %s ignored\n", field.str);
626 if (strcasecmp(field.str, "minimum") == 0)
627 which = MIN_KEYCODE_DEF;
628 else if (strcasecmp(field.str, "maximum") == 0)
629 which = MAX_KEYCODE_DEF;
632 ERROR("Unknown field encountered\n");
633 ACTION("Assigment to field %s ignored\n", field.str);
636 if (arrayNdx != NULL)
638 ERROR("The %s setting is not an array\n", field.str);
639 ACTION("Illegal array reference ignored\n");
643 if (ExprResolveKeyCode(keymap->ctx, stmt->value, &tmp) == 0)
645 ACTION("Assignment to field %s ignored\n", field.str);
648 if (tmp.uval > XKB_KEYCODE_MAX)
651 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
652 tmp.uval, 0, XKB_KEYCODE_MAX);
653 ACTION("Value of \"%s\" not changed\n", field.str);
656 if (which == MIN_KEYCODE_DEF)
658 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval))
661 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
662 tmp.uval, info->explicitMax);
663 ACTION("Minimum key code value not changed\n");
666 if ((info->computedMax > 0) && (info->computedMin < tmp.uval))
669 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
670 tmp.uval, info->computedMin);
671 ACTION("Minimum key code value not changed\n");
674 info->explicitMin = tmp.uval;
676 if (which == MAX_KEYCODE_DEF)
678 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval))
680 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
681 tmp.uval, info->explicitMin);
682 ACTION("Maximum code value not changed\n");
685 if ((info->computedMax > 0) && (info->computedMax > tmp.uval))
688 ("Maximum code (%d) must be >= highest defined key (%d)\n",
689 tmp.uval, info->computedMax);
690 ACTION("Maximum code value not changed\n");
693 info->explicitMax = tmp.uval;
705 HandleIndicatorNameDef(IndicatorNameDef *def, struct xkb_keymap *keymap,
706 enum merge_mode merge, KeyNamesInfo *info)
708 IndicatorNameInfo ii;
711 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
714 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
718 InitIndicatorNameInfo(&ii, info);
720 if (!ExprResolveString(keymap->ctx, def->name, &tmp))
723 snprintf(buf, sizeof(buf), "%d", def->ndx);
725 return ReportBadType("indicator", "name", buf, "string");
727 ii.name = xkb_atom_intern(keymap->ctx, tmp.str);
729 ii.virtual = def->virtual;
730 if (!AddIndicatorName(info, keymap, merge, &ii))
736 * Handle the xkb_keycodes section of a xkb file.
737 * All information about parsed keys is stored in the info struct.
739 * Such a section may have include statements, in which case this function is
740 * semi-recursive (it calls HandleIncludeKeycodes, which may call
741 * HandleKeycodesFile again).
743 * @param file The input file (parsed xkb_keycodes section)
744 * @param xkb Necessary to pass down, may have flags changed.
745 * @param merge Merge strategy (MERGE_OVERRIDE, etc.)
746 * @param info Struct to contain the fully parsed key information.
749 HandleKeycodesFile(XkbFile *file, struct xkb_keymap *keymap,
750 enum merge_mode merge, KeyNamesInfo *info)
755 info->name = uDupString(file->name);
759 switch (stmt->stmtType)
761 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
762 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, keymap, info))
765 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
766 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
769 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
770 if (!HandleAliasDef((KeyAliasDef *) stmt, merge, info->fileID,
774 case StmtVarDef: /* e.g. minimum, maximum */
775 if (!HandleKeyNameVar((VarDef *) stmt, keymap, info))
778 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
779 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt, keymap,
785 ERROR("Keycode files may define key and indicator names only\n");
786 ACTION("Ignoring definition of %s\n",
788 StmtInterpDef) ? "a symbol interpretation" :
789 "virtual modifiers"));
793 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
798 if (info->errorCount > 10)
801 ERROR("Too many errors\n");
803 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
810 * Compile the xkb_keycodes section, parse it's output, return the results.
812 * @param file The parsed XKB file (may have include statements requiring
814 * @param result The effective keycodes, as gathered from the file.
815 * @param merge Merge strategy.
817 * @return true on success, false otherwise.
820 CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap, enum merge_mode merge)
822 KeyNamesInfo info; /* contains all the info after parsing */
824 InitKeyNamesInfo(&info);
826 HandleKeycodesFile(file, keymap, merge, &info);
828 /* all the keys are now stored in info */
830 if (info.errorCount != 0)
833 if (info.explicitMin > 0) /* if "minimum" statement was present */
834 keymap->min_key_code = info.explicitMin;
836 keymap->min_key_code = info.computedMin;
838 if (info.explicitMax > 0) /* if "maximum" statement was present */
839 keymap->max_key_code = info.explicitMax;
841 keymap->max_key_code = info.computedMax;
843 if (XkbcAllocNames(keymap, XkbKeyNamesMask | XkbIndicatorNamesMask, 0)
846 for (i = info.computedMin; i <= info.computedMax; i++)
847 LongToKeyName(darray_item(info.names, i),
848 darray_item(keymap->names->keys, i).name);
850 WSGO("Cannot create struct xkb_names in CompileKeycodes\n");
855 IndicatorNameInfo *ii;
856 if (XkbcAllocIndicatorMaps(keymap) != Success) {
857 WSGO("Couldn't allocate IndicatorRec in CompileKeycodes\n");
858 ACTION("Physical indicators not set\n");
861 for (ii = info.leds; ii; ii = (IndicatorNameInfo *)ii->defs.next) {
862 free(keymap->names->indicators[ii->ndx - 1]);
863 keymap->names->indicators[ii->ndx - 1] =
864 xkb_atom_strdup(keymap->ctx, ii->name);
868 ApplyAliases(keymap, &info.aliases);
870 ClearKeyNamesInfo(&info);
874 ClearKeyNamesInfo(&info);