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