23 static int read_mouse();
37 if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
43 if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
44 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
48 set_mouse_bounds(get_screen_size());
54 if(mouse->dev_fd != -1) {
61 void set_mouse_bounds(const Rect &rect)
71 void process_mouse_event()
74 * - read all pending events from mouse fd (use O_NONBLOCK so that
75 * read will return -1 when there are no more events instead of blocking).
78 int prev_state = mouse->bnstate;
79 int prev_x = mouse->pointer_x;
80 int prev_y = mouse->pointer_y;
82 if(read_mouse() == -1) {
87 if(!(top = wm->get_grab_window())) {
88 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
90 wm->set_focused_window(top);
93 wm->set_focused_window(0);
97 /* - send each pointer move and button press/release to the topmost window
98 * with the pointer on it.
101 int dx = mouse->pointer_x - prev_x;
102 int dy = mouse->pointer_y - prev_y;
104 if((dx || dy) && top) {
105 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
106 if(motion_callback) {
107 Rect rect = top->get_absolute_rect();
108 motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
112 MouseButtonFuncType button_callback;
113 if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
114 int num_bits = sizeof mouse->bnstate * CHAR_BIT;
115 for(int i=0; i<num_bits; i++) {
116 int s = (mouse->bnstate >> i) & 1;
117 int prev_s = (prev_state >> i) & 1;
119 Rect rect = top->get_absolute_rect();
120 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
126 void get_pointer_pos(int *x, int *y)
128 *x = mouse->pointer_x;
129 *y = mouse->pointer_y;
132 int get_button_state()
134 return mouse->bnstate;
137 int get_button(int bn)
139 if(bn < 0 || bn >= 3) {
142 return (mouse->bnstate & (1 << bn)) != 0;
145 static int read_mouse()
148 signed char state[3] = {0, 0, 0};
150 if((rd = read(mouse->dev_fd, state, 3)) == -1) {
151 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
155 mouse->bnstate = state[0] & 7;
156 mouse->pointer_x += state[1];
157 mouse->pointer_y -= state[2];
159 if(mouse->pointer_x < mouse->bounds.x) {
160 mouse->pointer_x = mouse->bounds.x;
163 if(mouse->pointer_y < mouse->bounds.y) {
164 mouse->pointer_y = mouse->bounds.y;
167 if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
168 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
171 if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
172 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
177 #endif // WINNIE_FBDEV