Mercurial > sdl-ios-xcode
annotate src/joystick/macos/SDL_sysjoystick.c @ 1361:19418e4422cb
New configure-based build system. Still work in progress, but much improved
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 16 Feb 2006 10:11:48 +0000 |
parents | c71e05b4dc2e |
children | c0a74f199ecf |
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:
1133
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:
1133
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:
1133
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:
1133
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:
1133
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:
1133
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:
1133
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:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* SDL stuff -- "SDL_sysjoystick.c" | |
24 MacOS joystick functions by Frederick Reitberger | |
25 | |
26 The code that follows is meant for SDL. Use at your own risk. | |
27 */ | |
28 | |
29 #include <InputSprocket.h> | |
30 | |
31 #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
|
32 #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
|
33 #include "../SDL_joystick_c.h" |
0 | 34 |
35 | |
36 /* The max number of joysticks we will detect */ | |
37 #define MAX_JOYSTICKS 16 | |
38 /* Limit ourselves to 32 elements per device */ | |
39 #define kMaxReferences 32 | |
40 | |
41 #define ISpSymmetricAxisToFloat(axis) ((((float) axis) - kISpAxisMiddle) / (kISpAxisMaximum-kISpAxisMiddle)) | |
42 #define ISpAsymmetricAxisToFloat(axis) (((float) axis) / (kISpAxisMaximum)) | |
43 | |
44 | |
45 static ISpDeviceReference SYS_Joysticks[MAX_JOYSTICKS]; | |
46 static ISpElementListReference SYS_Elements[MAX_JOYSTICKS]; | |
47 static ISpDeviceDefinition SYS_DevDef[MAX_JOYSTICKS]; | |
48 | |
49 struct joystick_hwdata | |
50 { | |
51 char name[64]; | |
52 /* Uint8 id;*/ | |
53 ISpElementReference refs[kMaxReferences]; | |
54 /* gonna need some sort of mapping info */ | |
55 }; | |
56 | |
57 | |
58 /* Function to scan the system for joysticks. | |
59 * Joystick 0 should be the system default joystick. | |
60 * This function should return the number of available joysticks, or -1 | |
61 * on an unrecoverable fatal error. | |
62 */ | |
63 int SDL_SYS_JoystickInit(void) | |
64 { | |
65 static ISpDeviceClass classes[4] = { | |
66 kISpDeviceClass_Joystick, | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
67 #if kISpDeviceClass_Gamepad |
0 | 68 kISpDeviceClass_Gamepad, |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
69 #endif |
0 | 70 kISpDeviceClass_Wheel, |
71 0 | |
72 }; | |
73 OSErr err; | |
74 int i; | |
75 UInt32 count, numJoysticks; | |
76 | |
77 if ( (Ptr)0 == (Ptr)ISpStartup ) { | |
78 SDL_SetError("InputSprocket not installed"); | |
79 return -1; // InputSprocket not installed | |
80 } | |
81 | |
82 if( (Ptr)0 == (Ptr)ISpGetVersion ) { | |
83 SDL_SetError("InputSprocket not version 1.1 or newer"); | |
84 return -1; // old version of ISp (not at least 1.1) | |
85 } | |
86 | |
87 ISpStartup(); | |
88 | |
89 /* Get all the joysticks */ | |
90 numJoysticks = 0; | |
91 for ( i=0; classes[i]; ++i ) { | |
92 count = 0; | |
93 err = ISpDevices_ExtractByClass( | |
94 classes[i], | |
95 MAX_JOYSTICKS-numJoysticks, | |
96 &count, | |
97 &SYS_Joysticks[numJoysticks]); | |
98 numJoysticks += count; | |
99 } | |
100 | |
101 for(i = 0; i < numJoysticks; i++) | |
102 { | |
103 ISpDevice_GetDefinition( | |
104 SYS_Joysticks[i], sizeof(ISpDeviceDefinition), | |
105 &SYS_DevDef[i]); | |
106 | |
107 err = ISpElementList_New( | |
108 0, NULL, | |
109 &SYS_Elements[i], 0); | |
110 | |
111 if (err) { | |
112 SDL_OutOfMemory(); | |
113 return -1; | |
114 } | |
115 | |
116 ISpDevice_GetElementList( | |
117 SYS_Joysticks[i], | |
118 &SYS_Elements[i]); | |
119 } | |
120 | |
121 ISpDevices_Deactivate(numJoysticks, SYS_Joysticks); | |
122 | |
123 return numJoysticks; | |
124 } | |
125 | |
126 /* Function to get the device-dependent name of a joystick */ | |
127 const char *SDL_SYS_JoystickName(int index) | |
128 { | |
129 static char name[64]; | |
130 int len; | |
131 | |
132 /* convert pascal string to c-string */ | |
133 len = SYS_DevDef[index].deviceName[0]; | |
134 if ( len >= sizeof(name) ) { | |
135 len = (sizeof(name) - 1); | |
136 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
137 SDL_memcpy(name, &SYS_DevDef[index].deviceName[1], len); |
0 | 138 name[len] = '\0'; |
139 | |
140 return name; | |
141 } | |
142 | |
143 /* Function to open a joystick for use. | |
144 The joystick to open is specified by the index field of the joystick. | |
145 This should fill the nbuttons and naxes fields of the joystick structure. | |
146 It returns 0, or -1 if there is an error. | |
147 */ | |
148 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
149 { | |
150 int index; | |
151 UInt32 count, gotCount, count2; | |
152 long numAxis, numButtons, numHats, numBalls; | |
153 | |
154 count = kMaxReferences; | |
155 count2 = 0; | |
156 numAxis = numButtons = numHats = numBalls = 0; | |
157 | |
158 index = joystick->index; | |
159 | |
160 /* allocate memory for system specific hardware data */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
161 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); |
0 | 162 if (joystick->hwdata == NULL) |
163 { | |
164 SDL_OutOfMemory(); | |
165 return(-1); | |
166 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
167 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
168 SDL_strcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index)); |
0 | 169 joystick->name = joystick->hwdata->name; |
170 | |
171 ISpElementList_ExtractByKind( | |
172 SYS_Elements[index], | |
173 kISpElementKind_Axis, | |
174 count, | |
175 &gotCount, | |
176 joystick->hwdata->refs); | |
177 | |
178 numAxis = gotCount; | |
179 count -= gotCount; | |
180 count2 += gotCount; | |
181 | |
182 ISpElementList_ExtractByKind( | |
183 SYS_Elements[index], | |
184 kISpElementKind_DPad, | |
185 count, | |
186 &gotCount, | |
187 &(joystick->hwdata->refs[count2])); | |
188 | |
189 numHats = gotCount; | |
190 count -= gotCount; | |
191 count2 += gotCount; | |
192 | |
193 ISpElementList_ExtractByKind( | |
194 SYS_Elements[index], | |
195 kISpElementKind_Button, | |
196 count, | |
197 &gotCount, | |
198 &(joystick->hwdata->refs[count2])); | |
199 | |
200 numButtons = gotCount; | |
201 count -= gotCount; | |
202 count2 += gotCount; | |
203 | |
204 joystick->naxes = numAxis; | |
205 joystick->nhats = numHats; | |
206 joystick->nballs = numBalls; | |
207 joystick->nbuttons = numButtons; | |
208 | |
209 ISpDevices_Activate( | |
210 1, | |
211 &SYS_Joysticks[index]); | |
212 | |
213 return 0; | |
214 } | |
215 | |
216 /* Function to update the state of a joystick - called as a device poll. | |
217 * This function shouldn't update the joystick structure directly, | |
218 * but instead should call SDL_PrivateJoystick*() to deliver events | |
219 * and update joystick device state. | |
220 */ | |
221 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
222 { | |
223 int i, j; | |
224 ISpAxisData a; | |
225 ISpDPadData b; | |
226 //ISpDeltaData c; | |
227 ISpButtonData d; | |
228 | |
229 for(i = 0, j = 0; i < joystick->naxes; i++, j++) | |
230 { | |
231 Sint16 value; | |
232 | |
233 ISpElement_GetSimpleState( | |
234 joystick->hwdata->refs[j], | |
235 &a); | |
236 value = (ISpSymmetricAxisToFloat(a)* 32767.0); | |
237 if ( value != joystick->axes[i] ) { | |
238 SDL_PrivateJoystickAxis(joystick, i, value); | |
239 } | |
240 } | |
241 | |
242 for(i = 0; i < joystick->nhats; i++, j++) | |
243 { | |
244 Uint8 pos; | |
245 | |
246 ISpElement_GetSimpleState( | |
247 joystick->hwdata->refs[j], | |
248 &b); | |
249 switch(b) { | |
250 case kISpPadIdle: | |
251 pos = SDL_HAT_CENTERED; | |
252 break; | |
253 case kISpPadLeft: | |
254 pos = SDL_HAT_LEFT; | |
255 break; | |
256 case kISpPadUpLeft: | |
257 pos = SDL_HAT_LEFTUP; | |
258 break; | |
259 case kISpPadUp: | |
260 pos = SDL_HAT_UP; | |
261 break; | |
262 case kISpPadUpRight: | |
263 pos = SDL_HAT_RIGHTUP; | |
264 break; | |
265 case kISpPadRight: | |
266 pos = SDL_HAT_RIGHT; | |
267 break; | |
268 case kISpPadDownRight: | |
269 pos = SDL_HAT_RIGHTDOWN; | |
270 break; | |
271 case kISpPadDown: | |
272 pos = SDL_HAT_DOWN; | |
273 break; | |
274 case kISpPadDownLeft: | |
275 pos = SDL_HAT_LEFTDOWN; | |
276 break; | |
277 } | |
278 if ( pos != joystick->hats[i] ) { | |
279 SDL_PrivateJoystickHat(joystick, i, pos); | |
280 } | |
281 } | |
282 | |
283 for(i = 0; i < joystick->nballs; i++, j++) | |
284 { | |
285 /* ignore balls right now */ | |
286 } | |
287 | |
288 for(i = 0; i < joystick->nbuttons; i++, j++) | |
289 { | |
290 ISpElement_GetSimpleState( | |
291 joystick->hwdata->refs[j], | |
292 &d); | |
293 if ( d != joystick->buttons[i] ) { | |
294 SDL_PrivateJoystickButton(joystick, i, d); | |
295 } | |
296 } | |
297 } | |
298 | |
299 /* Function to close a joystick after use */ | |
300 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
301 { | |
302 int index; | |
303 | |
304 index = joystick->index; | |
305 | |
306 ISpDevices_Deactivate( | |
307 1, | |
308 &SYS_Joysticks[index]); | |
309 } | |
310 | |
311 /* Function to perform any system-specific joystick related cleanup */ | |
312 void SDL_SYS_JoystickQuit(void) | |
313 { | |
314 ISpShutdown(); | |
315 } | |
316 |