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.
This commit is contained in:
Martin Lund 2016-05-17 11:53:57 +02:00
parent 9571a84d70
commit 0687d7ff7b
4 changed files with 49 additions and 17 deletions

View file

@ -58,8 +58,13 @@ Display program version.
Display help. Display help.
.SH "KEYS" .SH "KEYS"
.TP .PP
In session, press ctrl-t + q to quit. .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" .SH "EXAMPLES"
.TP .TP

View file

@ -36,6 +36,10 @@
fprintf (stdout, "\r" ANSI_COLOR_YELLOW format ANSI_COLOR_RESET "\r\n", ## args); \ fprintf (stdout, "\r" ANSI_COLOR_YELLOW format ANSI_COLOR_RESET "\r\n", ## args); \
fflush(stdout); fflush(stdout);
#define warning_printf(format, args...) \
fprintf (stdout, "\rWarning: " format "\r\n", ## args); \
fflush(stdout);
#ifdef DEBUG #ifdef DEBUG
#define debug_printf(format, args...) \ #define debug_printf(format, args...) \
fprintf (stdout, "[debug] " format, ## args) fprintf (stdout, "[debug] " format, ## args)

View file

@ -24,6 +24,7 @@
#define KEY_CTRL_T 0x14 #define KEY_CTRL_T 0x14
#define KEY_Q 0x71 #define KEY_Q 0x71
#define KEY_T 0x74
void configure_stdout(void); void configure_stdout(void);
void restore_stdout(void); void restore_stdout(void);

View file

@ -166,8 +166,9 @@ int connect_tty(void)
{ {
fd_set rdfs; /* Read file descriptor set */ fd_set rdfs; /* Read file descriptor set */
int maxfd; /* Maximum file descriptor used */ int maxfd; /* Maximum file descriptor used */
char input_char, output_char;
static char previous_char = 0;
static bool first = true; static bool first = true;
static char input_char, previous_char = 0;
int status; int status;
/* Open tty device */ /* Open tty device */
@ -261,6 +262,8 @@ int connect_tty(void)
} }
if (FD_ISSET(STDIN_FILENO, &rdfs)) if (FD_ISSET(STDIN_FILENO, &rdfs))
{ {
char forward = true;
/* Input from stdin ready */ /* Input from stdin ready */
status = read(STDIN_FILENO, &input_char, 1); status = read(STDIN_FILENO, &input_char, 1);
if (status <= 0) if (status <= 0)
@ -269,25 +272,44 @@ int connect_tty(void)
goto error_read; goto error_read;
} }
/* Exit upon ctrl-t + q sequence */ /* Forward input to output except ctrl-t key */
if ((input_char == KEY_Q) && (previous_char == KEY_CTRL_T)) output_char = input_char;
exit(EXIT_SUCCESS); if (input_char == KEY_CTRL_T)
forward = false;
/* Ignore ctrl-t except when repeated */ /* Handle escape key commands */
if ((input_char != KEY_CTRL_T) || if (previous_char == KEY_CTRL_T)
((input_char == KEY_CTRL_T) && (previous_char == KEY_CTRL_T)))
{ {
/* Forward input to tty device */ switch (input_char)
status = write(fd, &input_char, 1); {
if (status < 0) case KEY_Q:
printf("Warning: Could not write to tty device\r\n"); /* 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 */ /* Write to log */
if (option.log) if (option.log)
log_write(input_char); log_write(output_char);
}
/* Save previous key */
previous_char = input_char;
/* Insert output delay */ /* Insert output delay */
if (option.output_delay) if (option.output_delay)