comparison src/joystick/win32/SDL_mmjoystick.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
34 #include "SDL_joystick.h" 34 #include "SDL_joystick.h"
35 #include "../SDL_sysjoystick.h" 35 #include "../SDL_sysjoystick.h"
36 #include "../SDL_joystick_c.h" 36 #include "../SDL_joystick_c.h"
37 37
38 #define MAX_JOYSTICKS 16 38 #define MAX_JOYSTICKS 16
39 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ 39 #define MAX_AXES 6 /* each joystick can have up to 6 axes */
40 #define MAX_BUTTONS 32 /* and 32 buttons */ 40 #define MAX_BUTTONS 32 /* and 32 buttons */
41 #define AXIS_MIN -32768 /* minimum value for axis coordinate */ 41 #define AXIS_MIN -32768 /* minimum value for axis coordinate */
42 #define AXIS_MAX 32767 /* maximum value for axis coordinate */ 42 #define AXIS_MAX 32767 /* maximum value for axis coordinate */
43 /* limit axis to 256 possible positions to filter out noise */ 43 /* limit axis to 256 possible positions to filter out noise */
44 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256) 44 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256)
45 #define JOY_BUTTON_FLAG(n) (1<<n) 45 #define JOY_BUTTON_FLAG(n) (1<<n)
46 46
47 47
48 /* array to hold joystick ID values */ 48 /* array to hold joystick ID values */
49 static UINT SYS_JoystickID[MAX_JOYSTICKS]; 49 static UINT SYS_JoystickID[MAX_JOYSTICKS];
50 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS]; 50 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS];
51 static char *SYS_JoystickName[MAX_JOYSTICKS]; 51 static char *SYS_JoystickName[MAX_JOYSTICKS];
52 52
53 /* The private structure used to keep track of a joystick */ 53 /* The private structure used to keep track of a joystick */
54 struct joystick_hwdata 54 struct joystick_hwdata
55 { 55 {
56 /* joystick ID */ 56 /* joystick ID */
57 UINT id; 57 UINT id;
58 58
59 /* values used to translate device-specific coordinates into 59 /* values used to translate device-specific coordinates into
60 SDL-standard ranges */ 60 SDL-standard ranges */
61 struct _transaxis { 61 struct _transaxis
62 int offset; 62 {
63 float scale; 63 int offset;
64 } transaxis[6]; 64 float scale;
65 } transaxis[6];
65 }; 66 };
66 67
67 /* Convert a win32 Multimedia API return code to a text message */ 68 /* Convert a win32 Multimedia API return code to a text message */
68 static void SetMMerror(char *function, int code); 69 static void SetMMerror (char *function, int code);
69 70
70 71
71 static char *GetJoystickName(int index, const char *szRegKey) 72 static char *
72 { 73 GetJoystickName (int index, const char *szRegKey)
73 /* added 7/24/2004 by Eckhard Stolberg */ 74 {
74 /* 75 /* added 7/24/2004 by Eckhard Stolberg */
75 see if there is a joystick for the current 76 /*
76 index (1-16) listed in the registry 77 see if there is a joystick for the current
77 */ 78 index (1-16) listed in the registry
78 char *name = NULL; 79 */
79 HKEY hKey; 80 char *name = NULL;
80 DWORD regsize; 81 HKEY hKey;
81 LONG regresult; 82 DWORD regsize;
82 unsigned char regkey[256]; 83 LONG regresult;
83 unsigned char regvalue[256]; 84 unsigned char regkey[256];
84 unsigned char regname[256]; 85 unsigned char regvalue[256];
85 86 unsigned char regname[256];
86 SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s", 87
87 REGSTR_PATH_JOYCONFIG, 88 SDL_snprintf ((char *) regkey, SDL_arraysize (regkey), "%s\\%s\\%s",
88 szRegKey, 89 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
89 REGSTR_KEY_JOYCURR); 90 regresult = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
90 regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, 91 (LPTSTR) & regkey, 0, KEY_READ, &hKey);
91 (LPTSTR) &regkey, 0, KEY_READ, &hKey); 92 if (regresult == ERROR_SUCCESS) {
92 if (regresult == ERROR_SUCCESS) 93 /*
93 { 94 find the registry key name for the
94 /* 95 joystick's properties
95 find the registry key name for the 96 */
96 joystick's properties 97 regsize = sizeof (regname);
97 */ 98 SDL_snprintf ((char *) regvalue, SDL_arraysize (regvalue),
98 regsize = sizeof(regname); 99 "Joystick%d%s", index + 1, REGSTR_VAL_JOYOEMNAME);
99 SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue), 100 regresult = RegQueryValueExA (hKey,
100 "Joystick%d%s", index+1, 101 (char *) regvalue, 0, 0,
101 REGSTR_VAL_JOYOEMNAME); 102 (LPBYTE) & regname,
102 regresult = RegQueryValueExA(hKey, 103 (LPDWORD) & regsize);
103 (char *) regvalue, 0, 0, (LPBYTE) &regname, 104 RegCloseKey (hKey);
104 (LPDWORD) &regsize); 105 if (regresult == ERROR_SUCCESS) {
105 RegCloseKey(hKey); 106 /* open that registry key */
106 if (regresult == ERROR_SUCCESS) 107 SDL_snprintf ((char *) regkey, SDL_arraysize (regkey),
107 { 108 "%s\\%s", REGSTR_PATH_JOYOEM, regname);
108 /* open that registry key */ 109 regresult =
109 SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s", 110 RegOpenKeyExA (HKEY_LOCAL_MACHINE, (char *) regkey, 0,
110 REGSTR_PATH_JOYOEM, regname); 111 KEY_READ, &hKey);
111 regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, 112 if (regresult == ERROR_SUCCESS) {
112 (char *) regkey, 0, KEY_READ, &hKey); 113 /* find the size for the OEM name text */
113 if (regresult == ERROR_SUCCESS) 114 regsize = sizeof (regvalue);
114 { 115 regresult =
115 /* find the size for the OEM name text */ 116 RegQueryValueExA (hKey,
116 regsize = sizeof(regvalue); 117 REGSTR_VAL_JOYOEMNAME,
117 regresult = 118 0, 0, NULL, (LPDWORD) & regsize);
118 RegQueryValueExA(hKey, 119 if (regresult == ERROR_SUCCESS) {
119 REGSTR_VAL_JOYOEMNAME, 120 /*
120 0, 0, NULL, 121 allocate enough memory
121 (LPDWORD) &regsize); 122 for the OEM name text ...
122 if (regresult == ERROR_SUCCESS) 123 */
123 { 124 name = (char *) SDL_malloc (regsize);
124 /* 125 /* ... and read it from the registry */
125 allocate enough memory 126 regresult =
126 for the OEM name text ... 127 RegQueryValueExA (hKey,
127 */ 128 REGSTR_VAL_JOYOEMNAME, 0, 0,
128 name = (char *) SDL_malloc(regsize); 129 (LPBYTE) name, (LPDWORD) & regsize);
129 /* ... and read it from the registry */ 130 RegCloseKey (hKey);
130 regresult = 131 }
131 RegQueryValueExA(hKey, 132 }
132 REGSTR_VAL_JOYOEMNAME, 0, 0, 133 }
133 (LPBYTE) name, 134 }
134 (LPDWORD) &regsize); 135 return (name);
135 RegCloseKey(hKey);
136 }
137 }
138 }
139 }
140 return(name);
141 } 136 }
142 137
143 /* Function to scan the system for joysticks. 138 /* Function to scan the system for joysticks.
144 * This function should set SDL_numjoysticks to the number of available 139 * This function should set SDL_numjoysticks to the number of available
145 * joysticks. Joystick 0 should be the system default joystick. 140 * joysticks. Joystick 0 should be the system default joystick.
146 * It should return 0, or -1 on an unrecoverable fatal error. 141 * It should return 0, or -1 on an unrecoverable fatal error.
147 */ 142 */
148 int SDL_SYS_JoystickInit(void) 143 int
149 { 144 SDL_SYS_JoystickInit (void)
150 int i; 145 {
151 int maxdevs; 146 int i;
152 int numdevs; 147 int maxdevs;
153 JOYINFOEX joyinfo; 148 int numdevs;
154 JOYCAPS joycaps; 149 JOYINFOEX joyinfo;
155 MMRESULT result; 150 JOYCAPS joycaps;
156 151 MMRESULT result;
157 /* Reset the joystick ID & name mapping tables */ 152
158 for ( i = 0; i < MAX_JOYSTICKS; ++i ) { 153 /* Reset the joystick ID & name mapping tables */
159 SYS_JoystickID[i] = 0; 154 for (i = 0; i < MAX_JOYSTICKS; ++i) {
160 SYS_JoystickName[i] = NULL; 155 SYS_JoystickID[i] = 0;
161 } 156 SYS_JoystickName[i] = NULL;
162 157 }
163 /* Loop over all potential joystick devices */ 158
164 numdevs = 0; 159 /* Loop over all potential joystick devices */
165 maxdevs = joyGetNumDevs(); 160 numdevs = 0;
166 for ( i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i ) { 161 maxdevs = joyGetNumDevs ();
167 162 for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) {
168 joyinfo.dwSize = sizeof(joyinfo); 163
169 joyinfo.dwFlags = JOY_RETURNALL; 164 joyinfo.dwSize = sizeof (joyinfo);
170 result = joyGetPosEx(SYS_JoystickID[i], &joyinfo); 165 joyinfo.dwFlags = JOY_RETURNALL;
171 if ( result == JOYERR_NOERROR ) { 166 result = joyGetPosEx (SYS_JoystickID[i], &joyinfo);
172 result = joyGetDevCaps(i, &joycaps, sizeof(joycaps)); 167 if (result == JOYERR_NOERROR) {
173 if ( result == JOYERR_NOERROR ) { 168 result = joyGetDevCaps (i, &joycaps, sizeof (joycaps));
174 SYS_JoystickID[numdevs] = i; 169 if (result == JOYERR_NOERROR) {
175 SYS_Joystick[numdevs] = joycaps; 170 SYS_JoystickID[numdevs] = i;
176 SYS_JoystickName[numdevs] = GetJoystickName(i, joycaps.szRegKey); 171 SYS_Joystick[numdevs] = joycaps;
177 numdevs++; 172 SYS_JoystickName[numdevs] =
178 } 173 GetJoystickName (i, joycaps.szRegKey);
179 } 174 numdevs++;
180 } 175 }
181 return(numdevs); 176 }
177 }
178 return (numdevs);
182 } 179 }
183 180
184 /* Function to get the device-dependent name of a joystick */ 181 /* Function to get the device-dependent name of a joystick */
185 const char *SDL_SYS_JoystickName(int index) 182 const char *
186 { 183 SDL_SYS_JoystickName (int index)
187 if ( SYS_JoystickName[index] != NULL ) { 184 {
188 return(SYS_JoystickName[index]); 185 if (SYS_JoystickName[index] != NULL) {
189 } else { 186 return (SYS_JoystickName[index]);
190 return(SYS_Joystick[index].szPname); 187 } else {
191 } 188 return (SYS_Joystick[index].szPname);
189 }
192 } 190 }
193 191
194 /* Function to open a joystick for use. 192 /* Function to open a joystick for use.
195 The joystick to open is specified by the index field of the joystick. 193 The joystick to open is specified by the index field of the joystick.
196 This should fill the nbuttons and naxes fields of the joystick structure. 194 This should fill the nbuttons and naxes fields of the joystick structure.
197 It returns 0, or -1 if there is an error. 195 It returns 0, or -1 if there is an error.
198 */ 196 */
199 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) 197 int
200 { 198 SDL_SYS_JoystickOpen (SDL_Joystick * joystick)
201 int index, i; 199 {
202 int caps_flags[MAX_AXES-2] = 200 int index, i;
203 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; 201 int caps_flags[MAX_AXES - 2] =
204 int axis_min[MAX_AXES], axis_max[MAX_AXES]; 202 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
205 203 int axis_min[MAX_AXES], axis_max[MAX_AXES];
206 204
207 /* shortcut */ 205
208 index = joystick->index; 206 /* shortcut */
209 axis_min[0] = SYS_Joystick[index].wXmin; 207 index = joystick->index;
210 axis_max[0] = SYS_Joystick[index].wXmax; 208 axis_min[0] = SYS_Joystick[index].wXmin;
211 axis_min[1] = SYS_Joystick[index].wYmin; 209 axis_max[0] = SYS_Joystick[index].wXmax;
212 axis_max[1] = SYS_Joystick[index].wYmax; 210 axis_min[1] = SYS_Joystick[index].wYmin;
213 axis_min[2] = SYS_Joystick[index].wZmin; 211 axis_max[1] = SYS_Joystick[index].wYmax;
214 axis_max[2] = SYS_Joystick[index].wZmax; 212 axis_min[2] = SYS_Joystick[index].wZmin;
215 axis_min[3] = SYS_Joystick[index].wRmin; 213 axis_max[2] = SYS_Joystick[index].wZmax;
216 axis_max[3] = SYS_Joystick[index].wRmax; 214 axis_min[3] = SYS_Joystick[index].wRmin;
217 axis_min[4] = SYS_Joystick[index].wUmin; 215 axis_max[3] = SYS_Joystick[index].wRmax;
218 axis_max[4] = SYS_Joystick[index].wUmax; 216 axis_min[4] = SYS_Joystick[index].wUmin;
219 axis_min[5] = SYS_Joystick[index].wVmin; 217 axis_max[4] = SYS_Joystick[index].wUmax;
220 axis_max[5] = SYS_Joystick[index].wVmax; 218 axis_min[5] = SYS_Joystick[index].wVmin;
221 219 axis_max[5] = SYS_Joystick[index].wVmax;
222 /* allocate memory for system specific hardware data */ 220
223 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); 221 /* allocate memory for system specific hardware data */
224 if (joystick->hwdata == NULL) 222 joystick->hwdata =
225 { 223 (struct joystick_hwdata *) SDL_malloc (sizeof (*joystick->hwdata));
226 SDL_OutOfMemory(); 224 if (joystick->hwdata == NULL) {
227 return(-1); 225 SDL_OutOfMemory ();
228 } 226 return (-1);
229 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); 227 }
230 228 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata));
231 /* set hardware data */ 229
232 joystick->hwdata->id = SYS_JoystickID[index]; 230 /* set hardware data */
233 for ( i = 0; i < MAX_AXES; ++i ) { 231 joystick->hwdata->id = SYS_JoystickID[index];
234 if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) { 232 for (i = 0; i < MAX_AXES; ++i) {
235 joystick->hwdata->transaxis[i].offset = 233 if ((i < 2) || (SYS_Joystick[index].wCaps & caps_flags[i - 2])) {
236 AXIS_MIN - axis_min[i]; 234 joystick->hwdata->transaxis[i].offset = AXIS_MIN - axis_min[i];
237 joystick->hwdata->transaxis[i].scale = 235 joystick->hwdata->transaxis[i].scale =
238 (float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]); 236 (float) (AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]);
239 } else { 237 } else {
240 joystick->hwdata->transaxis[i].offset = 0; 238 joystick->hwdata->transaxis[i].offset = 0;
241 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */ 239 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */
242 } 240 }
243 } 241 }
244 242
245 /* fill nbuttons, naxes, and nhats fields */ 243 /* fill nbuttons, naxes, and nhats fields */
246 joystick->nbuttons = SYS_Joystick[index].wNumButtons; 244 joystick->nbuttons = SYS_Joystick[index].wNumButtons;
247 joystick->naxes = SYS_Joystick[index].wNumAxes; 245 joystick->naxes = SYS_Joystick[index].wNumAxes;
248 if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) { 246 if (SYS_Joystick[index].wCaps & JOYCAPS_HASPOV) {
249 joystick->nhats = 1; 247 joystick->nhats = 1;
250 } else { 248 } else {
251 joystick->nhats = 0; 249 joystick->nhats = 0;
252 } 250 }
253 return(0); 251 return (0);
254 } 252 }
255 253
256 static Uint8 TranslatePOV(DWORD value) 254 static Uint8
257 { 255 TranslatePOV (DWORD value)
258 Uint8 pos; 256 {
259 257 Uint8 pos;
260 pos = SDL_HAT_CENTERED; 258
261 if ( value != JOY_POVCENTERED ) { 259 pos = SDL_HAT_CENTERED;
262 if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) { 260 if (value != JOY_POVCENTERED) {
263 pos |= SDL_HAT_UP; 261 if ((value > JOY_POVLEFT) || (value < JOY_POVRIGHT)) {
264 } 262 pos |= SDL_HAT_UP;
265 if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) { 263 }
266 pos |= SDL_HAT_RIGHT; 264 if ((value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD)) {
267 } 265 pos |= SDL_HAT_RIGHT;
268 if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) { 266 }
269 pos |= SDL_HAT_DOWN; 267 if ((value > JOY_POVRIGHT) && (value < JOY_POVLEFT)) {
270 } 268 pos |= SDL_HAT_DOWN;
271 if ( value > JOY_POVBACKWARD ) { 269 }
272 pos |= SDL_HAT_LEFT; 270 if (value > JOY_POVBACKWARD) {
273 } 271 pos |= SDL_HAT_LEFT;
274 } 272 }
275 return(pos); 273 }
274 return (pos);
276 } 275 }
277 276
278 /* Function to update the state of a joystick - called as a device poll. 277 /* Function to update the state of a joystick - called as a device poll.
279 * This function shouldn't update the joystick structure directly, 278 * This function shouldn't update the joystick structure directly,
280 * but instead should call SDL_PrivateJoystick*() to deliver events 279 * but instead should call SDL_PrivateJoystick*() to deliver events
281 * and update joystick device state. 280 * and update joystick device state.
282 */ 281 */
283 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) 282 void
284 { 283 SDL_SYS_JoystickUpdate (SDL_Joystick * joystick)
285 MMRESULT result; 284 {
286 int i; 285 MMRESULT result;
287 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, 286 int i;
288 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; 287 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
289 DWORD pos[MAX_AXES]; 288 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
290 struct _transaxis *transaxis; 289 };
291 int value, change; 290 DWORD pos[MAX_AXES];
292 JOYINFOEX joyinfo; 291 struct _transaxis *transaxis;
293 292 int value, change;
294 joyinfo.dwSize = sizeof(joyinfo); 293 JOYINFOEX joyinfo;
295 joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS; 294
296 if ( ! joystick->hats ) { 295 joyinfo.dwSize = sizeof (joyinfo);
297 joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS); 296 joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
298 } 297 if (!joystick->hats) {
299 result = joyGetPosEx(joystick->hwdata->id, &joyinfo); 298 joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
300 if ( result != JOYERR_NOERROR ) { 299 }
301 SetMMerror("joyGetPosEx", result); 300 result = joyGetPosEx (joystick->hwdata->id, &joyinfo);
302 return; 301 if (result != JOYERR_NOERROR) {
303 } 302 SetMMerror ("joyGetPosEx", result);
304 303 return;
305 /* joystick motion events */ 304 }
306 pos[0] = joyinfo.dwXpos; 305
307 pos[1] = joyinfo.dwYpos; 306 /* joystick motion events */
308 pos[2] = joyinfo.dwZpos; 307 pos[0] = joyinfo.dwXpos;
309 pos[3] = joyinfo.dwRpos; 308 pos[1] = joyinfo.dwYpos;
310 pos[4] = joyinfo.dwUpos; 309 pos[2] = joyinfo.dwZpos;
311 pos[5] = joyinfo.dwVpos; 310 pos[3] = joyinfo.dwRpos;
312 311 pos[4] = joyinfo.dwUpos;
313 transaxis = joystick->hwdata->transaxis; 312 pos[5] = joyinfo.dwVpos;
314 for (i = 0; i < joystick->naxes; i++) { 313
315 if (joyinfo.dwFlags & flags[i]) { 314 transaxis = joystick->hwdata->transaxis;
316 value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale); 315 for (i = 0; i < joystick->naxes; i++) {
317 change = (value - joystick->axes[i]); 316 if (joyinfo.dwFlags & flags[i]) {
318 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { 317 value =
319 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); 318 (int) (((float) pos[i] +
320 } 319 transaxis[i].offset) * transaxis[i].scale);
321 } 320 change = (value - joystick->axes[i]);
322 } 321 if ((change < -JOY_AXIS_THRESHOLD)
323 322 || (change > JOY_AXIS_THRESHOLD)) {
324 /* joystick button events */ 323 SDL_PrivateJoystickAxis (joystick, (Uint8) i, (Sint16) value);
325 if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) { 324 }
326 for ( i = 0; i < joystick->nbuttons; ++i ) { 325 }
327 if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) { 326 }
328 if ( ! joystick->buttons[i] ) { 327
329 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED); 328 /* joystick button events */
330 } 329 if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
331 } else { 330 for (i = 0; i < joystick->nbuttons; ++i) {
332 if ( joystick->buttons[i] ) { 331 if (joyinfo.dwButtons & JOY_BUTTON_FLAG (i)) {
333 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED); 332 if (!joystick->buttons[i]) {
334 } 333 SDL_PrivateJoystickButton (joystick, (Uint8) i,
335 } 334 SDL_PRESSED);
336 } 335 }
337 } 336 } else {
338 337 if (joystick->buttons[i]) {
339 /* joystick hat events */ 338 SDL_PrivateJoystickButton (joystick, (Uint8) i,
340 if ( joyinfo.dwFlags & JOY_RETURNPOV ) { 339 SDL_RELEASED);
341 Uint8 pos; 340 }
342 341 }
343 pos = TranslatePOV(joyinfo.dwPOV); 342 }
344 if ( pos != joystick->hats[0] ) { 343 }
345 SDL_PrivateJoystickHat(joystick, 0, pos); 344
346 } 345 /* joystick hat events */
347 } 346 if (joyinfo.dwFlags & JOY_RETURNPOV) {
347 Uint8 pos;
348
349 pos = TranslatePOV (joyinfo.dwPOV);
350 if (pos != joystick->hats[0]) {
351 SDL_PrivateJoystickHat (joystick, 0, pos);
352 }
353 }
348 } 354 }
349 355
350 /* Function to close a joystick after use */ 356 /* Function to close a joystick after use */
351 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) 357 void
352 { 358 SDL_SYS_JoystickClose (SDL_Joystick * joystick)
353 if (joystick->hwdata != NULL) { 359 {
354 /* free system specific hardware data */ 360 if (joystick->hwdata != NULL) {
355 SDL_free(joystick->hwdata); 361 /* free system specific hardware data */
356 } 362 SDL_free (joystick->hwdata);
363 }
357 } 364 }
358 365
359 /* Function to perform any system-specific joystick related cleanup */ 366 /* Function to perform any system-specific joystick related cleanup */
360 void SDL_SYS_JoystickQuit(void) 367 void
361 { 368 SDL_SYS_JoystickQuit (void)
362 int i; 369 {
363 for (i = 0; i < MAX_JOYSTICKS; i++) { 370 int i;
364 if ( SYS_JoystickName[i] != NULL ) { 371 for (i = 0; i < MAX_JOYSTICKS; i++) {
365 SDL_free(SYS_JoystickName[i]); 372 if (SYS_JoystickName[i] != NULL) {
366 } 373 SDL_free (SYS_JoystickName[i]);
367 } 374 }
375 }
368 } 376 }
369 377
370 378
371 /* implementation functions */ 379 /* implementation functions */
372 void SetMMerror(char *function, int code) 380 void
373 { 381 SetMMerror (char *function, int code)
374 static char *error; 382 {
375 static char errbuf[1024]; 383 static char *error;
376 384 static char errbuf[1024];
377 errbuf[0] = 0; 385
378 switch (code) 386 errbuf[0] = 0;
379 { 387 switch (code) {
380 case MMSYSERR_NODRIVER: 388 case MMSYSERR_NODRIVER:
381 error = "Joystick driver not present"; 389 error = "Joystick driver not present";
382 break; 390 break;
383 391
384 case MMSYSERR_INVALPARAM: 392 case MMSYSERR_INVALPARAM:
385 case JOYERR_PARMS: 393 case JOYERR_PARMS:
386 error = "Invalid parameter(s)"; 394 error = "Invalid parameter(s)";
387 break; 395 break;
388 396
389 case MMSYSERR_BADDEVICEID: 397 case MMSYSERR_BADDEVICEID:
390 error = "Bad device ID"; 398 error = "Bad device ID";
391 break; 399 break;
392 400
393 case JOYERR_UNPLUGGED: 401 case JOYERR_UNPLUGGED:
394 error = "Joystick not attached"; 402 error = "Joystick not attached";
395 break; 403 break;
396 404
397 case JOYERR_NOCANDO: 405 case JOYERR_NOCANDO:
398 error = "Can't capture joystick input"; 406 error = "Can't capture joystick input";
399 break; 407 break;
400 408
401 default: 409 default:
402 SDL_snprintf(errbuf, SDL_arraysize(errbuf), 410 SDL_snprintf (errbuf, SDL_arraysize (errbuf),
403 "%s: Unknown Multimedia system error: 0x%x", 411 "%s: Unknown Multimedia system error: 0x%x",
404 function, code); 412 function, code);
405 break; 413 break;
406 } 414 }
407 415
408 if ( ! errbuf[0] ) { 416 if (!errbuf[0]) {
409 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); 417 SDL_snprintf (errbuf, SDL_arraysize (errbuf), "%s: %s", function,
410 } 418 error);
411 SDL_SetError("%s", errbuf); 419 }
420 SDL_SetError ("%s", errbuf);
412 } 421 }
413 422
414 #endif /* SDL_JOYSTICK_WINMM */ 423 #endif /* SDL_JOYSTICK_WINMM */
424 /* vi: set ts=4 sw=4 expandtab: */