comparison src/joystick/SDL_joystick.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 97d0966f4bf7
children 96adc8025331
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
38 38
39 Uint8 SDL_numjoysticks = 0; 39 Uint8 SDL_numjoysticks = 0;
40 SDL_Joystick **SDL_joysticks = NULL; 40 SDL_Joystick **SDL_joysticks = NULL;
41 static SDL_Joystick *default_joystick = NULL; 41 static SDL_Joystick *default_joystick = NULL;
42 42
43 int SDL_JoystickInit(void) 43 int
44 { 44 SDL_JoystickInit(void)
45 int arraylen; 45 {
46 int status; 46 int arraylen;
47 47 int status;
48 SDL_numjoysticks = 0; 48
49 status = SDL_SYS_JoystickInit(); 49 SDL_numjoysticks = 0;
50 if ( status >= 0 ) { 50 status = SDL_SYS_JoystickInit();
51 arraylen = (status+1)*sizeof(*SDL_joysticks); 51 if (status >= 0) {
52 SDL_joysticks = (SDL_Joystick **)SDL_malloc(arraylen); 52 arraylen = (status + 1) * sizeof(*SDL_joysticks);
53 if ( SDL_joysticks == NULL ) { 53 SDL_joysticks = (SDL_Joystick **) SDL_malloc(arraylen);
54 SDL_numjoysticks = 0; 54 if (SDL_joysticks == NULL) {
55 } else { 55 SDL_numjoysticks = 0;
56 SDL_memset(SDL_joysticks, 0, arraylen); 56 } else {
57 SDL_numjoysticks = status; 57 SDL_memset(SDL_joysticks, 0, arraylen);
58 } 58 SDL_numjoysticks = status;
59 status = 0; 59 }
60 } 60 status = 0;
61 default_joystick = NULL; 61 }
62 return(status); 62 default_joystick = NULL;
63 return (status);
63 } 64 }
64 65
65 /* 66 /*
66 * Count the number of joysticks attached to the system 67 * Count the number of joysticks attached to the system
67 */ 68 */
68 int SDL_NumJoysticks(void) 69 int
69 { 70 SDL_NumJoysticks(void)
70 return SDL_numjoysticks; 71 {
72 return SDL_numjoysticks;
71 } 73 }
72 74
73 /* 75 /*
74 * Get the implementation dependent name of a joystick 76 * Get the implementation dependent name of a joystick
75 */ 77 */
76 const char *SDL_JoystickName(int device_index) 78 const char *
77 { 79 SDL_JoystickName(int device_index)
78 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { 80 {
79 SDL_SetError("There are %d joysticks available", 81 if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
80 SDL_numjoysticks); 82 SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
81 return(NULL); 83 return (NULL);
82 } 84 }
83 return(SDL_SYS_JoystickName(device_index)); 85 return (SDL_SYS_JoystickName(device_index));
84 } 86 }
85 87
86 /* 88 /*
87 * Open a joystick for use - the index passed as an argument refers to 89 * Open a joystick for use - the index passed as an argument refers to
88 * the N'th joystick on the system. This index is the value which will 90 * the N'th joystick on the system. This index is the value which will
89 * identify this joystick in future joystick events. 91 * identify this joystick in future joystick events.
90 * 92 *
91 * This function returns a joystick identifier, or NULL if an error occurred. 93 * This function returns a joystick identifier, or NULL if an error occurred.
92 */ 94 */
93 SDL_Joystick *SDL_JoystickOpen(int device_index) 95 SDL_Joystick *
94 { 96 SDL_JoystickOpen(int device_index)
95 int i; 97 {
96 SDL_Joystick *joystick; 98 int i;
97 99 SDL_Joystick *joystick;
98 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { 100
99 SDL_SetError("There are %d joysticks available", 101 if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
100 SDL_numjoysticks); 102 SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
101 return(NULL); 103 return (NULL);
102 } 104 }
103 105
104 /* If the joystick is already open, return it */ 106 /* If the joystick is already open, return it */
105 for ( i=0; SDL_joysticks[i]; ++i ) { 107 for (i = 0; SDL_joysticks[i]; ++i) {
106 if ( device_index == SDL_joysticks[i]->index ) { 108 if (device_index == SDL_joysticks[i]->index) {
107 joystick = SDL_joysticks[i]; 109 joystick = SDL_joysticks[i];
108 ++joystick->ref_count; 110 ++joystick->ref_count;
109 return(joystick); 111 return (joystick);
110 } 112 }
111 } 113 }
112 114
113 /* Create and initialize the joystick */ 115 /* Create and initialize the joystick */
114 joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick)); 116 joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
115 if ( joystick != NULL ) { 117 if (joystick != NULL) {
116 SDL_memset(joystick, 0, (sizeof *joystick)); 118 SDL_memset(joystick, 0, (sizeof *joystick));
117 joystick->index = device_index; 119 joystick->index = device_index;
118 if ( SDL_SYS_JoystickOpen(joystick) < 0 ) { 120 if (SDL_SYS_JoystickOpen(joystick) < 0) {
119 SDL_free(joystick); 121 SDL_free(joystick);
120 joystick = NULL; 122 joystick = NULL;
121 } else { 123 } else {
122 if ( joystick->naxes > 0 ) { 124 if (joystick->naxes > 0) {
123 joystick->axes = (Sint16 *)SDL_malloc 125 joystick->axes = (Sint16 *) SDL_malloc
124 (joystick->naxes*sizeof(Sint16)); 126 (joystick->naxes * sizeof(Sint16));
125 } 127 }
126 if ( joystick->nhats > 0 ) { 128 if (joystick->nhats > 0) {
127 joystick->hats = (Uint8 *)SDL_malloc 129 joystick->hats = (Uint8 *) SDL_malloc
128 (joystick->nhats*sizeof(Uint8)); 130 (joystick->nhats * sizeof(Uint8));
129 } 131 }
130 if ( joystick->nballs > 0 ) { 132 if (joystick->nballs > 0) {
131 joystick->balls = (struct balldelta *)SDL_malloc 133 joystick->balls = (struct balldelta *) SDL_malloc
132 (joystick->nballs*sizeof(*joystick->balls)); 134 (joystick->nballs * sizeof(*joystick->balls));
133 } 135 }
134 if ( joystick->nbuttons > 0 ) { 136 if (joystick->nbuttons > 0) {
135 joystick->buttons = (Uint8 *)SDL_malloc 137 joystick->buttons = (Uint8 *) SDL_malloc
136 (joystick->nbuttons*sizeof(Uint8)); 138 (joystick->nbuttons * sizeof(Uint8));
137 } 139 }
138 if ( ((joystick->naxes > 0) && !joystick->axes) 140 if (((joystick->naxes > 0) && !joystick->axes)
139 || ((joystick->nhats > 0) && !joystick->hats) 141 || ((joystick->nhats > 0) && !joystick->hats)
140 || ((joystick->nballs > 0) && !joystick->balls) 142 || ((joystick->nballs > 0) && !joystick->balls)
141 || ((joystick->nbuttons > 0) && !joystick->buttons)) { 143 || ((joystick->nbuttons > 0) && !joystick->buttons)) {
142 SDL_OutOfMemory(); 144 SDL_OutOfMemory();
143 SDL_JoystickClose(joystick); 145 SDL_JoystickClose(joystick);
144 joystick = NULL; 146 joystick = NULL;
145 } 147 }
146 if ( joystick->axes ) { 148 if (joystick->axes) {
147 SDL_memset(joystick->axes, 0, 149 SDL_memset(joystick->axes, 0,
148 joystick->naxes*sizeof(Sint16)); 150 joystick->naxes * sizeof(Sint16));
149 } 151 }
150 if ( joystick->hats ) { 152 if (joystick->hats) {
151 SDL_memset(joystick->hats, 0, 153 SDL_memset(joystick->hats, 0,
152 joystick->nhats*sizeof(Uint8)); 154 joystick->nhats * sizeof(Uint8));
153 } 155 }
154 if ( joystick->balls ) { 156 if (joystick->balls) {
155 SDL_memset(joystick->balls, 0, 157 SDL_memset(joystick->balls, 0,
156 joystick->nballs*sizeof(*joystick->balls)); 158 joystick->nballs * sizeof(*joystick->balls));
157 } 159 }
158 if ( joystick->buttons ) { 160 if (joystick->buttons) {
159 SDL_memset(joystick->buttons, 0, 161 SDL_memset(joystick->buttons, 0,
160 joystick->nbuttons*sizeof(Uint8)); 162 joystick->nbuttons * sizeof(Uint8));
161 } 163 }
162 } 164 }
163 } 165 }
164 if ( joystick ) { 166 if (joystick) {
165 /* Add joystick to list */ 167 /* Add joystick to list */
166 ++joystick->ref_count; 168 ++joystick->ref_count;
167 SDL_Lock_EventThread(); 169 SDL_Lock_EventThread();
168 for ( i=0; SDL_joysticks[i]; ++i ) 170 for (i = 0; SDL_joysticks[i]; ++i)
169 /* Skip to next joystick */; 171 /* Skip to next joystick */ ;
170 SDL_joysticks[i] = joystick; 172 SDL_joysticks[i] = joystick;
171 SDL_Unlock_EventThread(); 173 SDL_Unlock_EventThread();
172 } 174 }
173 return(joystick); 175 return (joystick);
174 } 176 }
175 177
176 /* 178 /*
177 * Returns 1 if the joystick has been opened, or 0 if it has not. 179 * Returns 1 if the joystick has been opened, or 0 if it has not.
178 */ 180 */
179 int SDL_JoystickOpened(int device_index) 181 int
180 { 182 SDL_JoystickOpened(int device_index)
181 int i, opened; 183 {
182 184 int i, opened;
183 opened = 0; 185
184 for ( i=0; SDL_joysticks[i]; ++i ) { 186 opened = 0;
185 if ( SDL_joysticks[i]->index == (Uint8)device_index ) { 187 for (i = 0; SDL_joysticks[i]; ++i) {
186 opened = 1; 188 if (SDL_joysticks[i]->index == (Uint8) device_index) {
187 break; 189 opened = 1;
188 } 190 break;
189 } 191 }
190 return(opened); 192 }
191 } 193 return (opened);
192 194 }
193 static int ValidJoystick(SDL_Joystick **joystick) 195
194 { 196 static int
195 int valid; 197 ValidJoystick(SDL_Joystick ** joystick)
196 198 {
197 if ( *joystick == NULL ) { 199 int valid;
198 *joystick = default_joystick; 200
199 } 201 if (*joystick == NULL) {
200 if ( *joystick == NULL ) { 202 *joystick = default_joystick;
201 SDL_SetError("Joystick hasn't been opened yet"); 203 }
202 valid = 0; 204 if (*joystick == NULL) {
203 } else { 205 SDL_SetError("Joystick hasn't been opened yet");
204 valid = 1; 206 valid = 0;
205 } 207 } else {
206 return valid; 208 valid = 1;
209 }
210 return valid;
207 } 211 }
208 212
209 /* 213 /*
210 * Get the device index of an opened joystick. 214 * Get the device index of an opened joystick.
211 */ 215 */
212 int SDL_JoystickIndex(SDL_Joystick *joystick) 216 int
213 { 217 SDL_JoystickIndex(SDL_Joystick * joystick)
214 if ( ! ValidJoystick(&joystick) ) { 218 {
215 return(-1); 219 if (!ValidJoystick(&joystick)) {
216 } 220 return (-1);
217 return(joystick->index); 221 }
222 return (joystick->index);
218 } 223 }
219 224
220 /* 225 /*
221 * Get the number of multi-dimensional axis controls on a joystick 226 * Get the number of multi-dimensional axis controls on a joystick
222 */ 227 */
223 int SDL_JoystickNumAxes(SDL_Joystick *joystick) 228 int
224 { 229 SDL_JoystickNumAxes(SDL_Joystick * joystick)
225 if ( ! ValidJoystick(&joystick) ) { 230 {
226 return(-1); 231 if (!ValidJoystick(&joystick)) {
227 } 232 return (-1);
228 return(joystick->naxes); 233 }
234 return (joystick->naxes);
229 } 235 }
230 236
231 /* 237 /*
232 * Get the number of hats on a joystick 238 * Get the number of hats on a joystick
233 */ 239 */
234 int SDL_JoystickNumHats(SDL_Joystick *joystick) 240 int
235 { 241 SDL_JoystickNumHats(SDL_Joystick * joystick)
236 if ( ! ValidJoystick(&joystick) ) { 242 {
237 return(-1); 243 if (!ValidJoystick(&joystick)) {
238 } 244 return (-1);
239 return(joystick->nhats); 245 }
246 return (joystick->nhats);
240 } 247 }
241 248
242 /* 249 /*
243 * Get the number of trackballs on a joystick 250 * Get the number of trackballs on a joystick
244 */ 251 */
245 int SDL_JoystickNumBalls(SDL_Joystick *joystick) 252 int
246 { 253 SDL_JoystickNumBalls(SDL_Joystick * joystick)
247 if ( ! ValidJoystick(&joystick) ) { 254 {
248 return(-1); 255 if (!ValidJoystick(&joystick)) {
249 } 256 return (-1);
250 return(joystick->nballs); 257 }
258 return (joystick->nballs);
251 } 259 }
252 260
253 /* 261 /*
254 * Get the number of buttons on a joystick 262 * Get the number of buttons on a joystick
255 */ 263 */
256 int SDL_JoystickNumButtons(SDL_Joystick *joystick) 264 int
257 { 265 SDL_JoystickNumButtons(SDL_Joystick * joystick)
258 if ( ! ValidJoystick(&joystick) ) { 266 {
259 return(-1); 267 if (!ValidJoystick(&joystick)) {
260 } 268 return (-1);
261 return(joystick->nbuttons); 269 }
270 return (joystick->nbuttons);
262 } 271 }
263 272
264 /* 273 /*
265 * Get the current state of an axis control on a joystick 274 * Get the current state of an axis control on a joystick
266 */ 275 */
267 Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis) 276 Sint16
268 { 277 SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
269 Sint16 state; 278 {
270 279 Sint16 state;
271 if ( ! ValidJoystick(&joystick) ) { 280
272 return(0); 281 if (!ValidJoystick(&joystick)) {
273 } 282 return (0);
274 if ( axis < joystick->naxes ) { 283 }
275 state = joystick->axes[axis]; 284 if (axis < joystick->naxes) {
276 } else { 285 state = joystick->axes[axis];
277 SDL_SetError("Joystick only has %d axes", joystick->naxes); 286 } else {
278 state = 0; 287 SDL_SetError("Joystick only has %d axes", joystick->naxes);
279 } 288 state = 0;
280 return(state); 289 }
290 return (state);
281 } 291 }
282 292
283 /* 293 /*
284 * Get the current state of a hat on a joystick 294 * Get the current state of a hat on a joystick
285 */ 295 */
286 Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat) 296 Uint8
287 { 297 SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
288 Uint8 state; 298 {
289 299 Uint8 state;
290 if ( ! ValidJoystick(&joystick) ) { 300
291 return(0); 301 if (!ValidJoystick(&joystick)) {
292 } 302 return (0);
293 if ( hat < joystick->nhats ) { 303 }
294 state = joystick->hats[hat]; 304 if (hat < joystick->nhats) {
295 } else { 305 state = joystick->hats[hat];
296 SDL_SetError("Joystick only has %d hats", joystick->nhats); 306 } else {
297 state = 0; 307 SDL_SetError("Joystick only has %d hats", joystick->nhats);
298 } 308 state = 0;
299 return(state); 309 }
310 return (state);
300 } 311 }
301 312
302 /* 313 /*
303 * Get the ball axis change since the last poll 314 * Get the ball axis change since the last poll
304 */ 315 */
305 int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy) 316 int
306 { 317 SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
307 int retval; 318 {
308 319 int retval;
309 if ( ! ValidJoystick(&joystick) ) { 320
310 return(-1); 321 if (!ValidJoystick(&joystick)) {
311 } 322 return (-1);
312 323 }
313 retval = 0; 324
314 if ( ball < joystick->nballs ) { 325 retval = 0;
315 if ( dx ) { 326 if (ball < joystick->nballs) {
316 *dx = joystick->balls[ball].dx; 327 if (dx) {
317 } 328 *dx = joystick->balls[ball].dx;
318 if ( dy ) { 329 }
319 *dy = joystick->balls[ball].dy; 330 if (dy) {
320 } 331 *dy = joystick->balls[ball].dy;
321 joystick->balls[ball].dx = 0; 332 }
322 joystick->balls[ball].dy = 0; 333 joystick->balls[ball].dx = 0;
323 } else { 334 joystick->balls[ball].dy = 0;
324 SDL_SetError("Joystick only has %d balls", joystick->nballs); 335 } else {
325 retval = -1; 336 SDL_SetError("Joystick only has %d balls", joystick->nballs);
326 } 337 retval = -1;
327 return(retval); 338 }
339 return (retval);
328 } 340 }
329 341
330 /* 342 /*
331 * Get the current state of a button on a joystick 343 * Get the current state of a button on a joystick
332 */ 344 */
333 Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button) 345 Uint8
334 { 346 SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
335 Uint8 state; 347 {
336 348 Uint8 state;
337 if ( ! ValidJoystick(&joystick) ) { 349
338 return(0); 350 if (!ValidJoystick(&joystick)) {
339 } 351 return (0);
340 if ( button < joystick->nbuttons ) { 352 }
341 state = joystick->buttons[button]; 353 if (button < joystick->nbuttons) {
342 } else { 354 state = joystick->buttons[button];
343 SDL_SetError("Joystick only has %d buttons",joystick->nbuttons); 355 } else {
344 state = 0; 356 SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
345 } 357 state = 0;
346 return(state); 358 }
359 return (state);
347 } 360 }
348 361
349 /* 362 /*
350 * Close a joystick previously opened with SDL_JoystickOpen() 363 * Close a joystick previously opened with SDL_JoystickOpen()
351 */ 364 */
352 void SDL_JoystickClose(SDL_Joystick *joystick) 365 void
353 { 366 SDL_JoystickClose(SDL_Joystick * joystick)
354 int i; 367 {
355 368 int i;
356 if ( ! ValidJoystick(&joystick) ) { 369
357 return; 370 if (!ValidJoystick(&joystick)) {
358 } 371 return;
359 372 }
360 /* First decrement ref count */ 373
361 if ( --joystick->ref_count > 0 ) { 374 /* First decrement ref count */
362 return; 375 if (--joystick->ref_count > 0) {
363 } 376 return;
364 377 }
365 /* Lock the event queue - prevent joystick polling */ 378
366 SDL_Lock_EventThread(); 379 /* Lock the event queue - prevent joystick polling */
367 380 SDL_Lock_EventThread();
368 if ( joystick == default_joystick ) { 381
369 default_joystick = NULL; 382 if (joystick == default_joystick) {
370 } 383 default_joystick = NULL;
371 SDL_SYS_JoystickClose(joystick); 384 }
372 385 SDL_SYS_JoystickClose(joystick);
373 /* Remove joystick from list */ 386
374 for ( i=0; SDL_joysticks[i]; ++i ) { 387 /* Remove joystick from list */
375 if ( joystick == SDL_joysticks[i] ) { 388 for (i = 0; SDL_joysticks[i]; ++i) {
376 SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i+1], 389 if (joystick == SDL_joysticks[i]) {
377 (SDL_numjoysticks-i)*sizeof(joystick)); 390 SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i + 1],
378 break; 391 (SDL_numjoysticks - i) * sizeof(joystick));
379 } 392 break;
380 } 393 }
381 394 }
382 /* Let the event thread keep running */ 395
383 SDL_Unlock_EventThread(); 396 /* Let the event thread keep running */
384 397 SDL_Unlock_EventThread();
385 /* Free the data associated with this joystick */ 398
386 if ( joystick->axes ) { 399 /* Free the data associated with this joystick */
387 SDL_free(joystick->axes); 400 if (joystick->axes) {
388 } 401 SDL_free(joystick->axes);
389 if ( joystick->hats ) { 402 }
390 SDL_free(joystick->hats); 403 if (joystick->hats) {
391 } 404 SDL_free(joystick->hats);
392 if ( joystick->balls ) { 405 }
393 SDL_free(joystick->balls); 406 if (joystick->balls) {
394 } 407 SDL_free(joystick->balls);
395 if ( joystick->buttons ) { 408 }
396 SDL_free(joystick->buttons); 409 if (joystick->buttons) {
397 } 410 SDL_free(joystick->buttons);
398 SDL_free(joystick); 411 }
399 } 412 SDL_free(joystick);
400 413 }
401 void SDL_JoystickQuit(void) 414
402 { 415 void
403 /* Stop the event polling */ 416 SDL_JoystickQuit(void)
404 SDL_Lock_EventThread(); 417 {
405 SDL_numjoysticks = 0; 418 /* Stop the event polling */
406 SDL_Unlock_EventThread(); 419 SDL_Lock_EventThread();
407 420 SDL_numjoysticks = 0;
408 /* Quit the joystick setup */ 421 SDL_Unlock_EventThread();
409 SDL_SYS_JoystickQuit(); 422
410 if ( SDL_joysticks ) { 423 /* Quit the joystick setup */
411 SDL_free(SDL_joysticks); 424 SDL_SYS_JoystickQuit();
412 SDL_joysticks = NULL; 425 if (SDL_joysticks) {
413 } 426 SDL_free(SDL_joysticks);
427 SDL_joysticks = NULL;
428 }
414 } 429 }
415 430
416 431
417 /* These are global for SDL_sysjoystick.c and SDL_events.c */ 432 /* These are global for SDL_sysjoystick.c and SDL_events.c */
418 433
419 int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value) 434 int
420 { 435 SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
421 int posted; 436 {
422 437 int posted;
423 /* Update internal joystick state */ 438
424 joystick->axes[axis] = value; 439 /* Update internal joystick state */
425 440 joystick->axes[axis] = value;
426 /* Post the event, if desired */ 441
427 posted = 0; 442 /* Post the event, if desired */
443 posted = 0;
428 #if !SDL_EVENTS_DISABLED 444 #if !SDL_EVENTS_DISABLED
429 if ( SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE ) { 445 if (SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE) {
430 SDL_Event event; 446 SDL_Event event;
431 event.type = SDL_JOYAXISMOTION; 447 event.type = SDL_JOYAXISMOTION;
432 event.jaxis.which = joystick->index; 448 event.jaxis.which = joystick->index;
433 event.jaxis.axis = axis; 449 event.jaxis.axis = axis;
434 event.jaxis.value = value; 450 event.jaxis.value = value;
435 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 451 if ((SDL_EventOK == NULL)
436 posted = 1; 452 || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
437 SDL_PushEvent(&event); 453 posted = 1;
438 } 454 SDL_PushEvent(&event);
439 } 455 }
456 }
440 #endif /* !SDL_EVENTS_DISABLED */ 457 #endif /* !SDL_EVENTS_DISABLED */
441 return(posted); 458 return (posted);
442 } 459 }
443 460
444 int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value) 461 int
445 { 462 SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
446 int posted; 463 {
447 464 int posted;
448 /* Update internal joystick state */ 465
449 joystick->hats[hat] = value; 466 /* Update internal joystick state */
450 467 joystick->hats[hat] = value;
451 /* Post the event, if desired */ 468
452 posted = 0; 469 /* Post the event, if desired */
470 posted = 0;
453 #if !SDL_EVENTS_DISABLED 471 #if !SDL_EVENTS_DISABLED
454 if ( SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE ) { 472 if (SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE) {
455 SDL_Event event; 473 SDL_Event event;
456 event.jhat.type = SDL_JOYHATMOTION; 474 event.jhat.type = SDL_JOYHATMOTION;
457 event.jhat.which = joystick->index; 475 event.jhat.which = joystick->index;
458 event.jhat.hat = hat; 476 event.jhat.hat = hat;
459 event.jhat.value = value; 477 event.jhat.value = value;
460 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 478 if ((SDL_EventOK == NULL)
461 posted = 1; 479 || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
462 SDL_PushEvent(&event); 480 posted = 1;
463 } 481 SDL_PushEvent(&event);
464 } 482 }
483 }
465 #endif /* !SDL_EVENTS_DISABLED */ 484 #endif /* !SDL_EVENTS_DISABLED */
466 return(posted); 485 return (posted);
467 } 486 }
468 487
469 int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball, 488 int
470 Sint16 xrel, Sint16 yrel) 489 SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
471 { 490 Sint16 xrel, Sint16 yrel)
472 int posted; 491 {
473 492 int posted;
474 /* Update internal mouse state */ 493
475 joystick->balls[ball].dx += xrel; 494 /* Update internal mouse state */
476 joystick->balls[ball].dy += yrel; 495 joystick->balls[ball].dx += xrel;
477 496 joystick->balls[ball].dy += yrel;
478 /* Post the event, if desired */ 497
479 posted = 0; 498 /* Post the event, if desired */
499 posted = 0;
480 #if !SDL_EVENTS_DISABLED 500 #if !SDL_EVENTS_DISABLED
481 if ( SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE ) { 501 if (SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE) {
482 SDL_Event event; 502 SDL_Event event;
483 event.jball.type = SDL_JOYBALLMOTION; 503 event.jball.type = SDL_JOYBALLMOTION;
484 event.jball.which = joystick->index; 504 event.jball.which = joystick->index;
485 event.jball.ball = ball; 505 event.jball.ball = ball;
486 event.jball.xrel = xrel; 506 event.jball.xrel = xrel;
487 event.jball.yrel = yrel; 507 event.jball.yrel = yrel;
488 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 508 if ((SDL_EventOK == NULL)
489 posted = 1; 509 || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
490 SDL_PushEvent(&event); 510 posted = 1;
491 } 511 SDL_PushEvent(&event);
492 } 512 }
513 }
493 #endif /* !SDL_EVENTS_DISABLED */ 514 #endif /* !SDL_EVENTS_DISABLED */
494 return(posted); 515 return (posted);
495 } 516 }
496 517
497 int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state) 518 int
498 { 519 SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
499 int posted; 520 {
521 int posted;
500 #if !SDL_EVENTS_DISABLED 522 #if !SDL_EVENTS_DISABLED
501 SDL_Event event; 523 SDL_Event event;
502 524
503 switch ( state ) { 525 switch (state) {
504 case SDL_PRESSED: 526 case SDL_PRESSED:
505 event.type = SDL_JOYBUTTONDOWN; 527 event.type = SDL_JOYBUTTONDOWN;
506 break; 528 break;
507 case SDL_RELEASED: 529 case SDL_RELEASED:
508 event.type = SDL_JOYBUTTONUP; 530 event.type = SDL_JOYBUTTONUP;
509 break; 531 break;
510 default: 532 default:
511 /* Invalid state -- bail */ 533 /* Invalid state -- bail */
512 return(0); 534 return (0);
513 } 535 }
514 #endif /* !SDL_EVENTS_DISABLED */ 536 #endif /* !SDL_EVENTS_DISABLED */
515 537
516 /* Update internal joystick state */ 538 /* Update internal joystick state */
517 joystick->buttons[button] = state; 539 joystick->buttons[button] = state;
518 540
519 /* Post the event, if desired */ 541 /* Post the event, if desired */
520 posted = 0; 542 posted = 0;
521 #if !SDL_EVENTS_DISABLED 543 #if !SDL_EVENTS_DISABLED
522 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { 544 if (SDL_ProcessEvents[event.type] == SDL_ENABLE) {
523 event.jbutton.which = joystick->index; 545 event.jbutton.which = joystick->index;
524 event.jbutton.button = button; 546 event.jbutton.button = button;
525 event.jbutton.state = state; 547 event.jbutton.state = state;
526 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { 548 if ((SDL_EventOK == NULL)
527 posted = 1; 549 || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
528 SDL_PushEvent(&event); 550 posted = 1;
529 } 551 SDL_PushEvent(&event);
530 } 552 }
553 }
531 #endif /* !SDL_EVENTS_DISABLED */ 554 #endif /* !SDL_EVENTS_DISABLED */
532 return(posted); 555 return (posted);
533 } 556 }
534 557
535 void SDL_JoystickUpdate(void) 558 void
536 { 559 SDL_JoystickUpdate(void)
537 int i; 560 {
538 561 int i;
539 for ( i=0; SDL_joysticks[i]; ++i ) { 562
540 SDL_SYS_JoystickUpdate(SDL_joysticks[i]); 563 for (i = 0; SDL_joysticks[i]; ++i) {
541 } 564 SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
542 } 565 }
543 566 }
544 int SDL_JoystickEventState(int state) 567
568 int
569 SDL_JoystickEventState(int state)
545 { 570 {
546 #if SDL_EVENTS_DISABLED 571 #if SDL_EVENTS_DISABLED
547 return SDL_IGNORE; 572 return SDL_IGNORE;
548 #else 573 #else
549 const Uint8 event_list[] = { 574 const Uint8 event_list[] = {
550 SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION, 575 SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
551 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, 576 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
552 }; 577 };
553 unsigned int i; 578 unsigned int i;
554 579
555 switch (state) { 580 switch (state) {
556 case SDL_QUERY: 581 case SDL_QUERY:
557 state = SDL_IGNORE; 582 state = SDL_IGNORE;
558 for ( i=0; i<SDL_arraysize(event_list); ++i ) { 583 for (i = 0; i < SDL_arraysize(event_list); ++i) {
559 state = SDL_EventState(event_list[i],SDL_QUERY); 584 state = SDL_EventState(event_list[i], SDL_QUERY);
560 if ( state == SDL_ENABLE ) { 585 if (state == SDL_ENABLE) {
561 break; 586 break;
562 } 587 }
563 } 588 }
564 break; 589 break;
565 default: 590 default:
566 for ( i=0; i<SDL_arraysize(event_list); ++i ) { 591 for (i = 0; i < SDL_arraysize(event_list); ++i) {
567 SDL_EventState(event_list[i], state); 592 SDL_EventState(event_list[i], state);
568 } 593 }
569 break; 594 break;
570 } 595 }
571 return(state); 596 return (state);
572 #endif /* SDL_EVENTS_DISABLED */ 597 #endif /* SDL_EVENTS_DISABLED */
573 } 598 }
599
600 /* vi: set ts=4 sw=4 expandtab: */