Debian Codegraph @

src/compose/state.c

raw
1 /*
2 * Copyright © 2013 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9
10 #include "table.h"
11 #include "utils.h"
12 #include "keysym.h"
13
14 struct xkb_compose_state {
15 int refcnt;
16 enum xkb_compose_state_flags flags;
17 struct xkb_compose_table *table;
18
19 /*
20 * Offsets into xkb_compose_table::nodes.
21 *
22 * They maintain the current and previous position in the trie; see
23 * xkb_compose_state_feed().
24 *
25 * This is also sufficient for inferring the current status; see
26 * xkb_compose_state_get_status().
27 */
28 uint32_t prev_context;
29 uint32_t context;
30 };
31
32 struct xkb_compose_state *
33 xkb_compose_state_new(struct xkb_compose_table *table,
34 enum xkb_compose_state_flags flags)
35 {
36 struct xkb_compose_state *state;
37
38 state = calloc(1, sizeof(*state));
39 if (!state)
40 return NULL;
41
42 state->refcnt = 1;
43 state->table = xkb_compose_table_ref(table);
44
45 state->flags = flags;
46 state->prev_context = 0;
47 state->context = 0;
48
49 return state;
50 }
51
52 struct xkb_compose_state *
53 xkb_compose_state_ref(struct xkb_compose_state *state)
54 {
55 assert(state->refcnt > 0);
56 state->refcnt++;
57 return state;
58 }
59
60 void
61 xkb_compose_state_unref(struct xkb_compose_state *state)
62 {
63 assert(!state || state->refcnt > 0);
64 if (!state || --state->refcnt > 0)
65 return;
66
67 xkb_compose_table_unref(state->table);
68 free(state);
69 }
70
71 struct xkb_compose_table *
72 xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
73 {
74 return state->table;
75 }
76
77 enum xkb_compose_feed_result
78 xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
79 {
80 uint32_t context;
81 const struct compose_node *node;
82
83 /*
84 * Modifiers do not affect the sequence directly. In particular,
85 * they do not cancel a sequence; otherwise it'd be impossible to
86 * have a sequence like <dead_acute><A> (needs Shift in the middle).
87 *
88 * The following test is not really accurate - in order to test if
89 * a key is "modifier key", we really need the keymap, but we don't
90 * have it here. However, this is (approximately) what libX11 does
91 * as well.
92 */
93 if (xkb_keysym_is_modifier(keysym))
94 return XKB_COMPOSE_FEED_IGNORED;
95
96 node = &darray_item(state->table->nodes, state->context);
97
98 context = (node->is_leaf ? 1 : node->internal.eqkid);
99 if (context == 1 && darray_size(state->table->nodes) == 1)
100 context = 0;
101
102 while (context != 0) {
103 node = &darray_item(state->table->nodes, context);
104 if (keysym < node->keysym)
105 context = node->lokid;
106 else if (keysym > node->keysym)
107 context = node->hikid;
108 else
109 break;
110 }
111
112 state->prev_context = state->context;
113 state->context = context;
114 return XKB_COMPOSE_FEED_ACCEPTED;
115 }
116
117 void
118 xkb_compose_state_reset(struct xkb_compose_state *state)
119 {
120 state->prev_context = 0;
121 state->context = 0;
122 }
123
124 enum xkb_compose_status
125 xkb_compose_state_get_status(struct xkb_compose_state *state)
126 {
127 const struct compose_node *prev_node, *node;
128
129 prev_node = &darray_item(state->table->nodes, state->prev_context);
130 node = &darray_item(state->table->nodes, state->context);
131
132 if (state->context == 0 && !prev_node->is_leaf)
133 return XKB_COMPOSE_CANCELLED;
134
135 if (state->context == 0)
136 return XKB_COMPOSE_NOTHING;
137
138 if (!node->is_leaf)
139 return XKB_COMPOSE_COMPOSING;
140
141 return XKB_COMPOSE_COMPOSED;
142 }
143
144 int
145 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
146 char *buffer, size_t size)
147 {
148 const struct compose_node *node =
149 &darray_item(state->table->nodes, state->context);
150
151 if (!node->is_leaf)
152 goto fail;
153
154 /* If there's no string specified, but only a keysym, try to do the
155 * most helpful thing. */
156 if (node->leaf.utf8 == 0 && node->leaf.keysym != XKB_KEY_NoSymbol) {
157 char utf8[XKB_KEYSYM_UTF8_MAX_SIZE];
158 int ret;
159
160 ret = xkb_keysym_to_utf8(node->leaf.keysym, utf8, sizeof(utf8));
161 if (ret < 0 || ret == 0) {
162 /* ret < 0 is impossible.
163 * ret == 0 means the keysym has no string representation. */
164 goto fail;
165 }
166
167 return snprintf(buffer, size, "%s", utf8);
168 }
169
170 return snprintf(buffer, size, "%s",
171 &darray_item(state->table->utf8, node->leaf.utf8));
172
173 fail:
174 if (size > 0)
175 buffer[0] = '\0';
176 return 0;
177 }
178
179 xkb_keysym_t
180 xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
181 {
182 const struct compose_node *node =
183 &darray_item(state->table->nodes, state->context);
184 if (!node->is_leaf)
185 return XKB_KEY_NoSymbol;
186 return node->leaf.keysym;
187 }