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