Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
426 lines (373 loc) · 12.6 KB

SDL_mmjoystick.c

File metadata and controls

426 lines (373 loc) · 12.6 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Apr 26, 2001
Apr 26, 2001
20
*/
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
22
Apr 14, 2006
Apr 14, 2006
23
24
#ifdef SDL_JOYSTICK_WINMM
Apr 26, 2001
Apr 26, 2001
25
26
/* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */
Jan 25, 2011
Jan 25, 2011
27
#include "../../core/windows/SDL_windows.h"
Feb 10, 2006
Feb 10, 2006
28
29
30
#include <mmsystem.h>
#include <regstr.h>
Feb 6, 2006
Feb 6, 2006
31
#include "SDL_events.h"
Apr 26, 2001
Apr 26, 2001
32
#include "SDL_joystick.h"
Feb 16, 2006
Feb 16, 2006
33
34
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
Apr 26, 2001
Apr 26, 2001
35
Oct 15, 2002
Oct 15, 2002
36
#define MAX_JOYSTICKS 16
Jul 10, 2006
Jul 10, 2006
37
38
#define MAX_AXES 6 /* each joystick can have up to 6 axes */
#define MAX_BUTTONS 32 /* and 32 buttons */
Apr 26, 2001
Apr 26, 2001
39
40
#define AXIS_MIN -32768 /* minimum value for axis coordinate */
#define AXIS_MAX 32767 /* maximum value for axis coordinate */
Feb 16, 2004
Feb 16, 2004
41
42
/* 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
43
44
45
46
#define JOY_BUTTON_FLAG(n) (1<<n)
/* array to hold joystick ID values */
Jul 10, 2006
Jul 10, 2006
47
48
49
static UINT SYS_JoystickID[MAX_JOYSTICKS];
static JOYCAPS SYS_Joystick[MAX_JOYSTICKS];
static char *SYS_JoystickName[MAX_JOYSTICKS];
Apr 26, 2001
Apr 26, 2001
50
51
52
53
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
Jul 10, 2006
Jul 10, 2006
54
55
56
57
58
59
60
61
62
63
/* joystick ID */
UINT id;
/* values used to translate device-specific coordinates into
SDL-standard ranges */
struct _transaxis
{
int offset;
float scale;
} transaxis[6];
Apr 26, 2001
Apr 26, 2001
64
65
};
Jan 21, 2011
Jan 21, 2011
66
/* Convert a Windows Multimedia API return code to a text message */
Apr 26, 2001
Apr 26, 2001
67
68
69
static void SetMMerror(char *function, int code);
Jul 10, 2006
Jul 10, 2006
70
71
static char *
GetJoystickName(int index, const char *szRegKey)
Aug 21, 2004
Aug 21, 2004
72
{
Jul 10, 2006
Jul 10, 2006
73
74
75
76
77
78
/* 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;
Jul 12, 2007
Jul 12, 2007
79
HKEY hTopKey;
Jul 10, 2006
Jul 10, 2006
80
81
82
HKEY hKey;
DWORD regsize;
LONG regresult;
Jul 12, 2007
Jul 12, 2007
83
84
85
char regkey[256];
char regvalue[256];
char regname[256];
Jul 10, 2006
Jul 10, 2006
86
Jul 12, 2007
Jul 12, 2007
87
SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
Jul 10, 2006
Jul 10, 2006
88
REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
Jul 12, 2007
Jul 12, 2007
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
hTopKey = HKEY_LOCAL_MACHINE;
regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
if (regresult != ERROR_SUCCESS) {
hTopKey = HKEY_CURRENT_USER;
regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
}
if (regresult != ERROR_SUCCESS) {
return NULL;
}
/* find the registry key name for the joystick's properties */
regsize = sizeof(regname);
SDL_snprintf(regvalue, SDL_arraysize(regvalue), "Joystick%d%s", index + 1,
REGSTR_VAL_JOYOEMNAME);
regresult =
RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, &regsize);
RegCloseKey(hKey);
if (regresult != ERROR_SUCCESS) {
return NULL;
}
/* open that registry key */
SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s", REGSTR_PATH_JOYOEM,
regname);
regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
if (regresult != ERROR_SUCCESS) {
return NULL;
}
/* find the size for the OEM name text */
regsize = sizeof(regvalue);
regresult =
RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, NULL, &regsize);
Jul 10, 2006
Jul 10, 2006
123
if (regresult == ERROR_SUCCESS) {
Jul 12, 2007
Jul 12, 2007
124
125
126
127
128
/* allocate enough memory for the OEM name text ... */
name = (char *) SDL_malloc(regsize);
if (name) {
/* ... and read it from the registry */
regresult = RegQueryValueExA(hKey,
Jul 10, 2006
Jul 10, 2006
129
REGSTR_VAL_JOYOEMNAME, 0, 0,
Jul 12, 2007
Jul 12, 2007
130
(LPBYTE) name, &regsize);
Jul 10, 2006
Jul 10, 2006
131
132
}
}
Jul 12, 2007
Jul 12, 2007
133
134
RegCloseKey(hKey);
Jul 10, 2006
Jul 10, 2006
135
return (name);
Aug 21, 2004
Aug 21, 2004
136
137
}
Apr 26, 2001
Apr 26, 2001
138
139
140
141
142
/* 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.
*/
Jul 10, 2006
Jul 10, 2006
143
144
int
SDL_SYS_JoystickInit(void)
Apr 26, 2001
Apr 26, 2001
145
{
Jul 10, 2006
Jul 10, 2006
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
int i;
int maxdevs;
int numdevs;
JOYINFOEX joyinfo;
JOYCAPS joycaps;
MMRESULT result;
/* Reset the joystick ID & name mapping tables */
for (i = 0; i < MAX_JOYSTICKS; ++i) {
SYS_JoystickID[i] = 0;
SYS_JoystickName[i] = NULL;
}
/* Loop over all potential joystick devices */
numdevs = 0;
maxdevs = joyGetNumDevs();
for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) {
joyinfo.dwSize = sizeof(joyinfo);
joyinfo.dwFlags = JOY_RETURNALL;
Oct 29, 2006
Oct 29, 2006
166
result = joyGetPosEx(i, &joyinfo);
Jul 10, 2006
Jul 10, 2006
167
168
169
170
171
172
173
174
175
176
177
178
if (result == JOYERR_NOERROR) {
result = joyGetDevCaps(i, &joycaps, sizeof(joycaps));
if (result == JOYERR_NOERROR) {
SYS_JoystickID[numdevs] = i;
SYS_Joystick[numdevs] = joycaps;
SYS_JoystickName[numdevs] =
GetJoystickName(i, joycaps.szRegKey);
numdevs++;
}
}
}
return (numdevs);
Apr 26, 2001
Apr 26, 2001
179
180
181
}
/* Function to get the device-dependent name of a joystick */
Jul 10, 2006
Jul 10, 2006
182
183
const char *
SDL_SYS_JoystickName(int index)
Apr 26, 2001
Apr 26, 2001
184
{
Jul 10, 2006
Jul 10, 2006
185
186
187
188
189
if (SYS_JoystickName[index] != NULL) {
return (SYS_JoystickName[index]);
} else {
return (SYS_Joystick[index].szPname);
}
Apr 26, 2001
Apr 26, 2001
190
191
192
193
194
195
196
}
/* 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.
*/
Jul 10, 2006
Jul 10, 2006
197
198
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
Apr 26, 2001
Apr 26, 2001
199
{
Jul 10, 2006
Jul 10, 2006
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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 */
joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) {
SDL_OutOfMemory();
return (-1);
}
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
/* 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);
Apr 26, 2001
Apr 26, 2001
252
253
}
Jul 10, 2006
Jul 10, 2006
254
255
static Uint8
TranslatePOV(DWORD value)
Apr 26, 2001
Apr 26, 2001
256
{
Jul 10, 2006
Jul 10, 2006
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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);
Apr 26, 2001
Apr 26, 2001
275
276
277
278
279
280
281
}
/* 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.
*/
Jul 10, 2006
Jul 10, 2006
282
283
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
Apr 26, 2001
Apr 26, 2001
284
{
Jul 10, 2006
Jul 10, 2006
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
323
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
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]) {
value =
(int) (((float) pos[i] +
transaxis[i].offset) * transaxis[i].scale);
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);
}
}
Apr 26, 2001
Apr 26, 2001
354
355
356
}
/* Function to close a joystick after use */
Jul 10, 2006
Jul 10, 2006
357
358
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
Apr 26, 2001
Apr 26, 2001
359
{
Jul 10, 2006
Jul 10, 2006
360
361
362
if (joystick->hwdata != NULL) {
/* free system specific hardware data */
SDL_free(joystick->hwdata);
Jan 6, 2010
Jan 6, 2010
363
joystick->hwdata = NULL;
Jul 10, 2006
Jul 10, 2006
364
}
Apr 26, 2001
Apr 26, 2001
365
366
367
}
/* Function to perform any system-specific joystick related cleanup */
Jul 10, 2006
Jul 10, 2006
368
369
void
SDL_SYS_JoystickQuit(void)
Apr 26, 2001
Apr 26, 2001
370
{
Jul 10, 2006
Jul 10, 2006
371
372
373
374
int i;
for (i = 0; i < MAX_JOYSTICKS; i++) {
if (SYS_JoystickName[i] != NULL) {
SDL_free(SYS_JoystickName[i]);
Jan 6, 2010
Jan 6, 2010
375
SYS_JoystickName[i] = NULL;
Jul 10, 2006
Jul 10, 2006
376
377
}
}
Apr 26, 2001
Apr 26, 2001
378
379
380
381
}
/* implementation functions */
Jul 10, 2006
Jul 10, 2006
382
383
void
SetMMerror(char *function, int code)
Apr 26, 2001
Apr 26, 2001
384
{
Jul 10, 2006
Jul 10, 2006
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
static char *error;
static char errbuf[1024];
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:
SDL_snprintf(errbuf, SDL_arraysize(errbuf),
"%s: Unknown Multimedia system error: 0x%x",
function, code);
break;
}
if (!errbuf[0]) {
SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
error);
}
SDL_SetError("%s", errbuf);
Apr 26, 2001
Apr 26, 2001
423
}
Apr 14, 2006
Apr 14, 2006
424
425
#endif /* SDL_JOYSTICK_WINMM */
Jul 10, 2006
Jul 10, 2006
426
/* vi: set ts=4 sw=4 expandtab: */