*work in progress*
authorEleni Maria Stea <elene.mst@gmail.com>
Sat, 16 Feb 2013 17:48:21 +0000 (19:48 +0200)
committerEleni Maria Stea <elene.mst@gmail.com>
Sat, 16 Feb 2013 17:48:21 +0000 (19:48 +0200)
win + keyb events work

src/event.cc
src/gfx.cc
src/keyboard.cc
src/main.cc
src/mouse.cc
src/window.cc
src/window.h
src/winnie.cc
src/wm.cc
src/wm.h

index 4b80d04..45eed6e 100644 (file)
@@ -1,6 +1,9 @@
+#include <stdio.h>
+
 #include <errno.h>
 #include <unistd.h>
 #include <sys/select.h>
+
 #include "event.h"
 #include "wm.h"
 #include "keyboard.h"
@@ -25,10 +28,11 @@ void process_events()
                while(select(maxfd + 1, &read_set, 0, 0, 0) == -1 && errno == EINTR);
 
                if(FD_ISSET(keyb_fd, &read_set)) {
+                       printf("WINNIE TODO PROCESS KEYB\n");
                        process_keyboard_event();
                }
-               if(FD_ISSET(mouse_fd, &read_set)) {
+/*             if(FD_ISSET(mouse_fd, &read_set)) {
                        process_mouse_event();
-               }
+               }*/
        }
 }
index 7b3513d..87b5939 100644 (file)
@@ -58,6 +58,7 @@ bool init_gfx()
 
 void destroy_gfx()
 {
+       clear_screen(0, 0, 0);
        close(dev_fd);
        dev_fd = -1;
 
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
index db00cb5..171f803 100644 (file)
@@ -24,8 +24,6 @@ int main()
        while(1) {
                process_events();
        }
-
-       winnie_shutdown();
 }
 
 static void display(Window *win)
@@ -43,5 +41,5 @@ static void keyboard(Window *win, int key, bool pressed)
 
 static void cleanup()
 {
-       destroy_gfx();
+       winnie_shutdown();
 }
index c83377c..47a4c33 100644 (file)
@@ -28,6 +28,7 @@ bool init_mouse()
 void destroy_mouse()
 {
        close(dev_fd);
+       dev_fd = -1;
 }
 
 void set_mouse_bounds(const Rect &rect)
index af30e3c..880a6ed 100644 (file)
@@ -93,3 +93,23 @@ void Window::set_mouse_motion_callback(MouseMotionFuncType func)
 {
        callbacks.motion = func;
 }
+
+const DisplayFuncType Window::get_display_callback() const
+{
+       return callbacks.display;
+}
+
+const KeyboardFuncType Window::get_keyboard_callback() const
+{
+       return callbacks.keyboard;
+}
+
+const MouseButtonFuncType Window::get_mouse_button_callback() const
+{
+       return callbacks.button;
+}
+
+const MouseMotionFuncType Window::get_mouse_motion_callback() const
+{
+       return callbacks.motion;
+}
index 7bccd2c..f61247d 100644 (file)
@@ -39,6 +39,11 @@ public:
        void set_mouse_button_callback(MouseButtonFuncType func);
        void set_mouse_motion_callback(MouseMotionFuncType func);
 
+       const DisplayFuncType get_display_callback() const;
+       const KeyboardFuncType get_keyboard_callback() const;
+       const MouseButtonFuncType get_mouse_button_callback() const;
+       const MouseMotionFuncType get_mouse_motion_callback() const;
+
        // XXX remove if not needed
        friend class WindowManager;
 };
index 4f068ba..37b6222 100644 (file)
@@ -1,4 +1,5 @@
 #include "winnie.h"
+#include "keyboard.h"
 
 bool winnie_init()
 {
@@ -6,6 +7,10 @@ bool winnie_init()
                return false;
        }
 
+       if(!init_keyboard()) {
+               return false;
+       }
+
        wm->invalidate_region(get_screen_size());
        return true;
 }
@@ -13,4 +18,5 @@ bool winnie_init()
 void winnie_shutdown()
 {
        destroy_gfx();
+       destroy_keyboard();
 }
index c0fab3c..1b283fc 100644 (file)
--- a/src/wm.cc
+++ b/src/wm.cc
@@ -14,6 +14,8 @@ WindowManager::WindowManager()
                throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
        }
 
+       focused_win = 0;
+
        bg_color[0] = 210;
        bg_color[1] = 106;
        bg_color[2] = 106;
@@ -45,10 +47,30 @@ void WindowManager::process_windows()
                if(intersect.width && intersect.height) {
                        (*it)->draw();
                }
+               it++;
        }
 }
 
 void WindowManager::add_window(Window *win)
 {
+       if(windows.empty()) {
+               focused_win = win;
+       }
+
        windows.push_back(win);
 }
+
+void WindowManager::set_focused_window(Window *win)
+{
+       focused_win = win;
+}
+
+const Window *WindowManager::get_focused_window() const
+{
+       return focused_win;
+}
+
+Window *WindowManager::get_focused_window()
+{
+       return focused_win;
+}
index 88a2178..dfac319 100644 (file)
--- a/src/wm.h
+++ b/src/wm.h
@@ -12,6 +12,7 @@ private:
        std::list<Rect> dirty_rects;
 
        int bg_color[3];
+       Window *focused_win;
 
 public:
        WindowManager();
@@ -20,6 +21,10 @@ public:
        void process_windows();
 
        void add_window(Window *win);
+
+       void set_focused_window(Window *win);
+       const Window *get_focused_window() const;
+       Window *get_focused_window();
 };
 
 extern WindowManager *wm;