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