#include <limits.h>
#include <fcntl.h>
-#ifdef _MSC_VER
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef _WIN32
#include <io.h>
+#include <windows.h>
#else
#include <unistd.h>
-#endif
-#include <sys/types.h>
-#include <sys/stat.h>
#include <termios.h>
+#endif
#include "test.h"
#include "utils.h"
}
char *
+test_makedir(const char *parent, const char *path)
+{
+ char *dirname;
+ int err;
+
+ dirname = asprintf_safe("%s/%s", parent, path);
+ assert(dirname);
+#ifdef _WIN32
+ err = _mkdir(dirname);
+#else
+ err = mkdir(dirname, 0777);
+#endif
+ assert(err == 0);
+
+ return dirname;
+}
+
+char *
+test_maketempdir(const char *template)
+{
+#ifdef _WIN32
+ const char *basetmp = getenv("TMP");
+ if (basetmp == NULL) {
+ basetmp = getenv("TEMP");
+ }
+ if (basetmp == NULL) {
+ basetmp = getenv("top_builddir");
+ }
+ assert(basetmp != NULL);
+ char *tmpdir = asprintf_safe("%s/%s", basetmp, template);
+ assert(tmpdir != NULL);
+ char *tmp = _mktemp(tmpdir);
+ assert(tmp == tmpdir);
+ int ret = _mkdir(tmp);
+ assert(ret == 0);
+ return tmpdir;
+#else
+ char *tmpdir = asprintf_safe("/tmp/%s", template);
+ assert(tmpdir != NULL);
+ char *tmp = mkdtemp(tmpdir);
+ assert(tmp == tmpdir);
+ return tmpdir;
+#endif
+}
+
+char *
test_get_path(const char *path_rel)
{
- int ret;
char *path;
const char *srcdir;
if (path_rel[0] == '/')
return strdup(path_rel);
- ret = asprintf(&path, "%s/test/data%s%s", srcdir,
- path_rel[0] ? "/" : "", path_rel);
- if (ret < 0) {
+ path = asprintf_safe("%s/test/data%s%s", srcdir,
+ path_rel[0] ? "/" : "", path_rel);
+ if (!path) {
fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
return NULL;
}
return keymap;
}
-
-void
-test_print_keycode_state(struct xkb_state *state,
- struct xkb_compose_state *compose_state,
- xkb_keycode_t keycode,
- enum xkb_consumed_mode consumed_mode)
-{
- struct xkb_keymap *keymap;
-
- xkb_keysym_t sym;
- const xkb_keysym_t *syms;
- int nsyms;
- char s[16];
- xkb_layout_index_t layout;
- enum xkb_compose_status status;
-
- keymap = xkb_state_get_keymap(state);
-
- nsyms = xkb_state_key_get_syms(state, keycode, &syms);
-
- if (nsyms <= 0)
- return;
-
- status = XKB_COMPOSE_NOTHING;
- if (compose_state)
- status = xkb_compose_state_get_status(compose_state);
-
- if (status == XKB_COMPOSE_COMPOSING || status == XKB_COMPOSE_CANCELLED)
- return;
-
- if (status == XKB_COMPOSE_COMPOSED) {
- sym = xkb_compose_state_get_one_sym(compose_state);
- syms = &sym;
- nsyms = 1;
- }
- else if (nsyms == 1) {
- sym = xkb_state_key_get_one_sym(state, keycode);
- syms = &sym;
- }
-
- printf("keysyms [ ");
- for (int i = 0; i < nsyms; i++) {
- xkb_keysym_get_name(syms[i], s, sizeof(s));
- printf("%-*s ", (int) sizeof(s), s);
- }
- printf("] ");
-
- if (status == XKB_COMPOSE_COMPOSED)
- xkb_compose_state_get_utf8(compose_state, s, sizeof(s));
- else
- xkb_state_key_get_utf8(state, keycode, s, sizeof(s));
- printf("unicode [ %s ] ", s);
-
- layout = xkb_state_key_get_layout(state, keycode);
- printf("layout [ %s (%d) ] ",
- xkb_keymap_layout_get_name(keymap, layout), layout);
-
- printf("level [ %d ] ",
- xkb_state_key_get_level(state, keycode, layout));
-
- printf("mods [ ");
- for (xkb_mod_index_t mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
- if (xkb_state_mod_index_is_active(state, mod,
- XKB_STATE_MODS_EFFECTIVE) <= 0)
- continue;
- if (xkb_state_mod_index_is_consumed2(state, keycode, mod,
- consumed_mode))
- printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
- else
- printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
- }
- printf("] ");
-
- printf("leds [ ");
- for (xkb_led_index_t led = 0; led < xkb_keymap_num_leds(keymap); led++) {
- if (xkb_state_led_index_is_active(state, led) <= 0)
- continue;
- printf("%s ", xkb_keymap_led_get_name(keymap, led));
- }
- printf("] ");
-
- printf("\n");
-}
-
-void
-test_print_state_changes(enum xkb_state_component changed)
-{
- if (changed == 0)
- return;
-
- printf("changed [ ");
- if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
- printf("effective-layout ");
- if (changed & XKB_STATE_LAYOUT_DEPRESSED)
- printf("depressed-layout ");
- if (changed & XKB_STATE_LAYOUT_LATCHED)
- printf("latched-layout ");
- if (changed & XKB_STATE_LAYOUT_LOCKED)
- printf("locked-layout ");
- if (changed & XKB_STATE_MODS_EFFECTIVE)
- printf("effective-mods ");
- if (changed & XKB_STATE_MODS_DEPRESSED)
- printf("depressed-mods ");
- if (changed & XKB_STATE_MODS_LATCHED)
- printf("latched-mods ");
- if (changed & XKB_STATE_MODS_LOCKED)
- printf("locked-mods ");
- if (changed & XKB_STATE_LEDS)
- printf("leds ");
- printf("]\n");
-}
-
-void
-test_disable_stdin_echo(void)
-{
- /* Same as `stty -echo`. */
- struct termios termios;
- if (tcgetattr(STDIN_FILENO, &termios) == 0) {
- termios.c_lflag &= ~ECHO;
- (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
- }
-}
-
-void
-test_enable_stdin_echo(void)
-{
- /* Same as `stty echo`. */
- struct termios termios;
- if (tcgetattr(STDIN_FILENO, &termios) == 0) {
- termios.c_lflag |= ECHO;
- (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
- }
-}