keymap: add xkb_keymap_new_from_buffer()
[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 bool
34 compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file)
35 {
36     if (file->file_type != FILE_TYPE_KEYMAP) {
37         log_err(keymap->ctx,
38                 "Cannot compile a %s file alone into a keymap\n",
39                 xkb_file_type_to_string(file->file_type));
40         return false;
41     }
42
43     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
44         log_err(keymap->ctx,
45                 "Failed to compile keymap\n");
46         return false;
47     }
48
49     return true;
50 }
51
52 static bool
53 text_v1_keymap_new_from_names(struct xkb_keymap *keymap,
54                               const struct xkb_rule_names *rmlvo)
55 {
56     bool ok;
57     struct xkb_component_names kccgst;
58     XkbFile *file;
59
60     log_dbg(keymap->ctx,
61             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
62             "variant '%s', options '%s'\n",
63             rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
64             rmlvo->options);
65
66     ok = xkb_components_from_rules(keymap->ctx, rmlvo, &kccgst);
67     if (!ok) {
68         log_err(keymap->ctx,
69                 "Couldn't look up rules '%s', model '%s', layout '%s', "
70                 "variant '%s', options '%s'\n",
71                 rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
72                 rmlvo->options);
73         return false;
74     }
75
76     log_dbg(keymap->ctx,
77             "Compiling from KcCGST: keycodes '%s', types '%s', "
78             "compat '%s', symbols '%s'\n",
79             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
80
81     file = XkbFileFromComponents(keymap->ctx, &kccgst);
82
83     free(kccgst.keycodes);
84     free(kccgst.types);
85     free(kccgst.compat);
86     free(kccgst.symbols);
87
88     if (!file) {
89         log_err(keymap->ctx,
90                 "Failed to generate parsed XKB file from components\n");
91         return false;
92     }
93
94     ok = compile_keymap_file(keymap, file);
95     FreeXkbFile(file);
96     return ok;
97 }
98
99 static bool
100 text_v1_keymap_new_from_string(struct xkb_keymap *keymap, const char *string)
101 {
102     bool ok;
103     XkbFile *xkb_file;
104
105     xkb_file = XkbParseString(keymap->ctx, string, "(input string)");
106     if (!xkb_file) {
107         log_err(keymap->ctx, "Failed to parse input xkb string\n");
108         return NULL;
109     }
110
111     ok = compile_keymap_file(keymap, xkb_file);
112     FreeXkbFile(xkb_file);
113     return ok;
114 }
115
116 static bool
117 text_v1_keymap_new_from_buffer(struct xkb_keymap *keymap,
118                                const char *buffer, size_t length)
119 {
120     bool ok;
121     XkbFile *xkb_file;
122     char *buf;
123
124     buf = malloc(length + 2);
125     if (!buf) {
126         log_err(keymap->ctx, "Cannot allocate memory for keymap\n");
127         return NULL;
128     }
129
130     /* yy_scan_buffer requires two terminating zero bytes */
131     memcpy(buf, buffer, length);
132     buf[length] = 0;
133     buf[length + 1] = 0;
134
135     xkb_file = XkbParseBuffer(keymap->ctx, buf, length + 2, "input");
136     if (!xkb_file) {
137         log_err(keymap->ctx, "Failed to parse input xkb file\n");
138         free(buf);
139         return NULL;
140     }
141
142     ok = compile_keymap_file(keymap, xkb_file);
143     FreeXkbFile(xkb_file);
144     free(buf);
145     return ok;
146 }
147
148 static bool
149 text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file)
150 {
151     bool ok;
152     XkbFile *xkb_file;
153
154     xkb_file = XkbParseFile(keymap->ctx, file, "(unknown file)", NULL);
155     if (!xkb_file) {
156         log_err(keymap->ctx, "Failed to parse input xkb file\n");
157         return false;
158     }
159
160     ok = compile_keymap_file(keymap, xkb_file);
161     FreeXkbFile(xkb_file);
162     return ok;
163 }
164
165 const struct xkb_keymap_format_ops text_v1_keymap_format_ops = {
166     .keymap_new_from_names = text_v1_keymap_new_from_names,
167     .keymap_new_from_string = text_v1_keymap_new_from_string,
168     .keymap_new_from_buffer = text_v1_keymap_new_from_buffer,
169     .keymap_new_from_file = text_v1_keymap_new_from_file,
170     .keymap_get_as_string = text_v1_keymap_get_as_string,
171 };