comparison src/joystick/macos/SDL_sysjoystick.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 92947e3a18db
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
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,
99 &count, 100 &SYS_Joysticks[numJoysticks]);
100 &SYS_Joysticks[numJoysticks]);
101 numJoysticks += count; 101 numJoysticks += count;
102 } 102 }
103 103
104 for(i = 0; i < numJoysticks; i++) 104 for (i = 0; i < numJoysticks; i++) {
105 { 105 ISpDevice_GetDefinition (SYS_Joysticks[i],
106 ISpDevice_GetDefinition( 106 sizeof (ISpDeviceDefinition),
107 SYS_Joysticks[i], sizeof(ISpDeviceDefinition), 107 &SYS_DevDef[i]);
108 &SYS_DevDef[i]); 108
109 109 err = ISpElementList_New (0, NULL, &SYS_Elements[i], 0);
110 err = ISpElementList_New( 110
111 0, NULL,
112 &SYS_Elements[i], 0);
113
114 if (err) { 111 if (err) {
115 SDL_OutOfMemory(); 112 SDL_OutOfMemory ();
116 return -1; 113 return -1;
117 } 114 }
118 115
119 ISpDevice_GetElementList( 116 ISpDevice_GetElementList (SYS_Joysticks[i], &SYS_Elements[i]);
120 SYS_Joysticks[i], 117 }
121 &SYS_Elements[i]); 118
122 } 119 ISpDevices_Deactivate (numJoysticks, SYS_Joysticks);
123
124 ISpDevices_Deactivate(numJoysticks, SYS_Joysticks);
125 120
126 return numJoysticks; 121 return numJoysticks;
127 } 122 }
128 123
129 /* Function to get the device-dependent name of a joystick */ 124 /* Function to get the device-dependent name of a joystick */
130 const char *SDL_SYS_JoystickName(int index) 125 const char *
126 SDL_SYS_JoystickName (int index)
131 { 127 {
132 static char name[64]; 128 static char name[64];
133 int len; 129 int len;
134 130
135 /* convert pascal string to c-string */ 131 /* convert pascal string to c-string */
136 len = SYS_DevDef[index].deviceName[0]; 132 len = SYS_DevDef[index].deviceName[0];
137 if ( len >= sizeof(name) ) { 133 if (len >= sizeof (name)) {
138 len = (sizeof(name) - 1); 134 len = (sizeof (name) - 1);
139 } 135 }
140 SDL_memcpy(name, &SYS_DevDef[index].deviceName[1], len); 136 SDL_memcpy (name, &SYS_DevDef[index].deviceName[1], len);
141 name[len] = '\0'; 137 name[len] = '\0';
142 138
143 return name; 139 return name;
144 } 140 }
145 141
146 /* Function to open a joystick for use. 142 /* Function to open a joystick for use.
147 The joystick to open is specified by the index field of the joystick. 143 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. 144 This should fill the nbuttons and naxes fields of the joystick structure.
149 It returns 0, or -1 if there is an error. 145 It returns 0, or -1 if there is an error.
150 */ 146 */
151 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) 147 int
152 { 148 SDL_SYS_JoystickOpen (SDL_Joystick * joystick)
153 int index; 149 {
154 UInt32 count, gotCount, count2; 150 int index;
155 long numAxis, numButtons, numHats, numBalls; 151 UInt32 count, gotCount, count2;
152 long numAxis, numButtons, numHats, numBalls;
156 153
157 count = kMaxReferences; 154 count = kMaxReferences;
158 count2 = 0; 155 count2 = 0;
159 numAxis = numButtons = numHats = numBalls = 0; 156 numAxis = numButtons = numHats = numBalls = 0;
160 157
161 index = joystick->index; 158 index = joystick->index;
162 159
163 /* allocate memory for system specific hardware data */ 160 /* allocate memory for system specific hardware data */
164 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); 161 joystick->hwdata =
165 if (joystick->hwdata == NULL) 162 (struct joystick_hwdata *) SDL_malloc (sizeof (*joystick->hwdata));
166 { 163 if (joystick->hwdata == NULL) {
167 SDL_OutOfMemory(); 164 SDL_OutOfMemory ();
168 return(-1); 165 return (-1);
169 } 166 }
170 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); 167 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata));
171 SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index), SDL_arraysize(joystick->hwdata->name)); 168 SDL_strlcpy (joystick->hwdata->name, SDL_SYS_JoystickName (index),
169 SDL_arraysize (joystick->hwdata->name));
172 joystick->name = joystick->hwdata->name; 170 joystick->name = joystick->hwdata->name;
173 171
174 ISpElementList_ExtractByKind( 172 ISpElementList_ExtractByKind (SYS_Elements[index],
175 SYS_Elements[index], 173 kISpElementKind_Axis,
176 kISpElementKind_Axis, 174 count, &gotCount, joystick->hwdata->refs);
177 count,
178 &gotCount,
179 joystick->hwdata->refs);
180 175
181 numAxis = gotCount; 176 numAxis = gotCount;
182 count -= gotCount; 177 count -= gotCount;
183 count2 += gotCount; 178 count2 += gotCount;
184 179
185 ISpElementList_ExtractByKind( 180 ISpElementList_ExtractByKind (SYS_Elements[index],
186 SYS_Elements[index], 181 kISpElementKind_DPad,
187 kISpElementKind_DPad, 182 count,
188 count, 183 &gotCount,
189 &gotCount, 184 &(joystick->hwdata->refs[count2]));
190 &(joystick->hwdata->refs[count2]));
191 185
192 numHats = gotCount; 186 numHats = gotCount;
193 count -= gotCount; 187 count -= gotCount;
194 count2 += gotCount; 188 count2 += gotCount;
195 189
196 ISpElementList_ExtractByKind( 190 ISpElementList_ExtractByKind (SYS_Elements[index],
197 SYS_Elements[index], 191 kISpElementKind_Button,
198 kISpElementKind_Button, 192 count,
199 count, 193 &gotCount,
200 &gotCount, 194 &(joystick->hwdata->refs[count2]));
201 &(joystick->hwdata->refs[count2]));
202 195
203 numButtons = gotCount; 196 numButtons = gotCount;
204 count -= gotCount; 197 count -= gotCount;
205 count2 += gotCount; 198 count2 += gotCount;
206 199
207 joystick->naxes = numAxis; 200 joystick->naxes = numAxis;
208 joystick->nhats = numHats; 201 joystick->nhats = numHats;
209 joystick->nballs = numBalls; 202 joystick->nballs = numBalls;
210 joystick->nbuttons = numButtons; 203 joystick->nbuttons = numButtons;
211 204
212 ISpDevices_Activate( 205 ISpDevices_Activate (1, &SYS_Joysticks[index]);
213 1,
214 &SYS_Joysticks[index]);
215 206
216 return 0; 207 return 0;
217 } 208 }
218 209
219 /* Function to update the state of a joystick - called as a device poll. 210 /* Function to update the state of a joystick - called as a device poll.
220 * This function shouldn't update the joystick structure directly, 211 * This function shouldn't update the joystick structure directly,
221 * but instead should call SDL_PrivateJoystick*() to deliver events 212 * but instead should call SDL_PrivateJoystick*() to deliver events
222 * and update joystick device state. 213 * and update joystick device state.
223 */ 214 */
224 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) 215 void
225 { 216 SDL_SYS_JoystickUpdate (SDL_Joystick * joystick)
226 int i, j; 217 {
227 ISpAxisData a; 218 int i, j;
228 ISpDPadData b; 219 ISpAxisData a;
220 ISpDPadData b;
229 //ISpDeltaData c; 221 //ISpDeltaData c;
230 ISpButtonData d; 222 ISpButtonData d;
231 223
232 for(i = 0, j = 0; i < joystick->naxes; i++, j++) 224 for (i = 0, j = 0; i < joystick->naxes; i++, j++) {
233 {
234 Sint16 value; 225 Sint16 value;
235 226
236 ISpElement_GetSimpleState( 227 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &a);
237 joystick->hwdata->refs[j], 228 value = (ISpSymmetricAxisToFloat (a) * 32767.0);
238 &a); 229 if (value != joystick->axes[i]) {
239 value = (ISpSymmetricAxisToFloat(a)* 32767.0); 230 SDL_PrivateJoystickAxis (joystick, i, value);
240 if ( value != joystick->axes[i] ) { 231 }
241 SDL_PrivateJoystickAxis(joystick, i, value); 232 }
242 } 233
243 } 234 for (i = 0; i < joystick->nhats; i++, j++) {
244
245 for(i = 0; i < joystick->nhats; i++, j++)
246 {
247 Uint8 pos; 235 Uint8 pos;
248 236
249 ISpElement_GetSimpleState( 237 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &b);
250 joystick->hwdata->refs[j], 238 switch (b) {
251 &b); 239 case kISpPadIdle:
252 switch(b) { 240 pos = SDL_HAT_CENTERED;
253 case kISpPadIdle: 241 break;
254 pos = SDL_HAT_CENTERED; 242 case kISpPadLeft:
255 break; 243 pos = SDL_HAT_LEFT;
256 case kISpPadLeft: 244 break;
257 pos = SDL_HAT_LEFT; 245 case kISpPadUpLeft:
258 break; 246 pos = SDL_HAT_LEFTUP;
259 case kISpPadUpLeft: 247 break;
260 pos = SDL_HAT_LEFTUP; 248 case kISpPadUp:
261 break; 249 pos = SDL_HAT_UP;
262 case kISpPadUp: 250 break;
263 pos = SDL_HAT_UP; 251 case kISpPadUpRight:
264 break; 252 pos = SDL_HAT_RIGHTUP;
265 case kISpPadUpRight: 253 break;
266 pos = SDL_HAT_RIGHTUP; 254 case kISpPadRight:
267 break; 255 pos = SDL_HAT_RIGHT;
268 case kISpPadRight: 256 break;
269 pos = SDL_HAT_RIGHT; 257 case kISpPadDownRight:
270 break; 258 pos = SDL_HAT_RIGHTDOWN;
271 case kISpPadDownRight: 259 break;
272 pos = SDL_HAT_RIGHTDOWN; 260 case kISpPadDown:
273 break; 261 pos = SDL_HAT_DOWN;
274 case kISpPadDown: 262 break;
275 pos = SDL_HAT_DOWN; 263 case kISpPadDownLeft:
276 break; 264 pos = SDL_HAT_LEFTDOWN;
277 case kISpPadDownLeft: 265 break;
278 pos = SDL_HAT_LEFTDOWN; 266 }
279 break; 267 if (pos != joystick->hats[i]) {
280 } 268 SDL_PrivateJoystickHat (joystick, i, pos);
281 if ( pos != joystick->hats[i] ) { 269 }
282 SDL_PrivateJoystickHat(joystick, i, pos); 270 }
283 } 271
284 } 272 for (i = 0; i < joystick->nballs; i++, j++) {
285
286 for(i = 0; i < joystick->nballs; i++, j++)
287 {
288 /* ignore balls right now */ 273 /* ignore balls right now */
289 } 274 }
290 275
291 for(i = 0; i < joystick->nbuttons; i++, j++) 276 for (i = 0; i < joystick->nbuttons; i++, j++) {
292 { 277 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &d);
293 ISpElement_GetSimpleState( 278 if (d != joystick->buttons[i]) {
294 joystick->hwdata->refs[j], 279 SDL_PrivateJoystickButton (joystick, i, d);
295 &d);
296 if ( d != joystick->buttons[i] ) {
297 SDL_PrivateJoystickButton(joystick, i, d);
298 } 280 }
299 } 281 }
300 } 282 }
301 283
302 /* Function to close a joystick after use */ 284 /* Function to close a joystick after use */
303 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) 285 void
286 SDL_SYS_JoystickClose (SDL_Joystick * joystick)
304 { 287 {
305 int index; 288 int index;
306 289
307 index = joystick->index; 290 index = joystick->index;
308 291
309 ISpDevices_Deactivate( 292 ISpDevices_Deactivate (1, &SYS_Joysticks[index]);
310 1,
311 &SYS_Joysticks[index]);
312 } 293 }
313 294
314 /* Function to perform any system-specific joystick related cleanup */ 295 /* Function to perform any system-specific joystick related cleanup */
315 void SDL_SYS_JoystickQuit(void) 296 void
316 { 297 SDL_SYS_JoystickQuit (void)
317 ISpShutdown(); 298 {
299 ISpShutdown ();
318 } 300 }
319 301
320 #endif /* SDL_JOYSTICK_MACOS */ 302 #endif /* SDL_JOYSTICK_MACOS */
303 /* vi: set ts=4 sw=4 expandtab: */