Add interface to compile keyboard description from keymap files
[profile/ivi/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
1 /*
2 Copyright 2009  Dan Nicholson
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the names of the authors or their
22 institutions shall not be used in advertising or otherwise to promote the
23 sale, use or other dealings in this Software without prior written
24 authorization from the authors.
25 */
26
27 #include <limits.h>
28 #include "xkbcomp.h"
29 #include "xkballoc.h"
30 #include "xkbrules.h"
31 #include <X11/extensions/XKM.h>
32 #include "parseutils.h"
33 #include "utils.h"
34
35 #ifndef DFLT_XKB_CONFIG_ROOT
36 #define DFLT_XKB_CONFIG_ROOT "/usr/share/X11/xkb"
37 #endif
38
39 /* Global debugging flags */
40 unsigned int debugFlags = 0;
41 unsigned int warningLevel = 0;
42
43 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
44
45 static XkbFile *
46 XkbKeymapFileFromComponents(const XkbComponentNamesPtr ktcsg)
47 {
48     XkbFile *keycodes, *types, *compat, *symbols, *geometry;
49     IncludeStmt *inc;
50
51     if (!ktcsg) {
52         ERROR("no components to generate keymap file from\n");
53         return NULL;
54     }
55
56     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
57     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
58
59     inc = IncludeCreate(ktcsg->types, MergeDefault);
60     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
61     AppendStmt(&keycodes->common, &types->common);
62
63     inc = IncludeCreate(ktcsg->compat, MergeDefault);
64     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
65     AppendStmt(&keycodes->common, &compat->common);
66
67     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
68     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
69     AppendStmt(&keycodes->common, &symbols->common);
70
71     inc = IncludeCreate(ktcsg->geometry, MergeDefault);
72     geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
73     AppendStmt(&keycodes->common, &geometry->common);
74
75     return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : "",
76                          &keycodes->common, 0);
77 }
78
79 static XkbComponentNamesPtr
80 XkbComponentsFromRules(const char *rulesPath, const XkbRF_VarDefsPtr defs)
81 {
82     XkbRF_RulesPtr rules;
83     XkbComponentNamesPtr names = NULL;
84
85     if (!(rules = XkbcRF_Load((char *)rulesPath, NULL, False, True))) {
86         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
87         goto fail;
88     }
89
90     if (!(names = _XkbTypedCalloc(1, XkbComponentNamesRec))) {
91         ERROR("failed to allocate XKB components\n");
92         goto unwind_rules;
93     }
94
95     if (!XkbcRF_GetComponents(rules, defs, names)) {
96         _XkbFree(names->keymap);
97         _XkbFree(names->keycodes);
98         _XkbFree(names->types);
99         _XkbFree(names->compat);
100         _XkbFree(names->symbols);
101         _XkbFree(names->geometry);
102         _XkbFree(names);
103         names = NULL;
104         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
105     }
106
107 unwind_rules:
108     XkbcRF_Free(rules, True);
109 fail:
110     return names;
111 }
112
113 XkbcDescPtr
114 XkbcCompileKeymapFromRules(const XkbRMLVOSet *rmlvo)
115 {
116     char rulesPath[PATH_MAX];
117     int pathlen;
118     XkbRF_VarDefsRec defs;
119     XkbComponentNamesPtr names;
120     XkbcDescPtr xkb;
121
122     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
123         ERROR("rules and layout required to generate XKB keymap\n");
124         return NULL;
125     }
126
127     pathlen = snprintf(rulesPath, sizeof(rulesPath),
128                        DFLT_XKB_CONFIG_ROOT "/rules/%s", rmlvo->rules);
129     if (pathlen >= sizeof(rulesPath)) {
130         ERROR("XKB rules path truncated\n");
131         return NULL;
132     }
133
134     defs.model = rmlvo->model;
135     defs.layout = rmlvo->layout;
136     defs.variant = rmlvo->variant;
137     defs.options = rmlvo->options;
138
139     names = XkbComponentsFromRules(rulesPath, &defs);
140     if (!names) {
141         ERROR("failed to generate XKB components from rules \"%s\"\n",
142               rmlvo->rules);
143         return NULL;
144     }
145
146     xkb = XkbcCompileKeymapFromComponents(names);
147
148     _XkbFree(names->keymap);
149     _XkbFree(names->keycodes);
150     _XkbFree(names->types);
151     _XkbFree(names->compat);
152     _XkbFree(names->symbols);
153     _XkbFree(names->geometry);
154     _XkbFree(names);
155
156     return xkb;
157 }
158
159 static XkbFile *
160 XkbChooseMap(XkbFile *file, const char *name)
161 {
162     XkbFile *map = file;
163
164     /* map specified? */
165     if (name) {
166         while (map) {
167             if (map->name && strcmp(map->name, name) == 0)
168                 break;
169             map = (XkbFile *) map->common.next;
170         }
171
172         if (!map)
173             ERROR("no map named \"%s\" in input file\n", name);
174     }
175     else if (file->common.next) {
176         /* look for map with XkbLC_Default flag. */
177         for (; map; map = (XkbFile *) map->common.next) {
178             if (map->flags & XkbLC_Default)
179                 break;
180         }
181
182         if (!map) {
183             map = file;
184             WARN("no map specified, but components have several\n");
185             WARN("using the first defined map, \"%s\"\n",
186                  map->name ? map->name : "");
187         }
188     }
189
190     return map;
191 }
192
193 XkbcDescPtr
194 XkbcCompileKeymapFromComponents(const XkbComponentNamesPtr ktcsg)
195 {
196     XkbFile *file, *mapToUse;
197     XkbcDescPtr xkb;
198
199     if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
200         ERROR("keycodes required to generate XKB keymap\n");
201         goto fail;
202     }
203
204     if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
205         ERROR("failed to generate parsed XKB file from components\n");
206         goto fail;
207     }
208
209     /* Find map to use */
210     if (!(mapToUse = XkbChooseMap(file, NULL)))
211         goto unwind_file;
212
213     /* Compile the keyboard */
214     if (!(xkb = XkbcAllocKeyboard())) {
215         ERROR("could not allocate keyboard description\n");
216         goto unwind_file;
217     }
218
219     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
220         ERROR("failed to compile keymap\n");
221         goto unwind_xkb;
222     }
223
224     return xkb;
225 unwind_xkb:
226     XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
227 unwind_file:
228     /* XXX: here's where we would free the XkbFile */
229 fail:
230     return NULL;
231 }
232
233 XkbcDescPtr
234 XkbcCompileKeymapFromFile(FILE *inputFile, const char *mapName)
235 {
236     XkbFile *file, *mapToUse;
237     XkbcDescPtr xkb;
238
239     if (!inputFile) {
240         ERROR("no file specified to generate XKB keymap\n");
241         goto fail;
242     }
243
244     setScanState("input", 1);
245     if (!XKBParseFile(inputFile, &file) || !file) {
246         ERROR("failed to parse input xkb file\n");
247         goto fail;
248     }
249
250     /* Find map to use */
251     if (!(mapToUse = XkbChooseMap(file, mapName)))
252         goto unwind_file;
253
254     switch (mapToUse->type) {
255     case XkmSemanticsFile:
256     case XkmLayoutFile:
257     case XkmKeymapFile:
258         break;
259     default:
260         ERROR("file type %d not handled\n", mapToUse->type);
261         goto unwind_file;
262     }
263
264     /* Compile the keyboard */
265     if (!(xkb = XkbcAllocKeyboard())) {
266         ERROR("could not allocate keyboard description\n");
267         goto unwind_file;
268     }
269
270     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
271         ERROR("failed to compile keymap\n");
272         goto unwind_xkb;
273     }
274
275     return xkb;
276 unwind_xkb:
277     XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
278 unwind_file:
279     /* XXX: here's where we would free the XkbFile */
280 fail:
281     return NULL;
282 }