ac686d03cedf6d51db20608151e291b59f668a1d
[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 "tokens.h"
29 #include "expr.h"
30 #include "vmod.h"
31 #include "action.h"
32 #include "misc.h"
33 #include "indicators.h"
34
35 #define KEYCODES        0
36 #define GEOMETRY        1
37 #define TYPES           2
38 #define COMPAT          3
39 #define SYMBOLS         4
40 #define MAX_SECTIONS    5
41
42 static XkbFile *sections[MAX_SECTIONS];
43
44 /**
45  * Compile the given file and store the output in xkb.
46  * @param file A list of XkbFiles, each denoting one type (e.g.
47  * XkmKeyNamesIdx, etc.)
48  */
49 Bool
50 CompileKeymap(XkbFile *file, XkbcDescPtr xkb, unsigned merge)
51 {
52     unsigned have;
53     Bool ok;
54     unsigned required, legal;
55     unsigned mainType;
56     char *mainName;
57     LEDInfo *unbound = NULL;
58
59     bzero(sections, MAX_SECTIONS * sizeof(XkbFile *));
60     mainType = file->type;
61     mainName = file->name;
62     switch (mainType)
63     {
64     case XkmSemanticsFile:
65         required = XkmSemanticsRequired;
66         legal = XkmSemanticsLegal;
67         break;
68     case XkmLayoutFile:        /* standard type  if setxkbmap -print */
69         required = XkmLayoutRequired;
70         legal = XkmKeymapLegal;
71         break;
72     case XkmKeymapFile:
73         required = XkmKeymapRequired;
74         legal = XkmKeymapLegal;
75         break;
76     default:
77         ERROR("Cannot compile %s alone into an XKM file\n",
78                XkbcConfigText(mainType));
79         return False;
80     }
81     have = 0;
82     ok = 1;
83     file = (XkbFile *) file->defs;
84     /* Check for duplicate entries in the input file */
85     while ((file) && (ok))
86     {
87         file->topName = mainName;
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             case XkmGeometryFile:
126                 sections[GEOMETRY] = file;
127                 break;
128             case XkmVirtualModsIndex:
129             case XkmIndicatorsIndex:
130                 WSGO("Found an isolated %s section\n",
131                       XkbcConfigText(file->type));
132                 break;
133             default:
134                 WSGO("Unknown file type %d\n", file->type);
135                 break;
136             }
137         if (ok)
138             have |= (1 << file->type);
139         file = (XkbFile *) file->common.next;
140     }
141     /* compile the sections we have in the file one-by-one, or fail. */
142     if (ok)
143     {
144         if (ok && (sections[KEYCODES] != NULL))
145             ok = CompileKeycodes(sections[KEYCODES], xkb, MergeOverride);
146         if (ok && (sections[GEOMETRY] != NULL))
147             ok = CompileGeometry(sections[GEOMETRY], xkb, MergeOverride);
148         if (ok && (sections[TYPES] != NULL))
149             ok = CompileKeyTypes(sections[TYPES], xkb, MergeOverride);
150         if (ok && (sections[COMPAT] != NULL))
151             ok = CompileCompatMap(sections[COMPAT], xkb, MergeOverride,
152                                   &unbound);
153         if (ok && (sections[SYMBOLS] != NULL))
154             ok = CompileSymbols(sections[SYMBOLS], xkb, MergeOverride);
155     }
156     if (!ok)
157         return False;
158     xkb->defined = have;
159     if (required & (~have))
160     {
161         register int i, bit;
162         unsigned missing;
163         missing = required & (~have);
164         for (i = 0, bit = 1; missing != 0; i++, bit <<= 1)
165         {
166             if (missing & bit)
167             {
168                 ERROR("Missing %s section in a %s file\n",
169                        XkbcConfigText(i), XkbcConfigText(mainType));
170                 missing &= ~bit;
171             }
172         }
173         ACTION("Description of %s not compiled\n",
174                 XkbcConfigText(mainType));
175         ok = False;
176     }
177     ok = BindIndicators(xkb, True, unbound, NULL);
178     return ok;
179 }