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