/*
|
* Copyright (C) 2019 Hertz Wang 1989wanghang@163.com
|
*
|
* This program 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.
|
*
|
* This program 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 this program; if not, see http://www.gnu.org/licenses
|
*
|
* Any non-GPL usage of this software or parts of this software is strictly
|
* forbidden.
|
*
|
*/
|
|
#include <assert.h>
|
#include <fcntl.h>
|
#include <poll.h>
|
#include <stdio.h>
|
#include <sys/stat.h>
|
#include <sys/types.h>
|
#include <sys/uio.h>
|
#include <termios.h>
|
#include <unistd.h>
|
|
static struct termios oldtty;
|
static int restore_tty = 0;
|
// termios.h
|
int read_key() {
|
unsigned char ch;
|
int n = 1;
|
struct timeval tv;
|
fd_set rfds;
|
|
FD_ZERO(&rfds);
|
FD_SET(0, &rfds);
|
tv.tv_sec = 0;
|
tv.tv_usec = 0;
|
n = select(1, &rfds, NULL, NULL, &tv);
|
if (n > 0) {
|
n = read(0, &ch, 1);
|
if (n == 1)
|
return ch;
|
|
return n;
|
}
|
return -1;
|
}
|
|
void term_init() {
|
struct termios tty;
|
if (tcgetattr(0, &tty) == 0) {
|
oldtty = tty;
|
restore_tty = 1;
|
|
tty.c_iflag &=
|
~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
tty.c_oflag |= OPOST;
|
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
|
tty.c_cflag &= ~(CSIZE | PARENB);
|
tty.c_cflag |= CS8;
|
tty.c_cc[VMIN] = 1;
|
tty.c_cc[VTIME] = 0;
|
tcsetattr(0, TCSANOW, &tty);
|
}
|
}
|
|
void term_deinit() {
|
if (restore_tty)
|
tcsetattr(0, TCSANOW, &oldtty);
|
}
|