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.
31 static struct xkb_compose_table *
32 xkb_compose_table_new(struct xkb_context *ctx,
34 enum xkb_compose_format format,
35 enum xkb_compose_compile_flags flags)
37 char *resolved_locale;
38 struct xkb_compose_table *table;
39 struct compose_node root;
41 resolved_locale = resolve_locale(locale);
45 table = calloc(1, sizeof(*table));
47 free(resolved_locale);
52 table->ctx = xkb_context_ref(ctx);
54 table->locale = resolved_locale;
55 table->format = format;
58 darray_init(table->nodes);
59 darray_init(table->utf8);
61 root.keysym = XKB_KEY_NoSymbol;
65 root.u.leaf.keysym = XKB_KEY_NoSymbol;
66 darray_append(table->nodes, root);
68 darray_append(table->utf8, '\0');
73 XKB_EXPORT struct xkb_compose_table *
74 xkb_compose_table_ref(struct xkb_compose_table *table)
81 xkb_compose_table_unref(struct xkb_compose_table *table)
83 if (!table || --table->refcnt > 0)
86 darray_free(table->nodes);
87 darray_free(table->utf8);
88 xkb_context_unref(table->ctx);
92 XKB_EXPORT struct xkb_compose_table *
93 xkb_compose_table_new_from_file(struct xkb_context *ctx,
96 enum xkb_compose_format format,
97 enum xkb_compose_compile_flags flags)
99 struct xkb_compose_table *table;
102 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
103 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
107 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
108 log_err_func(ctx, "unsupported compose format: %d\n", format);
112 table = xkb_compose_table_new(ctx, locale, format, flags);
116 ok = parse_file(table, file, "(unknown file)");
118 xkb_compose_table_unref(table);
125 XKB_EXPORT struct xkb_compose_table *
126 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
127 const char *buffer, size_t length,
129 enum xkb_compose_format format,
130 enum xkb_compose_compile_flags flags)
132 struct xkb_compose_table *table;
135 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
136 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
140 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
141 log_err_func(ctx, "unsupported compose format: %d\n", format);
145 table = xkb_compose_table_new(ctx, locale, format, flags);
149 ok = parse_string(table, buffer, length, "(input string)");
151 xkb_compose_table_unref(table);
158 XKB_EXPORT struct xkb_compose_table *
159 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
161 enum xkb_compose_compile_flags flags)
163 struct xkb_compose_table *table;
169 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
170 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
174 table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
179 cpath = get_xcomposefile_path();
181 file = fopen(cpath, "rb");
186 cpath = path = get_home_xcompose_file_path();
188 file = fopen(path, "rb");
195 cpath = path = get_locale_compose_file_path(table->locale);
197 file = fopen(path, "rb");
204 log_err(ctx, "couldn't find a Compose file for locale \"%s\"\n", locale);
205 xkb_compose_table_unref(table);
209 ok = parse_file(table, file, cpath);
212 xkb_compose_table_unref(table);
216 log_dbg(ctx, "created compose table from locale %s with path %s\n",
217 table->locale, path);