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 ********************************************************/
35 longText(unsigned long val)
39 LongToKeyName(val, buf);
40 return XkbcKeyNameText(buf);
43 /***====================================================================***/
46 LongToKeyName(unsigned long val, char *name)
48 name[0] = ((val >> 24) & 0xff);
49 name[1] = ((val >> 16) & 0xff);
50 name[2] = ((val >> 8) & 0xff);
51 name[3] = (val & 0xff);
55 /***====================================================================***/
57 typedef struct _IndicatorNameInfo
65 typedef struct _KeyNamesInfo
67 char *name; /* e.g. evdev+aliases(qwerty) */
71 int computedMin; /* lowest keycode stored */
72 int computedMax; /* highest keycode stored */
77 unsigned long names[XkbMaxLegalKeyCode + 1]; /* 4-letter name of key, keycode is the index */
78 unsigned files[XkbMaxLegalKeyCode + 1];
79 unsigned char has_alt_forms[XkbMaxLegalKeyCode + 1];
80 IndicatorNameInfo *leds;
84 static void HandleKeycodesFile(XkbFile * file,
90 InitIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
93 ii->defs.merge = info->merge;
94 ii->defs.fileID = info->fileID;
103 ClearIndicatorNameInfo(IndicatorNameInfo * ii, KeyNamesInfo * info)
105 if (ii == info->leds)
107 ClearCommonInfo(&ii->defs);
113 static IndicatorNameInfo *
114 NextIndicatorName(KeyNamesInfo * info)
116 IndicatorNameInfo *ii;
118 ii = uTypedAlloc(IndicatorNameInfo);
121 InitIndicatorNameInfo(ii, info);
122 info->leds = (IndicatorNameInfo *) AddCommonInfo(&info->leds->defs,
128 static IndicatorNameInfo *
129 FindIndicatorByIndex(KeyNamesInfo * info, int ndx)
131 IndicatorNameInfo *old;
133 for (old = info->leds; old != NULL;
134 old = (IndicatorNameInfo *) old->defs.next)
142 static IndicatorNameInfo *
143 FindIndicatorByName(KeyNamesInfo * info, Atom name)
145 IndicatorNameInfo *old;
147 for (old = info->leds; old != NULL;
148 old = (IndicatorNameInfo *) old->defs.next)
150 if (old->name == name)
157 AddIndicatorName(KeyNamesInfo * info, IndicatorNameInfo * new)
159 IndicatorNameInfo *old;
163 replace = (new->defs.merge == MergeReplace) ||
164 (new->defs.merge == MergeOverride);
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", XkbcAtomText(new->name));
172 if (old->ndx == new->ndx)
174 if (old->virtual != new->virtual)
177 old->virtual = new->virtual;
178 action = "Using %s instead of %s\n";
182 action = "Identical definitions ignored\n";
184 ACTION(action, (old->virtual ? "virtual" : "real"),
185 (old->virtual ? "real" : "virtual"));
191 action = "Ignoring %d, using %d\n";
193 action = "Using %d, ignoring %d\n";
194 ACTION(action, 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 Atom 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, XkbcAtomText(using),
251 newType, XkbcAtomText(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", new->ndx);
269 new->name = old->name;
271 new->virtual = old->virtual;
276 ClearKeyNamesInfo(KeyNamesInfo * info)
278 if (info->name != NULL)
281 info->computedMax = info->explicitMax = info->explicitMin = -1;
282 info->computedMin = 256;
283 info->effectiveMin = 8;
284 info->effectiveMax = 255;
285 bzero((char *) info->names, sizeof(info->names));
286 bzero((char *) info->files, sizeof(info->files));
287 bzero((char *) info->has_alt_forms, sizeof(info->has_alt_forms));
289 ClearIndicatorNameInfo(info->leds, info);
291 ClearAliases(&info->aliases);
296 InitKeyNamesInfo(KeyNamesInfo * info)
300 info->aliases = NULL;
301 ClearKeyNamesInfo(info);
302 info->errorCount = 0;
307 FindKeyByLong(KeyNamesInfo * info, unsigned long name)
311 for (i = info->effectiveMin; i <= info->effectiveMax; i++)
313 if (info->names[i] == name)
320 * Store the name of the key as a long in the info struct under the given
321 * keycode. If the same keys is referred to twice, print a warning.
322 * Note that the key's name is stored as a long, the keycode is the index.
325 AddKeyName(KeyNamesInfo * info,
327 char *name, unsigned merge, unsigned fileID, Bool reportCollisions)
332 if ((kc < info->effectiveMin) || (kc > info->effectiveMax))
334 ERROR("Illegal keycode %d for name <%s>\n", kc, name);
335 ACTION("Must be in the range %d-%d inclusive\n",
336 info->effectiveMin, info->effectiveMax);
339 if (kc < info->computedMin)
340 info->computedMin = kc;
341 if (kc > info->computedMax)
342 info->computedMax = kc;
343 lval = KeyNameToLong(name);
345 if (reportCollisions)
347 reportCollisions = ((warningLevel > 7) ||
349 && (fileID == info->files[kc])));
352 if (info->names[kc] != 0)
356 LongToKeyName(info->names[kc], buf);
358 if (info->names[kc] == lval)
360 if (info->has_alt_forms[kc] || (merge == MergeAltForm))
362 info->has_alt_forms[kc] = True;
364 else if (reportCollisions)
366 WARN("Multiple identical key name definitions\n");
367 ACTION("Later occurences of \"<%s> = %d\" ignored\n",
372 if (merge == MergeAugment)
374 if (reportCollisions)
376 WARN("Multiple names for keycode %d\n", kc);
377 ACTION("Using <%s>, ignoring <%s>\n", buf, name);
383 if (reportCollisions)
385 WARN("Multiple names for keycode %d\n", kc);
386 ACTION("Using <%s>, ignoring <%s>\n", name, buf);
392 old = FindKeyByLong(info, lval);
393 if ((old != 0) && (old != kc))
395 if (merge == MergeOverride)
397 info->names[old] = 0;
398 info->files[old] = 0;
399 info->has_alt_forms[old] = True;
400 if (reportCollisions)
402 WARN("Key name <%s> assigned to multiple keys\n", name);
403 ACTION("Using %d, ignoring %d\n", kc, old);
406 else if (merge != MergeAltForm)
408 if ((reportCollisions) && (warningLevel > 3))
410 WARN("Key name <%s> assigned to multiple keys\n", name);
411 ACTION("Using %d, ignoring %d\n", old, kc);
413 ("Use 'alternate' keyword to assign the same name to multiple keys\n");
419 info->has_alt_forms[old] = True;
422 info->names[kc] = lval;
423 info->files[kc] = fileID;
424 info->has_alt_forms[kc] = (merge == MergeAltForm);
428 /***====================================================================***/
431 MergeIncludedKeycodes(KeyNamesInfo * into, KeyNamesInfo * from,
437 if (from->errorCount > 0)
439 into->errorCount += from->errorCount;
442 if (into->name == NULL)
444 into->name = from->name;
447 for (i = from->computedMin; i <= from->computedMax; i++)
450 if (from->names[i] == 0)
452 LongToKeyName(from->names[i], buf);
454 if (from->has_alt_forms[i])
455 thisMerge = MergeAltForm;
458 if (!AddKeyName(into, i, buf, thisMerge, from->fileID, False))
463 IndicatorNameInfo *led, *next;
464 for (led = from->leds; led != NULL; led = next)
466 if (merge != MergeDefault)
467 led->defs.merge = merge;
468 if (!AddIndicatorName(into, led))
470 next = (IndicatorNameInfo *) led->defs.next;
473 if (!MergeAliases(&into->aliases, &from->aliases, merge))
475 if (from->explicitMin > 0)
477 if ((into->explicitMin < 0)
478 || (into->explicitMin > from->explicitMin))
479 into->effectiveMin = into->explicitMin = from->explicitMin;
481 if (from->explicitMax > 0)
483 if ((into->explicitMax < 0)
484 || (into->explicitMax < from->explicitMax))
485 into->effectiveMax = into->explicitMax = from->explicitMax;
491 * Handle the given include statement (e.g. "include "evdev+aliases(qwerty)").
493 * @param stmt The include statement from the keymap file.
494 * @param xkb Unused for all but the xkb->flags.
495 * @param info Struct to store the key info in.
498 HandleIncludeKeycodes(IncludeStmt * stmt, XkbcDescPtr xkb, KeyNamesInfo * info)
502 KeyNamesInfo included = {NULL};
506 if ((stmt->file == NULL) && (stmt->map == NULL))
510 bzero(info, sizeof(KeyNamesInfo));
512 else if (strcmp(stmt->file, "computed") == 0)
514 xkb->flags |= AutoKeyNames;
515 info->explicitMin = XkbMinLegalKeyCode;
516 info->explicitMax = XkbMaxLegalKeyCode;
517 return (info->errorCount == 0);
518 } /* parse file, store returned info in the xkb struct */
519 else if (ProcessIncludeFile(stmt, XkmKeyNamesIndex, &rtrn, &newMerge))
521 InitKeyNamesInfo(&included);
522 HandleKeycodesFile(rtrn, xkb, MergeOverride, &included);
523 if (stmt->stmt != NULL)
525 if (included.name != NULL)
526 uFree(included.name);
527 included.name = stmt->stmt;
533 info->errorCount += 10; /* XXX: why 10?? */
536 /* Do we have more than one include statement? */
537 if ((stmt->next != NULL) && (included.errorCount < 1))
541 KeyNamesInfo next_incl;
543 for (next = stmt->next; next != NULL; next = next->next)
545 if ((next->file == NULL) && (next->map == NULL))
548 MergeIncludedKeycodes(&included, info, next->merge);
549 ClearKeyNamesInfo(info);
551 else if (ProcessIncludeFile(next, XkmKeyNamesIndex, &rtrn, &op))
553 InitKeyNamesInfo(&next_incl);
554 HandleKeycodesFile(rtrn, xkb, MergeOverride, &next_incl);
555 MergeIncludedKeycodes(&included, &next_incl, op);
556 ClearKeyNamesInfo(&next_incl);
560 info->errorCount += 10; /* XXX: Why 10?? */
569 MergeIncludedKeycodes(info, &included, newMerge);
570 ClearKeyNamesInfo(&included);
572 return (info->errorCount == 0);
576 * Parse the given statement and store the output in the info struct.
580 HandleKeycodeDef(KeycodeDef * stmt, unsigned merge, KeyNamesInfo * info)
585 if (!ExprResolveInteger(stmt->value, &result, NULL, NULL))
587 ACTION("No value keycode assigned to name <%s>\n", stmt->name);
591 if ((code < info->effectiveMin) || (code > info->effectiveMax))
593 ERROR("Illegal keycode %d for name <%s>\n", code, stmt->name);
594 ACTION("Must be in the range %d-%d inclusive\n",
595 info->effectiveMin, info->effectiveMax);
598 if (stmt->merge != MergeDefault)
600 if (stmt->merge == MergeReplace)
601 merge = MergeOverride;
605 return AddKeyName(info, code, stmt->name, merge, info->fileID, True);
608 #define MIN_KEYCODE_DEF 0
609 #define MAX_KEYCODE_DEF 1
612 * Handle the minimum/maximum statement of the xkb file.
613 * Sets explicitMin/Max and effectiveMin/Max of the info struct.
615 * @return 1 on success, 0 otherwise.
618 HandleKeyNameVar(VarDef * stmt, KeyNamesInfo * info)
620 ExprResult tmp, field;
624 if (ExprResolveLhs(stmt->name, &tmp, &field, &arrayNdx) == 0)
625 return 0; /* internal error, already reported */
629 ERROR("Unknown element %s encountered\n", tmp.str);
630 ACTION("Default for field %s ignored\n", field.str);
633 if (uStrCaseCmp(field.str, "minimum") == 0)
634 which = MIN_KEYCODE_DEF;
635 else if (uStrCaseCmp(field.str, "maximum") == 0)
636 which = MAX_KEYCODE_DEF;
639 ERROR("Unknown field encountered\n");
640 ACTION("Assigment to field %s ignored\n", field.str);
643 if (arrayNdx != NULL)
645 ERROR("The %s setting is not an array\n", field.str);
646 ACTION("Illegal array reference ignored\n");
650 if (ExprResolveInteger(stmt->value, &tmp, NULL, NULL) == 0)
652 ACTION("Assignment to field %s ignored\n", field.str);
655 if ((tmp.ival < XkbMinLegalKeyCode) || (tmp.ival > XkbMaxLegalKeyCode))
658 ("Illegal keycode %d (must be in the range %d-%d inclusive)\n",
659 tmp.ival, XkbMinLegalKeyCode, XkbMaxLegalKeyCode);
660 ACTION("Value of \"%s\" not changed\n", field.str);
663 if (which == MIN_KEYCODE_DEF)
665 if ((info->explicitMax > 0) && (info->explicitMax < tmp.ival))
668 ("Minimum key code (%d) must be <= maximum key code (%d)\n",
669 tmp.ival, info->explicitMax);
670 ACTION("Minimum key code value not changed\n");
673 if ((info->computedMax > 0) && (info->computedMin < tmp.ival))
676 ("Minimum key code (%d) must be <= lowest defined key (%d)\n",
677 tmp.ival, info->computedMin);
678 ACTION("Minimum key code value not changed\n");
681 info->explicitMin = tmp.ival;
682 info->effectiveMin = tmp.ival;
684 if (which == MAX_KEYCODE_DEF)
686 if ((info->explicitMin > 0) && (info->explicitMin > tmp.ival))
688 ERROR("Maximum code (%d) must be >= minimum key code (%d)\n",
689 tmp.ival, info->explicitMin);
690 ACTION("Maximum code value not changed\n");
693 if ((info->computedMax > 0) && (info->computedMax > tmp.ival))
696 ("Maximum code (%d) must be >= highest defined key (%d)\n",
697 tmp.ival, info->computedMax);
698 ACTION("Maximum code value not changed\n");
701 info->explicitMax = tmp.ival;
702 info->effectiveMax = tmp.ival;
708 HandleIndicatorNameDef(IndicatorNameDef * def,
709 unsigned merge, KeyNamesInfo * info)
711 IndicatorNameInfo ii;
714 if ((def->ndx < 1) || (def->ndx > XkbNumIndicators))
717 ERROR("Name specified for illegal indicator index %d\n", def->ndx);
721 InitIndicatorNameInfo(&ii, info);
723 if (!ExprResolveString(def->name, &tmp, NULL, NULL))
726 snprintf(buf, sizeof(buf), "%d", def->ndx);
728 return ReportBadType("indicator", "name", buf, "string");
730 ii.name = XkbcInternAtom(tmp.str, False);
731 ii.virtual = def->virtual;
732 if (!AddIndicatorName(info, &ii))
738 * Handle the xkb_keycodes section of a xkb file.
739 * All information about parsed keys is stored in the info struct.
741 * Such a section may have include statements, in which case this function is
742 * semi-recursive (it calls HandleIncludeKeycodes, which may call
743 * HandleKeycodesFile again).
745 * @param file The input file (parsed xkb_keycodes section)
746 * @param xkb Necessary to pass down, may have flags changed.
747 * @param merge Merge strategy (MergeOverride, etc.)
748 * @param info Struct to contain the fully parsed key information.
751 HandleKeycodesFile(XkbFile * file,
752 XkbcDescPtr xkb, unsigned merge, KeyNamesInfo * info)
756 info->name = uStringDup(file->name);
760 switch (stmt->stmtType)
762 case StmtInclude: /* e.g. include "evdev+aliases(qwerty)" */
763 if (!HandleIncludeKeycodes((IncludeStmt *) stmt, xkb, info))
766 case StmtKeycodeDef: /* e.g. <ESC> = 9; */
767 if (!HandleKeycodeDef((KeycodeDef *) stmt, merge, info))
770 case StmtKeyAliasDef: /* e.g. alias <MENU> = <COMP>; */
771 if (!HandleAliasDef((KeyAliasDef *) stmt,
772 merge, info->fileID, &info->aliases))
775 case StmtVarDef: /* e.g. minimum, maximum */
776 if (!HandleKeyNameVar((VarDef *) stmt, info))
779 case StmtIndicatorNameDef: /* e.g. indicator 1 = "Caps Lock"; */
780 if (!HandleIndicatorNameDef((IndicatorNameDef *) stmt,
788 ERROR("Keycode files may define key and indicator names only\n");
789 ACTION("Ignoring definition of %s\n",
791 StmtInterpDef) ? "a symbol interpretation" :
792 "virtual modifiers"));
796 WSGO("Unexpected statement type %d in HandleKeycodesFile\n",
801 if (info->errorCount > 10)
804 ERROR("Too many errors\n");
806 ACTION("Abandoning keycodes file \"%s\"\n", file->topName);
814 * Compile the xkb_keycodes section, parse it's output, return the results.
816 * @param file The parsed XKB file (may have include statements requiring
818 * @param result The effective keycodes, as gathered from the file.
819 * @param merge Merge strategy.
821 * @return True on success, False otherwise.
824 CompileKeycodes(XkbFile *file, XkbcDescPtr xkb, unsigned merge)
826 KeyNamesInfo info; /* contains all the info after parsing */
828 InitKeyNamesInfo(&info);
829 HandleKeycodesFile(file, xkb, merge, &info);
831 /* all the keys are now stored in info */
833 if (info.errorCount == 0)
835 if (info.explicitMin > 0) /* if "minimum" statement was present */
836 xkb->min_key_code = info.effectiveMin;
838 xkb->min_key_code = info.computedMin;
839 if (info.explicitMax > 0) /* if "maximum" statement was present */
840 xkb->max_key_code = info.effectiveMax;
842 xkb->max_key_code = info.computedMax;
843 if (XkbcAllocNames(xkb, XkbKeyNamesMask | XkbIndicatorNamesMask, 0, 0)
847 xkb->names->keycodes = XkbcInternAtom(info.name, False);
848 uDEBUG2(1, "key range: %d..%d\n", xkb->min_key_code,
850 for (i = info.computedMin; i <= info.computedMax; i++)
852 LongToKeyName(info.names[i], xkb->names->keys[i].name);
853 uDEBUG2(2, "key %d = %s\n", i,
854 XkbcKeyNameText(xkb->names->keys[i].name));
859 WSGO("Cannot create XkbNamesRec in CompileKeycodes\n");
864 IndicatorNameInfo *ii;
865 if (XkbcAllocIndicatorMaps(xkb) != Success)
867 WSGO("Couldn't allocate IndicatorRec in CompileKeycodes\n");
868 ACTION("Physical indicators not set\n");
870 for (ii = info.leds; ii != NULL;
871 ii = (IndicatorNameInfo *) ii->defs.next)
873 xkb->names->indicators[ii->ndx - 1] =
874 XkbcInternAtom(XkbcAtomGetString(ii->name), False);
875 if (xkb->indicators != NULL)
877 register unsigned bit;
878 bit = 1 << (ii->ndx - 1);
880 xkb->indicators->phys_indicators &= ~bit;
882 xkb->indicators->phys_indicators |= bit;
887 ApplyAliases(xkb, False, &info.aliases);
890 ClearKeyNamesInfo(&info);