X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fkeyboard.cc;fp=src%2Fkeyboard.cc;h=8933d4282c39a96cde761a808f3daaf8e1ac28b6;hb=8a92836b3af157fd47c657cfe546887e5f5683a8;hp=0000000000000000000000000000000000000000;hpb=fb1f0ff6ab479c68cbac5c2d55de5c2beb229efd;p=winnie diff --git a/src/keyboard.cc b/src/keyboard.cc new file mode 100644 index 0000000..8933d42 --- /dev/null +++ b/src/keyboard.cc @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "keyboard.h" + +static int tty_fd = -1; +static enum {RAW, CANONICAL} ttystate = CANONICAL; + +static int keyb_fd = -1; + +bool init_keyboard() +{ + if((tty_fd = open("/dev/tty", O_RDWR)) == -1) { + fprintf(stderr, "Cannot open /dev/tty : %s\n", strerror(errno)); + return false; + } + + struct termios buf; + + if(tcgetattr(tty_fd, &buf) < 0) { + fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno)); + return false; + } + + buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); + buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + buf.c_cflag &= ~(CSIZE | PARENB); + buf.c_cflag |= CS8; + buf.c_oflag &= ~(OPOST); + buf.c_cc[VMIN] = 1; + buf.c_cc[VTIME] = 0; + + if(tcsetattr(tty_fd, TCSAFLUSH, &buf) < 0) { + return false; + } + + ttystate = RAW; + return true; +} + +void destroy_keyboard() +{ + struct termios buf; + + buf.c_lflag |= (ECHO | ICANON | IEXTEN | ISIG); + buf.c_iflag |= (BRKINT | ICRNL | INPCK | ISTRIP | IXON); + buf.c_cflag |= (CSIZE | PARENB); + buf.c_cflag &= CS8; + buf.c_oflag |= (OPOST); + buf.c_cc[VMIN] = 1; + buf.c_cc[VTIME] = 0; + + ttystate = CANONICAL; + close(tty_fd); + + tty_fd = -1; +} + +int get_keyboard_fd() +{ + return tty_fd; +} + +void process_keyboard_event() +{ + char key; + if(read(tty_fd, &key, 1) < 1) { + return; + } + + if(key == 'q') { + exit(0); + } + /* TODO: + * - handle system-wide key combinations (alt-tab?) + * - otherwise send keypress/release to focused window + */ +}