-
Notifications
You must be signed in to change notification settings - Fork 5
/
conion.c
executable file
·63 lines (53 loc) · 1.01 KB
/
conion.c
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
#include <stdio.h>
#include "conion.h"
#ifdef STD_CONSOLE_INPUT
void initialize_console_io()
{
}
void restore_console_io()
{
}
int kbhit(void)
{
return 0;
}
#else
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
static int initialized = 0;
static struct termios oldt;
void initialize_console_io(void)
{
struct termios newt;
if (initialized) return;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO | ISIG);
newt.c_iflag &= ~(ICRNL | INLCR);
newt.c_oflag &= ~OPOST;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
initialized = 1;
}
void restore_console_io(void)
{
if (!initialized) return;
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
initialized = 0;
}
int kbhit(void)
{
fd_set set;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
return select(STDIN_FILENO + 1, &set, NULL, NULL, &tv) > 0;
}
#endif
int getch(void)
{
return getchar();
}