-
Notifications
You must be signed in to change notification settings - Fork 0
/
conio.c
553 lines (426 loc) · 8.72 KB
/
conio.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#ifdef _WIN32
// creditos a https://github.com/thradams/conio/tree/master?tab=License-1-ov-file
#include <windows.h>
#include <conio.h>
#include "conio.h"
#include <limits.h>
#include <stdbool.h>
/*windows 10 can use VT100*/
bool EnableVTMode() {
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
{
return false;
}
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode))
{
return false;
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode))
{
return false;
}
return true;
}
static void clearbits(unsigned char * v,
int bit_index,
int nbits)
{
unsigned mask = ~((unsigned char)(0)) << (sizeof(v) * CHAR_BIT - (unsigned char)(nbits));
mask = mask >> (sizeof(v) * CHAR_BIT - (unsigned char)(bit_index)-(unsigned char)(nbits));
*v &= ~mask;
}
static void setbits(unsigned char *v,
int bit_index,
int nbits,
unsigned char number)
{
clearbits(&number, nbits, sizeof(number) * CHAR_BIT - nbits);
unsigned char big = number;
big = (big << bit_index);
clearbits(v, bit_index, nbits);
*v |= big;
}
static unsigned char getbits(unsigned char v, int bit_index, int nbits)
{
unsigned char r = v >> bit_index;
clearbits(&r, nbits, sizeof(unsigned char) * CHAR_BIT - nbits);
return r;
}
void c_gettextinfo(struct text_info *r)
{
if (r == 0)
return;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
r->attribute = (unsigned char)csbi.wAttributes;
r->curx = (unsigned char)csbi.dwCursorPosition.X;
r->cury = (unsigned char)csbi.dwCursorPosition.Y;
r->screenwidth = (unsigned char)csbi.dwMaximumWindowSize.X;
r->screenheight = (unsigned char)csbi.dwMaximumWindowSize.X;
r->normattr = 0;
}
int c_kbhit(void)
{
return _kbhit();
}
int c_getch(void)
{
return _getch();
}
int c_getche(void)
{
return _getche();
}
void c_setcursortype(int cur_t)
{
CONSOLE_CURSOR_INFO ci;
switch (cur_t)
{
case _NOCURSOR:// (turns off the cursor)
ci.bVisible = FALSE;
ci.dwSize = 1;
break;
case _SOLIDCURSOR:// (solid block cursor)
ci.bVisible = TRUE;
ci.dwSize = 100;
break;
default:
case _NORMALCURSOR: // (normal underscore cursor)
ci.bVisible = TRUE;
ci.dwSize = 50;
break;
}
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
}
void c_textattr(int newattr)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), newattr);
}
void c_textbackground(int newcolor)
{
struct text_info ti;
c_gettextinfo(&ti);
unsigned char wColor = ti.attribute;
unsigned char old = getbits(wColor, 4, 4);
setbits(&wColor, 4, 4, newcolor);
c_textattr(wColor);
}
void c_textcolor(int newcolor)
{
struct text_info ti;
c_gettextinfo(&ti);
unsigned char wColor = ti.attribute;
int old = getbits(wColor, 0, 4);
setbits(&wColor, 0, 4, newcolor);
c_textattr(wColor);
}
int c_wherex()
{
CONSOLE_SCREEN_BUFFER_INFO cbsi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cbsi))
{
return cbsi.dwCursorPosition.X + 1;
}
return -1;
}
int c_wherey()
{
CONSOLE_SCREEN_BUFFER_INFO cbsi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cbsi))
{
return cbsi.dwCursorPosition.Y;
}
return -1;
}
void c_gotoxy(int x, int y)
{
COORD point;
point.X = x - (short)1;
point.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}
void c_clrscr()
{
COORD coordScreen = { 0, 0 };
unsigned long cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
unsigned long dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
#elif __linux__
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "conio.h"
int c_kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= (tcflag_t) ~(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;
}
static int getCursorPosition2(int *x, int *y)
{
*x = -1;
*y = -1;
char buf[32];
unsigned int i = 0;
int ch;
printf("\x1B[6n");
while (i < sizeof(buf) - 1)
{
ch = c_getch();
if (ch == EOF || ch == 'R') break;
buf[i++] = (char) ch;
}
buf[i] = '\0';
if (buf[0] != '\x1b' || buf[1] != '[') return -1;
if (sscanf(&buf[2], "%d;%d", y, x) != 2) return -1;
return 0;
}
int c_wherex(void)
{
int x, y;
getCursorPosition2(&x, &y);
return x;
}
int c_wherey(void)
{
int x, y;
getCursorPosition2(&x, &y);
return y;
}
void c_gotoxy(int x, int y)
{
printf("\x1b[%d;%dH", y, x);
fflush(stdout);
}
void c_clrscr()
{
puts("\x1b[2J\x1b[1;1H");
fflush(stdout);
}
void c_textcolor(int newcolor)
{
//https://en.wikipedia.org/wiki/ANSI_escape_code
const char * s = "\x1b[30m";
switch (newcolor)
{
case BLACK:
s = "\x1b[30m";
break;
case BLUE:
s = "\x1b[34m";
break;
case GREEN:
s = "\x1b[32m";
break;
case CYAN:
s = "\x1b[36m";
break;
case RED:
s = "\x1b[31;1m";
break;
case MAGENTA:
s = "\x1b[35m";
break;
case BROWN:
s = "\x1b[31m";
break;
case LIGHTGRAY:
s = "\x1b[30;1m";
break;
case DARKGRAY:
s = "\x1b[30m";
break;
case LIGHTBLUE:
s = "\x1b[34;1m";
break;
case LIGHTGREEN:
s = "\x1b[32,1m";;
break;
case LIGHTCYAN:
s = "\x1b[36;1m";
break;
case LIGHTRED:
s = "\x1b[31;1m";
break;
case LIGHTMAGENTA:
s = "\x1b[35;1m";
break;
case YELLOW:
s = "\x1b[33;1m";
break;
case WHITE:
s = "\x1b[37;1m";
break;
case BLINK:
s = "\x1b[30m";
break;
};
printf("%s", s);
}
void c_textbackground(int newcolor)
{
//https://en.wikipedia.org/wiki/ANSI_escape_code
const char * s = "\x1b[40m";
switch (newcolor)
{
case BLACK:
s = "\x1b[40m";
break;
case BLUE:
s = "\x1b[44m";
break;
case GREEN:
s = "\x1b[42m";
break;
case CYAN:
s = "\x1b[46m";
break;
case RED:
s = "\x1b[41;1m";
break;
case MAGENTA:
s = "\x1b[45m";
break;
case BROWN:
s = "\x1b[41m";
break;
case LIGHTGRAY:
s = "\x1b[40;1m";
break;
case DARKGRAY:
s = "\x1b[40m";
break;
case LIGHTBLUE:
s = "\x1b[44;1m";
break;
case LIGHTGREEN:
s = "\x1b[42,1m";;
break;
case LIGHTCYAN:
s = "\x1b[46;1m";
break;
case LIGHTRED:
s = "\x1b[41;1m";
break;
case LIGHTMAGENTA:
s = "\x1b[45;1m";
break;
case YELLOW:
s = "\x1b[43;1m";
break;
case WHITE:
s = "\x1b[47;1m";
break;
case BLINK:
s = "\x1b[40m";
break;
};
puts(s);
}
/* Read 1 character - echo defines echo mode */
/*
static char getch_(int echo)
{
struct termios old, new;
int ch;
tcgetattr(0, &old);
new = old;
new.c_lflag &= ~ICANON;
if (!echo)
{
new.c_lflag &= ~ECHO;
}
tcsetattr(0, TCSANOW, &new);
ch = getchar();
tcsetattr(0, TCSANOW, &old);
return ch;
}
*/
/* Read 1 character without echo */
int c_getch(void)
{
struct termios old, new;
int ch;
tcgetattr(0, &old);
new = old;
new.c_lflag &= (tcflag_t) ~ICANON;
new.c_lflag &= (tcflag_t) ~ECHO;
tcsetattr(0, TCSANOW, &new);
ch = getchar();
tcsetattr(0, TCSANOW, &old);
return ch;
}
/* Read 1 character with echo */
int c_getche(void)
{
struct termios old, new;
int ch;
tcgetattr(0, &old);
new = old;
new.c_lflag &= (tcflag_t) ~ICANON;
//new.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &new);
ch = getchar();
tcsetattr(0, TCSANOW, &old);
return ch;
}
void c_setcursortype(int cur_t)
{
switch (cur_t)
{
case _NOCURSOR:
printf("\x1b[?25l");
break;
case _NORMALCURSOR:
printf("\x1b[?25h");
break;
case _SOLIDCURSOR://TODO
printf("\x1b[?25h");
break;
}
}
void c_gettextinfo(struct text_info *r)
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
r->screenheight = w.ws_row;
r->screenwidth = w.ws_col;
int x, y;
getCursorPosition2(&x, &y);
r->curx = x;
r->cury = y;
}
void c_textattr(int newattr)
{
//tODO
}
#endif //linux