From ffd2c0a0f8b898cb4199a0c63aff255a85cc0f11 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 16 Feb 2013 19:48:21 +0200 Subject: [PATCH] *work in progress* win + keyb events work --- src/event.cc | 8 ++++++-- src/gfx.cc | 1 + src/keyboard.cc | 41 +++++++++++++++++++++++++++-------------- src/main.cc | 4 +--- src/mouse.cc | 1 + src/window.cc | 20 ++++++++++++++++++++ src/window.h | 5 +++++ src/winnie.cc | 6 ++++++ src/wm.cc | 22 ++++++++++++++++++++++ src/wm.h | 5 +++++ 10 files changed, 94 insertions(+), 19 deletions(-) diff --git a/src/event.cc b/src/event.cc index 4b80d04..45eed6e 100644 --- a/src/event.cc +++ b/src/event.cc @@ -1,6 +1,9 @@ +#include + #include #include #include + #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(); - } + }*/ } } diff --git a/src/gfx.cc b/src/gfx.cc index 7b3513d..87b5939 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -58,6 +58,7 @@ bool init_gfx() void destroy_gfx() { + clear_screen(0, 0, 0); close(dev_fd); dev_fd = -1; diff --git a/src/keyboard.cc b/src/keyboard.cc index 8933d42..810607a 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,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 diff --git a/src/main.cc b/src/main.cc index db00cb5..171f803 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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(); } diff --git a/src/mouse.cc b/src/mouse.cc index c83377c..47a4c33 100644 --- a/src/mouse.cc +++ b/src/mouse.cc @@ -28,6 +28,7 @@ bool init_mouse() void destroy_mouse() { close(dev_fd); + dev_fd = -1; } void set_mouse_bounds(const Rect &rect) diff --git a/src/window.cc b/src/window.cc index af30e3c..880a6ed 100644 --- a/src/window.cc +++ b/src/window.cc @@ -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; +} diff --git a/src/window.h b/src/window.h index 7bccd2c..f61247d 100644 --- a/src/window.h +++ b/src/window.h @@ -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; }; diff --git a/src/winnie.cc b/src/winnie.cc index 4f068ba..37b6222 100644 --- a/src/winnie.cc +++ b/src/winnie.cc @@ -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(); } diff --git a/src/wm.cc b/src/wm.cc index c0fab3c..1b283fc 100644 --- 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; +} diff --git a/src/wm.h b/src/wm.h index 88a2178..dfac319 100644 --- a/src/wm.h +++ b/src/wm.h @@ -12,6 +12,7 @@ private: std::list 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; -- 1.7.10.4