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