xkbcomp: use new log functions
[platform/upstream/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 "xkbcomp-priv.h"
28 #include "rules.h"
29 #include "parseutils.h"
30
31 /* Global warning level */
32 unsigned int warningLevel = 0;
33
34 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
35
36 static XkbFile *
37 keymap_file_from_components(struct xkb_context *ctx,
38                             const struct xkb_component_names *ktcsg)
39 {
40     XkbFile *keycodes, *types, *compat, *symbols;
41     IncludeStmt *inc;
42
43     inc = IncludeCreate(ctx, ktcsg->keycodes, MERGE_DEFAULT);
44     keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
45                              (ParseCommon *) inc, 0);
46
47     inc = IncludeCreate(ctx, ktcsg->types, MERGE_DEFAULT);
48     types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
49                           (ParseCommon *) inc, 0);
50     AppendStmt(&keycodes->common, &types->common);
51
52     inc = IncludeCreate(ctx, ktcsg->compat, MERGE_DEFAULT);
53     compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
54                            (ParseCommon *) inc, 0);
55     AppendStmt(&keycodes->common, &compat->common);
56
57     inc = IncludeCreate(ctx, ktcsg->symbols, MERGE_DEFAULT);
58     symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
59                             (ParseCommon *) inc, 0);
60     AppendStmt(&keycodes->common, &symbols->common);
61
62     return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""),
63                          &keycodes->common, 0);
64 }
65
66 /**
67  * Compile the given file and store the output in keymap.
68  * @param file A list of XkbFiles, each denoting one type (e.g.
69  * FILE_TYPE_KEYCODES, etc.)
70  */
71 static struct xkb_keymap *
72 compile_keymap(struct xkb_context *ctx, XkbFile *file)
73 {
74     bool ok;
75     unsigned have = 0;
76     const char *main_name;
77     struct xkb_keymap *keymap;
78     XkbFile *keycodes = NULL;
79     XkbFile *types = NULL;
80     XkbFile *compat = NULL;
81     XkbFile *symbols = NULL;
82
83     keymap = XkbcAllocKeyboard(ctx);
84     if (!keymap)
85         goto err;
86
87     main_name = file->name ? file->name : "(unnamed)";
88
89     /*
90      * Other aggregate file types are converted to FILE_TYPE_KEYMAP
91      * in the parser.
92      */
93     if (file->type != FILE_TYPE_KEYMAP) {
94         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
95                 XkbcFileTypeText(file->type));
96         goto err;
97     }
98
99     /* Check for duplicate entries in the input file */
100     for (file = (XkbFile *) file->defs; file;
101          file = (XkbFile *) file->common.next) {
102         if (have & file->type) {
103             log_err(ctx,
104                     "More than one %s section in a keymap file; "
105                     "All sections after the first ignored\n",
106                     XkbcFileTypeText(file->type));
107             continue;
108         }
109
110         switch (file->type) {
111         case FILE_TYPE_KEYCODES:
112             keycodes = file;
113             break;
114
115         case FILE_TYPE_TYPES:
116             types = file;
117             break;
118
119         case FILE_TYPE_SYMBOLS:
120             symbols = file;
121             break;
122
123         case FILE_TYPE_COMPAT:
124             compat = file;
125             break;
126
127         default:
128             log_err(ctx, "Cannot define %s in a keymap file\n",
129                     XkbcFileTypeText(file->type));
130             continue;
131         }
132
133         if (!file->topName) {
134             free(file->topName);
135             file->topName = strdup(main_name);
136         }
137
138         have |= file->type;
139     }
140
141     if (REQUIRED_FILE_TYPES & (~have)) {
142         enum xkb_file_type bit;
143         enum xkb_file_type missing;
144
145         missing = REQUIRED_FILE_TYPES & (~have);
146
147         for (bit = 1; missing != 0; bit <<= 1) {
148             if (missing & bit) {
149                 log_err(ctx, "Required section %s missing from keymap\n",
150                         XkbcFileTypeText(bit));
151                 missing &= ~bit;
152             }
153         }
154
155         goto err;
156     }
157
158     ok = CompileKeycodes(keycodes, keymap, MERGE_OVERRIDE);
159     if (!ok) {
160         log_err(ctx, "Failed to compile keycodes\n");
161         goto err;
162     }
163     ok = CompileKeyTypes(types, keymap, MERGE_OVERRIDE);
164     if (!ok) {
165         log_err(ctx, "Failed to compile key types\n");
166         goto err;
167     }
168     ok = CompileCompatMap(compat, keymap, MERGE_OVERRIDE);
169     if (!ok) {
170         log_err(ctx, "Failed to compile compat map\n");
171         goto err;
172     }
173     ok = CompileSymbols(symbols, keymap, MERGE_OVERRIDE);
174     if (!ok) {
175         log_err(ctx, "Failed to compile symbols\n");
176         goto err;
177     }
178
179     ok = UpdateModifiersFromCompat(keymap);
180     if (!ok)
181         goto err;
182
183     return keymap;
184
185 err:
186     log_err(ctx, "Failed to compile keymap\n");
187     xkb_map_unref(keymap);
188     return NULL;
189 }
190
191 struct xkb_keymap *
192 xkb_map_new_from_kccgst(struct xkb_context *ctx,
193                         const struct xkb_component_names *kccgst,
194                         enum xkb_map_compile_flags flags)
195 {
196     XkbFile *file;
197     struct xkb_keymap *keymap;
198
199     if (!kccgst) {
200         log_err(ctx, "No components specified\n");
201         return NULL;
202     }
203
204     if (ISEMPTY(kccgst->keycodes)) {
205         log_err(ctx, "Keycodes required to generate XKB keymap\n");
206         return NULL;
207     }
208
209     if (ISEMPTY(kccgst->compat)) {
210         log_err(ctx, "Compat map required to generate XKB keymap\n");
211         return NULL;
212     }
213
214     if (ISEMPTY(kccgst->types)) {
215         log_err(ctx, "Types required to generate XKB keymap\n");
216         return NULL;
217     }
218
219     if (ISEMPTY(kccgst->symbols)) {
220         log_err(ctx, "Symbols required to generate XKB keymap\n");
221         return NULL;
222     }
223
224     file = keymap_file_from_components(ctx, kccgst);
225     if (!file) {
226         log_err(ctx, "Failed to generate parsed XKB file from components\n");
227         return NULL;
228     }
229
230     keymap = compile_keymap(ctx, file);
231     FreeXKBFile(file);
232     return keymap;
233 }
234
235 XKB_EXPORT struct xkb_keymap *
236 xkb_map_new_from_names(struct xkb_context *ctx,
237                        const struct xkb_rule_names *rmlvo,
238                        enum xkb_map_compile_flags flags)
239 {
240     struct xkb_component_names *kkctgs;
241     struct xkb_keymap *keymap;
242
243     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
244         log_err(ctx, "rules and layout required to generate XKB keymap\n");
245         return NULL;
246     }
247
248     kkctgs = xkb_components_from_rules(ctx, rmlvo);
249     if (!kkctgs) {
250         log_err(ctx, "failed to generate XKB components from rules \"%s\"\n",
251                 rmlvo->rules);
252         return NULL;
253     }
254
255     keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);
256
257     free(kkctgs->keycodes);
258     free(kkctgs->types);
259     free(kkctgs->compat);
260     free(kkctgs->symbols);
261     free(kkctgs);
262
263     return keymap;
264 }
265
266 XKB_EXPORT struct xkb_keymap *
267 xkb_map_new_from_string(struct xkb_context *ctx,
268                         const char *string,
269                         enum xkb_keymap_format format,
270                         enum xkb_map_compile_flags flags)
271 {
272     bool ok;
273     XkbFile *file;
274     struct xkb_keymap *keymap;
275
276     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
277         log_err(ctx, "unsupported keymap format %d\n", format);
278         return NULL;
279     }
280
281     if (!string) {
282         log_err(ctx, "No string specified to generate XKB keymap\n");
283         return NULL;
284     }
285
286     ok = XKBParseString(ctx, string, "input", &file);
287     if (!ok) {
288         log_err(ctx, "Failed to parse input xkb file\n");
289         return NULL;
290     }
291
292     keymap = compile_keymap(ctx, file);
293     FreeXKBFile(file);
294     return keymap;
295 }
296
297 XKB_EXPORT struct xkb_keymap *
298 xkb_map_new_from_file(struct xkb_context *ctx,
299                       FILE *file,
300                       enum xkb_keymap_format format,
301                       enum xkb_map_compile_flags flags)
302 {
303     bool ok;
304     XkbFile *xkb_file;
305     struct xkb_keymap *keymap;
306
307     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
308         log_err(ctx, "Unsupported keymap format %d\n", format);
309         return NULL;
310     }
311
312     if (!file) {
313         log_err(ctx, "No file specified to generate XKB keymap\n");
314         return NULL;
315     }
316
317     ok = XKBParseFile(ctx, file, "(unknown file)", &xkb_file);
318     if (!ok) {
319         log_err(ctx, "Failed to parse input xkb file\n");
320         return NULL;
321     }
322
323     keymap = compile_keymap(ctx, xkb_file);
324     FreeXKBFile(xkb_file);
325     return keymap;
326 }
327
328 XKB_EXPORT struct xkb_keymap *
329 xkb_map_ref(struct xkb_keymap *keymap)
330 {
331     keymap->refcnt++;
332     return keymap;
333 }
334
335 XKB_EXPORT void
336 xkb_map_unref(struct xkb_keymap *keymap)
337 {
338     if (!keymap || --keymap->refcnt > 0)
339         return;
340
341     XkbcFreeKeyboard(keymap);
342 }