55317079fead0e7ee27de42354c5c6cfe8d7d1fb
[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 = wm->get_window_at_pos(pointer_x, pointer_y);
38
39         if(top) {
40                 wm->set_focused_window(top);
41         }
42         else {
43                 wm->set_focused_window(0);
44         }
45
46         switch(sdl_event.type) {
47         case SDL_MOUSEMOTION:
48                 pointer_x = sdl_event.motion.x;
49                 pointer_y = sdl_event.motion.y;
50                 if(top && (motion_callback = top->get_mouse_motion_callback())) {
51                         Rect rect = top->get_absolute_rect();
52                         motion_callback(top, pointer_x - rect.x, pointer_y - rect.y);
53                 }
54                 break;
55
56         case SDL_MOUSEBUTTONUP:
57         case SDL_MOUSEBUTTONDOWN:
58                 bn = sdl_event.button.button - SDL_BUTTON_LEFT;
59                 if(sdl_event.button.state == SDL_PRESSED) {
60                         bnstate |= 1 << bn;
61                 }
62                 else {
63                         bnstate &= ~(1 << bn);
64                 }
65                 if(top && (button_callback = top->get_mouse_button_callback())) {
66                         button_callback(top, bn, sdl_event.button.state);
67                 }
68         }
69 }
70
71 void get_pointer_pos(int *x, int *y)
72 {
73         *x = pointer_x;
74         *y = pointer_y;
75 }
76
77 int get_button_state()
78 {
79         return bnstate;
80 }
81
82 int get_button(int bn)
83 {
84         if(bn < 0 || bn >= 3) {
85                 return 0;
86         }
87         return (bnstate & (1 << bn)) != 0;
88 }
89 #endif // WINNIE_SDL