22 #include <core/exception.h> 23 #include <utils/system/getkey.h> 43 oldflags = fcntl(STDIN_FILENO, F_GETFL, 0);
44 oldflags |= O_NONBLOCK;
45 fcntl(STDIN_FILENO, F_SETFL, oldflags);
54 oldflags = fcntl(STDIN_FILENO, F_GETFL, 0);
55 oldflags &= ~O_NONBLOCK;
56 fcntl(STDIN_FILENO, F_SETFL, oldflags);
70 bool blocking = (timeout_decisecs != 0);
71 char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
77 tcgetattr(STDIN_FILENO, &saved_attributes);
79 tcgetattr(STDIN_FILENO, &tattr);
80 tattr.c_lflag &= ~(ICANON);
81 tattr.c_lflag &= ~(ECHO);
82 if (timeout_decisecs < 0) {
84 tattr.c_cc[VTIME] = 0;
85 }
else if (timeout_decisecs > 0) {
87 tattr.c_cc[VTIME] = timeout_decisecs;
90 tattr.c_cc[VTIME] = 0;
92 tcsetattr(STDIN_FILENO, TCSANOW, &tattr);
94 ssize_t read_bytes = read(STDIN_FILENO, buf, 1);
96 tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
100 if (read_bytes == 1) {
102 }
else if (read_bytes < 0) {
103 throw Exception(errno,
"Failed to read key from keyboard (getkey)");
static void set_nonblock_flag()
Set non-blocking flag on STDIN.
Fawkes library namespace.
char getkey(int timeout_decisecs)
Get value of a single key-press non-blocking.
Base class for exceptions in Fawkes.
static void clear_nonblock_flag()
Clear non-blocking flag on STDIN.