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