2 * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3 * Copyright © 2012 Intel Corporation
4 * Copyright © 2012 Ran Benita <ran234@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the names of the authors or their
24 * institutions shall not be used in advertising or otherwise to promote the
25 * sale, use or other dealings in this Software without prior written
26 * authorization from the authors.
28 * Author: Dan Nicholson <dbn.lists@gmail.com>
29 * Daniel Stone <daniel@fooishbar.org>
30 * Ran Benita <ran234@gmail.com>
36 #include <sys/types.h>
43 * Test a sequence of keysyms, resulting from a sequence of key presses,
44 * against the keysyms they're supposed to generate.
46 * - Each test runs with a clean state.
47 * - Each line in the test is made up of:
48 * + A keycode, given as a KEY_* from linux/input.h.
49 * + A direction - DOWN for press, UP for release, BOTH for
50 * immediate press + release, REPEAT to just get the syms.
51 * + A sequence of keysyms that should result from this keypress.
53 * The vararg format is:
54 * <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
56 * See below for examples.
59 test_key_seq_va(struct xkb_keymap *keymap, va_list ap)
61 struct xkb_state *state;
67 const xkb_keysym_t *syms;
69 unsigned int nsyms, i;
72 fprintf(stderr, "----\n");
74 state = xkb_state_new(keymap);
78 kc = va_arg(ap, int) + EVDEV_OFFSET;
81 nsyms = xkb_state_key_get_syms(state, kc, &syms);
83 sym = xkb_state_key_get_one_sym(state, kc);
87 fprintf(stderr, "got %u syms for keycode %u: [", nsyms, kc);
89 if (op == DOWN || op == BOTH)
90 xkb_state_update_key(state, kc, XKB_KEY_DOWN);
91 if (op == UP || op == BOTH)
92 xkb_state_update_key(state, kc, XKB_KEY_UP);
94 for (i = 0; i < nsyms; i++) {
95 keysym = va_arg(ap, int);
96 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
97 fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
99 if (keysym == FINISH || keysym == NEXT) {
100 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
101 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
105 if (keysym != syms[i]) {
106 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
107 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
108 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
109 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
115 keysym = va_arg(ap, int);
116 if (keysym != XKB_KEY_NoSymbol) {
117 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
118 fprintf(stderr, "Expected %s, but got no keysyms.\n", ksbuf);
123 fprintf(stderr, "]\n");
125 keysym = va_arg(ap, int);
128 if (keysym == FINISH)
131 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
132 fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
136 xkb_state_unref(state);
140 xkb_state_unref(state);
145 test_key_seq(struct xkb_keymap *keymap, ...)
150 va_start(ap, keymap);
151 ret = test_key_seq_va(keymap, ap);
158 test_get_path(const char *path_rel)
162 const char *srcdir = getenv("top_srcdir");
164 if (path_rel[0] == '/')
165 return strdup(path_rel);
167 path_len = strlen(srcdir ? srcdir : ".") +
168 strlen(path_rel ? path_rel : "") + 12;
169 path = malloc(path_len);
171 fprintf(stderr, "Failed to allocate path (%d chars) for %s\n",
172 (int) path_len, path_rel);
175 snprintf(path, path_len,
176 "%s/test/data/%s", srcdir ? srcdir : ".",
177 path_rel ? path_rel : "");
183 test_read_file(const char *path_rel)
186 char *ret, *tmp, *path;
187 int fd, count, remaining;
189 path = test_get_path(path_rel);
193 fd = open(path, O_RDONLY);
198 if (fstat(fd, &info) != 0) {
203 ret = malloc(info.st_size + 1);
209 remaining = info.st_size;
211 while ((count = read(fd, tmp, remaining))) {
215 ret[info.st_size] = '\0';
218 if (remaining != 0) {
227 test_get_context(enum test_context_flags test_flags)
229 enum xkb_context_flags ctx_flags;
230 struct xkb_context *ctx;
233 ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
234 if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
235 unsetenv("XKB_DEFAULT_RULES");
236 unsetenv("XKB_DEFAULT_MODEL");
237 unsetenv("XKB_DEFAULT_LAYOUT");
238 unsetenv("XKB_DEFAULT_VARIANT");
239 unsetenv("XKB_DEFAULT_OPTIONS");
242 ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
245 ctx = xkb_context_new(ctx_flags);
249 path = test_get_path("");
251 xkb_context_unref(ctx);
255 xkb_context_include_path_append(ctx, path);
262 test_compile_file(struct xkb_context *context, const char *path_rel)
264 struct xkb_keymap *keymap;
268 path = test_get_path(path_rel);
272 file = fopen(path, "r");
274 fprintf(stderr, "Failed to open path: %s\n", path);
278 assert(file != NULL);
280 keymap = xkb_keymap_new_from_file(context, file,
281 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
285 fprintf(stderr, "Failed to compile path: %s\n", path);
290 fprintf(stderr, "Successfully compiled path: %s\n", path);
297 test_compile_string(struct xkb_context *context, const char *string)
299 struct xkb_keymap *keymap;
301 keymap = xkb_keymap_new_from_string(context, string,
302 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
304 fprintf(stderr, "Failed to compile string\n");
312 test_compile_buffer(struct xkb_context *context, const char *buf, size_t len)
314 struct xkb_keymap *keymap;
316 keymap = xkb_keymap_new_from_buffer(context, buf, len,
317 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
319 fprintf(stderr, "Failed to compile keymap from memory buffer\n");
327 test_compile_rules(struct xkb_context *context, const char *rules,
328 const char *model, const char *layout,
329 const char *variant, const char *options)
331 struct xkb_keymap *keymap;
332 struct xkb_rule_names rmlvo = {
333 .rules = isempty(rules) ? NULL : rules,
334 .model = isempty(model) ? NULL : model,
335 .layout = isempty(layout) ? NULL : layout,
336 .variant = isempty(variant) ? NULL : variant,
337 .options = isempty(options) ? NULL : options
340 if (!rules && !model && !layout && !variant && !options)
341 keymap = xkb_keymap_new_from_names(context, NULL, 0);
343 keymap = xkb_keymap_new_from_names(context, &rmlvo, 0);
347 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
348 rules, model, layout, variant, options);
356 test_print_keycode_state(struct xkb_state *state,
357 struct xkb_compose_state *compose_state,
358 xkb_keycode_t keycode,
359 enum xkb_consumed_mode consumed_mode)
361 struct xkb_keymap *keymap;
364 const xkb_keysym_t *syms;
367 xkb_layout_index_t layout;
368 enum xkb_compose_status status;
370 keymap = xkb_state_get_keymap(state);
372 nsyms = xkb_state_key_get_syms(state, keycode, &syms);
377 status = XKB_COMPOSE_NOTHING;
379 status = xkb_compose_state_get_status(compose_state);
381 if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
384 if (status == XKB_COMPOSE_COMPOSED) {
385 sym = xkb_compose_state_get_one_sym(compose_state);
389 else if (nsyms == 1) {
390 sym = xkb_state_key_get_one_sym(state, keycode);
394 printf("keysyms [ ");
395 for (int i = 0; i < nsyms; i++) {
396 xkb_keysym_get_name(syms[i], s, sizeof(s));
397 printf("%-*s ", (int) sizeof(s), s);
401 if (status == XKB_COMPOSE_COMPOSED)
402 xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
404 xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
405 printf("unicode [ %s ] ", s);
407 layout = xkb_state_key_get_layout(state, keycode);
408 printf("layout [ %s (%d) ] ",
409 xkb_keymap_layout_get_name(keymap, layout), layout);
411 printf("level [ %d ] ",
412 xkb_state_key_get_level(state, keycode, layout));
415 for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
416 if (xkb_state_mod_index_is_active(state, mod,
417 XKB_STATE_MODS_EFFECTIVE) <= 0)
419 if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
421 printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
423 printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
428 for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
429 if (xkb_state_led_index_is_active(state, led) <= 0)
431 printf("%s ", xkb_keymap_led_get_name(keymap, led));
439 test_print_state_changes(enum xkb_state_component changed)
444 printf("changed [ ");
445 if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
446 printf("effective-layout ");
447 if (changed & XKB_STATE_LAYOUT_DEPRESSED)
448 printf("depressed-layout ");
449 if (changed & XKB_STATE_LAYOUT_LATCHED)
450 printf("latched-layout ");
451 if (changed & XKB_STATE_LAYOUT_LOCKED)
452 printf("locked-layout ");
453 if (changed & XKB_STATE_MODS_EFFECTIVE)
454 printf("effective-mods ");
455 if (changed & XKB_STATE_MODS_DEPRESSED)
456 printf("depressed-mods ");
457 if (changed & XKB_STATE_MODS_LATCHED)
458 printf("latched-mods ");
459 if (changed & XKB_STATE_MODS_LOCKED)
460 printf("locked-mods ");
461 if (changed & XKB_STATE_LEDS)
467 test_disable_stdin_echo(void)
469 /* Same as `stty -echo`. */
470 struct termios termios;
471 if (tcgetattr(STDIN_FILENO, &termios) == 0) {
472 termios.c_lflag &= ~ECHO;
473 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
478 test_enable_stdin_echo(void)
480 /* Same as `stty echo`. */
481 struct termios termios;
482 if (tcgetattr(STDIN_FILENO, &termios) == 0) {
483 termios.c_lflag |= ECHO;
484 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);