test/x11-keyseq: new test
[platform/upstream/libxkbcommon.git] / test / x11comp.c
1 /*
2  * Copyright © 2014 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include <stdio.h>
25 #include <spawn.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <linux/input.h>
32
33 #include "test.h"
34 #include "xkbcommon/xkbcommon-x11.h"
35
36 int
37 main(void)
38 {
39     struct xkb_context *ctx = test_get_context(0);
40     struct xkb_keymap *keymap;
41     xcb_connection_t *conn;
42     int32_t device_id;
43     int pipefds[2];
44     int ret, status;
45     char displayfd[128], display[128];
46     char *search_path, *search_path_arg, *xkb_path;
47     char *original, *dump;
48     char *envp[] = { NULL };
49     char *xvfb_argv[] = { "Xvfb", "-displayfd", displayfd, NULL };
50     pid_t xvfb_pid;
51     char *xkbcomp_argv[] = { "xkbcomp", "-I", NULL /* search_path_arg */,
52                              NULL /* xkb_path */, display, NULL };
53     pid_t xkbcomp_pid;
54
55     /*
56      * What all of this mess does is:
57      * 1. Launch Xvfb on the next available DISPLAY. Xvfb reports the
58      *    display number to an fd passed with -displayfd once it's
59      *    initialized. We pass a pipe there to read it.
60      * 2. Make an xcb connection to this display.
61      * 3. Launch xkbcomp to change the keymap of the new display (doing
62      *    this programmatically is major work [which we may yet do some
63      *    day for xkbcommon-x11] so we use xkbcomp for now).
64      * 4. Download the keymap back from the display using xkbcommon-x11.
65      * 5. Compare received keymap to the uploaded keymap.
66      * 6. Kill the server & clean up.
67      */
68
69     ret = pipe(pipefds);
70     assert(ret == 0);
71
72     ret = snprintf(displayfd, sizeof(displayfd), "%d", pipefds[1]);
73     assert(ret >= 0 && ret < sizeof(displayfd));
74
75     ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
76     if (ret != 0) {
77         ret = SKIP_TEST;
78         goto err_ctx;
79     }
80
81     display[0] = ':';
82     ret = read(pipefds[0], display + 1, sizeof(display) - 1);
83     assert(ret > 0 && 1 + ret < sizeof(display) - 1);
84     display[1 + ret] = '\0';
85     close(pipefds[0]);
86     close(pipefds[1]);
87
88     conn = xcb_connect(display, NULL);
89     if (!conn || xcb_connection_has_error(conn)) {
90         ret = SKIP_TEST;
91         goto err_xvfd;
92     }
93     ret = xkb_x11_setup_xkb_extension(conn,
94                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
95                                       XKB_X11_MIN_MINOR_XKB_VERSION,
96                                       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
97                                       NULL, NULL, NULL, NULL);
98     if (!ret) {
99         ret = SKIP_TEST;
100         goto err_xcb;
101     }
102     device_id = xkb_x11_get_core_keyboard_device_id(conn);
103     assert(device_id != -1);
104
105     xkb_path = test_get_path("keymaps/host.xkb");
106     search_path = test_get_path("");
107     assert(search_path);
108     ret = asprintf(&search_path_arg, "-I%s", search_path);
109     assert(ret >= 0);
110     xkbcomp_argv[2] = search_path_arg;
111     xkbcomp_argv[3] = xkb_path;
112     ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL, xkbcomp_argv, envp);
113     free(search_path_arg);
114     free(search_path);
115     free(xkb_path);
116     if (ret != 0) {
117         ret = SKIP_TEST;
118         goto err_xcb;
119     }
120     ret = waitpid(xkbcomp_pid, &status, 0);
121     if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
122         ret = SKIP_TEST;
123         goto err_xcb;
124     }
125
126     keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
127                                             XKB_KEYMAP_COMPILE_NO_FLAGS);
128     assert(keymap);
129
130     original = test_read_file("keymaps/host.xkb");
131     assert(original);
132
133     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
134     assert(dump);
135
136     ret = streq(original, dump);
137     if (!ret) {
138         fprintf(stderr,
139                 "round-trip test failed: dumped map differs from original\n");
140         fprintf(stderr, "length: dumped %lu, original %lu\n",
141                 (unsigned long) strlen(dump),
142                 (unsigned long) strlen(original));
143         fprintf(stderr, "dumped map:\n");
144         fprintf(stderr, "%s\n", dump);
145         ret = 1;
146         goto err_dump;
147     }
148
149     ret = 0;
150 err_dump:
151     free(original);
152     free(dump);
153     xkb_keymap_unref(keymap);
154 err_xcb:
155     xcb_disconnect(conn);
156 err_xvfd:
157     kill(xvfb_pid, SIGTERM);
158 err_ctx:
159     xkb_context_unref(ctx);
160     return ret;
161 }