test: simplify error handling in interactive-evdev Passing -errno around and having separate labels depending on failure types is superfluous here. All the unref calls can handle NULL and nothing cares about errno once we're out of the immediate scope. So let's simplify this and deal with 0 and 1 only. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
diff --git a/test/interactive-evdev.c b/test/interactive-evdev.c
index d10c82e..7c96272 100644
--- a/test/interactive-evdev.c
+++ b/test/interactive-evdev.c
@@ -297,7 +297,7 @@ read_keyboard(struct keyboard *kbd)
if (len < 0 && errno != EWOULDBLOCK) {
fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
- return -errno;
+ return 1;
}
return 0;
@@ -306,8 +306,8 @@ read_keyboard(struct keyboard *kbd)
static int
loop(struct keyboard *kbds)
{
- int i, ret;
- int epfd;
+ int i, ret = 1;
+ int epfd = -1;
struct keyboard *kbd;
struct epoll_event ev;
struct epoll_event evs[16];
@@ -316,7 +316,7 @@ loop(struct keyboard *kbds)
if (epfd < 0) {
fprintf(stderr, "Couldn't create epoll instance: %s\n",
strerror(errno));
- return -errno;
+ goto out;
}
for (kbd = kbds; kbd; kbd = kbd->next) {
@@ -325,10 +325,9 @@ loop(struct keyboard *kbds)
ev.data.ptr = kbd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
if (ret) {
- ret = -errno;
fprintf(stderr, "Couldn't add %s to epoll: %s\n",
kbd->path, strerror(errno));
- goto err_epoll;
+ goto out;
}
}
@@ -337,25 +336,22 @@ loop(struct keyboard *kbds)
if (ret < 0) {
if (errno == EINTR)
continue;
- ret = -errno;
fprintf(stderr, "Couldn't poll for events: %s\n",
strerror(errno));
- goto err_epoll;
+ goto out;
}
for (i = 0; i < ret; i++) {
kbd = evs[i].data.ptr;
ret = read_keyboard(kbd);
if (ret) {
- goto err_epoll;
+ goto out;
}
}
}
- close(epfd);
- return 0;
-
-err_epoll:
+ ret = 0;
+out:
close(epfd);
return ret;
}
@@ -369,11 +365,11 @@ sigintr_handler(int signum)
int
main(int argc, char *argv[])
{
- int ret;
+ int ret = EXIT_FAILURE;
int opt;
struct keyboard *kbds;
- struct xkb_context *ctx;
- struct xkb_keymap *keymap;
+ struct xkb_context *ctx = NULL;
+ struct xkb_keymap *keymap = NULL;
struct xkb_compose_table *compose_table = NULL;
const char *rules = NULL;
const char *model = NULL;
@@ -439,18 +435,16 @@ main(int argc, char *argv[])
ctx = test_get_context(0);
if (!ctx) {
- ret = -1;
fprintf(stderr, "Couldn't create xkb context\n");
- goto err_out;
+ goto out;
}
if (keymap_path) {
FILE *file = fopen(keymap_path, "rb");
if (!file) {
- ret = EXIT_FAILURE;
fprintf(stderr, "Couldn't open '%s': %s\n",
keymap_path, strerror(errno));
- goto err_ctx;
+ goto out;
}
keymap = xkb_keymap_new_from_file(ctx, file,
XKB_KEYMAP_FORMAT_TEXT_V1,
@@ -463,9 +457,8 @@ main(int argc, char *argv[])
}
if (!keymap) {
- ret = -1;
fprintf(stderr, "Couldn't create xkb keymap\n");
- goto err_ctx;
+ goto out;
}
if (with_compose) {
@@ -474,16 +467,14 @@ main(int argc, char *argv[])
xkb_compose_table_new_from_locale(ctx, locale,
XKB_COMPOSE_COMPILE_NO_FLAGS);
if (!compose_table) {
- ret = -1;
fprintf(stderr, "Couldn't create compose from locale\n");
- goto err_xkb;
+ goto out;
}
}
kbds = get_keyboards(keymap, compose_table);
if (!kbds) {
- ret = -1;
- goto err_compose;
+ goto out;
}
act.sa_handler = sigintr_handler;
@@ -497,12 +488,10 @@ main(int argc, char *argv[])
test_enable_stdin_echo();
free_keyboards(kbds);
-err_compose:
+out:
xkb_compose_table_unref(compose_table);
-err_xkb:
xkb_keymap_unref(keymap);
-err_ctx:
xkb_context_unref(ctx);
-err_out:
- exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+
+ return ret;
}
diff --git a/tools/tools-common.c b/tools/tools-common.c
new file mode 100644
index 0000000..aecb2ab
--- /dev/null
+++ b/tools/tools-common.c
@@ -0,0 +1,89 @@
+/*
+ * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
+ * Copyright © 2012 Intel Corporation
+ * Copyright © 2012 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 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 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.
+ *
+ * Except as contained in this notice, the names of the authors or their
+ * institutions shall not be used in advertising or otherwise to promote the
+ * sale, use or other dealings in this Software without prior written
+ * authorization from the authors.
+ *
+ * Author: Dan Nicholson <dbn.lists@gmail.com>
+ * Daniel Stone <daniel@fooishbar.org>
+ * Ran Benita <ran234@gmail.com>
+ */
+
+#include "config.h"
+
+#include <limits.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef _MSC_VER
+#include <io.h>
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <termios.h>
+#endif
+
+#include "tools-common.h"
+
+#ifdef _MSC_VER
+void
+test_disable_stdin_echo(void)
+{
+ HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
+ DWORD mode = 0;
+ GetConsoleMode(stdin_handle, &mode);
+ SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
+}
+
+void
+test_enable_stdin_echo(void)
+{
+ HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
+ DWORD mode = 0;
+ GetConsoleMode(stdin_handle, &mode);
+ SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
+}
+#else
+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);
+ }
+}
+#endif
diff --git a/tools/tools-common.h b/tools/tools-common.h
new file mode 100644
index 0000000..035c392
--- /dev/null
+++ b/tools/tools-common.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * 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.
+ *
+ * Author: Daniel Stone <daniel@fooishbar.org>
+ */
+
+#include <assert.h>
+
+/* Don't use compat names in internal code. */
+#define _XKBCOMMON_COMPAT_H
+#include "xkbcommon/xkbcommon.h"
+#include "xkbcommon/xkbcommon-compose.h"
+#include "utils.h"
+
+/* The offset between KEY_* numbering, and keycodes in the XKB evdev
+ * dataset. */
+#define EVDEV_OFFSET 8
+
+enum key_seq_state {
+ DOWN,
+ REPEAT,
+ UP,
+ BOTH,
+ NEXT,
+ FINISH,
+};
+
+int
+test_key_seq(struct xkb_keymap *keymap, ...);
+
+int
+test_key_seq_va(struct xkb_keymap *keymap, va_list args);
+
+char *
+test_get_path(const char *path_rel);
+
+char *
+test_read_file(const char *path_rel);
+
+enum test_context_flags {
+ CONTEXT_NO_FLAG = 0,
+ CONTEXT_ALLOW_ENVIRONMENT_NAMES = (1 << 0),
+};
+
+struct xkb_context *
+test_get_context(enum test_context_flags flags);
+
+struct xkb_keymap *
+test_compile_file(struct xkb_context *context, const char *path_rel);
+
+struct xkb_keymap *
+test_compile_string(struct xkb_context *context, const char *string);
+
+struct xkb_keymap *
+test_compile_buffer(struct xkb_context *context, const char *buf, size_t len);
+
+struct xkb_keymap *
+test_compile_rules(struct xkb_context *context, const char *rules,
+ const char *model, const char *layout, const char *variant,
+ const char *options);
+
+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);
+
+void
+test_print_state_changes(enum xkb_state_component changed);
+
+void
+test_disable_stdin_echo(void);
+
+void
+test_enable_stdin_echo(void);
+
+#ifdef _MSC_VER
+#define setenv(varname, value, overwrite) _putenv_s((varname), (value))
+#define unsetenv(varname) _putenv_s(varname, "")
+#endif