32 |
void raw(void) |
void raw(void) |
33 |
{ |
{ |
34 |
static char init; |
static char init; |
35 |
/**/ |
/**/ |
36 |
struct termios new_kbd_mode; |
struct termios new_kbd_mode; |
37 |
|
|
38 |
if(init) |
if (init) |
39 |
return; |
return; |
40 |
/* put keyboard (stdin, actually) in raw, unbuffered mode */ |
/* put keyboard (stdin, actually) in raw, unbuffered mode */ |
41 |
tcgetattr(0, &g_old_kbd_mode); |
tcgetattr(0, &g_old_kbd_mode); |
42 |
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios)); |
memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios)); |
43 |
new_kbd_mode.c_lflag &= ~(ICANON | ECHO); |
new_kbd_mode.c_lflag &= ~(ICANON | ECHO); |
44 |
new_kbd_mode.c_cc[VTIME] = 0; |
new_kbd_mode.c_cc[VTIME] = 0; |
45 |
new_kbd_mode.c_cc[VMIN] = 1; |
new_kbd_mode.c_cc[VMIN] = 1; |
46 |
tcsetattr(0, TCSANOW, &new_kbd_mode); |
tcsetattr(0, TCSANOW, &new_kbd_mode); |
47 |
/* when we exit, go back to normal, "cooked" mode */ |
/* when we exit, go back to normal, "cooked" mode */ |
48 |
atexit(cooked); |
atexit(cooked); |
49 |
|
|
50 |
init = 1; |
init = 1; |
58 |
int status; |
int status; |
59 |
|
|
60 |
raw(); |
raw(); |
61 |
/* check stdin (fd 0) for activity */ |
/* check stdin (fd 0) for activity */ |
62 |
FD_ZERO(&read_handles); |
FD_ZERO(&read_handles); |
63 |
FD_SET(0, &read_handles); |
FD_SET(0, &read_handles); |
64 |
timeout.tv_sec = timeout.tv_usec = 0; |
timeout.tv_sec = timeout.tv_usec = 0; |
65 |
status = select(0 + 1, &read_handles, NULL, NULL, &timeout); |
status = select(0 + 1, &read_handles, NULL, NULL, &timeout); |
66 |
if(status < 0) |
if (status < 0) |
67 |
{ |
{ |
68 |
printf("select() failed in kbhit()\n"); |
printf("select() failed in kbhit()\n"); |
69 |
exit(1); |
exit(1); |
77 |
unsigned char temp; |
unsigned char temp; |
78 |
|
|
79 |
raw(); |
raw(); |
80 |
/* stdin = fd 0 */ |
/* stdin = fd 0 */ |
81 |
if(read(0, &temp, 1) != 1) |
if (read(0, &temp, 1) != 1) |
82 |
return 0; |
return 0; |
83 |
return temp; |
return temp; |
84 |
} |
} |