Organized getch.c a little more

This commit is contained in:
R. Eric Wheeler 2021-01-07 15:55:29 -08:00
parent d50a05a566
commit 382ca844d5
2 changed files with 25 additions and 7 deletions

View File

@ -1,19 +1,37 @@
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
static struct termios oldattr;
static void setRawMode(void)
{
struct termios newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
cfmakeraw(&newattr);
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
}
static void setNormalMode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
}
/* reads from keypress, doesn't echo */
int _getch(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
cfmakeraw(&newattr);
//newattr.c_lflag &= ~( ICANON | ECHO );
//tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
setRawMode();
atexit(setNormalMode);
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
setNormalMode();
return ch;
}

Binary file not shown.