-
Notifications
You must be signed in to change notification settings - Fork 5
/
stage1.c
114 lines (70 loc) · 1.94 KB
/
stage1.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <signal.h>
#include <string.h>
static struct termios old_termios, new_termios;
void reset_terminal() {
printf("\e[m"); // reset color changes
printf("\e[?25h"); // show cursor
fflush(stdout);
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
}
void configure_terminal() {
tcgetattr(STDIN_FILENO, &old_termios);
new_termios = old_termios; // save it to be able to reset on exit
new_termios.c_lflag &= ~(ICANON | ECHO); // turn off echo + non-canonical mode
new_termios.c_cc[VMIN] = 0;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
printf("\e[?25l"); // hide cursor
atexit(reset_terminal);
}
static int exit_loop;
void signal_handler(__attribute__((unused)) int signum) {
exit_loop = 1;
}
int read_key(char* buf, int k) {
if (buf[k] == '\033' && buf[k + 1] == '[') {
switch (buf[k + 2]) {
case 'A': return 1; // UP
case 'B': return 2; // DOWN
case 'C': return 3; // RIGHT
case 'D': return 4; // LEFT
}
}
return 0;
}
int read_input() {
char buf[4096]; // maximum input buffer
int n = read(STDIN_FILENO, buf, sizeof(buf));
int final_key = 0;
// it's okay if we miss some keys
// we will correct it on next frame
for (int k = 0; k <= n - 3; k += 3) {
int key = read_key(buf, k);
if (key == 0) continue;
final_key = key;
}
return final_key;
}
void print_key(int key) {
if (key == 1) printf("Up\n");
if (key == 2) printf("Down\n");
if (key == 3) printf("Right\n");
if (key == 4) printf("Left\n");
}
int main() {
configure_terminal();
signal(SIGINT, signal_handler);
struct timespec req = {};
struct timespec rem = {};
while (!exit_loop) {
int key = read_input();
print_key(key);
req.tv_nsec = 0.1 * 1000000000; // 0.1 seconds
nanosleep(&req, &rem);
}
}