2 * Copyright © 2013,2021 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.
30 #include "xkbcommon/xkbcommon.h"
32 static struct xkb_compose_table *
33 xkb_compose_table_new(struct xkb_context *ctx,
35 enum xkb_compose_format format,
36 enum xkb_compose_compile_flags flags)
38 char *resolved_locale;
39 struct xkb_compose_table *table;
40 struct compose_node dummy = {0,};
42 resolved_locale = resolve_locale(ctx, locale);
46 table = calloc(1, sizeof(*table));
48 free(resolved_locale);
53 table->ctx = xkb_context_ref(ctx);
55 table->locale = resolved_locale;
56 table->format = format;
59 darray_init(table->nodes);
60 darray_init(table->utf8);
62 dummy.keysym = XKB_KEY_NoSymbol;
65 dummy.leaf.is_leaf = true;
67 dummy.leaf.keysym = XKB_KEY_NoSymbol;
68 darray_append(table->nodes, dummy);
70 darray_append(table->utf8, '\0');
75 XKB_EXPORT struct xkb_compose_table *
76 xkb_compose_table_ref(struct xkb_compose_table *table)
83 xkb_compose_table_unref(struct xkb_compose_table *table)
85 if (!table || --table->refcnt > 0)
88 darray_free(table->nodes);
89 darray_free(table->utf8);
90 xkb_context_unref(table->ctx);
94 XKB_EXPORT struct xkb_compose_table *
95 xkb_compose_table_new_from_file(struct xkb_context *ctx,
98 enum xkb_compose_format format,
99 enum xkb_compose_compile_flags flags)
101 struct xkb_compose_table *table;
104 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
105 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
109 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
110 log_err_func(ctx, "unsupported compose format: %d\n", format);
114 table = xkb_compose_table_new(ctx, locale, format, flags);
118 ok = parse_file(table, file, "(unknown file)");
120 xkb_compose_table_unref(table);
127 XKB_EXPORT struct xkb_compose_table *
128 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
129 const char *buffer, size_t length,
131 enum xkb_compose_format format,
132 enum xkb_compose_compile_flags flags)
134 struct xkb_compose_table *table;
137 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
138 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
142 if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
143 log_err_func(ctx, "unsupported compose format: %d\n", format);
147 table = xkb_compose_table_new(ctx, locale, format, flags);
151 ok = parse_string(table, buffer, length, "(input string)");
153 xkb_compose_table_unref(table);
160 XKB_EXPORT struct xkb_compose_table *
161 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
163 enum xkb_compose_compile_flags flags)
165 struct xkb_compose_table *table;
170 if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
171 log_err_func(ctx, "unrecognized flags: %#x\n", flags);
175 table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
180 path = get_xcomposefile_path(ctx);
182 file = fopen(path, "rb");
188 path = get_xdg_xcompose_file_path(ctx);
190 file = fopen(path, "rb");
196 path = get_home_xcompose_file_path(ctx);
198 file = fopen(path, "rb");
204 path = get_locale_compose_file_path(ctx, table->locale);
206 file = fopen(path, "rb");
212 // TIZEN_ONLY(20210525)
213 // : fix not to display error log about the absense of compose file
214 if (!strncmp("en_US.UTF-8", locale, 11)) {
215 log_err(ctx, "couldn't find a Compose file for locale \"%s\" (mapped to \"%s\")\n",
216 locale, table->locale);
220 xkb_compose_table_unref(table);
224 ok = parse_file(table, file, path);
228 xkb_compose_table_unref(table);
232 log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID,
233 "created compose table from locale %s with path %s\n",
234 table->locale, path);
240 XKB_EXPORT const xkb_keysym_t *
241 xkb_compose_table_entry_sequence(struct xkb_compose_table_entry *entry,
242 size_t *sequence_length)
244 *sequence_length = entry->sequence_length;
245 return entry->sequence;
248 XKB_EXPORT xkb_keysym_t
249 xkb_compose_table_entry_keysym(struct xkb_compose_table_entry *entry)
251 return entry->keysym;
254 XKB_EXPORT const char *
255 xkb_compose_table_entry_utf8(struct xkb_compose_table_entry *entry)
260 enum node_direction {
267 struct xkb_compose_table_iterator_cursor {
268 uint32_t node_offset:30; /* WARNING: ensure it fits MAX_COMPOSE_NODES */
269 uint8_t direction:2; /* enum node_direction: current direction
270 * traversing the tree */
273 struct xkb_compose_table_iterator {
274 struct xkb_compose_table *table;
276 struct xkb_compose_table_entry entry;
277 /* Stack of pending nodes to process */
278 darray(struct xkb_compose_table_iterator_cursor) cursors;
281 XKB_EXPORT struct xkb_compose_table_iterator *
282 xkb_compose_table_iterator_new(struct xkb_compose_table *table)
284 struct xkb_compose_table_iterator *iter;
285 struct xkb_compose_table_iterator_cursor cursor;
286 xkb_keysym_t *sequence;
288 iter = calloc(1, sizeof(*iter));
292 iter->table = xkb_compose_table_ref(table);
293 sequence = calloc(MAX_LHS_LEN, sizeof(xkb_keysym_t));
298 iter->entry.sequence = sequence;
299 iter->entry.sequence_length = 0;
301 darray_init(iter->cursors);
302 cursor.direction = NODE_LEFT;
303 /* Offset 0 is a dummy null entry, skip it. */
304 cursor.node_offset = 1;
305 darray_append(iter->cursors, cursor);
311 xkb_compose_table_iterator_free(struct xkb_compose_table_iterator *iter)
313 xkb_compose_table_unref(iter->table);
314 darray_free(iter->cursors);
315 free(iter->entry.sequence);
319 XKB_EXPORT struct xkb_compose_table_entry *
320 xkb_compose_table_iterator_next(struct xkb_compose_table_iterator *iter)
323 * This function takes the following recursive traversal function,
324 * and makes it non-recursive and resumable. The iter->cursors stack
325 * is analogous to the call stack, and cursor->direction to the
326 * instruction pointer of a stack frame.
328 * traverse(xkb_keysym_t *sequence, size_t sequence_length, uint16_t p) {
330 * // cursor->direction == NODE_LEFT
331 * node = &darray_item(table->nodes, p)
332 * traverse(sequence, sequence_length, node->lokid)
333 * // cursor->direction == NODE_DOWN
334 * sequence[sequence_length++] = node->keysym
336 * emit(sequence, sequence_length, node->leaf.keysym, table->utf[node->leaf.utf8])
338 * traverse(sequence, sequence_length, node->internal.eqkid)
340 * // cursor->direction == NODE_RIGHT
341 * traverse(sequence, sequence_length, node->hikid)
342 * // cursor->direction == NODE_UP
346 struct xkb_compose_table_iterator_cursor *cursor;
347 const struct compose_node *node;
349 while (!darray_empty(iter->cursors)) {
350 cursor = &darray_item(iter->cursors, darray_size(iter->cursors) - 1);
351 node = &darray_item(iter->table->nodes, cursor->node_offset);
353 switch (cursor->direction) {
355 cursor->direction = NODE_DOWN;
357 struct xkb_compose_table_iterator_cursor new_cursor = {node->lokid, NODE_LEFT};
358 darray_append(iter->cursors, new_cursor);
363 cursor->direction = NODE_RIGHT;
364 assert (iter->entry.sequence_length <= MAX_LHS_LEN);
365 iter->entry.sequence[iter->entry.sequence_length] = node->keysym;
366 iter->entry.sequence_length++;
368 iter->entry.keysym = node->leaf.keysym;
369 iter->entry.utf8 = &darray_item(iter->table->utf8, node->leaf.utf8);
372 struct xkb_compose_table_iterator_cursor new_cursor = {node->internal.eqkid, NODE_LEFT};
373 darray_append(iter->cursors, new_cursor);
378 cursor->direction = NODE_UP;
379 iter->entry.sequence_length--;
381 struct xkb_compose_table_iterator_cursor new_cursor = {node->hikid, NODE_LEFT};
382 darray_append(iter->cursors, new_cursor);
387 darray_remove_last(iter->cursors);