xkbcomp: Turn an array into an anonymous struct
[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 = XkmKeymapRequired;
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     file = (XkbFile *) file->defs;
81     /* Check for duplicate entries in the input file */
82     while ((file) && (ok))
83     {
84         if (file->topName != mainName) {
85             free(file->topName);
86             file->topName = strdup(mainName);
87         }
88         if ((have & (1 << file->type)) != 0)
89         {
90             ERROR("More than one %s section in a %s file\n",
91                    XkbcConfigText(file->type), XkbcConfigText(mainType));
92             ACTION("All sections after the first ignored\n");
93             ok = False;
94         }
95         else if ((1 << file->type) & (~legal))
96         {
97             ERROR("Cannot define %s in a %s file\n",
98                    XkbcConfigText(file->type), XkbcConfigText(mainType));
99             ok = False;
100         }
101         else
102             switch (file->type)
103             {
104             case XkmSemanticsFile:
105             case XkmLayoutFile:
106             case XkmKeymapFile:
107                 WSGO("Illegal %s configuration in a %s file\n",
108                       XkbcConfigText(file->type), XkbcConfigText(mainType));
109                 ACTION("Ignored\n");
110                 ok = False;
111                 break;
112             case XkmKeyNamesIndex:
113                 sections.keycodes = file;
114                 break;
115             case XkmTypesIndex:
116                 sections.types = file;
117                 break;
118             case XkmSymbolsIndex:
119                 sections.symbols = file;
120                 break;
121             case XkmCompatMapIndex:
122                 sections.compat = file;
123                 break;
124             case XkmGeometryIndex:
125                 /* XXX free me! */
126                 break;
127             case XkmVirtualModsIndex:
128             case XkmIndicatorsIndex:
129                 WSGO("Found an isolated %s section\n",
130                       XkbcConfigText(file->type));
131                 break;
132             default:
133                 WSGO("Unknown file type %d\n", file->type);
134                 break;
135             }
136         if (ok)
137             have |= (1 << file->type);
138         file = (XkbFile *) file->common.next;
139     }
140     /* compile the sections we have in the file one-by-one, or fail. */
141     if (ok)
142     {
143         if (ok && (sections.keycodes != NULL))
144             ok = CompileKeycodes(sections.keycodes, xkb, MergeOverride);
145         if (ok && (sections.types != NULL))
146             ok = CompileKeyTypes(sections.types, xkb, MergeOverride);
147         if (ok && (sections.compat != NULL))
148             ok = CompileCompatMap(sections.compat, xkb, MergeOverride,
149                                   &unbound);
150         if (ok && (sections.symbols != NULL))
151             ok = CompileSymbols(sections.symbols, xkb, MergeOverride);
152     }
153     if (!ok)
154         return False;
155     xkb->defined = have;
156     if (required & (~have))
157     {
158         int i, bit;
159         unsigned missing;
160         missing = required & (~have);
161         for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
162         {
163             if (missing & bit)
164             {
165                 ERROR("Missing %s section in a %s file\n",
166                        XkbcConfigText(i), XkbcConfigText(mainType));
167                 missing &= ~bit;
168             }
169         }
170         ACTION("Description of %s not compiled\n",
171                 XkbcConfigText(mainType));
172         return False;
173     }
174     ok = BindIndicators(xkb, True, unbound, NULL);
175     return ok;
176 }