• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     uint16_t prev_context;
45     uint16_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     uint16_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 ? 1 : node->internal.eqkid);
113     if (context == 1 && darray_size(state->table->nodes) == 1)
114         context = 0;
115 
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;
122         else
123             break;
124     }
125 
126     state->prev_context = state->context;
127     state->context = context;
128     return XKB_COMPOSE_FEED_ACCEPTED;
129 }
130 
131 XKB_EXPORT void
xkb_compose_state_reset(struct xkb_compose_state * state)132 xkb_compose_state_reset(struct xkb_compose_state *state)
133 {
134     state->prev_context = 0;
135     state->context = 0;
136 }
137 
138 XKB_EXPORT enum xkb_compose_status
xkb_compose_state_get_status(struct xkb_compose_state * state)139 xkb_compose_state_get_status(struct xkb_compose_state *state)
140 {
141     const struct compose_node *prev_node, *node;
142 
143     prev_node = &darray_item(state->table->nodes, state->prev_context);
144     node = &darray_item(state->table->nodes, state->context);
145 
146     if (state->context == 0 && !prev_node->is_leaf)
147         return XKB_COMPOSE_CANCELLED;
148 
149     if (state->context == 0)
150         return XKB_COMPOSE_NOTHING;
151 
152     if (!node->is_leaf)
153         return XKB_COMPOSE_COMPOSING;
154 
155     return XKB_COMPOSE_COMPOSED;
156 }
157 
158 XKB_EXPORT int
xkb_compose_state_get_utf8(struct xkb_compose_state * state,char * buffer,size_t size)159 xkb_compose_state_get_utf8(struct xkb_compose_state *state,
160                            char *buffer, size_t size)
161 {
162     const struct compose_node *node =
163         &darray_item(state->table->nodes, state->context);
164 
165     if (!node->is_leaf)
166         goto fail;
167 
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) {
171         char name[64];
172         int ret;
173 
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. */
178             goto fail;
179         }
180 
181         return snprintf(buffer, size, "%s", name);
182     }
183 
184     return snprintf(buffer, size, "%s",
185                     &darray_item(state->table->utf8, node->leaf.utf8));
186 
187 fail:
188     if (size > 0)
189         buffer[0] = '\0';
190     return 0;
191 }
192 
193 XKB_EXPORT xkb_keysym_t
xkb_compose_state_get_one_sym(struct xkb_compose_state * state)194 xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
195 {
196     const struct compose_node *node =
197         &darray_item(state->table->nodes, state->context);
198     if (!node->is_leaf)
199         return XKB_KEY_NoSymbol;
200     return node->leaf.keysym;
201 }
202