comparison src/joystick/macos/SDL_sysjoystick.c @ 1895:c121d94672cb

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