2020-12-22 14:19:32 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2021-01-07 15:55:29 -08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2020-12-22 14:19:32 -08:00
|
|
|
|
|
|
|
/* reads from keypress, doesn't echo */
|
2021-01-07 13:56:08 -08:00
|
|
|
int _getch(void)
|
2020-12-22 14:19:32 -08:00
|
|
|
{
|
|
|
|
int ch;
|
2021-01-07 15:55:29 -08:00
|
|
|
|
|
|
|
setRawMode();
|
|
|
|
atexit(setNormalMode);
|
|
|
|
|
2020-12-22 14:19:32 -08:00
|
|
|
ch = getchar();
|
2021-01-07 15:55:29 -08:00
|
|
|
|
|
|
|
setNormalMode();
|
|
|
|
|
2020-12-22 14:19:32 -08:00
|
|
|
return ch;
|
2021-01-07 13:56:08 -08:00
|
|
|
}
|
|
|
|
|
2021-01-07 14:13:05 -08:00
|
|
|
int _ungetch(char ch)
|
2021-01-07 13:56:08 -08:00
|
|
|
{
|
|
|
|
return ungetc(ch, stdin);
|
2020-12-22 14:19:32 -08:00
|
|
|
}
|