*work in progress*
[winnie] / src / keyboard.cc
index 8933d42..810607a 100644 (file)
@@ -9,22 +9,22 @@
 #include <unistd.h>
 
 #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,50 @@ 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);
+       close(dev_fd);
 
-       tty_fd = -1;
+       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