comparison src/joystick/macos/SDL_sysjoystick.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
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 66 int
67 SDL_SYS_JoystickInit (void) 67 SDL_SYS_JoystickInit(void)
68 { 68 {
69 static ISpDeviceClass classes[4] = { 69 static ISpDeviceClass classes[4] = {
70 kISpDeviceClass_Joystick, 70 kISpDeviceClass_Joystick,
71 #if kISpDeviceClass_Gamepad 71 #if kISpDeviceClass_Gamepad
72 kISpDeviceClass_Gamepad, 72 kISpDeviceClass_Gamepad,
77 OSErr err; 77 OSErr err;
78 int i; 78 int i;
79 UInt32 count, numJoysticks; 79 UInt32 count, numJoysticks;
80 80
81 if ((Ptr) 0 == (Ptr) ISpStartup) { 81 if ((Ptr) 0 == (Ptr) ISpStartup) {
82 SDL_SetError ("InputSprocket not installed"); 82 SDL_SetError("InputSprocket not installed");
83 return -1; // InputSprocket not installed 83 return -1; // InputSprocket not installed
84 } 84 }
85 85
86 if ((Ptr) 0 == (Ptr) ISpGetVersion) { 86 if ((Ptr) 0 == (Ptr) ISpGetVersion) {
87 SDL_SetError ("InputSprocket not version 1.1 or newer"); 87 SDL_SetError("InputSprocket not version 1.1 or newer");
88 return -1; // old version of ISp (not at least 1.1) 88 return -1; // old version of ISp (not at least 1.1)
89 } 89 }
90 90
91 ISpStartup (); 91 ISpStartup();
92 92
93 /* Get all the joysticks */ 93 /* Get all the joysticks */
94 numJoysticks = 0; 94 numJoysticks = 0;
95 for (i = 0; classes[i]; ++i) { 95 for (i = 0; classes[i]; ++i) {
96 count = 0; 96 count = 0;
97 err = ISpDevices_ExtractByClass (classes[i], 97 err = ISpDevices_ExtractByClass(classes[i],
98 MAX_JOYSTICKS - numJoysticks, 98 MAX_JOYSTICKS - numJoysticks,
99 &count, 99 &count, &SYS_Joysticks[numJoysticks]);
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 ISpDevice_GetDefinition (SYS_Joysticks[i], 104 ISpDevice_GetDefinition(SYS_Joysticks[i],
106 sizeof (ISpDeviceDefinition), 105 sizeof(ISpDeviceDefinition), &SYS_DevDef[i]);
107 &SYS_DevDef[i]); 106
108 107 err = ISpElementList_New(0, NULL, &SYS_Elements[i], 0);
109 err = ISpElementList_New (0, NULL, &SYS_Elements[i], 0);
110 108
111 if (err) { 109 if (err) {
112 SDL_OutOfMemory (); 110 SDL_OutOfMemory();
113 return -1; 111 return -1;
114 } 112 }
115 113
116 ISpDevice_GetElementList (SYS_Joysticks[i], &SYS_Elements[i]); 114 ISpDevice_GetElementList(SYS_Joysticks[i], &SYS_Elements[i]);
117 } 115 }
118 116
119 ISpDevices_Deactivate (numJoysticks, SYS_Joysticks); 117 ISpDevices_Deactivate(numJoysticks, SYS_Joysticks);
120 118
121 return numJoysticks; 119 return numJoysticks;
122 } 120 }
123 121
124 /* Function to get the device-dependent name of a joystick */ 122 /* Function to get the device-dependent name of a joystick */
125 const char * 123 const char *
126 SDL_SYS_JoystickName (int index) 124 SDL_SYS_JoystickName(int index)
127 { 125 {
128 static char name[64]; 126 static char name[64];
129 int len; 127 int len;
130 128
131 /* convert pascal string to c-string */ 129 /* convert pascal string to c-string */
132 len = SYS_DevDef[index].deviceName[0]; 130 len = SYS_DevDef[index].deviceName[0];
133 if (len >= sizeof (name)) { 131 if (len >= sizeof(name)) {
134 len = (sizeof (name) - 1); 132 len = (sizeof(name) - 1);
135 } 133 }
136 SDL_memcpy (name, &SYS_DevDef[index].deviceName[1], len); 134 SDL_memcpy(name, &SYS_DevDef[index].deviceName[1], len);
137 name[len] = '\0'; 135 name[len] = '\0';
138 136
139 return name; 137 return name;
140 } 138 }
141 139
143 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.
144 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.
145 It returns 0, or -1 if there is an error. 143 It returns 0, or -1 if there is an error.
146 */ 144 */
147 int 145 int
148 SDL_SYS_JoystickOpen (SDL_Joystick * joystick) 146 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
149 { 147 {
150 int index; 148 int index;
151 UInt32 count, gotCount, count2; 149 UInt32 count, gotCount, count2;
152 long numAxis, numButtons, numHats, numBalls; 150 long numAxis, numButtons, numHats, numBalls;
153 151
157 155
158 index = joystick->index; 156 index = joystick->index;
159 157
160 /* allocate memory for system specific hardware data */ 158 /* allocate memory for system specific hardware data */
161 joystick->hwdata = 159 joystick->hwdata =
162 (struct joystick_hwdata *) SDL_malloc (sizeof (*joystick->hwdata)); 160 (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
163 if (joystick->hwdata == NULL) { 161 if (joystick->hwdata == NULL) {
164 SDL_OutOfMemory (); 162 SDL_OutOfMemory();
165 return (-1); 163 return (-1);
166 } 164 }
167 SDL_memset (joystick->hwdata, 0, sizeof (*joystick->hwdata)); 165 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
168 SDL_strlcpy (joystick->hwdata->name, SDL_SYS_JoystickName (index), 166 SDL_strlcpy(joystick->hwdata->name, SDL_SYS_JoystickName(index),
169 SDL_arraysize (joystick->hwdata->name)); 167 SDL_arraysize(joystick->hwdata->name));
170 joystick->name = joystick->hwdata->name; 168 joystick->name = joystick->hwdata->name;
171 169
172 ISpElementList_ExtractByKind (SYS_Elements[index], 170 ISpElementList_ExtractByKind(SYS_Elements[index],
173 kISpElementKind_Axis, 171 kISpElementKind_Axis,
174 count, &gotCount, joystick->hwdata->refs); 172 count, &gotCount, joystick->hwdata->refs);
175 173
176 numAxis = gotCount; 174 numAxis = gotCount;
177 count -= gotCount; 175 count -= gotCount;
178 count2 += gotCount; 176 count2 += gotCount;
179 177
180 ISpElementList_ExtractByKind (SYS_Elements[index], 178 ISpElementList_ExtractByKind(SYS_Elements[index],
181 kISpElementKind_DPad, 179 kISpElementKind_DPad,
182 count, 180 count,
183 &gotCount, 181 &gotCount,
184 &(joystick->hwdata->refs[count2])); 182 &(joystick->hwdata->refs[count2]));
185 183
186 numHats = gotCount; 184 numHats = gotCount;
187 count -= gotCount; 185 count -= gotCount;
188 count2 += gotCount; 186 count2 += gotCount;
189 187
190 ISpElementList_ExtractByKind (SYS_Elements[index], 188 ISpElementList_ExtractByKind(SYS_Elements[index],
191 kISpElementKind_Button, 189 kISpElementKind_Button,
192 count, 190 count,
193 &gotCount, 191 &gotCount,
194 &(joystick->hwdata->refs[count2])); 192 &(joystick->hwdata->refs[count2]));
195 193
196 numButtons = gotCount; 194 numButtons = gotCount;
197 count -= gotCount; 195 count -= gotCount;
198 count2 += gotCount; 196 count2 += gotCount;
199 197
200 joystick->naxes = numAxis; 198 joystick->naxes = numAxis;
201 joystick->nhats = numHats; 199 joystick->nhats = numHats;
202 joystick->nballs = numBalls; 200 joystick->nballs = numBalls;
203 joystick->nbuttons = numButtons; 201 joystick->nbuttons = numButtons;
204 202
205 ISpDevices_Activate (1, &SYS_Joysticks[index]); 203 ISpDevices_Activate(1, &SYS_Joysticks[index]);
206 204
207 return 0; 205 return 0;
208 } 206 }
209 207
210 /* 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.
211 * This function shouldn't update the joystick structure directly, 209 * This function shouldn't update the joystick structure directly,
212 * but instead should call SDL_PrivateJoystick*() to deliver events 210 * but instead should call SDL_PrivateJoystick*() to deliver events
213 * and update joystick device state. 211 * and update joystick device state.
214 */ 212 */
215 void 213 void
216 SDL_SYS_JoystickUpdate (SDL_Joystick * joystick) 214 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
217 { 215 {
218 int i, j; 216 int i, j;
219 ISpAxisData a; 217 ISpAxisData a;
220 ISpDPadData b; 218 ISpDPadData b;
221 //ISpDeltaData c; 219 //ISpDeltaData c;
222 ISpButtonData d; 220 ISpButtonData d;
223 221
224 for (i = 0, j = 0; i < joystick->naxes; i++, j++) { 222 for (i = 0, j = 0; i < joystick->naxes; i++, j++) {
225 Sint16 value; 223 Sint16 value;
226 224
227 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &a); 225 ISpElement_GetSimpleState(joystick->hwdata->refs[j], &a);
228 value = (ISpSymmetricAxisToFloat (a) * 32767.0); 226 value = (ISpSymmetricAxisToFloat(a) * 32767.0);
229 if (value != joystick->axes[i]) { 227 if (value != joystick->axes[i]) {
230 SDL_PrivateJoystickAxis (joystick, i, value); 228 SDL_PrivateJoystickAxis(joystick, i, value);
231 } 229 }
232 } 230 }
233 231
234 for (i = 0; i < joystick->nhats; i++, j++) { 232 for (i = 0; i < joystick->nhats; i++, j++) {
235 Uint8 pos; 233 Uint8 pos;
236 234
237 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &b); 235 ISpElement_GetSimpleState(joystick->hwdata->refs[j], &b);
238 switch (b) { 236 switch (b) {
239 case kISpPadIdle: 237 case kISpPadIdle:
240 pos = SDL_HAT_CENTERED; 238 pos = SDL_HAT_CENTERED;
241 break; 239 break;
242 case kISpPadLeft: 240 case kISpPadLeft:
263 case kISpPadDownLeft: 261 case kISpPadDownLeft:
264 pos = SDL_HAT_LEFTDOWN; 262 pos = SDL_HAT_LEFTDOWN;
265 break; 263 break;
266 } 264 }
267 if (pos != joystick->hats[i]) { 265 if (pos != joystick->hats[i]) {
268 SDL_PrivateJoystickHat (joystick, i, pos); 266 SDL_PrivateJoystickHat(joystick, i, pos);
269 } 267 }
270 } 268 }
271 269
272 for (i = 0; i < joystick->nballs; i++, j++) { 270 for (i = 0; i < joystick->nballs; i++, j++) {
273 /* ignore balls right now */ 271 /* ignore balls right now */
274 } 272 }
275 273
276 for (i = 0; i < joystick->nbuttons; i++, j++) { 274 for (i = 0; i < joystick->nbuttons; i++, j++) {
277 ISpElement_GetSimpleState (joystick->hwdata->refs[j], &d); 275 ISpElement_GetSimpleState(joystick->hwdata->refs[j], &d);
278 if (d != joystick->buttons[i]) { 276 if (d != joystick->buttons[i]) {
279 SDL_PrivateJoystickButton (joystick, i, d); 277 SDL_PrivateJoystickButton(joystick, i, d);
280 } 278 }
281 } 279 }
282 } 280 }
283 281
284 /* Function to close a joystick after use */ 282 /* Function to close a joystick after use */
285 void 283 void
286 SDL_SYS_JoystickClose (SDL_Joystick * joystick) 284 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
287 { 285 {
288 int index; 286 int index;
289 287
290 index = joystick->index; 288 index = joystick->index;
291 289
292 ISpDevices_Deactivate (1, &SYS_Joysticks[index]); 290 ISpDevices_Deactivate(1, &SYS_Joysticks[index]);
293 } 291 }
294 292
295 /* Function to perform any system-specific joystick related cleanup */ 293 /* Function to perform any system-specific joystick related cleanup */
296 void 294 void
297 SDL_SYS_JoystickQuit (void) 295 SDL_SYS_JoystickQuit(void)
298 { 296 {
299 ISpShutdown (); 297 ISpShutdown();
300 } 298 }
301 299
302 #endif /* SDL_JOYSTICK_MACOS */ 300 #endif /* SDL_JOYSTICK_MACOS */
303 /* vi: set ts=4 sw=4 expandtab: */ 301 /* vi: set ts=4 sw=4 expandtab: */