Messages: merge macros with and without message code
[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 "config.h"
31
32 #include "xkbcomp-priv.h"
33 #include "rules.h"
34
35 static bool
36 compile_keymap_file(struct xkb_keymap *keymap, XkbFile *file)
37 {
38     if (file->file_type != FILE_TYPE_KEYMAP) {
39         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
40                 "Cannot compile a %s file alone into a keymap\n",
41                 xkb_file_type_to_string(file->file_type));
42         return false;
43     }
44
45     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
46         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
47                 "Failed to compile keymap\n");
48         return false;
49     }
50
51     return true;
52 }
53
54 static bool
55 text_v1_keymap_new_from_names(struct xkb_keymap *keymap,
56                               const struct xkb_rule_names *rmlvo)
57 {
58     bool ok;
59     struct xkb_component_names kccgst;
60     XkbFile *file;
61
62     log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
63             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
64             "variant '%s', options '%s'\n",
65             rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
66             rmlvo->options);
67
68     ok = xkb_components_from_rules(keymap->ctx, rmlvo, &kccgst);
69     if (!ok) {
70         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
71                 "Couldn't look up rules '%s', model '%s', layout '%s', "
72                 "variant '%s', options '%s'\n",
73                 rmlvo->rules, rmlvo->model, rmlvo->layout, rmlvo->variant,
74                 rmlvo->options);
75         return false;
76     }
77
78     log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
79             "Compiling from KcCGST: keycodes '%s', types '%s', "
80             "compat '%s', symbols '%s'\n",
81             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
82
83     file = XkbFileFromComponents(keymap->ctx, &kccgst);
84
85     free(kccgst.keycodes);
86     free(kccgst.types);
87     free(kccgst.compat);
88     free(kccgst.symbols);
89
90     if (!file) {
91         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
92                 "Failed to generate parsed XKB file from components\n");
93         return false;
94     }
95
96     ok = compile_keymap_file(keymap, file);
97     FreeXkbFile(file);
98     return ok;
99 }
100
101 static bool
102 text_v1_keymap_new_from_string(struct xkb_keymap *keymap,
103                                const char *string, size_t len)
104 {
105     bool ok;
106     XkbFile *xkb_file;
107
108     xkb_file = XkbParseString(keymap->ctx, string, len, "(input string)", NULL);
109     if (!xkb_file) {
110         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
111                 "Failed to parse input xkb string\n");
112         return false;
113     }
114
115     ok = compile_keymap_file(keymap, xkb_file);
116     FreeXkbFile(xkb_file);
117     return ok;
118 }
119
120 static bool
121 text_v1_keymap_new_from_file(struct xkb_keymap *keymap, FILE *file)
122 {
123     bool ok;
124     XkbFile *xkb_file;
125
126     xkb_file = XkbParseFile(keymap->ctx, file, "(unknown file)", NULL);
127     if (!xkb_file) {
128         log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
129                 "Failed to parse input xkb file\n");
130         return false;
131     }
132
133     ok = compile_keymap_file(keymap, xkb_file);
134     FreeXkbFile(xkb_file);
135     return ok;
136 }
137
138 const struct xkb_keymap_format_ops text_v1_keymap_format_ops = {
139     .keymap_new_from_names = text_v1_keymap_new_from_names,
140     .keymap_new_from_string = text_v1_keymap_new_from_string,
141     .keymap_new_from_file = text_v1_keymap_new_from_file,
142     .keymap_get_as_string = text_v1_keymap_get_as_string,
143 };