Restore terminal on SIGCONT From: Petr Baudis New bash versions won't restore the tty settings after the process is backgrounded and then foregrounded back. This makes mpg123 do so itself in a SIGCONT handler. --- src/term.c | 38 +++++++++++++++++++++++++++++--------- 1 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/term.c b/src/term.c index d0fcc01..e37804e 100644 --- a/src/term.c +++ b/src/term.c @@ -26,30 +26,50 @@ extern int buffer_pid; static int term_enable = 0; static struct termios old_tio; +void term_sigcont(int sig); + +/* This must call only functions safe inside a signal handler. */ +int term_setup(struct termios *pattern) +{ + struct termios tio = *pattern; + + signal(SIGCONT, term_sigcont); + + tio.c_lflag &= ~(ICANON|ECHO); + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + return tcsetattr(0,TCSANOW,&tio); +} + +void term_sigcont(int sig) +{ + term_enable = 0; + + if (term_setup(&old_tio) < 0) { + fprintf(stderr,"Can't set terminal attributes\n"); + return; + } + + term_enable = 1; +} + /* initialze terminal */ void term_init(void) { - struct termios tio; debug("term_init"); term_enable = 0; - if(tcgetattr(0,&tio) < 0) { + if(tcgetattr(0,&old_tio) < 0) { fprintf(stderr,"Can't get terminal attributes\n"); return; } - old_tio=tio; - tio.c_lflag &= ~(ICANON|ECHO); - tio.c_cc[VMIN] = 1; - tio.c_cc[VTIME] = 0; - - if(tcsetattr(0,TCSANOW,&tio) < 0) { + if(term_setup(&old_tio) < 0) { fprintf(stderr,"Can't set terminal attributes\n"); return; } term_enable = 1; - } static long term_handle_input(struct frame *,int);