*work in progress*
[winnie] / src / mouse.cc
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <termios.h>
9 #include <unistd.h>
10
11 #include "geom.h"
12 #include "gfx.h"
13 #include "mouse.h"
14
15 #define BN_LEFT         1
16 #define BN_RIGHT        2
17 #define BN_MIDDLE       4
18
19
20 static int dev_fd = -1; // file descriptor for /dev/psaux
21 static Rect bounds;
22 static int pointer_x, pointer_y;
23 static int bnstate;
24
25 bool init_mouse()
26 {
27         if((dev_fd = open("/dev/psaux", O_RDONLY)) == -1) {
28                 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
29                 return false;
30         }
31
32         set_mouse_bounds(get_screen_size());
33         return true;
34 }
35
36 void destroy_mouse()
37 {
38         if(dev_fd != -1) {
39                 close(dev_fd);
40                 dev_fd = -1;
41         }
42 }
43
44 void set_mouse_bounds(const Rect &rect)
45 {
46         bounds = rect;
47 }
48
49 int get_mouse_fd()
50 {
51         return dev_fd;
52 }
53
54 void process_mouse_event()
55 {
56         /* TODO:
57          * - read all pending events from mouse fd (use O_NONBLOCK so that
58          *   read will return -1 when there are no more events instead of blocking).
59          */
60
61         int prev_state = bnstate;
62         int prev_x = pointer_x;
63         int prev_y = pointer_y;
64
65         if(read_mouse() == -1) {
66                 return;
67         }
68
69         /* - process each event and update the pointer and button state
70          * - send each pointer move and button press/release to the topmost window
71          *   with the pointer on it.
72          */
73 }
74
75 void get_pointer_pos(int *x, int *y)
76 {
77         *x = pointer_x;
78         *y = pointer_y;
79 }
80
81 int get_button_state(int bn)
82 {
83         return bnstate;
84 }
85
86 int get_button(int bn)
87 {
88         if(bn < 0 || bn >= 3) {
89                 return 0;
90         }
91         return (bnstate & (1 << bn)) != 0;
92 }
93
94
95 int read_mouse()
96 {
97         int rd;
98         signed char state[3] = {0, 0, 0};
99
100         if((rd = read(dev_fd, state, 3)) == -1) {
101                 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
102                 return -1;
103         }
104
105         bnstate = state[0] & 7;
106         pointer_x += state[1];
107         pointer_y -= state[2];
108
109         if(pointer_x < bounds.x) {
110                 pointer_x = bounds.x;
111         }
112
113         if(pointer_y < bounds.y) {
114                 pointer_y = bounds.y;
115         }
116
117         if(pointer_x > bounds.x + bounds.width - 1) {
118                 pointer_x = bounds.x + bounds.width - 1;
119         }
120
121         if(pointer_y > bounds.y + bounds.height - 1) {
122                 pointer_y = bounds.y + bounds.height - 1;
123         }
124
125         return 0;
126 }