Changes from Alfred:

- rename JoystickGUID -> SDL_JoystickGUID
- change SDL_JoystickGetGUIDString to take the string as an arg, rather than doing a malloc
This commit is contained in:
Sam Lantinga 2012-12-11 11:54:32 -08:00
parent 6b3312c807
commit 89ef9e3168
9 changed files with 36 additions and 40 deletions

View file

@ -65,7 +65,7 @@ typedef struct _SDL_Joystick SDL_Joystick;
/* A structure that encodes the stable unique id for a joystick device */ /* A structure that encodes the stable unique id for a joystick device */
typedef struct { typedef struct {
Uint8 data[16]; Uint8 data[16];
} JoystickGUID; } SDL_JoystickGUID;
typedef int SDL_JoystickID; typedef int SDL_JoystickID;
@ -102,22 +102,22 @@ extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);
/** /**
* Return the GUID for the joystick at this index * Return the GUID for the joystick at this index
*/ */
extern DECLSPEC JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index); extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index);
/** /**
* Return the GUID for this opened joystick * Return the GUID for this opened joystick
*/ */
extern DECLSPEC JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick * joystick); extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUID(SDL_Joystick * joystick);
/** /**
* Return a string representation for this guid. You are responsible for freeing memory from this call * Return a string representation for this guid. You are responsible for freeing memory from this call
*/ */
extern DECLSPEC char *SDLCALL SDL_JoystickGetGUIDString(JoystickGUID guid); extern DECLSPEC void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID);
/** /**
* convert a string into a joystick formatted guid * convert a string into a joystick formatted guid
*/ */
extern DECLSPEC JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID); extern DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);
/** /**
* Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not. * Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.

View file

@ -48,7 +48,7 @@ struct _SDL_HatAsButton
/* our in memory mapping db between joystick objects and controller mappings*/ /* our in memory mapping db between joystick objects and controller mappings*/
struct _SDL_ControllerMapping struct _SDL_ControllerMapping
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
const char *name; const char *name;
// mapping of axis/button id to controller version // mapping of axis/button id to controller version
@ -71,7 +71,7 @@ struct _SDL_ControllerMapping
/* our hard coded list of mapping support */ /* our hard coded list of mapping support */
typedef struct _ControllerMapping_t typedef struct _ControllerMapping_t
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
char *name; char *name;
const char *mapping; const char *mapping;
struct _ControllerMapping_t *next; struct _ControllerMapping_t *next;
@ -458,7 +458,7 @@ SDL_PrivateGameControllerParseControllerConfigString( struct _SDL_ControllerMapp
/* /*
* Make a new button mapping struct * Make a new button mapping struct
*/ */
void SDL_PrivateLoadButtonMapping( struct _SDL_ControllerMapping *pMapping, JoystickGUID guid, const char *pchName, const char *pchMapping ) void SDL_PrivateLoadButtonMapping( struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping )
{ {
int j; int j;
@ -673,7 +673,7 @@ SDL_GameControllerNameForIndex(int device_index)
} }
else else
{ {
JoystickGUID jGUID = SDL_JoystickGetDeviceGUID( device_index ); SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID( device_index );
pSupportedController = s_pSupportedControllers; pSupportedController = s_pSupportedControllers;
while ( pSupportedController ) while ( pSupportedController )
{ {
@ -700,10 +700,11 @@ int SDL_IsGameController(int device_index)
} }
else else
{ {
JoystickGUID jGUID = SDL_JoystickGetDeviceGUID( device_index ); SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID( device_index );
pSupportedController = s_pSupportedControllers; pSupportedController = s_pSupportedControllers;
// debug code to help get the guid string for a new joystick // debug code to help get the guid string for a new joystick
/*const char *pchGUID = SDL_JoystickGetGUIDString( jGUID ); /* char szGUID[33];
SDL_JoystickGetGUIDString( jGUID, szGUID, sizeof(szGUID) );
printf( "%s\n", pchGUID ); printf( "%s\n", pchGUID );
SDL_free( pchGUID );*/ SDL_free( pchGUID );*/
while ( pSupportedController ) while ( pSupportedController )
@ -759,7 +760,7 @@ SDL_GameControllerOpen(int device_index)
pSupportedController = SDL_PrivateGetControllerMapping(device_index); pSupportedController = SDL_PrivateGetControllerMapping(device_index);
if ( !pSupportedController ) if ( !pSupportedController )
{ {
JoystickGUID jGUID; SDL_JoystickGUID jGUID;
jGUID = SDL_JoystickGetDeviceGUID( device_index ); jGUID = SDL_JoystickGetDeviceGUID( device_index );
pSupportedController = s_pSupportedControllers; pSupportedController = s_pSupportedControllers;

View file

@ -683,34 +683,28 @@ SDL_PrivateJoystickNeedsPolling()
/* return the guid for this index */ /* return the guid for this index */
JoystickGUID SDL_JoystickGetDeviceGUID(int device_index) SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
{ {
return SDL_SYS_JoystickGetDeviceGUID( device_index ); return SDL_SYS_JoystickGetDeviceGUID( device_index );
} }
/* return the guid for this opened device */ /* return the guid for this opened device */
JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick) SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
{ {
return SDL_SYS_JoystickGetGUID( joystick ); return SDL_SYS_JoystickGetGUID( joystick );
} }
/* convert the guid to a printable string */ /* convert the guid to a printable string */
char *SDL_JoystickGetGUIDString(JoystickGUID guid) void SDL_JoystickGetGUIDString( SDL_JoystickGUID guid, char *pszGUID, int cbGUID )
{ {
static const char k_rgchHexToASCII[] = "0123456789abcdef"; static const char k_rgchHexToASCII[] = "0123456789abcdef";
char *pchOut = NULL; char *pchOut = NULL;
char *pchString = NULL; char *pchString = NULL;
int i; int i;
pchString = SDL_malloc(33); // 16 bytes
if ( !pchString )
{
SDL_OutOfMemory();
return NULL;
}
pchOut = pchString; pchOut = pszGUID;
for ( i = 0; i < sizeof(guid); i++ ) for ( i = 0; i < sizeof(guid) && i < (cbGUID-1); i++ )
{ {
// each input byte writes 2 ascii chars, and might write a null byte. // each input byte writes 2 ascii chars, and might write a null byte.
// If we don't have room for next input byte, stop // If we don't have room for next input byte, stop
@ -720,7 +714,6 @@ char *SDL_JoystickGetGUIDString(JoystickGUID guid)
*pchOut++ = k_rgchHexToASCII[ c & 0x0F ]; *pchOut++ = k_rgchHexToASCII[ c & 0x0F ];
} }
*pchOut = '\0'; *pchOut = '\0';
return pchString;
} }
@ -756,9 +749,9 @@ static unsigned char nibble( char c )
/* convert the string version of a joystick guid to the struct */ /* convert the string version of a joystick guid to the struct */
JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID) SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
int maxoutputbytes= sizeof(guid); int maxoutputbytes= sizeof(guid);
int len = SDL_strlen( pchGUID ); int len = SDL_strlen( pchGUID );
Uint8 *p; Uint8 *p;

View file

@ -104,10 +104,10 @@ extern void SDL_SYS_JoystickClose(SDL_Joystick * joystick);
extern void SDL_SYS_JoystickQuit(void); extern void SDL_SYS_JoystickQuit(void);
/* Function to return the stable GUID for a plugged in device */ /* Function to return the stable GUID for a plugged in device */
extern JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index); extern SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index);
/* Function to return the stable GUID for a opened joystick */ /* Function to return the stable GUID for a opened joystick */
extern JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick); extern SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);
#ifdef SDL_JOYSTICK_DINPUT #ifdef SDL_JOYSTICK_DINPUT
/* Function to get the current instance id of the joystick located at device_index */ /* Function to get the current instance id of the joystick located at device_index */

View file

@ -1083,7 +1083,7 @@ SDL_SYS_JoystickQuit(void)
} }
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
recDevice *device = gpDeviceList; recDevice *device = gpDeviceList;
int index; int index;
@ -1094,7 +1094,7 @@ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
return device->guid; return device->guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick) SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
{ {
return joystick->hwdata->guid; return joystick->hwdata->guid;
} }

View file

@ -953,13 +953,15 @@ SDL_SYS_JoystickQuit(void)
#endif #endif
} }
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid;
return JoystickByDevIndex(device_index)->guid; return JoystickByDevIndex(device_index)->guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick) SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid;
return joystick->hwdata->guid; return joystick->hwdata->guid;
} }

View file

@ -79,7 +79,7 @@ extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
LPUNKNOWN punkOuter); LPUNKNOWN punkOuter);
struct JoyStick_DeviceData_ struct JoyStick_DeviceData_
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
DIDEVICEINSTANCE dxdevice; DIDEVICEINSTANCE dxdevice;
char *joystickname; char *joystickname;
Uint8 send_add_event; Uint8 send_add_event;
@ -1676,7 +1676,7 @@ SDL_SYS_JoystickQuit(void)
/* return the stable device guid for this device index */ /* return the stable device guid for this device index */
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
JoyStick_DeviceData *device = SYS_Joystick; JoyStick_DeviceData *device = SYS_Joystick;
int index; int index;
@ -1687,7 +1687,7 @@ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
return device->guid; return device->guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick) SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
return joystick->hwdata->guid; return joystick->hwdata->guid;
} }

View file

@ -87,7 +87,7 @@ struct joystick_hwdata
LPDIRECTINPUTDEVICE8 InputDevice; LPDIRECTINPUTDEVICE8 InputDevice;
DIDEVCAPS Capabilities; DIDEVCAPS Capabilities;
int buffered; int buffered;
JoystickGUID guid; SDL_JoystickGUID guid;
input_t Inputs[MAX_INPUTS]; input_t Inputs[MAX_INPUTS];
int NumInputs; int NumInputs;

View file

@ -405,9 +405,9 @@ SDL_SYS_JoystickQuit(void)
} }
} }
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
// the GUID is just the first 16 chars of the name for now // the GUID is just the first 16 chars of the name for now
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
@ -415,9 +415,9 @@ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
return guid; return guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick) SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; SDL_JoystickGUID guid;
// the GUID is just the first 16 chars of the name for now // the GUID is just the first 16 chars of the name for now
const char *name = joystick->name; const char *name = joystick->name;
SDL_zero( guid ); SDL_zero( guid );