From c21ad1b67f3bb33cda9a85006486a6d88116a731 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 21 Nov 2019 16:46:25 +0200 Subject: [PATCH] moved terminal stuff to their own file --- visor/src/main_unix.c | 64 +++-------------------------------- visor/src/term.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ visor/src/term.h | 9 +++++ 3 files changed, 101 insertions(+), 60 deletions(-) create mode 100644 visor/src/term.c create mode 100644 visor/src/term.h diff --git a/visor/src/main_unix.c b/visor/src/main_unix.c index 37406a0..9e96d45 100644 --- a/visor/src/main_unix.c +++ b/visor/src/main_unix.c @@ -1,19 +1,9 @@ #include -#include -#include -#include -#include -#include -#include static int init(void); static void cleanup(void); static void sighandler(int s); -int term_width, term_height; -int ttyfd; -struct termios saved_term; - int main(int argc, char **argv) { int res; @@ -24,7 +14,7 @@ int main(int argc, char **argv) } for(;;) { - if((res = read(ttyfd, &c, 1)) == 0 || (res < 0 && errno != EINTR)) { + if(term_getchar() == 'q') { break; } /* proc input */ @@ -36,60 +26,14 @@ int main(int argc, char **argv) static int init(void) { - struct termios term; - struct winsize winsz; - - if((ttyfd = open("/dev/tty", O_RDWR)) == -1) { - perror("failed to open /dev/tty"); + if(term_init() == -1) { return -1; } - if(tcgetattr(ttyfd, &term) == -1) { - perror("failed to get terminal attr"); - return -1; - } - saved_term = term; - term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); - term.c_oflag &= ~OPOST; - term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); - term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8; - - if(tcsetattr(ttyfd, TCSAFLUSH, &term) == -1) { - perror("failed to change terminal attributes"); - return -1; - } - - ioctl(1, TIOCGWINSZ, &winsz); - term_width = winsz.ws_col; - term_height = winsz.ws_row; - - signal(SIGWINCH, sighandler); - - write(ttyfd, "\033[2J", 4); - + term_clear(); return 0; } static void cleanup(void) { - tcsetattr(ttyfd, TCSAFLUSH, &saved_term); - close(ttyfd); -} - -static void sighandler(int s) -{ - struct winsize winsz; - - signal(s, sighandler); - - switch(s) { - case SIGWINCH: - ioctl(1, TIOCGWINSZ, &winsz); - term_width = winsz.ws_col; - term_height = winsz.ws_row; - /* redraw */ - break; - - default: - break; - } + term_cleanup(); } diff --git a/visor/src/term.c b/visor/src/term.c new file mode 100644 index 0000000..5a167a6 --- /dev/null +++ b/visor/src/term.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include "term.h" + +static void sighandler(int s); + +static int term_width, term_height; +static int ttyfd = -1; +static struct termios saved_term; + + +int term_init(const char *ttypath) +{ + struct termios term; + struct winsize winsz; + + if((ttyfd = open("/dev/tty", O_RDWR)) == -1) { + perror("failed to open /dev/tty"); + return -1; + } + if(tcgetattr(ttyfd, &term) == -1) { + perror("failed to get terminal attr"); + return -1; + } + saved_term = term; + term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + term.c_oflag &= ~OPOST; + term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8; + + if(tcsetattr(ttyfd, TCSAFLUSH, &term) == -1) { + perror("failed to change terminal attributes"); + return -1; + } + + ioctl(1, TIOCGWINSZ, &winsz); + term_width = winsz.ws_col; + term_height = winsz.ws_row; + + signal(SIGWINCH, sighandler); + return 0; +} + +void term_cleanup(void) +{ + tcsetattr(ttyfd, TCSAFLUSH, &saved_term); + close(ttyfd); + ttyfd = -1; +} + +void term_clear(void) +{ + write(ttyfd, "\033[2J", 4); +} + +int term_getchar(void) +{ + int res; + char c; + while((res = read(ttyfd, &c, 1)) < 0 && errno == EINTR); + if(res <= 0) return -1; + return c; +} + + +static void sighandler(int s) +{ + struct winsize winsz; + + signal(s, sighandler); + + switch(s) { + case SIGWINCH: + ioctl(1, TIOCGWINSZ, &winsz); + term_width = winsz.ws_col; + term_height = winsz.ws_row; + /* redraw */ + break; + + default: + break; + } +} diff --git a/visor/src/term.h b/visor/src/term.h new file mode 100644 index 0000000..fd2a4c4 --- /dev/null +++ b/visor/src/term.h @@ -0,0 +1,9 @@ +#ifndef TERM_H_ +#define TERM_H_ + +int term_init(const char *ttypath); +void term_cleanup(void); + +void term_clear(void); + +#endif /* TERM_H_ */ -- 1.7.10.4