Mercurial > sdl-ios-xcode
annotate src/joystick/win32/SDL_mmjoystick.c @ 897:9e27fdb98eab
Date: Tue, 11 May 2004 22:05:32 -0400
From: Bob Ippolito
Subject: [SDL] SDL_QuartzWM patches
I've sent in two small patches to SDL_QuartzWM directly to Sam over the
past few months (well, I think I sent both anyway) and neither of them
have been implemented. I didn't receive a response, so I'm sure he was
just busy and/or they got lost, so I decided to sign up to the list and
post them here.
This patch rolls both of them together:
- Mouse cursor becomes visible if hidden when it moves outside of the
game window. If you want it to stay invisible you should warp it
because if it's not warped a user might click some random other
application! Commercial games behave in this way (or at least Warcraft
III does, which is the only one that uses a custom mouse cursor and no
warping that I've played in recent memory).
- Right mouse button emulation is changed from Command-Click to
Control-Click, which is how OS X behaves.
Consider copyright assigned to whomever needs it under whichever
license it needs to be under.. yadda yadda yadda.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 17 May 2004 00:16:24 +0000 |
parents | d37179d10ccc |
children | 1e6366bde299 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
531
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 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 | |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
41 #define MAX_JOYSTICKS 16 |
0 | 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 */ | |
834
d37179d10ccc
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
46 /* limit axis to 256 possible positions to filter out noise */ |
d37179d10ccc
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
47 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256) |
0 | 48 #define JOY_BUTTON_FLAG(n) (1<<n) |
49 | |
50 | |
51 /* array to hold joystick ID values */ | |
52 static UINT SYS_JoystickID[MAX_JOYSTICKS]; | |
53 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS]; | |
54 | |
55 /* The private structure used to keep track of a joystick */ | |
56 struct joystick_hwdata | |
57 { | |
58 /* joystick ID */ | |
59 UINT id; | |
60 | |
61 /* values used to translate device-specific coordinates into | |
62 SDL-standard ranges */ | |
63 struct _transaxis { | |
64 int offset; | |
65 float scale; | |
66 } transaxis[6]; | |
67 }; | |
68 | |
69 /* Convert a win32 Multimedia API return code to a text message */ | |
70 static void SetMMerror(char *function, int code); | |
71 | |
72 | |
73 /* Function to scan the system for joysticks. | |
74 * This function should set SDL_numjoysticks to the number of available | |
75 * joysticks. Joystick 0 should be the system default joystick. | |
76 * It should return 0, or -1 on an unrecoverable fatal error. | |
77 */ | |
78 int SDL_SYS_JoystickInit(void) | |
79 { | |
80 int i; | |
81 int maxdevs; | |
82 int numdevs; | |
83 JOYINFOEX joyinfo; | |
84 JOYCAPS joycaps; | |
85 MMRESULT result; | |
86 | |
87 numdevs = 0; | |
88 maxdevs = joyGetNumDevs(); | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
89 |
0 | 90 if ( maxdevs > MAX_JOYSTICKS ) { |
91 maxdevs = MAX_JOYSTICKS; | |
92 } | |
93 | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
94 |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
95 for ( i = 0; i < MAX_JOYSTICKS; i++ ) { |
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
96 SYS_JoystickID[i] = JOYSTICKID1 + i; |
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
97 } |
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
98 |
0 | 99 |
100 for ( i = 0; (i < maxdevs); ++i ) { | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
101 |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
102 /* added 8/31/2001 By Vitaliy Mikitchenko */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
103 joyinfo.dwSize = sizeof(joyinfo); |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
104 joyinfo.dwFlags = JOY_RETURNALL; |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
105 /* end addition */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
106 |
0 | 107 result = joyGetPosEx(SYS_JoystickID[i], &joyinfo); |
108 if ( result == JOYERR_NOERROR ) { | |
109 result = joyGetDevCaps(SYS_JoystickID[i], &joycaps, sizeof(joycaps)); | |
110 if ( result == JOYERR_NOERROR ) { | |
111 SYS_JoystickID[numdevs] = SYS_JoystickID[i]; | |
112 SYS_Joystick[numdevs] = joycaps; | |
113 numdevs++; | |
114 } | |
115 } | |
116 } | |
117 return(numdevs); | |
118 } | |
119 | |
120 /* Function to get the device-dependent name of a joystick */ | |
121 const char *SDL_SYS_JoystickName(int index) | |
122 { | |
123 /***-> test for invalid index ? */ | |
124 return(SYS_Joystick[index].szPname); | |
125 } | |
126 | |
127 /* Function to open a joystick for use. | |
128 The joystick to open is specified by the index field of the joystick. | |
129 This should fill the nbuttons and naxes fields of the joystick structure. | |
130 It returns 0, or -1 if there is an error. | |
131 */ | |
132 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
133 { | |
134 int index, i; | |
135 int caps_flags[MAX_AXES-2] = | |
136 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; | |
137 int axis_min[MAX_AXES], axis_max[MAX_AXES]; | |
138 | |
139 | |
140 /* shortcut */ | |
141 index = joystick->index; | |
142 axis_min[0] = SYS_Joystick[index].wXmin; | |
143 axis_max[0] = SYS_Joystick[index].wXmax; | |
144 axis_min[1] = SYS_Joystick[index].wYmin; | |
145 axis_max[1] = SYS_Joystick[index].wYmax; | |
146 axis_min[2] = SYS_Joystick[index].wZmin; | |
147 axis_max[2] = SYS_Joystick[index].wZmax; | |
148 axis_min[3] = SYS_Joystick[index].wRmin; | |
149 axis_max[3] = SYS_Joystick[index].wRmax; | |
150 axis_min[4] = SYS_Joystick[index].wUmin; | |
151 axis_max[4] = SYS_Joystick[index].wUmax; | |
152 axis_min[5] = SYS_Joystick[index].wVmin; | |
153 axis_max[5] = SYS_Joystick[index].wVmax; | |
154 | |
155 /* allocate memory for system specific hardware data */ | |
156 joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata)); | |
157 if (joystick->hwdata == NULL) | |
158 { | |
159 SDL_OutOfMemory(); | |
160 return(-1); | |
161 } | |
162 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
163 | |
164 /* set hardware data */ | |
165 joystick->hwdata->id = SYS_JoystickID[index]; | |
166 for ( i = 0; i < MAX_AXES; ++i ) { | |
167 if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) { | |
168 joystick->hwdata->transaxis[i].offset = | |
169 AXIS_MIN - axis_min[i]; | |
170 joystick->hwdata->transaxis[i].scale = | |
171 (float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]); | |
172 } else { | |
173 joystick->hwdata->transaxis[i].offset = 0; | |
174 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */ | |
175 } | |
176 } | |
177 | |
178 /* fill nbuttons, naxes, and nhats fields */ | |
179 joystick->nbuttons = SYS_Joystick[index].wNumButtons; | |
180 joystick->naxes = SYS_Joystick[index].wNumAxes; | |
181 if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) { | |
182 joystick->nhats = 1; | |
183 } else { | |
184 joystick->nhats = 0; | |
185 } | |
186 return(0); | |
187 } | |
188 | |
189 static Uint8 TranslatePOV(DWORD value) | |
190 { | |
191 Uint8 pos; | |
192 | |
193 pos = SDL_HAT_CENTERED; | |
194 if ( value != JOY_POVCENTERED ) { | |
195 if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) { | |
196 pos |= SDL_HAT_UP; | |
197 } | |
198 if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) { | |
199 pos |= SDL_HAT_RIGHT; | |
200 } | |
201 if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) { | |
202 pos |= SDL_HAT_DOWN; | |
203 } | |
204 if ( value > JOY_POVBACKWARD ) { | |
205 pos |= SDL_HAT_LEFT; | |
206 } | |
207 } | |
208 return(pos); | |
209 } | |
210 | |
211 /* Function to update the state of a joystick - called as a device poll. | |
212 * This function shouldn't update the joystick structure directly, | |
213 * but instead should call SDL_PrivateJoystick*() to deliver events | |
214 * and update joystick device state. | |
215 */ | |
216 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
217 { | |
218 MMRESULT result; | |
219 int i; | |
220 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, | |
221 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; | |
222 DWORD pos[MAX_AXES]; | |
223 struct _transaxis *transaxis; | |
224 int value, change; | |
225 JOYINFOEX joyinfo; | |
226 | |
227 joyinfo.dwSize = sizeof(joyinfo); | |
228 joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS; | |
229 if ( ! joystick->hats ) { | |
230 joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS); | |
231 } | |
232 result = joyGetPosEx(joystick->hwdata->id, &joyinfo); | |
233 if ( result != JOYERR_NOERROR ) { | |
234 SetMMerror("joyGetPosEx", result); | |
235 return; | |
236 } | |
237 | |
238 /* joystick motion events */ | |
239 pos[0] = joyinfo.dwXpos; | |
240 pos[1] = joyinfo.dwYpos; | |
241 pos[2] = joyinfo.dwZpos; | |
242 pos[3] = joyinfo.dwRpos; | |
243 pos[4] = joyinfo.dwUpos; | |
244 pos[5] = joyinfo.dwVpos; | |
245 | |
246 transaxis = joystick->hwdata->transaxis; | |
247 for (i = 0; i < joystick->naxes; i++) { | |
248 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
|
249 value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale); |
0 | 250 change = (value - joystick->axes[i]); |
251 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { | |
252 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); | |
253 } | |
254 } | |
255 } | |
256 | |
257 /* joystick button events */ | |
258 if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) { | |
259 for ( i = 0; i < joystick->nbuttons; ++i ) { | |
260 if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) { | |
261 if ( ! joystick->buttons[i] ) { | |
262 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED); | |
263 } | |
264 } else { | |
265 if ( joystick->buttons[i] ) { | |
266 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED); | |
267 } | |
268 } | |
269 } | |
270 } | |
271 | |
272 /* joystick hat events */ | |
273 if ( joyinfo.dwFlags & JOY_RETURNPOV ) { | |
274 Uint8 pos; | |
275 | |
276 pos = TranslatePOV(joyinfo.dwPOV); | |
277 if ( pos != joystick->hats[0] ) { | |
278 SDL_PrivateJoystickHat(joystick, 0, pos); | |
279 } | |
280 } | |
281 } | |
282 | |
283 /* Function to close a joystick after use */ | |
284 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
285 { | |
286 if (joystick->hwdata != NULL) { | |
287 /* free system specific hardware data */ | |
288 free(joystick->hwdata); | |
289 } | |
290 } | |
291 | |
292 /* Function to perform any system-specific joystick related cleanup */ | |
293 void SDL_SYS_JoystickQuit(void) | |
294 { | |
295 return; | |
296 } | |
297 | |
298 | |
299 /* implementation functions */ | |
300 void SetMMerror(char *function, int code) | |
301 { | |
302 static char *error; | |
303 static char errbuf[BUFSIZ]; | |
304 | |
305 errbuf[0] = 0; | |
306 switch (code) | |
307 { | |
308 case MMSYSERR_NODRIVER: | |
309 error = "Joystick driver not present"; | |
310 break; | |
311 | |
312 case MMSYSERR_INVALPARAM: | |
313 case JOYERR_PARMS: | |
314 error = "Invalid parameter(s)"; | |
315 break; | |
316 | |
317 case MMSYSERR_BADDEVICEID: | |
318 error = "Bad device ID"; | |
319 break; | |
320 | |
321 case JOYERR_UNPLUGGED: | |
322 error = "Joystick not attached"; | |
323 break; | |
324 | |
325 case JOYERR_NOCANDO: | |
326 error = "Can't capture joystick input"; | |
327 break; | |
328 | |
329 default: | |
330 sprintf(errbuf, "%s: Unknown Multimedia system error: 0x%x", | |
331 function, code); | |
332 break; | |
333 } | |
334 | |
335 if ( ! errbuf[0] ) { | |
336 sprintf(errbuf, "%s: %s", function, error); | |
337 } | |
338 SDL_SetError("%s", errbuf); | |
339 } |