Organized joystick hotplug code a bit.

Cleaned up names, return types, etc.
This commit is contained in:
Sam Lantinga 2012-11-27 00:58:12 -08:00
parent 1898cc34ab
commit 03e08a6a79
14 changed files with 616 additions and 655 deletions

View file

@ -62,8 +62,14 @@ extern "C" {
struct _SDL_Joystick; struct _SDL_Joystick;
typedef struct _SDL_Joystick 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; typedef int SDL_JoystickID;
/* Function prototypes */ /* Function prototypes */
/** /**
* Count the number of joysticks attached to the system right now * 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); 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 * 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); 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); extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);

View file

@ -63,7 +63,7 @@ SDL_JoystickNameForIndex(int device_index)
SDL_SetError("There are %d joysticks available", SDL_NumJoysticks()); SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
return (NULL); 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; return NULL;
} }
joystickname = SDL_SYS_JoystickNameForIndex( device_index ); joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
if ( joystickname ) if ( joystickname )
joystick->name = SDL_strdup( joystickname ); joystick->name = SDL_strdup( joystickname );
else 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 if the joystick in question is currently attached to the system,
* \return 0 if not plugged in, 1 if still present. * \return 0 if not plugged in, 1 if still present.
*/ */
int SDL_bool
SDL_JoystickGetAttached( SDL_Joystick * joystick ) SDL_JoystickGetAttached(SDL_Joystick * joystick)
{ {
if (!SDL_PrivateJoystickValid(joystick)) { if (!SDL_PrivateJoystickValid(joystick)) {
return (0); return SDL_FALSE;
} }
return SDL_SYS_JoystickAttached(joystick); return SDL_SYS_JoystickAttached(joystick);
@ -346,7 +346,7 @@ SDL_JoystickGetAttached( SDL_Joystick * joystick )
* Get the instance id for this opened joystick * Get the instance id for this opened joystick
*/ */
SDL_JoystickID SDL_JoystickID
SDL_JoystickInstanceID( SDL_Joystick * joystick ) SDL_JoystickInstanceID(SDL_Joystick * joystick)
{ {
if (!SDL_PrivateJoystickValid(joystick)) { if (!SDL_PrivateJoystickValid(joystick)) {
return (-1); 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 */ /* return 1 if you want to run the joystick update loop this frame, used by hotplug support */
int SDL_bool
SDL_PrivateJoystickNeedsPolling() SDL_PrivateJoystickNeedsPolling()
{ {
if ( SDL_SYS_JoystickNeedsPolling() ) if ( SDL_SYS_JoystickNeedsPolling() )
{ {
// sys layer needs us to think // sys layer needs us to think
return 1; return SDL_TRUE;
} }
else else
{ {
@ -662,16 +662,15 @@ SDL_PrivateJoystickNeedsPolling()
/* return the guid for this index */ /* 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 */ /* return the guid for this opened device */
JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick) JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
{ {
return SDL_SYS_PrivateJoystickGetGUID( joystick ); return SDL_SYS_JoystickGetGUID( joystick );
} }
/* convert the guid to a printable string */ /* convert the guid to a printable string */

View file

@ -43,7 +43,7 @@ extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick,
Uint8 button, Uint8 state); Uint8 button, Uint8 state);
/* Helper function to let lower sys layer tell the event system if the joystick code needs to think */ /* 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 */ /* Internal sanity checking functions */
extern int SDL_PrivateJoystickValid(SDL_Joystick * joystick); extern int SDL_PrivateJoystickValid(SDL_Joystick * joystick);

View file

@ -63,18 +63,32 @@ struct _SDL_Joystick
*/ */
extern int SDL_SYS_JoystickInit(void); 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 */ /* 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 */ /* 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. /* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick. 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. This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error. 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. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * 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 */ /* Function to perform any system-specific joystick related cleanup */
extern void SDL_SYS_JoystickQuit(void); extern void SDL_SYS_JoystickQuit(void);
/* Function to query if the joystick is currently attached /* Function to return the stable GUID for a plugged in device */
* It returns 1 if attached, 0 otherwise. extern JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index);
*/
extern int SDL_SYS_JoystickAttached(SDL_Joystick * joystick);
/* Function to return the number of joystick devices plugged in right now*/ /* Function to return the stable GUID for a opened joystick */
extern int SDL_SYS_NumJoysticks(); extern JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick);
/* 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);
#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 */
extern int SDL_SYS_IsXInputDeviceIndex( int device_index ); extern SDL_bool SDL_SYS_IsXInputDeviceIndex( int device_index );
#endif #endif
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -46,16 +46,31 @@ SDL_SYS_JoystickInit(void)
return (1); 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 */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
if (index == 0) { return accelerometerName;
return accelerometerName; }
} else {
SDL_SetError("No joystick available with that index"); /* Function to perform the mapping from device index to the instance id for this index */
return (NULL); SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
} {
return device_index;
} }
/* Function to open a joystick for use. /* 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. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * 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 */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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
@ -157,6 +151,6 @@ JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
return guid; return guid;
} }
#endif /* SDL_JOYSTICK_NDS */ #endif /* SDL_JOYSTICK_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -85,10 +85,30 @@ extern "C"
return (SDL_SYS_numjoysticks); return (SDL_SYS_numjoysticks);
} }
/* Function to get the device-dependent name of a joystick */ int SDL_SYS_NumJoysticks()
const char *SDL_SYS_JoystickNameForIndex(int index)
{ {
return SDL_joyname[index]; return SDL_SYS_numjoysticks;
}
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_JoystickNameForDeviceIndex(int device_index)
{
return SDL_joyname[device_index];
}
/* 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. /* Function to open a joystick for use.
@ -141,6 +161,12 @@ extern "C"
return (0); return (0);
} }
/* 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. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events * but instead should call SDL_PrivateJoystick*() to deliver events
@ -235,44 +261,17 @@ extern "C"
SDL_joyname[0] = NULL; SDL_joyname[0] = NULL;
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 SDL_SYS_numjoysticks;
}
int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -157,7 +157,7 @@ static void report_free(struct report *);
#define REP_BUF_DATA(rep) ((rep)->buf->data) #define REP_BUF_DATA(rep) ((rep)->buf->data)
#endif #endif
int SDL_SYS_numjoysticks = 0; static int SDL_SYS_numjoysticks = 0;
int int
SDL_SYS_JoystickInit(void) SDL_SYS_JoystickInit(void)
@ -200,13 +200,33 @@ SDL_SYS_JoystickInit(void)
return (SDL_SYS_numjoysticks); return (SDL_SYS_numjoysticks);
} }
const char * int SDL_SYS_NumJoysticks()
SDL_SYS_JoystickNameForIndex(int index)
{ {
if (joydevnames[index] != NULL) { return SDL_SYS_numjoysticks;
return (joydevnames[index]); }
void SDL_SYS_JoystickDetect()
{
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
if (joydevnames[device_index] != NULL) {
return (joydevnames[device_index]);
} }
return (joynames[index]); return (joynames[device_index]);
}
/* 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;
} }
static int static int
@ -404,6 +424,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
return (-1); return (-1);
} }
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
}
void void
SDL_SYS_JoystickUpdate(SDL_Joystick * joy) SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
{ {
@ -558,43 +584,17 @@ SDL_SYS_JoystickQuit(void)
return; return;
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 SDL_SYS_numjoysticks;
}
int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -63,7 +63,7 @@ static recDevice *gpDeviceList = NULL;
/* OSX reference to the notification object that tells us about device insertion/removal */ /* OSX reference to the notification object that tells us about device insertion/removal */
IONotificationPortRef notificationPort = 0; IONotificationPortRef notificationPort = 0;
/* if 1 then a device was added since the last update call */ /* if 1 then a device was added since the last update call */
Uint8 s_bDeviceAdded = 0; static SDL_bool s_bDeviceAdded = SDL_FALSE;
/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */ /* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
static int s_joystick_instance_id = -1; static int s_joystick_instance_id = -1;
@ -674,7 +674,7 @@ AddDeviceHelper( io_object_t ioHIDDeviceObject )
} }
device->send_open_event = 1; device->send_open_event = 1;
s_bDeviceAdded = 1; s_bDeviceAdded = SDL_TRUE;
/* Add device to the end of the list */ /* Add device to the end of the list */
if ( !gpDeviceList ) if ( !gpDeviceList )
@ -805,18 +805,89 @@ SDL_SYS_JoystickInit(void)
return SDL_SYS_NumJoysticks(); return SDL_SYS_NumJoysticks();
} }
/* Function to return the number of joystick devices plugged in right now */
int
SDL_SYS_NumJoysticks()
{
recDevice *device = gpDeviceList;
int nJoySticks = 0;
while ( device )
{
nJoySticks++;
device = device->pNext;
}
return nJoySticks;
}
/* Function to cause any queued joystick insertions to be processed
*/
void
SDL_SYS_JoystickDetect()
{
if ( s_bDeviceAdded )
{
recDevice *device = gpDeviceList;
s_bDeviceAdded = SDL_FALSE;
int device_index = 0;
// send notifications
while ( device )
{
if ( device->send_open_event )
{
device->send_open_event = 0;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
device_index++;
device = device->pNext;
}
}
}
SDL_bool
SDL_SYS_JoystickNeedsPolling()
{
return s_bDeviceAdded;
}
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
recDevice *device = gpDeviceList; recDevice *device = gpDeviceList;
for (; index > 0; index--) for (; device_index > 0; device_index--)
device = device->pNext; device = device->pNext;
return device->product; return device->product;
} }
/* Function to return the instance id of the joystick at device_index
*/
SDL_JoystickID
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
recDevice *device = gpDeviceList;
int index;
for (index = device_index; index > 0; index--)
device = device->pNext;
return device->instance_id;
}
/* Function to open a joystick for use. /* Function to open a joystick for use.
* The joystick to open is specified by the index field of the joystick. * 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. * This should fill the nbuttons and naxes fields of the joystick structure.
@ -842,57 +913,25 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return 0; return 0;
} }
/* Function to query if the joystick is currently attached
/* Function to return the instance id of the joystick at device_index * It returns 1 if attached, 0 otherwise.
*/ */
SDL_JoystickID SDL_bool
SDL_SYS_GetInstanceIdOfDeviceIndex( int device_index ) SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{ {
recDevice *device = gpDeviceList; recDevice *device = gpDeviceList;
int index; int index;
for (index = device_index; index > 0; index--) while ( device )
device = device->pNext;
return device->instance_id;
}
/* Function to cause any queued joystick insertions to be processed
*/
void
SDL_SYS_JoystickDetect()
{
if ( s_bDeviceAdded )
{ {
recDevice *device = gpDeviceList; if ( joystick->instance_id == device->instance_id )
s_bDeviceAdded = 0; return SDL_TRUE;
int device_index = 0;
// send notifications
while ( device )
{
if ( device->send_open_event )
{
device->send_open_event = 0;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
device_index++;
device = device->pNext;
}
}
}
device = device->pNext;
}
return SDL_FALSE;
}
/* Function to update the state of a joystick - called as a device poll. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * This function shouldn't update the joystick structure directly,
@ -1023,28 +1062,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
return; return;
} }
/* Function to query if the joystick is currently attached
* It returns 1 if attached, 0 otherwise.
*/
int
SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{
recDevice *device = gpDeviceList;
int index;
while ( device )
{
if ( joystick->instance_id == device->instance_id )
return (1);
device = device->pNext;
}
return 0;
}
/* Function to close a joystick after use */ /* Function to close a joystick after use */
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
@ -1067,29 +1084,7 @@ SDL_SYS_JoystickQuit(void)
} }
/* Function to return the number of joystick devices plugged in right now*/ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
int
SDL_SYS_NumJoysticks()
{
recDevice *device = gpDeviceList;
int nJoySticks = 0;
while ( device )
{
nJoySticks++;
device = device->pNext;
}
return nJoySticks;
}
int
SDL_SYS_JoystickNeedsPolling()
{
return s_bDeviceAdded;
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
recDevice *device = gpDeviceList; recDevice *device = gpDeviceList;
int index; int index;
@ -1100,7 +1095,7 @@ JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
return device->guid; return device->guid;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick *joystick) JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
{ {
return joystick->hwdata->guid; return joystick->hwdata->guid;
} }

View file

@ -37,14 +37,34 @@ SDL_SYS_JoystickInit(void)
return (0); return (0);
} }
int SDL_SYS_NumJoysticks()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
SDL_SetError("Logic error: No joysticks available"); SDL_SetError("Logic error: No joysticks available");
return (NULL); return (NULL);
} }
/* 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. /* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick. 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. This should fill the nbuttons and naxes fields of the joystick structure.
@ -57,6 +77,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return (-1); return (-1);
} }
/* 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. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events * but instead should call SDL_PrivateJoystick*() to deliver events
@ -82,44 +108,18 @@ SDL_SYS_JoystickQuit(void)
return; return;
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 0;
}
int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -40,17 +40,31 @@ SDL_SYS_JoystickInit(void)
return (1); 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 */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
switch(index) { return accelerometerName;
case 0: }
return accelerometerName;
default: /* Function to perform the mapping from device index to the instance id for this index */
SDL_SetError("No joystick available with that index"); SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
return NULL; {
} return device_index;
} }
/* Function to open a joystick for use. /* Function to open a joystick for use.
@ -61,17 +75,18 @@ SDL_SYS_JoystickNameForIndex(int index)
int int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{ {
if (device_index == 0) { joystick->naxes = 3;
joystick->naxes = 3; joystick->nhats = 0;
joystick->nhats = 0; joystick->nballs = 0;
joystick->nballs = 0; joystick->nbuttons = 0;
joystick->nbuttons = 0; [[SDLUIAccelerationDelegate sharedDelegate] startup];
[[SDLUIAccelerationDelegate sharedDelegate] startup]; return 0;
return 0; }
} else {
SDL_SetError("No joystick available with that index"); /* Function to determine is this joystick is attached to the system right now */
return (-1); SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
} {
return SDL_TRUE;
} }
/* Function to update the state of a joystick - called as a device poll. /* Function to update the state of a joystick - called as a device poll.
@ -118,43 +133,17 @@ SDL_SYS_JoystickQuit(void)
return; return;
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -298,7 +298,7 @@ CountLogicalJoysticks(int max)
ret = 0; ret = 0;
for (i = 0; i < max; i++) { for (i = 0; i < max; i++) {
name = SDL_SYS_JoystickNameForIndex(i); name = SDL_SYS_JoystickNameForDeviceIndex(i);
fd = open(SDL_joylist[i].fname, O_RDONLY, 0); fd = open(SDL_joylist[i].fname, O_RDONLY, 0);
if (fd >= 0) { if (fd >= 0) {
@ -507,27 +507,41 @@ SDL_SYS_JoystickInit(void)
return (numjoysticks); return (numjoysticks);
} }
int SDL_SYS_NumJoysticks()
{
return SDL_SYS_numjoysticks;
}
void SDL_SYS_JoystickDetect()
{
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
int fd; int fd;
static char namebuf[128]; static char namebuf[128];
const char *name; const char *name;
SDL_logical_joydecl(int oindex = index); SDL_logical_joydecl(int oindex = device_index);
#ifndef NO_LOGICAL_JOYSTICKS #ifndef NO_LOGICAL_JOYSTICKS
SDL_joylist_head(index, index); SDL_joylist_head(device_index, device_index);
#endif #endif
name = NULL; name = NULL;
fd = open(SDL_joylist[index].fname, O_RDONLY, 0); fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0);
if (fd >= 0) { if (fd >= 0) {
if ( if (
#if SDL_INPUT_LINUXEV #if SDL_INPUT_LINUXEV
(ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) && (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) &&
#endif #endif
(ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0)) { (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0)) {
name = SDL_joylist[index].fname; name = SDL_joylist[device_index].fname;
} else { } else {
name = namebuf; name = namebuf;
} }
@ -536,7 +550,7 @@ SDL_SYS_JoystickNameForIndex(int index)
#ifndef NO_LOGICAL_JOYSTICKS #ifndef NO_LOGICAL_JOYSTICKS
if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next
|| index != oindex) { || device_index != oindex) {
LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128); LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128);
} }
#endif #endif
@ -544,6 +558,12 @@ SDL_SYS_JoystickNameForIndex(int index)
return name; return name;
} }
/* 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;
}
static int static int
allocate_hatdata(SDL_Joystick * joystick) allocate_hatdata(SDL_Joystick * joystick)
{ {
@ -604,7 +624,7 @@ JS_ConfigJoystick(SDL_Joystick * joystick, int fd)
joystick->nbuttons = n; joystick->nbuttons = n;
} }
name = SDL_SYS_JoystickNameForIndex(joystick->instance_id); name = SDL_SYS_JoystickNameForDeviceIndex(joystick->instance_id);
/* Generic analog joystick support */ /* Generic analog joystick support */
if (SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat")) { if (SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat")) {
@ -856,6 +876,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return (0); return (0);
} }
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
}
#ifndef NO_LOGICAL_JOYSTICKS #ifndef NO_LOGICAL_JOYSTICKS
static SDL_Joystick * static SDL_Joystick *
@ -1239,44 +1265,17 @@ SDL_SYS_JoystickQuit(void)
} }
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 SDL_SYS_numjoysticks;
}
int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -36,9 +36,6 @@
#include "../../video/nds/SDL_ndsevents_c.h" #include "../../video/nds/SDL_ndsevents_c.h"
/* Function to scan the system for joysticks. /* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/ */
int int
SDL_SYS_JoystickInit(void) SDL_SYS_JoystickInit(void)
@ -46,14 +43,31 @@ SDL_SYS_JoystickInit(void)
return (1); 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 */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
if (!index) return "NDS builtin joypad";
return "NDS builtin joypad"; }
SDL_SetError("No joystick available with that index");
return (NULL); /* 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. /* Function to open a joystick for use.
@ -71,6 +85,11 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return 0; return 0;
} }
/* 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. /* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly, * This function shouldn't update the joystick structure directly,
@ -167,44 +186,17 @@ SDL_SYS_JoystickQuit(void)
{ {
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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

View file

@ -84,7 +84,7 @@ struct JoyStick_DeviceData_
char *joystickname; char *joystickname;
Uint8 send_add_event; Uint8 send_add_event;
int nInstanceID; int nInstanceID;
Uint8 bXInputDevice; SDL_bool bXInputDevice;
Uint8 XInputUserId; Uint8 XInputUserId;
struct JoyStick_DeviceData_ *pNext; struct JoyStick_DeviceData_ *pNext;
}; };
@ -674,18 +674,215 @@ SDL_SYS_JoystickInit(void)
return SDL_SYS_NumJoysticks(); return SDL_SYS_NumJoysticks();
} }
/* return the number of joysticks that are connected right now */
int SDL_SYS_NumJoysticks()
{
int nJoysticks = 0;
JoyStick_DeviceData *device = SYS_Joystick;
while ( device )
{
nJoysticks++;
device = device->pNext;
}
return nJoysticks;
}
static int s_iNewGUID = 0;
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
JoyStick_DeviceData *pNewJoystick;
SDL_bool bXInputDevice;
pNewJoystick = *(JoyStick_DeviceData **)pContext;
while ( pNewJoystick )
{
if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
{
if ( SYS_Joystick )
{
pNewJoystick->pNext = SYS_Joystick;
}
SYS_Joystick = pNewJoystick;
/* if we are replacing the front of the list then update it */
if ( pNewJoystick == *(JoyStick_DeviceData **)pContext )
{
*(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
}
s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
s_iNewGUID++;
if ( s_iNewGUID < MAX_JOYSTICKS )
return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
else
return DIENUM_STOP;
}
pNewJoystick = pNewJoystick->pNext;
}
s_bDeviceAdded = SDL_TRUE;
bXInputDevice = IsXInputDevice( &pdidInstance->guidProduct );
pNewJoystick = (JoyStick_DeviceData *)SDL_malloc( sizeof(JoyStick_DeviceData) );
if ( bXInputDevice )
{
SDL_memset(&(pNewJoystick->dxdevice), 0x0,
sizeof(DIDEVICEINSTANCE));
pNewJoystick->bXInputDevice = SDL_TRUE;
pNewJoystick->XInputUserId = INVALID_XINPUT_USERID;
}
else
{
pNewJoystick->bXInputDevice = SDL_FALSE;
SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
sizeof(DIDEVICEINSTANCE));
}
pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
pNewJoystick->send_add_event = 1;
pNewJoystick->nInstanceID = ++s_nInstanceID;
SDL_memcpy( &pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid) );
pNewJoystick->pNext = NULL;
if ( SYS_Joystick )
{
pNewJoystick->pNext = SYS_Joystick;
}
SYS_Joystick = pNewJoystick;
s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
s_iNewGUID++;
if ( s_iNewGUID < MAX_JOYSTICKS )
return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
else
return DIENUM_STOP;
}
/* detect any new joysticks being inserted into the system */
void SDL_SYS_JoystickDetect()
{
HRESULT result;
JoyStick_DeviceData *pCurList = NULL;
/* only enum the devices if the joystick thread told us something changed */
if ( s_bDeviceAdded || s_bDeviceRemoved )
{
s_bDeviceAdded = SDL_FALSE;
s_bDeviceRemoved = SDL_FALSE;
pCurList = SYS_Joystick;
SYS_Joystick = NULL;
s_iNewGUID = 0;
SDL_mutexP( s_mutexJoyStickEnum );
if ( !s_pKnownJoystickGUIDs )
s_pKnownJoystickGUIDs = SDL_malloc( sizeof(GUID)*MAX_JOYSTICKS );
SDL_memset( s_pKnownJoystickGUIDs, 0x0, sizeof(GUID)*MAX_JOYSTICKS );
/* Look for joysticks, wheels, head trackers, gamepads, etc.. */
result = IDirectInput8_EnumDevices(dinput,
DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
&pCurList, DIEDFL_ATTACHEDONLY);
SDL_mutexV( s_mutexJoyStickEnum );
}
if ( pCurList )
{
while ( pCurList )
{
JoyStick_DeviceData *pListNext = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = pCurList->nInstanceID;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif // !SDL_EVENTS_DISABLED
pListNext = pCurList->pNext;
SDL_free(pCurList->joystickname);
SDL_free( pCurList );
pCurList = pListNext;
}
}
if ( s_bDeviceAdded )
{
JoyStick_DeviceData *pNewJoystick;
int device_index = 0;
s_bDeviceAdded = SDL_FALSE;
pNewJoystick = SYS_Joystick;
while ( pNewJoystick )
{
if ( pNewJoystick->send_add_event )
{
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
pNewJoystick->send_add_event = 0;
}
device_index++;
pNewJoystick = pNewJoystick->pNext;
}
}
}
/* we need to poll if we have pending hotplug device changes or connected devices */
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
/* we have a new device or one was pulled, we need to think this frame please */
if ( s_bDeviceAdded || s_bDeviceRemoved )
return SDL_TRUE;
return SDL_FALSE;
}
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
JoyStick_DeviceData *device = SYS_Joystick; JoyStick_DeviceData *device = SYS_Joystick;
for (; index > 0; index--) for (; device_index > 0; device_index--)
device = device->pNext; device = device->pNext;
return device->joystickname; return device->joystickname;
} }
/* Function to perform the mapping between current device instance and this joysticks instance id */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
JoyStick_DeviceData *device = SYS_Joystick;
int index;
for (index = device_index; index > 0; index--)
device = device->pNext;
return device->nInstanceID;
}
/* Function to open a joystick for use. /* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick. 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. This should fill the nbuttons and naxes fields of the joystick structure.
@ -907,6 +1104,13 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return (0); return (0);
} }
/* return true if this joystick is plugged in right now */
SDL_bool SDL_SYS_JoystickAttached( SDL_Joystick * joystick )
{
return joystick->closed == 0 && joystick->hwdata->removed == 0;
}
/* Sort using the data offset into the DInput struct. /* Sort using the data offset into the DInput struct.
* This gives a reasonable ordering for the inputs. */ * This gives a reasonable ordering for the inputs. */
static int static int
@ -1444,214 +1648,8 @@ SDL_SYS_JoystickQuit(void)
} }
/* Function to perform the mapping between current device instance and this joysticks instance id */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
JoyStick_DeviceData *device = SYS_Joystick;
int index;
for (index = device_index; index > 0; index--)
device = device->pNext;
return device->nInstanceID;
}
/* return true if this joystick is plugged in right now */
int SDL_SYS_JoystickAttached( SDL_Joystick * joystick )
{
return joystick->closed == 0 && joystick->hwdata->removed == 0;
}
/* return the number of joysticks that are connected right now */
int SDL_SYS_NumJoysticks()
{
int nJoysticks = 0;
JoyStick_DeviceData *device = SYS_Joystick;
while ( device )
{
nJoysticks++;
device = device->pNext;
}
return nJoysticks;
}
static int s_iNewGUID = 0;
/* helper function for direct input, gets called for each connected joystick */
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
JoyStick_DeviceData *pNewJoystick;
SDL_bool bXInputDevice;
pNewJoystick = *(JoyStick_DeviceData **)pContext;
while ( pNewJoystick )
{
if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
{
if ( SYS_Joystick )
{
pNewJoystick->pNext = SYS_Joystick;
}
SYS_Joystick = pNewJoystick;
/* if we are replacing the front of the list then update it */
if ( pNewJoystick == *(JoyStick_DeviceData **)pContext )
{
*(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
}
s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
s_iNewGUID++;
if ( s_iNewGUID < MAX_JOYSTICKS )
return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
else
return DIENUM_STOP;
}
pNewJoystick = pNewJoystick->pNext;
}
s_bDeviceAdded = SDL_TRUE;
bXInputDevice = IsXInputDevice( &pdidInstance->guidProduct );
pNewJoystick = (JoyStick_DeviceData *)SDL_malloc( sizeof(JoyStick_DeviceData) );
if ( bXInputDevice )
{
SDL_memset(&(pNewJoystick->dxdevice), 0x0,
sizeof(DIDEVICEINSTANCE));
pNewJoystick->bXInputDevice = 1;
pNewJoystick->XInputUserId = INVALID_XINPUT_USERID;
}
else
{
pNewJoystick->bXInputDevice = 0;
SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
sizeof(DIDEVICEINSTANCE));
}
pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
pNewJoystick->send_add_event = 1;
pNewJoystick->nInstanceID = ++s_nInstanceID;
SDL_memcpy( &pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid) );
pNewJoystick->pNext = NULL;
if ( SYS_Joystick )
{
pNewJoystick->pNext = SYS_Joystick;
}
SYS_Joystick = pNewJoystick;
s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
s_iNewGUID++;
if ( s_iNewGUID < MAX_JOYSTICKS )
return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
else
return DIENUM_STOP;
}
/* detect any new joysticks being inserted into the system */
void SDL_SYS_JoystickDetect()
{
HRESULT result;
JoyStick_DeviceData *pCurList = NULL;
/* only enum the devices if the joystick thread told us something changed */
if ( s_bDeviceAdded || s_bDeviceRemoved )
{
s_bDeviceAdded = SDL_FALSE;
s_bDeviceRemoved = SDL_FALSE;
pCurList = SYS_Joystick;
SYS_Joystick = NULL;
s_iNewGUID = 0;
SDL_mutexP( s_mutexJoyStickEnum );
if ( !s_pKnownJoystickGUIDs )
s_pKnownJoystickGUIDs = SDL_malloc( sizeof(GUID)*MAX_JOYSTICKS );
SDL_memset( s_pKnownJoystickGUIDs, 0x0, sizeof(GUID)*MAX_JOYSTICKS );
/* Look for joysticks, wheels, head trackers, gamepads, etc.. */
result = IDirectInput8_EnumDevices(dinput,
DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
&pCurList, DIEDFL_ATTACHEDONLY);
SDL_mutexV( s_mutexJoyStickEnum );
}
if ( pCurList )
{
while ( pCurList )
{
JoyStick_DeviceData *pListNext = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = pCurList->nInstanceID;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif // !SDL_EVENTS_DISABLED
pListNext = pCurList->pNext;
SDL_free(pCurList->joystickname);
SDL_free( pCurList );
pCurList = pListNext;
}
}
if ( s_bDeviceAdded )
{
JoyStick_DeviceData *pNewJoystick;
int device_index = 0;
s_bDeviceAdded = SDL_FALSE;
pNewJoystick = SYS_Joystick;
while ( pNewJoystick )
{
if ( pNewJoystick->send_add_event )
{
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
pNewJoystick->send_add_event = 0;
}
device_index++;
pNewJoystick = pNewJoystick->pNext;
}
}
}
/* we need to poll if we have pending hotplug device changes or connected devices */
int SDL_SYS_JoystickNeedsPolling()
{
/* we have a new device or one was pulled, we need to think this frame please */
if ( s_bDeviceAdded || s_bDeviceRemoved )
return 1;
return 0;
}
/* return the stable device guid for this device index */ /* return the stable device guid for this device index */
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
JoyStick_DeviceData *device = SYS_Joystick; JoyStick_DeviceData *device = SYS_Joystick;
int index; int index;
@ -1662,8 +1660,13 @@ JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
return device->guid; return device->guid;
} }
/* return 1 if this device is using XInput */ JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
int SDL_SYS_IsXInputDeviceIndex( int device_index ) {
return joystick->hwdata->guid;
}
/* return SDL_TRUE if this device is using XInput */
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
{ {
JoyStick_DeviceData *device = SYS_Joystick; JoyStick_DeviceData *device = SYS_Joystick;
int index; int index;
@ -1674,11 +1677,6 @@ int SDL_SYS_IsXInputDeviceIndex( int device_index )
return device->bXInputDevice; return device->bXInputDevice;
} }
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
{
return joystick->hwdata->guid;
}
#endif /* SDL_JOYSTICK_DINPUT */ #endif /* SDL_JOYSTICK_DINPUT */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -179,17 +179,37 @@ SDL_SYS_JoystickInit(void)
return (SDL_SYS_numjoysticks); return (SDL_SYS_numjoysticks);
} }
int SDL_SYS_NumJoysticks()
{
return SDL_SYS_numjoysticks;
}
void SDL_SYS_JoystickDetect()
{
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
}
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickNameForIndex(int index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
if (SYS_JoystickName[index] != NULL) { if (SYS_JoystickName[device_index] != NULL) {
return (SYS_JoystickName[index]); return (SYS_JoystickName[device_index]);
} else { } else {
return (SYS_Joystick[index].szPname); return (SYS_Joystick[device_index].szPname);
} }
} }
/* 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. /* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick. 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. This should fill the nbuttons and naxes fields of the joystick structure.
@ -253,6 +273,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
return (0); return (0);
} }
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
}
static Uint8 static Uint8
TranslatePOV(DWORD value) TranslatePOV(DWORD value)
{ {
@ -379,44 +405,17 @@ SDL_SYS_JoystickQuit(void)
} }
} }
/* Function to perform the mapping from device index to the instance id for this index */ JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_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 SDL_SYS_numjoysticks;
}
int SDL_SYS_JoystickNeedsPolling()
{
return 0;
}
void SDL_SYS_JoystickDetect()
{
}
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
{ {
JoystickGUID guid; 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_JoystickNameForIndex( device_index ); const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid ); SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid; return guid;
} }
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
{ {
JoystickGUID guid; 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
@ -472,4 +471,5 @@ SetMMerror(char *function, int code)
} }
#endif /* SDL_JOYSTICK_WINMM */ #endif /* SDL_JOYSTICK_WINMM */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */