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