X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fmouse.cc;h=db87be7a43cdd63fb0ed79f7c062e1124de26e7f;hb=5deac1a20d178aa7d2e8bb5cbc79b6584c6287f5;hp=c46181037f39ae95f93a81856443e91fc3a91643;hpb=8a92836b3af157fd47c657cfe546887e5f5683a8;p=winnie diff --git a/src/mouse.cc b/src/mouse.cc index c461810..db87be7 100644 --- a/src/mouse.cc +++ b/src/mouse.cc @@ -1,19 +1,44 @@ -#include "mouse.h" +#include +#include +#include +#include + +#include +#include +#include +#include + #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() { - // TODO open /dev/psaux (see O_NONBLOCK comment below) + 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() { - // TODO close /dev/psaux + if(dev_fd != -1) { + close(dev_fd); + dev_fd = -1; + } } void set_mouse_bounds(const Rect &rect) @@ -31,8 +56,18 @@ 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 - * - send each pointer move and button press/release to the tompost window + */ + + 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 topmost window * with the pointer on it. */ } @@ -45,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; }