test/x11comp: fix compiler warnings
[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
32 #include "test.h"
33 #include "xkbcommon/xkbcommon-x11.h"
34
35 int
36 main(void)
37 {
38     struct xkb_context *ctx = test_get_context(0);
39     struct xkb_keymap *keymap;
40     xcb_connection_t *conn;
41     int32_t device_id;
42     int ret, status;
43     char display[512];
44     char *xkb_path;
45     char *original, *dump;
46     char *envp[] = { NULL };
47     char *xvfb_argv[] = {
48         (char *) "Xvfb", display, NULL
49     };
50     pid_t xvfb_pid = 0;
51     char *xkbcomp_argv[] = {
52         (char *) "xkbcomp", (char *) "-I", NULL /* xkb_path */, display, NULL
53     };
54     pid_t xkbcomp_pid;
55
56     char *xhost = NULL;
57     int xdpy_current;
58     int xdpy_candidate;
59
60     /*
61      * What all of this mess does is:
62      * 1. Launch Xvfb on available DISPLAY.
63      * 2. Make an xcb connection to this display.
64      * 3. Launch xkbcomp to change the keymap of the new display (doing
65      *    this programmatically is major work [which we may yet do some
66      *    day for xkbcommon-x11] so we use xkbcomp for now).
67      * 4. Download the keymap back from the display using xkbcommon-x11.
68      * 5. Compare received keymap to the uploaded keymap.
69      * 6. Kill the server & clean up.
70      */
71
72     ret = xcb_parse_display(NULL, &xhost, &xdpy_current, NULL);
73     assert(ret != 0);
74     /*
75      * IANA assigns TCP port numbers from 6000 through 6063 to X11
76      * clients.  In addition, the current XCB implementaion shows
77      * that, when an X11 client tries to establish a TCP connetion,
78      * the port number needed is specified by adding 6000 to a given
79      * display number.  So, one of reasonable ranges of xdpy_candidate
80      * is [0, 63].
81      */
82     for (xdpy_candidate = 63; xdpy_candidate >= 0; xdpy_candidate--) {
83         if (xdpy_candidate == xdpy_current) {
84             continue;
85         }
86         snprintf(display, sizeof(display), "%s:%d", xhost, xdpy_candidate);
87         ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
88         if (ret == 0) {
89             break;
90         }
91     }
92     free(xhost);
93     if (ret != 0) {
94         ret = SKIP_TEST;
95         goto err_ctx;
96     }
97
98     /* Wait for Xvfb fully waking up to accept a connection from a client. */
99     sleep(1);
100
101     conn = xcb_connect(display, NULL);
102     if (xcb_connection_has_error(conn)) {
103         ret = SKIP_TEST;
104         goto err_xvfd;
105     }
106     ret = xkb_x11_setup_xkb_extension(conn,
107                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
108                                       XKB_X11_MIN_MINOR_XKB_VERSION,
109                                       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
110                                       NULL, NULL, NULL, NULL);
111     if (!ret) {
112         ret = SKIP_TEST;
113         goto err_xcb;
114     }
115     device_id = xkb_x11_get_core_keyboard_device_id(conn);
116     assert(device_id != -1);
117
118     xkb_path = test_get_path("keymaps/host.xkb");
119     assert(ret >= 0);
120     xkbcomp_argv[2] = xkb_path;
121     ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL, xkbcomp_argv, envp);
122     free(xkb_path);
123     if (ret != 0) {
124         ret = SKIP_TEST;
125         goto err_xcb;
126     }
127     ret = waitpid(xkbcomp_pid, &status, 0);
128     if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
129         ret = SKIP_TEST;
130         goto err_xcb;
131     }
132
133     keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
134                                             XKB_KEYMAP_COMPILE_NO_FLAGS);
135     assert(keymap);
136
137     original = test_read_file("keymaps/host.xkb");
138     assert(original);
139
140     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
141     assert(dump);
142
143     if (!streq(original, dump)) {
144         fprintf(stderr,
145                 "round-trip test failed: dumped map differs from original\n");
146         fprintf(stderr, "length: dumped %lu, original %lu\n",
147                 (unsigned long) strlen(dump),
148                 (unsigned long) strlen(original));
149         fprintf(stderr, "dumped map:\n");
150         fprintf(stderr, "%s\n", dump);
151         ret = 1;
152         goto err_dump;
153     }
154
155     ret = 0;
156 err_dump:
157     free(original);
158     free(dump);
159     xkb_keymap_unref(keymap);
160 err_xcb:
161     xcb_disconnect(conn);
162 err_xvfd:
163     if (xvfb_pid > 0)
164         kill(xvfb_pid, SIGTERM);
165 err_ctx:
166     xkb_context_unref(ctx);
167     return ret;
168 }