test: simplify error handling in interactive-evdev
[platform/upstream/libxkbcommon.git] / tools / tools-common.c
1 /*
2  * Copyright © 2009 Dan Nicholson <dbn.lists@gmail.com>
3  * Copyright © 2012 Intel Corporation
4  * Copyright © 2012 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Except as contained in this notice, the names of the authors or their
24  * institutions shall not be used in advertising or otherwise to promote the
25  * sale, use or other dealings in this Software without prior written
26  * authorization from the authors.
27  *
28  * Author: Dan Nicholson <dbn.lists@gmail.com>
29  *         Daniel Stone <daniel@fooishbar.org>
30  *         Ran Benita <ran234@gmail.com>
31  */
32
33 #include "config.h"
34
35 #include <limits.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef _MSC_VER
40 #include <io.h>
41 #include <windows.h>
42 #else
43 #include <unistd.h>
44 #include <termios.h>
45 #endif
46
47 #include "tools-common.h"
48
49 #ifdef _MSC_VER
50 void
51 test_disable_stdin_echo(void)
52 {
53     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
54     DWORD mode = 0;
55     GetConsoleMode(stdin_handle, &mode);
56     SetConsoleMode(stdin_handle, mode & ~ENABLE_ECHO_INPUT);
57 }
58
59 void
60 test_enable_stdin_echo(void)
61 {
62     HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
63     DWORD mode = 0;
64     GetConsoleMode(stdin_handle, &mode);
65     SetConsoleMode(stdin_handle, mode | ENABLE_ECHO_INPUT);
66 }
67 #else
68 void
69 test_disable_stdin_echo(void)
70 {
71     /* Same as `stty -echo`. */
72     struct termios termios;
73     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
74         termios.c_lflag &= ~ECHO;
75         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
76     }
77 }
78
79 void
80 test_enable_stdin_echo(void)
81 {
82     /* Same as `stty echo`. */
83     struct termios termios;
84     if (tcgetattr(STDIN_FILENO, &termios) == 0) {
85         termios.c_lflag |= ECHO;
86         (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &termios);
87     }
88 }
89 #endif