Skip to content

Commit

Permalink
Wrap test with xvfb-run in meson
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed May 10, 2023
1 parent 25b2fe7 commit 4121efd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 56 deletions.
16 changes: 11 additions & 5 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,17 @@ if get_option('enable-x11')
executable('test-x11', 'test/x11.c', dependencies: x11_test_dep),
env: test_env,
)
test(
'x11comp',
executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep),
env: test_env,
)
x11comp = executable('test-x11comp', 'test/x11comp.c', dependencies: x11_test_dep)
xvfb = find_program('xvfb-run', required: false)
if xvfb.found()
test(
'x11comp',
xvfb,
args: ['--auto-servernum', x11comp],
depends: x11comp,
env: test_env,
)
endif
endif
if get_option('enable-xkbregistry')
test(
Expand Down
69 changes: 18 additions & 51 deletions test/x11comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@

#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"
Expand All @@ -45,60 +42,33 @@ main(void)
char display[512];
char *xkb_path;
char *original, *dump;
char *envp[] = { NULL };
char *xvfb_argv[] = {
(char *) "Xvfb", display, NULL
};
pid_t xvfb_pid = 0;
// char *envp[] = { NULL };

char *xkbcomp_argv[] = {
(char *) "xkbcomp", (char *) "-I", NULL /* xkb_path */, display, NULL
};
pid_t xkbcomp_pid;

// [FIXME] debugging
const char* s = getenv("XAUTHORITY");
fprintf(stderr, "XAUTHORITY: %s\n", (s != NULL) ? s : "getenv returned NULL");
char xauthority[512];
snprintf(xauthority, sizeof(xauthority), "XAUTHORITY=%s", s);

s = getenv("DISPLAY");
fprintf(stderr, "DISPLAY: %s\n", (s != NULL) ? s : "getenv returned NULL");
char *envp[] = { xauthority, NULL };

char *xhost = NULL;
int xdpy_current;
int xdpy_candidate;

/*
* What all of this mess does is:
* 1. Launch Xvfb on available DISPLAY.
* 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.
*/

ret = xcb_parse_display(NULL, &xhost, &xdpy_current, NULL);
int xdisplay;

/* Get display from environment ($DISPLAY) */
ret = xcb_parse_display(NULL, &xhost, &xdisplay, 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;
}
}
snprintf(display, sizeof(display), "%s:%d", xhost, xdisplay);
free(xhost);
if (ret != 0) {
ret = SKIP_TEST;
goto err_ctx;
}

/* Wait for Xvfb fully waking up to accept a connection from a client. */
sleep(1);
fprintf(stderr, "parsed DISPLAY: “%s”\n", display);

conn = xcb_connect(display, NULL);
if (xcb_connection_has_error(conn)) {
Expand Down Expand Up @@ -162,9 +132,6 @@ main(void)
err_xcb:
xcb_disconnect(conn);
err_xvfd:
if (xvfb_pid > 0)
kill(xvfb_pid, SIGTERM);
err_ctx:
xkb_context_unref(ctx);
return ret;
}

0 comments on commit 4121efd

Please sign in to comment.