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