Ensure we always have a complete keymap
[profile/ivi/libxkbcommon.git] / src / xkbcomp / keymap.c
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
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.
15
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.
24
25  ********************************************************/
26
27 #include "xkbcomp.h"
28 #include "xkbmisc.h"
29 #include "expr.h"
30 #include "vmod.h"
31 #include "action.h"
32 #include "misc.h"
33 #include "indicators.h"
34
35 /**
36  * Compile the given file and store the output in xkb.
37  * @param file A list of XkbFiles, each denoting one type (e.g.
38  * XkmKeyNamesIdx, etc.)
39  */
40 Bool
41 CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
42 {
43     unsigned have;
44     Bool ok;
45     unsigned required, legal;
46     unsigned mainType;
47     char *mainName;
48     LEDInfo *unbound = NULL;
49     struct {
50         XkbFile *keycodes;
51         XkbFile *types;
52         XkbFile *compat;
53         XkbFile *symbols;
54     } sections;
55
56     memset(&sections, 0, sizeof(sections));
57     mainType = file->type;
58     mainName = file->name;
59     switch (mainType)
60     {
61     case XkmSemanticsFile:
62         required = XkmSemanticsRequired;
63         legal = XkmSemanticsLegal;
64         break;
65     case XkmLayoutFile:        /* standard type  if setxkbmap -print */
66         required = XkmLayoutRequired;
67         legal = XkmKeymapLegal;
68         break;
69     case XkmKeymapFile:
70         required = XkmKeyNamesIndex | XkmTypesIndex | XkmSymbolsIndex | \
71                    XkmCompatMapIndex | XkmVirtualModsIndex;
72         legal = XkmKeymapLegal;
73         break;
74     default:
75         ERROR("Cannot compile %s alone into an XKM file\n",
76                XkbcConfigText(mainType));
77         return False;
78     }
79     have = 0;
80     ok = 1;
81     /* Check for duplicate entries in the input file */
82     for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
83     {
84         if (file->topName != mainName) {
85             free(file->topName);
86             file->topName = strdup(mainName);
87         }
88
89         if ((have & (1 << file->type)) != 0)
90         {
91             ERROR("More than one %s section in a %s file\n",
92                    XkbcConfigText(file->type), XkbcConfigText(mainType));
93             ACTION("All sections after the first ignored\n");
94             return False;
95         }
96         else if ((1 << file->type) & (~legal))
97         {
98             ERROR("Cannot define %s in a %s file\n",
99                    XkbcConfigText(file->type), XkbcConfigText(mainType));
100             return False;
101         }
102
103         switch (file->type)
104         {
105         case XkmKeyNamesIndex:
106             sections.keycodes = file;
107             break;
108         case XkmTypesIndex:
109             sections.types = file;
110             break;
111         case XkmSymbolsIndex:
112             sections.symbols = file;
113             break;
114         case XkmCompatMapIndex:
115             sections.compat = file;
116             break;
117         case XkmGeometryIndex:
118             continue;
119         case XkmVirtualModsIndex:
120         case XkmIndicatorsIndex:
121             WSGO("Found an isolated %s section\n", XkbcConfigText(file->type));
122             ACTION("Ignored\n");
123             continue;
124         default:
125             WSGO("Unknown file type %d\n", file->type);
126             ACTION("Ignored\n");
127             continue;
128         case XkmSemanticsFile:
129         case XkmLayoutFile:
130         case XkmKeymapFile:
131             WSGO("Illegal %s configuration in a %s file\n",
132                   XkbcConfigText(file->type), XkbcConfigText(mainType));
133             ACTION("Ignored\n");
134             continue;
135         }
136
137         have |= (1 << file->type);
138     }
139
140     if (required & (~have))
141     {
142         int i, bit;
143         unsigned missing;
144         missing = required & (~have);
145         for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
146         {
147             if (missing & bit)
148             {
149                 ERROR("Missing %s section in a %s file\n",
150                        XkbcConfigText(i), XkbcConfigText(mainType));
151                 missing &= ~bit;
152             }
153         }
154         ACTION("Description of %s not compiled\n",
155                 XkbcConfigText(mainType));
156         return False;
157     }
158
159     /* compile the sections we have in the file one-by-one, or fail. */
160     if (sections.keycodes != NULL &&
161         !CompileKeycodes(sections.keycodes, xkb, MergeOverride))
162         return False;
163     if (sections.types != NULL &&
164         !CompileKeyTypes(sections.types, xkb, MergeOverride))
165         return False;
166     if (sections.compat != NULL &&
167         !CompileCompatMap(sections.compat, xkb, MergeOverride, &unbound))
168         return False;
169     if (sections.symbols != NULL &&
170         !CompileSymbols(sections.symbols, xkb, MergeOverride))
171         return False;
172
173     xkb->defined = have;
174
175     ok = BindIndicators(xkb, True, unbound, NULL);
176     return ok;
177 }