2 * Copyright © 2013 Ran Benita <ran234@gmail.com>
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:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
29 static struct xkb_compose_table *
30 xkb_compose_table_new(struct xkb_context *ctx,
32 enum xkb_compose_format format,
33 enum xkb_compose_compile_flags flags)
35 char *resolved_locale;
36 struct xkb_compose_table *table;
37 struct compose_node root;
39 resolved_locale = resolve_locale(locale);
43 table = calloc(1, sizeof(*table));
45 free(resolved_locale);
50 table->ctx = xkb_context_ref(ctx);
52 table->locale = resolved_locale;
53 table->format = format;
56 darray_init(table->nodes);
57 darray_init(table->utf8);
59 root.keysym = XKB_KEY_NoSymbol;
63 root.u.leaf.keysym = XKB_KEY_NoSymbol;
64 darray_append(table->nodes, root);
66 darray_append(table->utf8, '\0');
71 XKB_EXPORT struct xkb_compose_table *
72 xkb_compose_table_ref(struct xkb_compose_table *table)
79 xkb_compose_table_unref(struct xkb_compose_table *table)
81 if (!table || --table->refcnt > 0)
84 darray_free(table->nodes);
85 darray_free(table->utf8);
86 xkb_context_unref(table->ctx);
90 XKB_EXPORT struct xkb_compose_table *
91 xkb_compose_table_new_from_file(struct xkb_context *ctx,
94 enum xkb_compose_format format,
95 enum xkb_compose_compile_flags flags)
97 struct xkb_compose_table *table;
100 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
101 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
105 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
106 log_err_func(ctx, "unsupported compose format: %d\n", format);
110 table = xkb_compose_table_new(ctx, locale, format, flags);
114 ok = parse_file(table, file, "(unknown file)");
116 xkb_compose_table_unref(table);
123 XKB_EXPORT struct xkb_compose_table *
124 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
125 const char *buffer, size_t length,
127 enum xkb_compose_format format,
128 enum xkb_compose_compile_flags flags)
130 struct xkb_compose_table *table;
133 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
134 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
138 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
139 log_err_func(ctx, "unsupported compose format: %d\n", format);
143 table = xkb_compose_table_new(ctx, locale, format, flags);
147 ok = parse_string(table, buffer, length, "(input string)");
149 xkb_compose_table_unref(table);
156 XKB_EXPORT struct xkb_compose_table *
157 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
159 enum xkb_compose_compile_flags flags)
161 struct xkb_compose_table *table;
167 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
168 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
172 table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
177 cpath = get_xcomposefile_path();
179 file = fopen(cpath, "r");
184 cpath = path = get_home_xcompose_file_path();
186 file = fopen(path, "r");
193 cpath = path = get_locale_compose_file_path(table->locale);
195 file = fopen(path, "r");
202 log_err(ctx, "couldn't find a Compose file for locale \"%s\"\n", locale);
203 xkb_compose_table_unref(table);
207 ok = parse_file(table, file, cpath);
210 xkb_compose_table_unref(table);
214 log_dbg(ctx, "created compose table from locale %s with path %s\n",
215 table->locale, path);