amd64 fixes
[btb/d2x.git] / arch / sdl / mouse.c
1 /* $Id: mouse.c,v 1.8 2003-11-27 09:24:43 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         SDL_GetMouseState(&Mouse.x, &Mouse.y); // necessary because polling only gives us the delta.
125 }
126
127 //========================================================================
128 void mouse_get_pos( int *x, int *y )
129 {
130         event_poll();
131 #ifdef _WIN32_WCE // needed here only for touchpens?
132 # ifdef LANDSCAPE
133         SDL_GetMouseState(&Mouse.y, &Mouse.x);
134 # else
135         SDL_GetMouseState(&Mouse.x, &Mouse.y);
136 # endif
137 #endif
138         *x=Mouse.x;
139         *y=Mouse.y;
140 }
141
142 void mouse_get_delta( int *dx, int *dy )
143 {
144         event_poll();
145         *dx = Mouse.delta_x;
146         *dy = Mouse.delta_y;
147         Mouse.delta_x = 0;
148         Mouse.delta_y = 0;
149 }
150
151 void mouse_get_pos_z( int *x, int *y, int *z )
152 {
153         event_poll();
154         *x=Mouse.x;
155         *y=Mouse.y;
156         *z=Mouse.z;
157 }
158
159 void mouse_get_delta_z( int *dx, int *dy, int *dz )
160 {
161         event_poll();
162         *dx = Mouse.delta_x;
163         *dy = Mouse.delta_y;
164         *dz = Mouse.delta_z;
165         Mouse.delta_x = 0;
166         Mouse.delta_y = 0;
167         Mouse.delta_z = 0;
168 }
169
170 int mouse_get_btns()
171 {
172         int i;
173         uint flag=1;
174         int status = 0;
175
176         event_poll();
177
178         for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
179                 if (Mouse.buttons[i].pressed)
180                         status |= flag;
181                 flag <<= 1;
182         }
183
184         return status;
185 }
186
187 void mouse_get_cyberman_pos( int *x, int *y )
188 {
189 }
190
191 // Returns how long this button has been down since last call.
192 fix mouse_button_down_time(int button)
193 {
194         fix time_down, time;
195
196         event_poll();
197
198         if (!Mouse.buttons[button].pressed) {
199                 time_down = Mouse.buttons[button].time_held_down;
200                 Mouse.buttons[button].time_held_down = 0;
201         } else {
202                 time = timer_get_fixed_seconds();
203                 time_down = time - Mouse.buttons[button].time_held_down;
204                 Mouse.buttons[button].time_held_down = time;
205         }
206         return time_down;
207 }
208
209 // Returns how many times this button has went down since last call
210 int mouse_button_down_count(int button)
211 {
212         int count;
213
214         event_poll();
215
216         count = Mouse.buttons[button].num_downs;
217         Mouse.buttons[button].num_downs = 0;
218
219         return count;
220 }
221
222 // Returns 1 if this button is currently down
223 int mouse_button_state(int button)
224 {
225         event_poll();
226         return Mouse.buttons[button].pressed;
227 }