-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermfuncs.cpp
350 lines (311 loc) · 7.15 KB
/
termfuncs.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <ctype.h>
using namespace std;
#include "termfuncs.h"
//
// TERMFUNCS.CPP Version: 2015-07-09
//
static const string CSI = "\033[";
// -- NOTE TO COMP11 STUDENTS ----------------
//
// DO NOT EDIT OR COPY CODE FROM THIS FILE
// TO USE THESE FUNCTIONS:
// at the top of your code put: #include "termfuncs.h"
// when you compile: g++ -Wall myprog.cpp termfuncs.cpp
//
// DO NOT DO THIS:
// #include "termfuncs.cpp"
//
//
// termfuncs.cpp -- some simple functions for using the terminal nicely
//
// char getachar() -- returns next char with no echo and no Enter needed
// char getacharnow(ds) -- only waits ds/10th seconds
// returns '\0' if no input by that time
// NOTE: getachar and getacharnow handle arrow keys by returning
// KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
//
// KLUDGE: if $TF_QUIT is defined then getachar will exit if that
// key is entered (total hack, I know)
//
// void screen_clear() -- clears the screen
// void screen_home() -- moves cursor to top of screen
// void screen_fg(string color)
// void screen_bg(string color)
// void screen_attr(string attr)
// void screen_bright()
// void screen_reset()
// void place_cursor(int, int); -- move the cursor to row col
// void place_char(char, int, int) -- move and place
// void hide_cursor()
// void show_cursor()
//
// int random_int(int low, int high)
// returns random int in [low, high]
// note: if SNAKE_SEED is set then use that for srand
// if not, then call srand with time(0) at first call
// void seed_random(s)
//
// hist: 2015-11-02 spcial TF_QUIT key for exit on a char
// hist: 2015-11-02 handle EOF for getachar
// hist: 2015-07-09 added arrow key handling
// hist: 2014-09-30 added flush to getachar* and also show_cursor sighandler
// hist: 2014-09-30 added hide/show cursor and random_int
// hist: 2014-07-20 getachar now uses read, not cin >> c which skipped blanks
// hist: 2014-07-01 merged screendraw and termfuncs
//
static string color_names[] = { "black", "red", "green", "yellow",
"blue", "magenta", "cyan", "white" };
static const int num_colors = 8;
static string attr_names[] = { "reset", "bright", "dim", "",
"underscore", "blink", "",
"reverse", "hidden" };
static const int num_attrs = 9;
static termios prev_tty_state;
static int prev_state_stored = 0;
static char handle_escape_seq();
//
// getachar
// returns a char using noecho and -icanon unless not a tty
//
char getachar()
{
char q_key = 0;
char *qk = NULL;
char c;
int nread;
if ( ( qk = getenv("TF_QUIT") ) != NULL ){
q_key = qk[0];
}
cout << std::flush;
if ( isatty(0) ){
struct termios info, orig;
// system("stty -echo -icanon");
tcgetattr(0, &info);
orig = info;
prev_tty_state = orig;
prev_state_stored = 1;
info.c_lflag &= ~ECHO;
info.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &info);
nread = read(0, &c, 1);
if ( nread == 1 && c == ESCAPE )
c = handle_escape_seq();
// system("stty echo icanon");
tcsetattr(0, TCSANOW, &orig);
}
else {
if ( cin.get(c) )
nread = 1;
else
nread = 0;
// cout << "got " << nread << " chars [" << c << "]\n";
if ( nread == 0 && qk != NULL )
exit(0);
}
if ( nread == 1 && qk != NULL && c == q_key ){
exit(0);
}
return ( nread > 0 ? c : 0 );
}
//
// getacharnow
// returns a char using noecho and -icanon unless not a tty
//
char getacharnow(int decisecs)
{
char c;
cout << std::flush;
if ( isatty(0) )
{
struct termios info, orig;
// system("stty -echo -icanon");
tcgetattr(0, &info);
orig = info;
prev_tty_state = orig;
prev_state_stored = 1;
info.c_lflag &= ~ECHO;
info.c_lflag &= ~ICANON;
info.c_cc[VMIN] = 0;
info.c_cc[VTIME] = decisecs;
tcsetattr(0, TCSANOW, &info);
if ( read(0, &c, 1) == 1 ){
if ( c == ESCAPE )
c = handle_escape_seq();
}
else
c = '\0';
tcsetattr(0, TCSANOW, &orig);
// system("stty echo icanon");
}
else {
if ( read(0, &c, 1) != 1 )
c = '\0';
}
return (int) c;
}
//
// ignore signals, then set tty to read in up to 100 chars in 1/10 second
// examine sequence and return code
// Note: read until an alpha char or inactivity
//
char handle_escape_seq()
{
struct termios info, orig;
char buf[100];
int pos = 0;
char c;
void (*oldhand)(int);
if ( !isatty(0) )
return ESCAPE;
oldhand = signal(SIGINT, SIG_IGN);
// system("stty -echo -icanon");
tcgetattr(0, &info);
orig = info;
info.c_lflag &= ~ECHO;
info.c_lflag &= ~ICANON;
info.c_cc[VMIN] = 0;
info.c_cc[VTIME] = 1; // 1/10 second
tcsetattr(0, TCSANOW, &info);
do
{
if ( read(0, &c, 1) == 1 ) // if got one
buf[pos++] = c; // store it
else
break; // else timeout
}
while( !isalpha((int)c) && pos < 99 ); // until alpha or full
if ( pos == 0 || (buf[0] != '[' && buf[0] != 'O' ) )
c = ESCAPE;
else
switch(buf[1]){
case 'A': c = KEY_UP; break;
case 'B': c = KEY_DOWN; break;
case 'C': c = KEY_RIGHT; break;
case 'D': c = KEY_LEFT; break;
default :
c = ESCAPE;
}
tcsetattr(0, TCSANOW, &orig);
signal(SIGINT, oldhand);
// system("stty echo icanon");
return c;
}
static void restore_tty_state()
{
if ( prev_state_stored )
tcsetattr(0, TCSANOW, &prev_tty_state);
}
void screen_clear()
{
cout << (CSI + "H");
cout << (CSI + "2J");
}
void screen_home()
{
cout << (CSI + "H");
}
static void (*prev_handler)(int) = NULL;;
void hide_cursor()
{
void on_sigint(int);
prev_handler = signal(SIGINT, on_sigint);
cout << (CSI + "?25l");
}
void show_cursor()
{
cout << (CSI + "?25h");
}
void on_sigint(int s)
{
cout << std::flush;
show_cursor();
if ( prev_handler ){
prev_handler(s);
}
restore_tty_state();
exit(SIGINT);
}
//
// lookup a string in an array
// args: string to find, list of strings, len of list
// rets: index of string or -1 for not found
//
static int lookup(string findme, string list[], int num)
{
int i;
for(i=0;i<num;i++)
{
if ( list[i] == findme )
return i;
}
return -1;
}
void screen_fg(string color)
{
int num = lookup(color, color_names, num_colors);
if ( num >= 0 ){
cout << CSI << ( 30 + num ) << "m";
}
}
void screen_bg(string color)
{
int num = lookup(color, color_names, num_colors);
if ( num >= 0 ){
cout << CSI << ( 40 + num ) << "m";
}
}
void screen_attr(string attr)
{
int num = lookup(attr, attr_names, num_attrs);
if ( num >= 0 ) {
cout << CSI << num << "m";
}
}
void screen_bright()
{
screen_attr("bright");
}
void screen_reset()
{
screen_attr("reset");
}
void place_cursor(int row, int col)
{
cout << "\033[" << (row + 1) << ";" << (col + 1) << "H";
flush(cout);
}
void place_char(char c, int row, int col)
{
place_cursor(row, col);
cout << c;
flush(cout);
}
static int rand_seed = -1;
void seed_random(int s)
{
if ( s > 0 ) {
srand(s);
rand_seed = s;
}
}
int random_int(int lo, int hi)
{
int range = hi - lo + 1;
char *seed_str;
if ( rand_seed == -1 ) {
seed_str = getenv( "SNAKE_SEED" );
if ( seed_str != NULL )
seed_random( (unsigned) atoi(seed_str) );
else
seed_random( (unsigned) time(NULL) );
}
if ( range <= 0 )
return 17; // add to function specs
return lo + ( rand() % range );
}