2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This is the joystick API for Simple DirectMedia Layer */
26 #include "SDL_events.h"
27 #include "SDL_sysjoystick.h"
28 #include "SDL_joystick_c.h"
29 #if !SDL_EVENTS_DISABLED
30 #include "../events/SDL_events_c.h"
33 /* This is used for Quake III Arena */
34 #if SDL_EVENTS_DISABLED
35 #define SDL_Lock_EventThread()
36 #define SDL_Unlock_EventThread()
39 Uint8 SDL_numjoysticks = 0;
40 SDL_Joystick **SDL_joysticks = NULL;
41 static SDL_Joystick *default_joystick = NULL;
43 int SDL_JoystickInit(void)
49 status = SDL_SYS_JoystickInit();
51 arraylen = (status+1)*sizeof(*SDL_joysticks);
52 SDL_joysticks = (SDL_Joystick **)SDL_malloc(arraylen);
53 if ( SDL_joysticks == NULL ) {
56 SDL_memset(SDL_joysticks, 0, arraylen);
57 SDL_numjoysticks = status;
61 default_joystick = NULL;
66 * Count the number of joysticks attached to the system
68 int SDL_NumJoysticks(void)
70 return SDL_numjoysticks;
74 * Get the implementation dependent name of a joystick
76 const char *SDL_JoystickName(int device_index)
78 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) {
79 SDL_SetError("There are %d joysticks available",
83 return(SDL_SYS_JoystickName(device_index));
87 * 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
89 * identify this joystick in future joystick events.
91 * This function returns a joystick identifier, or NULL if an error occurred.
93 SDL_Joystick *SDL_JoystickOpen(int device_index)
96 SDL_Joystick *joystick;
98 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) {
99 SDL_SetError("There are %d joysticks available",
104 /* If the joystick is already open, return it */
105 for ( i=0; SDL_joysticks[i]; ++i ) {
106 if ( device_index == SDL_joysticks[i]->index ) {
107 joystick = SDL_joysticks[i];
108 ++joystick->ref_count;
113 /* Create and initialize the joystick */
114 joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick));
119 SDL_memset(joystick, 0, (sizeof *joystick));
120 joystick->index = device_index;
121 if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
126 if ( joystick->naxes > 0 ) {
127 joystick->axes = (Sint16 *)SDL_malloc
128 (joystick->naxes*sizeof(Sint16));
130 if ( joystick->nhats > 0 ) {
131 joystick->hats = (Uint8 *)SDL_malloc
132 (joystick->nhats*sizeof(Uint8));
134 if ( joystick->nballs > 0 ) {
135 joystick->balls = (struct balldelta *)SDL_malloc
136 (joystick->nballs*sizeof(*joystick->balls));
138 if ( joystick->nbuttons > 0 ) {
139 joystick->buttons = (Uint8 *)SDL_malloc
140 (joystick->nbuttons*sizeof(Uint8));
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)) {
147 SDL_JoystickClose(joystick);
151 if ( joystick->axes ) {
152 SDL_memset(joystick->axes, 0,
153 joystick->naxes*sizeof(Sint16));
155 if ( joystick->hats ) {
156 SDL_memset(joystick->hats, 0,
157 joystick->nhats*sizeof(Uint8));
159 if ( joystick->balls ) {
160 SDL_memset(joystick->balls, 0,
161 joystick->nballs*sizeof(*joystick->balls));
163 if ( joystick->buttons ) {
164 SDL_memset(joystick->buttons, 0,
165 joystick->nbuttons*sizeof(Uint8));
168 /* Add joystick to list */
169 ++joystick->ref_count;
170 SDL_Lock_EventThread();
171 for ( i=0; SDL_joysticks[i]; ++i )
172 /* Skip to next joystick */ ;
173 SDL_joysticks[i] = joystick;
174 SDL_Unlock_EventThread();
180 * Returns 1 if the joystick has been opened, or 0 if it has not.
182 int SDL_JoystickOpened(int device_index)
187 for ( i=0; SDL_joysticks[i]; ++i ) {
188 if ( SDL_joysticks[i]->index == (Uint8)device_index ) {
196 static int ValidJoystick(SDL_Joystick **joystick)
200 if ( *joystick == NULL ) {
201 *joystick = default_joystick;
203 if ( *joystick == NULL ) {
204 SDL_SetError("Joystick hasn't been opened yet");
213 * Get the device index of an opened joystick.
215 int SDL_JoystickIndex(SDL_Joystick *joystick)
217 if ( ! ValidJoystick(&joystick) ) {
220 return(joystick->index);
224 * Get the number of multi-dimensional axis controls on a joystick
226 int SDL_JoystickNumAxes(SDL_Joystick *joystick)
228 if ( ! ValidJoystick(&joystick) ) {
231 return(joystick->naxes);
235 * Get the number of hats on a joystick
237 int SDL_JoystickNumHats(SDL_Joystick *joystick)
239 if ( ! ValidJoystick(&joystick) ) {
242 return(joystick->nhats);
246 * Get the number of trackballs on a joystick
248 int SDL_JoystickNumBalls(SDL_Joystick *joystick)
250 if ( ! ValidJoystick(&joystick) ) {
253 return(joystick->nballs);
257 * Get the number of buttons on a joystick
259 int SDL_JoystickNumButtons(SDL_Joystick *joystick)
261 if ( ! ValidJoystick(&joystick) ) {
264 return(joystick->nbuttons);
268 * Get the current state of an axis control on a joystick
270 Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
274 if ( ! ValidJoystick(&joystick) ) {
277 if ( axis < joystick->naxes ) {
278 state = joystick->axes[axis];
280 SDL_SetError("Joystick only has %d axes", joystick->naxes);
287 * Get the current state of a hat on a joystick
289 Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat)
293 if ( ! ValidJoystick(&joystick) ) {
296 if ( hat < joystick->nhats ) {
297 state = joystick->hats[hat];
299 SDL_SetError("Joystick only has %d hats", joystick->nhats);
306 * Get the ball axis change since the last poll
308 int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)
312 if ( ! ValidJoystick(&joystick) ) {
317 if ( ball < joystick->nballs ) {
319 *dx = joystick->balls[ball].dx;
322 *dy = joystick->balls[ball].dy;
324 joystick->balls[ball].dx = 0;
325 joystick->balls[ball].dy = 0;
327 SDL_SetError("Joystick only has %d balls", joystick->nballs);
334 * Get the current state of a button on a joystick
336 Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
340 if ( ! ValidJoystick(&joystick) ) {
343 if ( button < joystick->nbuttons ) {
344 state = joystick->buttons[button];
346 SDL_SetError("Joystick only has %d buttons",joystick->nbuttons);
353 * Close a joystick previously opened with SDL_JoystickOpen()
355 void SDL_JoystickClose(SDL_Joystick *joystick)
359 if ( ! ValidJoystick(&joystick) ) {
363 /* First decrement ref count */
364 if ( --joystick->ref_count > 0 ) {
368 /* Lock the event queue - prevent joystick polling */
369 SDL_Lock_EventThread();
371 if ( joystick == default_joystick ) {
372 default_joystick = NULL;
374 SDL_SYS_JoystickClose(joystick);
376 /* Remove joystick from list */
377 for ( i=0; SDL_joysticks[i]; ++i ) {
378 if ( joystick == SDL_joysticks[i] ) {
379 SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i+1],
380 (SDL_numjoysticks-i)*sizeof(joystick));
385 /* Let the event thread keep running */
386 SDL_Unlock_EventThread();
388 /* Free the data associated with this joystick */
389 if ( joystick->axes ) {
390 SDL_free(joystick->axes);
392 if ( joystick->hats ) {
393 SDL_free(joystick->hats);
395 if ( joystick->balls ) {
396 SDL_free(joystick->balls);
398 if ( joystick->buttons ) {
399 SDL_free(joystick->buttons);
404 void SDL_JoystickQuit(void)
406 const int numsticks = SDL_numjoysticks;
409 /* Stop the event polling */
410 SDL_Lock_EventThread();
411 SDL_numjoysticks = 0;
412 SDL_Unlock_EventThread();
414 if (SDL_joysticks != NULL) {
415 for (i = 0; i < numsticks; i++) {
416 SDL_Joystick *stick = SDL_joysticks[i];
417 if (stick && (stick->ref_count >= 1)) {
418 stick->ref_count = 1;
419 SDL_JoystickClose(stick);
424 /* Quit the joystick setup */
425 SDL_SYS_JoystickQuit();
426 if ( SDL_joysticks ) {
427 SDL_free(SDL_joysticks);
428 SDL_joysticks = NULL;
433 /* These are global for SDL_sysjoystick.c and SDL_events.c */
435 int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
439 /* Update internal joystick state */
440 joystick->axes[axis] = value;
442 /* Post the event, if desired */
444 #if !SDL_EVENTS_DISABLED
445 if ( SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE ) {
447 event.type = SDL_JOYAXISMOTION;
448 event.jaxis.which = joystick->index;
449 event.jaxis.axis = axis;
450 event.jaxis.value = value;
451 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
453 SDL_PushEvent(&event);
456 #endif /* !SDL_EVENTS_DISABLED */
460 int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
464 /* Update internal joystick state */
465 joystick->hats[hat] = value;
467 /* Post the event, if desired */
469 #if !SDL_EVENTS_DISABLED
470 if ( SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE ) {
472 event.jhat.type = SDL_JOYHATMOTION;
473 event.jhat.which = joystick->index;
474 event.jhat.hat = hat;
475 event.jhat.value = value;
476 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
478 SDL_PushEvent(&event);
481 #endif /* !SDL_EVENTS_DISABLED */
485 int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball,
486 Sint16 xrel, Sint16 yrel)
490 /* Update internal mouse state */
491 joystick->balls[ball].dx += xrel;
492 joystick->balls[ball].dy += yrel;
494 /* Post the event, if desired */
496 #if !SDL_EVENTS_DISABLED
497 if ( SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE ) {
499 event.jball.type = SDL_JOYBALLMOTION;
500 event.jball.which = joystick->index;
501 event.jball.ball = ball;
502 event.jball.xrel = xrel;
503 event.jball.yrel = yrel;
504 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
506 SDL_PushEvent(&event);
509 #endif /* !SDL_EVENTS_DISABLED */
513 int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
516 #if !SDL_EVENTS_DISABLED
521 event.type = SDL_JOYBUTTONDOWN;
524 event.type = SDL_JOYBUTTONUP;
527 /* Invalid state -- bail */
530 #endif /* !SDL_EVENTS_DISABLED */
532 /* Update internal joystick state */
533 joystick->buttons[button] = state;
535 /* Post the event, if desired */
537 #if !SDL_EVENTS_DISABLED
538 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
539 event.jbutton.which = joystick->index;
540 event.jbutton.button = button;
541 event.jbutton.state = state;
542 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
544 SDL_PushEvent(&event);
547 #endif /* !SDL_EVENTS_DISABLED */
551 void SDL_JoystickUpdate(void)
555 for ( i=0; SDL_joysticks[i]; ++i ) {
556 SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
560 int SDL_JoystickEventState(int state)
562 #if SDL_EVENTS_DISABLED
565 const Uint8 event_list[] = {
566 SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
567 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
574 for ( i=0; i<SDL_arraysize(event_list); ++i ) {
575 state = SDL_EventState(event_list[i],SDL_QUERY);
576 if ( state == SDL_ENABLE ) {
582 for ( i=0; i<SDL_arraysize(event_list); ++i ) {
583 SDL_EventState(event_list[i], state);
588 #endif /* SDL_EVENTS_DISABLED */