24 static int read_mouse();
38 if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
41 memset(mouse, 0, sizeof *mouse);
45 if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
46 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
50 set_mouse_bounds(get_screen_size());
56 if(mouse->dev_fd != -1) {
63 void set_mouse_bounds(const Rect &rect)
73 void process_mouse_event()
76 * - read all pending events from mouse fd (use O_NONBLOCK so that
77 * read will return -1 when there are no more events instead of blocking).
80 int prev_state = mouse->bnstate;
81 int prev_x = mouse->pointer_x;
82 int prev_y = mouse->pointer_y;
84 if(read_mouse() == -1) {
89 if(!(top = wm->get_grab_window())) {
90 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
92 wm->set_focused_window(top);
95 wm->set_focused_window(0);
99 /* - send each pointer move and button press/release to the topmost window
100 * with the pointer on it.
103 int dx = mouse->pointer_x - prev_x;
104 int dy = mouse->pointer_y - prev_y;
106 if((dx || dy) && top) {
107 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
108 if(motion_callback) {
109 Rect rect = top->get_absolute_rect();
110 motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
114 MouseButtonFuncType button_callback;
115 if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
116 int num_bits = sizeof mouse->bnstate * CHAR_BIT;
117 for(int i=0; i<num_bits; i++) {
118 int s = (mouse->bnstate >> i) & 1;
119 int prev_s = (prev_state >> i) & 1;
121 Rect rect = top->get_absolute_rect();
122 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
128 void get_pointer_pos(int *x, int *y)
130 *x = mouse->pointer_x;
131 *y = mouse->pointer_y;
134 int get_button_state()
136 return mouse->bnstate;
139 int get_button(int bn)
141 if(bn < 0 || bn >= 3) {
144 return (mouse->bnstate & (1 << bn)) != 0;
147 static int read_mouse()
150 signed char state[3] = {0, 0, 0};
152 if((rd = read(mouse->dev_fd, state, 3)) == -1) {
153 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
157 mouse->bnstate = state[0] & 7;
158 mouse->pointer_x += state[1];
159 mouse->pointer_y -= state[2];
161 if(mouse->pointer_x < mouse->bounds.x) {
162 mouse->pointer_x = mouse->bounds.x;
165 if(mouse->pointer_y < mouse->bounds.y) {
166 mouse->pointer_y = mouse->bounds.y;
169 if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
170 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
173 if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
174 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
179 #endif // WINNIE_FBDEV