Debian Codegraph @

test/x11comp.c

raw
1 /*
2 * Copyright © 2014 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <getopt.h>
10 #include <signal.h>
11 #include <spawn.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18
19 #include "xkbcommon/xkbcommon.h"
20 #include "xkbcommon/xkbcommon-keysyms.h"
21 #include "xkbcommon/xkbcommon-names.h"
22 #include "xkbcommon/xkbcommon-x11.h"
23
24 #include "evdev-scancodes.h"
25 #include "test.h"
26 #include "tools/tools-common.h"
27 #include "utils.h"
28 #include "utils-text.h"
29 #include "xvfb-wrapper.h"
30
31 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
32 * keycode set (where ESC is 9). */
33 #define EVDEV_OFFSET 8
34
35 enum update_files {
36 NO_UPDATE = 0,
37 UPDATE_USING_TEST_INPUT,
38 UPDATE_USING_TEST_OUTPUT
39 };
40
41 static enum update_files update_test_files = NO_UPDATE;
42
43
44 static char *
45 prepare_keymap(const char *original, bool strip_all_comments)
46 {
47 if (strip_all_comments) {
48 return strip_lines(original, strlen(original), "//");
49 } else {
50 char *tmp = uncomment(original, strlen(original), "//");
51 char *ret = strip_lines(tmp, strlen(tmp), "//");
52 free(tmp);
53 return ret;
54 }
55 }
56
57 /*
58 * Reset keymap to `empty` on the server.
59 * It seems that xkbcomp does not fully set the keymap on the server and
60 * the conflicting leftovers may raise errors.
61 */
62 static int
63 reset_keymap(const char* display)
64 {
65 char *envp[] = { NULL };
66 const char* setxkbmap_argv[] = {
67 "setxkbmap", "-display", display,
68 "-geometry", "pc",
69 "-keycodes", "evdev",
70 "-compat", "basic",
71 "-types", "basic+numpad", /* Avoid extra types */
72 "-symbols", "us",
73 // "-v", "10",
74 NULL
75 };
76
77 /* Launch xkbcomp */
78 pid_t setxkbmap_pid;
79 int ret = posix_spawnp(&setxkbmap_pid, "setxkbmap", NULL, NULL,
80 (char* const*)setxkbmap_argv, envp);
81 if (ret != 0) {
82 fprintf(stderr,
83 "[ERROR] Cannot run setxkbmap. posix_spawnp error %d: %s\n",
84 ret, strerror(ret));
85 if (ret == ENOENT) {
86 fprintf(stderr,
87 "[ERROR] setxkbmap may be missing. "
88 "Please install the corresponding package, "
89 "e.g. \"setxkbmap\" or \"x11-xkb-utils\".\n");
90 }
91 return TEST_SETUP_FAILURE;
92 }
93 int status;
94 ret = waitpid(setxkbmap_pid, &status, 0);
95 return (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
96 ? TEST_SETUP_FAILURE
97 : EXIT_SUCCESS;
98 }
99
100 // TODO: unused for now
101 // /* Use xkbcomp to load a keymap from a file */
102 // static int
103 // run_xkbcomp_file(const char* display, xcb_connection_t *conn, int32_t device_id,
104 // const char *xkb_path)
105 // {
106 // /* Prepare xkbcomp parameters */
107 // char *envp[] = { NULL };
108 // const char* xkbcomp_argv[] = {"xkbcomp", "-I", xkb_path, display, NULL};
109 //
110 // /* Launch xkbcomp */
111 // pid_t xkbcomp_pid;
112 // int ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL,
113 // (char* const*)xkbcomp_argv, envp);
114 // if (ret != 0) {
115 // fprintf(stderr,
116 // "[ERROR] Cannot run xkbcomp. posix_spawnp error %d: %s\n",
117 // ret, strerror(ret));
118 // if (ret == ENOENT) {
119 // fprintf(stderr,
120 // "[ERROR] xkbcomp may be missing. "
121 // "Please install the corresponding package, "
122 // "e.g. \"xkbcomp\" or \"x11-xkb-utils\".\n");
123 // }
124 // return TEST_SETUP_FAILURE;
125 // }
126 //
127 // /* Wait for xkbcomp to complete */
128 // int status;
129 // ret = waitpid(xkbcomp_pid, &status, 0);
130 // return (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
131 // ? TEST_SETUP_FAILURE
132 // : EXIT_SUCCESS;
133 // }
134
135 enum {
136 PIPE_READ = 0,
137 PIPE_WRITE = 1
138 };
139
140 /* Use xkbcomp to load a keymap from a string */
141 static int
142 run_xkbcomp_str(const char* display, const char *include_path, const char *keymap)
143 {
144 int ret = EXIT_FAILURE;
145 assert(keymap);
146
147 /* Prepare xkbcomp parameters */
148 char *envp[] = { NULL };
149 char include_path_arg[PATH_MAX+2] = "-I";
150 const char *xkbcomp_argv[] = {
151 "xkbcomp", "-I" /* reset include path*/, include_path_arg,
152 "-opt", "g", "-w", "10", "-" /* stdin */, display, NULL
153 };
154 if (include_path) {
155 ret = snprintf(include_path_arg, ARRAY_SIZE(include_path_arg),
156 "-I%s", include_path);
157 if (ret < 0 || ret >= (int)ARRAY_SIZE(include_path_arg)) {
158 return TEST_SETUP_FAILURE;
159 }
160 }
161
162 /* Prepare input */
163 int stdin_pipe[2];
164 posix_spawn_file_actions_t action;
165 if (pipe(stdin_pipe) == -1) {
166 perror("pipe");
167 ret = TEST_SETUP_FAILURE;
168 goto pipe_error;
169 }
170 if (posix_spawn_file_actions_init(&action)) {
171 perror("spawn_file_actions_init error");
172 goto posix_spawn_file_actions_init_error;
173 }
174
175 /* Make spawned process close unused write-end of pipe, else it will not
176 * receive EOF when write-end of the pipe is closed below and it will result
177 * in a deadlock. */
178 if (posix_spawn_file_actions_addclose(&action, stdin_pipe[PIPE_WRITE]))
179 goto posix_spawn_file_actions_error;
180 /* Make spawned process replace stdin with read end of the pipe */
181 if (posix_spawn_file_actions_adddup2(&action, stdin_pipe[PIPE_READ], STDIN_FILENO))
182 goto posix_spawn_file_actions_error;
183
184 /* Launch xkbcomp */
185 pid_t xkbcomp_pid;
186 ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", &action, NULL,
187 (char* const*)xkbcomp_argv, envp);
188 if (ret != 0) {
189 fprintf(stderr,
190 "[ERROR] Cannot run xkbcomp. posix_spawnp error %d: %s\n",
191 ret, strerror(ret));
192 if (ret == ENOENT) {
193 fprintf(stderr,
194 "[ERROR] xkbcomp may be missing. "
195 "Please install the corresponding package, "
196 "e.g. \"xkbcomp\" or \"x11-xkb-utils\".\n");
197 }
198 goto posix_spawn_file_actions_init_error;
199 }
200 /* Close unused read-end of pipe */
201 close(stdin_pipe[PIPE_READ]);
202 const ssize_t count = write(stdin_pipe[PIPE_WRITE], keymap, strlen(keymap));
203 /* Close write-end of the pipe, to emit EOF */
204 close(stdin_pipe[PIPE_WRITE]);
205 if (count == -1) {
206 perror("Cannot write keymap to stdin");
207 kill(xkbcomp_pid, SIGTERM);
208 }
209
210 /* Wait for xkbcomp to complete */
211 int status;
212 ret = waitpid(xkbcomp_pid, &status, 0);
213 ret = (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
214 ? TEST_SETUP_FAILURE
215 : EXIT_SUCCESS;
216 goto cleanup;
217
218 posix_spawn_file_actions_error:
219 perror("posix_spawn_file_actions_* error");
220 posix_spawn_file_actions_init_error:
221 close(stdin_pipe[PIPE_WRITE]);
222 close(stdin_pipe[PIPE_READ]);
223 ret = TEST_SETUP_FAILURE;
224 cleanup:
225 posix_spawn_file_actions_destroy(&action);
226 pipe_error:
227 return ret;
228 }
229
230 // TODO: implement tests to ensure compatibility with xkbcomp
231 // struct compile_buffer_private {
232 // const char* display;
233 // xcb_connection_t *conn;
234 // int32_t device_id;
235 // };
236 //
237 // static struct xkb_keymap *
238 // compile_buffer(struct xkb_context *ctx, const char *buf, size_t len,
239 // void *private)
240 // {
241 // const struct compile_buffer_private *config = private;
242 // assert(buf[len - 1] == '\0');
243 //
244 // char *include_path = test_get_path("");
245 // int ret = run_xkbcomp_str(config->display, config->conn,
246 // config->device_id, include_path, buf);
247 // free(include_path);
248 //
249 // if (ret != EXIT_SUCCESS)
250 // return NULL;
251 //
252 // return xkb_x11_keymap_new_from_device(ctx, config->conn, config->device_id,
253 // XKB_KEYMAP_COMPILE_NO_FLAGS);
254 // }
255
256 static int
257 test_keymap_roundtrip(struct xkb_context *ctx,
258 const char* display, xcb_connection_t *conn,
259 int32_t device_id, bool print_keymap, bool tweak,
260 enum xkb_keymap_serialize_flags serialize_flags,
261 const char *keymap_path)
262 {
263 /* Get raw keymap */
264 FILE *file = NULL;
265 if (isempty(keymap_path) || strcmp(keymap_path, "-") == 0) {
266 /* Read stdin */
267 file = tools_read_stdin();
268 if (!file)
269 return TEST_SETUP_FAILURE;
270 } else {
271 /* Read file from path */
272 file = fopen(keymap_path, "rb");
273 if (!file) {
274 perror("Unable to read file");
275 return TEST_SETUP_FAILURE;
276 }
277 }
278 char *original = read_file(keymap_path, file);
279 fclose(file);
280 if (!original)
281 return TEST_SETUP_FAILURE;
282
283 /* Pre-process keymap string */
284 char* expected = prepare_keymap(original, tweak);
285 free(original);
286 if (!expected) {
287 return TEST_SETUP_FAILURE;
288 }
289
290 /* Prepare X server */
291 int ret = reset_keymap(display);
292 if (ret != EXIT_SUCCESS) {
293 #ifdef __APPLE__
294 /* Brew may not provide setxkbmap */
295 #else
296 goto xkbcomp_error;
297 #endif
298 }
299
300 /* Load keymap into X server */
301 ret = run_xkbcomp_str(display, NULL, expected);
302 if (ret != EXIT_SUCCESS)
303 goto xkbcomp_error;
304
305 /* Get keymap from X server */
306 struct xkb_keymap *keymap =
307 xkb_x11_keymap_new_from_device(ctx, conn, device_id,
308 XKB_KEYMAP_COMPILE_NO_FLAGS);
309 assert(keymap);
310 if (!keymap) {
311 ret = EXIT_FAILURE;
312 fprintf(stderr, "ERROR: Failed to get keymap from X server.\n");
313 goto xkbcomp_error;
314 }
315
316 /* Dump keymap and compare to expected */
317 char *got = xkb_keymap_get_as_string2(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT,
318 serialize_flags);
319 if (!got) {
320 ret = EXIT_FAILURE;
321 fprintf(stderr, "ERROR: Failed to dump keymap.\n");
322 } else {
323 if (print_keymap)
324 fprintf(stdout, "%s\n", got);
325 if (!streq(got, expected)) {
326 ret = EXIT_FAILURE;
327 fprintf(stderr,
328 "ERROR: roundtrip failed. "
329 "Lengths difference: got %zu, expected %zu.\n",
330 strlen(got), strlen(expected));
331 } else {
332 ret = EXIT_SUCCESS;
333 fprintf(stderr, "Roundtrip succeed.\n");
334 }
335 free(got);
336 }
337
338 xkb_keymap_unref(keymap);
339 xkbcomp_error:
340 free(expected);
341 return ret;
342 }
343
344 static int
345 init_x11_connection(const char *display, xcb_connection_t **conn_rtrn,
346 int32_t *device_id_rtrn)
347 {
348 xcb_connection_t *conn = xcb_connect(display, NULL);
349 if (xcb_connection_has_error(conn)) {
350 return TEST_SETUP_FAILURE;
351 }
352
353 int ret = xkb_x11_setup_xkb_extension(conn,
354 XKB_X11_MIN_MAJOR_XKB_VERSION,
355 XKB_X11_MIN_MINOR_XKB_VERSION,
356 XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
357 NULL, NULL, NULL, NULL);
358 if (!ret)
359 goto err_xcb;
360
361 int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
362 if (device_id == -1)
363 goto err_xcb;
364
365 *conn_rtrn = conn;
366 *device_id_rtrn = device_id;
367 return EXIT_SUCCESS;
368
369 err_xcb:
370 xcb_disconnect(conn);
371 return TEST_SETUP_FAILURE;
372 }
373
374 X11_TEST(test_basic)
375 {
376 if (update_test_files != NO_UPDATE)
377 return EXIT_SUCCESS;
378
379 xcb_connection_t *conn = NULL;
380 int32_t device_id = 0;
381 int ret = init_x11_connection(display, &conn, &device_id);
382 if (ret != EXIT_SUCCESS) {
383 ret = TEST_SETUP_FAILURE;
384 goto err_xcb;
385 }
386
387 struct xkb_context *ctx = test_get_context(CONTEXT_NO_FLAG);
388 assert(ctx);
389
390 static const struct {
391 const char *path;
392 enum xkb_keymap_serialize_flags serialize_flags;
393 } keymaps[] = {
394 {
395 .path = "keymaps/host-no-pretty.xkb",
396 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
397 & ~XKB_KEYMAP_SERIALIZE_PRETTY
398 },
399 /* This last keymap will be used for the next tests */
400 {
401 .path = "keymaps/host.xkb",
402 .serialize_flags = TEST_KEYMAP_SERIALIZE_FLAGS
403 },
404 };
405 for (size_t k = 0; k < ARRAY_SIZE(keymaps); k++) {
406 fprintf(stderr, "------\n*** %s: #%zu ***\n", __func__, k);
407 char *keymap_path = test_get_path(keymaps[k].path);
408 assert(keymap_path);
409
410 ret = test_keymap_roundtrip(ctx, display, conn, device_id,
411 false, false, keymaps[k].serialize_flags,
412 keymap_path);
413 assert(ret == EXIT_SUCCESS);
414
415 free(keymap_path);
416 }
417
418 struct xkb_keymap *keymap =
419 xkb_x11_keymap_new_from_device(ctx, conn, device_id,
420 XKB_KEYMAP_COMPILE_NO_FLAGS);
421
422 /* Check capitalization transformation */
423 struct xkb_state *state =
424 xkb_x11_state_new_from_device(keymap, conn, device_id);
425 assert(state);
426 xkb_keysym_t sym;
427 sym = xkb_state_key_get_one_sym(state, KEY_A + EVDEV_OFFSET);
428 assert(sym == XKB_KEY_a);
429 sym = xkb_state_key_get_one_sym(state, KEY_LEFT + EVDEV_OFFSET);
430 assert(sym == XKB_KEY_Left);
431 const xkb_mod_index_t caps_idx =
432 xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
433 assert(caps_idx != XKB_MOD_INVALID);
434 const xkb_mod_mask_t caps = UINT32_C(1) << caps_idx;
435 xkb_state_update_mask(state, 0, 0, caps, 0, 0, 0);
436 sym = xkb_state_key_get_one_sym(state, KEY_A + EVDEV_OFFSET);
437 assert(sym == XKB_KEY_A);
438 sym = xkb_state_key_get_one_sym(state, KEY_LEFT + EVDEV_OFFSET);
439 assert(sym == XKB_KEY_Left);
440 xkb_state_unref(state);
441
442 xkb_keymap_unref(keymap);
443 xkb_context_unref(ctx);
444
445 ret = EXIT_SUCCESS;
446
447 err_xcb:
448 if (conn != NULL)
449 xcb_disconnect(conn);
450 return ret;
451 }
452
453 struct xkbcomp_roundtrip_data {
454 const char *path;
455 bool tweak_comments;
456 enum xkb_keymap_serialize_flags serialize_flags;
457 };
458
459 static int
460 xkbcomp_roundtrip(const char *display, void *private) {
461 const struct xkbcomp_roundtrip_data *priv = private;
462
463 xcb_connection_t *conn = NULL;
464 int32_t device_id = 0;
465 int ret = init_x11_connection(display, &conn, &device_id);
466 if (ret != EXIT_SUCCESS) {
467 ret = TEST_SETUP_FAILURE;
468 goto error_xcb;
469 }
470
471 struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
472 if (!ctx) {
473 ret = EXIT_FAILURE;
474 goto error_context;
475 }
476
477 ret = test_keymap_roundtrip(ctx, display, conn, device_id, true,
478 priv->tweak_comments, priv->serialize_flags,
479 priv->path);
480
481 xkb_context_unref(ctx);
482 error_context:
483 error_xcb:
484 xcb_disconnect(conn);
485 return ret;
486 }
487
488 static void
489 usage(FILE *fp, char *progname)
490 {
491 fprintf(fp,
492 "Usage: %s [--update] [--update-obtained] "
493 "[--keymap KEYMAP_FILE] [--tweak] [--no-pretty] [--help]\n",
494 progname);
495 }
496
497 int
498 main(int argc, char **argv) {
499 test_init();
500
501 enum options {
502 OPT_UPDATE_GOLDEN_TEST_WITH_OUTPUT,
503 OPT_UPDATE_GOLDEN_TEST_WITH_INPUT,
504 OPT_FILE,
505 OPT_TWEAK_COMMENTS,
506 OPT_NO_PRETTY,
507 };
508 static struct option opts[] = {
509 {"help", no_argument, 0, 'h'},
510 {"update-obtained", no_argument, 0, OPT_UPDATE_GOLDEN_TEST_WITH_OUTPUT},
511 {"update", no_argument, 0, OPT_UPDATE_GOLDEN_TEST_WITH_INPUT},
512 {"keymap", required_argument, 0, OPT_FILE},
513 {"tweak", no_argument, 0, OPT_TWEAK_COMMENTS},
514 {"no-pretty", no_argument, 0, OPT_NO_PRETTY},
515 {0, 0, 0, 0},
516 };
517
518 bool tweak_comments = false;
519 const char *path = NULL;
520 enum xkb_keymap_serialize_flags serialize_flags =
521 (enum xkb_keymap_serialize_flags) TEST_KEYMAP_SERIALIZE_FLAGS;
522
523 while (1) {
524 int opt;
525 int option_index = 0;
526
527 opt = getopt_long(argc, argv, "h", opts, &option_index);
528 if (opt == -1)
529 break;
530
531 switch (opt) {
532 case OPT_UPDATE_GOLDEN_TEST_WITH_OUTPUT:
533 update_test_files = UPDATE_USING_TEST_OUTPUT;
534 break;
535 case OPT_UPDATE_GOLDEN_TEST_WITH_INPUT:
536 update_test_files = UPDATE_USING_TEST_INPUT;
537 break;
538 case OPT_FILE:
539 path = optarg;
540 break;
541 case OPT_TWEAK_COMMENTS:
542 tweak_comments = optarg;
543 break;
544 case OPT_NO_PRETTY:
545 serialize_flags &= ~XKB_KEYMAP_SERIALIZE_PRETTY;
546 break;
547 case 'h':
548 usage(stdout, argv[0]);
549 return EXIT_SUCCESS;
550 default:
551 usage(stderr, argv[0]);
552 return EXIT_INVALID_USAGE;
553 }
554 }
555
556 if (update_test_files != NO_UPDATE && path != NULL) {
557 fprintf(stderr,
558 "ERROR: --update* and --keymap are mutually exclusive.\n");
559 usage(stderr, argv[0]);
560 return EXIT_INVALID_USAGE;
561 }
562
563 if (path == NULL) {
564 return x11_tests_run();
565 } else {
566 struct xkbcomp_roundtrip_data priv = {
567 .path = path,
568 .tweak_comments = tweak_comments,
569 .serialize_flags = serialize_flags
570 };
571 return xvfb_wrapper(xkbcomp_roundtrip, &priv);
572 }
573 }