Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Organized joystick hotplug code a bit.

Cleaned up names, return types, etc.
  • Loading branch information
slouken committed Nov 27, 2012
1 parent 7027394 commit a9a03ca67d6519417afe50a97341cbea3cb714b2
@@ -62,8 +62,14 @@ extern "C" {
struct _SDL_Joystick;
typedef struct _SDL_Joystick SDL_Joystick;

/* A structure that encodes the stable unique id for a joystick device */
typedef struct {
Uint8 data[16];
} JoystickGUID;

typedef int SDL_JoystickID;



/* Function prototypes */
/**
* Count the number of joysticks attached to the system right now
@@ -93,12 +99,6 @@ extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index);
*/
extern DECLSPEC const char *SDLCALL SDL_JoystickName(SDL_Joystick * joystick);

/* A structure that encodes the stable unique id for a joystick device */
typedef struct
{
Uint8 data[16];
} JoystickGUID;

/**
* Return the GUID for the joystick at this index
*/
@@ -119,14 +119,13 @@ extern DECLSPEC char *SDLCALL SDL_JoystickGetGUIDString(JoystickGUID guid);
*/
extern DECLSPEC JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID);


/**
* Returns 1 if the joystick has been opened and currently connected, or 0 if it has not.
* Returns SDL_TRUE if the joystick has been opened and currently connected, or SDL_FALSE if it has not.
*/
extern DECLSPEC int SDLCALL SDL_JoystickGetAttached(SDL_Joystick * joystick);
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick * joystick);

/**
* Get the device index of an opened joystick.
* Get the instance ID of an opened joystick.
*/
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);

@@ -63,7 +63,7 @@ SDL_JoystickNameForIndex(int device_index)
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
return (NULL);
}
return (SDL_SYS_JoystickNameForIndex(device_index));
return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
}

/*
@@ -112,7 +112,7 @@ SDL_JoystickOpen(int device_index)
return NULL;
}

joystickname = SDL_SYS_JoystickNameForIndex( device_index );
joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
if ( joystickname )
joystick->name = SDL_strdup( joystickname );
else
@@ -332,11 +332,11 @@ SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
* Return if the joystick in question is currently attached to the system,
* \return 0 if not plugged in, 1 if still present.
*/
int
SDL_JoystickGetAttached( SDL_Joystick * joystick )
SDL_bool
SDL_JoystickGetAttached(SDL_Joystick * joystick)
{
if (!SDL_PrivateJoystickValid(joystick)) {
return (0);
return SDL_FALSE;
}

return SDL_SYS_JoystickAttached(joystick);
@@ -346,7 +346,7 @@ SDL_JoystickGetAttached( SDL_Joystick * joystick )
* Get the instance id for this opened joystick
*/
SDL_JoystickID
SDL_JoystickInstanceID( SDL_Joystick * joystick )
SDL_JoystickInstanceID(SDL_Joystick * joystick)
{
if (!SDL_PrivateJoystickValid(joystick)) {
return (-1);
@@ -645,13 +645,13 @@ SDL_JoystickEventState(int state)
}

/* return 1 if you want to run the joystick update loop this frame, used by hotplug support */
int
SDL_bool
SDL_PrivateJoystickNeedsPolling()
{
if ( SDL_SYS_JoystickNeedsPolling() )
{
// sys layer needs us to think
return 1;
return SDL_TRUE;
}
else
{
@@ -662,16 +662,15 @@ SDL_PrivateJoystickNeedsPolling()


/* return the guid for this index */
JoystickGUID SDL_JoystickGetDeviceGUID( int device_index )
JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
{
return SDL_SYS_PrivateJoystickGetDeviceGUID( device_index );
return SDL_SYS_JoystickGetDeviceGUID( device_index );
}

/* return the guid for this opened device */
JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
{
return SDL_SYS_PrivateJoystickGetGUID( joystick );

return SDL_SYS_JoystickGetGUID( joystick );
}

/* convert the guid to a printable string */
@@ -43,7 +43,7 @@ extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick,
Uint8 button, Uint8 state);

/* Helper function to let lower sys layer tell the event system if the joystick code needs to think */
extern int SDL_PrivateJoystickNeedsPolling();
extern SDL_bool SDL_PrivateJoystickNeedsPolling();

/* Internal sanity checking functions */
extern int SDL_PrivateJoystickValid(SDL_Joystick * joystick);
@@ -63,18 +63,32 @@ struct _SDL_Joystick
*/
extern int SDL_SYS_JoystickInit(void);

/* Function to return the number of joystick devices plugged in right now */
extern int SDL_SYS_NumJoysticks();

/* Function to cause any queued joystick insertions to be processed */
extern void SDL_SYS_JoystickDetect();

/* Function to determine if the joystick loop needs to run right now */
extern SDL_bool SDL_SYS_JoystickNeedsPolling();

/* Function to get the device-dependent name of a joystick */
extern const char *SDL_SYS_JoystickNameForIndex(int index);
extern const char *SDL_SYS_JoystickNameForDeviceIndex(int device_index);

/* Function to get the current instance id of the joystick located at device_index */
extern SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex( int device_index );
extern SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index);

/* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick.
This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error.
*/
extern int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index );
extern int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index);

/* Function to query if the joystick is currently attached
* It returns 1 if attached, 0 otherwise.
*/
extern SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick * joystick);

/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
@@ -89,32 +103,15 @@ extern void SDL_SYS_JoystickClose(SDL_Joystick * joystick);
/* Function to perform any system-specific joystick related cleanup */
extern void SDL_SYS_JoystickQuit(void);

/* Function to query if the joystick is currently attached
* It returns 1 if attached, 0 otherwise.
*/
extern int SDL_SYS_JoystickAttached(SDL_Joystick * joystick);
/* Function to return the stable GUID for a plugged in device */
extern JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index);

/* Function to return the number of joystick devices plugged in right now*/
extern int SDL_SYS_NumJoysticks();

/* Function to cause any queued joystick insertions to be processed
*/
extern void SDL_SYS_JoystickDetect();

/* Function to determine if the joystick loop needs to run right now
*/
extern int SDL_SYS_JoystickNeedsPolling();

/* Function to return the stable GUID for a plugged in device
*/
extern JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index );

/* Function to return the stable GUID for a opened joystick
*/
extern JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick);
/* Function to return the stable GUID for a opened joystick */
extern JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);

#ifdef SDL_JOYSTICK_DINPUT
/* Function to get the current instance id of the joystick located at device_index */
extern int SDL_SYS_IsXInputDeviceIndex( int device_index );
extern SDL_bool SDL_SYS_IsXInputDeviceIndex( int device_index );
#endif

/* vi: set ts=4 sw=4 expandtab: */
@@ -46,16 +46,31 @@ SDL_SYS_JoystickInit(void)
return (1);
}

int SDL_SYS_NumJoysticks()
{
return 1;
}

void SDL_SYS_JoystickDetect()
{
}

SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}

/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickNameForIndex(int index)
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
if (index == 0) {
return accelerometerName;
} else {
SDL_SetError("No joystick available with that index");
return (NULL);
}
return accelerometerName;
}

/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
return device_index;
}

/* Function to open a joystick for use.
@@ -78,6 +93,11 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
}
}

/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
}

/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
@@ -111,43 +131,17 @@ SDL_SYS_JoystickQuit(void)
{
}

/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
{
return index;
}

/* Function to determine is this joystick is attached to the system right now */
int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return 1;
}

int SDL_SYS_NumJoysticks()
{
return 1;
}

int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}

void SDL_SYS_JoystickDetect()
{
}

JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
JoystickGUID guid;
// the GUID is just the first 16 chars of the name for now
const char *name = SDL_SYS_JoystickNameForIndex( device_index );
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
}

JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{
JoystickGUID guid;
// the GUID is just the first 16 chars of the name for now
@@ -157,6 +151,6 @@ JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
return guid;
}

#endif /* SDL_JOYSTICK_NDS */
#endif /* SDL_JOYSTICK_ANDROID */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit a9a03ca

Please sign in to comment.