Tiny reformatting
[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(ktcsg->keycodes, MERGE_DEFAULT);
44     keycodes = CreateXKBFile(ctx, FILE_TYPE_KEYCODES, NULL,
45                              (ParseCommon *)inc, 0);
46
47     inc = IncludeCreate(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(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(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 static struct xkb_keymap *
67 compile_keymap(struct xkb_context *ctx, XkbFile *file)
68 {
69     struct xkb_keymap *keymap;
70
71     keymap = CompileKeymap(ctx, file);
72
73     FreeXKBFile(file);
74     return keymap;
75 }
76
77 struct xkb_keymap *
78 xkb_map_new_from_kccgst(struct xkb_context *ctx,
79                         const struct xkb_component_names *kccgst,
80                         enum xkb_map_compile_flags flags)
81 {
82     XkbFile *file;
83
84     if (!kccgst) {
85         ERROR("no components specified\n");
86         return NULL;
87     }
88
89     if (ISEMPTY(kccgst->keycodes)) {
90         ERROR("keycodes required to generate XKB keymap\n");
91         return NULL;
92     }
93
94     if (ISEMPTY(kccgst->compat)) {
95         ERROR("compat map required to generate XKB keymap\n");
96         return NULL;
97     }
98
99     if (ISEMPTY(kccgst->types)) {
100         ERROR("types required to generate XKB keymap\n");
101         return NULL;
102     }
103
104     if (ISEMPTY(kccgst->symbols)) {
105         ERROR("symbols required to generate XKB keymap\n");
106         return NULL;
107     }
108
109     file = keymap_file_from_components(ctx, kccgst);
110     if (!file) {
111         ERROR("failed to generate parsed XKB file from components\n");
112         return NULL;
113     }
114
115     return compile_keymap(ctx, file);
116 }
117
118 _X_EXPORT struct xkb_keymap *
119 xkb_map_new_from_names(struct xkb_context *ctx,
120                        const struct xkb_rule_names *rmlvo,
121                        enum xkb_map_compile_flags flags)
122 {
123     struct xkb_component_names *kkctgs;
124     struct xkb_keymap *keymap;
125
126     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
127         ERROR("rules and layout required to generate XKB keymap\n");
128         return NULL;
129     }
130
131     kkctgs = xkb_components_from_rules(ctx, rmlvo);
132     if (!kkctgs) {
133         ERROR("failed to generate XKB components from rules \"%s\"\n",
134               rmlvo->rules);
135         return NULL;
136     }
137
138     keymap = xkb_map_new_from_kccgst(ctx, kkctgs, 0);
139
140     free(kkctgs->keycodes);
141     free(kkctgs->types);
142     free(kkctgs->compat);
143     free(kkctgs->symbols);
144     free(kkctgs);
145
146     return keymap;
147 }
148
149 _X_EXPORT struct xkb_keymap *
150 xkb_map_new_from_string(struct xkb_context *ctx,
151                         const char *string,
152                         enum xkb_keymap_format format,
153                         enum xkb_map_compile_flags flags)
154 {
155     XkbFile *file;
156
157     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
158         ERROR("unsupported keymap format %d\n", format);
159         return NULL;
160     }
161
162     if (!string) {
163         ERROR("no string specified to generate XKB keymap\n");
164         return NULL;
165     }
166
167     if (!XKBParseString(ctx, string, "input", &file)) {
168         ERROR("failed to parse input xkb file\n");
169         return NULL;
170     }
171
172     return compile_keymap(ctx, file);
173 }
174
175 _X_EXPORT struct xkb_keymap *
176 xkb_map_new_from_file(struct xkb_context *ctx,
177                       FILE *file,
178                       enum xkb_keymap_format format,
179                       enum xkb_map_compile_flags flags)
180 {
181     XkbFile *xkb_file;
182
183     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
184         ERROR("unsupported keymap format %d\n", format);
185         return NULL;
186     }
187
188     if (!file) {
189         ERROR("no file specified to generate XKB keymap\n");
190         return NULL;
191     }
192
193     if (!XKBParseFile(ctx, file, "(unknown file)", &xkb_file)) {
194         ERROR("failed to parse input xkb file\n");
195         return NULL;
196     }
197
198     return compile_keymap(ctx, xkb_file);
199 }
200
201 _X_EXPORT struct xkb_keymap *
202 xkb_map_ref(struct xkb_keymap *keymap)
203 {
204     keymap->refcnt++;
205     return keymap;
206 }
207
208 _X_EXPORT void
209 xkb_map_unref(struct xkb_keymap *keymap)
210 {
211     if (--keymap->refcnt > 0)
212         return;
213
214     XkbcFreeKeyboard(keymap);
215 }