-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
96 lines (89 loc) · 1.84 KB
/
functions.h
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
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include <sys/msg.h>
#include <omp.h>
static struct termios orig_term;
void u_cleanup(void)
{
tcsetattr(0, TCSANOW, &orig_term);
}
void die(char *str) {
perror(str);
exit(1);
}
int u_kbhit()
{
struct termios t;
int ret;
fd_set rfd;
struct timeval to;
static int first_hit=0;
if(first_hit==0)
{
if(tcgetattr(0, &t)!=0) exit(0);
orig_term=t;
cfmakeraw(&t);
if(tcsetattr(0, TCSANOW, &t)!=0) exit(0);
atexit(u_cleanup);
first_hit=1;
}
FD_ZERO(&rfd);
FD_SET(0, &rfd);
to.tv_sec=0;
to.tv_usec=0;
if(select(1, &rfd, NULL, NULL, &to)==1) return 1;
return 0;
}
int kbhit()
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int getch(void) {
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
int getche(void) {
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}