3da4c99778899e75048a5a198e2eba3b5ad6bf09
[platform/upstream/libxkbcommon.git] / test / state.c
1 /*
2  * Copyright © 2012 Intel Corporation
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  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <linux/input.h>
30
31 #include "test.h"
32
33 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
34  * keycode set (where ESC is 9). */
35 #define EVDEV_OFFSET 8
36
37 static void
38 print_state(struct xkb_state *state)
39 {
40     struct xkb_keymap *keymap;
41     xkb_layout_index_t group;
42     xkb_mod_index_t mod;
43     xkb_led_index_t led;
44
45     group = xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
46     mod = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
47     /* led = xkb_state_serialize_leds(state, XKB_STATE_EFFECTIVE); */
48     if (!group && !mod /* && !led */) {
49         fprintf(stderr, "\tno state\n");
50         return;
51     }
52
53     keymap = xkb_state_get_keymap(state);
54
55     for (group = 0; group < xkb_keymap_num_layouts(keymap); group++) {
56         if (xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE) != 1)
57             continue;
58         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
59                 xkb_keymap_layout_get_name(keymap, group),
60                 group,
61                 xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE) > 0 ?
62                     "effective " : "",
63                 xkb_state_layout_index_is_active(state, group, XKB_STATE_DEPRESSED) > 0 ?
64                     "depressed " : "",
65                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LATCHED) > 0 ?
66                     "latched " : "",
67                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LOCKED) > 0 ?
68                     "locked " : "");
69     }
70
71     for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
72         if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
73             continue;
74         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
75                 xkb_keymap_mod_get_name(keymap, mod),
76                 mod,
77                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) > 0 ?
78                     "depressed " : "",
79                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) > 0 ?
80                     "latched " : "",
81                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) > 0 ?
82                     "locked " : "");
83     }
84
85     for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
86         if (!xkb_state_led_index_is_active(state, led))
87             continue;
88         fprintf(stderr, "\tled %s (%d): active\n",
89                 xkb_keymap_led_get_name(keymap, led),
90                 led);
91     }
92 }
93
94 static void
95 test_update_key(struct xkb_keymap *keymap)
96 {
97     struct xkb_state *state = xkb_state_new(keymap);
98     const xkb_keysym_t *syms;
99     xkb_keysym_t one_sym;
100     int num_syms;
101
102     assert(state);
103
104     /* LCtrl down */
105     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
106     fprintf(stderr, "dumping state for LCtrl down:\n");
107     print_state(state);
108     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
109                                         XKB_STATE_DEPRESSED) > 0);
110
111     /* LCtrl + RAlt down */
112     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
113     fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
114     print_state(state);
115     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
116                                         XKB_STATE_DEPRESSED) > 0);
117     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
118                                         XKB_STATE_DEPRESSED) > 0);
119     assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
120                                           XKB_STATE_MATCH_ALL,
121                                           XKB_MOD_NAME_CTRL,
122                                           XKB_MOD_NAME_ALT,
123                                           NULL) > 0);
124     assert(xkb_state_mod_indices_are_active(state, XKB_STATE_DEPRESSED,
125                                           XKB_STATE_MATCH_ALL,
126                                           xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL),
127                                           xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT),
128                                           XKB_MOD_INVALID) > 0);
129     assert(!xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
130                                            XKB_STATE_MATCH_ALL,
131                                            XKB_MOD_NAME_ALT,
132                                            NULL) > 0);
133     assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
134                                           (XKB_STATE_MATCH_ANY |
135                                            XKB_STATE_MATCH_NON_EXCLUSIVE),
136                                           XKB_MOD_NAME_ALT,
137                                           NULL));
138
139     /* RAlt down */
140     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
141     fprintf(stderr, "dumping state for RAlt down:\n");
142     print_state(state);
143     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
144                                          XKB_STATE_EFFECTIVE) > 0);
145     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
146                                         XKB_STATE_DEPRESSED) > 0);
147     assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
148                                           XKB_STATE_MATCH_ANY,
149                                           XKB_MOD_NAME_CTRL,
150                                           XKB_MOD_NAME_ALT,
151                                           NULL) > 0);
152
153     /* none down */
154     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
155     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
156                                          XKB_STATE_EFFECTIVE));
157
158     /* Caps locked */
159     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
160     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
161     fprintf(stderr, "dumping state for Caps Lock:\n");
162     print_state(state);
163     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
164                                         XKB_STATE_LOCKED) > 0);
165     assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
166     num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
167     assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
168
169     /* Caps unlocked */
170     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
171     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
172     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
173                                          XKB_STATE_EFFECTIVE) > 0);
174     assert(!xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
175     num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
176     assert(num_syms == 1 && syms[0] == XKB_KEY_q);
177
178     /* Multiple symbols */
179     num_syms = xkb_state_key_get_syms(state, KEY_6 + EVDEV_OFFSET, &syms);
180     assert(num_syms == 5 &&
181            syms[0] == XKB_KEY_H && syms[1] == XKB_KEY_E &&
182            syms[2] == XKB_KEY_L && syms[3] == XKB_KEY_L &&
183            syms[4] == XKB_KEY_O);
184     one_sym = xkb_state_key_get_one_sym(state, KEY_6 + EVDEV_OFFSET);
185     assert(one_sym == XKB_KEY_NoSymbol);
186     xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_DOWN);
187     xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_UP);
188
189     one_sym = xkb_state_key_get_one_sym(state, KEY_5 + EVDEV_OFFSET);
190     assert(one_sym == XKB_KEY_5);
191
192     xkb_state_unref(state);
193 }
194
195 static void
196 test_serialisation(struct xkb_keymap *keymap)
197 {
198     struct xkb_state *state = xkb_state_new(keymap);
199     xkb_mod_mask_t base_mods;
200     xkb_mod_mask_t latched_mods;
201     xkb_mod_mask_t locked_mods;
202     xkb_mod_mask_t effective_mods;
203     xkb_mod_index_t caps, shift, ctrl;
204     xkb_layout_index_t base_group = 0;
205     xkb_layout_index_t latched_group = 0;
206     xkb_layout_index_t locked_group = 0;
207
208     assert(state);
209
210     caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
211     assert(caps != XKB_MOD_INVALID);
212     shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
213     assert(shift != XKB_MOD_INVALID);
214     ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
215     assert(ctrl != XKB_MOD_INVALID);
216
217     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
218     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
219     base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
220     assert(base_mods == 0);
221     latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
222     assert(latched_mods == 0);
223     locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
224     assert(locked_mods == (1 << caps));
225     effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
226     assert(effective_mods == locked_mods);
227
228     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
229     base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
230     assert(base_mods == (1 << shift));
231     latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
232     assert(latched_mods == 0);
233     locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
234     assert(locked_mods == (1 << caps));
235     effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
236     assert(effective_mods == (base_mods | locked_mods));
237
238     base_mods |= (1 << ctrl);
239     xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
240                           base_group, latched_group, locked_group);
241
242     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
243     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
244
245     xkb_state_unref(state);
246 }
247
248 static void
249 test_repeat(struct xkb_keymap *keymap)
250 {
251     assert(!xkb_keymap_key_repeats(keymap, KEY_LEFTSHIFT + 8));
252     assert(xkb_keymap_key_repeats(keymap, KEY_A + 8));
253     assert(xkb_keymap_key_repeats(keymap, KEY_8 + 8));
254     assert(xkb_keymap_key_repeats(keymap, KEY_DOWN + 8));
255     assert(xkb_keymap_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
256 }
257
258 static void
259 test_consume(struct xkb_keymap *keymap)
260 {
261     struct xkb_state *state = xkb_state_new(keymap);
262     xkb_mod_index_t alt, shift;
263     xkb_mod_mask_t mask;
264
265     assert(state);
266
267     alt = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
268     assert(alt != XKB_MOD_INVALID);
269     shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
270     assert(shift != XKB_MOD_INVALID);
271
272     xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
273     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
274     xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
275
276     fprintf(stderr, "dumping state for Alt-Shift-+\n");
277     print_state(state);
278
279     mask = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
280     assert(mask == ((1 << alt) | (1 << shift)));
281     mask = xkb_state_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
282                                               mask);
283     assert(mask == (1 << alt));
284
285     xkb_state_unref(state);
286 }
287
288 int
289 main(void)
290 {
291     struct xkb_context *context = test_get_context();
292     struct xkb_keymap *keymap;
293
294     assert(context);
295
296     keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
297     assert(keymap);
298
299     test_update_key(keymap);
300     test_serialisation(keymap);
301     test_repeat(keymap);
302     test_consume(keymap);
303
304     xkb_keymap_unref(keymap);
305     xkb_context_unref(context);
306 }