2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
28 #include <linux/input.h>
30 #include "xkbcommon/xkbcommon.h"
41 #define EVDEV_OFFSET 8
44 * Test a sequence of keysyms, resulting from a sequence of key presses,
45 * against the keysyms they're supposed to generate.
47 * - Each test runs with a clean state.
48 * - Each line in the test is made up of:
49 * + A keycode, given as a KEY_* from linux/input.h.
50 * + A direction - DOWN for press, UP for release, BOTH for
51 * immediate press + release.
52 * + A sequence of keysyms that should result from this keypress.
54 * The vararg format is:
55 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
57 * See below for examples.
60 test_key_seq(struct xkb_keymap *keymap, ...)
62 struct xkb_state *state;
69 const xkb_keysym_t *syms;
70 unsigned int nsyms, i;
73 state = xkb_state_new(keymap);
79 kc = va_arg(ap, int) + EVDEV_OFFSET;
82 nsyms = xkb_key_get_syms(state, kc, &syms);
83 fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
85 if (op == DOWN || op == BOTH)
86 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
87 if (op == UP || op == BOTH)
88 xkb_state_update_key(state, kc, XKB_KEY_UP);
90 for (i = 0; i < nsyms; i++) {
91 keysym = va_arg(ap, int);
92 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
93 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
95 if (keysym == FINISH || keysym == NEXT) {
96 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
97 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
101 if (keysym != syms[i]) {
102 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
103 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
104 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
105 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
110 fprintf(stderr, "]\n");
112 keysym = va_arg(ap, int);
115 if (keysym == FINISH)
118 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
119 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
124 xkb_state_unref(state);
129 xkb_state_unref(state);
136 struct xkb_context *ctx = test_get_context();
137 struct xkb_keymap *keymap;
140 keymap = test_compile_rules(ctx, "evdev", "evdev",
141 "us,il,ru", ",,phonetic",
142 "grp:alt_shift_toggle,grp:menu_toggle");
145 assert(test_key_seq(keymap,
146 KEY_H, BOTH, XKB_KEY_h, NEXT,
147 KEY_E, BOTH, XKB_KEY_e, NEXT,
148 KEY_L, BOTH, XKB_KEY_l, NEXT,
149 KEY_L, BOTH, XKB_KEY_l, NEXT,
150 KEY_O, BOTH, XKB_KEY_o, FINISH));
152 assert(test_key_seq(keymap,
153 KEY_H, BOTH, XKB_KEY_h, NEXT,
154 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
155 KEY_E, BOTH, XKB_KEY_E, NEXT,
156 KEY_L, BOTH, XKB_KEY_L, NEXT,
157 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
158 KEY_L, BOTH, XKB_KEY_l, NEXT,
159 KEY_O, BOTH, XKB_KEY_o, FINISH));
161 /* Base modifier cleared on key release... */
162 assert(test_key_seq(keymap,
163 KEY_H, BOTH, XKB_KEY_h, NEXT,
164 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
165 KEY_E, BOTH, XKB_KEY_E, NEXT,
166 KEY_L, BOTH, XKB_KEY_L, NEXT,
167 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
168 KEY_L, BOTH, XKB_KEY_L, NEXT,
169 KEY_O, BOTH, XKB_KEY_O, FINISH));
171 /* ... But only by the keycode that set it. */
172 assert(test_key_seq(keymap,
173 KEY_H, BOTH, XKB_KEY_h, NEXT,
174 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
175 KEY_E, BOTH, XKB_KEY_E, NEXT,
176 KEY_L, BOTH, XKB_KEY_L, NEXT,
177 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
178 KEY_L, BOTH, XKB_KEY_L, NEXT,
179 KEY_O, BOTH, XKB_KEY_O, FINISH));
182 * A base modifier should only be cleared when no other key affecting
183 * the modifier is down.
185 assert(test_key_seq(keymap,
186 KEY_H, BOTH, XKB_KEY_h, NEXT,
187 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
188 KEY_E, BOTH, XKB_KEY_E, NEXT,
189 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
190 KEY_L, BOTH, XKB_KEY_L, NEXT,
191 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
192 KEY_L, BOTH, XKB_KEY_L, NEXT,
193 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
194 KEY_O, BOTH, XKB_KEY_o, FINISH));
196 /* Group switching / locking. */
197 assert(test_key_seq(keymap,
198 KEY_H, BOTH, XKB_KEY_h, NEXT,
199 KEY_E, BOTH, XKB_KEY_e, NEXT,
200 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
201 KEY_K, BOTH, XKB_KEY_hebrew_lamed, NEXT,
202 KEY_F, BOTH, XKB_KEY_hebrew_kaph, NEXT,
203 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
204 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
205 KEY_O, BOTH, XKB_KEY_o, FINISH));
207 assert(test_key_seq(keymap,
208 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
209 KEY_LEFTALT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
210 KEY_LEFTALT, UP, XKB_KEY_ISO_Next_Group, NEXT,
211 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, FINISH));
213 assert(test_key_seq(keymap,
214 KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT,
215 KEY_LEFTSHIFT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
216 KEY_LEFTSHIFT, UP, XKB_KEY_ISO_Next_Group, NEXT,
217 KEY_LEFTALT, UP, XKB_KEY_Alt_L, FINISH));
219 /* Locked modifiers. */
220 assert(test_key_seq(keymap,
221 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
222 KEY_H, BOTH, XKB_KEY_H, NEXT,
223 KEY_E, BOTH, XKB_KEY_E, NEXT,
224 KEY_L, BOTH, XKB_KEY_L, NEXT,
225 KEY_L, BOTH, XKB_KEY_L, NEXT,
226 KEY_O, BOTH, XKB_KEY_O, FINISH));
228 assert(test_key_seq(keymap,
229 KEY_H, BOTH, XKB_KEY_h, NEXT,
230 KEY_E, BOTH, XKB_KEY_e, NEXT,
231 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
232 KEY_L, BOTH, XKB_KEY_L, NEXT,
233 KEY_L, BOTH, XKB_KEY_L, NEXT,
234 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
235 KEY_O, BOTH, XKB_KEY_o, FINISH));
237 assert(test_key_seq(keymap,
238 KEY_H, BOTH, XKB_KEY_h, NEXT,
239 KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT,
240 KEY_E, BOTH, XKB_KEY_E, NEXT,
241 KEY_L, BOTH, XKB_KEY_L, NEXT,
242 KEY_L, BOTH, XKB_KEY_L, NEXT,
243 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
244 KEY_O, BOTH, XKB_KEY_O, FINISH));
246 assert(test_key_seq(keymap,
247 KEY_H, BOTH, XKB_KEY_h, NEXT,
248 KEY_E, BOTH, XKB_KEY_e, NEXT,
249 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
250 KEY_L, BOTH, XKB_KEY_l, NEXT,
251 KEY_L, BOTH, XKB_KEY_l, NEXT,
252 KEY_O, BOTH, XKB_KEY_o, FINISH));
255 * A key release affecting a locked modifier should clear it
256 * regardless of the key press.
258 /* assert(test_key_seq(keymap, */
259 /* KEY_H, BOTH, XKB_KEY_h, NEXT, */
260 /* KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT, */
261 /* KEY_E, BOTH, XKB_KEY_E, NEXT, */
262 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
263 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
264 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
265 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
266 /* KEY_O, BOTH, XKB_KEY_o, FINISH)); */
268 /* Simple Num Lock sanity check. */
269 assert(test_key_seq(keymap,
270 KEY_KP1, BOTH, XKB_KEY_KP_End, NEXT,
271 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
272 KEY_KP1, BOTH, XKB_KEY_KP_1, NEXT,
273 KEY_KP2, BOTH, XKB_KEY_KP_2, NEXT,
274 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
275 KEY_KP2, BOTH, XKB_KEY_KP_Down, FINISH));
277 /* Test that the aliases in the ru(phonetic) symbols map work. */
278 assert(test_key_seq(keymap,
279 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
280 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
281 KEY_1, BOTH, XKB_KEY_1, NEXT,
282 KEY_Q, BOTH, XKB_KEY_Cyrillic_ya, NEXT,
283 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
284 KEY_1, BOTH, XKB_KEY_exclam, NEXT,
285 KEY_Q, BOTH, XKB_KEY_Cyrillic_YA, NEXT,
286 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
287 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
288 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
289 KEY_1, BOTH, XKB_KEY_1, NEXT,
290 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, NEXT,
291 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
292 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
293 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
294 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, FINISH));
296 xkb_map_unref(keymap);
297 xkb_context_unref(ctx);