keymap: simplify legal/required logic a bit
[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 *ctx, XkbFile *file)
37 {
38     unsigned have = 0;
39     bool ok;
40     unsigned mainType;
41     const char *mainName;
42     LEDInfo *unbound = NULL, *next;
43     struct xkb_keymap *keymap = XkbcAllocKeyboard(ctx);
44     struct {
45         XkbFile *keycodes;
46         XkbFile *types;
47         XkbFile *compat;
48         XkbFile *symbols;
49     } sections = { NULL };
50
51     if (!keymap)
52         return NULL;
53
54     mainType = file->type;
55     mainName = file->name ? file->name : "(unnamed)";
56
57     /*
58      * Other aggregate file types are converted to XkmKeymapFile
59      * in the parser.
60      */
61     if (mainType != XkmKeymapFile) {
62         ERROR("Cannot compile a %s file alone into a keymap\n",
63                XkbcConfigText(mainType));
64         return false;
65     }
66
67     /* Check for duplicate entries in the input file */
68     for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
69     {
70         if ((have & (1 << file->type)) != 0)
71         {
72             ERROR("More than one %s section in a %s file\n",
73                    XkbcConfigText(file->type), XkbcConfigText(mainType));
74             ACTION("All sections after the first ignored\n");
75             continue;
76         }
77         else if ((1 << file->type) & (~XkmKeymapLegal))
78         {
79             ERROR("Cannot define %s in a %s file\n",
80                    XkbcConfigText(file->type), XkbcConfigText(mainType));
81             continue;
82         }
83
84         switch (file->type)
85         {
86         case XkmKeyNamesIndex:
87             sections.keycodes = file;
88             break;
89         case XkmTypesIndex:
90             sections.types = file;
91             break;
92         case XkmSymbolsIndex:
93             sections.symbols = file;
94             break;
95         case XkmCompatMapIndex:
96             sections.compat = file;
97             break;
98         case XkmGeometryIndex:
99             continue;
100         default:
101             WSGO("Unknown file type %d\n", file->type);
102             ACTION("Ignored\n");
103             continue;
104         case XkmKeymapFile:
105             WSGO("Illegal %s configuration in a %s file\n",
106                   XkbcConfigText(file->type), XkbcConfigText(mainType));
107             ACTION("Ignored\n");
108             continue;
109         }
110
111         if (!file->topName || strcmp(file->topName, mainName) != 0) {
112             free(file->topName);
113             file->topName = strdup(mainName);
114         }
115
116         have |= (1 << file->type);
117     }
118
119     if (XkmKeymapRequired & (~have))
120     {
121         int i, bit;
122         unsigned missing;
123         missing = XkmKeymapRequired & (~have);
124         for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
125         {
126             if (missing & bit)
127             {
128                 ERROR("Required section %s missing from keymap\n", XkbcConfigText(i));
129                 missing &= ~bit;
130             }
131         }
132         goto err;
133     }
134
135     /* compile the sections we have in the file one-by-one, or fail. */
136     if (sections.keycodes == NULL ||
137         !CompileKeycodes(sections.keycodes, keymap, MergeOverride))
138     {
139         ERROR("Failed to compile keycodes\n");
140         goto err;
141     }
142     if (sections.types == NULL ||
143         !CompileKeyTypes(sections.types, keymap, MergeOverride))
144     {
145         ERROR("Failed to compile key types\n");
146         goto err;
147     }
148     if (sections.compat == NULL ||
149         !CompileCompatMap(sections.compat, keymap, MergeOverride, &unbound))
150     {
151         ERROR("Failed to compile compat map\n");
152         goto err;
153     }
154     if (sections.symbols == NULL ||
155         !CompileSymbols(sections.symbols, keymap, MergeOverride))
156     {
157         ERROR("Failed to compile symbols\n");
158         goto err;
159     }
160
161     ok = BindIndicators(keymap, true, unbound, NULL);
162     if (!ok)
163         goto err;
164
165     ok = UpdateModifiersFromCompat(keymap);
166     if (!ok)
167         goto err;
168
169     return keymap;
170
171 err:
172     ACTION("Failed to compile keymap\n");
173     xkb_map_unref(keymap);
174     while (unbound) {
175         next = (LEDInfo *) unbound->defs.next;
176         free(unbound);
177         unbound = next;
178     }
179     return NULL;
180 }