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