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