Skip to content

Latest commit

 

History

History
419 lines (367 loc) · 11 KB

SDL_mmjoystick.c

File metadata and controls

419 lines (367 loc) · 11 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
22
23
24
25
*/
/* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */
#include "SDL_error.h"
Feb 6, 2006
Feb 6, 2006
26
#include "SDL_events.h"
Apr 26, 2001
Apr 26, 2001
27
#include "SDL_joystick.h"
Feb 6, 2006
Feb 6, 2006
28
29
#include "SDL_stdlib.h"
#include "SDL_string.h"
Apr 26, 2001
Apr 26, 2001
30
31
32
#include "SDL_sysjoystick.h"
#include "SDL_joystick_c.h"
Feb 6, 2006
Feb 6, 2006
33
#include "SDL_windows.h"
Apr 26, 2001
Apr 26, 2001
34
#include <mmsystem.h>
Aug 21, 2004
Aug 21, 2004
35
#include <regstr.h>
Apr 26, 2001
Apr 26, 2001
36
Oct 15, 2002
Oct 15, 2002
37
#define MAX_JOYSTICKS 16
Apr 26, 2001
Apr 26, 2001
38
39
40
41
#define MAX_AXES 6 /* each joystick can have up to 6 axes */
#define MAX_BUTTONS 32 /* and 32 buttons */
#define AXIS_MIN -32768 /* minimum value for axis coordinate */
#define AXIS_MAX 32767 /* maximum value for axis coordinate */
Feb 16, 2004
Feb 16, 2004
42
43
/* limit axis to 256 possible positions to filter out noise */
#define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256)
Apr 26, 2001
Apr 26, 2001
44
45
46
47
48
49
#define JOY_BUTTON_FLAG(n) (1<<n)
/* array to hold joystick ID values */
static UINT SYS_JoystickID[MAX_JOYSTICKS];
static JOYCAPS SYS_Joystick[MAX_JOYSTICKS];
Aug 21, 2004
Aug 21, 2004
50
static char *SYS_JoystickName[MAX_JOYSTICKS];
Apr 26, 2001
Apr 26, 2001
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
/* joystick ID */
UINT id;
/* values used to translate device-specific coordinates into
SDL-standard ranges */
struct _transaxis {
int offset;
float scale;
} transaxis[6];
};
/* Convert a win32 Multimedia API return code to a text message */
static void SetMMerror(char *function, int code);
Aug 21, 2004
Aug 21, 2004
70
static char *GetJoystickName(int index, const char *szRegKey)
Aug 21, 2004
Aug 21, 2004
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
/* added 7/24/2004 by Eckhard Stolberg */
/*
see if there is a joystick for the current
index (1-16) listed in the registry
*/
char *name = NULL;
HKEY hKey;
DWORD regsize;
LONG regresult;
unsigned char regkey[256];
unsigned char regvalue[256];
unsigned char regname[256];
Feb 7, 2006
Feb 7, 2006
85
SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
Aug 21, 2004
Aug 21, 2004
86
87
88
89
90
91
92
93
94
95
96
97
REGSTR_PATH_JOYCONFIG,
szRegKey,
REGSTR_KEY_JOYCURR);
regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
(LPTSTR) &regkey, 0, KEY_READ, &hKey);
if (regresult == ERROR_SUCCESS)
{
/*
find the registry key name for the
joystick's properties
*/
regsize = sizeof(regname);
Feb 7, 2006
Feb 7, 2006
98
SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue),
Aug 21, 2004
Aug 21, 2004
99
"Joystick%d%s", index+1,
Aug 21, 2004
Aug 21, 2004
100
101
REGSTR_VAL_JOYOEMNAME);
regresult = RegQueryValueExA(hKey,
Sep 8, 2005
Sep 8, 2005
102
(char *) regvalue, 0, 0, (LPBYTE) &regname,
Aug 21, 2004
Aug 21, 2004
103
104
105
106
107
(LPDWORD) &regsize);
RegCloseKey(hKey);
if (regresult == ERROR_SUCCESS)
{
/* open that registry key */
Feb 7, 2006
Feb 7, 2006
108
SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s",
Aug 21, 2004
Aug 21, 2004
109
110
REGSTR_PATH_JOYOEM, regname);
regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
Sep 8, 2005
Sep 8, 2005
111
(char *) regkey, 0, KEY_READ, &hKey);
Aug 21, 2004
Aug 21, 2004
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
if (regresult == ERROR_SUCCESS)
{
/* find the size for the OEM name text */
regsize = sizeof(regvalue);
regresult =
RegQueryValueExA(hKey,
REGSTR_VAL_JOYOEMNAME,
0, 0, NULL,
(LPDWORD) &regsize);
if (regresult == ERROR_SUCCESS)
{
/*
allocate enough memory
for the OEM name text ...
*/
Feb 7, 2006
Feb 7, 2006
127
name = (char *) SDL_malloc(regsize);
Aug 21, 2004
Aug 21, 2004
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ... and read it from the registry */
regresult =
RegQueryValueExA(hKey,
REGSTR_VAL_JOYOEMNAME, 0, 0,
(LPBYTE) name,
(LPDWORD) &regsize);
RegCloseKey(hKey);
}
}
}
}
return(name);
}
Apr 26, 2001
Apr 26, 2001
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
int SDL_SYS_JoystickInit(void)
{
int i;
int maxdevs;
int numdevs;
JOYINFOEX joyinfo;
JOYCAPS joycaps;
MMRESULT result;
numdevs = 0;
maxdevs = joyGetNumDevs();
Sep 4, 2001
Sep 4, 2001
158
Apr 26, 2001
Apr 26, 2001
159
160
161
162
if ( maxdevs > MAX_JOYSTICKS ) {
maxdevs = MAX_JOYSTICKS;
}
Sep 4, 2001
Sep 4, 2001
163
Oct 15, 2002
Oct 15, 2002
164
165
for ( i = 0; i < MAX_JOYSTICKS; i++ ) {
SYS_JoystickID[i] = JOYSTICKID1 + i;
Aug 21, 2004
Aug 21, 2004
166
SYS_JoystickName[i] = NULL;
Oct 15, 2002
Oct 15, 2002
167
168
}
Apr 26, 2001
Apr 26, 2001
169
170
for ( i = 0; (i < maxdevs); ++i ) {
Sep 4, 2001
Sep 4, 2001
171
172
173
174
175
176
/* added 8/31/2001 By Vitaliy Mikitchenko */
joyinfo.dwSize = sizeof(joyinfo);
joyinfo.dwFlags = JOY_RETURNALL;
/* end addition */
Apr 26, 2001
Apr 26, 2001
177
178
179
180
181
182
result = joyGetPosEx(SYS_JoystickID[i], &joyinfo);
if ( result == JOYERR_NOERROR ) {
result = joyGetDevCaps(SYS_JoystickID[i], &joycaps, sizeof(joycaps));
if ( result == JOYERR_NOERROR ) {
SYS_JoystickID[numdevs] = SYS_JoystickID[i];
SYS_Joystick[numdevs] = joycaps;
Aug 21, 2004
Aug 21, 2004
183
SYS_JoystickName[numdevs] = GetJoystickName(numdevs, joycaps.szRegKey);
Apr 26, 2001
Apr 26, 2001
184
185
186
187
188
189
190
191
192
193
numdevs++;
}
}
}
return(numdevs);
}
/* Function to get the device-dependent name of a joystick */
const char *SDL_SYS_JoystickName(int index)
{
Aug 21, 2004
Aug 21, 2004
194
195
if ( SYS_JoystickName[index] != NULL ) {
return(SYS_JoystickName[index]);
Aug 21, 2004
Aug 21, 2004
196
197
198
} else {
return(SYS_Joystick[index].szPname);
}
Apr 26, 2001
Apr 26, 2001
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
}
/* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick.
This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error.
*/
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
{
int index, i;
int caps_flags[MAX_AXES-2] =
{ JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
int axis_min[MAX_AXES], axis_max[MAX_AXES];
/* shortcut */
index = joystick->index;
axis_min[0] = SYS_Joystick[index].wXmin;
axis_max[0] = SYS_Joystick[index].wXmax;
axis_min[1] = SYS_Joystick[index].wYmin;
axis_max[1] = SYS_Joystick[index].wYmax;
axis_min[2] = SYS_Joystick[index].wZmin;
axis_max[2] = SYS_Joystick[index].wZmax;
axis_min[3] = SYS_Joystick[index].wRmin;
axis_max[3] = SYS_Joystick[index].wRmax;
axis_min[4] = SYS_Joystick[index].wUmin;
axis_max[4] = SYS_Joystick[index].wUmax;
axis_min[5] = SYS_Joystick[index].wVmin;
axis_max[5] = SYS_Joystick[index].wVmax;
/* allocate memory for system specific hardware data */
Feb 7, 2006
Feb 7, 2006
230
joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
Apr 26, 2001
Apr 26, 2001
231
232
233
234
235
if (joystick->hwdata == NULL)
{
SDL_OutOfMemory();
return(-1);
}
Feb 7, 2006
Feb 7, 2006
236
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
Apr 26, 2001
Apr 26, 2001
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* set hardware data */
joystick->hwdata->id = SYS_JoystickID[index];
for ( i = 0; i < MAX_AXES; ++i ) {
if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) {
joystick->hwdata->transaxis[i].offset =
AXIS_MIN - axis_min[i];
joystick->hwdata->transaxis[i].scale =
(float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]);
} else {
joystick->hwdata->transaxis[i].offset = 0;
joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */
}
}
/* fill nbuttons, naxes, and nhats fields */
joystick->nbuttons = SYS_Joystick[index].wNumButtons;
joystick->naxes = SYS_Joystick[index].wNumAxes;
if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) {
joystick->nhats = 1;
} else {
joystick->nhats = 0;
}
return(0);
}
static Uint8 TranslatePOV(DWORD value)
{
Uint8 pos;
pos = SDL_HAT_CENTERED;
if ( value != JOY_POVCENTERED ) {
if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) {
pos |= SDL_HAT_UP;
}
if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) {
pos |= SDL_HAT_RIGHT;
}
if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) {
pos |= SDL_HAT_DOWN;
}
if ( value > JOY_POVBACKWARD ) {
pos |= SDL_HAT_LEFT;
}
}
return(pos);
}
/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
* and update joystick device state.
*/
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
MMRESULT result;
int i;
DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
JOY_RETURNR, JOY_RETURNU, JOY_RETURNV };
DWORD pos[MAX_AXES];
struct _transaxis *transaxis;
int value, change;
JOYINFOEX joyinfo;
joyinfo.dwSize = sizeof(joyinfo);
joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS;
if ( ! joystick->hats ) {
joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS);
}
result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
if ( result != JOYERR_NOERROR ) {
SetMMerror("joyGetPosEx", result);
return;
}
/* joystick motion events */
pos[0] = joyinfo.dwXpos;
pos[1] = joyinfo.dwYpos;
pos[2] = joyinfo.dwZpos;
pos[3] = joyinfo.dwRpos;
pos[4] = joyinfo.dwUpos;
pos[5] = joyinfo.dwVpos;
transaxis = joystick->hwdata->transaxis;
for (i = 0; i < joystick->naxes; i++) {
if (joyinfo.dwFlags & flags[i]) {
Jun 7, 2001
Jun 7, 2001
323
value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale);
Apr 26, 2001
Apr 26, 2001
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
change = (value - joystick->axes[i]);
if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) {
SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
}
}
}
/* joystick button events */
if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) {
for ( i = 0; i < joystick->nbuttons; ++i ) {
if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) {
if ( ! joystick->buttons[i] ) {
SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED);
}
} else {
if ( joystick->buttons[i] ) {
SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED);
}
}
}
}
/* joystick hat events */
if ( joyinfo.dwFlags & JOY_RETURNPOV ) {
Uint8 pos;
pos = TranslatePOV(joyinfo.dwPOV);
if ( pos != joystick->hats[0] ) {
SDL_PrivateJoystickHat(joystick, 0, pos);
}
}
}
/* Function to close a joystick after use */
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
{
if (joystick->hwdata != NULL) {
/* free system specific hardware data */
Feb 7, 2006
Feb 7, 2006
362
SDL_free(joystick->hwdata);
Apr 26, 2001
Apr 26, 2001
363
364
365
366
367
368
}
}
/* Function to perform any system-specific joystick related cleanup */
void SDL_SYS_JoystickQuit(void)
{
Aug 21, 2004
Aug 21, 2004
369
370
int i;
for (i = 0; i < MAX_JOYSTICKS; i++) {
Aug 21, 2004
Aug 21, 2004
371
if ( SYS_JoystickName[i] != NULL ) {
Feb 7, 2006
Feb 7, 2006
372
SDL_free(SYS_JoystickName[i]);
Aug 21, 2004
Aug 21, 2004
373
374
}
}
Apr 26, 2001
Apr 26, 2001
375
376
377
378
379
380
381
}
/* implementation functions */
void SetMMerror(char *function, int code)
{
static char *error;
Feb 6, 2006
Feb 6, 2006
382
static char errbuf[1024];
Apr 26, 2001
Apr 26, 2001
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
errbuf[0] = 0;
switch (code)
{
case MMSYSERR_NODRIVER:
error = "Joystick driver not present";
break;
case MMSYSERR_INVALPARAM:
case JOYERR_PARMS:
error = "Invalid parameter(s)";
break;
case MMSYSERR_BADDEVICEID:
error = "Bad device ID";
break;
case JOYERR_UNPLUGGED:
error = "Joystick not attached";
break;
case JOYERR_NOCANDO:
error = "Can't capture joystick input";
break;
default:
Feb 7, 2006
Feb 7, 2006
409
SDL_snprintf(errbuf, SDL_arraysize(errbuf),
Feb 6, 2006
Feb 6, 2006
410
"%s: Unknown Multimedia system error: 0x%x",
Apr 26, 2001
Apr 26, 2001
411
412
413
414
415
function, code);
break;
}
if ( ! errbuf[0] ) {
Feb 7, 2006
Feb 7, 2006
416
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error);
Apr 26, 2001
Apr 26, 2001
417
418
419
}
SDL_SetError("%s", errbuf);
}