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