state: apply control transformation on utf8/utf32 keysym strings
[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_LAYOUT_EFFECTIVE);
46     mod = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
47     /* led = xkb_state_serialize_leds(state, XKB_STATE_LEDS); */
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,
57                                              XKB_STATE_LAYOUT_EFFECTIVE |
58                                              XKB_STATE_LAYOUT_DEPRESSED |
59                                              XKB_STATE_LAYOUT_LATCHED |
60                                              XKB_STATE_LAYOUT_LOCKED) <= 0)
61             continue;
62         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
63                 xkb_keymap_layout_get_name(keymap, group),
64                 group,
65                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_EFFECTIVE) > 0 ?
66                     "effective " : "",
67                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_DEPRESSED) > 0 ?
68                     "depressed " : "",
69                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_LATCHED) > 0 ?
70                     "latched " : "",
71                 xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_LOCKED) > 0 ?
72                     "locked " : "");
73     }
74
75     for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
76         if (xkb_state_mod_index_is_active(state, mod,
77                                           XKB_STATE_MODS_EFFECTIVE |
78                                           XKB_STATE_MODS_DEPRESSED |
79                                           XKB_STATE_MODS_LATCHED |
80                                           XKB_STATE_MODS_LOCKED) <= 0)
81             continue;
82         fprintf(stderr, "\tmod %s (%d): %s%s%s%s\n",
83                 xkb_keymap_mod_get_name(keymap, mod),
84                 mod,
85                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_EFFECTIVE) > 0 ?
86                     "effective " : "",
87                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_DEPRESSED) > 0 ?
88                     "depressed " : "",
89                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_LATCHED) > 0 ?
90                     "latched " : "",
91                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_LOCKED) > 0 ?
92                     "locked " : "");
93     }
94
95     for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
96         if (xkb_state_led_index_is_active(state, led) <= 0)
97             continue;
98         fprintf(stderr, "\tled %s (%d): active\n",
99                 xkb_keymap_led_get_name(keymap, led),
100                 led);
101     }
102 }
103
104 static void
105 test_update_key(struct xkb_keymap *keymap)
106 {
107     struct xkb_state *state = xkb_state_new(keymap);
108     const xkb_keysym_t *syms;
109     xkb_keysym_t one_sym;
110     int num_syms;
111
112     assert(state);
113
114     /* LCtrl down */
115     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
116     fprintf(stderr, "dumping state for LCtrl down:\n");
117     print_state(state);
118     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
119                                         XKB_STATE_MODS_DEPRESSED) > 0);
120
121     /* LCtrl + RAlt down */
122     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
123     fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
124     print_state(state);
125     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
126                                         XKB_STATE_MODS_DEPRESSED) > 0);
127     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
128                                         XKB_STATE_MODS_DEPRESSED) > 0);
129     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
130                                           XKB_STATE_MATCH_ALL,
131                                           XKB_MOD_NAME_CTRL,
132                                           XKB_MOD_NAME_ALT,
133                                           NULL) > 0);
134     assert(xkb_state_mod_indices_are_active(state, XKB_STATE_MODS_DEPRESSED,
135                                             XKB_STATE_MATCH_ALL,
136                                             xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL),
137                                             xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT),
138                                             XKB_MOD_INVALID) > 0);
139     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
140                                           XKB_STATE_MATCH_ALL,
141                                           XKB_MOD_NAME_ALT,
142                                           NULL) == 0);
143     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
144                                           XKB_STATE_MATCH_ALL |
145                                           XKB_STATE_MATCH_NON_EXCLUSIVE,
146                                           XKB_MOD_NAME_ALT,
147                                           NULL) > 0);
148     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
149                                           (XKB_STATE_MATCH_ANY |
150                                            XKB_STATE_MATCH_NON_EXCLUSIVE),
151                                           XKB_MOD_NAME_ALT,
152                                           NULL) > 0);
153
154     /* RAlt down */
155     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
156     fprintf(stderr, "dumping state for RAlt down:\n");
157     print_state(state);
158     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
159                                         XKB_STATE_MODS_EFFECTIVE) == 0);
160     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
161                                         XKB_STATE_MODS_DEPRESSED) > 0);
162     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
163                                           XKB_STATE_MATCH_ANY,
164                                           XKB_MOD_NAME_CTRL,
165                                           XKB_MOD_NAME_ALT,
166                                           NULL) > 0);
167     assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_LATCHED,
168                                           XKB_STATE_MATCH_ANY,
169                                           XKB_MOD_NAME_CTRL,
170                                           XKB_MOD_NAME_ALT,
171                                           NULL) == 0);
172
173     /* none down */
174     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
175     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
176                                         XKB_STATE_MODS_EFFECTIVE) == 0);
177
178     /* Caps locked */
179     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
180     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
181                                         XKB_STATE_MODS_DEPRESSED) > 0);
182     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
183     fprintf(stderr, "dumping state for Caps Lock:\n");
184     print_state(state);
185     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
186                                         XKB_STATE_MODS_DEPRESSED) == 0);
187     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
188                                         XKB_STATE_MODS_LOCKED) > 0);
189     assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
190     num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
191     assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
192
193     /* Num Lock locked */
194     xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
195     xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_UP);
196     fprintf(stderr, "dumping state for Caps Lock + Num Lock:\n");
197     print_state(state);
198     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
199                                         XKB_STATE_MODS_LOCKED) > 0);
200     assert(xkb_state_mod_name_is_active(state, "Mod2",
201                                         XKB_STATE_MODS_LOCKED) > 0);
202     num_syms = xkb_state_key_get_syms(state, KEY_KP1 + EVDEV_OFFSET, &syms);
203     assert(num_syms == 1 && syms[0] == XKB_KEY_KP_1);
204     assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_NUM) > 0);
205
206     /* Num Lock unlocked */
207     xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
208     xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_UP);
209
210     /* Switch to group 2 */
211     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
212     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
213     assert(xkb_state_led_name_is_active(state, "Group 2") > 0);
214     assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_NUM) == 0);
215
216     /* Switch back to group 1. */
217     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
218     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
219
220     /* Caps unlocked */
221     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
222     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
223     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
224                                         XKB_STATE_MODS_EFFECTIVE) == 0);
225     assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) == 0);
226     num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
227     assert(num_syms == 1 && syms[0] == XKB_KEY_q);
228
229     /* Multiple symbols */
230     num_syms = xkb_state_key_get_syms(state, KEY_6 + EVDEV_OFFSET, &syms);
231     assert(num_syms == 5 &&
232            syms[0] == XKB_KEY_H && syms[1] == XKB_KEY_E &&
233            syms[2] == XKB_KEY_L && syms[3] == XKB_KEY_L &&
234            syms[4] == XKB_KEY_O);
235     one_sym = xkb_state_key_get_one_sym(state, KEY_6 + EVDEV_OFFSET);
236     assert(one_sym == XKB_KEY_NoSymbol);
237     xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_DOWN);
238     xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_UP);
239
240     one_sym = xkb_state_key_get_one_sym(state, KEY_5 + EVDEV_OFFSET);
241     assert(one_sym == XKB_KEY_5);
242
243     xkb_state_unref(state);
244 }
245
246 static void
247 test_serialisation(struct xkb_keymap *keymap)
248 {
249     struct xkb_state *state = xkb_state_new(keymap);
250     xkb_mod_mask_t base_mods;
251     xkb_mod_mask_t latched_mods;
252     xkb_mod_mask_t locked_mods;
253     xkb_mod_mask_t effective_mods;
254     xkb_mod_index_t caps, shift, ctrl;
255     xkb_layout_index_t base_group = 0;
256     xkb_layout_index_t latched_group = 0;
257     xkb_layout_index_t locked_group = 0;
258
259     assert(state);
260
261     caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
262     assert(caps != XKB_MOD_INVALID);
263     shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
264     assert(shift != XKB_MOD_INVALID);
265     ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
266     assert(ctrl != XKB_MOD_INVALID);
267
268     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
269     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
270     base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
271     assert(base_mods == 0);
272     latched_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
273     assert(latched_mods == 0);
274     locked_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
275     assert(locked_mods == (1U << caps));
276     effective_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
277     assert(effective_mods == locked_mods);
278
279     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
280     base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
281     assert(base_mods == (1U << shift));
282     latched_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
283     assert(latched_mods == 0);
284     locked_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
285     assert(locked_mods == (1U << caps));
286     effective_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
287     assert(effective_mods == (base_mods | locked_mods));
288
289     base_mods |= (1U << ctrl);
290     xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
291                           base_group, latched_group, locked_group);
292
293     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_DEPRESSED) > 0);
294     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
295
296     xkb_state_unref(state);
297 }
298
299 static void
300 test_repeat(struct xkb_keymap *keymap)
301 {
302     assert(!xkb_keymap_key_repeats(keymap, KEY_LEFTSHIFT + 8));
303     assert(xkb_keymap_key_repeats(keymap, KEY_A + 8));
304     assert(xkb_keymap_key_repeats(keymap, KEY_8 + 8));
305     assert(xkb_keymap_key_repeats(keymap, KEY_DOWN + 8));
306     assert(xkb_keymap_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
307 }
308
309 static void
310 test_consume(struct xkb_keymap *keymap)
311 {
312     struct xkb_state *state = xkb_state_new(keymap);
313     xkb_mod_index_t alt, shift;
314     xkb_mod_mask_t mask;
315
316     assert(state);
317
318     alt = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
319     assert(alt != XKB_MOD_INVALID);
320     shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
321     assert(shift != XKB_MOD_INVALID);
322
323     xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
324     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
325     xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
326
327     fprintf(stderr, "dumping state for Alt-Shift-+\n");
328     print_state(state);
329
330     mask = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
331     assert(mask == ((1U << alt) | (1U << shift)));
332     mask = xkb_state_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
333                                               mask);
334     assert(mask == (1U << alt));
335
336     mask = xkb_state_key_get_consumed_mods(state, KEY_EQUAL + EVDEV_OFFSET);
337     assert(mask == (1U << shift));
338
339     xkb_state_unref(state);
340 }
341
342 static void
343 key_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
344 {
345     xkb_keycode_t *counter = data;
346
347     assert(*counter == key);
348     (*counter)++;
349 }
350
351 static void
352 test_range(struct xkb_keymap *keymap)
353 {
354     xkb_keycode_t counter;
355
356     assert(xkb_keymap_min_keycode(keymap) == 9);
357     assert(xkb_keymap_max_keycode(keymap) == 253);
358
359     counter = xkb_keymap_min_keycode(keymap);
360     xkb_keymap_key_for_each(keymap, key_iter, &counter);
361     assert(counter == xkb_keymap_max_keycode(keymap) + 1);
362 }
363
364 static void
365 test_caps_keysym_transformation(struct xkb_keymap *keymap)
366 {
367     struct xkb_state *state = xkb_state_new(keymap);
368     xkb_mod_index_t caps, shift;
369     int nsyms;
370     xkb_keysym_t sym;
371     const xkb_keysym_t *syms;
372
373     assert(state);
374
375     /* See xkb_state_key_get_one_sym() for what's this all about. */
376
377     caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
378     shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
379     assert(caps != XKB_MOD_INVALID && shift != XKB_MOD_INVALID);
380
381     assert(xkb_state_key_get_layout(state, KEY_A + 8) == 0);
382     assert(xkb_state_key_get_layout(state, KEY_SEMICOLON + 8) == 0);
383
384     /* Without caps, no transformation. */
385     assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) == 0);
386     assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
387     assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 0);
388     sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
389     assert(sym == XKB_KEY_a);
390     assert(xkb_state_key_get_level(state, KEY_SEMICOLON + 8, 0) == 0);
391     sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
392     assert(sym == XKB_KEY_eacute);
393     nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
394     assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
395
396     /* With shift, no transformation (only different level). */
397     xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_DOWN);
398     assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) == 0);
399     assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) > 0);
400     assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 1);
401     sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
402     assert(sym == XKB_KEY_A);
403     sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
404     assert(sym == XKB_KEY_odiaeresis);
405     nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
406     assert(nsyms == 1 && syms[0] == XKB_KEY_odiaeresis);
407     xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_UP);
408     assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
409
410     /* With caps, transform in same level, only with _get_one_sym(). */
411     xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_DOWN);
412     xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_UP);
413     assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0);
414     assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
415     assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 1);
416     sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
417     assert(sym == XKB_KEY_A);
418     assert(xkb_state_key_get_level(state, KEY_SEMICOLON + 8, 0) == 0);
419     sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
420     assert(sym == XKB_KEY_Eacute);
421     nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
422     assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
423     xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_UP);
424     assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
425     xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_DOWN);
426     xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_UP);
427
428     xkb_state_unref(state);
429 }
430
431 static void
432 test_get_utf8_utf32(struct xkb_keymap *keymap)
433 {
434     char buf[256];
435     struct xkb_state *state = xkb_state_new(keymap);
436     assert(state);
437
438 #define TEST_KEY(key, expected_utf8, expected_utf32) do { \
439     assert(xkb_state_key_get_utf8(state, key + 8, NULL, 0) == strlen(expected_utf8)); \
440     assert(xkb_state_key_get_utf8(state, key + 8, buf, sizeof(buf)) == strlen(expected_utf8)); \
441     assert(memcmp(buf, expected_utf8, sizeof(expected_utf8)) == 0); \
442     assert(xkb_state_key_get_utf32(state, key + 8) == expected_utf32); \
443 } while (0)
444
445     /* Simple ASCII. */
446     TEST_KEY(KEY_A, "a", 0x61);
447     TEST_KEY(KEY_ESC, "\x1B", 0x1B);
448     TEST_KEY(KEY_1, "1", 0x31);
449
450     /* Invalid. */
451     TEST_KEY(XKB_KEYCODE_INVALID - 8, "", 0);
452     TEST_KEY(300, "", 0);
453
454     /* No string. */
455     TEST_KEY(KEY_LEFTCTRL, "", 0);
456     TEST_KEY(KEY_NUMLOCK, "", 0);
457
458     /* Multiple keysyms. */
459     TEST_KEY(KEY_6, "HELLO", 0);
460     TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
461
462     /* Check truncation. */
463     memset(buf, 'X', sizeof(buf));
464     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 0) == strlen("HELLO"));
465     assert(memcmp(buf, "X", 1) == 0);
466     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 1) == strlen("HELLO"));
467     assert(memcmp(buf, "", 1) == 0);
468     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 2) == strlen("HELLO"));
469     assert(memcmp(buf, "H", 2) == 0);
470     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 3) == strlen("HELLO"));
471     assert(memcmp(buf, "HE", 3) == 0);
472     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 5) == strlen("HELLO"));
473     assert(memcmp(buf, "HELL", 5) == 0);
474     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 6) == strlen("HELLO"));
475     assert(memcmp(buf, "HELLO", 6) == 0);
476     assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 7) == strlen("HELLO"));
477     assert(memcmp(buf, "HELLO\0X", 7) == 0);
478
479     /* Switch to ru layout */
480     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
481     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
482     assert(xkb_state_key_get_layout(state, KEY_A + 8) == 1);
483
484     /* Non ASCII. */
485     TEST_KEY(KEY_ESC, "\x1B", 0x1B);
486     TEST_KEY(KEY_A, "ф", 0x0444);
487     TEST_KEY(KEY_Z, "я", 0x044F);
488
489     /* Switch back to us layout */
490     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
491     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
492     assert(xkb_state_key_get_layout(state, KEY_A + 8) == 0);
493
494     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
495     TEST_KEY(KEY_A, "A", 0x41);
496     TEST_KEY(KEY_ESC, "\x1B", 0x1B);
497     TEST_KEY(KEY_1, "!", 0x21);
498     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
499
500     TEST_KEY(KEY_6, "HELLO", 0);
501     TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
502
503     xkb_state_unref(state);
504 }
505
506 static void
507 test_ctrl_string_transformation(struct xkb_keymap *keymap)
508 {
509     char buf[256];
510     struct xkb_state *state = xkb_state_new(keymap);
511     xkb_mod_index_t ctrl;
512
513     assert(state);
514
515     /* See xkb_state_key_get_utf8() for what's this all about. */
516
517     ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
518     assert(ctrl != XKB_MOD_INVALID);
519
520     /* First without. */
521     TEST_KEY(KEY_A, "a", 0x61);
522     TEST_KEY(KEY_B, "b", 0x62);
523     TEST_KEY(KEY_C, "c", 0x63);
524     TEST_KEY(KEY_ESC, "\x1B", 0x1B);
525     TEST_KEY(KEY_1, "1", 0x31);
526
527     /* And with. */
528     xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
529     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
530     TEST_KEY(KEY_A, "\x01", 0x01);
531     TEST_KEY(KEY_B, "\x02", 0x02);
532     TEST_KEY(KEY_C, "\x03", 0x03);
533     TEST_KEY(KEY_ESC, "\x1B", 0x1B);
534     TEST_KEY(KEY_1, "1", 0x31);
535     xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
536
537     /* Switch to ru layout */
538     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
539     xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
540     assert(xkb_state_key_get_layout(state, KEY_A + 8) == 1);
541
542     /* Non ASCII. */
543     xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
544     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
545     TEST_KEY(KEY_A, "\x01", 0x01);
546     TEST_KEY(KEY_B, "\x02", 0x02);
547     xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
548
549     xkb_state_unref(state);
550 }
551
552 int
553 main(void)
554 {
555     struct xkb_context *context = test_get_context(0);
556     struct xkb_keymap *keymap;
557
558     assert(context);
559
560     /* Make sure these are allowed. */
561     xkb_context_unref(NULL);
562     xkb_keymap_unref(NULL);
563     xkb_state_unref(NULL);
564
565     keymap = test_compile_rules(context, "evdev", "pc104", "us,ru", NULL, "grp:menu_toggle");
566     assert(keymap);
567
568     test_update_key(keymap);
569     test_serialisation(keymap);
570     test_repeat(keymap);
571     test_consume(keymap);
572     test_range(keymap);
573     test_get_utf8_utf32(keymap);
574     test_ctrl_string_transformation(keymap);
575
576     xkb_keymap_unref(keymap);
577     keymap = test_compile_rules(context, "evdev", NULL, "ch", "fr", NULL);
578     assert(keymap);
579
580     test_caps_keysym_transformation(keymap);
581
582     xkb_keymap_unref(keymap);
583     xkb_context_unref(context);
584 }