comparison src/joystick/win32/SDL_mmjoystick.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
64 float scale; 64 float scale;
65 } transaxis[6]; 65 } transaxis[6];
66 }; 66 };
67 67
68 /* Convert a win32 Multimedia API return code to a text message */ 68 /* Convert a win32 Multimedia API return code to a text message */
69 static void SetMMerror (char *function, int code); 69 static void SetMMerror(char *function, int code);
70 70
71 71
72 static char * 72 static char *
73 GetJoystickName (int index, const char *szRegKey) 73 GetJoystickName(int index, const char *szRegKey)
74 { 74 {
75 /* added 7/24/2004 by Eckhard Stolberg */ 75 /* added 7/24/2004 by Eckhard Stolberg */
76 /* 76 /*
77 see if there is a joystick for the current 77 see if there is a joystick for the current
78 index (1-16) listed in the registry 78 index (1-16) listed in the registry
83 LONG regresult; 83 LONG regresult;
84 unsigned char regkey[256]; 84 unsigned char regkey[256];
85 unsigned char regvalue[256]; 85 unsigned char regvalue[256];
86 unsigned char regname[256]; 86 unsigned char regname[256];
87 87
88 SDL_snprintf ((char *) regkey, SDL_arraysize (regkey), "%s\\%s\\%s", 88 SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
89 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR); 89 REGSTR_PATH_JOYCONFIG, szRegKey, 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 find the registry key name for the
95 joystick's properties 95 joystick's properties
96 */ 96 */
97 regsize = sizeof (regname); 97 regsize = sizeof(regname);
98 SDL_snprintf ((char *) regvalue, SDL_arraysize (regvalue), 98 SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue),
99 "Joystick%d%s", index + 1, REGSTR_VAL_JOYOEMNAME); 99 "Joystick%d%s", index + 1, REGSTR_VAL_JOYOEMNAME);
100 regresult = RegQueryValueExA (hKey, 100 regresult = RegQueryValueExA(hKey,
101 (char *) regvalue, 0, 0, 101 (char *) regvalue, 0, 0,
102 (LPBYTE) & regname, 102 (LPBYTE) & regname, (LPDWORD) & regsize);
103 (LPDWORD) & regsize); 103 RegCloseKey(hKey);
104 RegCloseKey (hKey);
105 if (regresult == ERROR_SUCCESS) { 104 if (regresult == ERROR_SUCCESS) {
106 /* open that registry key */ 105 /* open that registry key */
107 SDL_snprintf ((char *) regkey, SDL_arraysize (regkey), 106 SDL_snprintf((char *) regkey, SDL_arraysize(regkey),
108 "%s\\%s", REGSTR_PATH_JOYOEM, regname); 107 "%s\\%s", REGSTR_PATH_JOYOEM, regname);
109 regresult = 108 regresult =
110 RegOpenKeyExA (HKEY_LOCAL_MACHINE, (char *) regkey, 0, 109 RegOpenKeyExA(HKEY_LOCAL_MACHINE, (char *) regkey, 0,
111 KEY_READ, &hKey); 110 KEY_READ, &hKey);
112 if (regresult == ERROR_SUCCESS) { 111 if (regresult == ERROR_SUCCESS) {
113 /* find the size for the OEM name text */ 112 /* find the size for the OEM name text */
114 regsize = sizeof (regvalue); 113 regsize = sizeof(regvalue);
115 regresult = 114 regresult =
116 RegQueryValueExA (hKey, 115 RegQueryValueExA(hKey,
117 REGSTR_VAL_JOYOEMNAME, 116 REGSTR_VAL_JOYOEMNAME,
118 0, 0, NULL, (LPDWORD) & regsize); 117 0, 0, NULL, (LPDWORD) & regsize);
119 if (regresult == ERROR_SUCCESS) { 118 if (regresult == ERROR_SUCCESS) {
120 /* 119 /*
121 allocate enough memory 120 allocate enough memory
122 for the OEM name text ... 121 for the OEM name text ...
123 */ 122 */
124 name = (char *) SDL_malloc (regsize); 123 name = (char *) SDL_malloc(regsize);
125 /* ... and read it from the registry */ 124 /* ... and read it from the registry */
126 regresult = 125 regresult =
127 RegQueryValueExA (hKey, 126 RegQueryValueExA(hKey,
128 REGSTR_VAL_JOYOEMNAME, 0, 0, 127 REGSTR_VAL_JOYOEMNAME, 0, 0,
129 (LPBYTE) name, (LPDWORD) & regsize); 128 (LPBYTE) name, (LPDWORD) & regsize);
130 RegCloseKey (hKey); 129 RegCloseKey(hKey);
131 } 130 }
132 } 131 }
133 } 132 }
134 } 133 }
135 return (name); 134 return (name);
139 * This function should set SDL_numjoysticks to the number of available 138 * This function should set SDL_numjoysticks to the number of available
140 * joysticks. Joystick 0 should be the system default joystick. 139 * joysticks. Joystick 0 should be the system default joystick.
141 * It should return 0, or -1 on an unrecoverable fatal error. 140 * It should return 0, or -1 on an unrecoverable fatal error.
142 */ 141 */
143 int 142 int
144 SDL_SYS_JoystickInit (void) 143 SDL_SYS_JoystickInit(void)
145 { 144 {
146 int i; 145 int i;
147 int maxdevs; 146 int maxdevs;
148 int numdevs; 147 int numdevs;
149 JOYINFOEX joyinfo; 148 JOYINFOEX joyinfo;
156 SYS_JoystickName[i] = NULL; 155 SYS_JoystickName[i] = NULL;
157 } 156 }
158 157
159 /* Loop over all potential joystick devices */ 158 /* Loop over all potential joystick devices */
160 numdevs = 0; 159 numdevs = 0;
161 maxdevs = joyGetNumDevs (); 160 maxdevs = joyGetNumDevs();
162 for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) { 161 for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) {
163 162
164 joyinfo.dwSize = sizeof (joyinfo); 163 joyinfo.dwSize = sizeof(joyinfo);
165 joyinfo.dwFlags = JOY_RETURNALL; 164 joyinfo.dwFlags = JOY_RETURNALL;
166 result = joyGetPosEx (SYS_JoystickID[i], &joyinfo); 165 result = joyGetPosEx(SYS_JoystickID[i], &joyinfo);
167 if (result == JOYERR_NOERROR) { 166 if (result == JOYERR_NOERROR) {
168 result = joyGetDevCaps (i, &joycaps, sizeof (joycaps)); 167 result = joyGetDevCaps(i, &joycaps, sizeof(joycaps));
169 if (result == JOYERR_NOERROR) { 168 if (result == JOYERR_NOERROR) {
170 SYS_JoystickID[numdevs] = i; 169 SYS_JoystickID[numdevs] = i;
171 SYS_Joystick[numdevs] = joycaps; 170 SYS_Joystick[numdevs] = joycaps;
172 SYS_JoystickName[numdevs] = 171 SYS_JoystickName[numdevs] =
173 GetJoystickName (i, joycaps.szRegKey); 172 GetJoystickName(i, joycaps.szRegKey);
174 numdevs++; 173 numdevs++;
175 } 174 }
176 } 175 }
177 } 176 }
178 return (numdevs); 177 return (numdevs);
179 } 178 }
180 179
181 /* Function to get the device-dependent name of a joystick */ 180 /* Function to get the device-dependent name of a joystick */
182 const char * 181 const char *
183 SDL_SYS_JoystickName (int index) 182 SDL_SYS_JoystickName(int index)
184 { 183 {
185 if (SYS_JoystickName[index] != NULL) { 184 if (SYS_JoystickName[index] != NULL) {
186 return (SYS_JoystickName[index]); 185 return (SYS_JoystickName[index]);
187 } else { 186 } else {
188 return (SYS_Joystick[index].szPname); 187 return (SYS_Joystick[index].szPname);
193 The joystick to open is specified by the index field of the joystick. 192 The joystick to open is specified by the index field of the joystick.
194 This should fill the nbuttons and naxes fields of the joystick structure. 193 This should fill the nbuttons and naxes fields of the joystick structure.
195 It returns 0, or -1 if there is an error. 194 It returns 0, or -1 if there is an error.
196 */ 195 */
197 int 196 int
198 SDL_SYS_JoystickOpen (SDL_Joystick * joystick) 197 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
199 { 198 {
200 int index, i; 199 int index, i;
201 int caps_flags[MAX_AXES - 2] = 200 int caps_flags[MAX_AXES - 2] =
202 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; 201 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
203 int axis_min[MAX_AXES], axis_max[MAX_AXES]; 202 int axis_min[MAX_AXES], axis_max[MAX_AXES];
218 axis_min[5] = SYS_Joystick[index].wVmin; 217 axis_min[5] = SYS_Joystick[index].wVmin;
219 axis_max[5] = SYS_Joystick[index].wVmax; 218 axis_max[5] = SYS_Joystick[index].wVmax;
220 219
221 /* allocate memory for system specific hardware data */ 220 /* allocate memory for system specific hardware data */
222 joystick->hwdata = 221 joystick->hwdata =
223 (struct joystick_hwdata *) SDL_malloc (sizeof (*joystick->hwdata)); 222 (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
224 if (joystick->hwdata == NULL) { 223 if (joystick->hwdata == NULL) {
225 SDL_OutOfMemory (); 224 SDL_OutOfMemory();
226 return (-1); 225 return (-1);
227 } 226 }
228 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata)); 227 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
229 228
230 /* set hardware data */ 229 /* set hardware data */
231 joystick->hwdata->id = SYS_JoystickID[index]; 230 joystick->hwdata->id = SYS_JoystickID[index];
232 for (i = 0; i < MAX_AXES; ++i) { 231 for (i = 0; i < MAX_AXES; ++i) {
233 if ((i < 2) || (SYS_Joystick[index].wCaps & caps_flags[i - 2])) { 232 if ((i < 2) || (SYS_Joystick[index].wCaps & caps_flags[i - 2])) {
250 } 249 }
251 return (0); 250 return (0);
252 } 251 }
253 252
254 static Uint8 253 static Uint8
255 TranslatePOV (DWORD value) 254 TranslatePOV(DWORD value)
256 { 255 {
257 Uint8 pos; 256 Uint8 pos;
258 257
259 pos = SDL_HAT_CENTERED; 258 pos = SDL_HAT_CENTERED;
260 if (value != JOY_POVCENTERED) { 259 if (value != JOY_POVCENTERED) {
278 * This function shouldn't update the joystick structure directly, 277 * This function shouldn't update the joystick structure directly,
279 * but instead should call SDL_PrivateJoystick*() to deliver events 278 * but instead should call SDL_PrivateJoystick*() to deliver events
280 * and update joystick device state. 279 * and update joystick device state.
281 */ 280 */
282 void 281 void
283 SDL_SYS_JoystickUpdate (SDL_Joystick * joystick) 282 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
284 { 283 {
285 MMRESULT result; 284 MMRESULT result;
286 int i; 285 int i;
287 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, 286 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
288 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV 287 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
290 DWORD pos[MAX_AXES]; 289 DWORD pos[MAX_AXES];
291 struct _transaxis *transaxis; 290 struct _transaxis *transaxis;
292 int value, change; 291 int value, change;
293 JOYINFOEX joyinfo; 292 JOYINFOEX joyinfo;
294 293
295 joyinfo.dwSize = sizeof (joyinfo); 294 joyinfo.dwSize = sizeof(joyinfo);
296 joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS; 295 joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
297 if (!joystick->hats) { 296 if (!joystick->hats) {
298 joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS); 297 joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
299 } 298 }
300 result = joyGetPosEx (joystick->hwdata->id, &joyinfo); 299 result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
301 if (result != JOYERR_NOERROR) { 300 if (result != JOYERR_NOERROR) {
302 SetMMerror ("joyGetPosEx", result); 301 SetMMerror("joyGetPosEx", result);
303 return; 302 return;
304 } 303 }
305 304
306 /* joystick motion events */ 305 /* joystick motion events */
307 pos[0] = joyinfo.dwXpos; 306 pos[0] = joyinfo.dwXpos;
318 (int) (((float) pos[i] + 317 (int) (((float) pos[i] +
319 transaxis[i].offset) * transaxis[i].scale); 318 transaxis[i].offset) * transaxis[i].scale);
320 change = (value - joystick->axes[i]); 319 change = (value - joystick->axes[i]);
321 if ((change < -JOY_AXIS_THRESHOLD) 320 if ((change < -JOY_AXIS_THRESHOLD)
322 || (change > JOY_AXIS_THRESHOLD)) { 321 || (change > JOY_AXIS_THRESHOLD)) {
323 SDL_PrivateJoystickAxis (joystick, (Uint8) i, (Sint16) value); 322 SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
324 } 323 }
325 } 324 }
326 } 325 }
327 326
328 /* joystick button events */ 327 /* joystick button events */
329 if (joyinfo.dwFlags & JOY_RETURNBUTTONS) { 328 if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
330 for (i = 0; i < joystick->nbuttons; ++i) { 329 for (i = 0; i < joystick->nbuttons; ++i) {
331 if (joyinfo.dwButtons & JOY_BUTTON_FLAG (i)) { 330 if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
332 if (!joystick->buttons[i]) { 331 if (!joystick->buttons[i]) {
333 SDL_PrivateJoystickButton (joystick, (Uint8) i, 332 SDL_PrivateJoystickButton(joystick, (Uint8) i,
334 SDL_PRESSED); 333 SDL_PRESSED);
335 } 334 }
336 } else { 335 } else {
337 if (joystick->buttons[i]) { 336 if (joystick->buttons[i]) {
338 SDL_PrivateJoystickButton (joystick, (Uint8) i, 337 SDL_PrivateJoystickButton(joystick, (Uint8) i,
339 SDL_RELEASED); 338 SDL_RELEASED);
340 } 339 }
341 } 340 }
342 } 341 }
343 } 342 }
344 343
345 /* joystick hat events */ 344 /* joystick hat events */
346 if (joyinfo.dwFlags & JOY_RETURNPOV) { 345 if (joyinfo.dwFlags & JOY_RETURNPOV) {
347 Uint8 pos; 346 Uint8 pos;
348 347
349 pos = TranslatePOV (joyinfo.dwPOV); 348 pos = TranslatePOV(joyinfo.dwPOV);
350 if (pos != joystick->hats[0]) { 349 if (pos != joystick->hats[0]) {
351 SDL_PrivateJoystickHat (joystick, 0, pos); 350 SDL_PrivateJoystickHat(joystick, 0, pos);
352 } 351 }
353 } 352 }
354 } 353 }
355 354
356 /* Function to close a joystick after use */ 355 /* Function to close a joystick after use */
357 void 356 void
358 SDL_SYS_JoystickClose (SDL_Joystick * joystick) 357 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
359 { 358 {
360 if (joystick->hwdata != NULL) { 359 if (joystick->hwdata != NULL) {
361 /* free system specific hardware data */ 360 /* free system specific hardware data */
362 SDL_free (joystick->hwdata); 361 SDL_free(joystick->hwdata);
363 } 362 }
364 } 363 }
365 364
366 /* Function to perform any system-specific joystick related cleanup */ 365 /* Function to perform any system-specific joystick related cleanup */
367 void 366 void
368 SDL_SYS_JoystickQuit (void) 367 SDL_SYS_JoystickQuit(void)
369 { 368 {
370 int i; 369 int i;
371 for (i = 0; i < MAX_JOYSTICKS; i++) { 370 for (i = 0; i < MAX_JOYSTICKS; i++) {
372 if (SYS_JoystickName[i] != NULL) { 371 if (SYS_JoystickName[i] != NULL) {
373 SDL_free (SYS_JoystickName[i]); 372 SDL_free(SYS_JoystickName[i]);
374 } 373 }
375 } 374 }
376 } 375 }
377 376
378 377
379 /* implementation functions */ 378 /* implementation functions */
380 void 379 void
381 SetMMerror (char *function, int code) 380 SetMMerror(char *function, int code)
382 { 381 {
383 static char *error; 382 static char *error;
384 static char errbuf[1024]; 383 static char errbuf[1024];
385 384
386 errbuf[0] = 0; 385 errbuf[0] = 0;
405 case JOYERR_NOCANDO: 404 case JOYERR_NOCANDO:
406 error = "Can't capture joystick input"; 405 error = "Can't capture joystick input";
407 break; 406 break;
408 407
409 default: 408 default:
410 SDL_snprintf (errbuf, SDL_arraysize (errbuf), 409 SDL_snprintf(errbuf, SDL_arraysize(errbuf),
411 "%s: Unknown Multimedia system error: 0x%x", 410 "%s: Unknown Multimedia system error: 0x%x",
412 function, code); 411 function, code);
413 break; 412 break;
414 } 413 }
415 414
416 if (!errbuf[0]) { 415 if (!errbuf[0]) {
417 SDL_snprintf (errbuf, SDL_arraysize (errbuf), "%s: %s", function, 416 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
418 error); 417 error);
419 } 418 }
420 SDL_SetError ("%s", errbuf); 419 SDL_SetError("%s", errbuf);
421 } 420 }
422 421
423 #endif /* SDL_JOYSTICK_WINMM */ 422 #endif /* SDL_JOYSTICK_WINMM */
424 /* vi: set ts=4 sw=4 expandtab: */ 423 /* vi: set ts=4 sw=4 expandtab: */