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.
24 #include <linux/input.h>
36 #define EVDEV_OFFSET 8
39 * Test a sequence of keysyms, resulting from a sequence of key presses,
40 * against the keysyms they're supposed to generate.
42 * - Each test runs with a clean state.
43 * - Each line in the test is made up of:
44 * + A keycode, given as a KEY_* from linux/input.h.
45 * + A direction - DOWN for press, UP for release, BOTH for
46 * immediate press + release.
47 * + A sequence of keysyms that should result from this keypress.
49 * The vararg format is:
50 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
52 * See below for examples.
55 test_key_seq(struct xkb_keymap *keymap, ...)
57 struct xkb_state *state;
64 const xkb_keysym_t *syms;
65 unsigned int nsyms, i;
68 state = xkb_state_new(keymap);
74 kc = va_arg(ap, int) + EVDEV_OFFSET;
77 nsyms = xkb_key_get_syms(state, kc, &syms);
78 fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
80 if (op == DOWN || op == BOTH)
81 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
82 if (op == UP || op == BOTH)
83 xkb_state_update_key(state, kc, XKB_KEY_UP);
85 for (i = 0; i < nsyms; i++) {
86 keysym = va_arg(ap, int);
87 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
88 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
90 if (keysym == FINISH || keysym == NEXT) {
91 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
92 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
96 if (keysym != syms[i]) {
97 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
98 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
99 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
100 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
105 fprintf(stderr, "]\n");
107 keysym = va_arg(ap, int);
110 if (keysym == FINISH)
113 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
114 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
119 xkb_state_unref(state);
124 xkb_state_unref(state);
131 struct xkb_context *ctx = test_get_context();
132 struct xkb_keymap *keymap;
135 keymap = test_compile_rules(ctx, "evdev", "evdev",
136 "us,il,ru,de", ",,phonetic,neo",
137 "grp:alt_shift_toggle,grp:menu_toggle");
140 assert(test_key_seq(keymap,
141 KEY_H, BOTH, XKB_KEY_h, NEXT,
142 KEY_E, BOTH, XKB_KEY_e, NEXT,
143 KEY_L, BOTH, XKB_KEY_l, NEXT,
144 KEY_L, BOTH, XKB_KEY_l, NEXT,
145 KEY_O, BOTH, XKB_KEY_o, FINISH));
147 assert(test_key_seq(keymap,
148 KEY_H, BOTH, XKB_KEY_h, NEXT,
149 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
150 KEY_E, BOTH, XKB_KEY_E, NEXT,
151 KEY_L, BOTH, XKB_KEY_L, NEXT,
152 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
153 KEY_L, BOTH, XKB_KEY_l, NEXT,
154 KEY_O, BOTH, XKB_KEY_o, FINISH));
156 /* Base modifier cleared on key release... */
157 assert(test_key_seq(keymap,
158 KEY_H, BOTH, XKB_KEY_h, NEXT,
159 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
160 KEY_E, BOTH, XKB_KEY_E, NEXT,
161 KEY_L, BOTH, XKB_KEY_L, NEXT,
162 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
163 KEY_L, BOTH, XKB_KEY_L, NEXT,
164 KEY_O, BOTH, XKB_KEY_O, FINISH));
166 /* ... But only by the keycode that set it. */
167 assert(test_key_seq(keymap,
168 KEY_H, BOTH, XKB_KEY_h, NEXT,
169 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
170 KEY_E, BOTH, XKB_KEY_E, NEXT,
171 KEY_L, BOTH, XKB_KEY_L, NEXT,
172 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
173 KEY_L, BOTH, XKB_KEY_L, NEXT,
174 KEY_O, BOTH, XKB_KEY_O, FINISH));
177 * A base modifier should only be cleared when no other key affecting
178 * the modifier is down.
180 assert(test_key_seq(keymap,
181 KEY_H, BOTH, XKB_KEY_h, NEXT,
182 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
183 KEY_E, BOTH, XKB_KEY_E, NEXT,
184 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
185 KEY_L, BOTH, XKB_KEY_L, NEXT,
186 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
187 KEY_L, BOTH, XKB_KEY_L, NEXT,
188 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
189 KEY_O, BOTH, XKB_KEY_o, FINISH));
191 /* Group switching / locking. */
192 assert(test_key_seq(keymap,
193 KEY_H, BOTH, XKB_KEY_h, NEXT,
194 KEY_E, BOTH, XKB_KEY_e, NEXT,
195 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
196 KEY_K, BOTH, XKB_KEY_hebrew_lamed, NEXT,
197 KEY_F, BOTH, XKB_KEY_hebrew_kaph, NEXT,
198 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
199 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
200 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
201 KEY_O, BOTH, XKB_KEY_o, FINISH));
203 assert(test_key_seq(keymap,
204 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
205 KEY_LEFTALT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
206 KEY_LEFTALT, UP, XKB_KEY_ISO_Next_Group, NEXT,
207 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, FINISH));
209 assert(test_key_seq(keymap,
210 KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT,
211 KEY_LEFTSHIFT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
212 KEY_LEFTSHIFT, UP, XKB_KEY_ISO_Next_Group, NEXT,
213 KEY_LEFTALT, UP, XKB_KEY_Alt_L, FINISH));
215 /* Locked modifiers. */
216 assert(test_key_seq(keymap,
217 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
218 KEY_H, BOTH, XKB_KEY_H, NEXT,
219 KEY_E, BOTH, XKB_KEY_E, NEXT,
220 KEY_L, BOTH, XKB_KEY_L, NEXT,
221 KEY_L, BOTH, XKB_KEY_L, NEXT,
222 KEY_O, BOTH, XKB_KEY_O, FINISH));
224 assert(test_key_seq(keymap,
225 KEY_H, BOTH, XKB_KEY_h, NEXT,
226 KEY_E, BOTH, XKB_KEY_e, NEXT,
227 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
228 KEY_L, BOTH, XKB_KEY_L, NEXT,
229 KEY_L, BOTH, XKB_KEY_L, NEXT,
230 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
231 KEY_O, BOTH, XKB_KEY_o, FINISH));
233 assert(test_key_seq(keymap,
234 KEY_H, BOTH, XKB_KEY_h, NEXT,
235 KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT,
236 KEY_E, BOTH, XKB_KEY_E, NEXT,
237 KEY_L, BOTH, XKB_KEY_L, NEXT,
238 KEY_L, BOTH, XKB_KEY_L, NEXT,
239 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
240 KEY_O, BOTH, XKB_KEY_O, FINISH));
242 assert(test_key_seq(keymap,
243 KEY_H, BOTH, XKB_KEY_h, NEXT,
244 KEY_E, BOTH, XKB_KEY_e, NEXT,
245 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
246 KEY_L, BOTH, XKB_KEY_l, NEXT,
247 KEY_L, BOTH, XKB_KEY_l, NEXT,
248 KEY_O, BOTH, XKB_KEY_o, FINISH));
251 * A key release affecting a locked modifier should clear it
252 * regardless of the key press.
254 /* assert(test_key_seq(keymap, */
255 /* KEY_H, BOTH, XKB_KEY_h, NEXT, */
256 /* KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT, */
257 /* KEY_E, BOTH, XKB_KEY_E, NEXT, */
258 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
259 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
260 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
261 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
262 /* KEY_O, BOTH, XKB_KEY_o, FINISH)); */
264 /* Simple Num Lock sanity check. */
265 assert(test_key_seq(keymap,
266 KEY_KP1, BOTH, XKB_KEY_KP_End, NEXT,
267 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
268 KEY_KP1, BOTH, XKB_KEY_KP_1, NEXT,
269 KEY_KP2, BOTH, XKB_KEY_KP_2, NEXT,
270 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
271 KEY_KP2, BOTH, XKB_KEY_KP_Down, FINISH));
273 /* Test that the aliases in the ru(phonetic) symbols map work. */
274 assert(test_key_seq(keymap,
275 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
276 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
277 KEY_1, BOTH, XKB_KEY_1, NEXT,
278 KEY_Q, BOTH, XKB_KEY_Cyrillic_ya, NEXT,
279 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
280 KEY_1, BOTH, XKB_KEY_exclam, NEXT,
281 KEY_Q, BOTH, XKB_KEY_Cyrillic_YA, NEXT,
282 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
283 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
284 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
285 KEY_1, BOTH, XKB_KEY_1, NEXT,
286 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, NEXT,
287 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
288 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
289 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
290 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, FINISH));
292 #define KS(name) xkb_keysym_from_name(name)
294 /* Test that levels (1-5) in de(neo) symbols map work. */
295 assert(test_key_seq(keymap,
296 /* Switch to the group. */
297 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
298 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
299 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
302 KEY_1, BOTH, XKB_KEY_1, NEXT,
303 KEY_Q, BOTH, XKB_KEY_x, NEXT,
304 KEY_KP7, BOTH, XKB_KEY_KP_7, NEXT,
305 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
307 /* Level 2 with Shift. */
308 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
309 KEY_1, BOTH, XKB_KEY_degree, NEXT,
310 KEY_Q, BOTH, XKB_KEY_X, NEXT,
311 KEY_KP7, BOTH, KS("U2714"), NEXT,
312 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
314 * XXX: de(neo) uses shift(both_capslock) which causes
315 * the interesting result in the next line. Since it's
316 * a key release, it doesn't actually lock the modifier,
317 * and applications by-and-large ignore the keysym on
318 * release(?). Is this a problem?
320 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
322 /* Level 2 with the Lock modifier. */
323 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
324 KEY_RIGHTSHIFT, BOTH, XKB_KEY_Caps_Lock, NEXT,
325 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
326 KEY_6, BOTH, XKB_KEY_6, NEXT,
327 KEY_H, BOTH, XKB_KEY_S, NEXT,
328 KEY_KP3, BOTH, XKB_KEY_KP_3, NEXT,
329 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
330 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
331 KEY_RIGHTSHIFT, BOTH, XKB_KEY_Caps_Lock, NEXT,
332 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
335 KEY_CAPSLOCK, DOWN, XKB_KEY_ISO_Level3_Shift, NEXT,
336 KEY_6, BOTH, XKB_KEY_cent, NEXT,
337 KEY_Q, BOTH, XKB_KEY_ellipsis, NEXT,
338 KEY_KP7, BOTH, KS("U2195"), NEXT,
339 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
340 KEY_CAPSLOCK, UP, XKB_KEY_ISO_Level3_Shift, NEXT,
343 KEY_CAPSLOCK, DOWN, XKB_KEY_ISO_Level3_Shift, NEXT,
344 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
345 KEY_5, BOTH, XKB_KEY_malesymbol, NEXT,
346 KEY_E, BOTH, XKB_KEY_Greek_lambda, NEXT,
347 KEY_SPACE, BOTH, XKB_KEY_nobreakspace, NEXT,
348 KEY_KP8, BOTH, XKB_KEY_intersection, NEXT,
349 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
350 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
351 KEY_CAPSLOCK, UP, XKB_KEY_ISO_Level3_Shift, NEXT,
354 KEY_RIGHTALT, DOWN, XKB_KEY_ISO_Level5_Shift, NEXT,
356 * XXX: This doesn't work, but gives level1 keysyms.
357 * This does work when when de(neo) is the first layout
358 * (before us,il etc.). It's like that in the X server
359 * as well. Investigate.
361 KEY_RIGHTALT, UP, XKB_KEY_ISO_Level5_Shift, NEXT,
363 KEY_V, BOTH, XKB_KEY_p, FINISH));
365 xkb_map_unref(keymap);
366 xkb_context_unref(ctx);