summaryrefslogtreecommitdiff
path: root/src/xkbcomp/xkbcomp.c
blob: 32eab6d5dfb35ecdd8e669f6aa624dbed5358042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright 2009  Dan Nicholson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the names of the authors or their
 * institutions shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization from the authors.
 */

#include "xkbcomp-priv.h"
#include "rules.h"
#include "parseutils.h"

/* Global warning level */
unsigned int warningLevel = 0;

#define ISEMPTY(str) (!(str) || (strlen(str) == 0))

static XkbFile *
keymap_file_from_components(struct xkb_context *ctx,
                            const struct xkb_component_names *ktcsg)
{
    XkbFile *keycodes, *types, *compat, *symbols;
    IncludeStmt *inc;

    inc = IncludeCreate(ktcsg->keycodes, MERGE_DEFAULT);
    keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
                             (ParseCommon *) inc, 0);

    inc = IncludeCreate(ktcsg->types, MERGE_DEFAULT);
    types = CreateXKBFile(ctx, FILE_TYPE_TYPES, NULL,
                          (ParseCommon *) inc, 0);
    AppendStmt(&keycodes->common, &types->common);

    inc = IncludeCreate(ktcsg->compat, MERGE_DEFAULT);
    compat = CreateXKBFile(ctx, FILE_TYPE_COMPAT, NULL,
                           (ParseCommon *) inc, 0);
    AppendStmt(&keycodes->common, &compat->common);

    inc = IncludeCreate(ktcsg->symbols, MERGE_DEFAULT);
    symbols = CreateXKBFile(ctx, FILE_TYPE_SYMBOLS, NULL,
                            (ParseCommon *) inc, 0);
    AppendStmt(&keycodes->common, &symbols->common);

    return CreateXKBFile(ctx, FILE_TYPE_KEYMAP, strdup(""),
                         &keycodes->common, 0);
}

/**
 * Compile the given file and store the output in keymap.
 * @param file A list of XkbFiles, each denoting one type (e.g.
 * FILE_TYPE_KEYCODES, etc.)
 */
static struct xkb_keymap *
compile_keymap(struct xkb_context *ctx, XkbFile *file)
{
    bool ok;
    unsigned have = 0;
    const char *main_name;
    struct xkb_keymap *keymap;
    XkbFile *keycodes = NULL;
    XkbFile *types = NULL;
    XkbFile *compat = NULL;
    XkbFile *symbols = NULL;

    keymap = XkbcAllocKeyboard(ctx);
    if (!keymap)
        goto err;

    main_name = file->name ? file->name : "(unnamed)";

    /*
     * Other aggregate file types are converted to FILE_TYPE_KEYMAP
     * in the parser.
     */
    if (file->type != FILE_TYPE_KEYMAP) {
        ERROR("Cannot compile a %s file alone into a keymap\n",
              XkbcFileTypeText(file->type));
        goto err;
    }

    /* Check for duplicate entries in the input file */
    for (file = (XkbFile *) file->defs; file;
         file = (XkbFile *) file->common.next) {
        if (have & file->type) {
            ERROR("More than one %s section in a keymap file\n",
                  XkbcFileTypeText(file->type));
            ACTION("All sections after the first ignored\n");
            continue;
        }

        switch (file->type) {
        case FILE_TYPE_KEYCODES:
            keycodes = file;
            break;

        case FILE_TYPE_TYPES:
            types = file;
            break;

        case FILE_TYPE_SYMBOLS:
            symbols = file;
            break;

        case FILE_TYPE_COMPAT:
            compat = file;
            break;

        default:
            ERROR("Cannot define %s in a keymap file\n",
                  XkbcFileTypeText(file->type));
            continue;
        }

        if (!file->topName) {
            free(file->topName);
            file->topName = strdup(main_name);
        }

        have |= file->type;
    }

    if (REQUIRED_FILE_TYPES & (~have)) {
        enum xkb_file_type bit;
        enum xkb_file_type missing;

        missing = REQUIRED_FILE_TYPES & (~have);

        for (bit = 1; missing != 0; bit <<= 1) {
            if (missing & bit) {
                ERROR("Required section %s missing from keymap\n",
                      XkbcFileTypeText(bit));
                missing &= ~bit;
            }
        }

        goto err;
    }

    ok = CompileKeycodes(keycodes, keymap, MERGE_OVERRIDE);
    if (!ok) {
        ERROR("Failed to compile keycodes\n");
        goto err;
    }
    ok = CompileKeyTypes(types, keymap, MERGE_OVERRIDE);
    if (!ok) {
        ERROR("Failed to compile key types\n");
        goto err;
    }
    ok = CompileCompatMap(compat, keymap, MERGE_OVERRIDE);
    if (!ok) {
        ERROR("Failed to compile compat map\n");
        goto err;
    }
    ok = CompileSymbols(symbols, keymap, MERGE_OVERRIDE);
    if (!ok) {
        ERROR("Failed to compile symbols\n");
        goto err;
    }

    ok = UpdateModifiersFromCompat(keymap);
    if (!ok)
        goto err;

    return keymap;

err:
    ACTION("Failed to compile keymap\n");
    xkb_map_unref(keymap);
    return NULL;
}

struct xkb_keymap *
xkb_map_new_from_kccgst(struct xkb_context *ctx,
                        const struct xkb_component_names *kccgst,
                        enum xkb_map_compile_flags flags)
{
    XkbFile *file;
    struct xkb_keymap *keymap;

    if (!kccgst) {
        ERROR("No components specified\n");
        return NULL;
    }

    if (ISEMPTY(kccgst->keycodes)) {
        ERROR("Keycodes required to generate XKB keymap\n");
        return NULL;
    }

    if (ISEMPTY(kccgst->compat)) {
        ERROR("Compat map required to generate XKB keymap\n");
        return NULL;
    }

    if (ISEMPTY(kccgst->types)) {
        ERROR("Types required to generate XKB keymap\n");
        return NULL;
    }

    if (ISEMPTY(kccgst->symbols)) {
        ERROR("Symbols required to generate XKB keymap\n");
        return NULL;
    }

    file = keymap_file_from_components(ctx, kccgst);
    if (!file) {
        ERROR("Failed to generate parsed XKB file from components\n");
        return NULL;
    }

    keymap = compile_keymap(ctx, file);
    FreeXKBFile(file);
    return keymap;
}

XKB_EXPORT struct xkb_keymap *
xkb_map_new_from_names(struct xkb_context *ctx,
                       const struct xkb_rule_names *rmlvo,
                       enum xkb_map_compile_flags flags)
{
    struct xkb_component_names *kkctgs;
    struct xkb_keymap *keymap;

    if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
        ERROR("rules and layout required to generate XKB keymap\n");
        return NULL;
    }

    kkctgs = xkb_components_from_rules(ctx, rmlvo);
    if (!kkctgs) {
        ERROR("failed to generate XKB components from rules \"%s\"\n",
              rmlvo->rules);
        return NULL;
    }

    keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);

    free(kkctgs->keycodes);
    free(kkctgs->types);
    free(kkctgs->compat);
    free(kkctgs->symbols);
    free(kkctgs);

    return keymap;
}

XKB_EXPORT struct xkb_keymap *
xkb_map_new_from_string(struct xkb_context *ctx,
                        const char *string,
                        enum xkb_keymap_format format,
                        enum xkb_map_compile_flags flags)
{
    bool ok;
    XkbFile *file;
    struct xkb_keymap *keymap;

    if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
        ERROR("unsupported keymap format %d\n", format);
        return NULL;
    }

    if (!string) {
        ERROR("No string specified to generate XKB keymap\n");
        return NULL;
    }

    ok = XKBParseString(ctx, string, "input", &file);
    if (!ok) {
        ERROR("Failed to parse input xkb file\n");
        return NULL;
    }

    keymap = compile_keymap(ctx, file);
    FreeXKBFile(file);
    return keymap;
}

XKB_EXPORT struct xkb_keymap *
xkb_map_new_from_file(struct xkb_context *ctx,
                      FILE *file,
                      enum xkb_keymap_format format,
                      enum xkb_map_compile_flags flags)
{
    bool ok;
    XkbFile *xkb_file;
    struct xkb_keymap *keymap;

    if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
        ERROR("Unsupported keymap format %d\n", format);
        return NULL;
    }

    if (!file) {
        ERROR("No file specified to generate XKB keymap\n");
        return NULL;
    }

    ok = XKBParseFile(ctx, file, "(unknown file)", &xkb_file);
    if (!ok) {
        ERROR("Failed to parse input xkb file\n");
        return NULL;
    }

    keymap = compile_keymap(ctx, xkb_file);
    FreeXKBFile(xkb_file);
    return keymap;
}

XKB_EXPORT struct xkb_keymap *
xkb_map_ref(struct xkb_keymap *keymap)
{
    keymap->refcnt++;
    return keymap;
}

XKB_EXPORT void
xkb_map_unref(struct xkb_keymap *keymap)
{
    if (!keymap || --keymap->refcnt > 0)
        return;

    XkbcFreeKeyboard(keymap);
}