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.
30 struct xkb_compose_state {
32 enum xkb_compose_state_flags flags;
33 struct xkb_compose_table *table;
36 * Offsets into xkb_compose_table::nodes.
38 * They maintain the current and previous position in the trie; see
39 * xkb_compose_state_feed().
41 * This is also sufficient for inferring the current status; see
42 * xkb_compose_state_get_status().
44 uint32_t prev_context;
48 XKB_EXPORT struct xkb_compose_state *
49 xkb_compose_state_new(struct xkb_compose_table *table,
50 enum xkb_compose_state_flags flags)
52 struct xkb_compose_state *state;
54 state = calloc(1, sizeof(*state));
59 state->table = xkb_compose_table_ref(table);
62 state->prev_context = 0;
68 XKB_EXPORT struct xkb_compose_state *
69 xkb_compose_state_ref(struct xkb_compose_state *state)
76 xkb_compose_state_unref(struct xkb_compose_state *state)
78 if (!state || --state->refcnt > 0)
81 xkb_compose_table_unref(state->table);
85 XKB_EXPORT struct xkb_compose_table *
86 xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
91 XKB_EXPORT enum xkb_compose_feed_result
92 xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
95 const struct compose_node *node;
98 * Modifiers do not affect the sequence directly. In particular,
99 * they do not cancel a sequence; otherwise it'd be impossible to
100 * have a sequence like <dead_acute><A> (needs Shift in the middle).
102 * The following test is not really accurate - in order to test if
103 * a key is "modifier key", we really need the keymap, but we don't
104 * have it here. However, this is (approximately) what libX11 does
107 if (xkb_keysym_is_modifier(keysym))
108 return XKB_COMPOSE_FEED_IGNORED;
110 node = &darray_item(state->table->nodes, state->context);
112 context = (node->is_leaf ? 1 : node->internal.eqkid);
113 if (context == 1 && darray_size(state->table->nodes) == 1)
116 while (context != 0) {
117 node = &darray_item(state->table->nodes, context);
118 if (keysym < node->keysym)
119 context = node->lokid;
120 else if (keysym > node->keysym)
121 context = node->hikid;
126 state->prev_context = state->context;
127 state->context = context;
128 return XKB_COMPOSE_FEED_ACCEPTED;
132 xkb_compose_state_reset(struct xkb_compose_state *state)
134 state->prev_context = 0;
138 XKB_EXPORT enum xkb_compose_status
139 xkb_compose_state_get_status(struct xkb_compose_state *state)
141 const struct compose_node *prev_node, *node;
143 prev_node = &darray_item(state->table->nodes, state->prev_context);
144 node = &darray_item(state->table->nodes, state->context);
146 if (state->context == 0 && !prev_node->is_leaf)
147 return XKB_COMPOSE_CANCELLED;
149 if (state->context == 0)
150 return XKB_COMPOSE_NOTHING;
153 return XKB_COMPOSE_COMPOSING;
155 return XKB_COMPOSE_COMPOSED;
159 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
160 char *buffer, size_t size)
162 const struct compose_node *node =
163 &darray_item(state->table->nodes, state->context);
168 /* If there's no string specified, but only a keysym, try to do the
169 * most helpful thing. */
170 if (node->leaf.utf8 == 0 && node->leaf.keysym != XKB_KEY_NoSymbol) {
174 ret = xkb_keysym_to_utf8(node->leaf.keysym, name, sizeof(name));
175 if (ret < 0 || ret == 0) {
176 /* ret < 0 is impossible.
177 * ret == 0 means the keysym has no string representation. */
181 return snprintf(buffer, size, "%s", name);
184 return snprintf(buffer, size, "%s",
185 &darray_item(state->table->utf8, node->leaf.utf8));
193 XKB_EXPORT xkb_keysym_t
194 xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
196 const struct compose_node *node =
197 &darray_item(state->table->nodes, state->context);
199 return XKB_KEY_NoSymbol;
200 return node->leaf.keysym;