1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker * Copyright © 2013 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker *
4*2b949d04SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker *
11*2b949d04SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker * Software.
14*2b949d04SAndroid Build Coastguard Worker *
15*2b949d04SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker */
23*2b949d04SAndroid Build Coastguard Worker
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker
26*2b949d04SAndroid Build Coastguard Worker #include "table.h"
27*2b949d04SAndroid Build Coastguard Worker #include "utils.h"
28*2b949d04SAndroid Build Coastguard Worker #include "keysym.h"
29*2b949d04SAndroid Build Coastguard Worker
30*2b949d04SAndroid Build Coastguard Worker struct xkb_compose_state {
31*2b949d04SAndroid Build Coastguard Worker int refcnt;
32*2b949d04SAndroid Build Coastguard Worker enum xkb_compose_state_flags flags;
33*2b949d04SAndroid Build Coastguard Worker struct xkb_compose_table *table;
34*2b949d04SAndroid Build Coastguard Worker
35*2b949d04SAndroid Build Coastguard Worker /*
36*2b949d04SAndroid Build Coastguard Worker * Offsets into xkb_compose_table::nodes.
37*2b949d04SAndroid Build Coastguard Worker *
38*2b949d04SAndroid Build Coastguard Worker * They maintain the current and previous position in the trie; see
39*2b949d04SAndroid Build Coastguard Worker * xkb_compose_state_feed().
40*2b949d04SAndroid Build Coastguard Worker *
41*2b949d04SAndroid Build Coastguard Worker * This is also sufficient for inferring the current status; see
42*2b949d04SAndroid Build Coastguard Worker * xkb_compose_state_get_status().
43*2b949d04SAndroid Build Coastguard Worker */
44*2b949d04SAndroid Build Coastguard Worker uint16_t prev_context;
45*2b949d04SAndroid Build Coastguard Worker uint16_t context;
46*2b949d04SAndroid Build Coastguard Worker };
47*2b949d04SAndroid Build Coastguard Worker
48*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_state *
xkb_compose_state_new(struct xkb_compose_table * table,enum xkb_compose_state_flags flags)49*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_new(struct xkb_compose_table *table,
50*2b949d04SAndroid Build Coastguard Worker enum xkb_compose_state_flags flags)
51*2b949d04SAndroid Build Coastguard Worker {
52*2b949d04SAndroid Build Coastguard Worker struct xkb_compose_state *state;
53*2b949d04SAndroid Build Coastguard Worker
54*2b949d04SAndroid Build Coastguard Worker state = calloc(1, sizeof(*state));
55*2b949d04SAndroid Build Coastguard Worker if (!state)
56*2b949d04SAndroid Build Coastguard Worker return NULL;
57*2b949d04SAndroid Build Coastguard Worker
58*2b949d04SAndroid Build Coastguard Worker state->refcnt = 1;
59*2b949d04SAndroid Build Coastguard Worker state->table = xkb_compose_table_ref(table);
60*2b949d04SAndroid Build Coastguard Worker
61*2b949d04SAndroid Build Coastguard Worker state->flags = flags;
62*2b949d04SAndroid Build Coastguard Worker state->prev_context = 0;
63*2b949d04SAndroid Build Coastguard Worker state->context = 0;
64*2b949d04SAndroid Build Coastguard Worker
65*2b949d04SAndroid Build Coastguard Worker return state;
66*2b949d04SAndroid Build Coastguard Worker }
67*2b949d04SAndroid Build Coastguard Worker
68*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_state *
xkb_compose_state_ref(struct xkb_compose_state * state)69*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_ref(struct xkb_compose_state *state)
70*2b949d04SAndroid Build Coastguard Worker {
71*2b949d04SAndroid Build Coastguard Worker state->refcnt++;
72*2b949d04SAndroid Build Coastguard Worker return state;
73*2b949d04SAndroid Build Coastguard Worker }
74*2b949d04SAndroid Build Coastguard Worker
75*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_compose_state_unref(struct xkb_compose_state * state)76*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_unref(struct xkb_compose_state *state)
77*2b949d04SAndroid Build Coastguard Worker {
78*2b949d04SAndroid Build Coastguard Worker if (!state || --state->refcnt > 0)
79*2b949d04SAndroid Build Coastguard Worker return;
80*2b949d04SAndroid Build Coastguard Worker
81*2b949d04SAndroid Build Coastguard Worker xkb_compose_table_unref(state->table);
82*2b949d04SAndroid Build Coastguard Worker free(state);
83*2b949d04SAndroid Build Coastguard Worker }
84*2b949d04SAndroid Build Coastguard Worker
85*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_compose_table *
xkb_compose_state_get_compose_table(struct xkb_compose_state * state)86*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
87*2b949d04SAndroid Build Coastguard Worker {
88*2b949d04SAndroid Build Coastguard Worker return state->table;
89*2b949d04SAndroid Build Coastguard Worker }
90*2b949d04SAndroid Build Coastguard Worker
91*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT enum xkb_compose_feed_result
xkb_compose_state_feed(struct xkb_compose_state * state,xkb_keysym_t keysym)92*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
93*2b949d04SAndroid Build Coastguard Worker {
94*2b949d04SAndroid Build Coastguard Worker uint16_t context;
95*2b949d04SAndroid Build Coastguard Worker const struct compose_node *node;
96*2b949d04SAndroid Build Coastguard Worker
97*2b949d04SAndroid Build Coastguard Worker /*
98*2b949d04SAndroid Build Coastguard Worker * Modifiers do not affect the sequence directly. In particular,
99*2b949d04SAndroid Build Coastguard Worker * they do not cancel a sequence; otherwise it'd be impossible to
100*2b949d04SAndroid Build Coastguard Worker * have a sequence like <dead_acute><A> (needs Shift in the middle).
101*2b949d04SAndroid Build Coastguard Worker *
102*2b949d04SAndroid Build Coastguard Worker * The following test is not really accurate - in order to test if
103*2b949d04SAndroid Build Coastguard Worker * a key is "modifier key", we really need the keymap, but we don't
104*2b949d04SAndroid Build Coastguard Worker * have it here. However, this is (approximately) what libX11 does
105*2b949d04SAndroid Build Coastguard Worker * as well.
106*2b949d04SAndroid Build Coastguard Worker */
107*2b949d04SAndroid Build Coastguard Worker if (xkb_keysym_is_modifier(keysym))
108*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_FEED_IGNORED;
109*2b949d04SAndroid Build Coastguard Worker
110*2b949d04SAndroid Build Coastguard Worker node = &darray_item(state->table->nodes, state->context);
111*2b949d04SAndroid Build Coastguard Worker
112*2b949d04SAndroid Build Coastguard Worker context = (node->is_leaf ? 1 : node->internal.eqkid);
113*2b949d04SAndroid Build Coastguard Worker if (context == 1 && darray_size(state->table->nodes) == 1)
114*2b949d04SAndroid Build Coastguard Worker context = 0;
115*2b949d04SAndroid Build Coastguard Worker
116*2b949d04SAndroid Build Coastguard Worker while (context != 0) {
117*2b949d04SAndroid Build Coastguard Worker node = &darray_item(state->table->nodes, context);
118*2b949d04SAndroid Build Coastguard Worker if (keysym < node->keysym)
119*2b949d04SAndroid Build Coastguard Worker context = node->lokid;
120*2b949d04SAndroid Build Coastguard Worker else if (keysym > node->keysym)
121*2b949d04SAndroid Build Coastguard Worker context = node->hikid;
122*2b949d04SAndroid Build Coastguard Worker else
123*2b949d04SAndroid Build Coastguard Worker break;
124*2b949d04SAndroid Build Coastguard Worker }
125*2b949d04SAndroid Build Coastguard Worker
126*2b949d04SAndroid Build Coastguard Worker state->prev_context = state->context;
127*2b949d04SAndroid Build Coastguard Worker state->context = context;
128*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_FEED_ACCEPTED;
129*2b949d04SAndroid Build Coastguard Worker }
130*2b949d04SAndroid Build Coastguard Worker
131*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_compose_state_reset(struct xkb_compose_state * state)132*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_reset(struct xkb_compose_state *state)
133*2b949d04SAndroid Build Coastguard Worker {
134*2b949d04SAndroid Build Coastguard Worker state->prev_context = 0;
135*2b949d04SAndroid Build Coastguard Worker state->context = 0;
136*2b949d04SAndroid Build Coastguard Worker }
137*2b949d04SAndroid Build Coastguard Worker
138*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT enum xkb_compose_status
xkb_compose_state_get_status(struct xkb_compose_state * state)139*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_get_status(struct xkb_compose_state *state)
140*2b949d04SAndroid Build Coastguard Worker {
141*2b949d04SAndroid Build Coastguard Worker const struct compose_node *prev_node, *node;
142*2b949d04SAndroid Build Coastguard Worker
143*2b949d04SAndroid Build Coastguard Worker prev_node = &darray_item(state->table->nodes, state->prev_context);
144*2b949d04SAndroid Build Coastguard Worker node = &darray_item(state->table->nodes, state->context);
145*2b949d04SAndroid Build Coastguard Worker
146*2b949d04SAndroid Build Coastguard Worker if (state->context == 0 && !prev_node->is_leaf)
147*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_CANCELLED;
148*2b949d04SAndroid Build Coastguard Worker
149*2b949d04SAndroid Build Coastguard Worker if (state->context == 0)
150*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_NOTHING;
151*2b949d04SAndroid Build Coastguard Worker
152*2b949d04SAndroid Build Coastguard Worker if (!node->is_leaf)
153*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_COMPOSING;
154*2b949d04SAndroid Build Coastguard Worker
155*2b949d04SAndroid Build Coastguard Worker return XKB_COMPOSE_COMPOSED;
156*2b949d04SAndroid Build Coastguard Worker }
157*2b949d04SAndroid Build Coastguard Worker
158*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT int
xkb_compose_state_get_utf8(struct xkb_compose_state * state,char * buffer,size_t size)159*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_get_utf8(struct xkb_compose_state *state,
160*2b949d04SAndroid Build Coastguard Worker char *buffer, size_t size)
161*2b949d04SAndroid Build Coastguard Worker {
162*2b949d04SAndroid Build Coastguard Worker const struct compose_node *node =
163*2b949d04SAndroid Build Coastguard Worker &darray_item(state->table->nodes, state->context);
164*2b949d04SAndroid Build Coastguard Worker
165*2b949d04SAndroid Build Coastguard Worker if (!node->is_leaf)
166*2b949d04SAndroid Build Coastguard Worker goto fail;
167*2b949d04SAndroid Build Coastguard Worker
168*2b949d04SAndroid Build Coastguard Worker /* If there's no string specified, but only a keysym, try to do the
169*2b949d04SAndroid Build Coastguard Worker * most helpful thing. */
170*2b949d04SAndroid Build Coastguard Worker if (node->leaf.utf8 == 0 && node->leaf.keysym != XKB_KEY_NoSymbol) {
171*2b949d04SAndroid Build Coastguard Worker char name[64];
172*2b949d04SAndroid Build Coastguard Worker int ret;
173*2b949d04SAndroid Build Coastguard Worker
174*2b949d04SAndroid Build Coastguard Worker ret = xkb_keysym_to_utf8(node->leaf.keysym, name, sizeof(name));
175*2b949d04SAndroid Build Coastguard Worker if (ret < 0 || ret == 0) {
176*2b949d04SAndroid Build Coastguard Worker /* ret < 0 is impossible.
177*2b949d04SAndroid Build Coastguard Worker * ret == 0 means the keysym has no string representation. */
178*2b949d04SAndroid Build Coastguard Worker goto fail;
179*2b949d04SAndroid Build Coastguard Worker }
180*2b949d04SAndroid Build Coastguard Worker
181*2b949d04SAndroid Build Coastguard Worker return snprintf(buffer, size, "%s", name);
182*2b949d04SAndroid Build Coastguard Worker }
183*2b949d04SAndroid Build Coastguard Worker
184*2b949d04SAndroid Build Coastguard Worker return snprintf(buffer, size, "%s",
185*2b949d04SAndroid Build Coastguard Worker &darray_item(state->table->utf8, node->leaf.utf8));
186*2b949d04SAndroid Build Coastguard Worker
187*2b949d04SAndroid Build Coastguard Worker fail:
188*2b949d04SAndroid Build Coastguard Worker if (size > 0)
189*2b949d04SAndroid Build Coastguard Worker buffer[0] = '\0';
190*2b949d04SAndroid Build Coastguard Worker return 0;
191*2b949d04SAndroid Build Coastguard Worker }
192*2b949d04SAndroid Build Coastguard Worker
193*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT xkb_keysym_t
xkb_compose_state_get_one_sym(struct xkb_compose_state * state)194*2b949d04SAndroid Build Coastguard Worker xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
195*2b949d04SAndroid Build Coastguard Worker {
196*2b949d04SAndroid Build Coastguard Worker const struct compose_node *node =
197*2b949d04SAndroid Build Coastguard Worker &darray_item(state->table->nodes, state->context);
198*2b949d04SAndroid Build Coastguard Worker if (!node->is_leaf)
199*2b949d04SAndroid Build Coastguard Worker return XKB_KEY_NoSymbol;
200*2b949d04SAndroid Build Coastguard Worker return node->leaf.keysym;
201*2b949d04SAndroid Build Coastguard Worker }
202