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 extern SDL_Event sdl_event;
42 if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
45 memset(mouse, 0, sizeof *mouse);
54 void set_mouse_bounds(const Rect &rect)
63 void process_mouse_event()
66 MouseMotionFuncType motion_callback = 0;
67 MouseButtonFuncType button_callback = 0;
70 if(!(win = wm->get_grab_window())) {
71 win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
73 wm->set_focused_window(win);
76 wm->set_focused_window(0);
80 switch(sdl_event.type) {
82 mouse->pointer_x = sdl_event.motion.x;
83 mouse->pointer_y = sdl_event.motion.y;
84 if(win && (motion_callback = win->get_mouse_motion_callback())) {
85 Rect rect = win->get_absolute_rect();
86 motion_callback(win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
90 case SDL_MOUSEBUTTONUP:
91 case SDL_MOUSEBUTTONDOWN:
92 bn = sdl_event.button.button - SDL_BUTTON_LEFT;
93 if(sdl_event.button.state == SDL_PRESSED) {
94 mouse->bnstate |= 1 << bn;
97 mouse->bnstate &= ~(1 << bn);
99 if(win && (button_callback = win->get_mouse_button_callback())) {
100 Rect rect = win->get_absolute_rect();
101 button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
106 void get_pointer_pos(int *x, int *y)
108 *x = mouse->pointer_x;
109 *y = mouse->pointer_y;
112 int get_button_state()
114 return mouse->bnstate;
117 int get_button(int bn)
119 if(bn < 0 || bn >= 3) {
122 return (mouse->bnstate & (1 << bn)) != 0;