shared memory: TODO fix client bug
[winnie] / libwinnie / src / sdl / 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_SDL
23 #include <SDL/SDL.h>
24
25 #include "mouse.h"
26 #include "shalloc.h"
27 #include "wm.h"
28 #include "window.h"
29 #include "winnie.h"
30
31 extern SDL_Event sdl_event;
32
33 struct Mouse {
34         int pointer_x;
35         int pointer_y;
36         int bnstate;
37 };
38
39 static Mouse *mouse;
40
41 bool init_mouse()
42 {
43         if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
44                 return false;
45         }
46         get_subsys()->mouse_offset = (int)((char*)mouse - (char*)get_pool());
47
48         memset(mouse, 0, sizeof *mouse);
49         return true;
50 }
51
52 void destroy_mouse()
53 {
54         sh_free(mouse);
55 }
56
57 bool client_open_mouse(void *smem_start, int offset)
58 {
59         mouse = (Mouse*)((unsigned char*)smem_start + offset);
60         return true;
61 }
62
63 void client_close_mouse()
64 {
65 }
66
67 void set_mouse_bounds(const Rect &rect)
68 {
69 }
70
71 int get_mouse_fd()
72 {
73         return -1;
74 }
75
76 void process_mouse_event()
77 {
78         int bn;
79         MouseMotionFuncType motion_callback = 0;
80         MouseButtonFuncType button_callback = 0;
81
82         Window *win;
83         if(!(win = wm->get_grab_window())) {
84                 win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
85                 if(win) {
86                         wm->set_focused_window(win);
87                 }
88                 else {
89                         wm->set_focused_window(0);
90                 }
91         }
92
93         switch(sdl_event.type) {
94         case SDL_MOUSEMOTION:
95                 mouse->pointer_x = sdl_event.motion.x;
96                 mouse->pointer_y = sdl_event.motion.y;
97                 if(win && (motion_callback = win->get_mouse_motion_callback())) {
98                         Rect rect = win->get_absolute_rect();
99                         motion_callback(win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
100                 }
101                 break;
102
103         case SDL_MOUSEBUTTONUP:
104         case SDL_MOUSEBUTTONDOWN:
105                 bn = sdl_event.button.button - SDL_BUTTON_LEFT;
106                 if(sdl_event.button.state == SDL_PRESSED) {
107                         mouse->bnstate |= 1 << bn;
108                 }
109                 else {
110                         mouse->bnstate &= ~(1 << bn);
111                 }
112                 if(win && (button_callback = win->get_mouse_button_callback())) {
113                         Rect rect = win->get_absolute_rect();
114                         button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
115                 }
116         }
117 }
118
119 void get_pointer_pos(int *x, int *y)
120 {
121         *x = mouse->pointer_x;
122         *y = mouse->pointer_y;
123 }
124
125 int get_button_state()
126 {
127         return mouse->bnstate;
128 }
129
130 int get_button(int bn)
131 {
132         if(bn < 0 || bn >= 3) {
133                 return 0;
134         }
135         return (mouse->bnstate & (1 << bn)) != 0;
136 }
137 #endif // WINNIE_SDL