whitespace
[btb/d2x.git] / arch / sdl / mouse.c
1 /*
2  *
3  * SDL mouse driver.
4  *
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include <conf.h>
9 #endif
10
11 #include <string.h>
12 #include <SDL/SDL.h>
13 #include "fix.h"
14 #include "timer.h"
15 #include "event.h"
16 #include "mouse.h"
17
18 #define MOUSE_MAX_BUTTONS       8
19
20 #define Z_SENSITIVITY 100
21
22 struct mousebutton {
23         ubyte pressed;
24         fix time_went_down;
25         fix time_held_down;
26         uint num_downs;
27         uint num_ups;
28 };
29
30 static struct mouseinfo {
31         struct mousebutton buttons[MOUSE_MAX_BUTTONS];
32         int delta_x, delta_y, delta_z;
33         int x,y,z;
34 } Mouse;
35
36 void d_mouse_init(void)
37 {
38         memset(&Mouse,0,sizeof(Mouse));
39 }
40
41 void mouse_button_handler(SDL_MouseButtonEvent *mbe)
42 {
43         // to bad, SDL buttons use a different mapping as descent expects,
44         // this is at least true and tested for the first three buttons 
45         int button_remap[11] = {
46                 MB_LEFT,
47                 MB_MIDDLE,
48                 MB_RIGHT,
49                 MB_Z_UP,
50                 MB_Z_DOWN,
51                 MB_PITCH_BACKWARD,
52                 MB_PITCH_FORWARD,
53                 MB_BANK_LEFT,
54                 MB_BANK_RIGHT,
55                 MB_HEAD_LEFT,
56                 MB_HEAD_RIGHT
57         };
58
59         int button = button_remap[mbe->button - 1]; // -1 since SDL seems to start counting at 1
60
61         if (mbe->state == SDL_PRESSED) {
62                 Mouse.buttons[button].pressed = 1;
63                 Mouse.buttons[button].time_went_down = timer_get_fixed_seconds();
64                 Mouse.buttons[button].num_downs++;
65
66                 if (button == MB_Z_UP) {
67                         Mouse.delta_z += Z_SENSITIVITY;
68                         Mouse.z += Z_SENSITIVITY;
69                 } else if (button == MB_Z_DOWN) {
70                         Mouse.delta_z -= Z_SENSITIVITY;
71                         Mouse.z -= Z_SENSITIVITY;
72                 }
73         } else {
74                 Mouse.buttons[button].pressed = 0;
75                 Mouse.buttons[button].time_held_down += timer_get_fixed_seconds() - Mouse.buttons[button].time_went_down;
76                 Mouse.buttons[button].num_ups++;
77         }
78 }
79
80 void mouse_motion_handler(SDL_MouseMotionEvent *mme)
81 {
82         Mouse.delta_x += mme->xrel;
83         Mouse.delta_y += mme->yrel;
84         Mouse.x += mme->xrel;
85         Mouse.y += mme->yrel;
86 }
87
88 void mouse_flush()      // clears all mice events...
89 {
90         int i;
91         fix current_time;
92
93         event_poll();
94
95         current_time = timer_get_fixed_seconds();
96         for (i=0; i<MOUSE_MAX_BUTTONS; i++) {
97                 Mouse.buttons[i].pressed=0;
98                 Mouse.buttons[i].time_went_down=current_time;
99                 Mouse.buttons[i].time_held_down=0;
100                 Mouse.buttons[i].num_ups=0;
101                 Mouse.buttons[i].num_downs=0;
102         }
103         Mouse.delta_x = 0;
104         Mouse.delta_y = 0;
105         Mouse.delta_z = 0;
106         Mouse.x = 0;
107         Mouse.y = 0;
108         Mouse.z = 0;
109 }
110
111 //========================================================================
112 void mouse_get_pos( int *x, int *y )
113 {
114         event_poll();
115         *x=Mouse.x;
116         *y=Mouse.y;
117 }
118
119 void mouse_get_delta( int *dx, int *dy )
120 {
121         event_poll();
122         *dx = Mouse.delta_x;
123         *dy = Mouse.delta_y;
124         Mouse.delta_x = 0;
125         Mouse.delta_y = 0;
126 }
127
128 void mouse_get_pos_z( int *x, int *y, int *z )
129 {
130         event_poll();
131         *x=Mouse.x;
132         *y=Mouse.y;
133         *z=Mouse.z;
134 }
135
136 void mouse_get_delta_z( int *dx, int *dy, int *dz )
137 {
138         event_poll();
139         *dx = Mouse.delta_x;
140         *dy = Mouse.delta_y;
141         *dz = Mouse.delta_z;
142         Mouse.delta_x = 0;
143         Mouse.delta_y = 0;
144         Mouse.delta_z = 0;
145 }
146
147 int mouse_get_btns()
148 {
149         int i;
150         uint flag=1;
151         int status = 0;
152
153         event_poll();
154
155         for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
156                 if (Mouse.buttons[i].pressed)
157                         status |= flag;
158                 flag <<= 1;
159         }
160
161         return status;
162 }
163
164 void mouse_get_cyberman_pos( int *x, int *y )
165 {
166 }
167
168 // Returns how long this button has been down since last call.
169 fix mouse_button_down_time(int button)
170 {
171         fix time_down, time;
172
173         event_poll();
174
175         if (!Mouse.buttons[button].pressed) {
176                 time_down = Mouse.buttons[button].time_held_down;
177                 Mouse.buttons[button].time_held_down = 0;
178         } else {
179                 time = timer_get_fixed_seconds();
180                 time_down = time - Mouse.buttons[button].time_held_down;
181                 Mouse.buttons[button].time_held_down = time;
182         }
183         return time_down;
184 }
185
186 // Returns how many times this button has went down since last call
187 int mouse_button_down_count(int button)
188 {
189         int count;
190
191         event_poll();
192
193         count = Mouse.buttons[button].num_downs;
194         Mouse.buttons[button].num_downs = 0;
195
196         return count;
197 }
198
199 // Returns 1 if this button is currently down
200 int mouse_button_state(int button)
201 {
202         event_poll();
203         return Mouse.buttons[button].pressed;
204 }