mirror of
https://github.com/tbsdtv/media_build.git
synced 2025-07-23 04:13:02 +02:00
63 lines
1.2 KiB
C
63 lines
1.2 KiB
C
#include "kb.h"
|
|
|
|
struct termios orig_termios;
|
|
|
|
void reset_terminal_mode()
|
|
{
|
|
tcsetattr(0, TCSANOW, &orig_termios);
|
|
}
|
|
|
|
void set_conio_terminal_mode()
|
|
{
|
|
struct termios new_termios;
|
|
|
|
/* take two copies - one for now, one for later */
|
|
tcgetattr(0, &orig_termios);
|
|
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
|
|
|
|
/* register cleanup handler, and set the new terminal mode */
|
|
atexit(reset_terminal_mode);
|
|
cfmakeraw(&new_termios);
|
|
tcsetattr(0, TCSANOW, &new_termios);
|
|
}
|
|
|
|
int kbhit()
|
|
{
|
|
struct timeval tv = { 0L, 0L };
|
|
fd_set fds;
|
|
FD_SET(0, &fds);
|
|
return select(1, &fds, NULL, NULL, &tv);
|
|
}
|
|
|
|
int kbgetchar()
|
|
{
|
|
int r;
|
|
unsigned char c;
|
|
if ((r = read(0, &c, sizeof(c))) < 0) {
|
|
return r;
|
|
} else {
|
|
return c;
|
|
}
|
|
}
|
|
|
|
char getch(void)
|
|
{
|
|
char buf = 0;
|
|
struct termios old = {0};
|
|
if (tcgetattr(0, &old) < 0)
|
|
perror("tcsetattr()");
|
|
old.c_lflag &= ~ICANON;
|
|
old.c_lflag &= ~ECHO;
|
|
old.c_cc[VMIN] = 1;
|
|
old.c_cc[VTIME] = 0;
|
|
if (tcsetattr(0, TCSANOW, &old) < 0)
|
|
perror("tcsetattr ICANON");
|
|
if (read(0, &buf, 1) < 0)
|
|
perror ("read()");
|
|
old.c_lflag |= ICANON;
|
|
old.c_lflag |= ECHO;
|
|
if (tcsetattr(0, TCSADRAIN, &old) < 0)
|
|
perror ("tcsetattr ~ICANON");
|
|
return (buf);
|
|
}
|