Interactive tools: Escape control character for Unicode output
[platform/upstream/libxkbcommon.git] / test / keymap.c
1 /*
2  * Copyright © 2016 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: Mike Blumenkrantz <zmike@osg.samsung.com>
24  */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "test.h"
33 #include "keymap.h"
34
35 static void
36 test_garbage_key(void)
37 {
38     struct xkb_context *context = test_get_context(0);
39     struct xkb_keymap *keymap;
40     xkb_keycode_t kc;
41     int nsyms;
42     const xkb_keysym_t *syms;
43     const xkb_layout_index_t first_layout = 0;
44     xkb_level_index_t nlevels;
45
46     assert(context);
47
48     keymap = test_compile_rules(context, NULL, NULL, "garbage", NULL, NULL);
49     assert(keymap);
50
51     /* TLDE uses the 'us' sym on the first level and is thus [grave, exclam] */
52     kc = xkb_keymap_key_by_name(keymap, "TLDE");
53     assert(kc != XKB_KEYCODE_INVALID);
54     nlevels = xkb_keymap_num_levels_for_key(keymap, kc, first_layout);
55     assert(nlevels == 2);
56     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &syms);
57     assert(nsyms == 1);
58     assert(*syms == XKB_KEY_grave); /* fallback from 'us' */
59     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 1, &syms);
60     assert(nsyms == 1);
61     assert(*syms == XKB_KEY_exclam);
62
63     /* AE13 has no 'us' fallback and ends up as [NoSymbol, asciitilde] */
64     kc = xkb_keymap_key_by_name(keymap, "AE13");
65     assert(kc != XKB_KEYCODE_INVALID);
66     nlevels = xkb_keymap_num_levels_for_key(keymap, kc, first_layout);
67     assert(nlevels == 2);
68     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &syms);
69     assert(nsyms == 0);
70     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 1, &syms);
71     assert(nsyms == 1);
72     assert(*syms == XKB_KEY_asciitilde);
73
74     xkb_keymap_unref(keymap);
75     xkb_context_unref(context);
76 }
77
78 static void
79 test_keymap(void)
80 {
81     struct xkb_context *context = test_get_context(0);
82     struct xkb_keymap *keymap;
83     xkb_keycode_t kc;
84     const char *keyname;
85     xkb_mod_mask_t masks_out[4] = { 0, 0, 0, 0 };
86     size_t mask_count;
87     xkb_mod_mask_t shift_mask;
88     xkb_mod_mask_t lock_mask;
89     xkb_mod_mask_t mod2_mask;
90
91     assert(context);
92
93     keymap = test_compile_rules(context, "evdev", "pc104", "us,ru", NULL, "grp:menu_toggle");
94     assert(keymap);
95
96     kc = xkb_keymap_key_by_name(keymap, "AE09");
97     assert(kc != XKB_KEYCODE_INVALID);
98     keyname = xkb_keymap_key_get_name(keymap, kc);
99     assert(streq(keyname, "AE09"));
100
101     kc = xkb_keymap_key_by_name(keymap, "COMP");
102     assert(kc != XKB_KEYCODE_INVALID);
103     keyname = xkb_keymap_key_get_name(keymap, kc);
104     assert(streq(keyname, "COMP"));
105
106     kc = xkb_keymap_key_by_name(keymap, "MENU");
107     assert(kc != XKB_KEYCODE_INVALID);
108     keyname = xkb_keymap_key_get_name(keymap, kc);
109     assert(streq(keyname, "COMP"));
110
111     kc = xkb_keymap_key_by_name(keymap, "AC01");
112     assert(kc != XKB_KEYCODE_INVALID);
113
114     // AC01 level 0 ('a') requires no modifiers on us-pc104
115     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
116     assert(mask_count == 1);
117     assert(masks_out[0] == 0);
118
119     shift_mask = 1 << xkb_keymap_mod_get_index(keymap, "Shift");
120     lock_mask = 1 << xkb_keymap_mod_get_index(keymap, "Lock");
121     mod2_mask = 1 << xkb_keymap_mod_get_index(keymap, "Mod2");
122
123     // AC01 level 1 ('A') requires either Shift or Lock modifiers on us-pc104
124     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 1, masks_out, 4);
125     assert(mask_count == 2);
126     assert(masks_out[0] == shift_mask);
127     assert(masks_out[1] == lock_mask);
128
129     kc = xkb_keymap_key_by_name(keymap, "KP1");
130
131     // KP1 level 0 ('End') requires no modifiers or Shift+Mod2 on us-pc104
132     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
133     assert(mask_count == 2);
134     assert(masks_out[0] == 0);
135     assert(masks_out[1] == (shift_mask | mod2_mask));
136
137     // KP1 level 1 ('1') requires either Shift or Mod2 modifiers on us-pc104
138     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 1, masks_out, 4);
139     assert(mask_count == 2);
140     assert(masks_out[0] == shift_mask);
141     assert(masks_out[1] == mod2_mask);
142
143     // Return key is not affected by modifiers on us-pc104
144     kc = xkb_keymap_key_by_name(keymap, "RTRN");
145     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
146     assert(mask_count == 1);
147     assert(masks_out[0] == 0);
148
149     xkb_keymap_unref(keymap);
150     xkb_context_unref(context);
151 }
152
153 #define Mod1Mask (1 << 3)
154 #define Mod2Mask (1 << 4)
155 #define Mod3Mask (1 << 5)
156
157 static void
158 test_numeric_keysyms(void)
159 {
160     struct xkb_context *context = test_get_context(0);
161     struct xkb_keymap *keymap;
162     const struct xkb_key *key;
163     xkb_keycode_t kc;
164     int keysyms_count;
165     const xkb_layout_index_t first_layout = 0;
166     const xkb_keysym_t *keysyms;
167
168     assert(context);
169
170     keymap = test_compile_rules(context, "evdev", "pc104", "numeric_keysyms", NULL, NULL);
171     assert(keymap);
172
173     kc = xkb_keymap_key_by_name(keymap, "AD01");
174     keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &keysyms);
175     assert(keysyms_count == 1);
176     assert(keysyms[0] == 0x1ffffffd);
177     key = XkbKey(keymap, kc);
178     assert(key->modmap == Mod1Mask);
179
180     kc = xkb_keymap_key_by_name(keymap, "AD02");
181     keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &keysyms);
182     assert(keysyms_count == 1);
183     assert(keysyms[0] == 0x1ffffffe);
184     key = XkbKey(keymap, kc);
185     assert(key->modmap == Mod2Mask);
186
187     kc = xkb_keymap_key_by_name(keymap, "AD03");
188     keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &keysyms);
189     assert(keysyms_count == 1);
190     assert(keysyms[0] == 0x1fffffff);
191     /* Invalid numeric keysym */
192     keysyms_count = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 1, &keysyms);
193     assert(keysyms_count == 0);
194     key = XkbKey(keymap, kc);
195     assert(key->modmap == Mod3Mask);
196
197     xkb_keymap_unref(keymap);
198     xkb_context_unref(context);
199 }
200
201 int
202 main(void)
203 {
204     test_garbage_key();
205     test_keymap();
206     test_numeric_keysyms();
207
208     return 0;
209 }