Add flags to keymap compilation entrypoints
[platform/upstream/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-priv.h"
28 #include "indicators.h"
29
30 /**
31  * Compile the given file and store the output in xkb.
32  * @param file A list of XkbFiles, each denoting one type (e.g.
33  * XkmKeyNamesIdx, etc.)
34  */
35 struct xkb_keymap *
36 CompileKeymap(struct xkb_context *context, XkbFile *file)
37 {
38     unsigned have;
39     bool ok;
40     unsigned required, legal;
41     unsigned mainType;
42     const char *mainName;
43     LEDInfo *unbound = NULL, *next;
44     struct xkb_keymap *xkb = XkbcAllocKeyboard(context);
45     struct {
46         XkbFile *keycodes;
47         XkbFile *types;
48         XkbFile *compat;
49         XkbFile *symbols;
50     } sections;
51
52     if (!xkb)
53         return NULL;
54
55     memset(&sections, 0, sizeof(sections));
56     mainType = file->type;
57     mainName = file->name ? file->name : "(unnamed)";
58     switch (mainType)
59     {
60     case XkmSemanticsFile:
61         required = XkmSemanticsRequired;
62         legal = XkmSemanticsLegal;
63         break;
64     case XkmLayoutFile:        /* standard type  if setxkbmap -print */
65         required = XkmLayoutRequired;
66         legal = XkmKeymapLegal;
67         break;
68     case XkmKeymapFile:
69         required = XkmKeyNamesIndex | XkmTypesIndex | XkmSymbolsIndex |
70                    XkmCompatMapIndex;
71         legal = XkmKeymapLegal;
72         break;
73     default:
74         ERROR("Cannot compile %s alone into an XKM file\n",
75                XkbcConfigText(mainType));
76         return false;
77     }
78     have = 0;
79     ok = 1;
80     /* Check for duplicate entries in the input file */
81     for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
82     {
83         if ((have & (1 << file->type)) != 0)
84         {
85             ERROR("More than one %s section in a %s file\n",
86                    XkbcConfigText(file->type), XkbcConfigText(mainType));
87             ACTION("All sections after the first ignored\n");
88             continue;
89         }
90         else if ((1 << file->type) & (~legal))
91         {
92             ERROR("Cannot define %s in a %s file\n",
93                    XkbcConfigText(file->type), XkbcConfigText(mainType));
94             continue;
95         }
96
97         switch (file->type)
98         {
99         case XkmKeyNamesIndex:
100             sections.keycodes = file;
101             break;
102         case XkmTypesIndex:
103             sections.types = file;
104             break;
105         case XkmSymbolsIndex:
106             sections.symbols = file;
107             break;
108         case XkmCompatMapIndex:
109             sections.compat = file;
110             break;
111         case XkmGeometryIndex:
112             continue;
113         default:
114             WSGO("Unknown file type %d\n", file->type);
115             ACTION("Ignored\n");
116             continue;
117         case XkmSemanticsFile:
118         case XkmLayoutFile:
119         case XkmKeymapFile:
120             WSGO("Illegal %s configuration in a %s file\n",
121                   XkbcConfigText(file->type), XkbcConfigText(mainType));
122             ACTION("Ignored\n");
123             continue;
124         }
125
126         if (!file->topName || strcmp(file->topName, mainName) != 0) {
127             free(file->topName);
128             file->topName = strdup(mainName);
129         }
130
131         have |= (1 << file->type);
132     }
133
134     if (required & (~have))
135     {
136         int i, bit;
137         unsigned missing;
138         missing = required & (~have);
139         for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
140         {
141             if (missing & bit)
142             {
143                 ERROR("Required section %s missing from keymap\n", XkbcConfigText(i));
144                 missing &= ~bit;
145             }
146         }
147         goto err;
148     }
149
150     /* compile the sections we have in the file one-by-one, or fail. */
151     if (sections.keycodes == NULL ||
152         !CompileKeycodes(sections.keycodes, xkb, MergeOverride))
153     {
154         ERROR("Failed to compile keycodes\n");
155         goto err;
156     }
157     if (sections.types == NULL ||
158         !CompileKeyTypes(sections.types, xkb, MergeOverride))
159     {
160         ERROR("Failed to compile key types\n");
161         goto err;
162     }
163     if (sections.compat == NULL ||
164         !CompileCompatMap(sections.compat, xkb, MergeOverride, &unbound))
165     {
166         ERROR("Failed to compile compat map\n");
167         goto err;
168     }
169     if (sections.symbols == NULL ||
170         !CompileSymbols(sections.symbols, xkb, MergeOverride))
171     {
172         ERROR("Failed to compile symbols\n");
173         goto err;
174     }
175
176     ok = BindIndicators(xkb, true, unbound, NULL);
177     if (!ok)
178         goto err;
179
180     ok = UpdateModifiersFromCompat(xkb);
181     if (!ok)
182         goto err;
183
184     return xkb;
185
186 err:
187     ACTION("Failed to compile keymap\n");
188     xkb_map_unref(xkb);
189     while (unbound) {
190         next = (LEDInfo *) unbound->defs.next;
191         free(unbound);
192         unbound = next;
193     }
194     return NULL;
195 }