Mercurial > sdl-ios-xcode
annotate src/joystick/win32/SDL_mmjoystick.c @ 1626:a80e1e0880b8
Fixed bug #176
[I'm fixing this for the public headers, but I'm not going to bother for the SDL library code (yet)]
To clarify: Normaly, GCC (or, to be precise, the preprocessor) will ignore
this, and compile the code happily. However, one can specify -Wundef to get a
warning about this.
One can probably argue whether to consider this a bug or not; but I think that
(a) from a semantic point of view, using "#if FOO" when FOO is not defined is
strange, and (b) since it is possible to trigger a warning about this, and a
trivial fix exists, it should be corrected.
I can think of two alternative patches, BTW:
1) Simply use #define HAVE_FOO 0, instead of not defining HAVE_FOO at all
2) Change
#if HAVE_FOO
to
#if HAVE_FOO+0
which always does the right thing.
But I think I still prefer the attached patch :-).
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 13 Apr 2006 13:38:40 +0000 |
parents | bb6839704ed6 |
children | 92947e3a18db |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 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 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */ | |
25 | |
1433
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
26 #define WIN32_LEAN_AND_MEAN |
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
27 #include <windows.h> |
1358
c71e05b4dc2e
More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
28 #include <mmsystem.h> |
c71e05b4dc2e
More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
29 #include <regstr.h> |
c71e05b4dc2e
More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
30 |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
31 #include "SDL_events.h" |
0 | 32 #include "SDL_joystick.h" |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
33 #include "../SDL_sysjoystick.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
34 #include "../SDL_joystick_c.h" |
0 | 35 |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
36 #define MAX_JOYSTICKS 16 |
0 | 37 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ |
38 #define MAX_BUTTONS 32 /* and 32 buttons */ | |
39 #define AXIS_MIN -32768 /* minimum value for axis coordinate */ | |
40 #define AXIS_MAX 32767 /* maximum value for axis coordinate */ | |
834
d37179d10ccc
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
41 /* limit axis to 256 possible positions to filter out noise */ |
d37179d10ccc
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
42 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256) |
0 | 43 #define JOY_BUTTON_FLAG(n) (1<<n) |
44 | |
45 | |
46 /* array to hold joystick ID values */ | |
47 static UINT SYS_JoystickID[MAX_JOYSTICKS]; | |
48 static JOYCAPS SYS_Joystick[MAX_JOYSTICKS]; | |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
49 static char *SYS_JoystickName[MAX_JOYSTICKS]; |
0 | 50 |
51 /* The private structure used to keep track of a joystick */ | |
52 struct joystick_hwdata | |
53 { | |
54 /* joystick ID */ | |
55 UINT id; | |
56 | |
57 /* values used to translate device-specific coordinates into | |
58 SDL-standard ranges */ | |
59 struct _transaxis { | |
60 int offset; | |
61 float scale; | |
62 } transaxis[6]; | |
63 }; | |
64 | |
65 /* Convert a win32 Multimedia API return code to a text message */ | |
66 static void SetMMerror(char *function, int code); | |
67 | |
68 | |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
69 static char *GetJoystickName(int index, const char *szRegKey) |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
70 { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
71 /* added 7/24/2004 by Eckhard Stolberg */ |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
72 /* |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
73 see if there is a joystick for the current |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
74 index (1-16) listed in the registry |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
75 */ |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
76 char *name = NULL; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
77 HKEY hKey; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
78 DWORD regsize; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
79 LONG regresult; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
80 unsigned char regkey[256]; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
81 unsigned char regvalue[256]; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
82 unsigned char regname[256]; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
83 |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
84 SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s", |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
85 REGSTR_PATH_JOYCONFIG, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
86 szRegKey, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
87 REGSTR_KEY_JOYCURR); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
88 regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
89 (LPTSTR) ®key, 0, KEY_READ, &hKey); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
90 if (regresult == ERROR_SUCCESS) |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
91 { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
92 /* |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
93 find the registry key name for the |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
94 joystick's properties |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
95 */ |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
96 regsize = sizeof(regname); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
97 SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue), |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
98 "Joystick%d%s", index+1, |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
99 REGSTR_VAL_JOYOEMNAME); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
100 regresult = RegQueryValueExA(hKey, |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
938
diff
changeset
|
101 (char *) regvalue, 0, 0, (LPBYTE) ®name, |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
102 (LPDWORD) ®size); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
103 RegCloseKey(hKey); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
104 if (regresult == ERROR_SUCCESS) |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
105 { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
106 /* open that registry key */ |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
107 SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s", |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
108 REGSTR_PATH_JOYOEM, regname); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
109 regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
938
diff
changeset
|
110 (char *) regkey, 0, KEY_READ, &hKey); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
111 if (regresult == ERROR_SUCCESS) |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
112 { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
113 /* find the size for the OEM name text */ |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
114 regsize = sizeof(regvalue); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
115 regresult = |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
116 RegQueryValueExA(hKey, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
117 REGSTR_VAL_JOYOEMNAME, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
118 0, 0, NULL, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
119 (LPDWORD) ®size); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
120 if (regresult == ERROR_SUCCESS) |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
121 { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
122 /* |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
123 allocate enough memory |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
124 for the OEM name text ... |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
125 */ |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
126 name = (char *) SDL_malloc(regsize); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
127 /* ... and read it from the registry */ |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
128 regresult = |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
129 RegQueryValueExA(hKey, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
130 REGSTR_VAL_JOYOEMNAME, 0, 0, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
131 (LPBYTE) name, |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
132 (LPDWORD) ®size); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
133 RegCloseKey(hKey); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
134 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
135 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
136 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
137 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
138 return(name); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
139 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
140 |
0 | 141 /* Function to scan the system for joysticks. |
142 * This function should set SDL_numjoysticks to the number of available | |
143 * joysticks. Joystick 0 should be the system default joystick. | |
144 * It should return 0, or -1 on an unrecoverable fatal error. | |
145 */ | |
146 int SDL_SYS_JoystickInit(void) | |
147 { | |
148 int i; | |
149 int maxdevs; | |
150 int numdevs; | |
151 JOYINFOEX joyinfo; | |
152 JOYCAPS joycaps; | |
153 MMRESULT result; | |
154 | |
155 numdevs = 0; | |
156 maxdevs = joyGetNumDevs(); | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
157 |
0 | 158 if ( maxdevs > MAX_JOYSTICKS ) { |
159 maxdevs = MAX_JOYSTICKS; | |
160 } | |
161 | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
162 |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
163 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
|
164 SYS_JoystickID[i] = JOYSTICKID1 + i; |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
165 SYS_JoystickName[i] = NULL; |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
166 } |
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
167 |
0 | 168 |
169 for ( i = 0; (i < maxdevs); ++i ) { | |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
170 |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
171 /* added 8/31/2001 By Vitaliy Mikitchenko */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
172 joyinfo.dwSize = sizeof(joyinfo); |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
173 joyinfo.dwFlags = JOY_RETURNALL; |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
174 /* end addition */ |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
175 |
0 | 176 result = joyGetPosEx(SYS_JoystickID[i], &joyinfo); |
177 if ( result == JOYERR_NOERROR ) { | |
178 result = joyGetDevCaps(SYS_JoystickID[i], &joycaps, sizeof(joycaps)); | |
179 if ( result == JOYERR_NOERROR ) { | |
180 SYS_JoystickID[numdevs] = SYS_JoystickID[i]; | |
181 SYS_Joystick[numdevs] = joycaps; | |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
182 SYS_JoystickName[numdevs] = GetJoystickName(numdevs, joycaps.szRegKey); |
0 | 183 numdevs++; |
184 } | |
185 } | |
186 } | |
187 return(numdevs); | |
188 } | |
189 | |
190 /* Function to get the device-dependent name of a joystick */ | |
191 const char *SDL_SYS_JoystickName(int index) | |
192 { | |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
193 if ( SYS_JoystickName[index] != NULL ) { |
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
194 return(SYS_JoystickName[index]); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
195 } else { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
196 return(SYS_Joystick[index].szPname); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
197 } |
0 | 198 } |
199 | |
200 /* Function to open a joystick for use. | |
201 The joystick to open is specified by the index field of the joystick. | |
202 This should fill the nbuttons and naxes fields of the joystick structure. | |
203 It returns 0, or -1 if there is an error. | |
204 */ | |
205 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
206 { | |
207 int index, i; | |
208 int caps_flags[MAX_AXES-2] = | |
209 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; | |
210 int axis_min[MAX_AXES], axis_max[MAX_AXES]; | |
211 | |
212 | |
213 /* shortcut */ | |
214 index = joystick->index; | |
215 axis_min[0] = SYS_Joystick[index].wXmin; | |
216 axis_max[0] = SYS_Joystick[index].wXmax; | |
217 axis_min[1] = SYS_Joystick[index].wYmin; | |
218 axis_max[1] = SYS_Joystick[index].wYmax; | |
219 axis_min[2] = SYS_Joystick[index].wZmin; | |
220 axis_max[2] = SYS_Joystick[index].wZmax; | |
221 axis_min[3] = SYS_Joystick[index].wRmin; | |
222 axis_max[3] = SYS_Joystick[index].wRmax; | |
223 axis_min[4] = SYS_Joystick[index].wUmin; | |
224 axis_max[4] = SYS_Joystick[index].wUmax; | |
225 axis_min[5] = SYS_Joystick[index].wVmin; | |
226 axis_max[5] = SYS_Joystick[index].wVmax; | |
227 | |
228 /* allocate memory for system specific hardware data */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
229 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); |
0 | 230 if (joystick->hwdata == NULL) |
231 { | |
232 SDL_OutOfMemory(); | |
233 return(-1); | |
234 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
235 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
0 | 236 |
237 /* set hardware data */ | |
238 joystick->hwdata->id = SYS_JoystickID[index]; | |
239 for ( i = 0; i < MAX_AXES; ++i ) { | |
240 if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) { | |
241 joystick->hwdata->transaxis[i].offset = | |
242 AXIS_MIN - axis_min[i]; | |
243 joystick->hwdata->transaxis[i].scale = | |
244 (float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]); | |
245 } else { | |
246 joystick->hwdata->transaxis[i].offset = 0; | |
247 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */ | |
248 } | |
249 } | |
250 | |
251 /* fill nbuttons, naxes, and nhats fields */ | |
252 joystick->nbuttons = SYS_Joystick[index].wNumButtons; | |
253 joystick->naxes = SYS_Joystick[index].wNumAxes; | |
254 if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) { | |
255 joystick->nhats = 1; | |
256 } else { | |
257 joystick->nhats = 0; | |
258 } | |
259 return(0); | |
260 } | |
261 | |
262 static Uint8 TranslatePOV(DWORD value) | |
263 { | |
264 Uint8 pos; | |
265 | |
266 pos = SDL_HAT_CENTERED; | |
267 if ( value != JOY_POVCENTERED ) { | |
268 if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) { | |
269 pos |= SDL_HAT_UP; | |
270 } | |
271 if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) { | |
272 pos |= SDL_HAT_RIGHT; | |
273 } | |
274 if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) { | |
275 pos |= SDL_HAT_DOWN; | |
276 } | |
277 if ( value > JOY_POVBACKWARD ) { | |
278 pos |= SDL_HAT_LEFT; | |
279 } | |
280 } | |
281 return(pos); | |
282 } | |
283 | |
284 /* Function to update the state of a joystick - called as a device poll. | |
285 * This function shouldn't update the joystick structure directly, | |
286 * but instead should call SDL_PrivateJoystick*() to deliver events | |
287 * and update joystick device state. | |
288 */ | |
289 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
290 { | |
291 MMRESULT result; | |
292 int i; | |
293 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, | |
294 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; | |
295 DWORD pos[MAX_AXES]; | |
296 struct _transaxis *transaxis; | |
297 int value, change; | |
298 JOYINFOEX joyinfo; | |
299 | |
300 joyinfo.dwSize = sizeof(joyinfo); | |
301 joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS; | |
302 if ( ! joystick->hats ) { | |
303 joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS); | |
304 } | |
305 result = joyGetPosEx(joystick->hwdata->id, &joyinfo); | |
306 if ( result != JOYERR_NOERROR ) { | |
307 SetMMerror("joyGetPosEx", result); | |
308 return; | |
309 } | |
310 | |
311 /* joystick motion events */ | |
312 pos[0] = joyinfo.dwXpos; | |
313 pos[1] = joyinfo.dwYpos; | |
314 pos[2] = joyinfo.dwZpos; | |
315 pos[3] = joyinfo.dwRpos; | |
316 pos[4] = joyinfo.dwUpos; | |
317 pos[5] = joyinfo.dwVpos; | |
318 | |
319 transaxis = joystick->hwdata->transaxis; | |
320 for (i = 0; i < joystick->naxes; i++) { | |
321 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
|
322 value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale); |
0 | 323 change = (value - joystick->axes[i]); |
324 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { | |
325 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); | |
326 } | |
327 } | |
328 } | |
329 | |
330 /* joystick button events */ | |
331 if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) { | |
332 for ( i = 0; i < joystick->nbuttons; ++i ) { | |
333 if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) { | |
334 if ( ! joystick->buttons[i] ) { | |
335 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED); | |
336 } | |
337 } else { | |
338 if ( joystick->buttons[i] ) { | |
339 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED); | |
340 } | |
341 } | |
342 } | |
343 } | |
344 | |
345 /* joystick hat events */ | |
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 } | |
354 } | |
355 | |
356 /* Function to close a joystick after use */ | |
357 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
358 { | |
359 if (joystick->hwdata != NULL) { | |
360 /* free system specific hardware data */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
361 SDL_free(joystick->hwdata); |
0 | 362 } |
363 } | |
364 | |
365 /* Function to perform any system-specific joystick related cleanup */ | |
366 void SDL_SYS_JoystickQuit(void) | |
367 { | |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
368 int i; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
369 for (i = 0; i < MAX_JOYSTICKS; i++) { |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
370 if ( SYS_JoystickName[i] != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
371 SDL_free(SYS_JoystickName[i]); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
372 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
373 } |
0 | 374 } |
375 | |
376 | |
377 /* implementation functions */ | |
378 void SetMMerror(char *function, int code) | |
379 { | |
380 static char *error; | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
381 static char errbuf[1024]; |
0 | 382 |
383 errbuf[0] = 0; | |
384 switch (code) | |
385 { | |
386 case MMSYSERR_NODRIVER: | |
387 error = "Joystick driver not present"; | |
388 break; | |
389 | |
390 case MMSYSERR_INVALPARAM: | |
391 case JOYERR_PARMS: | |
392 error = "Invalid parameter(s)"; | |
393 break; | |
394 | |
395 case MMSYSERR_BADDEVICEID: | |
396 error = "Bad device ID"; | |
397 break; | |
398 | |
399 case JOYERR_UNPLUGGED: | |
400 error = "Joystick not attached"; | |
401 break; | |
402 | |
403 case JOYERR_NOCANDO: | |
404 error = "Can't capture joystick input"; | |
405 break; | |
406 | |
407 default: | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
408 SDL_snprintf(errbuf, SDL_arraysize(errbuf), |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
409 "%s: Unknown Multimedia system error: 0x%x", |
0 | 410 function, code); |
411 break; | |
412 } | |
413 | |
414 if ( ! errbuf[0] ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
415 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); |
0 | 416 } |
417 SDL_SetError("%s", errbuf); | |
418 } |