2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
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.
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.
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/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
30 #include <sys/ioctl.h>
46 static int read_mouse();
60 if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
63 get_subsys()->mouse_offset = (int)((char*)mouse - (char*)get_pool());
64 memset(mouse, 0, sizeof *mouse);
68 if((mouse->dev_fd = open("/dev/psaux", O_RDONLY | O_NONBLOCK)) == -1) {
69 fprintf(stderr, "Cannot open /dev/psaux : %s\n", strerror(errno));
73 set_mouse_bounds(get_screen_size());
79 if(mouse->dev_fd != -1) {
86 void set_mouse_bounds(const Rect &rect)
96 void process_mouse_event()
99 * - read all pending events from mouse fd (use O_NONBLOCK so that
100 * read will return -1 when there are no more events instead of blocking).
103 int prev_state = mouse->bnstate;
104 int prev_x = mouse->pointer_x;
105 int prev_y = mouse->pointer_y;
107 if(read_mouse() == -1) {
112 if(!(top = wm->get_grab_window())) {
113 top = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
115 wm->set_focused_window(top);
118 wm->set_focused_window(0);
122 /* - send each pointer move and button press/release to the topmost window
123 * with the pointer on it.
126 int dx = mouse->pointer_x - prev_x;
127 int dy = mouse->pointer_y - prev_y;
129 if((dx || dy) && top) {
130 MouseMotionFuncType motion_callback = top->get_mouse_motion_callback();
131 if(motion_callback) {
132 Rect rect = top->get_absolute_rect();
133 motion_callback(top, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
137 MouseButtonFuncType button_callback;
138 if((mouse->bnstate != prev_state) && top && (button_callback = top->get_mouse_button_callback())) {
139 int num_bits = sizeof mouse->bnstate * CHAR_BIT;
140 for(int i=0; i<num_bits; i++) {
141 int s = (mouse->bnstate >> i) & 1;
142 int prev_s = (prev_state >> i) & 1;
144 Rect rect = top->get_absolute_rect();
145 button_callback(top, i, s, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
151 void get_pointer_pos(int *x, int *y)
153 *x = mouse->pointer_x;
154 *y = mouse->pointer_y;
157 int get_button_state()
159 return mouse->bnstate;
162 int get_button(int bn)
164 if(bn < 0 || bn >= 3) {
167 return (mouse->bnstate & (1 << bn)) != 0;
170 static int read_mouse()
173 signed char state[3] = {0, 0, 0};
175 if((rd = read(mouse->dev_fd, state, 3)) == -1) {
176 fprintf(stderr, "Unable to get mouse state : %s\n", strerror(errno));
180 mouse->bnstate = state[0] & 7;
181 mouse->pointer_x += state[1];
182 mouse->pointer_y -= state[2];
184 if(mouse->pointer_x < mouse->bounds.x) {
185 mouse->pointer_x = mouse->bounds.x;
188 if(mouse->pointer_y < mouse->bounds.y) {
189 mouse->pointer_y = mouse->bounds.y;
192 if(mouse->pointer_x > mouse->bounds.x + mouse->bounds.width - 1) {
193 mouse->pointer_x = mouse->bounds.x + mouse->bounds.width - 1;
196 if(mouse->pointer_y > mouse->bounds.y + mouse->bounds.height - 1) {
197 mouse->pointer_y = mouse->bounds.y + mouse->bounds.height - 1;
202 #endif // WINNIE_FBDEV