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>
37 #define EVDEV_OFFSET 8
40 * Test a sequence of keysyms, resulting from a sequence of key presses,
41 * against the keysyms they're supposed to generate.
43 * - Each test runs with a clean state.
44 * - Each line in the test is made up of:
45 * + A keycode, given as a KEY_* from linux/input.h.
46 * + A direction - DOWN for press, UP for release, BOTH for
47 * immediate press + release, REPEAT to just get the syms.
48 * + A sequence of keysyms that should result from this keypress.
50 * The vararg format is:
51 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
53 * See below for examples.
56 test_key_seq(struct xkb_keymap *keymap, ...)
58 struct xkb_state *state;
65 const xkb_keysym_t *syms;
66 unsigned int nsyms, i;
69 state = xkb_state_new(keymap);
75 kc = va_arg(ap, int) + EVDEV_OFFSET;
78 nsyms = xkb_key_get_syms(state, kc, &syms);
79 fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
81 if (op == DOWN || op == BOTH)
82 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
83 if (op == UP || op == BOTH)
84 xkb_state_update_key(state, kc, XKB_KEY_UP);
86 for (i = 0; i < nsyms; i++) {
87 keysym = va_arg(ap, int);
88 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
89 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
91 if (keysym == FINISH || keysym == NEXT) {
92 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
93 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
97 if (keysym != syms[i]) {
98 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
99 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
100 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
101 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
106 fprintf(stderr, "]\n");
108 keysym = va_arg(ap, int);
111 if (keysym == FINISH)
114 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
115 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
120 xkb_state_unref(state);
125 xkb_state_unref(state);
132 struct xkb_context *ctx = test_get_context();
133 struct xkb_keymap *keymap;
136 keymap = test_compile_rules(ctx, "evdev", "evdev",
137 "us,il,ru,de", ",,phonetic,neo",
138 "grp:alt_shift_toggle,grp:menu_toggle");
141 assert(test_key_seq(keymap,
142 KEY_H, BOTH, XKB_KEY_h, NEXT,
143 KEY_E, BOTH, XKB_KEY_e, NEXT,
144 KEY_L, BOTH, XKB_KEY_l, NEXT,
145 KEY_L, BOTH, XKB_KEY_l, NEXT,
146 KEY_O, BOTH, XKB_KEY_o, FINISH));
148 /* Simple shifted level. */
149 assert(test_key_seq(keymap,
150 KEY_H, BOTH, XKB_KEY_h, NEXT,
151 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
152 KEY_E, BOTH, XKB_KEY_E, NEXT,
153 KEY_L, BOTH, XKB_KEY_L, NEXT,
154 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
155 KEY_L, BOTH, XKB_KEY_l, NEXT,
156 KEY_O, BOTH, XKB_KEY_o, FINISH));
158 /* Key repeat shifted and unshifted in the middle. */
159 assert(test_key_seq(keymap,
160 KEY_H, DOWN, XKB_KEY_h, NEXT,
161 KEY_H, REPEAT, XKB_KEY_h, NEXT,
162 KEY_H, REPEAT, XKB_KEY_h, NEXT,
163 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
164 KEY_H, REPEAT, XKB_KEY_H, NEXT,
165 KEY_H, REPEAT, XKB_KEY_H, NEXT,
166 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
167 KEY_H, REPEAT, XKB_KEY_h, NEXT,
168 KEY_H, REPEAT, XKB_KEY_h, NEXT,
169 KEY_H, UP, XKB_KEY_h, NEXT,
170 KEY_H, BOTH, XKB_KEY_h, FINISH));
172 /* Base modifier cleared on key release... */
173 assert(test_key_seq(keymap,
174 KEY_H, BOTH, XKB_KEY_h, NEXT,
175 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
176 KEY_E, BOTH, XKB_KEY_E, NEXT,
177 KEY_L, BOTH, XKB_KEY_L, NEXT,
178 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
179 KEY_L, BOTH, XKB_KEY_L, NEXT,
180 KEY_O, BOTH, XKB_KEY_O, FINISH));
182 /* ... But only by the keycode that set it. */
183 assert(test_key_seq(keymap,
184 KEY_H, BOTH, XKB_KEY_h, NEXT,
185 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
186 KEY_E, BOTH, XKB_KEY_E, NEXT,
187 KEY_L, BOTH, XKB_KEY_L, NEXT,
188 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
189 KEY_L, BOTH, XKB_KEY_L, NEXT,
190 KEY_O, BOTH, XKB_KEY_O, FINISH));
193 * A base modifier should only be cleared when no other key affecting
194 * the modifier is down.
196 assert(test_key_seq(keymap,
197 KEY_H, BOTH, XKB_KEY_h, NEXT,
198 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
199 KEY_E, BOTH, XKB_KEY_E, NEXT,
200 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
201 KEY_L, BOTH, XKB_KEY_L, NEXT,
202 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
203 KEY_L, BOTH, XKB_KEY_L, NEXT,
204 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
205 KEY_O, BOTH, XKB_KEY_o, FINISH));
207 /* Group switching / locking. */
208 assert(test_key_seq(keymap,
209 KEY_H, BOTH, XKB_KEY_h, NEXT,
210 KEY_E, BOTH, XKB_KEY_e, NEXT,
211 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
212 KEY_K, BOTH, XKB_KEY_hebrew_lamed, NEXT,
213 KEY_F, BOTH, XKB_KEY_hebrew_kaph, NEXT,
214 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
215 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
216 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
217 KEY_O, BOTH, XKB_KEY_o, FINISH));
219 assert(test_key_seq(keymap,
220 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
221 KEY_LEFTALT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
222 KEY_LEFTALT, UP, XKB_KEY_ISO_Next_Group, NEXT,
223 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, FINISH));
225 assert(test_key_seq(keymap,
226 KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT,
227 KEY_LEFTSHIFT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
228 KEY_LEFTSHIFT, UP, XKB_KEY_ISO_Next_Group, NEXT,
229 KEY_LEFTALT, UP, XKB_KEY_Alt_L, FINISH));
231 /* Locked modifiers. */
232 assert(test_key_seq(keymap,
233 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
234 KEY_H, BOTH, XKB_KEY_H, NEXT,
235 KEY_E, BOTH, XKB_KEY_E, NEXT,
236 KEY_L, BOTH, XKB_KEY_L, NEXT,
237 KEY_L, BOTH, XKB_KEY_L, NEXT,
238 KEY_O, BOTH, XKB_KEY_O, FINISH));
240 assert(test_key_seq(keymap,
241 KEY_H, BOTH, XKB_KEY_h, NEXT,
242 KEY_E, BOTH, XKB_KEY_e, NEXT,
243 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
244 KEY_L, BOTH, XKB_KEY_L, NEXT,
245 KEY_L, BOTH, XKB_KEY_L, NEXT,
246 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
247 KEY_O, BOTH, XKB_KEY_o, FINISH));
249 assert(test_key_seq(keymap,
250 KEY_H, BOTH, XKB_KEY_h, NEXT,
251 KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT,
252 KEY_E, BOTH, XKB_KEY_E, NEXT,
253 KEY_L, BOTH, XKB_KEY_L, NEXT,
254 KEY_L, BOTH, XKB_KEY_L, NEXT,
255 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
256 KEY_O, BOTH, XKB_KEY_O, FINISH));
258 assert(test_key_seq(keymap,
259 KEY_H, BOTH, XKB_KEY_h, NEXT,
260 KEY_E, BOTH, XKB_KEY_e, NEXT,
261 KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
262 KEY_L, BOTH, XKB_KEY_l, NEXT,
263 KEY_L, BOTH, XKB_KEY_l, NEXT,
264 KEY_O, BOTH, XKB_KEY_o, FINISH));
267 * A key release affecting a locked modifier should clear it
268 * regardless of the key press.
270 /* assert(test_key_seq(keymap, */
271 /* KEY_H, BOTH, XKB_KEY_h, NEXT, */
272 /* KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT, */
273 /* KEY_E, BOTH, XKB_KEY_E, NEXT, */
274 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
275 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
276 /* KEY_L, BOTH, XKB_KEY_L, NEXT, */
277 /* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
278 /* KEY_O, BOTH, XKB_KEY_o, FINISH)); */
280 /* Simple Num Lock sanity check. */
281 assert(test_key_seq(keymap,
282 KEY_KP1, BOTH, XKB_KEY_KP_End, NEXT,
283 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
284 KEY_KP1, BOTH, XKB_KEY_KP_1, NEXT,
285 KEY_KP2, BOTH, XKB_KEY_KP_2, NEXT,
286 KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
287 KEY_KP2, BOTH, XKB_KEY_KP_Down, FINISH));
289 /* Test that the aliases in the ru(phonetic) symbols map work. */
290 assert(test_key_seq(keymap,
291 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
292 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
293 KEY_1, BOTH, XKB_KEY_1, NEXT,
294 KEY_Q, BOTH, XKB_KEY_Cyrillic_ya, NEXT,
295 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
296 KEY_1, BOTH, XKB_KEY_exclam, NEXT,
297 KEY_Q, BOTH, XKB_KEY_Cyrillic_YA, NEXT,
298 KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
299 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
300 KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
301 KEY_1, BOTH, XKB_KEY_1, NEXT,
302 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, NEXT,
303 KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
304 KEY_V, BOTH, XKB_KEY_Cyrillic_zhe, NEXT,
305 KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
306 KEY_V, BOTH, XKB_KEY_Cyrillic_ZHE, FINISH));
308 #define KS(name) xkb_keysym_from_name(name)
310 /* Test that levels (1-5) in de(neo) symbols map work. */
311 assert(test_key_seq(keymap,
312 /* Switch to the group. */
313 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
314 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
315 KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
318 KEY_1, BOTH, XKB_KEY_1, NEXT,
319 KEY_Q, BOTH, XKB_KEY_x, NEXT,
320 KEY_KP7, BOTH, XKB_KEY_KP_7, NEXT,
321 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
323 /* Level 2 with Shift. */
324 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
325 KEY_1, BOTH, XKB_KEY_degree, NEXT,
326 KEY_Q, BOTH, XKB_KEY_X, NEXT,
327 KEY_KP7, BOTH, KS("U2714"), NEXT,
328 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
330 * XXX: de(neo) uses shift(both_capslock) which causes
331 * the interesting result in the next line. Since it's
332 * a key release, it doesn't actually lock the modifier,
333 * and applications by-and-large ignore the keysym on
334 * release(?). Is this a problem?
336 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
338 /* Level 2 with the Lock modifier. */
339 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
340 KEY_RIGHTSHIFT, BOTH, XKB_KEY_Caps_Lock, NEXT,
341 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
342 KEY_6, BOTH, XKB_KEY_6, NEXT,
343 KEY_H, BOTH, XKB_KEY_S, NEXT,
344 KEY_KP3, BOTH, XKB_KEY_KP_3, NEXT,
345 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
346 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
347 KEY_RIGHTSHIFT, BOTH, XKB_KEY_Caps_Lock, NEXT,
348 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
351 KEY_CAPSLOCK, DOWN, XKB_KEY_ISO_Level3_Shift, NEXT,
352 KEY_6, BOTH, XKB_KEY_cent, NEXT,
353 KEY_Q, BOTH, XKB_KEY_ellipsis, NEXT,
354 KEY_KP7, BOTH, KS("U2195"), NEXT,
355 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
356 KEY_CAPSLOCK, UP, XKB_KEY_ISO_Level3_Shift, NEXT,
359 KEY_CAPSLOCK, DOWN, XKB_KEY_ISO_Level3_Shift, NEXT,
360 KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
361 KEY_5, BOTH, XKB_KEY_malesymbol, NEXT,
362 KEY_E, BOTH, XKB_KEY_Greek_lambda, NEXT,
363 KEY_SPACE, BOTH, XKB_KEY_nobreakspace, NEXT,
364 KEY_KP8, BOTH, XKB_KEY_intersection, NEXT,
365 KEY_ESC, BOTH, XKB_KEY_Escape, NEXT,
366 KEY_LEFTSHIFT, UP, XKB_KEY_Caps_Lock, NEXT,
367 KEY_CAPSLOCK, UP, XKB_KEY_ISO_Level3_Shift, NEXT,
370 KEY_RIGHTALT, DOWN, XKB_KEY_ISO_Level5_Shift, NEXT,
372 * XXX: This doesn't work, but gives level1 keysyms.
373 * This does work when when de(neo) is the first layout
374 * (before us,il etc.). It's like that in the X server
375 * as well. Investigate.
377 KEY_RIGHTALT, UP, XKB_KEY_ISO_Level5_Shift, NEXT,
379 KEY_V, BOTH, XKB_KEY_p, FINISH));
381 xkb_map_unref(keymap);
382 xkb_context_unref(ctx);