Skip to content
Snippets Groups Projects
Commit 2ee25082 authored by rswindell's avatar rswindell
Browse files

kbhit() doesn't call select() if input is not a tty

(select returns 1 on /dev/null).
Changed more uses of 0 constant to STDIN_FILENO (defined unistd.h).
Changed C++ comments to C-style.
parent a5a3ab4c
No related branches found
No related tags found
No related merge requests found
/* conwrap.c */ /* conwrap.c */
/* -- To give DOS's getch() function to Unix - Casey Martin 2000 */ /* DOS's kbhit and getch functions for Unix - Casey Martin 2000 */
/* $Id$ */ /* $Id$ */
...@@ -53,26 +53,17 @@ ...@@ -53,26 +53,17 @@
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include "conwrap.h" // Verify prototypes #include "conwrap.h" /* Verify prototypes */
static struct termios current; // our current term settings static struct termios current; /* our current term settings */
static struct termios original; // old termios settings static struct termios original; /* old termios settings */
static int beensetup = 0; // has _termios_setup() been called? static int beensetup = 0; /* has _termios_setup() been called? */
static int istty = 0; /* is stdin a tty? */
/*
I'm using a variable function here simply for the sake of speed. The
termios functions must be called before a kbhit() can be successful, so
on the first call, we just set up the terminal, point to variable function
to kbhit_norm(), and then call the new function. Otherwise, testing would
be required on every call to determine if termios has already been setup.
Maybe I'm being way too anal, though.
*/
/* Resets the termios to its previous state */ /* Resets the termios to its previous state */
void _termios_reset(void) void _termios_reset(void)
{ {
tcsetattr(0, TCSANOW, &original); tcsetattr(STDIN_FILENO, TCSANOW, &original);
} }
/************************************************ /************************************************
...@@ -83,10 +74,10 @@ void _termios_reset(void) ...@@ -83,10 +74,10 @@ void _termios_reset(void)
#endif #endif
void _sighandler_stop(int sig) void _sighandler_stop(int sig)
{ {
// clean up the terminal /* clean up the terminal */
_termios_reset(); _termios_reset();
// ... and stop /* ... and stop */
kill(getpid(), SIGSTOP); kill(getpid(), SIGSTOP);
} }
#if defined(__BORLANDC__) #if defined(__BORLANDC__)
...@@ -94,8 +85,8 @@ void _sighandler_stop(int sig) ...@@ -94,8 +85,8 @@ void _sighandler_stop(int sig)
#endif #endif
void _sighandler_cont(int sig) void _sighandler_cont(int sig)
{ {
// restore terminal /* restore terminal */
tcsetattr(0, TCSANOW, &current); tcsetattr(STDIN_FILENO, TCSANOW, &current);
} }
...@@ -107,17 +98,18 @@ void _termios_setup(void) ...@@ -107,17 +98,18 @@ void _termios_setup(void)
tcgetattr(STDIN_FILENO, &original); tcgetattr(STDIN_FILENO, &original);
memcpy(&current, &original, sizeof(struct termios)); memcpy(&current, &original, sizeof(struct termios));
current.c_cc[VMIN] = 1; // read() will return with one char current.c_cc[VMIN] = 1; /* read() will return with one char */
current.c_cc[VTIME] = 0; // read() blocks forever current.c_cc[VTIME] = 0; /* read() blocks forever */
current.c_lflag &= ~ICANON; // character mode current.c_lflag &= ~ICANON; /* character mode */
current.c_lflag &= ~ECHO; // turn off echoing current.c_lflag &= ~ECHO; /* turn off echoing */
tcsetattr(STDIN_FILENO, TCSANOW, &current); tcsetattr(STDIN_FILENO, TCSANOW, &current);
// Let's install an exit function, also. This way, we can reset /* Let's install an exit function, also. This way, we can reset
// the termios silently * the termios silently
*/
atexit(_termios_reset); atexit(_termios_reset);
// install the Ctrl-Z handler /* install the Ctrl-Z handler */
signal(SIGSTOP, _sighandler_stop); signal(SIGSTOP, _sighandler_stop);
signal(SIGCONT, _sighandler_cont); signal(SIGCONT, _sighandler_cont);
} }
...@@ -127,8 +119,15 @@ int kbhit(void) ...@@ -127,8 +119,15 @@ int kbhit(void)
{ {
fd_set inp; fd_set inp;
struct timeval timeout = {0, 0}; struct timeval timeout = {0, 0};
struct stat st;
if(!istty) {
istty = isatty(STDIN_FILENO);
if(!istty)
return 0;
}
// set up select() args /* set up select() args */
FD_ZERO(&inp); FD_ZERO(&inp);
FD_SET(STDIN_FILENO, &inp); FD_SET(STDIN_FILENO, &inp);
...@@ -141,15 +140,13 @@ int getch(void) ...@@ -141,15 +140,13 @@ int getch(void)
{ {
char c; char c;
if (!beensetup) if(!beensetup)
// I hate to test for this every time, but this shouldn't be
// called that often anyway...
_termios_setup(); _termios_setup();
// get a char out of stdin /* get a char out of stdin */
read(STDIN_FILENO, &c, 1); read(STDIN_FILENO, &c, 1);
return c; return c;
} }
#endif // __unix__ #endif /* __unix__ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment