casting in fbdev
[winnie] / libwinnie / src / fbdev / mouse.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #ifdef WINNIE_FBDEV
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <termios.h>
32 #include <unistd.h>
33
34 #include "geom.h"
35 #include "gfx.h"
36 #include "mouse.h"
37 #include "shalloc.h"
38 #include "window.h"
39 #include "winnie.h"
40 #include "wm.h"
41
42 #define BN_LEFT         1
43 #define BN_RIGHT        2
44 #define BN_MIDDLE       4
45
46 static int read_mouse();
47
48 struct Mouse {
49         int dev_fd;
50         Rect bounds;
51         int pointer_x;
52         int pointer_y;
53         int bnstate;
54 };
55
56 static Mouse *mouse;
57
58 bool init_mouse()
59 {
60         if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
61                 return false;
62         }
63         get_subsys()->mouse_offset = (int)((char*)mouse - (char*)get_pool());
64         memset(mouse, 0, sizeof *mouse);
65
66         mouse->dev_fd = -1;
67
68         if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
69                 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
70                 return false;
71         }
72
73         set_mouse_bounds(get_screen_size());
74         return true;
75 }
76
77 void destroy_mouse()
78 {
79         if(mouse->dev_fd != -1) {
80                 close(mouse->dev_fd);
81                 mouse->dev_fd = -1;
82         }
83         sh_free(mouse);
84 }
85
86 bool client_open_mouse(void *smem_start, int offset)
87 {
88         mouse = (Mouse*)((unsigned char*)smem_start + offset);
89         return true;
90 }
91
92 void client_close_mouse()
93 {
94 }
95
96 void set_mouse_bounds(const Rect &rect)
97 {
98         mouse->bounds = rect;
99 }
100
101 int get_mouse_fd()
102 {
103         return mouse->dev_fd;
104 }
105
106 void process_mouse_event()
107 {
108         /* TODO:
109          * - read all pending events from mouse fd (use O_NONBLOCK so that
110          *   read will return -1 when there are no more events instead of blocking).
111          */
112
113         int prev_state = mouse->bnstate;
114         int prev_x = mouse->pointer_x;
115         int prev_y = mouse->pointer_y;
116
117         if(read_mouse() == -1) {
118                 return;
119         }
120
121         Window *top;
122         if(!(top = wm->get_grab_window())) {
123                 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
124                 if(top) {
125                         wm->set_focused_window(top);
126                 }
127                 else {
128                         wm->set_focused_window(0);
129                 }
130         }
131
132          /* - send each pointer move and button press/release to the topmost window
133          *   with the pointer on it.
134          */
135
136         int dx = mouse->pointer_x - prev_x;
137         int dy = mouse->pointer_y - prev_y;
138
139         if((dx || dy) && top) {
140                 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
141                 if(motion_callback) {
142                         Rect rect = top->get_absolute_rect();
143                         motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
144                 }
145         }
146
147         MouseButtonFuncType button_callback;
148         if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
149                 int num_bits = sizeof mouse->bnstate * CHAR_BIT;
150                 for(int i=0; i<num_bits; i++) {
151                         int s = (mouse->bnstate >> i) & 1;
152                         int prev_s = (prev_state >> i) & 1;
153                         if(s != prev_s) {
154                                 Rect rect = top->get_absolute_rect();
155                                 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
156                         }
157                 }
158         }
159 }
160
161 void get_pointer_pos(int *x, int *y)
162 {
163         *x = mouse->pointer_x;
164         *y = mouse->pointer_y;
165 }
166
167 int get_button_state()
168 {
169         return mouse->bnstate;
170 }
171
172 int get_button(int bn)
173 {
174         if(bn < 0 || bn >= 3) {
175                 return 0;
176         }
177         return (mouse->bnstate & (1 << bn)) != 0;
178 }
179
180 static int read_mouse()
181 {
182         int rd;
183         signed char state[3] = {0, 0, 0};
184
185         if((rd = read(mouse->dev_fd, state, 3)) == -1) {
186                 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
187                 return -1;
188         }
189
190         mouse->bnstate = state[0] & 7;
191         mouse->pointer_x += state[1];
192         mouse->pointer_y -= state[2];
193
194         if(mouse->pointer_x < mouse->bounds.x) {
195                 mouse->pointer_x = mouse->bounds.x;
196         }
197
198         if(mouse->pointer_y < mouse->bounds.y) {
199                 mouse->pointer_y = mouse->bounds.y;
200         }
201
202         if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
203                 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
204         }
205
206         if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
207                 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
208         }
209
210         return 0;
211 }
212 #endif // WINNIE_FBDEV