1 /*
2  * Copyright © 2013 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23 
24 #include "config.h"
25 
26 #include "table.h"
27 #include "utils.h"
28 #include "keysym.h"
29 
30 struct xkb_compose_state {
31     int refcnt;
32     enum xkb_compose_state_flags flags;
33     struct xkb_compose_table *table;
34 
35     /*
36      * Offsets into xkb_compose_table::nodes.
37      *
38      * They maintain the current and previous position in the trie; see
39      * xkb_compose_state_feed().
40      *
41      * This is also sufficient for inferring the current status; see
42      * xkb_compose_state_get_status().
43      */
44     uint32_t prev_context;
45     uint32_t context;
46 };
47 
48 XKB_EXPORT struct xkb_compose_state *
xkb_compose_state_new(struct xkb_compose_table * table,enum xkb_compose_state_flags flags)49 xkb_compose_state_new(struct xkb_compose_table *table,
50                       enum xkb_compose_state_flags flags)
51 {
52     struct xkb_compose_state *state;
53 
54     state = calloc(1, sizeof(*state));
55     if (!state)
56         return NULL;
57 
58     state->refcnt = 1;
59     state->table = xkb_compose_table_ref(table);
60 
61     state->flags = flags;
62     state->prev_context = 0;
63     state->context = 0;
64 
65     return state;
66 }
67 
68 XKB_EXPORT struct xkb_compose_state *
xkb_compose_state_ref(struct xkb_compose_state * state)69 xkb_compose_state_ref(struct xkb_compose_state *state)
70 {
71     state->refcnt++;
72     return state;
73 }
74 
75 XKB_EXPORT void
xkb_compose_state_unref(struct xkb_compose_state * state)76 xkb_compose_state_unref(struct xkb_compose_state *state)
77 {
78     if (!state || --state->refcnt > 0)
79         return;
80 
81     xkb_compose_table_unref(state->table);
82     free(state);
83 }
84 
85 XKB_EXPORT struct xkb_compose_table *
xkb_compose_state_get_compose_table(struct xkb_compose_state * state)86 xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
87 {
88     return state->table;
89 }
90 
91 XKB_EXPORT enum xkb_compose_feed_result
xkb_compose_state_feed(struct xkb_compose_state * state,xkb_keysym_t keysym)92 xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
93 {
94     uint32_t context;
95     const struct compose_node *node;
96 
97     /*
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).
101      *
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
105      * as well.
106      */
107     if (xkb_keysym_is_modifier(keysym))
108         return XKB_COMPOSE_FEED_IGNORED;
109 
110     node = &darray_item(state->table->nodes, state->context);
111 
112     context = (node->is_leaf ? 0 : node->u.successor);
113     node = &darray_item(state->table->nodes, context);
114 
115     while (node->keysym != keysym && node->next != 0) {
116         context = node->next;
117         node = &darray_item(state->table->nodes, context);
118     }
119 
120     if (node->keysym != keysym)
121         context = 0;
122 
123     state->prev_context = state->context;
124     state->context = context;
125     return XKB_COMPOSE_FEED_ACCEPTED;
126 }
127 
128 XKB_EXPORT void
xkb_compose_state_reset(struct xkb_compose_state * state)129 xkb_compose_state_reset(struct xkb_compose_state *state)
130 {
131     state->prev_context = 0;
132     state->context = 0;
133 }
134 
135 XKB_EXPORT enum xkb_compose_status
xkb_compose_state_get_status(struct xkb_compose_state * state)136 xkb_compose_state_get_status(struct xkb_compose_state *state)
137 {
138     const struct compose_node *prev_node, *node;
139 
140     prev_node = &darray_item(state->table->nodes, state->prev_context);
141     node = &darray_item(state->table->nodes, state->context);
142 
143     if (state->context == 0 && !prev_node->is_leaf)
144         return XKB_COMPOSE_CANCELLED;
145 
146     if (state->context == 0)
147         return XKB_COMPOSE_NOTHING;
148 
149     if (!node->is_leaf)
150         return XKB_COMPOSE_COMPOSING;
151 
152     return XKB_COMPOSE_COMPOSED;
153 }
154 
155 XKB_EXPORT int
xkb_compose_state_get_utf8(struct xkb_compose_state * state,char * buffer,size_t size)156 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
157                            char *buffer, size_t size)
158 {
159     const struct compose_node *node =
160         &darray_item(state->table->nodes, state->context);
161 
162     if (!node->is_leaf)
163         goto fail;
164 
165     /* If there's no string specified, but only a keysym, try to do the
166      * most helpful thing. */
167     if (node->u.leaf.utf8 == 0 && node->u.leaf.keysym != XKB_KEY_NoSymbol) {
168         char name[64];
169         int ret;
170 
171         ret = xkb_keysym_to_utf8(node->u.leaf.keysym, name, sizeof(name));
172         if (ret < 0 || ret == 0) {
173             /* ret < 0 is impossible.
174              * ret == 0 means the keysym has no string representation. */
175             goto fail;
176         }
177 
178         return snprintf(buffer, size, "%s", name);
179     }
180 
181     return snprintf(buffer, size, "%s",
182                     &darray_item(state->table->utf8, node->u.leaf.utf8));
183 
184 fail:
185     if (size > 0)
186         buffer[0] = '\0';
187     return 0;
188 }
189 
190 XKB_EXPORT xkb_keysym_t
xkb_compose_state_get_one_sym(struct xkb_compose_state * state)191 xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
192 {
193     const struct compose_node *node =
194         &darray_item(state->table->nodes, state->context);
195     if (!node->is_leaf)
196         return XKB_KEY_NoSymbol;
197     return node->u.leaf.keysym;
198 }
199