abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 76bae5939f653c248aa104c2bc72f39a4a57c01e
parent 16d76139db8d26ac15c0eada65fa1a1a03bdbe47
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Fri, 19 Dec 2014 22:46:26 +0100

Improve terminal restoring

Instead of simply moving the cursor to the bottom of the screen
use the alternate screen buffer i.e. the smcup/rmcup capabilities.

This is currently hardcoded to the xterm originated \e[?1047{h,l}
sequence. I also patched dvtm to support these.

Diffstat:
abduco.c | 16++--------------
client.c | 32++++++++++++++++++++++++++------
2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/abduco.c b/abduco.c @@ -113,7 +113,7 @@ typedef struct { static Server server = { .running = true, .exit_status = -1, .host = "@localhost" }; static Client client; static struct termios orig_term, cur_term; -bool has_term; +static bool has_term, alternate_buffer; static struct sockaddr_un sockaddr = { .sun_family = AF_UNIX, @@ -194,7 +194,6 @@ static bool recv_packet(int socket, Packet *pkt) { static void info(const char *str, ...) { va_list ap; va_start(ap, str); - fprintf(stderr, "\033[999H"); if (str) { fprintf(stderr, "%s: %s: ", server.name, server.session_name); vfprintf(stderr, str, ap); @@ -409,19 +408,8 @@ static bool attach_session(const char *name, const bool terminate) { sigaction(SIGWINCH, &sa, NULL); sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); - atexit(client_restore_terminal); - - cur_term = orig_term; - cur_term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF); - cur_term.c_oflag &= ~(OPOST); - cur_term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - cur_term.c_cflag &= ~(CSIZE|PARENB); - cur_term.c_cflag |= CS8; - cur_term.c_cc[VLNEXT] = _POSIX_VDISABLE; - cur_term.c_cc[VMIN] = 1; - cur_term.c_cc[VTIME] = 0; - tcsetattr(STDIN_FILENO, TCSADRAIN, &cur_term); + client_setup_terminal(); int status = client_mainloop(); client_restore_terminal(); if (status == -1) { diff --git a/client.c b/client.c @@ -21,15 +21,35 @@ static bool client_recv_packet(Packet *pkt) { return false; } -static void client_show_cursor(void) { - printf("\033[?25h"); - fflush(stdout); -} - static void client_restore_terminal(void) { if (has_term) tcsetattr(STDIN_FILENO, TCSADRAIN, &orig_term); - client_show_cursor(); + if (alternate_buffer) { + printf("\033[?1049l"); + fflush(stdout); + alternate_buffer = false; + } +} + +static void client_setup_terminal(void) { + atexit(client_restore_terminal); + + cur_term = orig_term; + cur_term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF); + cur_term.c_oflag &= ~(OPOST); + cur_term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + cur_term.c_cflag &= ~(CSIZE|PARENB); + cur_term.c_cflag |= CS8; + cur_term.c_cc[VLNEXT] = _POSIX_VDISABLE; + cur_term.c_cc[VMIN] = 1; + cur_term.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSADRAIN, &cur_term); + + if (!alternate_buffer) { + printf("\033[?1049h\033[H"); + fflush(stdout); + alternate_buffer = true; + } } static int client_mainloop(void) {