summaryrefslogtreecommitdiff
path: root/src/terminal.c
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2014-06-16 15:04:08 +0200
committerLars-Dominik Braun <lars@6xq.net>2014-06-16 15:04:08 +0200
commit0117308ab5509a5c31b7778f9551f7335c553f77 (patch)
treef593cb6ced9ce3e7e246545f5db8e7410de28e0e /src/terminal.c
parent8cc95de0834c5217b080ad25907501f5ad152f6e (diff)
downloadpianobar-0117308ab5509a5c31b7778f9551f7335c553f77.tar.gz
pianobar-0117308ab5509a5c31b7778f9551f7335c553f77.tar.bz2
pianobar-0117308ab5509a5c31b7778f9551f7335c553f77.zip
Re-init terminal when awaking from ^Z
And simplify code that nobody touched in a long long timeā€¦ Fixes input issues reported in #458.
Diffstat (limited to 'src/terminal.c')
-rw-r--r--src/terminal.c67
1 files changed, 22 insertions, 45 deletions
diff --git a/src/terminal.c b/src/terminal.c
index 44513ce..2715d89 100644
--- a/src/terminal.c
+++ b/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
@@ -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);
}