-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkinput.c
235 lines (214 loc) · 4.41 KB
/
kinput.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
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
/*
* do non-blocking keyboard reads
*
* copyright 1991 by Larry Moss ([email protected])
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include "config.h"
#ifdef __TURBOC__
# include <stdlib.h>
# include <conio.h>
#else
/* I know some systems prefer <time.h>, but I'm not sure which */
# include <sys/time.h>
#endif
#ifdef SYSV
# include <termio.h>
#else /* SYSV */
# ifndef __TURBOC__
# include <sgtty.h>
# endif /* __TURBOC__ */
#endif /* SYSV */
#include "kinput.h"
#ifndef __TURBOC__
# include "terms.h"
#endif
#include "term.h"
#include "turboc.h"
extern unsigned int score, word_count, level;
#ifndef __TURBOC__
int key_pressed();
#endif
static void intrrpt();
#ifndef NOJOB
static void pause();
static void cont();
#endif
static void die();
/*
* This function will return -1 if no key is available, or the key
* that was pressed by the user. It is checking stdin, without blocking.
*/
int key_pressed()
{
#ifdef SYSV
int chars_read;
static char keypressed;
chars_read = read(0, &keypressed, 1);
if (chars_read == 1)
return((int)keypressed);
return(-1);
#elif __TURBOC__
if (kbhit())
return getch(); /* don't echo */
else
return -1;
#else /* SYSV */
int mask = 1, chars_read;
static char keypressed;
struct timeval waittime;
waittime.tv_sec=0;
#ifdef SYSV2
waittime.tv_usec=PAUSE;
#else
waittime.tv_usec=4;
#endif
if (select(1, &mask, 0, 0, &waittime)) {
chars_read = read(0, &keypressed, 1);
if (chars_read == 1)
return((int)keypressed);
}
return(-1);
#endif /* SYSV */
}
/*
* Set the terminal to cbreak mode, turn off echo and prevent special
* characters from affecting the input. We will handle EOF and other fun
* stuff our own way. Backup copies of the current setup will be kept
* to insure the terminal gets returned to its initial state.
*/
void setterm(setting)
int setting;
{
#ifndef __TURBOC__
# ifdef SYSV
struct termio termrec;
static struct termio old_termrec;
# else /* SYSV */
struct sgttyb termrec;
struct tchars trec;
static struct tchars old_trec;
static struct sgttyb old_termrec;
# endif /* SYSV */
#endif /* __TURBOC__ */
if(setting == NEW) {
signal(SIGINT, intrrpt);
#ifndef __TURBOC__
signal(SIGQUIT, die);
#endif
signal(SIGTERM, die);
#ifndef NOJOB
signal(SIGTSTP, pause);
signal(SIGCONT, cont);
#endif /* NOJOB */
#ifdef SYSV
ioctl(0, TCGETA, &termrec);
old_termrec = termrec;
termrec.c_iflag &= ~(IGNPAR|PARMRK|INLCR|IGNCR|ICRNL);
termrec.c_iflag |= BRKINT;
termrec.c_lflag &= ~(ICANON|ECHO);
#ifdef SYSV2
termrec.c_cc[VTIME] = PAUSE / 100000;
#else
termrec.c_cc[VTIME] = 0;
#endif
termrec.c_cc[VMIN] = 0;
ioctl(0, TCSETAF, &termrec);
#elif !defined(__TURBOC__)
ioctl(0, TIOCGETP, &termrec);
old_termrec = termrec;
termrec.sg_flags |= CBREAK;
termrec.sg_flags &= ~ECHO;
ioctl(0, TIOCSETP, &termrec);
ioctl(0, TIOCGETC, &trec);
old_trec = trec;
trec.t_eofc = (char) -1;
trec.t_quitc = (char) -1;
ioctl(0, TIOCSETC, &trec);
#endif /* SYSV */
} else {
signal(SIGINT, SIG_DFL);
#ifndef __TURBOC__
signal(SIGQUIT, SIG_DFL);
#endif
signal(SIGTERM, SIG_DFL);
#ifndef NOJOB
signal(SIGTSTP, SIG_DFL);
#endif /* NOJOB */
#ifdef SYSV
ioctl(0, TCSETAF, &old_termrec);
#elif !defined(__TURBOC__)
ioctl(0, TIOCSETP, &old_termrec);
ioctl(0, TIOCSETC, &old_trec);
# endif /* SYSV */
}
}
/*
* Interrupt handlers
*/
#ifndef NOJOB
static void pause(sig)
int sig;
{
#ifdef SYSV
signal(sig, intrrpt);
#endif /* SYSV */
goto_xy(0, SCREENLENGTH + 1);
setterm(ORIG);
putchar('\n');
kill(getpid(), SIGSTOP);
}
static void cont(sig)
int sig;
{
#ifdef SYSV
signal(sig, intrrpt);
#endif /* SYSV */
setterm(NEW);
redraw();
}
#endif /* NOJOB */
static void intrrpt(sig)
int sig;
{
char c;
setterm(ORIG);
#ifdef __TURBOC__
clrscr();
cputs("are you sure you want to quit? ");
if((c = getch()) == 'y' || c == 'Y') {
textattr_clr;
printf("\n\nfinal: score = %u\twords = %u\t level = %d\n",
score, word_count, level);
exit(1);
#else
printf("\n\rare you sure you want to quit? ");
if((c = getchar()) == 'y' || c == 'Y') {
clrdisp();
printf("\n\nfinal: score = %u\twords = %u\t level = %d\n",
score, word_count, level);
highlight(0);
exit(1);
#endif
} else {
#ifdef SYSV
signal(sig, intrrpt);
#endif /* SYSV */
setterm(NEW);
redraw();
}
}
static void die(sig)
int sig;
{
textattr_clr;
#ifndef __TURBOC__
setterm(ORIG);
clrdisp();
highlight(0);
exit(1);
#endif
}