Mercurial > sdl-ios-xcode
annotate src/joystick/win32/SDL_mmjoystick.c @ 252:e8157fcb3114
Updated the source with the correct e-mail address
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 14 Dec 2001 12:38:15 +0000 |
parents | 6a4e09bbbbc0 |
children | f6ffac90895c |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
165
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */ | |
29 | |
30 #include <stdlib.h> | |
31 #include <stdio.h> /* For the definition of NULL */ | |
32 | |
33 #include "SDL_error.h" | |
34 #include "SDL_joystick.h" | |
35 #include "SDL_sysjoystick.h" | |
36 #include "SDL_joystick_c.h" | |
37 | |
38 #include <windows.h> | |
39 #include <mmsystem.h> | |
40 | |
41 #define MAX_JOYSTICKS 2 /* only 2 are supported in the multimedia API */ | |
42 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ | |
43 #define MAX_BUTTONS 32 /* and 32 buttons */ | |
44 #define AXIS_MIN -32768 /* minimum value for axis coordinate */ | |
45 #define AXIS_MAX 32767 /* maximum value for axis coordinate */ | |
46 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */ | |
47 #define JOY_BUTTON_FLAG(n) (1<<n) | |
48 | |
49 | |
50 /* array to hold joystick ID values */ | |
51 static UINT SYS_JoystickID[MAX_JOYSTICKS]; | |
52 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS]; | |
53 | |
54 /* The private structure used to keep track of a joystick */ | |
55 struct joystick_hwdata | |
56 { | |
57 /* joystick ID */ | |
58 UINT id; | |
59 | |
60 /* values used to translate device-specific coordinates into | |
61 SDL-standard ranges */ | |
62 struct _transaxis { | |
63 int offset; | |
64 float scale; | |
65 } transaxis[6]; | |
66 }; | |
67 | |
68 /* Convert a win32 Multimedia API return code to a text message */ | |
69 static void SetMMerror(char *function, int code); | |
70 | |
71 | |
72 /* Function to scan the system for joysticks. | |
73 * This function should set SDL_numjoysticks to the number of available | |
74 * joysticks. Joystick 0 should be the system default joystick. | |
75 * It should return 0, or -1 on an unrecoverable fatal error. | |
76 */ | |
77 int SDL_SYS_JoystickInit(void) | |
78 { | |
79 int i; | |
80 int maxdevs; | |
81 int numdevs; | |
82 JOYINFOEX joyinfo; | |
83 JOYCAPS joycaps; | |
84 MMRESULT result; | |
85 | |
86 numdevs = 0; | |
87 maxdevs = joyGetNumDevs(); | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
88 |
0 | 89 if ( maxdevs > MAX_JOYSTICKS ) { |
90 maxdevs = MAX_JOYSTICKS; | |
91 } | |
92 | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
93 |
0 | 94 SYS_JoystickID[0] = JOYSTICKID1; |
95 SYS_JoystickID[1] = JOYSTICKID2; | |
96 | |
97 for ( i = 0; (i < maxdevs); ++i ) { | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
98 |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
99 /* added 8/31/2001 By Vitaliy Mikitchenko */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
100 joyinfo.dwSize = sizeof(joyinfo); |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
101 joyinfo.dwFlags = JOY_RETURNALL; |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
102 /* end addition */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
103 |
0 | 104 result = joyGetPosEx(SYS_JoystickID[i], &joyinfo); |
105 if ( result == JOYERR_NOERROR ) { | |
106 result = joyGetDevCaps(SYS_JoystickID[i], &joycaps, sizeof(joycaps)); | |
107 if ( result == JOYERR_NOERROR ) { | |
108 SYS_JoystickID[numdevs] = SYS_JoystickID[i]; | |
109 SYS_Joystick[numdevs] = joycaps; | |
110 numdevs++; | |
111 } | |
112 } | |
113 } | |
114 return(numdevs); | |
115 } | |
116 | |
117 /* Function to get the device-dependent name of a joystick */ | |
118 const char *SDL_SYS_JoystickName(int index) | |
119 { | |
120 /***-> test for invalid index ? */ | |
121 return(SYS_Joystick[index].szPname); | |
122 } | |
123 | |
124 /* Function to open a joystick for use. | |
125 The joystick to open is specified by the index field of the joystick. | |
126 This should fill the nbuttons and naxes fields of the joystick structure. | |
127 It returns 0, or -1 if there is an error. | |
128 */ | |
129 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
130 { | |
131 int index, i; | |
132 int caps_flags[MAX_AXES-2] = | |
133 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; | |
134 int axis_min[MAX_AXES], axis_max[MAX_AXES]; | |
135 | |
136 | |
137 /* shortcut */ | |
138 index = joystick->index; | |
139 axis_min[0] = SYS_Joystick[index].wXmin; | |
140 axis_max[0] = SYS_Joystick[index].wXmax; | |
141 axis_min[1] = SYS_Joystick[index].wYmin; | |
142 axis_max[1] = SYS_Joystick[index].wYmax; | |
143 axis_min[2] = SYS_Joystick[index].wZmin; | |
144 axis_max[2] = SYS_Joystick[index].wZmax; | |
145 axis_min[3] = SYS_Joystick[index].wRmin; | |
146 axis_max[3] = SYS_Joystick[index].wRmax; | |
147 axis_min[4] = SYS_Joystick[index].wUmin; | |
148 axis_max[4] = SYS_Joystick[index].wUmax; | |
149 axis_min[5] = SYS_Joystick[index].wVmin; | |
150 axis_max[5] = SYS_Joystick[index].wVmax; | |
151 | |
152 /* allocate memory for system specific hardware data */ | |
153 joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata)); | |
154 if (joystick->hwdata == NULL) | |
155 { | |
156 SDL_OutOfMemory(); | |
157 return(-1); | |
158 } | |
159 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
160 | |
161 /* set hardware data */ | |
162 joystick->hwdata->id = SYS_JoystickID[index]; | |
163 for ( i = 0; i < MAX_AXES; ++i ) { | |
164 if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) { | |
165 joystick->hwdata->transaxis[i].offset = | |
166 AXIS_MIN - axis_min[i]; | |
167 joystick->hwdata->transaxis[i].scale = | |
168 (float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]); | |
169 } else { | |
170 joystick->hwdata->transaxis[i].offset = 0; | |
171 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */ | |
172 } | |
173 } | |
174 | |
175 /* fill nbuttons, naxes, and nhats fields */ | |
176 joystick->nbuttons = SYS_Joystick[index].wNumButtons; | |
177 joystick->naxes = SYS_Joystick[index].wNumAxes; | |
178 if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) { | |
179 joystick->nhats = 1; | |
180 } else { | |
181 joystick->nhats = 0; | |
182 } | |
183 return(0); | |
184 } | |
185 | |
186 static Uint8 TranslatePOV(DWORD value) | |
187 { | |
188 Uint8 pos; | |
189 | |
190 pos = SDL_HAT_CENTERED; | |
191 if ( value != JOY_POVCENTERED ) { | |
192 if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) { | |
193 pos |= SDL_HAT_UP; | |
194 } | |
195 if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) { | |
196 pos |= SDL_HAT_RIGHT; | |
197 } | |
198 if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) { | |
199 pos |= SDL_HAT_DOWN; | |
200 } | |
201 if ( value > JOY_POVBACKWARD ) { | |
202 pos |= SDL_HAT_LEFT; | |
203 } | |
204 } | |
205 return(pos); | |
206 } | |
207 | |
208 /* Function to update the state of a joystick - called as a device poll. | |
209 * This function shouldn't update the joystick structure directly, | |
210 * but instead should call SDL_PrivateJoystick*() to deliver events | |
211 * and update joystick device state. | |
212 */ | |
213 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
214 { | |
215 MMRESULT result; | |
216 int i; | |
217 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, | |
218 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; | |
219 DWORD pos[MAX_AXES]; | |
220 struct _transaxis *transaxis; | |
221 int value, change; | |
222 JOYINFOEX joyinfo; | |
223 | |
224 joyinfo.dwSize = sizeof(joyinfo); | |
225 joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS; | |
226 if ( ! joystick->hats ) { | |
227 joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS); | |
228 } | |
229 result = joyGetPosEx(joystick->hwdata->id, &joyinfo); | |
230 if ( result != JOYERR_NOERROR ) { | |
231 SetMMerror("joyGetPosEx", result); | |
232 return; | |
233 } | |
234 | |
235 /* joystick motion events */ | |
236 pos[0] = joyinfo.dwXpos; | |
237 pos[1] = joyinfo.dwYpos; | |
238 pos[2] = joyinfo.dwZpos; | |
239 pos[3] = joyinfo.dwRpos; | |
240 pos[4] = joyinfo.dwUpos; | |
241 pos[5] = joyinfo.dwVpos; | |
242 | |
243 transaxis = joystick->hwdata->transaxis; | |
244 for (i = 0; i < joystick->naxes; i++) { | |
245 if (joyinfo.dwFlags & flags[i]) { | |
49
6f3c474f9abd
Fixed bug in joystick motion, thanks to Alexandre Duret-Lutz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
246 value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale); |
0 | 247 change = (value - joystick->axes[i]); |
248 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { | |
249 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); | |
250 } | |
251 } | |
252 } | |
253 | |
254 /* joystick button events */ | |
255 if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) { | |
256 for ( i = 0; i < joystick->nbuttons; ++i ) { | |
257 if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) { | |
258 if ( ! joystick->buttons[i] ) { | |
259 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED); | |
260 } | |
261 } else { | |
262 if ( joystick->buttons[i] ) { | |
263 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED); | |
264 } | |
265 } | |
266 } | |
267 } | |
268 | |
269 /* joystick hat events */ | |
270 if ( joyinfo.dwFlags & JOY_RETURNPOV ) { | |
271 Uint8 pos; | |
272 | |
273 pos = TranslatePOV(joyinfo.dwPOV); | |
274 if ( pos != joystick->hats[0] ) { | |
275 SDL_PrivateJoystickHat(joystick, 0, pos); | |
276 } | |
277 } | |
278 } | |
279 | |
280 /* Function to close a joystick after use */ | |
281 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
282 { | |
283 if (joystick->hwdata != NULL) { | |
284 /* free system specific hardware data */ | |
285 free(joystick->hwdata); | |
286 } | |
287 } | |
288 | |
289 /* Function to perform any system-specific joystick related cleanup */ | |
290 void SDL_SYS_JoystickQuit(void) | |
291 { | |
292 return; | |
293 } | |
294 | |
295 | |
296 /* implementation functions */ | |
297 void SetMMerror(char *function, int code) | |
298 { | |
299 static char *error; | |
300 static char errbuf[BUFSIZ]; | |
301 | |
302 errbuf[0] = 0; | |
303 switch (code) | |
304 { | |
305 case MMSYSERR_NODRIVER: | |
306 error = "Joystick driver not present"; | |
307 break; | |
308 | |
309 case MMSYSERR_INVALPARAM: | |
310 case JOYERR_PARMS: | |
311 error = "Invalid parameter(s)"; | |
312 break; | |
313 | |
314 case MMSYSERR_BADDEVICEID: | |
315 error = "Bad device ID"; | |
316 break; | |
317 | |
318 case JOYERR_UNPLUGGED: | |
319 error = "Joystick not attached"; | |
320 break; | |
321 | |
322 case JOYERR_NOCANDO: | |
323 error = "Can't capture joystick input"; | |
324 break; | |
325 | |
326 default: | |
327 sprintf(errbuf, "%s: Unknown Multimedia system error: 0x%x", | |
328 function, code); | |
329 break; | |
330 } | |
331 | |
332 if ( ! errbuf[0] ) { | |
333 sprintf(errbuf, "%s: %s", function, error); | |
334 } | |
335 SDL_SetError("%s", errbuf); | |
336 } |