From 05b4673efbeb372387b0a9359af5046afb87c8cf Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 21 Nov 2019 04:39:27 +0200 Subject: [PATCH] starting a rough editor program as a scaffold to test and try the visor algorithms --- .gitignore | 1 + visor/Makefile | 23 ++++++++++++ visor/src/main_unix.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 visor/Makefile create mode 100644 visor/src/main_unix.c diff --git a/.gitignore b/.gitignore index a8f9429..1f97476 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.swp *.a *.so +visor/visor diff --git a/visor/Makefile b/visor/Makefile new file mode 100644 index 0000000..b97b548 --- /dev/null +++ b/visor/Makefile @@ -0,0 +1,23 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +dep = $(obj:.o=.d) +bin = visor + +CFLAGS = -pedantic -Wall -g + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +-include $(dep) + +%.d: %.c + @echo dep $@ + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) + +.PHONY: cleandep +cleandep: + rm -f $(dep) diff --git a/visor/src/main_unix.c b/visor/src/main_unix.c new file mode 100644 index 0000000..37406a0 --- /dev/null +++ b/visor/src/main_unix.c @@ -0,0 +1,95 @@ +#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; + char c; + + if(init() == -1) { + return 1; + } + + for(;;) { + if((res = read(ttyfd, &c, 1)) == 0 || (res < 0 && errno != EINTR)) { + break; + } + /* proc input */ + } + + cleanup(); + return 0; +} + +static int init(void) +{ + 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); + + write(ttyfd, "\033[2J", 4); + + 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; + } +} -- 1.7.10.4