24 static int read_mouse();
38 if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
44 if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
45 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
49 set_mouse_bounds(get_screen_size());
55 if(mouse->dev_fd != -1) {
62 void set_mouse_bounds(const Rect &rect)
72 void process_mouse_event()
75 * - read all pending events from mouse fd (use O_NONBLOCK so that
76 * read will return -1 when there are no more events instead of blocking).
79 int prev_state = mouse->bnstate;
80 int prev_x = mouse->pointer_x;
81 int prev_y = mouse->pointer_y;
83 if(read_mouse() == -1) {
88 if(!(top = wm->get_grab_window())) {
89 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
91 wm->set_focused_window(top);
94 wm->set_focused_window(0);
98 /* - send each pointer move and button press/release to the topmost window
99 * with the pointer on it.
102 int dx = mouse->pointer_x - prev_x;
103 int dy = mouse->pointer_y - prev_y;
105 if((dx || dy) && top) {
106 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
107 if(motion_callback) {
108 Rect rect = top->get_absolute_rect();
109 motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
113 MouseButtonFuncType button_callback;
114 if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
115 int num_bits = sizeof mouse->bnstate * CHAR_BIT;
116 for(int i=0; i<num_bits; i++) {
117 int s = (mouse->bnstate >> i) & 1;
118 int prev_s = (prev_state >> i) & 1;
120 Rect rect = top->get_absolute_rect();
121 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
127 void get_pointer_pos(int *x, int *y)
129 *x = mouse->pointer_x;
130 *y = mouse->pointer_y;
133 int get_button_state()
135 return mouse->bnstate;
138 int get_button(int bn)
140 if(bn < 0 || bn >= 3) {
143 return (mouse->bnstate & (1 << bn)) != 0;
146 static int read_mouse()
149 signed char state[3] = {0, 0, 0};
151 if((rd = read(mouse->dev_fd, state, 3)) == -1) {
152 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
156 mouse->bnstate = state[0] & 7;
157 mouse->pointer_x += state[1];
158 mouse->pointer_y -= state[2];
160 if(mouse->pointer_x < mouse->bounds.x) {
161 mouse->pointer_x = mouse->bounds.x;
164 if(mouse->pointer_y < mouse->bounds.y) {
165 mouse->pointer_y = mouse->bounds.y;
168 if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
169 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
172 if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
173 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
178 #endif // WINNIE_FBDEV