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