Rename private xkb_map_new to xkb_keymap_new
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
1 /*
2  * Copyright © 2009 Dan Nicholson
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Dan Nicholson <dbn.lists@gmail.com>
26  *          Ran Benita <ran234@gmail.com>
27  *          Daniel Stone <daniel@fooishbar.org>
28  */
29
30 #include "xkbcomp-priv.h"
31 #include "rules.h"
32
33 static struct xkb_keymap *
34 compile_keymap_file(struct xkb_context *ctx, XkbFile *file)
35 {
36     struct xkb_keymap *keymap;
37
38     keymap = xkb_keymap_new(ctx);
39     if (!keymap)
40         goto err;
41
42     if (file->file_type != FILE_TYPE_KEYMAP) {
43         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
44                 xkb_file_type_to_string(file->file_type));
45         goto err;
46     }
47
48     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
49         log_err(ctx, "Failed to compile keymap\n");
50         goto err;
51     }
52
53     return keymap;
54
55 err:
56     xkb_keymap_unref(keymap);
57     return NULL;
58 }
59
60 XKB_EXPORT struct xkb_keymap *
61 xkb_keymap_new_from_names(struct xkb_context *ctx,
62                           const struct xkb_rule_names *rmlvo_in,
63                           enum xkb_keymap_compile_flags flags)
64 {
65     bool ok;
66     struct xkb_component_names kccgst;
67     struct xkb_rule_names rmlvo = *rmlvo_in;
68     XkbFile *file;
69     struct xkb_keymap *keymap;
70
71     if (isempty(rmlvo.rules))
72         rmlvo.rules = DEFAULT_XKB_RULES;
73     if (isempty(rmlvo.model))
74         rmlvo.model = DEFAULT_XKB_MODEL;
75     if (isempty(rmlvo.layout))
76         rmlvo.layout = DEFAULT_XKB_LAYOUT;
77
78     log_dbg(ctx,
79             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
80             "variant '%s', options '%s'\n",
81             strnull(rmlvo.rules), strnull(rmlvo.model),
82             strnull(rmlvo.layout), strnull(rmlvo.variant),
83             strnull(rmlvo.options));
84
85     ok = xkb_components_from_rules(ctx, &rmlvo, &kccgst);
86     if (!ok) {
87         log_err(ctx,
88                 "Couldn't look up rules '%s', model '%s', layout '%s', "
89                 "variant '%s', options '%s'\n",
90                 strnull(rmlvo.rules), strnull(rmlvo.model),
91                 strnull(rmlvo.layout), strnull(rmlvo.variant),
92                 strnull(rmlvo.options));
93         return NULL;
94     }
95
96     log_dbg(ctx,
97             "Compiling from KcCGST: keycodes '%s', types '%s', "
98             "compat '%s', symbols '%s'\n",
99             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
100
101     file = XkbFileFromComponents(ctx, &kccgst);
102
103     free(kccgst.keycodes);
104     free(kccgst.types);
105     free(kccgst.compat);
106     free(kccgst.symbols);
107
108     if (!file) {
109         log_err(ctx,
110                 "Failed to generate parsed XKB file from components\n");
111         return NULL;
112     }
113
114     keymap = compile_keymap_file(ctx, file);
115     FreeXkbFile(file);
116     return keymap;
117 }
118
119 XKB_EXPORT struct xkb_keymap *
120 xkb_keymap_new_from_string(struct xkb_context *ctx,
121                            const char *string,
122                            enum xkb_keymap_format format,
123                            enum xkb_keymap_compile_flags flags)
124 {
125     bool ok;
126     XkbFile *file;
127     struct xkb_keymap *keymap;
128
129     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
130         log_err(ctx, "Unsupported keymap format %d\n", format);
131         return NULL;
132     }
133
134     if (!string) {
135         log_err(ctx, "No string specified to generate XKB keymap\n");
136         return NULL;
137     }
138
139     ok = XkbParseString(ctx, string, "input", &file);
140     if (!ok) {
141         log_err(ctx, "Failed to parse input xkb file\n");
142         return NULL;
143     }
144
145     keymap = compile_keymap_file(ctx, file);
146     FreeXkbFile(file);
147     return keymap;
148 }
149
150 XKB_EXPORT struct xkb_keymap *
151 xkb_keymap_new_from_file(struct xkb_context *ctx,
152                          FILE *file,
153                          enum xkb_keymap_format format,
154                          enum xkb_keymap_compile_flags flags)
155 {
156     bool ok;
157     XkbFile *xkb_file;
158     struct xkb_keymap *keymap;
159
160     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
161         log_err(ctx, "Unsupported keymap format %d\n", format);
162         return NULL;
163     }
164
165     if (!file) {
166         log_err(ctx, "No file specified to generate XKB keymap\n");
167         return NULL;
168     }
169
170     ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file);
171     if (!ok) {
172         log_err(ctx, "Failed to parse input xkb file\n");
173         return NULL;
174     }
175
176     keymap = compile_keymap_file(ctx, xkb_file);
177     FreeXkbFile(xkb_file);
178     return keymap;
179 }