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