added memory allocator
[winnie] / src / sdl / mouse.cc
1 #ifdef WINNIE_SDL
2 #include <SDL/SDL.h>
3
4 #include "mouse.h"
5 #include "shalloc.h"
6 #include "window.h"
7 #include "wm.h"
8
9 extern SDL_Event sdl_event;
10
11 struct Mouse {
12         int pointer_x;
13         int pointer_y;
14         int bnstate;
15 };
16
17 static Mouse *mouse;
18
19 bool init_mouse()
20 {
21         if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
22                 return false;
23         }
24         return true;
25 }
26
27 void destroy_mouse()
28 {
29         sh_free(mouse);
30 }
31
32 void set_mouse_bounds(const Rect &rect)
33 {
34 }
35
36 int get_mouse_fd()
37 {
38         return -1;
39 }
40
41 void process_mouse_event()
42 {
43         int bn;
44         MouseMotionFuncType motion_callback = 0;
45         MouseButtonFuncType button_callback = 0;
46
47         Window *win;
48         if(!(win = wm->get_grab_window())) {
49                 win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
50                 if(win) {
51                         wm->set_focused_window(win);
52                 }
53                 else {
54                         wm->set_focused_window(0);
55                 }
56         }
57
58         switch(sdl_event.type) {
59         case SDL_MOUSEMOTION:
60                 mouse->pointer_x = sdl_event.motion.x;
61                 mouse->pointer_y = sdl_event.motion.y;
62                 if(win && (motion_callback = win->get_mouse_motion_callback())) {
63                         Rect rect = win->get_absolute_rect();
64                         motion_callback(win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
65                 }
66                 break;
67
68         case SDL_MOUSEBUTTONUP:
69         case SDL_MOUSEBUTTONDOWN:
70                 bn = sdl_event.button.button - SDL_BUTTON_LEFT;
71                 if(sdl_event.button.state == SDL_PRESSED) {
72                         mouse->bnstate |= 1 << bn;
73                 }
74                 else {
75                         mouse->bnstate &= ~(1 << bn);
76                 }
77                 if(win && (button_callback = win->get_mouse_button_callback())) {
78                         Rect rect = win->get_absolute_rect();
79                         button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
80                 }
81         }
82 }
83
84 void get_pointer_pos(int *x, int *y)
85 {
86         *x = mouse->pointer_x;
87         *y = mouse->pointer_y;
88 }
89
90 int get_button_state()
91 {
92         return mouse->bnstate;
93 }
94
95 int get_button(int bn)
96 {
97         if(bn < 0 || bn >= 3) {
98                 return 0;
99         }
100         return (mouse->bnstate & (1 << bn)) != 0;
101 }
102 #endif // WINNIE_SDL