Update SDL_Joystick to SDL-1.2 SVN. #undef UNICODE in win32/SDL_mmjoystick.c to avoid joystick name mangling.

git-svn-id: https://gambatte.svn.sourceforge.net/svnroot/gambatte@187 9dfb2916-2d38-0410-aef4-c5fe6c9ffc24
This commit is contained in:
sinamas 2008-10-24 04:04:12 +00:00
parent a04726f285
commit f834c0a646
4 changed files with 150 additions and 142 deletions

View File

@ -30,6 +30,9 @@
#include "SDL_event.h"
#endif
#define SDL_Lock_EventThread()
#define SDL_Unlock_EventThread()
#define SDL_PRESSED 1
#define SDL_RELEASED 0
@ -37,22 +40,6 @@ Uint8 SDL_numjoysticks = 0;
SDL_Joystick **SDL_joysticks = NULL;
static SDL_Joystick *default_joystick = NULL;
static int ValidJoystick(SDL_Joystick **joystick)
{
int valid;
if ( *joystick == NULL ) {
*joystick = default_joystick;
}
if ( *joystick == NULL ) {
SDL_SetError("Joystick hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
int SDL_JoystickInit(void)
{
int arraylen;
@ -75,21 +62,6 @@ int SDL_JoystickInit(void)
return(status);
}
void SDL_JoystickQuit(void)
{
/* Stop the event polling */
/*SDL_Lock_EventThread();*/
SDL_numjoysticks = 0;
/*SDL_Unlock_EventThread();*/
/* Quit the joystick setup */
SDL_SYS_JoystickQuit();
if ( SDL_joysticks ) {
SDL_free(SDL_joysticks);
SDL_joysticks = NULL;
}
}
/*
* Count the number of joysticks attached to the system
*/
@ -140,117 +112,68 @@ SDL_Joystick *SDL_JoystickOpen(int device_index)
/* Create and initialize the joystick */
joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick));
if ( joystick != NULL ) {
SDL_memset(joystick, 0, (sizeof *joystick));
joystick->index = device_index;
if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
SDL_free(joystick);
joystick = NULL;
} else {
if ( joystick->naxes > 0 ) {
joystick->axes = (Sint16 *)SDL_malloc
(joystick->naxes*sizeof(Sint16));
}
if ( joystick->nhats > 0 ) {
joystick->hats = (Uint8 *)SDL_malloc
(joystick->nhats*sizeof(Uint8));
}
if ( joystick->nballs > 0 ) {
joystick->balls = (struct balldelta *)SDL_malloc
(joystick->nballs*sizeof(*joystick->balls));
}
if ( joystick->nbuttons > 0 ) {
joystick->buttons = (Uint8 *)SDL_malloc
(joystick->nbuttons*sizeof(Uint8));
}
if ( ((joystick->naxes > 0) && !joystick->axes)
|| ((joystick->nhats > 0) && !joystick->hats)
|| ((joystick->nballs > 0) && !joystick->balls)
|| ((joystick->nbuttons > 0) && !joystick->buttons)) {
SDL_OutOfMemory();
SDL_JoystickClose(joystick);
joystick = NULL;
}
if ( joystick->axes ) {
SDL_memset(joystick->axes, 0,
joystick->naxes*sizeof(Sint16));
}
if ( joystick->hats ) {
SDL_memset(joystick->hats, 0,
joystick->nhats*sizeof(Uint8));
}
if ( joystick->balls ) {
SDL_memset(joystick->balls, 0,
joystick->nballs*sizeof(*joystick->balls));
}
if ( joystick->buttons ) {
SDL_memset(joystick->buttons, 0,
joystick->nbuttons*sizeof(Uint8));
}
}
}
if ( joystick ) {
/* Add joystick to list */
++joystick->ref_count;
/*SDL_Lock_EventThread();*/
for ( i=0; SDL_joysticks[i]; ++i )
/* Skip to next joystick */;
SDL_joysticks[i] = joystick;
/*SDL_Unlock_EventThread();*/
}
return(joystick);
}
/*
* Close a joystick previously opened with SDL_JoystickOpen()
*/
void SDL_JoystickClose(SDL_Joystick *joystick)
{
int i;
if ( ! ValidJoystick(&joystick) ) {
return;
if ( !joystick ) {
return(NULL);
}
/* First decrement ref count */
if ( --joystick->ref_count > 0 ) {
return;
SDL_memset(joystick, 0, (sizeof *joystick));
joystick->index = device_index;
if ( SDL_SYS_JoystickOpen(joystick) < 0 ) {
SDL_free(joystick);
return(NULL);
}
/* Lock the event queue - prevent joystick polling */
/*SDL_Lock_EventThread();*/
if ( joystick == default_joystick ) {
default_joystick = NULL;
if ( joystick->naxes > 0 ) {
joystick->axes = (Sint16 *)SDL_malloc
(joystick->naxes*sizeof(Sint16));
}
SDL_SYS_JoystickClose(joystick);
/* Remove joystick from list */
for ( i=0; SDL_joysticks[i]; ++i ) {
if ( joystick == SDL_joysticks[i] ) {
SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i+1],
(SDL_numjoysticks-i)*sizeof(joystick));
break;
}
if ( joystick->nhats > 0 ) {
joystick->hats = (Uint8 *)SDL_malloc
(joystick->nhats*sizeof(Uint8));
}
if ( joystick->nballs > 0 ) {
joystick->balls = (struct balldelta *)SDL_malloc
(joystick->nballs*sizeof(*joystick->balls));
}
if ( joystick->nbuttons > 0 ) {
joystick->buttons = (Uint8 *)SDL_malloc
(joystick->nbuttons*sizeof(Uint8));
}
if ( ((joystick->naxes > 0) && !joystick->axes)
|| ((joystick->nhats > 0) && !joystick->hats)
|| ((joystick->nballs > 0) && !joystick->balls)
|| ((joystick->nbuttons > 0) && !joystick->buttons)) {
SDL_OutOfMemory();
SDL_JoystickClose(joystick);
return(NULL);
}
/* Let the event thread keep running */
/*SDL_Unlock_EventThread();*/
/* Free the data associated with this joystick */
if ( joystick->axes ) {
SDL_free(joystick->axes);
SDL_memset(joystick->axes, 0,
joystick->naxes*sizeof(Sint16));
}
if ( joystick->hats ) {
SDL_free(joystick->hats);
SDL_memset(joystick->hats, 0,
joystick->nhats*sizeof(Uint8));
}
if ( joystick->balls ) {
SDL_free(joystick->balls);
SDL_memset(joystick->balls, 0,
joystick->nballs*sizeof(*joystick->balls));
}
if ( joystick->buttons ) {
SDL_free(joystick->buttons);
SDL_memset(joystick->buttons, 0,
joystick->nbuttons*sizeof(Uint8));
}
SDL_free(joystick);
/* Add joystick to list */
++joystick->ref_count;
SDL_Lock_EventThread();
for ( i=0; SDL_joysticks[i]; ++i )
/* Skip to next joystick */ ;
SDL_joysticks[i] = joystick;
SDL_Unlock_EventThread();
return(joystick);
}
/*
@ -270,6 +193,22 @@ int SDL_JoystickOpened(int device_index)
return(opened);
}
static int ValidJoystick(SDL_Joystick **joystick)
{
int valid;
if ( *joystick == NULL ) {
*joystick = default_joystick;
}
if ( *joystick == NULL ) {
SDL_SetError("Joystick hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
/*
* Get the device index of an opened joystick.
*/
@ -325,15 +264,6 @@ int SDL_JoystickNumButtons(SDL_Joystick *joystick)
return(joystick->nbuttons);
}
void SDL_JoystickUpdate(void)
{
int i;
for ( i=0; SDL_joysticks[i]; ++i ) {
SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
}
}
/*
* Get the current state of an axis control on a joystick
*/
@ -419,6 +349,74 @@ Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
return(state);
}
/*
* Close a joystick previously opened with SDL_JoystickOpen()
*/
void SDL_JoystickClose(SDL_Joystick *joystick)
{
int i;
if ( ! ValidJoystick(&joystick) ) {
return;
}
/* First decrement ref count */
if ( --joystick->ref_count > 0 ) {
return;
}
/* Lock the event queue - prevent joystick polling */
SDL_Lock_EventThread();
if ( joystick == default_joystick ) {
default_joystick = NULL;
}
SDL_SYS_JoystickClose(joystick);
/* Remove joystick from list */
for ( i=0; SDL_joysticks[i]; ++i ) {
if ( joystick == SDL_joysticks[i] ) {
SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i+1],
(SDL_numjoysticks-i)*sizeof(joystick));
break;
}
}
/* Let the event thread keep running */
SDL_Unlock_EventThread();
/* Free the data associated with this joystick */
if ( joystick->axes ) {
SDL_free(joystick->axes);
}
if ( joystick->hats ) {
SDL_free(joystick->hats);
}
if ( joystick->balls ) {
SDL_free(joystick->balls);
}
if ( joystick->buttons ) {
SDL_free(joystick->buttons);
}
SDL_free(joystick);
}
void SDL_JoystickQuit(void)
{
/* Stop the event polling */
SDL_Lock_EventThread();
SDL_numjoysticks = 0;
SDL_Unlock_EventThread();
/* Quit the joystick setup */
SDL_SYS_JoystickQuit();
if ( SDL_joysticks ) {
SDL_free(SDL_joysticks);
SDL_joysticks = NULL;
}
}
/* These are global for SDL_sysjoystick.c and SDL_events.c */
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
@ -515,3 +513,12 @@ int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
#endif /* !SDL_EVENTS_DISABLED */
return(posted);
}
void SDL_JoystickUpdate(void)
{
int i;
for ( i=0; SDL_joysticks[i]; ++i ) {
SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
}
}

View File

@ -721,7 +721,7 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
recDevice *device = joystick->hwdata;
recElement *element;
SInt32 value;
SInt32 value, range;
int i;
if (device->removed) /* device was unplugged; ignore it. */
@ -774,10 +774,11 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
Uint8 pos = 0;
value = HIDGetElementValue(device, element);
if (element->max == 3) /* 4 position hatswitch - scale up value */
range = (element->max - element->min + 1);
value = HIDGetElementValue(device, element) - element->min;
if (range == 4) /* 4 position hatswitch - scale up value */
value *= 2;
else if (element->max != 7) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
else if (range != 8) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
value = -1;
switch(value)
{

View File

@ -471,8 +471,7 @@ int SDL_SYS_JoystickInit(void)
dev_nums[numjoysticks] = sb.st_rdev;
++numjoysticks;
}
} else
break;
}
}
#if SDL_INPUT_LINUXEV

View File

@ -19,6 +19,7 @@
Sam Lantinga
slouken@libsdl.org
*/
#undef UNICODE
#include "SDL_config.h"
/* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */