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