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 ********************************************************/
34 #include "parseutils.h"
37 longText(unsigned long val)
41 LongToKeyName(val, buf);
42 return XkbcKeyNameText(buf);
45 /***====================================================================***/
48 LongToKeyName(unsigned long val, char *name)
50 name[0] = ((val >> 24) & 0xff);
51 name[1] = ((val >> 16) & 0xff);
52 name[2] = ((val >> 8) & 0xff);
53 name[3] = (val & 0xff);
56 /***====================================================================***/
58 typedef struct _IndicatorNameInfo
66 typedef struct _KeyNamesInfo
68 char *name; /* e.g. evdev+aliases(qwerty) */
72 xkb_keycode_t computedMin; /* lowest keycode stored */
73 xkb_keycode_t computedMax; /* highest keycode stored */
74 xkb_keycode_t explicitMin;
75 xkb_keycode_t explicitMax;
76 xkb_keycode_t arraySize;
79 unsigned char *has_alt_forms;
80 IndicatorNameInfo *leds;
84 static void HandleKeycodesFile(XkbFile * file,
85 struct xkb_desc * xkb,
90 ResizeKeyNameArrays(KeyNamesInfo *info, int newMax)
95 tmp = uTypedRealloc(info->names, newMax + 1, unsigned long);
98 ("Couldn't reallocate for larger maximum keycode (%d)\n",
100 ACTION("Maximum key value not changed\n");
104 for (i = info->arraySize + 1; i <= newMax; i++)
107 tmp = uTypedRealloc(info->files, newMax + 1, unsigned);
110 ("Couldn't reallocate for larger maximum keycode (%d)\n",
112 ACTION("Maximum key value not changed\n");
116 for (i = info->arraySize + 1; i <= newMax; i++)
119 tmp = uTypedRealloc(info->has_alt_forms, newMax + 1, unsigned char);
122 ("Couldn't reallocate for larger maximum keycode (%d)\n",
124 ACTION("Maximum key value not changed\n");
127 info->has_alt_forms = tmp;
128 for (i = info->arraySize + 1; i <= newMax; i++)
129 info->has_alt_forms[i] = 0;
131 info->arraySize = newMax;
137 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
139 ii->defs.defined = 0;
140 ii->defs.merge = info->merge;
141 ii->defs.fileID = info->fileID;
142 ii->defs.next = NULL;
149 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
151 if (ii == info->leds)
153 ClearCommonInfo(&ii->defs);
158 static IndicatorNameInfo *
159 NextIndicatorName(KeyNamesInfo * info)
161 IndicatorNameInfo *ii;
163 ii = uTypedAlloc(IndicatorNameInfo);
166 InitIndicatorNameInfo(ii, info);
167 info->leds = (IndicatorNameInfo *) AddCommonInfo(&info->leds->defs,
173 static IndicatorNameInfo *
174 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
176 IndicatorNameInfo *old;
178 for (old = info->leds; old != NULL;
179 old = (IndicatorNameInfo *) old->defs.next)
187 static IndicatorNameInfo *
188 FindIndicatorByName(KeyNamesInfo * info, xkb_atom_t name)
190 IndicatorNameInfo *old;
192 for (old = info->leds; old != NULL;
193 old = (IndicatorNameInfo *) old->defs.next)
195 if (old->name == name)
202 AddIndicatorName(KeyNamesInfo * info, IndicatorNameInfo * new)
204 IndicatorNameInfo *old;
207 replace = (new->defs.merge == MergeReplace) ||
208 (new->defs.merge == MergeOverride);
209 old = FindIndicatorByName(info, new->name);
212 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
213 || (warningLevel > 9))
215 WARN("Multiple indicators named %s\n", XkbcAtomText(new->name));
216 if (old->ndx == new->ndx)
218 if (old->virtual != new->virtual)
221 old->virtual = new->virtual;
222 ACTION("Using %s instead of %s\n",
223 (old->virtual ? "virtual" : "real"),
224 (old->virtual ? "real" : "virtual"));
228 ACTION("Identical definitions ignored\n");
235 ACTION("Ignoring %d, using %d\n", old->ndx, new->ndx);
237 ACTION("Using %d, ignoring %d\n", old->ndx, new->ndx);
241 if (info->leds == old)
242 info->leds = (IndicatorNameInfo *) old->defs.next;
245 IndicatorNameInfo *tmp;
248 tmp = (IndicatorNameInfo *) tmp->defs.next)
250 if (tmp->defs.next == (CommonInfo *) old)
252 tmp->defs.next = old->defs.next;
261 old = FindIndicatorByIndex(info, new->ndx);
264 if (((old->defs.fileID == new->defs.fileID) && (warningLevel > 0))
265 || (warningLevel > 9))
267 WARN("Multiple names for indicator %d\n", new->ndx);
268 if ((old->name == new->name) && (old->virtual == new->virtual))
269 ACTION("Identical definitions ignored\n");
272 const char *oldType, *newType;
273 xkb_atom_t using, ignoring;
275 oldType = "virtual indicator";
277 oldType = "real indicator";
279 newType = "virtual indicator";
281 newType = "real indicator";
285 ignoring = old->name;
290 ignoring = new->name;
292 ACTION("Using %s %s, ignoring %s %s\n",
293 oldType, XkbcAtomText(using),
294 newType, XkbcAtomText(ignoring));
299 old->name = new->name;
300 old->virtual = new->virtual;
305 new = NextIndicatorName(info);
308 WSGO("Couldn't allocate name for indicator %d\n", old->ndx);
312 new->name = old->name;
314 new->virtual = old->virtual;
319 ClearKeyNamesInfo(KeyNamesInfo * info)
323 info->computedMax = info->explicitMax = info->explicitMin = 0;
324 info->computedMin = XKB_KEYCODE_MAX;
330 free(info->has_alt_forms);
331 info->has_alt_forms = NULL;
333 ClearIndicatorNameInfo(info->leds, info);
335 ClearAliases(&info->aliases);
339 InitKeyNamesInfo(KeyNamesInfo * info)
343 info->aliases = NULL;
346 info->has_alt_forms = NULL;
347 ClearKeyNamesInfo(info);
348 info->errorCount = 0;
352 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
356 for (i = info->computedMin; i <= info->computedMax; i++)
358 if (info->names[i] == name)
365 * Store the name of the key as a long in the info struct under the given
366 * keycode. If the same keys is referred to twice, print a warning.
367 * Note that the key's name is stored as a long, the keycode is the index.
370 AddKeyName(KeyNamesInfo * info,
372 char *name, unsigned merge, unsigned fileID, Bool reportCollisions)
377 if (kc > info->arraySize && !ResizeKeyNameArrays(info, kc)) {
378 ERROR("Couldn't resize KeyNames arrays for keycode %d\n", kc);
379 ACTION("Ignoring key %d\n", kc);
382 if (kc < info->computedMin)
383 info->computedMin = kc;
384 if (kc > info->computedMax)
385 info->computedMax = kc;
386 lval = KeyNameToLong(name);
388 if (reportCollisions)
390 reportCollisions = ((warningLevel > 7) ||
392 && (fileID == info->files[kc])));
395 if (info->names[kc] != 0)
399 LongToKeyName(info->names[kc], buf);
401 if (info->names[kc] == lval)
403 if (info->has_alt_forms[kc] || (merge == MergeAltForm))
405 info->has_alt_forms[kc] = True;
407 else if (reportCollisions)
409 WARN("Multiple identical key name definitions\n");
410 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
415 if (merge == MergeAugment)
417 if (reportCollisions)
419 WARN("Multiple names for keycode %d\n", kc);
420 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
426 if (reportCollisions)
428 WARN("Multiple names for keycode %d\n", kc);
429 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
435 old = FindKeyByLong(info, lval);
436 if ((old != 0) && (old != kc))
438 if (merge == MergeOverride)
440 info->names[old] = 0;
441 info->files[old] = 0;
442 info->has_alt_forms[old] = True;
443 if (reportCollisions)
445 WARN("Key name <%s> assigned to multiple keys\n", name);
446 ACTION("Using %d, ignoring %d\n", kc, old);
449 else if (merge != MergeAltForm)
451 if ((reportCollisions) && (warningLevel > 3))
453 WARN("Key name <%s> assigned to multiple keys\n", name);
454 ACTION("Using %d, ignoring %d\n", old, kc);
456 ("Use 'alternate' keyword to assign the same name to multiple keys\n");
462 info->has_alt_forms[old] = True;
465 info->names[kc] = lval;
466 info->files[kc] = fileID;
467 info->has_alt_forms[kc] = (merge == MergeAltForm);
471 /***====================================================================***/
474 MergeIncludedKeycodes(KeyNamesInfo * into, KeyNamesInfo * from,
480 if (from->errorCount > 0)
482 into->errorCount += from->errorCount;
485 if (into->name == NULL)
487 into->name = from->name;
490 if (from->computedMax > into->arraySize &&
491 !ResizeKeyNameArrays(into, from->computedMax)) {
492 ERROR("Couldn't resize KeyNames arrays for key %d\n",
494 ACTION("Ignoring include file %s\n", from->name);
495 into->errorCount += 10;
498 for (i = from->computedMin; i <= from->computedMax; i++)
501 if (from->names[i] == 0)
503 LongToKeyName(from->names[i], buf);
505 if (from->has_alt_forms[i])
506 thisMerge = MergeAltForm;
509 if (!AddKeyName(into, i, buf, thisMerge, from->fileID, False))
514 IndicatorNameInfo *led, *next;
515 for (led = from->leds; led != NULL; led = next)
517 if (merge != MergeDefault)
518 led->defs.merge = merge;
519 if (!AddIndicatorName(into, led))
521 next = (IndicatorNameInfo *) led->defs.next;
524 if (!MergeAliases(&into->aliases, &from->aliases, merge))
526 if (from->explicitMin != 0)
528 if ((into->explicitMin == 0)
529 || (into->explicitMin > from->explicitMin))
530 into->explicitMin = from->explicitMin;
532 if (from->explicitMax > 0)
534 if ((into->explicitMax == 0)
535 || (into->explicitMax < from->explicitMax))
536 into->explicitMax = from->explicitMax;
541 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
543 * @param stmt The include statement from the keymap file.
544 * @param xkb Unused for all but the xkb->flags.
545 * @param info Struct to store the key info in.
548 HandleIncludeKeycodes(IncludeStmt * stmt, struct xkb_desc * xkb, KeyNamesInfo * info)
552 KeyNamesInfo included;
555 memset(&included, 0, sizeof(included));
558 if ((stmt->file == NULL) && (stmt->map == NULL))
562 memset(info, 0, sizeof(KeyNamesInfo));
564 else if (stmt->file && strcmp(stmt->file, "computed") == 0)
566 xkb->flags |= AutoKeyNames;
567 info->explicitMin = 0;
568 info->explicitMax = XKB_KEYCODE_MAX;
569 return (info->errorCount == 0);
570 } /* parse file, store returned info in the xkb struct */
571 else if (ProcessIncludeFile(stmt, XkmKeyNamesIndex, &rtrn, &newMerge))
573 InitKeyNamesInfo(&included);
574 HandleKeycodesFile(rtrn, xkb, MergeOverride, &included);
575 if (stmt->stmt != NULL)
578 included.name = stmt->stmt;
585 info->errorCount += 10; /* XXX: why 10?? */
588 /* Do we have more than one include statement? */
589 if ((stmt->next != NULL) && (included.errorCount < 1))
593 KeyNamesInfo next_incl;
595 for (next = stmt->next; next != NULL; next = next->next)
597 if ((next->file == NULL) && (next->map == NULL))
600 MergeIncludedKeycodes(&included, info, next->merge);
601 ClearKeyNamesInfo(info);
603 else if (ProcessIncludeFile(next, XkmKeyNamesIndex, &rtrn, &op))
605 InitKeyNamesInfo(&next_incl);
606 HandleKeycodesFile(rtrn, xkb, MergeOverride, &next_incl);
607 MergeIncludedKeycodes(&included, &next_incl, op);
608 ClearKeyNamesInfo(&next_incl);
613 info->errorCount += 10; /* XXX: Why 10?? */
614 ClearKeyNamesInfo(&included);
623 MergeIncludedKeycodes(info, &included, newMerge);
624 ClearKeyNamesInfo(&included);
626 return (info->errorCount == 0);
630 * Parse the given statement and store the output in the info struct.
634 HandleKeycodeDef(KeycodeDef * stmt, unsigned merge, KeyNamesInfo * info)
636 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
637 (info->explicitMax != 0 && stmt->value > info->explicitMax))
639 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
640 ACTION("Must be in the range %d-%d inclusive\n",
642 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
645 if (stmt->merge != MergeDefault)
647 if (stmt->merge == MergeReplace)
648 merge = MergeOverride;
652 return AddKeyName(info, stmt->value, stmt->name, merge, info->fileID,
656 #define MIN_KEYCODE_DEF 0
657 #define MAX_KEYCODE_DEF 1
660 * Handle the minimum/maximum statement of the xkb file.
661 * Sets explicitMin/Max of the info struct.
663 * @return 1 on success, 0 otherwise.
666 HandleKeyNameVar(VarDef * stmt, KeyNamesInfo * info)
668 ExprResult tmp, field;
672 if (ExprResolveLhs(stmt->name, &tmp, &field, &arrayNdx) == 0)
673 return 0; /* internal error, already reported */
677 ERROR("Unknown element %s encountered\n", tmp.str);
678 ACTION("Default for field %s ignored\n", field.str);
681 if (strcasecmp(field.str, "minimum") == 0)
682 which = MIN_KEYCODE_DEF;
683 else if (strcasecmp(field.str, "maximum") == 0)
684 which = MAX_KEYCODE_DEF;
687 ERROR("Unknown field encountered\n");
688 ACTION("Assigment to field %s ignored\n", field.str);
691 if (arrayNdx != NULL)
693 ERROR("The %s setting is not an array\n", field.str);
694 ACTION("Illegal array reference ignored\n");
698 if (ExprResolveKeyCode(stmt->value, &tmp) == 0)
700 ACTION("Assignment to field %s ignored\n", field.str);
703 if (tmp.uval > XKB_KEYCODE_MAX)
706 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
707 tmp.uval, 0, XKB_KEYCODE_MAX);
708 ACTION("Value of \"%s\" not changed\n", field.str);
711 if (which == MIN_KEYCODE_DEF)
713 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval))
716 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
717 tmp.uval, info->explicitMax);
718 ACTION("Minimum key code value not changed\n");
721 if ((info->computedMax > 0) && (info->computedMin < tmp.uval))
724 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
725 tmp.uval, info->computedMin);
726 ACTION("Minimum key code value not changed\n");
729 info->explicitMin = tmp.uval;
731 if (which == MAX_KEYCODE_DEF)
733 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval))
735 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
736 tmp.uval, info->explicitMin);
737 ACTION("Maximum code value not changed\n");
740 if ((info->computedMax > 0) && (info->computedMax > tmp.uval))
743 ("Maximum code (%d) must be >= highest defined key (%d)\n",
744 tmp.uval, info->computedMax);
745 ACTION("Maximum code value not changed\n");
748 info->explicitMax = tmp.uval;
760 HandleIndicatorNameDef(IndicatorNameDef * def,
761 unsigned merge, KeyNamesInfo * info)
763 IndicatorNameInfo ii;
766 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
769 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
773 InitIndicatorNameInfo(&ii, info);
775 if (!ExprResolveString(def->name, &tmp))
778 snprintf(buf, sizeof(buf), "%d", def->ndx);
780 return ReportBadType("indicator", "name", buf, "string");
782 ii.name = xkb_intern_atom(tmp.str);
784 ii.virtual = def->virtual;
785 if (!AddIndicatorName(info, &ii))
791 * Handle the xkb_keycodes section of a xkb file.
792 * All information about parsed keys is stored in the info struct.
794 * Such a section may have include statements, in which case this function is
795 * semi-recursive (it calls HandleIncludeKeycodes, which may call
796 * HandleKeycodesFile again).
798 * @param file The input file (parsed xkb_keycodes section)
799 * @param xkb Necessary to pass down, may have flags changed.
800 * @param merge Merge strategy (MergeOverride, etc.)
801 * @param info Struct to contain the fully parsed key information.
804 HandleKeycodesFile(XkbFile * file,
805 struct xkb_desc * xkb, unsigned merge, KeyNamesInfo * info)
810 info->name = uDupString(file->name);
814 switch (stmt->stmtType)
816 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
817 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, xkb, info))
820 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
821 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
824 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
825 if (!HandleAliasDef((KeyAliasDef *) stmt,
826 merge, info->fileID, &info->aliases))
829 case StmtVarDef: /* e.g. minimum, maximum */
830 if (!HandleKeyNameVar((VarDef *) stmt, info))
833 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
834 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt,
842 ERROR("Keycode files may define key and indicator names only\n");
843 ACTION("Ignoring definition of %s\n",
845 StmtInterpDef) ? "a symbol interpretation" :
846 "virtual modifiers"));
850 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
855 if (info->errorCount > 10)
858 ERROR("Too many errors\n");
860 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
867 * Compile the xkb_keycodes section, parse it's output, return the results.
869 * @param file The parsed XKB file (may have include statements requiring
871 * @param result The effective keycodes, as gathered from the file.
872 * @param merge Merge strategy.
874 * @return True on success, False otherwise.
877 CompileKeycodes(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
879 KeyNamesInfo info; /* contains all the info after parsing */
881 InitKeyNamesInfo(&info);
882 HandleKeycodesFile(file, xkb, merge, &info);
884 /* all the keys are now stored in info */
886 if (info.errorCount == 0)
888 if (info.explicitMin > 0) /* if "minimum" statement was present */
889 xkb->min_key_code = info.explicitMin;
891 xkb->min_key_code = info.computedMin;
892 if (info.explicitMax > 0) /* if "maximum" statement was present */
893 xkb->max_key_code = info.explicitMax;
895 xkb->max_key_code = info.computedMax;
896 if (XkbcAllocNames(xkb, XkbKeyNamesMask | XkbIndicatorNamesMask, 0)
900 for (i = info.computedMin; i <= info.computedMax; i++)
901 LongToKeyName(info.names[i], xkb->names->keys[i].name);
905 WSGO("Cannot create struct xkb_names in CompileKeycodes\n");
910 IndicatorNameInfo *ii;
911 if (XkbcAllocIndicatorMaps(xkb) != Success)
913 WSGO("Couldn't allocate IndicatorRec in CompileKeycodes\n");
914 ACTION("Physical indicators not set\n");
916 for (ii = info.leds; ii != NULL;
917 ii = (IndicatorNameInfo *) ii->defs.next)
919 free(UNCONSTIFY(xkb->names->indicators[ii->ndx - 1]));
920 xkb->names->indicators[ii->ndx - 1] = XkbcAtomGetString(ii->name);
921 if (xkb->indicators != NULL)
924 bit = 1 << (ii->ndx - 1);
926 xkb->indicators->phys_indicators &= ~bit;
928 xkb->indicators->phys_indicators |= bit;
933 ApplyAliases(xkb, &info.aliases);
934 ClearKeyNamesInfo(&info);
937 ClearKeyNamesInfo(&info);