803e8e0dcbeadbd36a6b9e6e144b05f1b1e6c2c5
[winnie] / src / fbdev / mouse.cc
1 #ifdef WINNIE_FBDEV
2 #include <errno.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <fcntl.h>
9 #include <sys/ioctl.h>
10 #include <termios.h>
11 #include <unistd.h>
12
13 #include "geom.h"
14 #include "gfx.h"
15 #include "mouse.h"
16 #include "window.h"
17 #include "wm.h"
18
19 #define BN_LEFT         1
20 #define BN_RIGHT        2
21 #define BN_MIDDLE       4
22
23 static int read_mouse();
24
25 struct Mouse {
26         int dev_fd;
27         Rect bounds;
28         int pointer_x;
29         int pointer_y;
30         int bnstate;
31 };
32
33 static Mouse *mouse;
34
35 bool init_mouse()
36 {
37         if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
38                 return false;
39         }
40
41         mouse->dev_fd = -1;
42
43         if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
44                 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
45                 return false;
46         }
47
48         set_mouse_bounds(get_screen_size());
49         return true;
50 }
51
52 void destroy_mouse()
53 {
54         if(mouse->dev_fd != -1) {
55                 close(mouse->dev_fd);
56                 mouse->dev_fd = -1;
57         }
58         free(mouse);
59 }
60
61 void set_mouse_bounds(const Rect &rect)
62 {
63         mouse->bounds = rect;
64 }
65
66 int get_mouse_fd()
67 {
68         return mouse->dev_fd;
69 }
70
71 void process_mouse_event()
72 {
73         /* TODO:
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).
76          */
77
78         int prev_state = mouse->bnstate;
79         int prev_x = mouse->pointer_x;
80         int prev_y = mouse->pointer_y;
81
82         if(read_mouse() == -1) {
83                 return;
84         }
85
86         Window *top;
87         if(!(top = wm->get_grab_window())) {
88                 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
89                 if(top) {
90                         wm->set_focused_window(top);
91                 }
92                 else {
93                         wm->set_focused_window(0);
94                 }
95         }
96
97          /* - send each pointer move and button press/release to the topmost window
98          *   with the pointer on it.
99          */
100
101         int dx = mouse->pointer_x - prev_x;
102         int dy = mouse->pointer_y - prev_y;
103
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);
109                 }
110         }
111
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;
118                         if(s != prev_s) {
119                                 Rect rect = top->get_absolute_rect();
120                                 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
121                         }
122                 }
123         }
124 }
125
126 void get_pointer_pos(int *x, int *y)
127 {
128         *x = mouse->pointer_x;
129         *y = mouse->pointer_y;
130 }
131
132 int get_button_state()
133 {
134         return mouse->bnstate;
135 }
136
137 int get_button(int bn)
138 {
139         if(bn < 0 || bn >= 3) {
140                 return 0;
141         }
142         return (mouse->bnstate & (1 << bn)) != 0;
143 }
144
145 static int read_mouse()
146 {
147         int rd;
148         signed char state[3] = {0, 0, 0};
149
150         if((rd = read(mouse->dev_fd, state, 3)) == -1) {
151                 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
152                 return -1;
153         }
154
155         mouse->bnstate = state[0] & 7;
156         mouse->pointer_x += state[1];
157         mouse->pointer_y -= state[2];
158
159         if(mouse->pointer_x < mouse->bounds.x) {
160                 mouse->pointer_x = mouse->bounds.x;
161         }
162
163         if(mouse->pointer_y < mouse->bounds.y) {
164                 mouse->pointer_y = mouse->bounds.y;
165         }
166
167         if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
168                 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
169         }
170
171         if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
172                 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
173         }
174
175         return 0;
176 }
177 #endif // WINNIE_FBDEV