-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkb.c
More file actions
executable file
·109 lines (103 loc) · 2.27 KB
/
Copy pathkb.c
File metadata and controls
executable file
·109 lines (103 loc) · 2.27 KB
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
/* kb.c */
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef GRX
#include <vga.h>
#include <vgamouse.h>
#endif
#define TRUE 1
#define FALSE 0
/*
* keyboard stuff
*/
static struct termios kbd_setup;
static struct termios kbd_reset;
static int kbd_initted = FALSE;
static int kbd_isatty;
static int kbd_lastchr;
static int kbd_filedsc;
static int opt_act = 0;
static enum { normal, test_key, wait_key, unknown } kbd_mode = unknown;
void
kbd_restore(void)
{
if(kbd_initted && kbd_isatty && (kbd_mode != normal)) {
tcsetattr(kbd_filedsc, opt_act, &kbd_reset);
kbd_mode = normal;
}
}
void
kbd_init(void)
{
if(!kbd_initted) {
kbd_initted = TRUE;
kbd_lastchr = EOF;
kbd_filedsc = fileno(stdin);
kbd_isatty = isatty(kbd_filedsc);
if(kbd_isatty) {
tcgetattr(kbd_filedsc, &kbd_setup);
tcgetattr(kbd_filedsc, &kbd_reset);
kbd_setup.c_lflag &= ~(ICANON | ECHO );
kbd_setup.c_iflag &= ~(INLCR | ICRNL);
atexit(kbd_restore);
kbd_mode = normal;
}
}
}
int
_kbhit(void)
{
unsigned char buf[4];
if(!kbd_initted) {
kbd_init();
}
if(!kbd_isatty) {
return(TRUE);
}
if(kbd_lastchr != EOF) {
return(TRUE);
}
if(kbd_mode != test_key) {
kbd_setup.c_cc[VMIN] = 0;
kbd_setup.c_cc[VTIME] = 0;
if(tcsetattr(kbd_filedsc, opt_act, &kbd_setup) == EOF) return(FALSE);
kbd_mode = test_key;
}
if(read(kbd_filedsc,buf,1) > 0) {
kbd_lastchr = buf[0];
return(TRUE);
}
return(FALSE);
}
int
_getch(void)
{
unsigned char buf[4];
if(!kbd_initted) {
kbd_init();
}
if(!kbd_isatty) {
return(getc(stdin));
}
if(kbd_lastchr != EOF) {
buf[0] = kbd_lastchr;
kbd_lastchr = EOF;
return(buf[0]);
}
if(kbd_mode != wait_key) {
kbd_setup.c_cc[VMIN] = 1;
kbd_setup.c_cc[VTIME] = 0;
if(tcsetattr(kbd_filedsc, opt_act, &kbd_setup) == EOF)
return(EOF);
kbd_mode = wait_key;
}
if(read(kbd_filedsc,buf,1) > 0) {
return(buf[0]);
}
return(EOF);
}