From a9a03ca67d6519417afe50a97341cbea3cb714b2 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 27 Nov 2012 00:58:12 -0800 Subject: [PATCH] Organized joystick hotplug code a bit. Cleaned up names, return types, etc. --- include/SDL_joystick.h | 21 +- src/joystick/SDL_joystick.c | 23 +- src/joystick/SDL_joystick_c.h | 2 +- src/joystick/SDL_sysjoystick.h | 49 ++- src/joystick/android/SDL_sysjoystick.c | 68 ++-- src/joystick/beos/SDL_bejoystick.cc | 63 ++-- src/joystick/bsd/SDL_sysjoystick.c | 68 ++-- src/joystick/darwin/SDL_sysjoystick.c | 183 +++++----- src/joystick/dummy/SDL_sysjoystick.c | 60 ++-- src/joystick/iphoneos/SDL_sysjoystick.m | 85 ++--- src/joystick/linux/SDL_sysjoystick.c | 75 ++--- src/joystick/nds/SDL_sysjoystick.c | 68 ++-- src/joystick/windows/SDL_dxjoystick.c | 430 ++++++++++++------------ src/joystick/windows/SDL_mmjoystick.c | 68 ++-- 14 files changed, 612 insertions(+), 651 deletions(-) diff --git a/include/SDL_joystick.h b/include/SDL_joystick.h index 8fd75a03e..aa719faa3 100644 --- a/include/SDL_joystick.h +++ b/include/SDL_joystick.h @@ -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); diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 1149795a0..6baae4540 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -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 */ diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h index b647c7b15..221fffa47 100644 --- a/src/joystick/SDL_joystick_c.h +++ b/src/joystick/SDL_joystick_c.h @@ -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); diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h index 028855b8b..f0183e017 100644 --- a/src/joystick/SDL_sysjoystick.h +++ b/src/joystick/SDL_sysjoystick.h @@ -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: */ diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c index 30a423da1..d2d94dcaa 100644 --- a/src/joystick/android/SDL_sysjoystick.c +++ b/src/joystick/android/SDL_sysjoystick.c @@ -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: */ diff --git a/src/joystick/beos/SDL_bejoystick.cc b/src/joystick/beos/SDL_bejoystick.cc index 289c3fd7e..a76e23ab3 100755 --- a/src/joystick/beos/SDL_bejoystick.cc +++ b/src/joystick/beos/SDL_bejoystick.cc @@ -85,10 +85,30 @@ extern "C" 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 */ - const char *SDL_SYS_JoystickNameForIndex(int index) + 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 SDL_joyname[index]; + return device_index; } /* Function to open a joystick for use. @@ -141,6 +161,12 @@ extern "C" 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. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events @@ -235,44 +261,17 @@ extern "C" SDL_joyname[0] = NULL; } -/* 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 SDL_SYS_numjoysticks; - } - - 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 diff --git a/src/joystick/bsd/SDL_sysjoystick.c b/src/joystick/bsd/SDL_sysjoystick.c index 18be0f3e1..e090e28db 100644 --- a/src/joystick/bsd/SDL_sysjoystick.c +++ b/src/joystick/bsd/SDL_sysjoystick.c @@ -157,7 +157,7 @@ static void report_free(struct report *); #define REP_BUF_DATA(rep) ((rep)->buf->data) #endif -int SDL_SYS_numjoysticks = 0; +static int SDL_SYS_numjoysticks = 0; int SDL_SYS_JoystickInit(void) @@ -200,13 +200,33 @@ SDL_SYS_JoystickInit(void) return (SDL_SYS_numjoysticks); } +int SDL_SYS_NumJoysticks() +{ + return SDL_SYS_numjoysticks; +} + +void SDL_SYS_JoystickDetect() +{ +} + +SDL_bool SDL_SYS_JoystickNeedsPolling() +{ + return SDL_FALSE; +} + const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { - if (joydevnames[index] != NULL) { - return (joydevnames[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 @@ -404,6 +424,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index) 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 SDL_SYS_JoystickUpdate(SDL_Joystick * joy) { @@ -558,43 +584,17 @@ SDL_SYS_JoystickQuit(void) return; } -/* 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 SDL_SYS_numjoysticks; -} - -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 diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c index c80c499e2..b0f0a9837 100644 --- a/src/joystick/darwin/SDL_sysjoystick.c +++ b/src/joystick/darwin/SDL_sysjoystick.c @@ -63,7 +63,7 @@ static recDevice *gpDeviceList = NULL; /* OSX reference to the notification object that tells us about device insertion/removal */ IONotificationPortRef notificationPort = 0; /* 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 int s_joystick_instance_id = -1; @@ -674,7 +674,7 @@ AddDeviceHelper( io_object_t ioHIDDeviceObject ) } device->send_open_event = 1; - s_bDeviceAdded = 1; + s_bDeviceAdded = SDL_TRUE; /* Add device to the end of the list */ if ( !gpDeviceList ) @@ -805,18 +805,89 @@ SDL_SYS_JoystickInit(void) 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 */ const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { recDevice *device = gpDeviceList; - for (; index > 0; index--) + for (; device_index > 0; device_index--) device = device->pNext; 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. * 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. @@ -842,58 +913,26 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) return 0; } - -/* Function to return the instance id of the joystick at device_index +/* Function to query if the joystick is currently attached + * It returns 1 if attached, 0 otherwise. */ -SDL_JoystickID -SDL_SYS_GetInstanceIdOfDeviceIndex( int device_index ) +SDL_bool +SDL_SYS_JoystickAttached(SDL_Joystick * joystick) { - recDevice *device = gpDeviceList; + recDevice *device = gpDeviceList; int index; - for (index = device_index; index > 0; index--) - device = device->pNext; - - return device->instance_id; -} - - -/* Function to cause any queued joystick insertions to be processed - */ -void -SDL_SYS_JoystickDetect() -{ - if ( s_bDeviceAdded ) + while ( device ) { - recDevice *device = gpDeviceList; - s_bDeviceAdded = 0; - 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; - } + if ( joystick->instance_id == device->instance_id ) + return SDL_TRUE; + + device = device->pNext; } + + return SDL_FALSE; } - /* Function to update the state of a joystick - called as a device poll. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events @@ -1023,28 +1062,6 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) 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 */ void 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*/ -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 ) +JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) { recDevice *device = gpDeviceList; int index; @@ -1100,7 +1095,7 @@ JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) return device->guid; } -JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick *joystick) +JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick) { return joystick->hwdata->guid; } diff --git a/src/joystick/dummy/SDL_sysjoystick.c b/src/joystick/dummy/SDL_sysjoystick.c index 690a98539..54e692a0e 100644 --- a/src/joystick/dummy/SDL_sysjoystick.c +++ b/src/joystick/dummy/SDL_sysjoystick.c @@ -37,14 +37,34 @@ SDL_SYS_JoystickInit(void) 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 */ const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { SDL_SetError("Logic error: No joysticks available"); 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. 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. @@ -57,6 +77,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events @@ -82,44 +108,18 @@ SDL_SYS_JoystickQuit(void) return; } -/* 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 0; -} - -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 diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m index 5c5d66618..a8e11dd62 100755 --- a/src/joystick/iphoneos/SDL_sysjoystick.m +++ b/src/joystick/iphoneos/SDL_sysjoystick.m @@ -40,17 +40,31 @@ 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) { - switch(index) { - case 0: - return accelerometerName; - default: - 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. @@ -61,17 +75,18 @@ int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) { - if (device_index == 0) { - joystick->naxes = 3; - joystick->nhats = 0; - joystick->nballs = 0; - joystick->nbuttons = 0; - [[SDLUIAccelerationDelegate sharedDelegate] startup]; - return 0; - } else { - SDL_SetError("No joystick available with that index"); - return (-1); - } + joystick->naxes = 3; + joystick->nhats = 0; + joystick->nballs = 0; + joystick->nbuttons = 0; + [[SDLUIAccelerationDelegate sharedDelegate] startup]; + 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. @@ -118,43 +133,17 @@ return; } -/* 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 diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index 8c6c0a3e4..933864862 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -298,7 +298,7 @@ CountLogicalJoysticks(int max) ret = 0; 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); if (fd >= 0) { @@ -507,27 +507,41 @@ SDL_SYS_JoystickInit(void) 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 */ const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { int fd; static char namebuf[128]; const char *name; - SDL_logical_joydecl(int oindex = index); + SDL_logical_joydecl(int oindex = device_index); #ifndef NO_LOGICAL_JOYSTICKS - SDL_joylist_head(index, index); + SDL_joylist_head(device_index, device_index); #endif 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 ( #if SDL_INPUT_LINUXEV (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) && #endif (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0)) { - name = SDL_joylist[index].fname; + name = SDL_joylist[device_index].fname; } else { name = namebuf; } @@ -536,7 +550,7 @@ SDL_SYS_JoystickNameForIndex(int index) #ifndef NO_LOGICAL_JOYSTICKS if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next - || index != oindex) { + || device_index != oindex) { LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128); } #endif @@ -544,6 +558,12 @@ SDL_SYS_JoystickNameForIndex(int index) 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 allocate_hatdata(SDL_Joystick * joystick) { @@ -604,7 +624,7 @@ JS_ConfigJoystick(SDL_Joystick * joystick, int fd) joystick->nbuttons = n; } - name = SDL_SYS_JoystickNameForIndex(joystick->instance_id); + name = SDL_SYS_JoystickNameForDeviceIndex(joystick->instance_id); /* Generic analog joystick support */ 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); } +/* 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 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 */ -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 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 diff --git a/src/joystick/nds/SDL_sysjoystick.c b/src/joystick/nds/SDL_sysjoystick.c index e7fa3bb49..9e64a3bd0 100644 --- a/src/joystick/nds/SDL_sysjoystick.c +++ b/src/joystick/nds/SDL_sysjoystick.c @@ -36,9 +36,6 @@ #include "../../video/nds/SDL_ndsevents_c.h" /* 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 SDL_SYS_JoystickInit(void) @@ -46,14 +43,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) +{ + return "NDS builtin joypad"; +} + +/* Function to perform the mapping from device index to the instance id for this index */ +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index) { - if (!index) - return "NDS builtin joypad"; - SDL_SetError("No joystick available with that index"); - return (NULL); + return device_index; } /* Function to open a joystick for use. @@ -71,6 +85,11 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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. * 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 */ -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 diff --git a/src/joystick/windows/SDL_dxjoystick.c b/src/joystick/windows/SDL_dxjoystick.c index ba3d52c38..df1a8f44b 100644 --- a/src/joystick/windows/SDL_dxjoystick.c +++ b/src/joystick/windows/SDL_dxjoystick.c @@ -84,7 +84,7 @@ struct JoyStick_DeviceData_ char *joystickname; Uint8 send_add_event; int nInstanceID; - Uint8 bXInputDevice; + SDL_bool bXInputDevice; Uint8 XInputUserId; struct JoyStick_DeviceData_ *pNext; }; @@ -674,18 +674,215 @@ SDL_SYS_JoystickInit(void) 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 */ const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { JoyStick_DeviceData *device = SYS_Joystick; - for (; index > 0; index--) + for (; device_index > 0; device_index--) device = device->pNext; 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. 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. @@ -907,6 +1104,13 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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. * This gives a reasonable ordering for the inputs. */ static int @@ -1444,8 +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) +/* return the stable device guid for this device index */ +JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) { JoyStick_DeviceData *device = SYS_Joystick; int index; @@ -1453,217 +1657,16 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_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 device->guid; } -/* return the stable device guid for this device index */ -JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index ) +JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick) { - JoyStick_DeviceData *device = SYS_Joystick; - int index; - - for (index = device_index; index > 0; index--) - device = device->pNext; - - return device->guid; + return joystick->hwdata->guid; } -/* return 1 if this device is using XInput */ -int SDL_SYS_IsXInputDeviceIndex( int device_index ) +/* return SDL_TRUE if this device is using XInput */ +SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index) { JoyStick_DeviceData *device = SYS_Joystick; int index; @@ -1674,11 +1677,6 @@ int SDL_SYS_IsXInputDeviceIndex( int device_index ) return device->bXInputDevice; } -JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick) -{ - return joystick->hwdata->guid; -} - #endif /* SDL_JOYSTICK_DINPUT */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/windows/SDL_mmjoystick.c b/src/joystick/windows/SDL_mmjoystick.c index e2c2531c7..90e98fe5c 100644 --- a/src/joystick/windows/SDL_mmjoystick.c +++ b/src/joystick/windows/SDL_mmjoystick.c @@ -179,17 +179,37 @@ SDL_SYS_JoystickInit(void) 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 */ const char * -SDL_SYS_JoystickNameForIndex(int index) +SDL_SYS_JoystickNameForDeviceIndex(int device_index) { - if (SYS_JoystickName[index] != NULL) { - return (SYS_JoystickName[index]); + if (SYS_JoystickName[device_index] != NULL) { + return (SYS_JoystickName[device_index]); } 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. 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. @@ -253,6 +273,12 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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 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 */ -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 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 @@ -472,4 +471,5 @@ SetMMerror(char *function, int code) } #endif /* SDL_JOYSTICK_WINMM */ + /* vi: set ts=4 sw=4 expandtab: */