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.
28 struct xkb_compose_state {
30 enum xkb_compose_state_flags flags;
31 struct xkb_compose_table *table;
34 * Offsets into xkb_compose_table::nodes.
36 * They maintain the current and previous position in the trie; see
37 * xkb_compose_state_feed().
39 * This is also sufficient for inferring the current status; see
40 * xkb_compose_state_get_status().
42 uint32_t prev_context;
46 XKB_EXPORT struct xkb_compose_state *
47 xkb_compose_state_new(struct xkb_compose_table *table,
48 enum xkb_compose_state_flags flags)
50 struct xkb_compose_state *state;
52 state = calloc(1, sizeof(*state));
57 state->table = xkb_compose_table_ref(table);
60 state->prev_context = 0;
66 XKB_EXPORT struct xkb_compose_state *
67 xkb_compose_state_ref(struct xkb_compose_state *state)
74 xkb_compose_state_unref(struct xkb_compose_state *state)
76 if (!state || --state->refcnt > 0)
79 xkb_compose_table_unref(state->table);
83 XKB_EXPORT struct xkb_compose_table *
84 xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
89 XKB_EXPORT enum xkb_compose_feed_result
90 xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
93 const struct compose_node *node;
96 * Modifiers do not affect the sequence directly. In particular,
97 * they do not cancel a sequence; otherwise it'd be impossible to
98 * have a sequence like <dead_acute><A> (needs Shift in the middle).
100 * The following test is not really accurate - in order to test if
101 * a key is "modifier key", we really need the keymap, but we don't
102 * have it here. However, this is (approximately) what libX11 does
105 if (xkb_keysym_is_modifier(keysym))
106 return XKB_COMPOSE_FEED_IGNORED;
108 node = &darray_item(state->table->nodes, state->context);
110 context = (node->is_leaf ? 0 : node->u.successor);
111 node = &darray_item(state->table->nodes, context);
113 while (node->keysym != keysym && node->next != 0) {
114 context = node->next;
115 node = &darray_item(state->table->nodes, context);
118 if (node->keysym != keysym)
121 state->prev_context = state->context;
122 state->context = context;
123 return XKB_COMPOSE_FEED_ACCEPTED;
127 xkb_compose_state_reset(struct xkb_compose_state *state)
129 state->prev_context = 0;
133 XKB_EXPORT enum xkb_compose_status
134 xkb_compose_state_get_status(struct xkb_compose_state *state)
136 const struct compose_node *prev_node, *node;
138 prev_node = &darray_item(state->table->nodes, state->prev_context);
139 node = &darray_item(state->table->nodes, state->context);
141 if (state->context == 0 && !prev_node->is_leaf)
142 return XKB_COMPOSE_CANCELLED;
144 if (state->context == 0)
145 return XKB_COMPOSE_NOTHING;
148 return XKB_COMPOSE_COMPOSING;
150 return XKB_COMPOSE_COMPOSED;
154 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
155 char *buffer, size_t size)
157 const struct compose_node *node =
158 &darray_item(state->table->nodes, state->context);
163 /* If there's no string specified, but only a keysym, try to do the
164 * most helpful thing. */
165 if (node->u.leaf.utf8 == 0 && node->u.leaf.keysym != XKB_KEY_NoSymbol) {
169 ret = xkb_keysym_to_utf8(node->u.leaf.keysym, name, sizeof(name));
170 if (ret < 0 || ret == 0) {
171 /* ret < 0 is impossible.
172 * ret == 0 means the keysym has no string representation. */
176 return snprintf(buffer, size, "%s", name);
179 return snprintf(buffer, size, "%s",
180 &darray_item(state->table->utf8, node->u.leaf.utf8));
188 XKB_EXPORT xkb_keysym_t
189 xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
191 const struct compose_node *node =
192 &darray_item(state->table->nodes, state->context);
194 return XKB_KEY_NoSymbol;
195 return node->u.leaf.keysym;