From 0687d7ff7be3fea2f1ebd9dd78eb89eacc44d209 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Tue, 17 May 2016 11:53:57 +0200 Subject: [PATCH] Further simplification of key handling Changed so that the "ctrl-t ctrl-t" sequence is now simply "ctrl-t t" to send the ctrl-t key code. This is inspired by screen which does similar to send its command key code (ctrl-a a). This change also allows to easier add new key commands if needed. Updated man page accordingly. --- man/tio.1 | 9 +++++-- src/include/tio/print.h | 4 ++++ src/include/tio/tty.h | 1 + src/tty.c | 52 +++++++++++++++++++++++++++++------------ 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/man/tio.1 b/man/tio.1 index c744961..5e4e1d7 100644 --- a/man/tio.1 +++ b/man/tio.1 @@ -58,8 +58,13 @@ Display program version. Display help. .SH "KEYS" -.TP -In session, press ctrl-t + q to quit. +.PP +.TP 16n +In session, the following key sequences are intercepted as tio commands: +.IP "\fBctrl-t q" +Quit +.IP "\fBctrl-t t" +Send ctrl-t key code .SH "EXAMPLES" .TP diff --git a/src/include/tio/print.h b/src/include/tio/print.h index 7eb06cb..7254174 100644 --- a/src/include/tio/print.h +++ b/src/include/tio/print.h @@ -36,6 +36,10 @@ fprintf (stdout, "\r" ANSI_COLOR_YELLOW format ANSI_COLOR_RESET "\r\n", ## args); \ fflush(stdout); +#define warning_printf(format, args...) \ + fprintf (stdout, "\rWarning: " format "\r\n", ## args); \ + fflush(stdout); + #ifdef DEBUG #define debug_printf(format, args...) \ fprintf (stdout, "[debug] " format, ## args) diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 3ada24f..9324dfd 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -24,6 +24,7 @@ #define KEY_CTRL_T 0x14 #define KEY_Q 0x71 +#define KEY_T 0x74 void configure_stdout(void); void restore_stdout(void); diff --git a/src/tty.c b/src/tty.c index 9e4d99d..845334f 100644 --- a/src/tty.c +++ b/src/tty.c @@ -166,8 +166,9 @@ int connect_tty(void) { fd_set rdfs; /* Read file descriptor set */ int maxfd; /* Maximum file descriptor used */ + char input_char, output_char; + static char previous_char = 0; static bool first = true; - static char input_char, previous_char = 0; int status; /* Open tty device */ @@ -261,6 +262,8 @@ int connect_tty(void) } if (FD_ISSET(STDIN_FILENO, &rdfs)) { + char forward = true; + /* Input from stdin ready */ status = read(STDIN_FILENO, &input_char, 1); if (status <= 0) @@ -269,25 +272,44 @@ int connect_tty(void) goto error_read; } - /* Exit upon ctrl-t + q sequence */ - if ((input_char == KEY_Q) && (previous_char == KEY_CTRL_T)) - exit(EXIT_SUCCESS); + /* Forward input to output except ctrl-t key */ + output_char = input_char; + if (input_char == KEY_CTRL_T) + forward = false; - /* Ignore ctrl-t except when repeated */ - if ((input_char != KEY_CTRL_T) || - ((input_char == KEY_CTRL_T) && (previous_char == KEY_CTRL_T))) + /* Handle escape key commands */ + if (previous_char == KEY_CTRL_T) { - /* Forward input to tty device */ - status = write(fd, &input_char, 1); - if (status < 0) - printf("Warning: Could not write to tty device\r\n"); + switch (input_char) + { + case KEY_Q: + /* Exit upon ctrl-t q sequence */ + exit(EXIT_SUCCESS); + case KEY_T: + /* Send ctrl-t key code upon ctrl-t t sequence */ + output_char = KEY_CTRL_T; + break; + default: + /* Ignore unknown ctrl-t escaped keys */ + forward = false; + break; + } } - previous_char = input_char; + if (forward) + { + /* Send output to tty device */ + status = write(fd, &output_char, 1); + if (status < 0) + warning_printf("Could not write to tty device"); - /* Write to log */ - if (option.log) - log_write(input_char); + /* Write to log */ + if (option.log) + log_write(output_char); + } + + /* Save previous key */ + previous_char = input_char; /* Insert output delay */ if (option.output_delay)