Remove unused stuff from XKBcommonint.h
[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.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 #include "xkballoc.h"
35
36 /**
37  * Compile the given file and store the output in xkb.
38  * @param file A list of XkbFiles, each denoting one type (e.g.
39  * XkmKeyNamesIdx, etc.)
40  */
41 struct xkb_keymap *
42 CompileKeymap(struct xkb_context *context, XkbFile *file)
43 {
44     unsigned have;
45     bool ok;
46     unsigned required, legal;
47     unsigned mainType;
48     const char *mainName;
49     LEDInfo *unbound = NULL, *next;
50     struct xkb_keymap *xkb = XkbcAllocKeyboard(context);
51     struct {
52         XkbFile *keycodes;
53         XkbFile *types;
54         XkbFile *compat;
55         XkbFile *symbols;
56     } sections;
57
58     if (!xkb)
59         return NULL;
60
61     memset(&sections, 0, sizeof(sections));
62     mainType = file->type;
63     mainName = file->name ? file->name : "(unnamed)";
64     switch (mainType)
65     {
66     case XkmSemanticsFile:
67         required = XkmSemanticsRequired;
68         legal = XkmSemanticsLegal;
69         break;
70     case XkmLayoutFile:        /* standard type  if setxkbmap -print */
71         required = XkmLayoutRequired;
72         legal = XkmKeymapLegal;
73         break;
74     case XkmKeymapFile:
75         required = XkmKeyNamesIndex | XkmTypesIndex | XkmSymbolsIndex |
76                    XkmCompatMapIndex;
77         legal = XkmKeymapLegal;
78         break;
79     default:
80         ERROR("Cannot compile %s alone into an XKM file\n",
81                XkbcConfigText(mainType));
82         return false;
83     }
84     have = 0;
85     ok = 1;
86     /* Check for duplicate entries in the input file */
87     for (file = (XkbFile *) file->defs; file; file = (XkbFile *) file->common.next)
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             continue;
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             continue;
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         default:
120             WSGO("Unknown file type %d\n", file->type);
121             ACTION("Ignored\n");
122             continue;
123         case XkmSemanticsFile:
124         case XkmLayoutFile:
125         case XkmKeymapFile:
126             WSGO("Illegal %s configuration in a %s file\n",
127                   XkbcConfigText(file->type), XkbcConfigText(mainType));
128             ACTION("Ignored\n");
129             continue;
130         }
131
132         if (!file->topName || strcmp(file->topName, mainName) != 0) {
133             free(file->topName);
134             file->topName = strdup(mainName);
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("Required section %s missing from keymap\n", XkbcConfigText(i));
150                 missing &= ~bit;
151             }
152         }
153         goto err;
154     }
155
156     /* compile the sections we have in the file one-by-one, or fail. */
157     if (sections.keycodes == NULL ||
158         !CompileKeycodes(sections.keycodes, xkb, MergeOverride))
159     {
160         ERROR("Failed to compile keycodes\n");
161         goto err;
162     }
163     if (sections.types == NULL ||
164         !CompileKeyTypes(sections.types, xkb, MergeOverride))
165     {
166         ERROR("Failed to compile key types\n");
167         goto err;
168     }
169     if (sections.compat == NULL ||
170         !CompileCompatMap(sections.compat, xkb, MergeOverride, &unbound))
171     {
172         ERROR("Failed to compile compat map\n");
173         goto err;
174     }
175     if (sections.symbols == NULL ||
176         !CompileSymbols(sections.symbols, xkb, MergeOverride))
177     {
178         ERROR("Failed to compile symbols\n");
179         goto err;
180     }
181
182     ok = BindIndicators(xkb, true, unbound, NULL);
183     if (!ok)
184         goto err;
185
186     ok = UpdateModifiersFromCompat(xkb);
187     if (!ok)
188         goto err;
189
190     return xkb;
191
192 err:
193     ACTION("Failed to compile keymap\n");
194     xkb_map_unref(xkb);
195     while (unbound) {
196         next = (LEDInfo *) unbound->defs.next;
197         free(unbound);
198         unbound = next;
199     }
200     return NULL;
201 }