86e9ccee3bd438244c6cded1e5d7b34234239364
[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         memset(mouse, 0, sizeof *mouse);
25         return true;
26 }
27
28 void destroy_mouse()
29 {
30         sh_free(mouse);
31 }
32
33 void set_mouse_bounds(const Rect &rect)
34 {
35 }
36
37 int get_mouse_fd()
38 {
39         return -1;
40 }
41
42 void process_mouse_event()
43 {
44         int bn;
45         MouseMotionFuncType motion_callback = 0;
46         MouseButtonFuncType button_callback = 0;
47
48         Window *win;
49         if(!(win = wm->get_grab_window())) {
50                 win = wm->get_window_at_pos(mouse->pointer_x, mouse->pointer_y);
51                 if(win) {
52                         wm->set_focused_window(win);
53                 }
54                 else {
55                         wm->set_focused_window(0);
56                 }
57         }
58
59         switch(sdl_event.type) {
60         case SDL_MOUSEMOTION:
61                 mouse->pointer_x = sdl_event.motion.x;
62                 mouse->pointer_y = sdl_event.motion.y;
63                 if(win && (motion_callback = win->get_mouse_motion_callback())) {
64                         Rect rect = win->get_absolute_rect();
65                         motion_callback(win, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
66                 }
67                 break;
68
69         case SDL_MOUSEBUTTONUP:
70         case SDL_MOUSEBUTTONDOWN:
71                 bn = sdl_event.button.button - SDL_BUTTON_LEFT;
72                 if(sdl_event.button.state == SDL_PRESSED) {
73                         mouse->bnstate |= 1 << bn;
74                 }
75                 else {
76                         mouse->bnstate &= ~(1 << bn);
77                 }
78                 if(win && (button_callback = win->get_mouse_button_callback())) {
79                         Rect rect = win->get_absolute_rect();
80                         button_callback(win, bn, sdl_event.button.state, mouse->pointer_x - rect.x, mouse->pointer_y - rect.y);
81                 }
82         }
83 }
84
85 void get_pointer_pos(int *x, int *y)
86 {
87         *x = mouse->pointer_x;
88         *y = mouse->pointer_y;
89 }
90
91 int get_button_state()
92 {
93         return mouse->bnstate;
94 }
95
96 int get_button(int bn)
97 {
98         if(bn < 0 || bn >= 3) {
99                 return 0;
100         }
101         return (mouse->bnstate & (1 << bn)) != 0;
102 }
103 #endif // WINNIE_SDL