10 static void sighandler(int s);
12 static int term_width, term_height;
13 static int ttyfd = -1;
14 static struct termios saved_term;
17 int term_init(const char *ttypath)
22 if((ttyfd = open(ttypath ? ttypath : "/dev/tty", O_RDWR)) == -1) {
23 perror("failed to open /dev/tty");
26 if(tcgetattr(ttyfd, &term) == -1) {
27 perror("failed to get terminal attr");
31 term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
32 term.c_oflag &= ~OPOST;
33 term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
34 term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8;
36 if(tcsetattr(ttyfd, TCSAFLUSH, &term) == -1) {
37 perror("failed to change terminal attributes");
41 ioctl(1, TIOCGWINSZ, &winsz);
42 term_width = winsz.ws_col;
43 term_height = winsz.ws_row;
45 signal(SIGWINCH, sighandler);
49 void term_cleanup(void)
51 tcsetattr(ttyfd, TCSAFLUSH, &saved_term);
58 write(ttyfd, "\033[2J", 4);
61 int term_getchar(void)
65 while((res = read(ttyfd, &c, 1)) < 0 && errno == EINTR);
66 if(res <= 0) return -1;
71 static void sighandler(int s)
75 signal(s, sighandler);
79 ioctl(1, TIOCGWINSZ, &winsz);
80 term_width = winsz.ws_col;
81 term_height = winsz.ws_row;