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_keymap * 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;
144 ii->name = XKB_ATOM_NONE;
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_keymap * 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(xkb->context, stmt, XkmKeyNamesIndex, &rtrn,
574 InitKeyNamesInfo(&included);
575 HandleKeycodesFile(rtrn, xkb, MergeOverride, &included);
576 if (stmt->stmt != NULL)
579 included.name = stmt->stmt;
586 info->errorCount += 10; /* XXX: why 10?? */
589 /* Do we have more than one include statement? */
590 if ((stmt->next != NULL) && (included.errorCount < 1))
594 KeyNamesInfo next_incl;
596 for (next = stmt->next; next != NULL; next = next->next)
598 if ((next->file == NULL) && (next->map == NULL))
601 MergeIncludedKeycodes(&included, info, next->merge);
602 ClearKeyNamesInfo(info);
604 else if (ProcessIncludeFile(xkb->context, next, XkmKeyNamesIndex,
607 InitKeyNamesInfo(&next_incl);
608 HandleKeycodesFile(rtrn, xkb, MergeOverride, &next_incl);
609 MergeIncludedKeycodes(&included, &next_incl, op);
610 ClearKeyNamesInfo(&next_incl);
615 info->errorCount += 10; /* XXX: Why 10?? */
616 ClearKeyNamesInfo(&included);
625 MergeIncludedKeycodes(info, &included, newMerge);
626 ClearKeyNamesInfo(&included);
628 return (info->errorCount == 0);
632 * Parse the given statement and store the output in the info struct.
636 HandleKeycodeDef(KeycodeDef * stmt, unsigned merge, KeyNamesInfo * info)
638 if ((info->explicitMin != 0 && stmt->value < info->explicitMin) ||
639 (info->explicitMax != 0 && stmt->value > info->explicitMax))
641 ERROR("Illegal keycode %lu for name <%s>\n", stmt->value, stmt->name);
642 ACTION("Must be in the range %d-%d inclusive\n",
644 info->explicitMax ? info->explicitMax : XKB_KEYCODE_MAX);
647 if (stmt->merge != MergeDefault)
649 if (stmt->merge == MergeReplace)
650 merge = MergeOverride;
654 return AddKeyName(info, stmt->value, stmt->name, merge, info->fileID,
658 #define MIN_KEYCODE_DEF 0
659 #define MAX_KEYCODE_DEF 1
662 * Handle the minimum/maximum statement of the xkb file.
663 * Sets explicitMin/Max of the info struct.
665 * @return 1 on success, 0 otherwise.
668 HandleKeyNameVar(VarDef * stmt, KeyNamesInfo * info)
670 ExprResult tmp, field;
674 if (ExprResolveLhs(stmt->name, &tmp, &field, &arrayNdx) == 0)
675 return 0; /* internal error, already reported */
679 ERROR("Unknown element %s encountered\n", tmp.str);
680 ACTION("Default for field %s ignored\n", field.str);
683 if (strcasecmp(field.str, "minimum") == 0)
684 which = MIN_KEYCODE_DEF;
685 else if (strcasecmp(field.str, "maximum") == 0)
686 which = MAX_KEYCODE_DEF;
689 ERROR("Unknown field encountered\n");
690 ACTION("Assigment to field %s ignored\n", field.str);
693 if (arrayNdx != NULL)
695 ERROR("The %s setting is not an array\n", field.str);
696 ACTION("Illegal array reference ignored\n");
700 if (ExprResolveKeyCode(stmt->value, &tmp) == 0)
702 ACTION("Assignment to field %s ignored\n", field.str);
705 if (tmp.uval > XKB_KEYCODE_MAX)
708 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
709 tmp.uval, 0, XKB_KEYCODE_MAX);
710 ACTION("Value of \"%s\" not changed\n", field.str);
713 if (which == MIN_KEYCODE_DEF)
715 if ((info->explicitMax > 0) && (info->explicitMax < tmp.uval))
718 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
719 tmp.uval, info->explicitMax);
720 ACTION("Minimum key code value not changed\n");
723 if ((info->computedMax > 0) && (info->computedMin < tmp.uval))
726 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
727 tmp.uval, info->computedMin);
728 ACTION("Minimum key code value not changed\n");
731 info->explicitMin = tmp.uval;
733 if (which == MAX_KEYCODE_DEF)
735 if ((info->explicitMin > 0) && (info->explicitMin > tmp.uval))
737 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
738 tmp.uval, info->explicitMin);
739 ACTION("Maximum code value not changed\n");
742 if ((info->computedMax > 0) && (info->computedMax > tmp.uval))
745 ("Maximum code (%d) must be >= highest defined key (%d)\n",
746 tmp.uval, info->computedMax);
747 ACTION("Maximum code value not changed\n");
750 info->explicitMax = tmp.uval;
762 HandleIndicatorNameDef(IndicatorNameDef *def, KeyNamesInfo *info)
764 IndicatorNameInfo ii;
767 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
770 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
774 InitIndicatorNameInfo(&ii, info);
776 if (!ExprResolveString(def->name, &tmp))
779 snprintf(buf, sizeof(buf), "%d", def->ndx);
781 return ReportBadType("indicator", "name", buf, "string");
783 ii.name = xkb_intern_atom(tmp.str);
785 ii.virtual = def->virtual;
786 if (!AddIndicatorName(info, &ii))
792 * Handle the xkb_keycodes section of a xkb file.
793 * All information about parsed keys is stored in the info struct.
795 * Such a section may have include statements, in which case this function is
796 * semi-recursive (it calls HandleIncludeKeycodes, which may call
797 * HandleKeycodesFile again).
799 * @param file The input file (parsed xkb_keycodes section)
800 * @param xkb Necessary to pass down, may have flags changed.
801 * @param merge Merge strategy (MergeOverride, etc.)
802 * @param info Struct to contain the fully parsed key information.
805 HandleKeycodesFile(XkbFile * file,
806 struct xkb_keymap * xkb, unsigned merge, KeyNamesInfo * info)
811 info->name = uDupString(file->name);
815 switch (stmt->stmtType)
817 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
818 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, xkb, info))
821 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
822 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
825 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
826 if (!HandleAliasDef((KeyAliasDef *) stmt,
827 merge, info->fileID, &info->aliases))
830 case StmtVarDef: /* e.g. minimum, maximum */
831 if (!HandleKeyNameVar((VarDef *) stmt, info))
834 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
835 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt, info))
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_keymap * 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);
924 ApplyAliases(xkb, &info.aliases);
925 ClearKeyNamesInfo(&info);
928 ClearKeyNamesInfo(&info);