Program Listing for File keyboard.cpp¶
↰ Return to documentation for file (lib/utilities/keyboard.cpp
)
// ---------------------------------------------------------------------
// This file is part of falcon-core.
//
// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
//
// Falcon-server is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Falcon-server is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
// ---------------------------------------------------------------------
#include <signal.h>
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>
#include "keyboard.hpp"
int s_interrupted = 0;
int kbhit() {
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); // STDIN_FILENO is 0
select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
void nonblock(int state) {
struct termios ttystate;
// get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
if (state == 1) {
// turn off canonical mode
ttystate.c_lflag &= ~ICANON;
// turn off echo
ttystate.c_lflag &= ~ECHO;
// minimum of number input read.
ttystate.c_cc[VMIN] = 1;
} else if (state == 0) {
// turn on canonical mode
ttystate.c_lflag |= ICANON;
// turn on echo
ttystate.c_lflag |= ECHO;
}
// set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
void s_signal_handler(int signal_value) { s_interrupted = 1; }
void s_catch_sigint_signal(void) {
struct sigaction action;
action.sa_handler = s_signal_handler;
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
}