2014-08-09 13:14:34 -06:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Ran Benita <ran234@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-27 04:03:20 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-08-09 13:14:34 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
#include "xkbcommon/xkbcommon-x11.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
struct xkb_context *ctx = test_get_context(0);
|
|
|
|
struct xkb_keymap *keymap;
|
|
|
|
xcb_connection_t *conn;
|
|
|
|
int32_t device_id;
|
|
|
|
int ret, status;
|
2015-08-23 22:44:20 -06:00
|
|
|
char display[512];
|
2015-08-23 13:22:11 -06:00
|
|
|
char *xkb_path;
|
2014-08-09 13:14:34 -06:00
|
|
|
char *original, *dump;
|
|
|
|
char *envp[] = { NULL };
|
2017-07-29 14:31:19 -06:00
|
|
|
char *xvfb_argv[] = {
|
|
|
|
(char *) "Xvfb", display, NULL
|
|
|
|
};
|
2017-04-11 11:19:15 -06:00
|
|
|
pid_t xvfb_pid = 0;
|
2017-07-29 14:31:19 -06:00
|
|
|
char *xkbcomp_argv[] = {
|
|
|
|
(char *) "xkbcomp", (char *) "-I", NULL /* xkb_path */, display, NULL
|
|
|
|
};
|
2014-08-09 13:14:34 -06:00
|
|
|
pid_t xkbcomp_pid;
|
|
|
|
|
2016-03-13 14:56:48 -06:00
|
|
|
char *xhost = NULL;
|
2015-08-23 22:44:20 -06:00
|
|
|
int xdpy_current;
|
|
|
|
int xdpy_candidate;
|
|
|
|
|
2014-08-09 13:14:34 -06:00
|
|
|
/*
|
|
|
|
* What all of this mess does is:
|
2015-08-23 22:44:20 -06:00
|
|
|
* 1. Launch Xvfb on available DISPLAY.
|
2014-08-09 13:14:34 -06:00
|
|
|
* 2. Make an xcb connection to this display.
|
|
|
|
* 3. Launch xkbcomp to change the keymap of the new display (doing
|
|
|
|
* this programmatically is major work [which we may yet do some
|
|
|
|
* day for xkbcommon-x11] so we use xkbcomp for now).
|
|
|
|
* 4. Download the keymap back from the display using xkbcommon-x11.
|
|
|
|
* 5. Compare received keymap to the uploaded keymap.
|
|
|
|
* 6. Kill the server & clean up.
|
|
|
|
*/
|
|
|
|
|
2015-08-23 22:44:20 -06:00
|
|
|
ret = xcb_parse_display(NULL, &xhost, &xdpy_current, NULL);
|
|
|
|
assert(ret != 0);
|
|
|
|
/*
|
|
|
|
* IANA assigns TCP port numbers from 6000 through 6063 to X11
|
|
|
|
* clients. In addition, the current XCB implementaion shows
|
|
|
|
* that, when an X11 client tries to establish a TCP connetion,
|
|
|
|
* the port number needed is specified by adding 6000 to a given
|
|
|
|
* display number. So, one of reasonable ranges of xdpy_candidate
|
|
|
|
* is [0, 63].
|
|
|
|
*/
|
|
|
|
for (xdpy_candidate = 63; xdpy_candidate >= 0; xdpy_candidate--) {
|
|
|
|
if (xdpy_candidate == xdpy_current) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
snprintf(display, sizeof(display), "%s:%d", xhost, xdpy_candidate);
|
|
|
|
ret = posix_spawnp(&xvfb_pid, "Xvfb", NULL, NULL, xvfb_argv, envp);
|
|
|
|
if (ret == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 14:56:48 -06:00
|
|
|
free(xhost);
|
2014-08-09 13:14:34 -06:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = SKIP_TEST;
|
|
|
|
goto err_ctx;
|
|
|
|
}
|
|
|
|
|
2015-08-23 22:44:20 -06:00
|
|
|
/* Wait for Xvfb fully waking up to accept a connection from a client. */
|
|
|
|
sleep(1);
|
2014-08-09 13:14:34 -06:00
|
|
|
|
|
|
|
conn = xcb_connect(display, NULL);
|
2014-08-18 10:47:10 -06:00
|
|
|
if (xcb_connection_has_error(conn)) {
|
2014-08-09 13:14:34 -06:00
|
|
|
ret = SKIP_TEST;
|
|
|
|
goto err_xvfd;
|
|
|
|
}
|
|
|
|
ret = xkb_x11_setup_xkb_extension(conn,
|
|
|
|
XKB_X11_MIN_MAJOR_XKB_VERSION,
|
|
|
|
XKB_X11_MIN_MINOR_XKB_VERSION,
|
|
|
|
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
if (!ret) {
|
|
|
|
ret = SKIP_TEST;
|
|
|
|
goto err_xcb;
|
|
|
|
}
|
|
|
|
device_id = xkb_x11_get_core_keyboard_device_id(conn);
|
|
|
|
assert(device_id != -1);
|
|
|
|
|
|
|
|
xkb_path = test_get_path("keymaps/host.xkb");
|
|
|
|
assert(ret >= 0);
|
2015-08-23 13:22:11 -06:00
|
|
|
xkbcomp_argv[2] = xkb_path;
|
2014-08-09 13:14:34 -06:00
|
|
|
ret = posix_spawnp(&xkbcomp_pid, "xkbcomp", NULL, NULL, xkbcomp_argv, envp);
|
|
|
|
free(xkb_path);
|
|
|
|
if (ret != 0) {
|
|
|
|
ret = SKIP_TEST;
|
|
|
|
goto err_xcb;
|
|
|
|
}
|
|
|
|
ret = waitpid(xkbcomp_pid, &status, 0);
|
|
|
|
if (ret < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
|
|
|
ret = SKIP_TEST;
|
|
|
|
goto err_xcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
|
|
|
|
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
|
|
assert(keymap);
|
|
|
|
|
|
|
|
original = test_read_file("keymaps/host.xkb");
|
|
|
|
assert(original);
|
|
|
|
|
|
|
|
dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
|
|
|
|
assert(dump);
|
|
|
|
|
2015-08-23 13:22:11 -06:00
|
|
|
if (!streq(original, dump)) {
|
2014-08-09 13:14:34 -06:00
|
|
|
fprintf(stderr,
|
|
|
|
"round-trip test failed: dumped map differs from original\n");
|
|
|
|
fprintf(stderr, "length: dumped %lu, original %lu\n",
|
|
|
|
(unsigned long) strlen(dump),
|
|
|
|
(unsigned long) strlen(original));
|
|
|
|
fprintf(stderr, "dumped map:\n");
|
|
|
|
fprintf(stderr, "%s\n", dump);
|
|
|
|
ret = 1;
|
|
|
|
goto err_dump;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
err_dump:
|
|
|
|
free(original);
|
|
|
|
free(dump);
|
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
err_xcb:
|
|
|
|
xcb_disconnect(conn);
|
|
|
|
err_xvfd:
|
2017-04-11 11:19:15 -06:00
|
|
|
if (xvfb_pid > 0)
|
|
|
|
kill(xvfb_pid, SIGTERM);
|
2014-08-09 13:14:34 -06:00
|
|
|
err_ctx:
|
|
|
|
xkb_context_unref(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|