From 52e08b5a5e7d44271d217892372c6c0878484c44 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sun, 17 Feb 2013 14:07:11 +0200 Subject: [PATCH] *work in progress* quick backup added get_window_at_pos (TODO set focused window - send mouse event) --- src/gfx.cc | 6 ++++- src/keyboard.cc | 10 +++++--- src/main.cc | 4 +-- src/mouse.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++----- src/mouse.h | 3 +++ src/window.cc | 11 +++++++++ src/window.h | 1 + src/wm.cc | 15 ++++++++++++ src/wm.h | 2 ++ 9 files changed, 112 insertions(+), 13 deletions(-) diff --git a/src/gfx.cc b/src/gfx.cc index 87b5939..3de2e1d 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -59,7 +59,11 @@ bool init_gfx() void destroy_gfx() { clear_screen(0, 0, 0); - close(dev_fd); + + if(dev_fd != -1) { + close(dev_fd); + } + dev_fd = -1; munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth)); diff --git a/src/keyboard.cc b/src/keyboard.cc index 810607a..6275227 100644 --- a/src/keyboard.cc +++ b/src/keyboard.cc @@ -62,9 +62,11 @@ void destroy_keyboard() } ttystate = CANONICAL; - close(dev_fd); - dev_fd = -1; + if(dev_fd != -1) { + close(dev_fd); + dev_fd = -1; + } } int get_keyboard_fd() @@ -79,9 +81,9 @@ void process_keyboard_event() return; } - if(key == 'q') { +/* if(key == 'q') { exit(0); - } + }*/ Window *focused_win = wm->get_focused_window(); if(focused_win) { diff --git a/src/main.cc b/src/main.cc index 171f803..da43b53 100644 --- a/src/main.cc +++ b/src/main.cc @@ -15,7 +15,7 @@ int main() Window *win1 = new Window; win1->set_title("title1"); win1->move(5, 10); - win1->resize(600, 800); + win1->resize(200, 300); win1->set_display_callback(display); win1->set_keyboard_callback(keyboard); @@ -28,7 +28,7 @@ int main() static void display(Window *win) { - fill_rect(win->get_rect(), 0, 0, 0); + fill_rect(win->get_rect(), 106, 106, 250); } static void keyboard(Window *win, int key, bool pressed) diff --git a/src/mouse.cc b/src/mouse.cc index 47a4c33..80d52be 100644 --- a/src/mouse.cc +++ b/src/mouse.cc @@ -8,27 +8,37 @@ #include #include -#include "mouse.h" #include "geom.h" +#include "gfx.h" +#include "mouse.h" + +#define BN_LEFT 1 +#define BN_RIGHT 2 +#define BN_MIDDLE 4 + static int dev_fd = -1; // file descriptor for /dev/psaux static Rect bounds; static int pointer_x, pointer_y; +static int bnstate; bool init_mouse() { - if((dev_fd = open("/dev/psaux", O_NONBLOCK)) == -1) { + if((dev_fd = open("/dev/psaux", O_RDONLY)) == -1) { fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno)); return false; } + set_mouse_bounds(get_screen_size()); return true; } void destroy_mouse() { - close(dev_fd); - dev_fd = -1; + if(dev_fd != -1) { + close(dev_fd); + dev_fd = -1; + } } void set_mouse_bounds(const Rect &rect) @@ -46,7 +56,17 @@ void process_mouse_event() /* TODO: * - read all pending events from mouse fd (use O_NONBLOCK so that * read will return -1 when there are no more events instead of blocking). - * - process each event and update the pointer and button state + */ + + int prev_state = bnstate; + int prev_x = pointer_x; + int prev_y = pointer_y; + + if(read_mouse() == -1) { + return; + } + + /* - process each event and update the pointer and button state * - send each pointer move and button press/release to the tompost window * with the pointer on it. */ @@ -60,6 +80,47 @@ void get_pointer_pos(int *x, int *y) int get_button_state(int bn) { - // TODO + return bnstate; +} + +int get_button(int bn) +{ + if(bn < 0 || bn >= 3) { + return 0; + } + return (bnstate & (1 << bn)) != 0; +} + + +int read_mouse() +{ + int rd; + signed char state[3] = {0, 0, 0}; + + if((rd = read(dev_fd, state, 3)) == -1) { + fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno)); + return -1; + } + + bnstate = state[0] & 7; + pointer_x += state[1]; + pointer_y -= state[2]; + + if(pointer_x < bounds.x) { + pointer_x = bounds.x; + } + + if(pointer_y < bounds.y) { + pointer_y = bounds.y; + } + + if(pointer_x > bounds.x + bounds.width - 1) { + pointer_x = bounds.x + bounds.width - 1; + } + + if(pointer_y > bounds.y + bounds.height - 1) { + pointer_y = bounds.y + bounds.height - 1; + } + return 0; } diff --git a/src/mouse.h b/src/mouse.h index 535f0d4..271ee83 100644 --- a/src/mouse.h +++ b/src/mouse.h @@ -13,5 +13,8 @@ void process_mouse_event(); void get_pointer_pos(int *x, int *y); int get_button_state(int bn); +int get_button(int bn); + +int read_mouse(); #endif // MOUSE_H_ diff --git a/src/window.cc b/src/window.cc index 880a6ed..f3937f1 100644 --- a/src/window.cc +++ b/src/window.cc @@ -22,6 +22,17 @@ const Rect &Window::get_rect() const return rect; } +bool Window::contains_ptr(int ptr_x, int ptr_y) +{ + if((rect.x <= ptr_x) && ((rect.x + rect.width) >= ptr_x)) { + if((rect.y <= ptr_y) && (ptr_y <= (rect.y + rect.height))) { + return true; + } + } + + return false; +} + void Window::move(int x, int y) { invalidate(); // moved, should redraw, MUST BE CALLED FIRST diff --git a/src/window.h b/src/window.h index f61247d..dae3a42 100644 --- a/src/window.h +++ b/src/window.h @@ -17,6 +17,7 @@ public: ~Window(); const Rect &get_rect() const; + bool contains_ptr(int ptr_x, int ptr_y); void move(int x, int y); void resize(int x, int y); diff --git a/src/wm.cc b/src/wm.cc index 1b283fc..7fe5986 100644 --- a/src/wm.cc +++ b/src/wm.cc @@ -74,3 +74,18 @@ Window *WindowManager::get_focused_window() { return focused_win; } + +Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y) +{ + Window *win = new Window; + std::list::reverse_iterator rit = windows.rbegin(); + while(rit != windows.rend()) { + if((*rit)->contains_ptr(pointer_x, pointer_y)) { + win = *rit; + break; + } + rit++; + } + + return win; +} diff --git a/src/wm.h b/src/wm.h index dfac319..fc10953 100644 --- a/src/wm.h +++ b/src/wm.h @@ -25,6 +25,8 @@ public: void set_focused_window(Window *win); const Window *get_focused_window() const; Window *get_focused_window(); + + Window *get_window_at_pos(int pointer_x, int pointer_y); }; extern WindowManager *wm; -- 1.7.10.4