23 static int dev_fd = -1; // file descriptor for /dev/psaux
25 static int pointer_x, pointer_y;
30 if((dev_fd = open("/dev/psaux", O_RDONLY)) == -1) {
31 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
35 set_mouse_bounds(get_screen_size());
47 void set_mouse_bounds(const Rect &rect)
57 void process_mouse_event()
60 * - read all pending events from mouse fd (use O_NONBLOCK so that
61 * read will return -1 when there are no more events instead of blocking).
64 int prev_state = bnstate;
65 int prev_x = pointer_x;
66 int prev_y = pointer_y;
68 if(read_mouse() == -1) {
72 Window *top = wm->get_window_at_pos(pointer_x, pointer_y);
74 wm->set_focused_window(top);
77 wm->set_focused_window(0);
81 /* - send each pointer move and button press/release to the topmost window
82 * with the pointer on it.
85 int dx = pointer_x - prev_x;
86 int dy = pointer_y - prev_y;
89 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
91 Rect rect = top->get_rect();
92 motion_callback(top, pointer_x - rect.x, pointer_y - rect.y);
96 MouseButtonFuncType button_callback = top->get_mouse_button_callback();
97 if(button_callback && (bnstate != prev_state)) {
98 int num_bits = sizeof bnstate * CHAR_BIT;
99 for(int i=0; i<num_bits; i++) {
100 int s = (bnstate >> i) & 1;
101 int prev_s = (prev_state >> i) & 1;
103 button_callback(top, i, s);
108 unsigned char *fb = get_framebuffer();
109 Rect scr = get_screen_size();
110 fb += (scr.width * pointer_y + pointer_x) * 4;
116 void get_pointer_pos(int *x, int *y)
122 int get_button_state(int bn)
127 int get_button(int bn)
129 if(bn < 0 || bn >= 3) {
132 return (bnstate & (1 << bn)) != 0;
139 signed char state[3] = {0, 0, 0};
141 if((rd = read(dev_fd, state, 3)) == -1) {
142 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
146 bnstate = state[0] & 7;
147 pointer_x += state[1];
148 pointer_y -= state[2];
150 if(pointer_x < bounds.x) {
151 pointer_x = bounds.x;
154 if(pointer_y < bounds.y) {
155 pointer_y = bounds.y;
158 if(pointer_x > bounds.x + bounds.width - 1) {
159 pointer_x = bounds.x + bounds.width - 1;
162 if(pointer_y > bounds.y + bounds.height - 1) {
163 pointer_y = bounds.y + bounds.height - 1;