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