X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fkeyboard.cc;h=1b23e1251afcfe442c5496f43e2b25601c5540e3;hb=4047a2dc058e7e54e4ff95311fb556ae8eeeedb9;hp=8933d4282c39a96cde761a808f3daaf8e1ac28b6;hpb=8a92836b3af157fd47c657cfe546887e5f5683a8;p=winnie diff --git a/src/keyboard.cc b/src/keyboard.cc index 8933d42..1b23e12 100644 --- a/src/keyboard.cc +++ b/src/keyboard.cc @@ -9,22 +9,22 @@ #include #include "keyboard.h" +#include "window.h" +#include "wm.h" -static int tty_fd = -1; +static int dev_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) { + if((dev_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) { + if(tcgetattr(dev_fd, &buf) < 0) { fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno)); return false; } @@ -34,10 +34,8 @@ bool init_keyboard() 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) { + if(tcsetattr(dev_fd, TCSAFLUSH, &buf) < 0) { return false; } @@ -49,35 +47,52 @@ void destroy_keyboard() { struct termios buf; + if(tcgetattr(dev_fd, &buf) < 0) { + fprintf(stderr, "Cannot get the tty parameters : %s\n", strerror(errno)); + } + 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(dev_fd, TCSAFLUSH, &buf) < 0) { + fprintf(stderr, "Cannot set the tty parameters : %s\n", strerror(errno)); + } ttystate = CANONICAL; - close(tty_fd); - tty_fd = -1; + if(dev_fd != -1) { + close(dev_fd); + dev_fd = -1; + } } int get_keyboard_fd() { - return tty_fd; + return dev_fd; } void process_keyboard_event() { char key; - if(read(tty_fd, &key, 1) < 1) { + if(read(dev_fd, &key, 1) < 1) { return; } if(key == 'q') { exit(0); } + + Window *focused_win = wm->get_focused_window(); + if(focused_win) { + KeyboardFuncType keyb_callback = focused_win->get_keyboard_callback(); + if(keyb_callback) { + keyb_callback(focused_win, key, true); + } + } + /* TODO: * - handle system-wide key combinations (alt-tab?) * - otherwise send keypress/release to focused window