Skip to content

Commit

Permalink
Re-init terminal when awaking from ^Z
Browse files Browse the repository at this point in the history
And simplify code that nobody touched in a long long time… Fixes input
issues reported in #458.
  • Loading branch information
PromyLOPh committed Jun 16, 2014
1 parent 8cc95de commit 0117308
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 55 deletions.
8 changes: 2 additions & 6 deletions src/main.c
Expand Up @@ -414,15 +414,11 @@ static void BarMainLoop (BarApp_t *app) {

int main (int argc, char **argv) {
static BarApp_t app;
/* terminal attributes _before_ we started messing around with ~ECHO */
struct termios termOrig;

memset (&app, 0, sizeof (app));

/* save terminal attributes, before disabling echoing */
BarTermSave (&termOrig);
BarTermSetEcho (0);
BarTermSetBuffer (0);
BarTermInit ();

/* signals */
signal (SIGPIPE, SIG_IGN);
Expand Down Expand Up @@ -506,7 +502,7 @@ int main (int argc, char **argv) {
BarSettingsDestroy (&app.settings);

/* restore terminal attributes, zsh doesn't need this, bash does... */
BarTermRestore (&termOrig);
BarTermRestore ();

return 0;
}
Expand Down
67 changes: 22 additions & 45 deletions src/terminal.c
@@ -1,5 +1,5 @@
/*
Copyright (c) 2008-2010
Copyright (c) 2008-2014
Lars-Dominik Braun <lars@6xq.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -21,61 +21,38 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef __FreeBSD__
#define _POSIX_C_SOURCE 1 /* fileno() */
#define _BSD_SOURCE /* setlinebuf() */
#define _DARWIN_C_SOURCE /* setlinebuf() on OS X */
#endif

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include "terminal.h"

/* en/disable echoing for stdin
* @param 1 = enable, everything else = disable
*/
void BarTermSetEcho (char enable) {
struct termios termopts;
/* need a global variable here, since these functions get called from a signal
* handler */
static struct termios restore;

tcgetattr (fileno (stdin), &termopts);
if (enable == 1) {
termopts.c_lflag |= ECHO;
} else {
termopts.c_lflag &= ~ECHO;
}
tcsetattr(fileno (stdin), TCSANOW, &termopts);
/* init terminal attributes when continuing, assuming the shell modified them;
* tcget/setattr and signal are async signal safe */
static void BarTermHandleCont (int sig) {
BarTermInit ();
}

/* en/disable stdin buffering; when enabling line-buffer method will be
* selected for you
* @param 1 = enable, everything else = disable
*/
void BarTermSetBuffer (char enable) {
struct termios termopts;
void BarTermInit () {
struct termios newopt;

tcgetattr (fileno (stdin), &termopts);
if (enable == 1) {
termopts.c_lflag |= ICANON;
setlinebuf (stdin);
} else {
termopts.c_lflag &= ~ICANON;
setvbuf (stdin, NULL, _IONBF, 1);
}
tcsetattr(fileno (stdin), TCSANOW, &termopts);
}
tcgetattr (STDIN_FILENO, &restore);
memcpy (&newopt, &restore, sizeof (newopt));

/* disable echoing and line buffer */
newopt.c_lflag &= ~(ECHO | ICANON);
tcsetattr (STDIN_FILENO, TCSANOW, &newopt);

/* Save old terminal settings
* @param save settings here
*/
void BarTermSave (struct termios *termOrig) {
tcgetattr (fileno (stdin), termOrig);
signal (SIGCONT, BarTermHandleCont);
}

/* Restore terminal settings
* @param Old settings
*/
void BarTermRestore (struct termios *termOrig) {
tcsetattr (fileno (stdin), TCSANOW, termOrig);
void BarTermRestore () {
tcsetattr (STDIN_FILENO, TCSANOW, &restore);
}

6 changes: 2 additions & 4 deletions src/terminal.h
Expand Up @@ -24,9 +24,7 @@ THE SOFTWARE.
#ifndef SRC_TERMINAL_H_WY8F3MNH
#define SRC_TERMINAL_H_WY8F3MNH

void BarTermSetEcho (char);
void BarTermSetBuffer (char);
void BarTermSave (struct termios *);
void BarTermRestore (struct termios *termOrig);
void BarTermInit ();
void BarTermRestore ();

#endif /* SRC_TERMINAL_H_WY8F3MNH */

0 comments on commit 0117308

Please sign in to comment.