Mercurial > sdl-ios-xcode
annotate src/joystick/win32/SDL_mmjoystick.c @ 4089:37345dc3dd83 SDL-1.2
Switched afxres.h to winresrc.h
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 23 Jul 2007 18:36:58 +0000 |
parents | 209a3ef8a328 |
children | 96ce26f24b01 |
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; |
4051
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
79 HKEY hTopKey; |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
80 HKEY hKey; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
81 DWORD regsize; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
82 LONG regresult; |
4051
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
83 char regkey[256]; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
84 char regvalue[256]; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
85 char regname[256]; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
86 |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
87 SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s\\%s", |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
88 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
89 hTopKey = HKEY_LOCAL_MACHINE; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
90 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
91 if (regresult != ERROR_SUCCESS) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
92 hTopKey = HKEY_CURRENT_USER; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
93 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
94 } |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
95 if (regresult != ERROR_SUCCESS) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
96 return NULL; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
97 } |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
98 |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
99 /* find the registry key name for the joystick's properties */ |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
100 regsize = sizeof(regname); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
101 SDL_snprintf(regvalue, SDL_arraysize(regvalue), "Joystick%d%s", index+1, REGSTR_VAL_JOYOEMNAME); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
102 regresult = RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE)regname, ®size); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
103 RegCloseKey(hKey); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
104 |
4051
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
105 if (regresult != ERROR_SUCCESS) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
106 return NULL; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
107 } |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
108 |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
109 /* open that registry key */ |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
110 SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s", REGSTR_PATH_JOYOEM, regname); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
111 regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
112 if (regresult != ERROR_SUCCESS) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
113 return NULL; |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
114 } |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
115 |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
116 /* find the size for the OEM name text */ |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
117 regsize = sizeof(regvalue); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
118 regresult = RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, NULL, ®size); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
119 if (regresult == ERROR_SUCCESS) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
120 /* allocate enough memory for the OEM name text ... */ |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
121 name = (char *) SDL_malloc(regsize); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
122 if ( name ) { |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
123 /* ... and read it from the registry */ |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
124 regresult = RegQueryValueExA(hKey, |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
125 REGSTR_VAL_JOYOEMNAME, 0, 0, |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
126 (LPBYTE) name, ®size); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
127 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
128 } |
4051
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
129 RegCloseKey(hKey); |
209a3ef8a328
Fixed joystick name detection
Sam Lantinga <slouken@libsdl.org>
parents:
3886
diff
changeset
|
130 |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
131 return(name); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
132 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
133 |
0 | 134 /* Function to scan the system for joysticks. |
135 * This function should set SDL_numjoysticks to the number of available | |
136 * joysticks. Joystick 0 should be the system default joystick. | |
137 * It should return 0, or -1 on an unrecoverable fatal error. | |
138 */ | |
139 int SDL_SYS_JoystickInit(void) | |
140 { | |
141 int i; | |
142 int maxdevs; | |
143 int numdevs; | |
144 JOYINFOEX joyinfo; | |
145 JOYCAPS joycaps; | |
146 MMRESULT result; | |
147 | |
1748
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
148 /* Reset the joystick ID & name mapping tables */ |
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
149 for ( i = 0; i < MAX_JOYSTICKS; ++i ) { |
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
150 SYS_JoystickID[i] = 0; |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
151 SYS_JoystickName[i] = NULL; |
531
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
152 } |
8450e66651ea
Fixed joystick detection problem on Windows XP (thanks Maciej!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
153 |
1748
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
154 /* Loop over all potential joystick devices */ |
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
155 numdevs = 0; |
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
156 maxdevs = joyGetNumDevs(); |
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
157 for ( i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i ) { |
165
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
158 |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
159 joyinfo.dwSize = sizeof(joyinfo); |
6a4e09bbbbc0
Joystick initialization fix submitted by Vitaliy Mikitchenko
Sam Lantinga <slouken@libsdl.org>
parents:
49
diff
changeset
|
160 joyinfo.dwFlags = JOY_RETURNALL; |
3886
36f155ec8133
joyGetPosEx() was using the wrong variable for specifying a device; this is
Ryan C. Gordon <icculus@icculus.org>
parents:
1748
diff
changeset
|
161 result = joyGetPosEx(i, &joyinfo); |
0 | 162 if ( result == JOYERR_NOERROR ) { |
1748
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
163 result = joyGetDevCaps(i, &joycaps, sizeof(joycaps)); |
0 | 164 if ( result == JOYERR_NOERROR ) { |
1748
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
165 SYS_JoystickID[numdevs] = i; |
0 | 166 SYS_Joystick[numdevs] = joycaps; |
1748
5e86e34453d4
------- Comment #1 From Max Horn 2006-04-17 03:08 [reply] -------
Sam Lantinga <slouken@libsdl.org>
parents:
1635
diff
changeset
|
167 SYS_JoystickName[numdevs] = GetJoystickName(i, joycaps.szRegKey); |
0 | 168 numdevs++; |
169 } | |
170 } | |
171 } | |
172 return(numdevs); | |
173 } | |
174 | |
175 /* Function to get the device-dependent name of a joystick */ | |
176 const char *SDL_SYS_JoystickName(int index) | |
177 { | |
938
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
178 if ( SYS_JoystickName[index] != NULL ) { |
fa2ce068b0b6
Hmm, this should work a little better. :)
Sam Lantinga <slouken@libsdl.org>
parents:
937
diff
changeset
|
179 return(SYS_JoystickName[index]); |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
180 } else { |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
181 return(SYS_Joystick[index].szPname); |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
182 } |
0 | 183 } |
184 | |
185 /* Function to open a joystick for use. | |
186 The joystick to open is specified by the index field of the joystick. | |
187 This should fill the nbuttons and naxes fields of the joystick structure. | |
188 It returns 0, or -1 if there is an error. | |
189 */ | |
190 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
191 { | |
192 int index, i; | |
193 int caps_flags[MAX_AXES-2] = | |
194 { JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV }; | |
195 int axis_min[MAX_AXES], axis_max[MAX_AXES]; | |
196 | |
197 | |
198 /* shortcut */ | |
199 index = joystick->index; | |
200 axis_min[0] = SYS_Joystick[index].wXmin; | |
201 axis_max[0] = SYS_Joystick[index].wXmax; | |
202 axis_min[1] = SYS_Joystick[index].wYmin; | |
203 axis_max[1] = SYS_Joystick[index].wYmax; | |
204 axis_min[2] = SYS_Joystick[index].wZmin; | |
205 axis_max[2] = SYS_Joystick[index].wZmax; | |
206 axis_min[3] = SYS_Joystick[index].wRmin; | |
207 axis_max[3] = SYS_Joystick[index].wRmax; | |
208 axis_min[4] = SYS_Joystick[index].wUmin; | |
209 axis_max[4] = SYS_Joystick[index].wUmax; | |
210 axis_min[5] = SYS_Joystick[index].wVmin; | |
211 axis_max[5] = SYS_Joystick[index].wVmax; | |
212 | |
213 /* 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
|
214 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); |
0 | 215 if (joystick->hwdata == NULL) |
216 { | |
217 SDL_OutOfMemory(); | |
218 return(-1); | |
219 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
220 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
0 | 221 |
222 /* set hardware data */ | |
223 joystick->hwdata->id = SYS_JoystickID[index]; | |
224 for ( i = 0; i < MAX_AXES; ++i ) { | |
225 if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) { | |
226 joystick->hwdata->transaxis[i].offset = | |
227 AXIS_MIN - axis_min[i]; | |
228 joystick->hwdata->transaxis[i].scale = | |
229 (float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]); | |
230 } else { | |
231 joystick->hwdata->transaxis[i].offset = 0; | |
232 joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */ | |
233 } | |
234 } | |
235 | |
236 /* fill nbuttons, naxes, and nhats fields */ | |
237 joystick->nbuttons = SYS_Joystick[index].wNumButtons; | |
238 joystick->naxes = SYS_Joystick[index].wNumAxes; | |
239 if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) { | |
240 joystick->nhats = 1; | |
241 } else { | |
242 joystick->nhats = 0; | |
243 } | |
244 return(0); | |
245 } | |
246 | |
247 static Uint8 TranslatePOV(DWORD value) | |
248 { | |
249 Uint8 pos; | |
250 | |
251 pos = SDL_HAT_CENTERED; | |
252 if ( value != JOY_POVCENTERED ) { | |
253 if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) { | |
254 pos |= SDL_HAT_UP; | |
255 } | |
256 if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) { | |
257 pos |= SDL_HAT_RIGHT; | |
258 } | |
259 if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) { | |
260 pos |= SDL_HAT_DOWN; | |
261 } | |
262 if ( value > JOY_POVBACKWARD ) { | |
263 pos |= SDL_HAT_LEFT; | |
264 } | |
265 } | |
266 return(pos); | |
267 } | |
268 | |
269 /* Function to update the state of a joystick - called as a device poll. | |
270 * This function shouldn't update the joystick structure directly, | |
271 * but instead should call SDL_PrivateJoystick*() to deliver events | |
272 * and update joystick device state. | |
273 */ | |
274 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
275 { | |
276 MMRESULT result; | |
277 int i; | |
278 DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, | |
279 JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; | |
280 DWORD pos[MAX_AXES]; | |
281 struct _transaxis *transaxis; | |
282 int value, change; | |
283 JOYINFOEX joyinfo; | |
284 | |
285 joyinfo.dwSize = sizeof(joyinfo); | |
286 joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS; | |
287 if ( ! joystick->hats ) { | |
288 joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS); | |
289 } | |
290 result = joyGetPosEx(joystick->hwdata->id, &joyinfo); | |
291 if ( result != JOYERR_NOERROR ) { | |
292 SetMMerror("joyGetPosEx", result); | |
293 return; | |
294 } | |
295 | |
296 /* joystick motion events */ | |
297 pos[0] = joyinfo.dwXpos; | |
298 pos[1] = joyinfo.dwYpos; | |
299 pos[2] = joyinfo.dwZpos; | |
300 pos[3] = joyinfo.dwRpos; | |
301 pos[4] = joyinfo.dwUpos; | |
302 pos[5] = joyinfo.dwVpos; | |
303 | |
304 transaxis = joystick->hwdata->transaxis; | |
305 for (i = 0; i < joystick->naxes; i++) { | |
306 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
|
307 value = (int)(((float)pos[i] + transaxis[i].offset) * transaxis[i].scale); |
0 | 308 change = (value - joystick->axes[i]); |
309 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) { | |
310 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); | |
311 } | |
312 } | |
313 } | |
314 | |
315 /* joystick button events */ | |
316 if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) { | |
317 for ( i = 0; i < joystick->nbuttons; ++i ) { | |
318 if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) { | |
319 if ( ! joystick->buttons[i] ) { | |
320 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED); | |
321 } | |
322 } else { | |
323 if ( joystick->buttons[i] ) { | |
324 SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED); | |
325 } | |
326 } | |
327 } | |
328 } | |
329 | |
330 /* joystick hat events */ | |
331 if ( joyinfo.dwFlags & JOY_RETURNPOV ) { | |
332 Uint8 pos; | |
333 | |
334 pos = TranslatePOV(joyinfo.dwPOV); | |
335 if ( pos != joystick->hats[0] ) { | |
336 SDL_PrivateJoystickHat(joystick, 0, pos); | |
337 } | |
338 } | |
339 } | |
340 | |
341 /* Function to close a joystick after use */ | |
342 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
343 { | |
344 if (joystick->hwdata != NULL) { | |
345 /* free system specific hardware data */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
346 SDL_free(joystick->hwdata); |
0 | 347 } |
348 } | |
349 | |
350 /* Function to perform any system-specific joystick related cleanup */ | |
351 void SDL_SYS_JoystickQuit(void) | |
352 { | |
937
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
353 int i; |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
354 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
|
355 if ( SYS_JoystickName[i] != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
356 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
|
357 } |
1e6366bde299
Date: Tue, 27 Jul 2004 17:14:00 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
834
diff
changeset
|
358 } |
0 | 359 } |
360 | |
361 | |
362 /* implementation functions */ | |
363 void SetMMerror(char *function, int code) | |
364 { | |
365 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
|
366 static char errbuf[1024]; |
0 | 367 |
368 errbuf[0] = 0; | |
369 switch (code) | |
370 { | |
371 case MMSYSERR_NODRIVER: | |
372 error = "Joystick driver not present"; | |
373 break; | |
374 | |
375 case MMSYSERR_INVALPARAM: | |
376 case JOYERR_PARMS: | |
377 error = "Invalid parameter(s)"; | |
378 break; | |
379 | |
380 case MMSYSERR_BADDEVICEID: | |
381 error = "Bad device ID"; | |
382 break; | |
383 | |
384 case JOYERR_UNPLUGGED: | |
385 error = "Joystick not attached"; | |
386 break; | |
387 | |
388 case JOYERR_NOCANDO: | |
389 error = "Can't capture joystick input"; | |
390 break; | |
391 | |
392 default: | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
393 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
|
394 "%s: Unknown Multimedia system error: 0x%x", |
0 | 395 function, code); |
396 break; | |
397 } | |
398 | |
399 if ( ! errbuf[0] ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
400 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function, error); |
0 | 401 } |
402 SDL_SetError("%s", errbuf); | |
403 } | |
1635
92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents:
1433
diff
changeset
|
404 |
92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents:
1433
diff
changeset
|
405 #endif /* SDL_JOYSTICK_WINMM */ |