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