]> icculus.org git repositories - btb/d2x.git/blob - arch/sdl/mouse.c
make sure we don't strcpy to ourself
[btb/d2x.git] / arch / sdl / mouse.c
1 /*
2  *
3  * SDL mouse driver.
4  *
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <conf.h>
10 #endif
11
12 #include <string.h>
13
14 #include <SDL.h>
15
16 #include "fix.h"
17 #include "timer.h"
18 #include "event.h"
19 #include "mouse.h"
20 #include "key.h"
21
22 #ifdef _WIN32_WCE
23 # define LANDSCAPE
24 #endif
25
26 #define Z_SENSITIVITY 100
27
28 static struct mouseinfo {
29         int delta_x, delta_y, delta_z;
30         int x,y,z;
31 } Mouse;
32
33
34 cvar_t mouse_axes[3] = {
35         { "mouse_axisx", "4", 1 },
36         { "mouse_axisy", "2", 1 },
37         { "mouse_axisz", "0", 1 },
38 };
39
40 cvar_t mouse_invert[] = {
41         { "mouse_invertx", "0", 1 },
42         { "mouse_inverty", "0", 1 },
43         { "mouse_invertz", "0", 1 },
44 };
45
46
47 void d_mouse_init(void)
48 {
49         int i;
50
51         memset(&Mouse,0,sizeof(Mouse));
52
53         for (i = 0; i < 3; i++) {
54                 cvar_registervariable(&mouse_axes[i]);
55                 cvar_registervariable(&mouse_invert[i]);
56         }
57 }
58
59 void mouse_button_handler(SDL_MouseButtonEvent *mbe)
60 {
61         // to bad, SDL buttons use a different mapping as descent expects,
62         // this is at least true and tested for the first three buttons 
63         int button_remap[11] = {
64                 MB_LEFT,
65                 MB_MIDDLE,
66                 MB_RIGHT,
67                 MB_Z_UP,
68                 MB_Z_DOWN,
69                 MB_PITCH_BACKWARD,
70                 MB_PITCH_FORWARD,
71                 MB_BANK_LEFT,
72                 MB_BANK_RIGHT,
73                 MB_HEAD_LEFT,
74                 MB_HEAD_RIGHT
75         };
76
77         int button = button_remap[mbe->button - 1]; // -1 since SDL seems to start counting at 1
78
79         vkey_handler(KEY_MB1 + button, mbe->state == SDL_PRESSED);
80
81         if (mbe->state == SDL_PRESSED) {
82                 if (button == MB_Z_UP) {
83                         Mouse.delta_z += Z_SENSITIVITY;
84                         Mouse.z += Z_SENSITIVITY;
85                 } else if (button == MB_Z_DOWN) {
86                         Mouse.delta_z -= Z_SENSITIVITY;
87                         Mouse.z -= Z_SENSITIVITY;
88                 }
89         }
90 }
91
92 void mouse_motion_handler(SDL_MouseMotionEvent *mme)
93 {
94 #ifdef LANDSCAPE
95         Mouse.delta_y += mme->xrel;
96         Mouse.delta_x += mme->yrel;
97         Mouse.y += mme->xrel;
98         Mouse.x += mme->yrel;
99 #else
100         Mouse.delta_x += mme->xrel;
101         Mouse.delta_y += mme->yrel;
102         Mouse.x += mme->xrel;
103         Mouse.y += mme->yrel;
104 #endif
105 }
106
107 void mouse_flush()      // clears all mice events...
108 {
109         event_poll();
110
111         Mouse.delta_x = 0;
112         Mouse.delta_y = 0;
113         Mouse.delta_z = 0;
114         Mouse.x = 0;
115         Mouse.y = 0;
116         Mouse.z = 0;
117         SDL_GetMouseState(&Mouse.x, &Mouse.y); // necessary because polling only gives us the delta.
118 }
119
120 //========================================================================
121 void mouse_get_pos( int *x, int *y )
122 {
123         event_poll();
124 #ifdef _WIN32_WCE // needed here only for touchpens?
125 # ifdef LANDSCAPE
126         SDL_GetMouseState(&Mouse.y, &Mouse.x);
127 # else
128         SDL_GetMouseState(&Mouse.x, &Mouse.y);
129 # endif
130 #endif
131         *x=Mouse.x;
132         *y=Mouse.y;
133 }
134
135 void mouse_get_delta( int *dx, int *dy, int *dz )
136 {
137         event_poll();
138         *dx = Mouse.delta_x;
139         *dy = Mouse.delta_y;
140         *dz = Mouse.delta_z;
141         Mouse.delta_x = 0;
142         Mouse.delta_y = 0;
143         Mouse.delta_z = 0;
144 }
145
146 int mouse_get_btns()
147 {
148         int i;
149         uint flag=1;
150         int status = 0;
151
152         event_poll();
153
154         for (i=0; i<MOUSE_MAX_BUTTONS; i++ ) {
155                 if (keyd_pressed[KEY_MB1 + i])
156                         status |= flag;
157                 flag <<= 1;
158         }
159
160         return status;
161 }
162
163
164 // Returns 1 if this button is currently down
165 int mouse_button_state(int button)
166 {
167         event_poll();
168         return keyd_pressed[KEY_MB1 + button];
169 }
170
171
172 int mouse_set_mode(int i)
173 {
174         SDL_GrabMode old;
175
176         old = SDL_WM_GrabInput(SDL_GRAB_QUERY);
177         if (i)
178                 SDL_WM_GrabInput(SDL_GRAB_ON);
179         else
180                 SDL_WM_GrabInput(SDL_GRAB_OFF);
181
182         return (old == SDL_GRAB_ON);
183 }